org.apache.commons.math3.analysis.UnivariateFunction Java Examples

The following examples show how to use org.apache.commons.math3.analysis.UnivariateFunction. 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: RealTransformerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@link RealTransformer#transform(UnivariateFunction, double, double, int, TransformType)}
 * should throw a {@link MathIllegalArgumentException} if number of samples
 * is invalid.
 */
@Test
public void testTransformFunctionInvalidDataSize() {
    final TransformType[] type = TransformType.values();
    final RealTransformer transformer = createRealTransformer();
    final UnivariateFunction f = getValidFunction();
    final double a = getValidLowerBound();
    final double b = getValidUpperBound();
    for (int i = 0; i < getNumberOfInvalidDataSizes(); i++) {
        final int n = getInvalidDataSize(i);
        for (int j = 0; j < type.length; j++) {
            try {
                transformer.transform(f, a, b, n, type[j]);
                Assert.fail(type[j] + ", " + n);
            } catch (MathIllegalArgumentException e) {
                // Expected: do nothing
            }
        }
    }
}
 
Example #2
Source File: RealVectorAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void doTestMapFunction(final UnivariateFunction f,
    final boolean inPlace) {
    final double[] data = new double[values.length + 6];
    System.arraycopy(values, 0, data, 0, values.length);
    data[values.length + 0] = 0.5 * FastMath.PI;
    data[values.length + 1] = -0.5 * FastMath.PI;
    data[values.length + 2] = FastMath.E;
    data[values.length + 3] = -FastMath.E;
    data[values.length + 4] = 1.0;
    data[values.length + 5] = -1.0;
    final double[] expected = new double[data.length];
    for (int i = 0; i < data.length; i++) {
        expected[i] = f.value(data[i]);
    }
    final RealVector v = create(data);
    final RealVector actual;
    if (inPlace) {
        actual = v.mapToSelf(f);
        Assert.assertSame(v, actual);
    } else {
        actual = v.map(f);
    }
    TestUtils.assertEquals(f.getClass().getSimpleName(), expected, actual, 1E-16);
}
 
Example #3
Source File: NevilleInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new NevilleInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #4
Source File: BracketFinderTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testIntervalBoundsOrdering() {
    final UnivariateFunction func = new UnivariateFunction() {
            public double value(double x) {
                return x * x;
            }
        };

    final BracketFinder bFind = new BracketFinder();

    bFind.search(func, GoalType.MINIMIZE, -1, 1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 1, -1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 1, 2);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 2, 1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());
}
 
Example #5
Source File: FastFourierTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testTransformFunctionSizeNotAPowerOfTwo() {
    final int n = 127;
    final UnivariateFunction f = new Sin();
    final DftNormalization[] norm;
    norm = DftNormalization.values();
    final TransformType[] type;
    type = TransformType.values();
    for (int i = 0; i < norm.length; i++) {
        for (int j = 0; j < type.length; j++) {
            final FastFourierTransformer fft;
            fft = new FastFourierTransformer(norm[i]);
            try {
                fft.transform(f, 0.0, Math.PI, n, type[j]);
                Assert.fail(norm[i] + ", " + type[j] +
                    ": MathIllegalArgumentException was expected");
            } catch (MathIllegalArgumentException e) {
                // Expected behaviour
            }
        }
    }
}
 
Example #6
Source File: FastFourierTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testTransformFunctionInvalidBounds() {
    final int n = 128;
    final UnivariateFunction f = new Sin();
    final DftNormalization[] norm;
    norm = DftNormalization.values();
    final TransformType[] type;
    type = TransformType.values();
    for (int i = 0; i < norm.length; i++) {
        for (int j = 0; j < type.length; j++) {
            final FastFourierTransformer fft;
            fft = new FastFourierTransformer(norm[i]);
            try {
                fft.transform(f, Math.PI, 0.0, n, type[j]);
                Assert.fail(norm[i] + ", " + type[j] +
                    ": NumberIsTooLargeException was expected");
            } catch (NumberIsTooLargeException e) {
                // Expected behaviour
            }
        }
    }
}
 
