List of usage examples for org.apache.commons.math.linear RealVector toArray
double[] toArray();
From source file:com.zinnia.nectar.util.math.MatrixSolver.java
public static double[] solveMatrix(double[][] coefficientMatrix, double[] rhsVector) { RealVector x = null; try {//from w w w . j a v a 2s . c o m RealMatrix a = new Array2DRowRealMatrix(coefficientMatrix); RealVector b = new ArrayRealVector(rhsVector); DecompositionSolver solver = new LUDecompositionImpl(a).getSolver(); x = solver.solve(b); } catch (Exception e) { e.printStackTrace(); } double[] result; result = x.toArray(); return result; }
From source file:juicebox.data.MatrixZoomData.java
/** * Computes eigenvector from Pearson's.//from www . ja v a 2s . co m * * @param df Expected values, needed to get Pearson's * @param which Which eigenvector; 0 is principal. * @return Eigenvector */ public double[] computeEigenvector(ExpectedValueFunction df, int which) { BasicMatrix pearsons = getPearsons(df); if (pearsons == null) { return null; } int dim = pearsons.getRowDimension(); double[][] data = new double[dim][dim]; BitSet bitSet = new BitSet(dim); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { float tmp = pearsons.getEntry(i, j); data[i][j] = tmp; if (data[i][j] != 0 && !Float.isNaN(tmp)) { bitSet.set(i); } } } int[] nonCentromereColumns = new int[bitSet.cardinality()]; int count = 0; for (int i = 0; i < dim; i++) { if (bitSet.get(i)) nonCentromereColumns[count++] = i; } RealMatrix subMatrix = new Array2DRowRealMatrix(data).getSubMatrix(nonCentromereColumns, nonCentromereColumns); RealVector rv = (new EigenDecompositionImpl(subMatrix, 0)).getEigenvector(which); double[] ev = rv.toArray(); int size = pearsons.getColumnDimension(); double[] eigenvector = new double[size]; int num = 0; for (int i = 0; i < size; i++) { if (num < nonCentromereColumns.length && i == nonCentromereColumns[num]) { eigenvector[i] = ev[num]; num++; } else { eigenvector[i] = Double.NaN; } } return eigenvector; }