org.apache.commons.math3.analysis.differentiation.DerivativeStructure Java Examples

The following examples show how to use org.apache.commons.math3.analysis.differentiation.DerivativeStructure. 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: FieldVector3DTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAdd() {
    FieldVector3D<DerivativeStructure> v1 = createVector(1, 2, 3, 3);
    FieldVector3D<DerivativeStructure> v2 = createVector(-3, -2, -1, 3);
    v1 = v1.add(v2);
    checkVector(v1, -2, 0, 2, 2, 0, 0, 0, 2, 0, 0, 0, 2);

    checkVector(v2.add(v1), -5, -2, 1, 3, 0, 0, 0, 3, 0, 0, 0, 3);
    checkVector(v2.add(new Vector3D(-2, 0, 2)), -5, -2, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1);
    checkVector(v2.add(3, v1), -9, -2, 5, 7, 0, 0, 0, 7, 0, 0, 0, 7);
    checkVector(v2.add(3, new Vector3D(-2, 0, 2)), -9, -2, 5, 1, 0, 0, 0, 1, 0, 0, 0, 1);
    checkVector(v2.add(new DerivativeStructure(3, 1, 2, 3), new Vector3D(-2, 0, 2)),
                -9, -2, 5, 1, 0, -2, 0, 1, 0, 0, 0, 3);

    checkVector(createVector(1, 2, 3, 4).add(new DerivativeStructure(4, 1, 3, 5.0),
                                             createVector(3, -2, 1, 4)),
                16, -8,  8,
                 6,  0,  0,  3,
                 0,  6,  0, -2,
                 0,  0,  6,  1);

}
 
Example #2
Source File: StatisticalReferenceDatasetFactory.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static StatisticalReferenceDataset createLanczos1()
    throws IOException {
    final BufferedReader in =
        createBufferedReaderFromResource("Lanczos1.dat");
    StatisticalReferenceDataset dataset = null;
    try {
        dataset = new StatisticalReferenceDataset(in) {

            @Override
            public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) {
                return a[0].multiply(a[3].multiply(-x).exp()).add(
                            a[1].multiply(a[4].multiply(-x).exp())).add(
                                 a[2].multiply(a[5].multiply(-x).exp()));
            }

        };
    } finally {
        in.close();
    }
    return dataset;
}
 
Example #3
Source File: FieldRotationDSTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testVectorOnePair() throws MathArithmeticException {

    FieldVector3D<DerivativeStructure> u = createVector(3, 2, 1);
    FieldVector3D<DerivativeStructure> v = createVector(-4, 2, 2);
    FieldRotation<DerivativeStructure> r = new FieldRotation<DerivativeStructure>(u, v);
    checkVector(r.applyTo(u.scalarMultiply(v.getNorm())), v.scalarMultiply(u.getNorm()));

    checkAngle(new FieldRotation<DerivativeStructure>(u, u.negate()).getAngle(), FastMath.PI);

    try {
        new FieldRotation<DerivativeStructure>(u, createVector(0, 0, 0));
        Assert.fail("an exception should have been thrown");
    } catch (MathArithmeticException e) {
        // expected behavior
    }

}
 
Example #4
Source File: FieldVector3DTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAngularSeparation() throws MathArithmeticException {
    FieldVector3D<DerivativeStructure> v1 = createVector(2, -1, 4, 3);

    FieldVector3D<DerivativeStructure>  k = v1.normalize();
    FieldVector3D<DerivativeStructure>  i = k.orthogonal();
    FieldVector3D<DerivativeStructure> v2 = k.scalarMultiply(FastMath.cos(1.2)).add(i.scalarMultiply(FastMath.sin(1.2)));

    Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1, v2).getReal() - 1.2) < 1.0e-12);
    Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1, v2.toVector3D()).getReal() - 1.2) < 1.0e-12);
    Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1.toVector3D(), v2).getReal() - 1.2) < 1.0e-12);

    try {
        FieldVector3D.angle(v1, Vector3D.ZERO);
        Assert.fail("an exception should have been thrown");
    } catch (MathArithmeticException mae) {
        // expected
    }
    Assert.assertEquals(0.0, FieldVector3D.angle(v1, v1.toVector3D()).getReal(), 1.0e-15);
    Assert.assertEquals(FastMath.PI, FieldVector3D.angle(v1, v1.negate().toVector3D()).getReal(), 1.0e-15);

}
 