Example #7
Source File: BaseSecantSolverAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSolutionAboveSide() {
    UnivariateFunction f = new SinFunction();
    UnivariateSolver solver = getSolver();
    double left = -1.5;
    double right = 0.05;
    for(int i = 0; i < 10; i++) {
        // Test whether the allowed solutions are taken into account.
        double solution = getSolution(solver, 100, f, left, right, AllowedSolution.ABOVE_SIDE);
        if (!Double.isNaN(solution)) {
            Assert.assertTrue(f.value(solution) >= 0.0);
        }

        // Prepare for next test.
        left -= 0.1;
        right += 0.3;
    }
}
 
Example #8
Source File: SincTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testShortcut() {
    final Sinc s = new Sinc();
    final UnivariateFunction f = new UnivariateFunction() {
        public double value(double x) {
            Dfp dfpX = new DfpField(25).newDfp(x);
            return DfpMath.sin(dfpX).divide(dfpX).toDouble();
        }
    };

    for (double x = 1e-30; x < 1e10; x *= 2) {
        final double fX = f.value(x);
        final double sX = s.value(x);
        Assert.assertEquals("x=" + x, fX, sX, 2.0e-16);
    }
}
 
Example #9
Source File: RiddersSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of solver for the quintic function.
 */
@Test
public void testQuinticFunction() {
    UnivariateFunction f = new QuinticFunction();
    UnivariateSolver solver = new RiddersSolver();
    double min, max, expected, result, tolerance;

    min = -0.4; max = 0.2; expected = 0.0;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);

    min = 0.75; max = 1.5; expected = 1.0;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);

    min = -0.9; max = -0.2; expected = -0.5;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);
}
 
Example #10
Source File: RealTransformerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void doTestTransformFunction(final int n, final double tol,
    final TransformType type) {
    final RealTransformer transformer = createRealTransformer();
    final UnivariateFunction f = getValidFunction();
    final double a = getValidLowerBound();
    final double b = getValidUpperBound();
    final double[] x = createRealData(n);
    for (int i = 0; i < n; i++) {
        final double t = a + i * (b - a) / n;
        x[i] = f.value(t);
    }
    final double[] expected = transform(x, type);
    final double[] actual = transformer.transform(f, a, b, n, type);
    for (int i = 0; i < n; i++) {
        final String msg = String.format("%d, %d", n, i);
        final double delta = tol * FastMath.abs(expected[i]);
        Assert.assertEquals(msg, expected[i], actual[i], delta);
    }
}
 
Example #11
Source File: DividedDifferenceInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #12
Source File: RiddersSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of solver for the quintic function.
 */
@Test
public void testQuinticFunction() {
    UnivariateFunction f = new QuinticFunction();
    UnivariateSolver solver = new RiddersSolver();
    double min, max, expected, result, tolerance;

    min = -0.4; max = 0.2; expected = 0.0;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);

    min = 0.75; max = 1.5; expected = 1.0;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);

    min = -0.9; max = -0.2; expected = -0.5;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);
}
 
Example #13
Source File: BrentOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMinEndpoints() {
    UnivariateFunction f = new Sin();
    UnivariateOptimizer optimizer = new BrentOptimizer(1e-8, 1e-14);

    // endpoint is minimum
    double result = optimizer.optimize(new MaxEval(50),
                                       new UnivariateObjectiveFunction(f),
                                       GoalType.MINIMIZE,
                                       new SearchInterval(3 * Math.PI / 2, 5)).getPoint();
    Assert.assertEquals(3 * Math.PI / 2, result, 1e-6);

    result = optimizer.optimize(new MaxEval(50),
                                new UnivariateObjectiveFunction(f),
                                GoalType.MINIMIZE,
                                new SearchInterval(4, 3 * Math.PI / 2)).getPoint();
    Assert.assertEquals(3 * Math.PI / 2, result, 1e-6);
}
 
