Java Code Examples for org.apache.commons.math3.analysis.differentiation.DerivativeStructure#getPartialDerivative()

The following examples show how to use org.apache.commons.math3.analysis.differentiation.DerivativeStructure#getPartialDerivative() . 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: NewtonRaphsonSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve()
    throws TooManyEvaluationsException {
    final double startValue = getStartValue();
    final double absoluteAccuracy = getAbsoluteAccuracy();

    double x0 = startValue;
    double x1;
    while (true) {
        final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0);
        x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1));
        if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
            return x1;
        }

        x0 = x1;
    }
}
 
Example 2
Source File: NewtonRaphsonSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve()
    throws TooManyEvaluationsException {
    final double startValue = getStartValue();
    final double absoluteAccuracy = getAbsoluteAccuracy();

    double x0 = startValue;
    double x1;
    while (true) {
        final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0);
        x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1));
        if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
            return x1;
        }

        x0 = x1;
    }
}
 
Example 3
Source File: NewtonRaphsonSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve()
    throws TooManyEvaluationsException {
    final double startValue = getStartValue();
    final double absoluteAccuracy = getAbsoluteAccuracy();

    double x0 = startValue;
    double x1;
    while (true) {
        final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0);
        x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1));
        if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
            return x1;
        }

        x0 = x1;
    }
}
 
Example 4
Source File: NewtonRaphsonSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve()
    throws TooManyEvaluationsException {
    final double startValue = getStartValue();
    final double absoluteAccuracy = getAbsoluteAccuracy();

    double x0 = startValue;
    double x1;
    while (true) {
        final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0);
        x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1));
        if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
            return x1;
        }

        x0 = x1;
    }
}
 
Example 5
Source File: NewtonRaphsonSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve()
    throws TooManyEvaluationsException {
    final double startValue = getStartValue();
    final double absoluteAccuracy = getAbsoluteAccuracy();

    double x0 = startValue;
    double x1;
    while (true) {
        final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0);
        x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1));
        if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
            return x1;
        }

        x0 = x1;
    }
}
 
Example 6
Source File: NewtonRaphsonSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve()
    throws TooManyEvaluationsException {
    final double startValue = getStartValue();
    final double absoluteAccuracy = getAbsoluteAccuracy();

    double x0 = startValue;
    double x1;
    while (true) {
        final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0);
        x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1));
        if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
            return x1;
        }

        x0 = x1;
    }
}
 
Example 7
Source File: FunctionUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Convert a {@link DifferentiableUnivariateFunction} into a {@link UnivariateDifferentiableFunction}.
 * <p>
 * Note that the converted function is able to handle {@link DerivativeStructure} up to order one.
 * If the function is called with higher order, a {@link NumberIsTooLargeException} will be thrown.
 * </p>
 * @param f function to convert
 * @return converted function
 * @deprecated this conversion method is temporary in version 3.1, as the {@link
 * DifferentiableUnivariateFunction} interface itself is deprecated
 */
@Deprecated
public static UnivariateDifferentiableFunction toUnivariateDifferential(final DifferentiableUnivariateFunction f) {
    return new UnivariateDifferentiableFunction() {

        /** {@inheritDoc} */
        public double value(final double x) {
            return f.value(x);
        }

        /** {@inheritDoc}
         * @exception NumberIsTooLargeException if derivation order is greater than 1
         */
        public DerivativeStructure value(final DerivativeStructure t)
            throws NumberIsTooLargeException {
            switch (t.getOrder()) {
                case 0 :
                    return new DerivativeStructure(t.getFreeParameters(), 0, f.value(t.getValue()));
                case 1 : {
                    final int parameters = t.getFreeParameters();
                    final double[] derivatives = new double[parameters + 1];
                    derivatives[0] = f.value(t.getValue());
                    final double fPrime = f.derivative().value(t.getValue());
                    int[] orders = new int[parameters];
                    for (int i = 0; i < parameters; ++i) {
                        orders[i] = 1;
                        derivatives[i + 1] = fPrime * t.getPartialDerivative(orders);
                        orders[i] = 0;
                    }
                    return new DerivativeStructure(parameters, 1, derivatives);
                }
                default :
                    throw new NumberIsTooLargeException(t.getOrder(), 1, true);
            }
        }

    };
}
 