Example #5
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    DerivativeStructure x3 = variables[2];
    DerivativeStructure x4 = variables[3];
    DerivativeStructure x5 = variables[4];
    DerivativeStructure[] f = new DerivativeStructure[m];
  for (int i = 0; i < m; ++i) {
    double temp = 10.0 * i;
    DerivativeStructure tmp1 = x4.multiply(-temp).exp();
    DerivativeStructure tmp2 = x5.multiply(-temp).exp();
    f[i] = x1.add(x2.multiply(tmp1)).add(x3.multiply(tmp2)).negate().add(y[i]);
  }
  return f;
}
 
Example #6
Source File: FieldVector3DTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAdd() {
    FieldVector3D<DerivativeStructure> v1 = createVector(1, 2, 3, 3);
    FieldVector3D<DerivativeStructure> v2 = createVector(-3, -2, -1, 3);
    v1 = v1.add(v2);
    checkVector(v1, -2, 0, 2, 2, 0, 0, 0, 2, 0, 0, 0, 2);

    checkVector(v2.add(v1), -5, -2, 1, 3, 0, 0, 0, 3, 0, 0, 0, 3);
    checkVector(v2.add(new Vector3D(-2, 0, 2)), -5, -2, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1);
    checkVector(v2.add(3, v1), -9, -2, 5, 7, 0, 0, 0, 7, 0, 0, 0, 7);
    checkVector(v2.add(3, new Vector3D(-2, 0, 2)), -9, -2, 5, 1, 0, 0, 0, 1, 0, 0, 0, 1);
    checkVector(v2.add(new DerivativeStructure(3, 1, 2, 3), new Vector3D(-2, 0, 2)),
                -9, -2, 5, 1, 0, -2, 0, 1, 0, 0, 0, 3);

    checkVector(createVector(1, 2, 3, 4).add(new DerivativeStructure(4, 1, 3, 5.0),
                                             createVector(3, -2, 1, 4)),
                16, -8,  8,
                 6,  0,  0,  3,
                 0,  6,  0, -2,
                 0,  0,  6,  1);

}
 
Example #7
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    DerivativeStructure x3 = variables[2];
    DerivativeStructure x4 = variables[3];
    DerivativeStructure x5 = variables[4];
    DerivativeStructure[] f = new DerivativeStructure[m];
  for (int i = 0; i < m; ++i) {
    double temp = 10.0 * i;
    DerivativeStructure tmp1 = x4.multiply(-temp).exp();
    DerivativeStructure tmp2 = x5.multiply(-temp).exp();
    f[i] = x1.add(x2.multiply(tmp1)).add(x3.multiply(tmp2)).negate().add(y[i]);
  }
  return f;
}
 
Example #8
Source File: PolynomialFunctionNewtonFormTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test for derivatives.
 */
@Test
public void testDerivative() {

    // x^3 = 0 * [1] + 1 * [x] + 3 * [x(x-1)] + 1 * [x(x-1)(x-2)]
    PolynomialFunctionNewtonForm p =
            new PolynomialFunctionNewtonForm(new double[] { 0, 1, 3, 1 },
                                             new double[] { 0, 1, 2 });

    double eps = 2.0e-14;
    for (double t = 0.0; t < 10.0; t += 0.1) {
        DerivativeStructure x = new DerivativeStructure(1, 4, 0, t);
        DerivativeStructure y = p.value(x);
        Assert.assertEquals(t * t * t,   y.getValue(),              eps * t * t * t);
        Assert.assertEquals(3.0 * t * t, y.getPartialDerivative(1), eps * 3.0 * t * t);
        Assert.assertEquals(6.0 * t,     y.getPartialDerivative(2), eps * 6.0 * t);
        Assert.assertEquals(6.0,         y.getPartialDerivative(3), eps * 6.0);
        Assert.assertEquals(0.0,         y.getPartialDerivative(4), eps);
    }

}
 