Example #14
Source File: StepFunctionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSomeValues() {
    final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 };
    final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 };

    final UnivariateFunction f = new StepFunction(x, y);

    Assert.assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS);
    Assert.assertEquals(4, f.value(-10), EPS);
    Assert.assertEquals(-1, f.value(-0.4), EPS);
    Assert.assertEquals(-5.5, f.value(0), EPS);
    Assert.assertEquals(0.4, f.value(2), EPS);
    Assert.assertEquals(5.8, f.value(10), EPS);
    Assert.assertEquals(51.2, f.value(30), EPS);
    Assert.assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS);
}
 
Example #15
Source File: BracketFinderTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testIntervalBoundsOrdering() {
    final UnivariateFunction func = new UnivariateFunction() {
            public double value(double x) {
                return x * x;
            }
        };

    final BracketFinder bFind = new BracketFinder();

    bFind.search(func, GoalType.MINIMIZE, -1, 1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 1, -1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 1, 2);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 2, 1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());
}
 
Example #16
Source File: BaseSecantSolverAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSolutionAboveSide() {
    UnivariateFunction f = new SinFunction();
    UnivariateSolver solver = getSolver();
    double left = -1.5;
    double right = 0.05;
    for(int i = 0; i < 10; i++) {
        // Test whether the allowed solutions are taken into account.
        double solution = getSolution(solver, 100, f, left, right, AllowedSolution.ABOVE_SIDE);
        if (!Double.isNaN(solution)) {
            Assert.assertTrue(f.value(solution) >= 0.0);
        }

        // Prepare for next test.
        left -= 0.1;
        right += 0.3;
    }
}
 
Example #17
Source File: BracketFinderTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCubicMax() {
    final BracketFinder bFind = new BracketFinder();
    final UnivariateFunction func = new UnivariateFunction() {
            public double value(double x) {
                if (x < -2) {
                    return value(-2);
                }
                else  {
                    return -(x - 1) * (x + 2) * (x + 3);
                }
            }
        };

    bFind.search(func, GoalType.MAXIMIZE, -2 , -1);
    final double tol = 1e-15;
    Assert.assertEquals(-2, bFind.getLo(), tol);
    Assert.assertEquals(-1, bFind.getMid(), tol);
    Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol);
}
 
Example #18
Source File: IterativeLegendreGaussIntegrator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute the n-th stage integral.
 *
 * @param n Number of steps.
 * @return the value of n-th stage integral.
 * @throws TooManyEvaluationsException if the maximum number of evaluations
 * is exceeded.
 */
private double stage(final int n)
    throws TooManyEvaluationsException {
    // Function to be integrated is stored in the base class.
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double x)
                throws MathIllegalArgumentException, TooManyEvaluationsException {
                return computeObjectiveValue(x);
            }
        };

    final double min = getMin();
    final double max = getMax();
    final double step = (max - min) / n;

    double sum = 0;
    for (int i = 0; i < n; i++) {
        // Integrate over each sub-interval [a, b].
        final double a = min + i * step;
        final double b = a + step;
        final GaussIntegrator g = FACTORY.legendreHighPrecision(numberOfPoints, a, b);
        sum += g.integrate(f);
    }

    return sum;
}
 
Example #19
Source File: UnivariateMultiStartOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() {
    UnivariateFunction f = new Sin();
    UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    UnivariateMultiStartOptimizer<UnivariateFunction> optimizer =
        new UnivariateMultiStartOptimizer<UnivariateFunction>(underlying, 10, g);
    optimizer.optimize(300, f, GoalType.MINIMIZE, -100.0, 100.0);
    UnivariatePointValuePair[] optima = optimizer.getOptima();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI);
        Assert.assertTrue(FastMath.abs(d - FastMath.rint(d)) < 1.0e-8);
        Assert.assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10);
        Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 200);
    Assert.assertTrue(optimizer.getEvaluations() < 300);
}
 