Example 8
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGradient() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.1) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.1) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 1, 0, r),
                                                            new DerivativeStructure(3, 1, 1, theta),
                                                            new DerivativeStructure(3, 1, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 1, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 1, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 1, 2, sc.getCartesian().getZ()));
                Vector3D refCGradient = new Vector3D(cvalue.getPartialDerivative(1, 0, 0),
                                                     cvalue.getPartialDerivative(0, 1, 0),
                                                     cvalue.getPartialDerivative(0, 0, 1));

                Vector3D testCGradient = new Vector3D(sc.toCartesianGradient(sGradient));
                
                Assert.assertEquals(0, testCGradient.distance(refCGradient) / refCGradient.getNorm(), 5.0e-14);

            }
        }
    }
}
 
Example 9
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGradient() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.1) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.1) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 1, 0, r),
                                                            new DerivativeStructure(3, 1, 1, theta),
                                                            new DerivativeStructure(3, 1, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 1, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 1, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 1, 2, sc.getCartesian().getZ()));
                Vector3D refCGradient = new Vector3D(cvalue.getPartialDerivative(1, 0, 0),
                                                     cvalue.getPartialDerivative(0, 1, 0),
                                                     cvalue.getPartialDerivative(0, 0, 1));

                Vector3D testCGradient = new Vector3D(sc.toCartesianGradient(sGradient));
                
                Assert.assertEquals(0, testCGradient.distance(refCGradient) / refCGradient.getNorm(), 5.0e-14);

            }
        }
    }
}
 
Example 10
Source File: FunctionUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Convert a {@link DifferentiableUnivariateFunction} into a {@link UnivariateDifferentiableFunction}.
 * <p>
 * Note that the converted function is able to handle {@link DerivativeStructure} up to order one.
 * If the function is called with higher order, a {@link NumberIsTooLargeException} will be thrown.
 * </p>
 * @param f function to convert
 * @return converted function
 * @deprecated this conversion method is temporary in version 3.1, as the {@link
 * DifferentiableUnivariateFunction} interface itself is deprecated
 */
@Deprecated
public static UnivariateDifferentiableFunction toUnivariateDifferential(final DifferentiableUnivariateFunction f) {
    return new UnivariateDifferentiableFunction() {

        /** {@inheritDoc} */
        public double value(final double x) {
            return f.value(x);
        }

        /** {@inheritDoc}
         * @exception NumberIsTooLargeException if derivation order is greater than 1
         */
        public DerivativeStructure value(final DerivativeStructure t)
            throws NumberIsTooLargeException {
            switch (t.getOrder()) {
                case 0 :
                    return new DerivativeStructure(t.getFreeParameters(), 0, f.value(t.getValue()));
                case 1 : {
                    final int parameters = t.getFreeParameters();
                    final double[] derivatives = new double[parameters + 1];
                    derivatives[0] = f.value(t.getValue());
                    final double fPrime = f.derivative().value(t.getValue());
                    int[] orders = new int[parameters];
                    for (int i = 0; i < parameters; ++i) {
                        orders[i] = 1;
                        derivatives[i + 1] = fPrime * t.getPartialDerivative(orders);
                        orders[i] = 0;
                    }
                    return new DerivativeStructure(parameters, 1, derivatives);
                }
                default :
                    throw new NumberIsTooLargeException(t.getOrder(), 1, true);
            }
        }

    };
}
 
Example 11
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGradient() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.1) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.1) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 1, 0, r),
                                                            new DerivativeStructure(3, 1, 1, theta),
                                                            new DerivativeStructure(3, 1, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 1, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 1, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 1, 2, sc.getCartesian().getZ()));
                Vector3D refCGradient = new Vector3D(cvalue.getPartialDerivative(1, 0, 0),
                                                     cvalue.getPartialDerivative(0, 1, 0),
                                                     cvalue.getPartialDerivative(0, 0, 1));

                Vector3D testCGradient = new Vector3D(sc.toCartesianGradient(sGradient));
                
                Assert.assertEquals(0, testCGradient.distance(refCGradient) / refCGradient.getNorm(), 5.0e-14);

            }
        }
    }
}
 
