/** * Subclasses must override to return an appropriately sparse or dense result * * @param rows the row cardinality * @param columns the column cardinality * @return a Matrix */ @Override protected Matrix matrixLike(int rows, int columns) { return new DenseMatrix(rows, columns); }
/** * Returns an empty matrix of the same underlying class as the receiver and of the specified * size. * * @param rows the int number of rows * @param columns the int number of columns */ @Override public Matrix like(int rows, int columns) { return new DenseMatrix(rows, columns); }
/** * Returns an empty matrix of the same underlying class as the receiver and of the specified * size. * * @param rows the int number of rows * @param columns the int number of columns */ @Override public Matrix like(int rows, int columns) { return new DenseMatrix(rows, columns); }
/** * Returns an empty matrix of the same underlying class as the receiver and of the specified size. * * @param rows the int number of rows * @param columns the int number of columns */ @Override public Matrix like(int rows, int columns) { return new DenseMatrix(rows, columns); }
static Matrix createMiIi(Iterable<Vector> featureVectors, int numFeatures) { double[][] MiIi = new double[numFeatures][Iterables.size(featureVectors)]; int n = 0; for (Vector featureVector : featureVectors) { for (int m = 0; m < numFeatures; m++) { MiIi[m][n] = featureVector.getQuick(m); } n++; } return new DenseMatrix(MiIi, true); }
@Override public Matrix like(int rows, int columns) { if (denseLike) return new DenseMatrix(rows, columns); else return new SparseMatrix(rows, columns); }
public static Matrix createDiagonalMatrix(double[] diagonal, int rows, int columns) { double[][] dData = new double[rows][columns]; for (int i = 0; i < Math.min(rows, columns); ++i) { dData[i][i] = diagonal[i]; } return new DenseMatrix(dData); }
/** * Return an empty matrix of the same underlying class as the receiver * * @return a Matrix */ @Override public Matrix like() { return new DenseMatrix(rowSize(), columnSize()); }
private static Matrix reshape(double[] values, int rows, int columns) { Matrix m = new DenseMatrix(rows, columns); int i = 0; for (double v : values) { m.set(i % rows, i / rows, v); i++; } return m; } }
private static Matrix reshape(double[] values, int rows, int columns) { Matrix m = new DenseMatrix(rows, columns); int i = 0; for (double v : values) { m.set(i % rows, i / rows, v); i++; } return m; }
private static Matrix reshape(double[] values, int rows, int columns) { Matrix m = new DenseMatrix(rows, columns); int i = 0; for (double v : values) { m.set(i % rows, i / rows, v); i++; } return m; }
private Matrix columnVectorAsMatrix(Vector v) { double[][] matrix = new double[numFeatures][1]; for (Vector.Element e : v.all()) { matrix[e.index()][0] = e.get(); } return new DenseMatrix(matrix, true); }
/** test A = USVt */ @Test public void testAEqualUSVt() { checkAEqualUSVt(new DenseMatrix(testSquare)); checkAEqualUSVt(new DenseMatrix(testNonSquare)); checkAEqualUSVt(new DenseMatrix(testNonSquare).transpose()); }
/** test that U is orthogonal */ @Test public void testUOrthogonal() { checkOrthogonal(new SingularValueDecomposition(new DenseMatrix(testSquare)).getU()); checkOrthogonal(new SingularValueDecomposition(new DenseMatrix(testNonSquare)).getU()); checkOrthogonal(new SingularValueDecomposition(new DenseMatrix(testNonSquare).transpose()).getU()); }
/** test that V is orthogonal */ @Test public void testVOrthogonal() { checkOrthogonal(new SingularValueDecomposition(new DenseMatrix(testSquare)).getV()); checkOrthogonal(new SingularValueDecomposition(new DenseMatrix(testNonSquare)).getV()); checkOrthogonal(new SingularValueDecomposition(new DenseMatrix(testNonSquare).transpose()).getV()); }
@Test public void testTransposeView() { Matrix m = Matrices.gaussianView(5, 6, 1234L); Matrix controlM = new DenseMatrix(5, 6).assign(m); System.out.printf("M=\n%s\n", m); System.out.printf("controlM=\n%s\n", controlM); Matrix mtm = Matrices.transposedView(m).times(m); Matrix controlMtm = controlM.transpose().times(controlM); System.out.printf("M'M=\n%s\n", mtm); Matrix diff = mtm.minus(controlMtm); assertEquals(0, diff.aggregate(Functions.PLUS, Functions.ABS), 1e-10); }
@Override public Matrix matrixFactory(double[][] values) { Matrix base = new DenseMatrix(values); // for general tests, we just make a scrambled matrix and fill it // with the standard data. Then we can test the details of the // row and/or column swapping separately. PivotedMatrix pm = new PivotedMatrix(base.like()); pm.swap(0, 1); pm.swapRows(1, 2); pm.assign(base); return pm; }
public static void checkOrthogonal(Matrix m) { Matrix mTm = m.transpose().times(m); Matrix id = new DenseMatrix(mTm.numRows(),mTm.numRows()); for (int i = 0; i < mTm.numRows(); i++) { id.set(i, i, 1); } assertEquals(0, Algebra.getNorm(mTm.minus(id)), NORM_TOLERANCE); }
@Test public void testEigen() { Matrix a = new DenseSymmetricMatrix(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false); Matrix b = new DenseMatrix(a.numRows(), a.numCols()); b.assign(a); assertEquals(0, a.minus(b).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); EigenDecomposition edA = new EigenDecomposition(a); EigenDecomposition edB = new EigenDecomposition(b); System.out.println(edA.getV()); assertEquals(0, edA.getV().minus(edB.getV()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, edA.getRealEigenvalues().minus(edA.getRealEigenvalues()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); }
@Test public void randomMatrix() { Matrix a = new DenseMatrix(60, 60).assign(Functions.random()); QRDecomposition qr = new QRDecomposition(a); // how close is Q to actually being orthornormal? double maxIdent = qr.getQ().transpose().times(qr.getQ()).viewDiagonal().assign(Functions.plus(-1)).norm(1); assertEquals(0, maxIdent, 1.0e-13); // how close is Q R to the original value of A? Matrix z = qr.getQ().times(qr.getR()).minus(a); double maxError = z.aggregate(Functions.MIN, Functions.ABS); assertEquals(0, maxError, 1.0e-13); }