List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension
int getRowDimension();
From source file:net.myrrix.online.factorizer.als.AlternatingLeastSquaresTest.java
private static RealMatrix buildTestXYTProduct(boolean reconstructR) throws ExecutionException, InterruptedException { System.setProperty("model.reconstructRMatrix", Boolean.toString(reconstructR)); FastByIDMap<FastByIDFloatMap> byRow = new FastByIDMap<FastByIDFloatMap>(); FastByIDMap<FastByIDFloatMap> byCol = new FastByIDMap<FastByIDFloatMap>(); // Octave: R = [ 0 2 3 1 0 ; 0 0 4 5 0 ; 1 0 0 0 2 ; 3 0 1 0 5 ; 0 2 2 2 0 ] MatrixUtils.addTo(0, 1, 2.0f, byRow, byCol); MatrixUtils.addTo(0, 2, 3.0f, byRow, byCol); MatrixUtils.addTo(0, 3, 1.0f, byRow, byCol); MatrixUtils.addTo(1, 2, 4.0f, byRow, byCol); MatrixUtils.addTo(1, 3, 5.0f, byRow, byCol); MatrixUtils.addTo(2, 0, 1.0f, byRow, byCol); MatrixUtils.addTo(2, 4, 2.0f, byRow, byCol); MatrixUtils.addTo(3, 0, 3.0f, byRow, byCol); MatrixUtils.addTo(3, 2, 1.0f, byRow, byCol); MatrixUtils.addTo(3, 4, 5.0f, byRow, byCol); MatrixUtils.addTo(4, 1, 2.0f, byRow, byCol); MatrixUtils.addTo(4, 2, 2.0f, byRow, byCol); MatrixUtils.addTo(4, 3, 2.0f, byRow, byCol); // Octave: Y = [ 0.1 0.2 ; 0.2 0.5 ; 0.3 0.1 ; 0.2 0.2 ; 0.5 0.4 ]; FastByIDMap<float[]> previousY = new FastByIDMap<float[]>(); previousY.put(0L, new float[] { 0.1f, 0.2f }); previousY.put(1L, new float[] { 0.2f, 0.5f }); previousY.put(2L, new float[] { 0.3f, 0.1f }); previousY.put(3L, new float[] { 0.2f, 0.2f }); previousY.put(4L, new float[] { 0.5f, 0.4f }); MatrixFactorizer als = new AlternatingLeastSquares(byRow, byCol, 2, 0.0001, 40); als.setPreviousY(previousY);//from ww w .j av a2 s . c o m als.call(); RealMatrix product = MatrixUtils.multiplyXYT(als.getX(), als.getY()); StringBuilder productString = new StringBuilder(100); for (int row = 0; row < product.getRowDimension(); row++) { productString.append('\n').append(Arrays.toString(doubleToFloatArray(product.getRow(row)))); } log.info("{}", productString); return product; }
From source file:ch.zhaw.iamp.rct.weights.Weights.java
/** * Calculates a pseudo-inverse using the values in the given files. They are * first read, then converted to a matrix, and finally used for the * calculation of the inverse, using Singular value decomposition (SVD). * * @param pathToA The file which contains the matrix A, represented in * comma-separated-value format.//w w w .j av a2 s . co m * @param targetTrajectoryFile The file which contains the target * trajectory, represented in comma-separated-value format. * @param weightsFile The file, to which the calculated weights should be * written to. * @param offset The numbers of first steps to ignore (to skip fading-memory * initialization steps). */ public static void calculateWeights(final String pathToA, final String targetTrajectoryFile, final String weightsFile, final int offset) { try { RealMatrix A = csvToMatrix(pathToA); // cut first n elements A = A.getSubMatrix(offset, A.getRowDimension() - 1, 0, A.getColumnDimension() - 1); A = addNoise(A); RealMatrix b = csvToMatrix(targetTrajectoryFile); // adjust b to cutting int n = offset % b.getRowDimension(); if (n > 0) { RealMatrix tmp = b.getSubMatrix(n, b.getRowDimension() - 1, 0, b.getColumnDimension() - 1); b = b.getSubMatrix(0, n - 1, 0, b.getColumnDimension() - 1); double[][] tmpArray = tmp.getData(); double[][] tmpArray2 = b.getData(); b = MatrixUtils.createRealMatrix(concat(tmpArray, tmpArray2)); tmpArray = b.getData(); for (int i = 0; tmpArray.length < A.getRowDimension(); ++i) { tmpArray2 = new double[1][tmpArray[0].length]; for (int j = 0; j < tmpArray[i].length; ++j) { tmpArray2[0][j] = tmpArray[i][j]; } tmpArray = concat(tmpArray, tmpArray2); } b = MatrixUtils.createRealMatrix(tmpArray); } DecompositionSolver solver = new SingularValueDecomposition(A).getSolver(); RealMatrix x = solver.solve(b).transpose(); matrixToCsv(x, weightsFile); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Could not read a file: " + ex.getMessage(), "File Error", JOptionPane.ERROR_MESSAGE); Logger.getLogger(Weights.class.getName()).log(Level.WARNING, "Could not read a file: {0}", ex); } catch (DimensionMismatchException ex) { JOptionPane.showMessageDialog(null, "<html>Could not calculate the " + "pseudo-inverse since a dimension mismatch occurred.<br />" + "Please make sure that all lines of the CSV file posses " + "the same amount of entries.<br />Hint: Remove the last " + "line and try it again.</html>", "Matrix Dimension Mismatch", JOptionPane.ERROR_MESSAGE); Logger.getLogger(Weights.class.getName()).log(Level.WARNING, "A dimension mismatch occurred: {0}", ex); } }
From source file:ch.zhaw.iamp.rct.weights.Weights.java
private static void matrixToCsv(final RealMatrix matrix, final String targetFilePath) throws IOException { Writer out = new FileWriter(targetFilePath); for (int i = 0; i < matrix.getRowDimension(); ++i) { for (int j = 0; j < matrix.getColumnDimension(); ++j) { out.append("" + matrix.getEntry(i, j)); if (j < matrix.getColumnDimension() - 1) { out.append(","); }/*from w ww . j a v a 2s .c o m*/ } out.append("\n"); out.flush(); } }
From source file:edu.oregonstate.eecs.mcplan.util.Csv.java
public static void write(final PrintStream out, final RealMatrix m) { final Writer writer = new Writer(out); for (int i = 0; i < m.getRowDimension(); ++i) { for (int j = 0; j < m.getColumnDimension(); ++j) { writer.cell(m.getEntry(i, j)); }/*from ww w .j a v a 2 s . com*/ writer.newline(); } }
From source file:com.yahoo.egads.utilities.SpectralMethods.java
public static RealMatrix createHankelMatrix(RealMatrix data, int windowSize) { int n = data.getRowDimension(); int m = data.getColumnDimension(); int k = n - windowSize + 1; RealMatrix res = MatrixUtils.createRealMatrix(k, m * windowSize); double[] buffer = {}; for (int i = 0; i < n; ++i) { double[] row = data.getRow(i); buffer = ArrayUtils.addAll(buffer, row); if (i >= windowSize - 1) { RealMatrix mat = MatrixUtils.createRowRealMatrix(buffer); res.setRowMatrix(i - windowSize + 1, mat); buffer = ArrayUtils.subarray(buffer, m, buffer.length); }//from w w w. j av a2s. co m } return res; }
From source file:edu.byu.nlp.stats.DirichletDistribution.java
/** Returns an array of logs of samples from the specified Dirichlet distributions. * Similar to calling:/*w w w . j a va 2 s . co m*/ * <pre> * double[][] thetas = sample(alpha, rnd); * for (int k = 0; k < thetas.length; k++) { * DoubleArrays.logToSelf(thetas[k]); * } * </pre> * EXCEPT that precision is better preserved. **/ public static double[][] logSample(RealMatrix alphas, RandomGenerator rnd) { double[][] thetas = new double[alphas.getRowDimension()][]; for (int i = 0; i < thetas.length; i++) { thetas[i] = logSample(alphas.getRowVector(i), rnd); } return thetas; }
From source file:edu.mit.genecircuits.GcUtils.java
/** Convert dense Apache Commons to Colt matrix */ static public DoubleMatrix2D apache2colt(RealMatrix apache) { // Convert apache commons matrix to colt DoubleMatrix2D colt = new DenseDoubleMatrix2D(apache.getRowDimension(), apache.getColumnDimension()); for (int i = 0; i < apache.getRowDimension(); i++) for (int j = 0; j < apache.getColumnDimension(); j++) colt.set(i, j, apache.getEntry(i, j)); return colt;// w ww . j av a 2s. c o m }
From source file:com.yahoo.egads.utilities.SpectralMethods.java
public static RealMatrix averageHankelMatrix(RealMatrix hankelMat, int windowSize) { int k = hankelMat.getRowDimension(); int m = hankelMat.getColumnDimension() / windowSize; int n = k + windowSize - 1; RealMatrix result = MatrixUtils.createRealMatrix(n, m); for (int t = 0; t < n; ++t) { int i = (t < windowSize) ? 0 : (t - windowSize + 1); int j = (t < windowSize) ? t : (windowSize - 1); int counter = 0; for (; i < k && j >= 0; ++i, --j, ++counter) { for (int h = 0; h < m; ++h) { result.addToEntry(t, h, hankelMat.getEntry(i, j * m + h)); }// w w w . java 2 s . c o m } for (int h = 0; h < m; ++h) { result.setEntry(t, h, result.getEntry(t, h) / counter); } } return result; }
From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java
/** * Returns the standard deviations of rows. * * @param matrix The matrix of which the standard deviations of rows to be computed * @return A double array of row standard deviations. *//*from www . j a v a 2 s. co m*/ public static double[] rowStdDevs(RealMatrix matrix) { double[] retval = new double[matrix.getRowDimension()]; for (int i = 0; i < retval.length; i++) { retval[i] = new DescriptiveStatistics(matrix.getRow(i)).getStandardDeviation(); } return retval; }
From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java
/** * Returns the column range from the matrix as a new matrix. * * @param matrix The input matrix//from www . j av a 2 s . c o m * @param start The index of the column to start with (inclusive) * @param end The index of the column to end with (inclusive) * @return A new matrix with columns specified */ public static RealMatrix getColumRange(RealMatrix matrix, int start, int end) { return matrix.getSubMatrix(0, matrix.getRowDimension() - 1, start, end); }