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()));
}
}
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();
}
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()));
}
}
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();
}
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;
}
Aggregations