public Matrix getU() { // U = (Y inv(R)) U_0 return cd1.solveRight(y).times(svd.getU()); }
@Test public void testTimesSquaredTimesVector() { Vector vectorA = new DenseVector(vectorAValues); Vector ttA = test.timesSquared(vectorA); Vector ttASlow = test.transpose().times(test.times(vectorA)); assertTrue("M'Mv != M.timesSquared(v): " + ttA + " != " + ttASlow, ttASlow.minus(ttA).norm(2) < 1.0e-12); }
public SequentialBigSvd(Matrix A, int p) { // Y = A * \Omega y = A.times(new RandomTrinaryMatrix(A.columnSize(), p)); // R'R = Y' Y cd1 = new CholeskyDecomposition(y.transpose().times(y)); // B = Q" A = (Y R^{-1} )' A b = cd1.solveRight(y).transpose().times(A); // L L' = B B' cd2 = new CholeskyDecomposition(b.times(b.transpose())); // U_0 D V_0' = L svd = new SingularValueDecomposition(cd2.getL()); }
public Matrix getV() { // V = (B' inv(L')) V_0 return cd2.solveRight(b.transpose()).times(svd.getV()); } }
@Test(expected = CardinalityException.class) public void testTimesMatrixCardinality() { Matrix other = test.like(5, 8); test.times(other); }
@Test(expected = CardinalityException.class) public void testTimesMatrixCardinality() { Matrix other = test.like(5, 8); test.times(other); }
@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); }
@Test(expected = CardinalityException.class) public void testTimesVector() { Vector vectorA = new DenseVector(vectorAValues); Vector testTimesVectorA = test.times(vectorA); Vector expected = new DenseVector(new double[]{5.0, 11.0, 17.0}); assertTrue("Matrix times vector not equals: " + vectorA + " != " + testTimesVectorA, expected.minus(testTimesVectorA).norm(2) < 1.0e-12); test.times(testTimesVectorA); }
@Test public void testSvdHang() throws IOException, InterruptedException, ExecutionException, TimeoutException { System.out.printf("starting hanging-svd\n"); final Matrix m = readTsv("hanging-svd.tsv"); SingularValueDecomposition svd = new SingularValueDecomposition(m); assertEquals(0, m.minus(svd.getU().times(svd.getS()).times(svd.getV().transpose())).aggregate(Functions.PLUS, Functions.ABS), 1e-10); System.out.printf("No hang\n"); }
@Test public void testTimesDouble() { Matrix value = test.times(4.53); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row][col] * 4.53, value.getQuick(row, col), EPSILON); } } }
@Test public void testTimesDouble() { Matrix value = test.times(4.53); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row + 1][col + 1] * 4.53, value.getQuick(row, col), EPSILON); } } }
@Test public void testSingularValues() { Matrix A = lowRankMatrix(); SequentialBigSvd s = new SequentialBigSvd(A, 8); SingularValueDecomposition svd = new SingularValueDecomposition(A); Vector reference = new DenseVector(svd.getSingularValues()).viewPart(0, 8); assertEquals(reference, s.getSingularValues()); assertEquals(A, s.getU().times(new DiagonalMatrix(s.getSingularValues())).times(s.getV().transpose())); }
@Test public void testConjugateGradientSolver() { Matrix a = getA(); Vector b = getB(); ConjugateGradientSolver solver = new ConjugateGradientSolver(); Vector x = solver.solve(a, b); assertEquals(0.0, Math.sqrt(a.times(x).getDistanceSquared(b)), EPSILON); assertEquals(0.0, solver.getResidualNorm(), ConjugateGradientSolver.DEFAULT_MAX_ERROR); assertEquals(10, solver.getIterations()); }
@Test public void testTimesMatrix() { Matrix transpose = test.transpose(); Matrix value = test.times(transpose); assertEquals("rows", test.rowSize(), value.rowSize()); assertEquals("cols", test.rowSize(), value.columnSize()); // TODO: check the math too, lazy }
@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); }
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 testRankDeficient() { Matrix A = rank4Matrix(); CholeskyDecomposition cd = new CholeskyDecomposition(A); PivotedMatrix Ax = new PivotedMatrix(A, cd.getPivot()); CholeskyDecomposition cd2 = new CholeskyDecomposition(Ax, false); assertEquals(0, cd2.getL().times(cd2.getL().transpose()).minus(Ax).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, cd.getL().times(cd.getL().transpose()).minus(A).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); Assert.assertFalse(cd.isPositiveDefinite()); Matrix L = cd.getL(); Matrix Abar = L.times(L.transpose()); double error = A.minus(Abar).aggregate(Functions.MAX, Functions.ABS); Assert.assertEquals(0, error, 1.0e-10); }
private void validateYtY(Matrix matrixToTest, int numThreads) { OpenIntObjectHashMap<Vector> matrixToTestAsRowVectors = asRowVectors(matrixToTest); ImplicitFeedbackAlternatingLeastSquaresSolver solver = new ImplicitFeedbackAlternatingLeastSquaresSolver( matrixToTest.columnSize(), 1, 1, matrixToTestAsRowVectors, numThreads); Matrix yTy = matrixToTest.transpose().times(matrixToTest); Matrix shouldMatchyTy = solver.getYtransposeY(matrixToTestAsRowVectors); for (int row = 0; row < yTy.rowSize(); row++) { for (int column = 0; column < yTy.columnSize(); column++) { assertEquals(yTy.getQuick(row, column), shouldMatchyTy.getQuick(row, column), 0); } } }