Search in sources :

Example 56 with RealVector

use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.

the class NonlinearLeastSquares method main.

public static void main(String[] args) {
    PrintPrecision.set(6);
    // 1D fitting example in book appendix: f(x) = exp(-a * x) * sin(b * x) + c
    // (xi, yi)
    double[][] data = { { 3, 2.5 }, { 6, 1.7 }, { 8, 2.5 }, { 8, 2.3 }, { 15, 2.1 } };
    final int n = data.length;
    // number of parameters
    final int k = 3;
    MultivariateVectorFunction V = new MultivariateVectorFunction() {

        @Override
        public double[] value(double[] p) {
            double[] V = new double[n];
            double a = p[0];
            double b = p[1];
            double c = p[2];
            for (int i = 0; i < n; i++) {
                double x = data[i][0];
                V[i] = exp(-a * x) * sin(b * x) + c;
            }
            return V;
        }
    };
    MultivariateMatrixFunction J = new MultivariateMatrixFunction() {

        @Override
        public double[][] value(double[] p) {
            double[][] J = new double[n][k];
            double a = p[0];
            double b = p[1];
            // double c = p[2];
            for (int i = 0; i < n; i++) {
                double x = data[i][0];
                // df(x)/da
                J[i][0] = -exp(-a * x) * x * sin(b * x);
                // df(x)/db
                J[i][1] = exp(-a * x) * x * cos(b * x);
                // df(x)/dc
                J[i][2] = 1;
            }
            return J;
        }
    };
    RealVector z = makeTargetVector(data);
    RealVector p0 = new ArrayRealVector(new double[] { 0, 1, 2 });
    {
        RealVector popt = solveLevenvergMarquardt(V, J, z, p0);
        System.out.println("Levenberg:    popt = " + Matrix.toString(popt.toArray()));
    }
    {
        RealVector popt = solveGaussNewton(V, J, z, p0);
        System.out.println("Gauss-Newton:    popt = " + Matrix.toString(popt.toArray()));
    }
    {
        MaxIterations = 10;
        RealVector popt = solveNLS2(V, J, z, p0);
        System.out.println("solveNLS2 (LM): popt = " + Matrix.toString(popt.toArray()));
    }
}
Also used : RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) MultivariateMatrixFunction(org.apache.commons.math3.analysis.MultivariateMatrixFunction) MultivariateVectorFunction(org.apache.commons.math3.analysis.MultivariateVectorFunction)

Example 57 with RealVector

use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.

the class NonlinearLeastSquares method solveLevenvergMarquardt.

/**
 * Solves the nonlinear least-squares problem defined by the arguments
 * using Levenberg-Marquardt optimization.
 *
 * @param V the "value" function, V(p) must return a vector for the current parameters p
 * @param J the "Jacobian" function, J(p) must return a matrix for the current parameters p
 * @param z the vector of observed ("target") values
 * @param p0 initial parameter vector
 * @return the vector of optimal parameters
 */
public static RealVector solveLevenvergMarquardt(MultivariateVectorFunction V, MultivariateMatrixFunction J, RealVector z, RealVector p0) {
    LeastSquaresProblem problem = makeProblem(V, J, z, p0);
    LeastSquaresOptimizer optimizer = new LevenbergMarquardtOptimizer();
    Optimum solution = optimizer.optimize(problem);
    // System.out.println("iterations = " + solution.getIterations());
    return solution.getPoint();
}
Also used : Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) LeastSquaresOptimizer(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer)

Example 58 with RealVector

use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.

the class GeneralizedSymmetricEigenSolver method main.