Example #9
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 #10
Source File: FieldVector3DTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testVectorialProducts() {
    FieldVector3D<DerivativeStructure> v1 = createVector(2, 1, -4, 3);
    FieldVector3D<DerivativeStructure> v2 = createVector(3, 1, -1, 3);

    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v2).getReal() - 11) < 1.0e-12);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v2.toVector3D()).getReal() - 11) < 1.0e-12);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1.toVector3D(), v2).getReal() - 11) < 1.0e-12);

    FieldVector3D<DerivativeStructure> v3 = FieldVector3D.crossProduct(v1, v2);
    checkVector(v3, 3, -10, -1);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12);

    v3 = FieldVector3D.crossProduct(v1, v2.toVector3D());
    checkVector(v3, 3, -10, -1);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12);

    v3 = FieldVector3D.crossProduct(v1.toVector3D(), v2);
    checkVector(v3, 3, -10, -1);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12);
    Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12);

}
 
Example #11
Source File: FieldVector3DTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testDistance1() {
    FieldVector3D<DerivativeStructure> v1 = createVector(1, -2, 3, 3);
    FieldVector3D<DerivativeStructure> v2 = createVector(-4, 2, 0, 3);
    Assert.assertEquals(0.0, FieldVector3D.distance1(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0);
    DerivativeStructure distance = FieldVector3D.distance1(v1, v2);
    Assert.assertEquals(12.0, distance.getReal(), 1.0e-12);
    Assert.assertEquals(0, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
    Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
    Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
    distance = FieldVector3D.distance1(v1, new Vector3D(-4, 2, 0));
    Assert.assertEquals(12.0, distance.getReal(), 1.0e-12);
    Assert.assertEquals( 1, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
    Assert.assertEquals(-1, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
    Assert.assertEquals( 1, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
    distance = FieldVector3D.distance1(new Vector3D(-4, 2, 0), v1);
    Assert.assertEquals(12.0, distance.getReal(), 1.0e-12);
    Assert.assertEquals( 1, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
    Assert.assertEquals(-1, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
    Assert.assertEquals( 1, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
}
 
Example #12
Source File: FieldVector3DTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAccurateCrossProduct() {
    // the vectors u1 and u2 are nearly but not exactly anti-parallel
    // (7.31e-16 degrees from 180 degrees) naive cross product (i.e.
    // computing u1.x * u2.x + u1.y * u2.y + u1.z * u2.z
    // leads to a result of   [0.0009765, -0.0001220, -0.0039062],
    // instead of the correct [0.0006913, -0.0001254, -0.0007909]
    final FieldVector3D<DerivativeStructure> u1 = createVector(-1321008684645961.0 /   268435456.0,
                                     -5774608829631843.0 /   268435456.0,
                                     -7645843051051357.0 /  8589934592.0, 3);
    final FieldVector3D<DerivativeStructure> u2 = createVector( 1796571811118507.0 /  2147483648.0,
                                      7853468008299307.0 /  2147483648.0,
                                      2599586637357461.0 / 17179869184.0, 3);
    final FieldVector3D<DerivativeStructure> u3 = createVector(12753243807587107.0 / 18446744073709551616.0, 
                                     -2313766922703915.0 / 18446744073709551616.0, 
                                      -227970081415313.0 /   288230376151711744.0, 3);
    FieldVector3D<DerivativeStructure> cNaive = new FieldVector3D<DerivativeStructure>(u1.getY().multiply(u2.getZ()).subtract(u1.getZ().multiply(u2.getY())),
                                   u1.getZ().multiply(u2.getX()).subtract(u1.getX().multiply(u2.getZ())),
                                   u1.getX().multiply(u2.getY()).subtract(u1.getY().multiply(u2.getX())));
    FieldVector3D<DerivativeStructure> cAccurate = FieldVector3D.crossProduct(u1, u2);
    Assert.assertTrue(FieldVector3D.distance(u3, cNaive).getReal() > 2.9 * u3.getNorm().getReal());
    Assert.assertEquals(0.0, FieldVector3D.distance(u3, cAccurate).getReal(), 1.0e-30 * cAccurate.getNorm().getReal());
}
 
Example #13
Source File: FieldRotationDSTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCompose() throws MathIllegalArgumentException {

    FieldRotation<DerivativeStructure> r1       = new FieldRotation<DerivativeStructure>(createVector(2, -3, 5), createAngle(1.7));
    FieldRotation<DerivativeStructure> r2       = new FieldRotation<DerivativeStructure>(createVector(-1, 3, 2), createAngle(0.3));
    FieldRotation<DerivativeStructure> r3       = r2.applyTo(r1);
    FieldRotation<DerivativeStructure> r3Double = r2.applyTo(new Rotation(r1.getQ0().getReal(),
                                                  r1.getQ1().getReal(),
                                                  r1.getQ2().getReal(),
                                                  r1.getQ3().getReal(),
                                                  false));

    for (double x = -0.9; x < 0.9; x += 0.2) {
        for (double y = -0.9; y < 0.9; y += 0.2) {
            for (double z = -0.9; z < 0.9; z += 0.2) {
                FieldVector3D<DerivativeStructure> u = createVector(x, y, z);
                checkVector(r2.applyTo(r1.applyTo(u)), r3.applyTo(u));
                checkVector(r2.applyTo(r1.applyTo(u)), r3Double.applyTo(u));
            }
        }
    }

}
 
Example #14
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public DerivativeStructure value(DerivativeStructure[] variables) {
    DerivativeStructure[] y = new DerivativeStructure[factors.getRowDimension()];
    for (int i = 0; i < y.length; ++i) {
        y[i] = variables[0].getField().getZero();
        for (int j = 0; j < factors.getColumnDimension(); ++j) {
            y[i] = y[i].add(variables[j].multiply(factors.getEntry(i, j)));
        }
    }

    DerivativeStructure sum = variables[0].getField().getZero();
    for (int i = 0; i < y.length; ++i) {
        DerivativeStructure ri = y[i].subtract(target[i]);
        sum = sum.add(ri.multiply(ri));
    }
    return sum;
}
 
Example #15
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public DerivativeStructure value(DerivativeStructure[] variables) {
    DerivativeStructure[] y = new DerivativeStructure[factors.getRowDimension()];
    for (int i = 0; i < y.length; ++i) {
        y[i] = variables[0].getField().getZero();
        for (int j = 0; j < factors.getColumnDimension(); ++j) {
            y[i] = y[i].add(variables[j].multiply(factors.getEntry(i, j)));
        }
    }

    DerivativeStructure sum = variables[0].getField().getZero();
    for (int i = 0; i < y.length; ++i) {
        DerivativeStructure ri = y[i].subtract(target[i]);
        sum = sum.add(ri.multiply(ri));
    }
    return sum;
}
 
Example #16
Source File: StatisticalReferenceDatasetFactory.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static StatisticalReferenceDataset createKirby2()
    throws IOException {
    final BufferedReader in = createBufferedReaderFromResource("Kirby2.dat");
    StatisticalReferenceDataset dataset = null;
    try {
        dataset = new StatisticalReferenceDataset(in) {

            @Override
            public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) {
                final DerivativeStructure p = a[0].add(a[1].add(a[2].multiply(x)).multiply(x));
                final DerivativeStructure q = a[3].add(a[4].multiply(x)).multiply(x).add(1.0);
                return p.divide(q);
            }

        };
    } finally {
        in.close();
    }
    return dataset;
}
 
Example #17
Source File: StatisticalReferenceDatasetFactory.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static StatisticalReferenceDataset createLanczos1()
    throws IOException {
    final BufferedReader in =
        createBufferedReaderFromResource("Lanczos1.dat");
    StatisticalReferenceDataset dataset = null;
    try {
        dataset = new StatisticalReferenceDataset(in) {

            @Override
            public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) {
                return a[0].multiply(a[3].multiply(-x).exp()).add(
                            a[1].multiply(a[4].multiply(-x).exp())).add(
                                 a[2].multiply(a[5].multiply(-x).exp()));
            }

        };
    } finally {
        in.close();
    }
    return dataset;
}
 
Example #18
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the Jacobian matrix.
 *
 * @param params Model parameters at which to compute the Jacobian.
 * @return the weighted Jacobian: W<sup>1/2</sup> J.
 * @throws DimensionMismatchException if the Jacobian dimension does not
 * match problem dimension.
 * @since 3.1
 */
protected RealMatrix computeWeightedJacobian(double[] params) {
    ++jacobianEvaluations;

    final DerivativeStructure[] dsPoint = new DerivativeStructure[params.length];
    final int nC = params.length;
    for (int i = 0; i < nC; ++i) {
        dsPoint[i] = new DerivativeStructure(nC, 1, i, params[i]);
    }
    final DerivativeStructure[] dsValue = jF.value(dsPoint);
    final int nR = getTarget().length;
    if (dsValue.length != nR) {
        throw new DimensionMismatchException(dsValue.length, nR);
    }
    final double[][] jacobianData = new double[nR][nC];
    for (int i = 0; i < nR; ++i) {
        int[] orders = new int[nC];
        for (int j = 0; j < nC; ++j) {
            orders[j] = 1;
            jacobianData[i][j] = dsValue[i].getPartialDerivative(orders);
            orders[j] = 0;
        }
    }

    return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(jacobianData));
}
 
Example #19
Source File: FieldRotationDSTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testVectorOnePair() throws MathArithmeticException {

    FieldVector3D<DerivativeStructure> u = createVector(3, 2, 1);
    FieldVector3D<DerivativeStructure> v = createVector(-4, 2, 2);
    FieldRotation<DerivativeStructure> r = new FieldRotation<DerivativeStructure>(u, v);
    checkVector(r.applyTo(u.scalarMultiply(v.getNorm())), v.scalarMultiply(u.getNorm()));

    checkAngle(new FieldRotation<DerivativeStructure>(u, u.negate()).getAngle(), FastMath.PI);

    try {
        new FieldRotation<DerivativeStructure>(u, createVector(0, 0, 0));
        Assert.fail("an exception should have been thrown");
    } catch (MathArithmeticException e) {
        // expected behavior
    }

}
 
Example #20
Source File: StatisticalReferenceDatasetFactory.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static StatisticalReferenceDataset createMGH17()
    throws IOException {
    final BufferedReader in = createBufferedReaderFromResource("MGH17.dat");
    StatisticalReferenceDataset dataset = null;
    try {
        dataset = new StatisticalReferenceDataset(in) {

            @Override
            public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) {
                return a[0].add(a[1].multiply(a[3].multiply(-x).exp())).add(a[2].multiply(a[4].multiply(-x).exp()));
            }

        };
    } finally {
        in.close();
    }
    return dataset;
}
 
Example #21
Source File: FieldRotationDSTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testArray() throws MathIllegalArgumentException {

    FieldRotation<DerivativeStructure> r = new FieldRotation<DerivativeStructure>(createAxis(2, -3, 5), createAngle(1.7));

    for (double x = -0.9; x < 0.9; x += 0.2) {
        for (double y = -0.9; y < 0.9; y += 0.2) {
            for (double z = -0.9; z < 0.9; z += 0.2) {
                FieldVector3D<DerivativeStructure> u = createVector(x, y, z);
                FieldVector3D<DerivativeStructure> v = r.applyTo(u);
                DerivativeStructure[] out = new DerivativeStructure[3];
                r.applyTo(new DerivativeStructure[] { u.getX(), u.getY(), u.getZ() }, out);
                Assert.assertEquals(v.getX().getReal(), out[0].getReal(), 1.0e-10);
                Assert.assertEquals(v.getY().getReal(), out[1].getReal(), 1.0e-10);
                Assert.assertEquals(v.getZ().getReal(), out[2].getReal(), 1.0e-10);
                r.applyInverseTo(out, out);
                Assert.assertEquals(u.getX().getReal(), out[0].getReal(), 1.0e-10);
                Assert.assertEquals(u.getY().getReal(), out[1].getReal(), 1.0e-10);
                Assert.assertEquals(u.getZ().getReal(), out[2].getReal(), 1.0e-10);
            }
        }
    }

}
 
Example #22
Source File: HermiteInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWikipedia() {
    // this test corresponds to the example from Wikipedia page:
    // http://en.wikipedia.org/wiki/Hermite_interpolation
    HermiteInterpolator interpolator = new HermiteInterpolator();
    interpolator.addSamplePoint(-1, new double[] { 2 }, new double[] { -8 }, new double[] { 56 });
    interpolator.addSamplePoint( 0, new double[] { 1 }, new double[] {  0 }, new double[] {  0 });
    interpolator.addSamplePoint( 1, new double[] { 2 }, new double[] {  8 }, new double[] { 56 });
    for (double x = -1.0; x <= 1.0; x += 0.125) {
        DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 1, 0, x))[0];
        double x2 = x * x;
        double x4 = x2 * x2;
        double x8 = x4 * x4;
        Assert.assertEquals(x8 + 1, y.getValue(), 1.0e-15);
        Assert.assertEquals(8 * x4 * x2 * x, y.getPartialDerivative(1), 1.0e-15);
    }
    checkPolynomial(new PolynomialFunction(new double[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }),
                    interpolator.getPolynomials()[0]);
}
 
Example #23
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    return new DerivativeStructure[] {
        x1.subtract(13.0).add(x2.negate().add(5.0).multiply(x2).subtract(2).multiply(x2)),
        x1.subtract(29.0).add(x2.add(1).multiply(x2).subtract(14).multiply(x2))
    };
}
 
Example #24
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public DerivativeStructure getRadius(DerivativeStructure cx, DerivativeStructure cy) {
    DerivativeStructure r = cx.getField().getZero();
    for (Vector2D point : points) {
        r = r.add(distance(point, cx, cy));
    }
    return r.divide(points.size());
}
 
Example #25
Source File: SincTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDerivatives1Dot2Normalized() {
    DerivativeStructure s = new Sinc(true).value(new DerivativeStructure(1, 5, 0, 1.2));
    Assert.assertEquals(-0.15591488063143983888, s.getPartialDerivative(0), 6.0e-17);
    Assert.assertEquals(-0.54425176145292298767, s.getPartialDerivative(1), 2.0e-16);
    Assert.assertEquals(2.4459044611635856107,   s.getPartialDerivative(2), 9.0e-16);
    Assert.assertEquals(0.5391369206235909586,   s.getPartialDerivative(3), 7.0e-16);
    Assert.assertEquals(-16.984649869728849865,  s.getPartialDerivative(4), 8.0e-15);
    Assert.assertEquals(5.0980327462666316586,   s.getPartialDerivative(5), 9.0e-15);
}
 
Example #26
Source File: FieldVector3DTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAngular() {
    Assert.assertEquals(0,           createVector(1, 0, 0, 3).getAlpha().getReal(), 1.0e-10);
    Assert.assertEquals(0,           createVector(1, 0, 0, 3).getDelta().getReal(), 1.0e-10);
    Assert.assertEquals(FastMath.PI / 2, createVector(0, 1, 0, 3).getAlpha().getReal(), 1.0e-10);
    Assert.assertEquals(0,           createVector(0, 1, 0, 3).getDelta().getReal(), 1.0e-10);
    Assert.assertEquals(FastMath.PI / 2, createVector(0, 0, 1, 3).getDelta().getReal(), 1.0e-10);
  
    FieldVector3D<DerivativeStructure> u = createVector(-1, 1, -1, 3);
    Assert.assertEquals(3 * FastMath.PI /4, u.getAlpha().getReal(), 1.0e-10);
    Assert.assertEquals(-1.0 / FastMath.sqrt(3), u.getDelta().sin().getReal(), 1.0e-10);
}
 
Example #27
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[] value(double[] variables) {
    DerivativeStructure[] dsV = new DerivativeStructure[variables.length];
    for (int i = 0; i < variables.length; ++i) {
        dsV[i] = new DerivativeStructure(0, 0, variables[i]);
    }
    DerivativeStructure[] dsY = value(dsV);
    double[] y = new double[dsY.length];
    for (int i = 0; i < dsY.length; ++i) {
        y[i] = dsY[i].getValue();
    }
    return y;
}
 
Example #28
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 #29
Source File: HermiteInterpolatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSine() {
    HermiteInterpolator interpolator = new HermiteInterpolator();
    for (double x = 0; x < FastMath.PI; x += 0.5) {
        interpolator.addSamplePoint(x, new double[] { FastMath.sin(x) });
    }
    for (double x = 0.1; x <= 2.9; x += 0.01) {
        DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 2, 0, x))[0];
        Assert.assertEquals( FastMath.sin(x), y.getValue(), 3.5e-5);
        Assert.assertEquals( FastMath.cos(x), y.getPartialDerivative(1), 1.3e-4);
        Assert.assertEquals(-FastMath.sin(x), y.getPartialDerivative(2), 2.9e-3);
    }
}
 
Example #30
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DerivativeStructure[] value(DerivativeStructure[] variables) {
    DerivativeStructure x1 = variables[0];
    DerivativeStructure x2 = variables[1];
    DerivativeStructure x3 = variables[2];
    DerivativeStructure x4 = variables[3];
    DerivativeStructure[] f = new DerivativeStructure[m];
    for (int i = 0; i < m; ++i) {
        f[i] = x1.multiply(x2.add(v[i]).multiply(v[i])).divide(x4.add(x3.add(v[i]).multiply(v[i]))).negate().add(y[i]);
    }
    return f;
}