buf.append(String.valueOf(this.getS())); } catch (IllegalArgumentException exc) { buf.append(unknown).append(exc.getMessage());
@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 random() { Matrix m = new DenseMatrix(200, 30).assign(Functions.random()); Vector b = new DenseVector(200).assign(1); LSMR r = new LSMR(); Vector x1 = r.solve(m, b); // assertEquals(0, m.times(x1).minus(b).norm(2), 1.0e-2); double norm = new SingularValueDecomposition(m).getS().viewDiagonal().norm(2); double actual = m.transpose().times(m).times(x1).minus(m.transpose().times(b)).norm(2); System.out.printf("%.4f\n", actual / norm * 1.0e6); assertEquals(0, actual, norm * 1.0e-5); // and we need to check that the error estimates are pretty good. assertEquals(m.times(x1).minus(b).norm(2), r.getResidualNorm(), 1.0e-5); assertEquals(actual, r.getNormalEquationResidual(), 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();
buf.append(String.valueOf(this.getS())); } catch (IllegalArgumentException exc) { buf.append(unknown).append(exc.getMessage());
buf.append(String.valueOf(this.getS())); } catch (IllegalArgumentException exc) { buf.append(unknown).append(exc.getMessage());
/** * 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"); }
/** * 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"); }
/** * 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 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 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();
@Test public void testInitialization() { // Start with super clusterable data. List<? extends WeightedVector> data = cubishTestData(0.01); // Just do initialization of ball k-means. This should drop a point into each of the clusters. BallKMeans r = new BallKMeans(new BruteSearch(new SquaredEuclideanDistanceMeasure()), 6, 20); r.cluster(data); // Put the centroids into a matrix. Matrix x = new DenseMatrix(6, 5); int row = 0; for (Centroid c : r) { x.viewRow(row).assign(c.viewPart(0, 5)); row++; } // Verify that each column looks right. Should contain zeros except for a single 6. final Vector columnNorms = x.aggregateColumns(new VectorFunction() { @Override public double apply(Vector f) { // Return the sum of three discrepancy measures. return Math.abs(f.minValue()) + Math.abs(f.maxValue() - 6) + Math.abs(f.norm(1) - 6); } }); // Verify all errors are nearly zero. assertEquals(0, columnNorms.norm(1) / columnNorms.size(), 0.1); // Verify that the centroids are a permutation of the original ones. SingularValueDecomposition svd = new SingularValueDecomposition(x); Vector s = svd.getS().viewDiagonal().assign(Functions.div(6)); assertEquals(5, s.getLengthSquared(), 0.05); assertEquals(5, s.norm(1), 0.05); }