LanczosSolver { public void solve(LanczosState state, int desiredRank) { solve(state, desiredRank, false); }  void solve(LanczosState state,
                    int desiredRank); void solve(LanczosState state,
                    int desiredRank,
                    boolean isSymmetric); static final double SAFE_MAX; }
@Test public void testEigenvalueCheck() throws Exception { int size = 100; Matrix m = randomHierarchicalSymmetricMatrix(size); Vector initialVector = new DenseVector(size); initialVector.assign(1.0 / Math.sqrt(size)); LanczosSolver solver = new LanczosSolver(); int desiredRank = 80; LanczosState state = new LanczosState(m, desiredRank, initialVector); solver.solve(state, desiredRank, true); EigenDecomposition decomposition = new EigenDecomposition(m); Vector eigenvalues = decomposition.getRealEigenvalues(); float fractionOfEigensExpectedGood = 0.6f; for (int i = 0; i < fractionOfEigensExpectedGood * desiredRank; i++) { double s = state.getSingularValue(i); double e = eigenvalues.get(i); log.info("{} : L = {}, E = {}", i, s, e); assertTrue("Singular value differs from eigenvalue", Math.abs((s-e)/e) < ERROR_TOLERANCE); Vector v = state.getRightSingularVector(i); Vector v2 = decomposition.getV().viewColumn(i); double error = 1 - Math.abs(v.dot(v2)/(v.norm(2) * v2.norm(2))); log.info("error: {}", error); assertTrue(i + ": 1 - cosAngle = " + error, error < ERROR_TOLERANCE); } }
@Test public void testLanczosSolver() throws Exception { int numRows = 800; int numColumns = 500; Matrix corpus = randomHierarchicalMatrix(numRows, numColumns, false); Vector initialVector = new DenseVector(numColumns); initialVector.assign(1.0 / Math.sqrt(numColumns)); int rank = 50; LanczosState state = new LanczosState(corpus, rank, initialVector); LanczosSolver solver = new LanczosSolver(); solver.solve(state, rank, false); assertOrthonormal(state); for (int i = 0; i < rank/2; i++) { assertEigen(i, state.getRightSingularVector(i), corpus, ERROR_TOLERANCE, false); } }
@Test public void testLanczosSolverSymmetric() throws Exception { int numCols = 500; Matrix corpus = randomHierarchicalSymmetricMatrix(numCols); Vector initialVector = new DenseVector(numCols); initialVector.assign(1.0 / Math.sqrt(numCols)); int rank = 30; LanczosState state = new LanczosState(corpus, rank, initialVector); LanczosSolver solver = new LanczosSolver(); solver.solve(state, rank, true); }
@Test public void testEigenvalueCheck() throws Exception { int size = 100; Matrix m = randomHierarchicalSymmetricMatrix(size); Vector initialVector = new DenseVector(size); initialVector.assign(1.0 / Math.sqrt(size)); LanczosSolver solver = new LanczosSolver(); int desiredRank = 80; LanczosState state = new LanczosState(m, desiredRank, initialVector); solver.solve(state, desiredRank, true); EigenDecomposition decomposition = new EigenDecomposition(m); Vector eigenvalues = decomposition.getRealEigenvalues(); float fractionOfEigensExpectedGood = 0.6f; for (int i = 0; i < fractionOfEigensExpectedGood * desiredRank; i++) { double s = state.getSingularValue(desiredRank - i - 1); double e = eigenvalues.get(eigenvalues.size() - i - 1); log.info("{} : L = {}, E = {}", i, s, e); assertTrue("Singular value differs from eigenvalue", Math.abs((s-e)/e) < ERROR_TOLERANCE); Vector v = state.getRightSingularVector(i); Vector v2 = decomposition.getV().viewColumn(eigenvalues.size() - i - 1); double error = 1 - Math.abs(v.dot(v2)/(v.norm(2) * v2.norm(2))); log.info("error: {}", error); assertTrue(i + ": 1 - cosAngle = " + error, error < ERROR_TOLERANCE); } }