Search in sources :

Example 11 with RealVector

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

the class ProcrustesFit method makeDataMatrix.

private RealMatrix makeDataMatrix(List<double[]> X) {
    final int m = X.size();
    final int n = X.get(0).length;
    RealMatrix M = MatrixUtils.createRealMatrix(n, m);
    int i = 0;
    for (double[] x : X) {
        RealVector xi = MatrixUtils.createRealVector(x);
        M.setColumnVector(i, xi);
        i++;
    }
    return M;
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Example 12 with RealVector

use of org.apache.commons.math3.linear.RealVector in project FSensor by KalebKE.

the class CalibrationUtil method calibrate.

/**
 * Compensate for hard and soft iron distortions (magnetic) or sensor skew and offsets (acceleration) depending on the sensor type.
 *
 * @param vector      the vector to be compensated.
 * @param calibration the calibration to be applied
 * @return the compensated vector
 */
public static float[] calibrate(float[] vector, Calibration calibration) {
    RealVector point = new ArrayRealVector(3);
    point.setEntry(0, vector[0]);
    point.setEntry(1, vector[1]);
    point.setEntry(2, vector[2]);
    point = calibration.scalar.operate((point.subtract(calibration.offset)));
    vector[0] = (float) point.getEntry(0);
    vector[1] = (float) point.getEntry(1);
    vector[2] = (float) point.getEntry(2);
    return vector;
}
Also used : RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Example 13 with RealVector

use of org.apache.commons.math3.linear.RealVector in project FSensor by KalebKE.

the class FitPoints method formAlgebraicMatrix.

/**
 * Create a matrix in the algebraic form of the polynomial Ax^2 + By^2 +
 * Cz^2 + 2Dxy + 2Exz + 2Fyz + 2Gx + 2Hy + 2Iz = 1.
 *
 * @param v the vector polynomial.
 * @return the matrix of the algebraic form of the polynomial.
 */
private RealMatrix formAlgebraicMatrix(RealVector v) {
    // a =
    // [ Ax^2 2Dxy 2Exz 2Gx ]
    // [ 2Dxy By^2 2Fyz 2Hy ]
    // [ 2Exz 2Fyz Cz^2 2Iz ]
    // [ 2Gx 2Hy 2Iz -1 ] ]
    RealMatrix a = new Array2DRowRealMatrix(4, 4);
    a.setEntry(0, 0, v.getEntry(0));
    a.setEntry(0, 1, v.getEntry(3));
    a.setEntry(0, 2, v.getEntry(4));
    a.setEntry(0, 3, v.getEntry(6));
    a.setEntry(1, 0, v.getEntry(3));
    a.setEntry(1, 1, v.getEntry(1));
    a.setEntry(1, 2, v.getEntry(5));
    a.setEntry(1, 3, v.getEntry(7));
    a.setEntry(2, 0, v.getEntry(4));
    a.setEntry(2, 1, v.getEntry(5));
    a.setEntry(2, 2, v.getEntry(2));
    a.setEntry(2, 3, v.getEntry(8));
    a.setEntry(3, 0, v.getEntry(6));
    a.setEntry(3, 1, v.getEntry(7));
    a.setEntry(3, 2, v.getEntry(8));
    a.setEntry(3, 3, -1);
    return a;
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix)

Example 14 with RealVector

use of org.apache.commons.math3.linear.RealVector in project FSensor by KalebKE.

the class FitPoints method generateRIV.

/**
 * Generates a vector from the identity matrix of r.
 *
 * @param subr centered algebraic form of the ellipsoid
 */
private RealVector generateRIV(RealMatrix subr) {
    riv = new ArrayRealVector(3);
    riv.setEntry(0, Math.abs(subr.getEntry(0, 0)));
    riv.setEntry(1, Math.abs(subr.getEntry(1, 1)));
    riv.setEntry(2, Math.abs(subr.getEntry(2, 2)));
    return riv;
}
Also used : ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Example 15 with RealVector

use of org.apache.commons.math3.linear.RealVector in project FSensor by KalebKE.

the class FitPoints method findCenter.

/**
 * Find the offset of the ellipsoid.
 *
 * @param a the algebraic from of the polynomial.
 * @return a vector containing the offset of the ellipsoid.
 */
private RealVector findCenter(RealMatrix a) {
    RealMatrix subA = a.getSubMatrix(0, 2, 0, 2);
    for (int q = 0; q < subA.getRowDimension(); q++) {
        for (int s = 0; s < subA.getColumnDimension(); s++) {
            subA.multiplyEntry(q, s, -1.0);
        }
    }
    RealVector subV = a.getRowVector(3).getSubVector(0, 3);
    // inv (dtd)
    DecompositionSolver solver = new SingularValueDecomposition(subA).getSolver();
    RealMatrix subAi = solver.getInverse();
    return subAi.operate(subV);
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DecompositionSolver(org.apache.commons.math3.linear.DecompositionSolver) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) SingularValueDecomposition(org.apache.commons.math3.linear.SingularValueDecomposition)

Aggregations

RealVector (org.apache.commons.math3.linear.RealVector)41 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)30 RealMatrix (org.apache.commons.math3.linear.RealMatrix)22 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)10 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 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)3 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)3 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)3 DecompositionSolver (org.apache.commons.math3.linear.DecompositionSolver)3 SingularValueDecomposition (org.apache.commons.math3.linear.SingularValueDecomposition)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 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)2 ValueAndJacobianFunction (org.apache.commons.math3.fitting.leastsquares.ValueAndJacobianFunction)2