Example 12
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGradient() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.1) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.1) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 1, 0, r),
                                                            new DerivativeStructure(3, 1, 1, theta),
                                                            new DerivativeStructure(3, 1, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 1, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 1, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 1, 2, sc.getCartesian().getZ()));
                Vector3D refCGradient = new Vector3D(cvalue.getPartialDerivative(1, 0, 0),
                                                     cvalue.getPartialDerivative(0, 1, 0),
                                                     cvalue.getPartialDerivative(0, 0, 1));

                Vector3D testCGradient = new Vector3D(sc.toCartesianGradient(sGradient));
                
                Assert.assertEquals(0, testCGradient.distance(refCGradient) / refCGradient.getNorm(), 5.0e-14);

            }
        }
    }
}
 
Example 13
Source File: FunctionUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Convert a {@link DifferentiableUnivariateFunction} into a {@link UnivariateDifferentiableFunction}.
 * <p>
 * Note that the converted function is able to handle {@link DerivativeStructure} up to order one.
 * If the function is called with higher order, a {@link NumberIsTooLargeException} will be thrown.
 * </p>
 * @param f function to convert
 * @return converted function
 * @deprecated this conversion method is temporary in version 3.1, as the {@link
 * DifferentiableUnivariateFunction} interface itself is deprecated
 */
@Deprecated
public static UnivariateDifferentiableFunction toUnivariateDifferential(final DifferentiableUnivariateFunction f) {
    return new UnivariateDifferentiableFunction() {

        /** {@inheritDoc} */
        public double value(final double x) {
            return f.value(x);
        }

        /** {@inheritDoc}
         * @exception NumberIsTooLargeException if derivation order is greater than 1
         */
        public DerivativeStructure value(final DerivativeStructure t)
            throws NumberIsTooLargeException {
            switch (t.getOrder()) {
                case 0 :
                    return new DerivativeStructure(t.getFreeParameters(), 0, f.value(t.getValue()));
                case 1 : {
                    final int parameters = t.getFreeParameters();
                    final double[] derivatives = new double[parameters + 1];
                    derivatives[0] = f.value(t.getValue());
                    final double fPrime = f.derivative().value(t.getValue());
                    int[] orders = new int[parameters];
                    for (int i = 0; i < parameters; ++i) {
                        orders[i] = 1;
                        derivatives[i + 1] = fPrime * t.getPartialDerivative(orders);
                        orders[i] = 0;
                    }
                    return new DerivativeStructure(parameters, 1, derivatives);
                }
                default :
                    throw new NumberIsTooLargeException(t.getOrder(), 1, true);
            }
        }

    };
}
 
Example 14
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGradient() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.1) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.1) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 1, 0, r),
                                                            new DerivativeStructure(3, 1, 1, theta),
                                                            new DerivativeStructure(3, 1, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 1, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 1, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 1, 2, sc.getCartesian().getZ()));
                Vector3D refCGradient = new Vector3D(cvalue.getPartialDerivative(1, 0, 0),
                                                     cvalue.getPartialDerivative(0, 1, 0),
                                                     cvalue.getPartialDerivative(0, 0, 1));

                Vector3D testCGradient = new Vector3D(sc.toCartesianGradient(sGradient));
                
                Assert.assertEquals(0, testCGradient.distance(refCGradient) / refCGradient.getNorm(), 5.0e-14);

            }
        }
    }
}
 
Example 15
Source File: FunctionUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Convert a {@link DifferentiableUnivariateFunction} into a {@link UnivariateDifferentiableFunction}.
 * <p>
 * Note that the converted function is able to handle {@link DerivativeStructure} up to order one.
 * If the function is called with higher order, a {@link NumberIsTooLargeException} will be thrown.
 * </p>
 * @param f function to convert
 * @return converted function
 * @deprecated this conversion method is temporary in version 3.1, as the {@link
 * DifferentiableUnivariateFunction} interface itself is deprecated
 */
@Deprecated
public static UnivariateDifferentiableFunction toUnivariateDifferential(final DifferentiableUnivariateFunction f) {
    return new UnivariateDifferentiableFunction() {

        /** {@inheritDoc} */
        public double value(final double x) {
            return f.value(x);
        }

        /** {@inheritDoc}
         * @exception NumberIsTooLargeException if derivation order is greater than 1
         */
        public DerivativeStructure value(final DerivativeStructure t)
            throws NumberIsTooLargeException {
            switch (t.getOrder()) {
                case 0 :
                    return new DerivativeStructure(t.getFreeParameters(), 0, f.value(t.getValue()));
                case 1 : {
                    final int parameters = t.getFreeParameters();
                    final double[] derivatives = new double[parameters + 1];
                    derivatives[0] = f.value(t.getValue());
                    final double fPrime = f.derivative().value(t.getValue());
                    int[] orders = new int[parameters];
                    for (int i = 0; i < parameters; ++i) {
                        orders[i] = 1;
                        derivatives[i + 1] = fPrime * t.getPartialDerivative(orders);
                        orders[i] = 0;
                    }
                    return new DerivativeStructure(parameters, 1, derivatives);
                }
                default :
                    throw new NumberIsTooLargeException(t.getOrder(), 1, true);
            }
        }

    };
}
 
