public Matrix getU() { // U = (Y inv(R)) U_0 return cd1.solveRight(y).times(svd.getU()); }
buf.append(String.valueOf(this.getU())); } catch (IllegalArgumentException exc) { buf.append(unknown).append(exc.getMessage());
/** 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 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 dimensions */ @Test public void testDimensions() { Matrix matrix = new DenseMatrix(testSquare); int m = matrix.numRows(); int n = matrix.numCols(); SingularValueDecomposition svd = new SingularValueDecomposition(matrix); assertEquals(m, svd.getU().numRows()); assertEquals(m, svd.getU().numCols()); assertEquals(m, svd.getS().numCols()); assertEquals(n, svd.getS().numCols()); assertEquals(n, svd.getV().numRows()); assertEquals(n, svd.getV().numCols()); }
@Test public void testLeftVectors() { Matrix A = lowRankMatrix(); SequentialBigSvd s = new SequentialBigSvd(A, 8); SingularValueDecomposition svd = new SingularValueDecomposition(A); // can only check first few singular vectors because once the singular values // go to zero, the singular vectors are not uniquely determined Matrix u1 = svd.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS); Matrix u2 = s.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS); assertEquals(0, u1.minus(u2).aggregate(Functions.PLUS, Functions.ABS), 1.0e-9); }
/** test matrices values */ @Test public void testMatricesValues1() { SingularValueDecomposition svd = new SingularValueDecomposition(new DenseMatrix(testSquare)); Matrix uRef = new DenseMatrix(new double[][] { { 3.0 / 5.0, 4.0 / 5.0 }, { 4.0 / 5.0, -3.0 / 5.0 } }); Matrix sRef = new DenseMatrix(new double[][] { { 3.0, 0.0 }, { 0.0, 1.0 } }); Matrix vRef = new DenseMatrix(new double[][] { { 4.0 / 5.0, -3.0 / 5.0 }, { 3.0 / 5.0, 4.0 / 5.0 } }); // check values against known references Matrix u = svd.getU(); assertEquals(0, Algebra.getNorm(u.minus(uRef)), NORM_TOLERANCE); Matrix s = svd.getS(); assertEquals(0, Algebra.getNorm(s.minus(sRef)), NORM_TOLERANCE); Matrix v = svd.getV(); assertEquals(0, Algebra.getNorm(v.minus(vRef)), NORM_TOLERANCE); }
public static void checkAEqualUSVt(Matrix matrix) { SingularValueDecomposition svd = new SingularValueDecomposition(matrix); Matrix u = svd.getU(); Matrix s = svd.getS(); Matrix v = svd.getV();
public Matrix getU() { // U = (Y inv(R)) U_0 return cd1.solveRight(y).times(svd.getU()); }
public Matrix getU() { // U = (Y inv(R)) U_0 return cd1.solveRight(y).times(svd.getU()); }
public void computeU(Iterable<File> partsOfA, File tmpDir) throws IOException { // step 4, compute pieces of U for (File file : partsOfA) { MatrixWritable m = new MatrixWritable(); m.readFields(new DataInputStream(new FileInputStream(file))); Matrix aI = m.get(); Matrix y = aI.times(new RandomTrinaryMatrix(seed, aI.numCols(), dim, false)); Matrix uI = r2.solveRight(y).times(svd.getU()); m.set(uI); try (DataOutputStream out = new DataOutputStream(new FileOutputStream( new File(tmpDir, String.format("U-%s", file.getName().replaceAll(".*-", "")))))) { m.write(out); } } }
public void computeU(Iterable<File> partsOfA, File tmpDir) throws IOException { // step 4, compute pieces of U for (File file : partsOfA) { MatrixWritable m = new MatrixWritable(); m.readFields(new DataInputStream(new FileInputStream(file))); Matrix aI = m.get(); Matrix y = aI.times(new RandomTrinaryMatrix(seed, aI.numCols(), dim, false)); Matrix uI = r2.solveRight(y).times(svd.getU()); m.set(uI); DataOutputStream out = new DataOutputStream(new FileOutputStream( new File(tmpDir, String.format("U-%s", file.getName().replaceAll(".*-", ""))))); try { m.write(out); } finally { out.close(); } } }
public void computeU(Iterable<File> partsOfA, File tmpDir) throws IOException { // step 4, compute pieces of U for (File file : partsOfA) { MatrixWritable m = new MatrixWritable(); m.readFields(new DataInputStream(new FileInputStream(file))); Matrix aI = m.get(); Matrix y = aI.times(new RandomTrinaryMatrix(seed, aI.numCols(), dim, false)); Matrix uI = r2.solveRight(y).times(svd.getU()); m.set(uI); DataOutputStream out = new DataOutputStream(new FileOutputStream( new File(tmpDir, String.format("U-%s", file.getName().replaceAll(".*-", ""))))); try { m.write(out); } finally { out.close(); } } }
/** * Computes the inverse covariance from the input covariance matrix given in input. * * @param m A covariance matrix. * @throws IllegalArgumentException if <tt>eigen values equal to 0 found</tt>. */ public void setCovarianceMatrix(Matrix m) { if (m.numRows() != m.numCols()) { throw new CardinalityException(m.numRows(), m.numCols()); } // See http://www.mlahanas.de/Math/svd.htm for details, // which specifically details the case of covariance matrix inversion // Complexity: O(min(nm2,mn2)) SingularValueDecomposition svd = new SingularValueDecomposition(m); Matrix sInv = svd.getS(); // Inverse Diagonal Elems for (int i = 0; i < sInv.numRows(); i++) { double diagElem = sInv.get(i, i); if (diagElem > 0.0) { sInv.set(i, i, 1 / diagElem); } else { throw new IllegalStateException("Eigen Value equals to 0 found."); } } inverseCovarianceMatrix = svd.getU().times(sInv.times(svd.getU().transpose())); Preconditions.checkArgument(inverseCovarianceMatrix != null, "inverseCovarianceMatrix not initialized"); }
/** 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 public void testLeftVectors() throws IOException { Matrix A = lowRankMatrixInMemory(20, 20); SequentialBigSvd s = new SequentialBigSvd(A, 6); SingularValueDecomposition svd = new SingularValueDecomposition(A); // can only check first few singular vectors Matrix u1 = svd.getU().viewPart(0, 20, 0, 3).assign(Functions.ABS); Matrix u2 = s.getU().viewPart(0, 20, 0, 3).assign(Functions.ABS); assertEquals(u1, u2); }
/** test dimensions */ @Test public void testDimensions() { Matrix matrix = new DenseMatrix(testSquare); int m = matrix.numRows(); int n = matrix.numCols(); SingularValueDecomposition svd = new SingularValueDecomposition(matrix); assertEquals(m, svd.getU().numRows()); assertEquals(m, svd.getU().numCols()); assertEquals(m, svd.getS().numCols()); assertEquals(n, svd.getS().numCols()); assertEquals(n, svd.getV().numRows()); assertEquals(n, svd.getV().numCols()); }
@Test public void testLeftVectors() { Matrix A = lowRankMatrix(); SequentialBigSvd s = new SequentialBigSvd(A, 8); SingularValueDecomposition svd = new SingularValueDecomposition(A); // can only check first few singular vectors because once the singular values // go to zero, the singular vectors are not uniquely determined Matrix u1 = svd.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS); Matrix u2 = s.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS); assertEquals(0, u1.minus(u2).aggregate(Functions.PLUS, Functions.ABS), 1.0e-9); }
/** test matrices values */ @Test public void testMatricesValues1() { SingularValueDecomposition svd = new SingularValueDecomposition(new DenseMatrix(testSquare)); Matrix uRef = new DenseMatrix(new double[][] { { 3.0 / 5.0, 4.0 / 5.0 }, { 4.0 / 5.0, -3.0 / 5.0 } }); Matrix sRef = new DenseMatrix(new double[][] { { 3.0, 0.0 }, { 0.0, 1.0 } }); Matrix vRef = new DenseMatrix(new double[][] { { 4.0 / 5.0, -3.0 / 5.0 }, { 3.0 / 5.0, 4.0 / 5.0 } }); // check values against known references Matrix u = svd.getU(); assertEquals(0, Algebra.getNorm(u.minus(uRef)), NORM_TOLERANCE); Matrix s = svd.getS(); assertEquals(0, Algebra.getNorm(s.minus(sRef)), NORM_TOLERANCE); Matrix v = svd.getV(); assertEquals(0, Algebra.getNorm(v.minus(vRef)), NORM_TOLERANCE); }
public static void checkAEqualUSVt(Matrix matrix) { SingularValueDecomposition svd = new SingularValueDecomposition(matrix); Matrix u = svd.getU(); Matrix s = svd.getS(); Matrix v = svd.getV();