Example usage for org.apache.commons.math3.analysis MultivariateMatrixFunction value

List of usage examples for org.apache.commons.math3.analysis MultivariateMatrixFunction value

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis MultivariateMatrixFunction value.

Prototype

double[][] value(double[] point) throws IllegalArgumentException;

Source Link

Document

Compute the value for the function at the given point.

Usage

From source file:de.tuberlin.uebb.jdae.simulation.PendulumTest.java

@Test
public void testJacobian() {

    final ExecutableDAE dae = runtime.causalise(reduction, ImmutableList.<GlobalEquation>of(initial_y),
            model.initials(reduction.ctxt), events, SimulationOptions.DEFAULT);

    final Block initBlock = (Block) dae.initials[2];

    final MultivariateMatrixFunction jacobian = initBlock.jacobian();

    int length_eq = -1;
    while (!(initBlock.equations[++length_eq].eq instanceof LengthBlockEquation))
        ;//from w  ww. j  a  va 2s  . co m

    int xaccel_eq = -1;
    while (!(initBlock.equations[++xaccel_eq].eq instanceof XAccelBlockEquation))
        ;

    if (length_eq < xaccel_eq)
        xaccel_eq += 2;

    int yaccel_eq = -1;
    while (!(initBlock.equations[++yaccel_eq].eq instanceof YAccelBlockEquation))
        ;
    if (length_eq < yaccel_eq)
        yaccel_eq += 2;

    final double[][] testData = new double[][] { { 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0 }, { 0, 0, 0, 1, 1 },
            { 1, 2, 3, 4, 5 }, { -5, 4, -3, 2, -1 }, { 1, 1, 1, 1, 1 } };

    for (double[] data : testData) {
        final double[][] M = jacobian.value(data);
        assertThat(M.length, is(5));
        assertThat(M[0].length, is(5));
        assertThat(M[1].length, is(5));
        assertThat(M[2].length, is(5));
        assertThat(M[3].length, is(5));
        assertThat(M[4].length, is(5));

        // x + y - 1
        assertEquals(2 * data[0], M[length_eq][0], 1e-6);
        assertEquals(0.0, M[length_eq][1], 1e-6);
        assertEquals(0.0, M[length_eq][2], 1e-6);
        assertEquals(0.0, M[length_eq][3], 1e-6);
        assertEquals(0.0, M[length_eq][4], 1e-6);

        // 2x * dx + 2y * dy
        assertEquals(2 * data[1], M[length_eq + 1][0], 1e-6);
        assertEquals(2 * data[0], M[length_eq + 1][1], 1e-6);
        assertEquals(0.0, M[length_eq + 1][2], 1e-6);
        assertEquals(0.0, M[length_eq + 1][3], 1e-6);
        assertEquals(0.0, M[length_eq + 1][4], 1e-6);

        // 2x * ddx + 2dx + 2y * ddy + 2dy
        assertEquals(2 * data[2], M[length_eq + 2][0], 1e-6);
        assertEquals(4 * data[1], M[length_eq + 2][1], 1e-6);
        assertEquals(2 * data[0], M[length_eq + 2][2], 1e-6);
        assertEquals(0.0, M[length_eq + 2][3], 1e-6);
        assertEquals(2 * dae.data[3][0], M[length_eq + 2][4], 1e-6);

        // ddy = F*y - g
        assertEquals(0.0, M[yaccel_eq][0], 1e-6);
        assertEquals(0.0, M[yaccel_eq][1], 1e-6);
        assertEquals(0.0, M[yaccel_eq][2], 1e-6);
        assertEquals(-dae.data[3][0], M[yaccel_eq][3], 1e-6);
        assertEquals(1.0, M[yaccel_eq][4], 1e-6);

        // ddx = F*x
        assertEquals(-data[3], M[xaccel_eq][0], 1e-6);
        assertEquals(0.0, M[xaccel_eq][1], 1e-6);
        assertEquals(1.0, M[xaccel_eq][2], 1e-6);
        assertEquals(-data[0], M[xaccel_eq][3], 1e-6);
        assertEquals(0.0, M[xaccel_eq][4], 1e-6);
    }
}

From source file:org.orekit.propagation.conversion.AbstractPropagatorConverter.java

/** Find the propagator that minimize the mean square error for a sample of {@link SpacecraftState states}.
 * @param initial initial estimation parameters (position, velocity, free parameters)
 * @return fitted parameters/*from  ww w  .ja  v a2s .c o  m*/
 * @exception OrekitException if propagator cannot be adapted
 * @exception MaxCountExceededException if maximal number of iterations is exceeded
 */
private double[] fit(final double[] initial) throws OrekitException, MaxCountExceededException {

    final MultivariateVectorFunction f = getObjectiveFunction();
    final MultivariateMatrixFunction jac = getObjectiveFunctionJacobian();
    final MultivariateJacobianFunction fJac = new MultivariateJacobianFunction() {
        /** {@inheritDoc} */
        @Override
        public Pair<RealVector, RealMatrix> value(final RealVector point) {
            final double[] p = point.toArray();
            return new Pair<RealVector, RealMatrix>(MatrixUtils.createRealVector(f.value(p)),
                    MatrixUtils.createRealMatrix(jac.value(p)));
        }
    };
    final LeastSquaresProblem problem = new LeastSquaresBuilder().maxIterations(maxIterations)
            .maxEvaluations(Integer.MAX_VALUE).model(fJac).target(target).weight(new DiagonalMatrix(weight))
            .start(initial).checker(checker).build();
    optimum = optimizer.optimize(problem);
    return optimum.getPoint().toArray();
}