Example #20
Source File: BaseSecantSolverAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinZero() {
    // The sinus function is behaved well around the root at pi. The second
    // order derivative is zero, which means linear approximating methods
    // still converge quadratically.
    UnivariateFunction f = new Sin();
    double result;
    UnivariateSolver solver = getSolver();

    result = solver.solve(100, f, 3, 4);
    //System.out.println(
    //    "Root: " + result + " Evaluations: " + solver.getEvaluations());
    Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy());
    Assert.assertTrue(solver.getEvaluations() <= 6);
    result = solver.solve(100, f, 1, 4);
    //System.out.println(
    //    "Root: " + result + " Evaluations: " + solver.getEvaluations());
    Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy());
    Assert.assertTrue(solver.getEvaluations() <= 7);
}
 
Example #21
Source File: RiddersSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of solver for the sine function.
 */
@Test
public void testSinFunction() {
    UnivariateFunction f = new SinFunction();
    UnivariateSolver solver = new RiddersSolver();
    double min, max, expected, result, tolerance;

    min = 3.0; max = 4.0; expected = FastMath.PI;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);

    min = -1.0; max = 1.5; expected = 0.0;
    tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                FastMath.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(100, f, min, max);
    Assert.assertEquals(expected, result, tolerance);
}
 
Example #22
Source File: BracketFinderTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testIntervalBoundsOrdering() {
    final UnivariateFunction func = new UnivariateFunction() {
            public double value(double x) {
                return x * x;
            }
        };

    final BracketFinder bFind = new BracketFinder();

    bFind.search(func, GoalType.MINIMIZE, -1, 1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 1, -1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 1, 2);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, 2, 1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());
}
 
Example #23
Source File: RealVectorAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void doTestMapFunction(final UnivariateFunction f,
    final boolean inPlace) {
    final double[] data = new double[values.length + 6];
    System.arraycopy(values, 0, data, 0, values.length);
    data[values.length + 0] = 0.5 * FastMath.PI;
    data[values.length + 1] = -0.5 * FastMath.PI;
    data[values.length + 2] = FastMath.E;
    data[values.length + 3] = -FastMath.E;
    data[values.length + 4] = 1.0;
    data[values.length + 5] = -1.0;
    final double[] expected = new double[data.length];
    for (int i = 0; i < data.length; i++) {
        expected[i] = f.value(data[i]);
    }
    final RealVector v = create(data);
    final RealVector actual;
    if (inPlace) {
        actual = v.mapToSelf(f);
        Assert.assertSame(v, actual);
    } else {
        actual = v.map(f);
    }
    TestUtils.assertEquals(f.getClass().getSimpleName(), expected, actual, 1E-16);
}
 
Example #24
Source File: MultiStartUnivariateOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testQuinticMin() {
    // The quintic function has zeros at 0, +-0.5 and +-1.
    // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
    UnivariateFunction f = new QuinticFunction();
    UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(4312000053L);
    MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g);

    UnivariatePointValuePair optimum
        = optimizer.optimize(new MaxEval(300),
                             new UnivariateObjectiveFunction(f),
                             GoalType.MINIMIZE,
                             new SearchInterval(-0.3, -0.2));
    Assert.assertEquals(-0.27195613, optimum.getPoint(), 1e-9);
    Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9);

    UnivariatePointValuePair[] optima = optimizer.getOptima();
    for (int i = 0; i < optima.length; ++i) {
        Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1e-9);
    }
    Assert.assertTrue(optimizer.getEvaluations() >= 50);
    Assert.assertTrue(optimizer.getEvaluations() <= 100);
}
 
Example #25
Source File: SplineInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInterpolateLinear() throws Exception {
    double x[] = { 0.0, 0.5, 1.0 };
    double y[] = { 0.0, 0.5, 0.0 };
    UnivariateInterpolator i = new SplineInterpolator();
    UnivariateFunction f = i.interpolate(x, y);
    verifyInterpolation(f, x, y);
    verifyConsistency((PolynomialSplineFunction) f, x);

    // Verify coefficients using analytical values
    PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
    double target[] = {y[0], 1.5d, 0d, -2d};
    TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
    target = new double[]{y[1], 0d, -3d, 2d};
    TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
}
 