Example 16
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testHessian() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.2) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.2) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 2, 0, r),
                                                            new DerivativeStructure(3, 2, 1, theta),
                                                            new DerivativeStructure(3, 2, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };
                double[][] sHessian = new double[3][3];
                sHessian[0][0] = svalue.getPartialDerivative(2, 0, 0); // d2F/dR2
                sHessian[1][0] = svalue.getPartialDerivative(1, 1, 0); // d2F/dRdTheta
                sHessian[2][0] = svalue.getPartialDerivative(1, 0, 1); // d2F/dRdPhi
                sHessian[0][1] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][1] = svalue.getPartialDerivative(0, 2, 0); // d2F/dTheta2
                sHessian[2][1] = svalue.getPartialDerivative(0, 1, 1); // d2F/dThetadPhi
                sHessian[0][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[2][2] = svalue.getPartialDerivative(0, 0, 2); // d2F/dPhi2

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 2, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 2, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 2, 2, sc.getCartesian().getZ()));
                double[][] refCHessian = new double[3][3];
                refCHessian[0][0] = cvalue.getPartialDerivative(2, 0, 0); // d2F/dX2
                refCHessian[1][0] = cvalue.getPartialDerivative(1, 1, 0); // d2F/dXdY
                refCHessian[2][0] = cvalue.getPartialDerivative(1, 0, 1); // d2F/dXdZ
                refCHessian[0][1] = refCHessian[1][0];
                refCHessian[1][1] = cvalue.getPartialDerivative(0, 2, 0); // d2F/dY2
                refCHessian[2][1] = cvalue.getPartialDerivative(0, 1, 1); // d2F/dYdZ
                refCHessian[0][2] = refCHessian[2][0];
                refCHessian[1][2] = refCHessian[2][1];
                refCHessian[2][2] = cvalue.getPartialDerivative(0, 0, 2); // d2F/dZ2
                double norm =  0;
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        norm = FastMath.max(norm, FastMath.abs(refCHessian[i][j]));
                    }
                }

                double[][] testCHessian = sc.toCartesianHessian(sHessian, sGradient);
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        Assert.assertEquals("" + FastMath.abs((refCHessian[i][j] - testCHessian[i][j]) / norm),
                                            refCHessian[i][j], testCHessian[i][j], 1.0e-14 * norm);
                    }
                }

            }
        }
    }
}
 
Example 17
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testHessian() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.2) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.2) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 2, 0, r),
                                                            new DerivativeStructure(3, 2, 1, theta),
                                                            new DerivativeStructure(3, 2, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };
                double[][] sHessian = new double[3][3];
                sHessian[0][0] = svalue.getPartialDerivative(2, 0, 0); // d2F/dR2
                sHessian[1][0] = svalue.getPartialDerivative(1, 1, 0); // d2F/dRdTheta
                sHessian[2][0] = svalue.getPartialDerivative(1, 0, 1); // d2F/dRdPhi
                sHessian[0][1] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][1] = svalue.getPartialDerivative(0, 2, 0); // d2F/dTheta2
                sHessian[2][1] = svalue.getPartialDerivative(0, 1, 1); // d2F/dThetadPhi
                sHessian[0][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[2][2] = svalue.getPartialDerivative(0, 0, 2); // d2F/dPhi2

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 2, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 2, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 2, 2, sc.getCartesian().getZ()));
                double[][] refCHessian = new double[3][3];
                refCHessian[0][0] = cvalue.getPartialDerivative(2, 0, 0); // d2F/dX2
                refCHessian[1][0] = cvalue.getPartialDerivative(1, 1, 0); // d2F/dXdY
                refCHessian[2][0] = cvalue.getPartialDerivative(1, 0, 1); // d2F/dXdZ
                refCHessian[0][1] = refCHessian[1][0];
                refCHessian[1][1] = cvalue.getPartialDerivative(0, 2, 0); // d2F/dY2
                refCHessian[2][1] = cvalue.getPartialDerivative(0, 1, 1); // d2F/dYdZ
                refCHessian[0][2] = refCHessian[2][0];
                refCHessian[1][2] = refCHessian[2][1];
                refCHessian[2][2] = cvalue.getPartialDerivative(0, 0, 2); // d2F/dZ2
                double norm =  0;
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        norm = FastMath.max(norm, FastMath.abs(refCHessian[i][j]));
                    }
                }

                double[][] testCHessian = sc.toCartesianHessian(sHessian, sGradient);
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        Assert.assertEquals("" + FastMath.abs((refCHessian[i][j] - testCHessian[i][j]) / norm),
                                            refCHessian[i][j], testCHessian[i][j], 1.0e-14 * norm);
                    }
                }

            }
        }
    }
}
 
