Java Code Examples for org.apache.commons.math3.linear.RealMatrix#operate()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#operate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 2
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 3
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2.0, -3.0 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example 4
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 5
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 6
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2.0, -3.0 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example 7
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 8
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 9
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 10
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 11
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares3() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 12
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 13
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2.0, -3.0 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example 14
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 15
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
/**
 * Find the first singular vector/value of a matrix A based on the Power method.
 * 
 * @see http://www.cs.yale.edu/homes/el327/datamining2013aFiles/07_singular_value_decomposition.pdf
 * @param A target matrix
 * @param x0 initial vector
 * @param nIter number of iterations for the Power method
 * @param u 1st left singular vector
 * @param v 1st right singular vector
 * @return 1st singular value
 */
public static double power1(@Nonnull final RealMatrix A, @Nonnull final double[] x0,
        final int nIter, @Nonnull final double[] u, @Nonnull final double[] v) {
    Preconditions.checkArgument(A.getColumnDimension() == x0.length,
        "Column size of A and length of x should be same");
    Preconditions.checkArgument(A.getRowDimension() == u.length,
        "Row size of A and length of u should be same");
    Preconditions.checkArgument(x0.length == v.length, "Length of x and u should be same");
    Preconditions.checkArgument(nIter >= 1, "Invalid number of iterations: " + nIter);

    RealMatrix AtA = A.transpose().multiply(A);

    RealVector x = new ArrayRealVector(x0);
    for (int i = 0; i < nIter; i++) {
        x = AtA.operate(x);
    }

    double xNorm = x.getNorm();
    for (int i = 0, n = v.length; i < n; i++) {
        v[i] = x.getEntry(i) / xNorm;
    }

    RealVector Av = new ArrayRealVector(A.operate(v));
    double s = Av.getNorm();

    for (int i = 0, n = u.length; i < n; i++) {
        u[i] = Av.getEntry(i) / s;
    }

    return s;
}
 
Example 16
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testLeastSquares3() {
    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(ls),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 10, 10 }),
                             new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 17
Source File: RidgeRegression.java    From Surus with Apache License 2.0 5 votes vote down vote up
public void updateCoefficients(double l2penalty) {
       if (this.X_svd == null) {
       	this.X_svd = new SingularValueDecomposition(X);
       }
    RealMatrix V = this.X_svd.getV();
    double[] s = this.X_svd.getSingularValues();
    RealMatrix U = this.X_svd.getU();
    
    for (int i = 0; i < s.length; i++) {
    	s[i] = s[i] / (s[i]*s[i] + l2penalty);
    }
    RealMatrix S = MatrixUtils.createRealDiagonalMatrix(s);
    
    RealMatrix Z = V.multiply(S).multiply(U.transpose());
    
    this.coefficients = Z.operate(this.Y);
    
    this.fitted = this.X.operate(this.coefficients);
    double errorVariance = 0;
    for (int i = 0; i < residuals.length; i++) {
    	this.residuals[i] = this.Y[i] - this.fitted[i];
    	errorVariance += this.residuals[i] * this.residuals[i];
    }
    errorVariance = errorVariance / (X.getRowDimension() - X.getColumnDimension());
    
    RealMatrix errorVarianceMatrix = MatrixUtils.createRealIdentityMatrix(this.Y.length).scalarMultiply(errorVariance);
    RealMatrix coefficientsCovarianceMatrix = Z.multiply(errorVarianceMatrix).multiply(Z.transpose());
    this.standarderrors = getDiagonal(coefficientsCovarianceMatrix);
}
 
Example 18
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testLeastSquares3() {
    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(ls),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 10, 10 }),
                             new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 19
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testLeastSquares3() {
    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(ls),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 10, 10 }),
                             new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example 20
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
/**
 * Lanczos tridiagonalization for a symmetric matrix C to make s * s tridiagonal matrix T.
 *
 * @see http://www.cas.mcmaster.ca/~qiao/publications/spie05.pdf
 * @param C target symmetric matrix
 * @param a initial vector
 * @param T result is stored here
 */
public static void lanczosTridiagonalization(@Nonnull final RealMatrix C,
        @Nonnull final double[] a, @Nonnull final RealMatrix T) {
    Preconditions.checkArgument(Arrays.deepEquals(C.getData(), C.transpose().getData()),
        "Target matrix C must be a symmetric matrix");
    Preconditions.checkArgument(C.getColumnDimension() == a.length,
        "Column size of A and length of a should be same");
    Preconditions.checkArgument(T.getRowDimension() == T.getColumnDimension(),
        "T must be a square matrix");

    int s = T.getRowDimension();

    // initialize T with zeros
    T.setSubMatrix(new double[s][s], 0, 0);

    RealVector a0 = new ArrayRealVector(a.length);
    RealVector r = new ArrayRealVector(a);

    double beta0 = 1.d;

    for (int i = 0; i < s; i++) {
        RealVector a1 = r.mapDivide(beta0);
        RealVector Ca1 = C.operate(a1);

        double alpha1 = a1.dotProduct(Ca1);

        r = Ca1.add(a1.mapMultiply(-1.d * alpha1)).add(a0.mapMultiply(-1.d * beta0));

        double beta1 = r.getNorm();

        T.setEntry(i, i, alpha1);
        if (i - 1 >= 0) {
            T.setEntry(i, i - 1, beta0);
        }
        if (i + 1 < s) {
            T.setEntry(i, i + 1, beta1);
        }

        a0 = a1.copy();
        beta0 = beta1;
    }
}