Example #26
Source File: SplineInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInterpolateLinearDegenerateTwoSegment()
    {
    double x[] = { 0.0, 0.5, 1.0 };
    double y[] = { 0.0, 0.5, 1.0 };
    UnivariateInterpolator i = new SplineInterpolator();
    UnivariateFunction f = i.interpolate(x, y);
    verifyInterpolation(f, x, y);
    verifyConsistency((PolynomialSplineFunction) f, x);

    // Verify coefficients using analytical values
    PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
    double target[] = {y[0], 1d};
    TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
    target = new double[]{y[1], 1d};
    TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);

    // Check interpolation
    Assert.assertEquals(0.0,f.value(0.0), interpolationTolerance);
    Assert.assertEquals(0.4,f.value(0.4), interpolationTolerance);
    Assert.assertEquals(1.0,f.value(1.0), interpolationTolerance);
}
 
Example #27
Source File: BaseAbstractUnivariateIntegrator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prepare for computation.
 * Subclasses must call this method if they override any of the
 * {@code solve} methods.
 *
 * @param maxEval Maximum number of evaluations.
 * @param f the integrand function
 * @param lower the min bound for the interval
 * @param upper the upper bound for the interval
 * @throws NullArgumentException if {@code f} is {@code null}.
 * @throws MathIllegalArgumentException if {@code min >= max}.
 */
protected void setup(final int maxEval,
                     final UnivariateFunction f,
                     final double lower, final double upper)
    throws NullArgumentException, MathIllegalArgumentException {

    // Checks.
    MathUtils.checkNotNull(f);
    UnivariateSolverUtils.verifyInterval(lower, upper);

    // Reset.
    min = lower;
    max = upper;
    function = f;
    evaluations.setMaximalCount(maxEval);
    evaluations.resetCount();
    iterations.resetCount();

}
 
Example #28
Source File: AkimaSplineInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInterpolateCubic()
{
    final int numberOfElements = 10;
    final double minimumX = -3;
    final double maximumX = 3;
    final int numberOfSamples = 100;
    final double interpolationTolerance = 0.37;
    final double maxTolerance = 3.8;

    UnivariateFunction f = new UnivariateFunction()
    {
        public double value( double x )
        {
            return ( 3 * x * x * x ) - ( 0.5 * x * x ) + ( 1 * x ) - 1;
        }
    };

    testInterpolation( minimumX, maximumX, numberOfElements, numberOfSamples, f, interpolationTolerance,
                       maxTolerance );
}
 
Example #29
Source File: FastFourierTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStandardTransformFunction() {
    final UnivariateFunction f = new Sinc();
    final double min = -FastMath.PI;
    final double max = FastMath.PI;
    final DftNormalization[] norm;
    norm = DftNormalization.values();
    final TransformType[] type;
    type = TransformType.values();
    for (int i = 0; i < norm.length; i++) {
        for (int j = 0; j < type.length; j++) {
            doTestTransformFunction(f, min, max, 2, 1.0E-15, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 4, 1.0E-14, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 8, 1.0E-14, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 16, 1.0E-13, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 32, 1.0E-13, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 64, 1.0E-12, norm[i], type[j]);
            doTestTransformFunction(f, min, max, 128, 1.0E-11, norm[i], type[j]);
        }
    }
}
 
Example #30
Source File: SplineInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInterpolateLinearDegenerateTwoSegment()
    {
    double x[] = { 0.0, 0.5, 1.0 };
    double y[] = { 0.0, 0.5, 1.0 };
    UnivariateInterpolator i = new SplineInterpolator();
    UnivariateFunction f = i.interpolate(x, y);
    verifyInterpolation(f, x, y);
    verifyConsistency((PolynomialSplineFunction) f, x);

    // Verify coefficients using analytical values
    PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
    double target[] = {y[0], 1d};
    TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
    target = new double[]{y[1], 1d};
    TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);

    // Check interpolation
    Assert.assertEquals(0.0,f.value(0.0), interpolationTolerance);
    Assert.assertEquals(0.4,f.value(0.4), interpolationTolerance);
    Assert.assertEquals(1.0,f.value(1.0), interpolationTolerance);
}