Example 18
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testHessian() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.2) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.2) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 2, 0, r),
                                                            new DerivativeStructure(3, 2, 1, theta),
                                                            new DerivativeStructure(3, 2, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };
                double[][] sHessian = new double[3][3];
                sHessian[0][0] = svalue.getPartialDerivative(2, 0, 0); // d2F/dR2
                sHessian[1][0] = svalue.getPartialDerivative(1, 1, 0); // d2F/dRdTheta
                sHessian[2][0] = svalue.getPartialDerivative(1, 0, 1); // d2F/dRdPhi
                sHessian[0][1] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][1] = svalue.getPartialDerivative(0, 2, 0); // d2F/dTheta2
                sHessian[2][1] = svalue.getPartialDerivative(0, 1, 1); // d2F/dThetadPhi
                sHessian[0][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[2][2] = svalue.getPartialDerivative(0, 0, 2); // d2F/dPhi2

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 2, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 2, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 2, 2, sc.getCartesian().getZ()));
                double[][] refCHessian = new double[3][3];
                refCHessian[0][0] = cvalue.getPartialDerivative(2, 0, 0); // d2F/dX2
                refCHessian[1][0] = cvalue.getPartialDerivative(1, 1, 0); // d2F/dXdY
                refCHessian[2][0] = cvalue.getPartialDerivative(1, 0, 1); // d2F/dXdZ
                refCHessian[0][1] = refCHessian[1][0];
                refCHessian[1][1] = cvalue.getPartialDerivative(0, 2, 0); // d2F/dY2
                refCHessian[2][1] = cvalue.getPartialDerivative(0, 1, 1); // d2F/dYdZ
                refCHessian[0][2] = refCHessian[2][0];
                refCHessian[1][2] = refCHessian[2][1];
                refCHessian[2][2] = cvalue.getPartialDerivative(0, 0, 2); // d2F/dZ2
                double norm =  0;
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        norm = FastMath.max(norm, FastMath.abs(refCHessian[i][j]));
                    }
                }

                double[][] testCHessian = sc.toCartesianHessian(sHessian, sGradient);
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        Assert.assertEquals("" + FastMath.abs((refCHessian[i][j] - testCHessian[i][j]) / norm),
                                            refCHessian[i][j], testCHessian[i][j], 1.0e-14 * norm);
                    }
                }

            }
        }
    }
}
 
