@Override public double norm(double power) { return delegate.norm(power); }
@Override public double norm(double power) { return delegate.norm(power); }
protected static double calculateScaleFactor(Vector nextVector) { return nextVector.norm(2); }
@Override public EigenStatus verify(VectorIterable corpus, Vector vector) { Vector resultantVector = corpus.timesSquared(vector); double newNorm = resultantVector.norm(2); double oldNorm = vector.norm(2); double eigenValue; double cosAngle; if (newNorm > 0 && oldNorm > 0) { eigenValue = newNorm / oldNorm; cosAngle = resultantVector.dot(vector) / newNorm * oldNorm; } else { eigenValue = 1.0; cosAngle = 0.0; } return new EigenStatus(eigenValue, cosAngle, false); }
/** * You have to start somewhere... * TODO: start instead wherever you find a vector with maximum residual length after subtracting off the projection * TODO: onto all previous eigenvectors. * * @param corpus the corpus matrix * @param eigens not currently used, but should be (see above TODO) * @return the index into the corpus where the "starting seed" input vector lies. */ private int getRandomStartingIndex(Matrix corpus, Matrix eigens) { int index; Vector v; do { double r = rng.nextDouble(); index = (int) (r * corpus.numRows()); v = corpus.viewRow(index); } while (v == null || v.norm(2) == 0 || v.getNumNondefaultElements() < 5); return index; }
public static void assertOrthonormal(Matrix currentEigens, double errorMargin) { List<String> nonOrthogonals = Lists.newArrayList(); for (int i = 0; i < currentEigens.numRows(); i++) { Vector ei = currentEigens.viewRow(i); for (int j = 0; j <= i; j++) { Vector ej = currentEigens.viewRow(j); if (ei.norm(2) == 0 || ej.norm(2) == 0) { continue; } double dot = ei.dot(ej); if (i == j) { assertTrue("not norm 1 : " + dot + " (eigen #" + i + ')', Math.abs(1.0 - dot) < errorMargin); } else { if (Math.abs(dot) > errorMargin) { log.info("not orthogonal : {} (eigens {}, {})", dot, i, j); nonOrthogonals.add("(" + i + ',' + j + ')'); } } } log.info("{}:{}", nonOrthogonals.size(), nonOrthogonals); } }
@Override public Matrix assignColumn(int column, Vector other) { if (columnSize() != other.size()) { throw new IndexException(columnSize(), other.size()); } if (other.viewPart(column + 1, other.size() - column - 1).norm(1) > 1.0e-14) { throw new IllegalArgumentException("Cannot set lower portion of triangular matrix to non-zero"); } for (Vector.Element element : other.viewPart(0, column).all()) { setQuick(element.index(), column, element.get()); } return this; }
public static Matrix randomHierarchicalMatrix(int numRows, int numCols, boolean symmetric) { Matrix matrix = new DenseMatrix(numRows, numCols); // TODO rejigger tests so that it doesn't expect this particular seed Random r = new Random(1234L); for (int row = 0; row < numRows; row++) { Vector v = new DenseVector(numCols); for (int col = 0; col < numCols; col++) { double val = r.nextGaussian(); v.set(col, val); } v.assign(Functions.MULT, 1/((row + 1) * v.norm(2))); matrix.assignRow(row, v); } if (symmetric) { return matrix.times(matrix.transpose()); } return matrix; }
private static void doTestAggregation(Vector v, Vector w) { assertEquals("aggregate(plus, pow(2)) not equal to " + v.getLengthSquared(), v.getLengthSquared(), v.aggregate(Functions.PLUS, Functions.pow(2)), EPSILON); assertEquals("aggregate(plus, abs) not equal to " + v.norm(1), v.norm(1), v.aggregate(Functions.PLUS, Functions.ABS), EPSILON); assertEquals("aggregate(max, abs) not equal to " + v.norm(Double.POSITIVE_INFINITY), v.norm(Double.POSITIVE_INFINITY), v.aggregate(Functions.MAX, Functions.ABS), EPSILON); assertEquals("v.dot(w) != v.aggregate(w, plus, mult)", v.dot(w), v.aggregate(w, Functions.PLUS, Functions.MULT), EPSILON); assertEquals("|(v-w)|^2 != v.aggregate(w, plus, chain(pow(2), minus))", v.minus(w).dot(v.minus(w)), v.aggregate(w, Functions.PLUS, Functions.chain(Functions.pow(2), Functions.MINUS)), EPSILON); }
@Test public void testRadius() { MultiNormal gen = new MultiNormal(0.1, new DenseVector(10)); OnlineSummarizer s = new OnlineSummarizer(); for (int i = 0; i < 10000; i++) { double x = gen.sample().norm(2) / Math.sqrt(10); s.add(x); } assertEquals(0.1, s.getMean(), 0.01); } }
@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 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 public void testSwap() { Matrix m = new DenseMatrix(10, 10); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { m.set(i, j, 10 * i + j); } } PivotedMatrix pm = new PivotedMatrix(m); pm.swap(3, 5); assertEquals(0, pm.viewDiagonal().minus( new DenseVector(new double[]{0, 11, 22, 55, 44, 33, 66, 77, 88, 99})).norm(1), 1.0e-10); pm.swap(2, 7); assertEquals(0, pm.viewDiagonal().minus( new DenseVector(new double[]{0, 11, 77, 55, 44, 33, 66, 22, 88, 99})).norm(1), 1.0e-10); pm.swap(5, 8); assertEquals(0, pm.viewColumn(4).minus( new DenseVector(new double[]{4.0,14.0,74.0,54.0,44.0,84.0,64.0,24.0,34.0,94.0})).norm(1), 1.0e-10); assertEquals(0, pm.viewDiagonal().minus( new DenseVector(new double[]{0, 11, 77, 55, 44, 88, 66, 22, 33, 99})).norm(1), 1.0e-10); } }
@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); }
@Test public void testBasics() { Matrix a = new DenseSymmetricMatrix(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false); System.out.println(a.toString()); assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10); assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus( new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10); assertEquals(4, a.get(0, 3), 1.0e-10); System.out.println(a); Matrix m = new DenseMatrix(4, 4).assign(a); assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); System.out.println(m); assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate( Functions.PLUS, Functions.ABS), 1.0e-10); System.out.println(a.plus(a)); assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); }
@Test public void testDeficientRank() { Matrix a = new DenseMatrix(10, 3).assign(new DoubleFunction() { private final Random gen = RandomUtils.getRandom(); @Override public double apply(double arg1) { return gen.nextGaussian(); } }); a = a.transpose().times(a); EigenDecomposition eig = new EigenDecomposition(a); Matrix d = eig.getD(); Matrix v = eig.getV(); check("EigenvalueDecomposition (rank deficient)...", a.times(v), v.times(d)); Assert.assertEquals(0, eig.getImagEigenvalues().norm(1), 1.0e-10); Assert.assertEquals(3, eig.getRealEigenvalues().norm(0), 1.0e-10); }
@Test public void testSetData() throws IOException { File f = File.createTempFile("matrix", ".m", getTestTempDir()); f.deleteOnExit(); Matrix m0 = new DenseMatrix(100000, 30); MultiNormal gen = new MultiNormal(30); for (MatrixSlice row : m0) { row.vector().assign(gen.sample()); } FileBasedMatrix.writeMatrix(f, m0); FileBasedMatrix m = new FileBasedMatrix(100000, 30); m.setData(f, true); assertEquals(0, m0.minus(m).aggregate(Functions.MAX, Functions.ABS), 1.0e-8); int i = 0; for (MatrixSlice row : m) { assertEquals(0, row.vector().minus(m0.viewRow(i++)).norm(1), 1.0e-8); } } }
@Test public void testBasics() { Matrix a = new UpperTriangular(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false); assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10); assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus( new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10); assertEquals(4, a.get(0, 3), 1.0e-10); print(a); Matrix m = new DenseMatrix(4, 4).assign(a); assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); print(m); assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate( Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, m.plus(m).minus(a.plus(a)).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); }
@Test public void testBasics() { DiagonalMatrix a = new DiagonalMatrix(new double[]{1, 2, 3, 4}); assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 2, 3, 4})).norm(1), 1.0e-10); assertEquals(0, a.viewPart(0, 3, 0, 3).viewDiagonal().minus( new DenseVector(new double[]{1, 2, 3})).norm(1), 1.0e-10); assertEquals(4, a.get(3, 3), 1.0e-10); Matrix m = new DenseMatrix(4, 4); m.assign(a); assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate( Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); m = new DenseMatrix(new double[][]{{1, 2, 3, 4}, {5, 6, 7, 8}}); assertEquals(100, a.timesLeft(m).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(100, a.times(m.transpose()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); }