// ---------------------------------------------------------------------
public static void main(String[] args) {
    RealMatrix A = MatrixUtils.createRealMatrix(new double[][] { { 3, -1, 5 }, { -1, -2, 7 }, { 5, 7, 0 } });
    RealMatrix B = MatrixUtils.createRealMatrix(new double[][] { { 10, 2, 7 }, { 2, 12, 3 }, { 7, 3, 15 } });
    GeneralizedSymmetricEigenSolver decomp = new GeneralizedSymmetricEigenSolver(A, B);
    double[] evals = decomp.getRealEigenvalues();
    System.out.println("evals = " + Arrays.toString(evals));
    RealMatrix V = decomp.getV();
    System.out.println("V = \n" + Matrix.toString(V.getData()));
    RealMatrix D = decomp.getD();
    System.out.println("D = \n" + Matrix.toString(D.getData()));
    for (int i = 0; i < evals.length; i++) {
        double lambda = evals[i];
        RealVector evec = decomp.getEigenVector(i);
        System.out.println("evec = " + Arrays.toString(evec.toArray()));
        RealVector L = A.operate(evec);
        // System.out.println("L = "+ Arrays.toString(L.toArray()));
        RealVector R = B.operate(evec).mapMultiply(lambda);
        // System.out.println("R = "+ Arrays.toString(R.toArray()));
        RealVector res = L.subtract(R);
        // System.out.println("res = "+ Arrays.toString(res.toArray()));	// L - R must be 0
        System.out.println("res = 0? " + Matrix.isZero(res.toArray(), 1e-6));
        // check A*V = B*V*D
        RealMatrix AV = A.multiply(V);
        System.out.println("AV = \n" + Matrix.toString(AV.getData()));
        RealMatrix BVD = B.multiply(V).multiply(D);
        System.out.println("BVD = \n" + Matrix.toString(BVD.getData()));
    }
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector)

Example 59 with RealVector

use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.

the class NonlinearLeastSquares method solveNLS2.

// --------------------------------------------------------------------
// book version
// --------------------------------------------------------------------
public static RealVector solveNLS2(MultivariateVectorFunction V, MultivariateMatrixFunction J, RealVector z, RealVector p0) {
    LeastSquaresProblem problem = LeastSquaresFactory.create(model(V, J), z, p0, null, MaxEvaluations, MaxIterations);
    LeastSquaresOptimizer optimizer = new LevenbergMarquardtOptimizer();
    Optimum solution = optimizer.optimize(problem);
    // System.out.println("iterations = " + solution.getIterations());
    if (solution.getIterations() > MaxIterations)
        return null;
    else
        return solution.getPoint();
}
Also used : Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) LeastSquaresOptimizer(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer)

Example 60 with RealVector

use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.

the class ProcrustesFit method makeDataMatrix.

private RealMatrix makeDataMatrix(Pnt2d[] points, double[] meanX) {
    RealMatrix M = MatrixUtils.createRealMatrix(2, points.length);
    RealVector mean = MatrixUtils.createRealVector(meanX);
    int i = 0;
    for (Pnt2d p : points) {
        RealVector cv = p.toRealVector();
        // RealVector cv = MatrixUtils.createRealVector(p.toDoubleArray());
        if (meanX != null) {
            cv = cv.subtract(mean);
        }
        M.setColumnVector(i, cv);
        i++;
    }
    return M;
}
Also used : Pnt2d(imagingbook.pub.geometry.basic.Pnt2d) RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Aggregations

RealVector (org.apache.commons.math3.linear.RealVector)51 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)36 RealMatrix (org.apache.commons.math3.linear.RealMatrix)29 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)11 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)7 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)7 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)6 DecompositionSolver (org.apache.commons.math3.linear.DecompositionSolver)5 SingularValueDecomposition (org.apache.commons.math3.linear.SingularValueDecomposition)5 Test (org.junit.jupiter.api.Test)5 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)4 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)4 VectorPool (nars.rl.horde.math.VectorPool)3 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)3 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)3 CombinedAttributeValues (org.knime.base.node.mine.treeensemble2.data.BinaryNominalSplitsPCA.CombinedAttributeValues)3 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)3 File (java.io.File)2 Comparator (java.util.Comparator)2 DimensionMismatchException (org.apache.commons.math3.exception.DimensionMismatchException)2