Example 19
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testHessian() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.2) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.2) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 2, 0, r),
                                                            new DerivativeStructure(3, 2, 1, theta),
                                                            new DerivativeStructure(3, 2, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };
                double[][] sHessian = new double[3][3];
                sHessian[0][0] = svalue.getPartialDerivative(2, 0, 0); // d2F/dR2
                sHessian[1][0] = svalue.getPartialDerivative(1, 1, 0); // d2F/dRdTheta
                sHessian[2][0] = svalue.getPartialDerivative(1, 0, 1); // d2F/dRdPhi
                sHessian[0][1] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][1] = svalue.getPartialDerivative(0, 2, 0); // d2F/dTheta2
                sHessian[2][1] = svalue.getPartialDerivative(0, 1, 1); // d2F/dThetadPhi
                sHessian[0][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[2][2] = svalue.getPartialDerivative(0, 0, 2); // d2F/dPhi2

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 2, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 2, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 2, 2, sc.getCartesian().getZ()));
                double[][] refCHessian = new double[3][3];
                refCHessian[0][0] = cvalue.getPartialDerivative(2, 0, 0); // d2F/dX2
                refCHessian[1][0] = cvalue.getPartialDerivative(1, 1, 0); // d2F/dXdY
                refCHessian[2][0] = cvalue.getPartialDerivative(1, 0, 1); // d2F/dXdZ
                refCHessian[0][1] = refCHessian[1][0];
                refCHessian[1][1] = cvalue.getPartialDerivative(0, 2, 0); // d2F/dY2
                refCHessian[2][1] = cvalue.getPartialDerivative(0, 1, 1); // d2F/dYdZ
                refCHessian[0][2] = refCHessian[2][0];
                refCHessian[1][2] = refCHessian[2][1];
                refCHessian[2][2] = cvalue.getPartialDerivative(0, 0, 2); // d2F/dZ2
                double norm =  0;
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        norm = FastMath.max(norm, FastMath.abs(refCHessian[i][j]));
                    }
                }

                double[][] testCHessian = sc.toCartesianHessian(sHessian, sGradient);
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        Assert.assertEquals("" + FastMath.abs((refCHessian[i][j] - testCHessian[i][j]) / norm),
                                            refCHessian[i][j], testCHessian[i][j], 1.0e-14 * norm);
                    }
                }

            }
        }
    }
}
 
Example 20
Source File: SphericalCoordinatesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testHessian() {
    for (double r = 0.2; r < 10; r += 0.5) {
        for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.2) {
            for (double phi = 0.1; phi < FastMath.PI; phi += 0.2) {
                SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi);

                DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 2, 0, r),
                                                            new DerivativeStructure(3, 2, 1, theta),
                                                            new DerivativeStructure(3, 2, 2, phi));
                double[] sGradient = new double[] {
                    svalue.getPartialDerivative(1, 0, 0),
                    svalue.getPartialDerivative(0, 1, 0),
                    svalue.getPartialDerivative(0, 0, 1),
                };
                double[][] sHessian = new double[3][3];
                sHessian[0][0] = svalue.getPartialDerivative(2, 0, 0); // d2F/dR2
                sHessian[1][0] = svalue.getPartialDerivative(1, 1, 0); // d2F/dRdTheta
                sHessian[2][0] = svalue.getPartialDerivative(1, 0, 1); // d2F/dRdPhi
                sHessian[0][1] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][1] = svalue.getPartialDerivative(0, 2, 0); // d2F/dTheta2
                sHessian[2][1] = svalue.getPartialDerivative(0, 1, 1); // d2F/dThetadPhi
                sHessian[0][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[1][2] = Double.NaN; // just to check upper-right part is not used
                sHessian[2][2] = svalue.getPartialDerivative(0, 0, 2); // d2F/dPhi2

                DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 2, 0, sc.getCartesian().getX()),
                                                            new DerivativeStructure(3, 2, 1, sc.getCartesian().getY()),
                                                            new DerivativeStructure(3, 2, 2, sc.getCartesian().getZ()));
                double[][] refCHessian = new double[3][3];
                refCHessian[0][0] = cvalue.getPartialDerivative(2, 0, 0); // d2F/dX2
                refCHessian[1][0] = cvalue.getPartialDerivative(1, 1, 0); // d2F/dXdY
                refCHessian[2][0] = cvalue.getPartialDerivative(1, 0, 1); // d2F/dXdZ
                refCHessian[0][1] = refCHessian[1][0];
                refCHessian[1][1] = cvalue.getPartialDerivative(0, 2, 0); // d2F/dY2
                refCHessian[2][1] = cvalue.getPartialDerivative(0, 1, 1); // d2F/dYdZ
                refCHessian[0][2] = refCHessian[2][0];
                refCHessian[1][2] = refCHessian[2][1];
                refCHessian[2][2] = cvalue.getPartialDerivative(0, 0, 2); // d2F/dZ2
                double norm =  0;
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        norm = FastMath.max(norm, FastMath.abs(refCHessian[i][j]));
                    }
                }

                double[][] testCHessian = sc.toCartesianHessian(sHessian, sGradient);
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        Assert.assertEquals("" + FastMath.abs((refCHessian[i][j] - testCHessian[i][j]) / norm),
                                            refCHessian[i][j], testCHessian[i][j], 1.0e-14 * norm);
                    }
                }

            }
        }
    }
}