@Override public DenseVector vectorToTest(int size) { DenseVector r = new DenseVector(size); r.assign(Functions.random()); return r; }
/** * Test for out of range column or row access. */ @Test public void testIndexRange() { Matrix m = new DenseMatrix(20, 30).assign(Functions.random()); try { m.viewColumn(30); fail("Should have thrown exception"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().startsWith("Index 30 is outside allowable")); } try { m.viewRow(20); fail("Should have thrown exception"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().startsWith("Index 20 is outside allowable")); } } }
@Test(timeout=50000) public void testTimesOtherSparseEfficiency() { Random raw = RandomUtils.getRandom(); Gamma gen = new Gamma(0.1, 0.1, raw); // build a sequential sparse matrix and a diagonal matrix and multiply them Matrix x = new SparseRowMatrix(1000, 2000, false); for (int i = 0; i < 1000; i++) { int[] values = new int[1000]; for (int k = 0; k < 1000; k++) { int j = (int) Math.min(1000, gen.nextDouble()); values[j]++; } for (int j = 0; j < 1000; j++) { if (values[j] > 0) { x.set(i, j, values[j]); } } } Vector d = new DenseVector(2000).assign(Functions.random()); Matrix y = new DiagonalMatrix(d); long t0 = System.nanoTime(); Matrix z = x.times(y); double elapsedTime = (System.nanoTime() - t0) * 1e-6; System.out.printf("done in %.1f ms\n", elapsedTime); for (MatrixSlice row : z) { for (Vector.Element element : row.nonZeroes()) { assertEquals(x.get(row.index(), element.index()) * d.get(element.index()), element.get(), 1e-12); } } }
@Ignore public void fasterThanBefore() { OnlineSummarizer s1 = new OnlineSummarizer(); OnlineSummarizer s2 = new OnlineSummarizer(); Matrix a = new DenseMatrix(60, 60).assign(Functions.random()); decompositionSpeedCheck(new Decomposer() { @Override public QR decompose(Matrix a) { return new QRDecomposition(a); } }, s1, a, "new"); decompositionSpeedCheck(new Decomposer() { @Override public QR decompose(Matrix a) { return new OldQRDecomposition(a); } }, s2, a, "old"); // should be much more than twice as fast. (originally was on s2.getMedian, but we factored out com.tdunning ) System.out.printf("Speedup is about %.1f times\n", s2.getMean() / s1.getMean()); assertTrue(s1.getMean() < 0.5 * s2.getMean()); }
@Test(timeout=50000) public void testTimesCorrect() { Random raw = RandomUtils.getRandom(); // build two large sequential sparse matrices and multiply them Matrix x = new SparseRowMatrix(100, 2000, false) .assign(Functions.random()); Matrix y = new SparseRowMatrix(2000, 100, false) .assign(Functions.random()); Matrix xd = new DenseMatrix(100, 2000).assign(x); Matrix yd = new DenseMatrix(2000, 100).assign(y); assertEquals(0, xd.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); assertEquals(0, x.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); assertEquals(0, xd.times(y).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15); } }
@Test public void testProjection() { Vector v1 = new DenseVector(10).assign(Functions.random()); WeightedVector v2 = new WeightedVector(v1, v1, 31); assertEquals(v1.dot(v1), v2.getWeight(), 1.0e-13); assertEquals(31, v2.getIndex()); Matrix y = new DenseMatrix(10, 4).assign(Functions.random()); Matrix q = new QRDecomposition(y.viewPart(0, 10, 0, 3)).getQ(); Vector nullSpace = y.viewColumn(3).minus(q.times(q.transpose().times(y.viewColumn(3)))); WeightedVector v3 = new WeightedVector(q.viewColumn(0).plus(q.viewColumn(1)), nullSpace, 1); assertEquals(0, v3.getWeight(), 1.0e-13); Vector qx = q.viewColumn(0).plus(q.viewColumn(1)).normalize(); WeightedVector v4 = new WeightedVector(qx, q.viewColumn(0), 2); assertEquals(Math.sqrt(0.5), v4.getWeight(), 1.0e-13); WeightedVector v5 = WeightedVector.project(q.viewColumn(0), qx); assertEquals(Math.sqrt(0.5), v5.getWeight(), 1.0e-13); }
@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 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); }