org.apache.commons.math.analysis.polynomials.PolynomialFunction Java Examples
The following examples show how to use
org.apache.commons.math.analysis.polynomials.PolynomialFunction.
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: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of solver for the quadratic function. */ public void testQuadraticFunction() throws MathException { double min, max, expected, result, tolerance; // p(x) = 2x^2 + 5x - 3 = (x+3)(2x-1) double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); UnivariateRealSolver solver = new LaguerreSolver(); min = 0.0; max = 2.0; expected = 0.5; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(f, min, max); assertEquals(expected, result, tolerance); min = -4.0; max = -1.0; expected = -3.0; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(f, min, max); assertEquals(expected, result, tolerance); }
Example #2
Source File: SplineInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testInterpolateLinearDegenerateTwoSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 1.0 }; UnivariateRealInterpolator i = new SplineInterpolator(); UnivariateRealFunction 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 assertEquals(0.0,f.value(0.0), interpolationTolerance); assertEquals(0.4,f.value(0.4), interpolationTolerance); assertEquals(1.0,f.value(1.0), interpolationTolerance); }
Example #3
Source File: SplineInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testInterpolateLinearDegenerateThreeSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateRealInterpolator i = new SplineInterpolator(); UnivariateRealFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // 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); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation assertEquals(0,f.value(0), interpolationTolerance); assertEquals(1.4,f.value(1.4), interpolationTolerance); assertEquals(1.5,f.value(1.5), interpolationTolerance); }
Example #4
Source File: SplineInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testInterpolateLinearDegenerateThreeSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateRealInterpolator i = new SplineInterpolator(); UnivariateRealFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // 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); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation assertEquals(0,f.value(0), interpolationTolerance); assertEquals(1.4,f.value(1.4), interpolationTolerance); assertEquals(1.5,f.value(1.5), interpolationTolerance); }
Example #5
Source File: LinearInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testInterpolateLinearDegenerateThreeSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateRealInterpolator i = new LinearInterpolator(); UnivariateRealFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // 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); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation Assert.assertEquals(0,f.value(0), interpolationTolerance); Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance); Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance); }
Example #6
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of solver for the quadratic function. */ @Test public void testQuadraticFunction() { double min, max, expected, result, tolerance; // p(x) = 2x^2 + 5x - 3 = (x+3)(2x-1) double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); min = 0.0; max = 2.0; 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); min = -4.0; max = -1.0; expected = -3.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); }
Example #7
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of solver for the linear function. */ @Test public void testLinearFunction() { double min, max, expected, result, tolerance; // p(x) = 4x - 1 double coefficients[] = { -1.0, 4.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); min = 0.0; max = 1.0; expected = 0.25; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); }
Example #8
Source File: SplineInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testInterpolateLinearDegenerateThreeSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateRealInterpolator i = new SplineInterpolator(); UnivariateRealFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // 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); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation assertEquals(0,f.value(0), interpolationTolerance); assertEquals(1.4,f.value(1.4), interpolationTolerance); assertEquals(1.5,f.value(1.5), interpolationTolerance); }
Example #9
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testSmallError() throws OptimizationException { Random randomizer = new Random(53882150042l); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); maxError = Math.max(maxError, error); assertTrue(Math.abs(error) < 0.1); } } assertTrue(maxError > 0.01); }
Example #10
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } PolynomialFunction fitted = new PolynomialFunction(fitter.fit()); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); Assert.assertEquals(0.0, error, 1.0e-6); } } }
Example #11
Source File: LegendreGaussIntegratorTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testExactIntegration() throws ConvergenceException, FunctionEvaluationException { Random random = new Random(86343623467878363l); for (int n = 2; n < 6; ++n) { LegendreGaussIntegrator integrator = new LegendreGaussIntegrator(n, 64); // an n points Gauss-Legendre integrator integrates 2n-1 degree polynoms exactly for (int degree = 0; degree <= 2 * n - 1; ++degree) { for (int i = 0; i < 10; ++i) { double[] coeff = new double[degree + 1]; for (int k = 0; k < coeff.length; ++k) { coeff[k] = 2 * random.nextDouble() - 1; } PolynomialFunction p = new PolynomialFunction(coeff); double result = integrator.integrate(p, -5.0, 15.0); double reference = exactIntegration(p, -5.0, 15.0); assertEquals(n + " " + degree + " " + i, reference, result, 1.0e-12 * (1.0 + Math.abs(reference))); } } } }
Example #12
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test deprecated APIs. */ @Deprecated public void testDeprecated() throws MathException { double min, max, expected, result, tolerance; // p(x) = 4x - 1 double coefficients[] = { -1.0, 4.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); UnivariateRealSolver solver = new LaguerreSolver(f); min = 0.0; max = 1.0; expected = 0.25; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(min, max); assertEquals(expected, result, tolerance); }
Example #13
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test deprecated APIs. */ @Deprecated public void testDeprecated() throws MathException { double min, max, expected, result, tolerance; // p(x) = 4x - 1 double coefficients[] = { -1.0, 4.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); UnivariateRealSolver solver = new LaguerreSolver(f); min = 0.0; max = 1.0; expected = 0.25; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(min, max); assertEquals(expected, result, tolerance); }
Example #14
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() throws OptimizationException { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); assertEquals(0.0, error, 1.0e-6); } } }
Example #15
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() throws OptimizationException { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); assertEquals(0.0, error, 1.0e-6); } } }
Example #16
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of solver for the quadratic function. */ @Test public void testQuadraticFunction() { double min, max, expected, result, tolerance; // p(x) = 2x^2 + 5x - 3 = (x+3)(2x-1) double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); min = 0.0; max = 2.0; 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); min = -4.0; max = -1.0; expected = -3.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); }
Example #17
Source File: SplineInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testInterpolateLinearDegenerateTwoSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 1.0 }; UnivariateRealInterpolator i = new SplineInterpolator(); UnivariateRealFunction 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 #18
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() throws OptimizationException { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); assertEquals(0.0, error, 1.0e-6); } } }
Example #19
Source File: LegendreGaussIntegratorTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testExactIntegration() throws ConvergenceException, MathUserException { Random random = new Random(86343623467878363l); for (int n = 2; n < 6; ++n) { LegendreGaussIntegrator integrator = new LegendreGaussIntegrator(n, 64); // an n points Gauss-Legendre integrator integrates 2n-1 degree polynoms exactly for (int degree = 0; degree <= 2 * n - 1; ++degree) { for (int i = 0; i < 10; ++i) { double[] coeff = new double[degree + 1]; for (int k = 0; k < coeff.length; ++k) { coeff[k] = 2 * random.nextDouble() - 1; } PolynomialFunction p = new PolynomialFunction(coeff); double result = integrator.integrate(p, -5.0, 15.0); double reference = exactIntegration(p, -5.0, 15.0); assertEquals(n + " " + degree + " " + i, reference, result, 1.0e-12 * (1.0 + FastMath.abs(reference))); } } } }
Example #20
Source File: SplineInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testInterpolateLinearDegenerateThreeSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateRealInterpolator i = new SplineInterpolator(); UnivariateRealFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // 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); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation Assert.assertEquals(0,f.value(0), interpolationTolerance); Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance); Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance); }
Example #21
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test of solver for the quadratic function. */ @Test public void testQuadraticFunction() { double min, max, expected, result, tolerance; // p(x) = 2x^2 + 5x - 3 = (x+3)(2x-1) double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); min = 0.0; max = 2.0; 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); min = -4.0; max = -1.0; expected = -3.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: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
private void checkUnsolvableProblem(DifferentiableMultivariateVectorialOptimizer optimizer, boolean solvable) { Random randomizer = new Random(1248788532l); for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, optimizer); // reusing the same point over and over again does not bring // information, the problem cannot be solved in this case for // degrees greater than 1 (but one point is sufficient for // degree 0) for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, 0.0, p.value(0.0)); } try { fitter.fit(); assertTrue(solvable || (degree == 0)); } catch(OptimizationException e) { assertTrue((! solvable) && (degree > 0)); } } }
Example #23
Source File: LinearInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testInterpolateLinearDegenerateTwoSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 1.0 }; UnivariateRealInterpolator i = new LinearInterpolator(); UnivariateRealFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // 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 #24
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testSmallError() throws OptimizationException { Random randomizer = new Random(53882150042l); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); maxError = Math.max(maxError, error); assertTrue(Math.abs(error) < 0.1); } } assertTrue(maxError > 0.01); }
Example #25
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Test deprecated APIs. */ @Deprecated public void testDeprecated() throws MathException { double min, max, expected, result, tolerance; // p(x) = 4x - 1 double coefficients[] = { -1.0, 4.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); UnivariateRealSolver solver = new LaguerreSolver(f); min = 0.0; max = 1.0; expected = 0.25; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(min, max); assertEquals(expected, result, tolerance); }
Example #26
Source File: LegendreGaussIntegratorTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testExactIntegration() throws ConvergenceException, FunctionEvaluationException { Random random = new Random(86343623467878363l); for (int n = 2; n < 6; ++n) { LegendreGaussIntegrator integrator = new LegendreGaussIntegrator(n, 64); // an n points Gauss-Legendre integrator integrates 2n-1 degree polynoms exactly for (int degree = 0; degree <= 2 * n - 1; ++degree) { for (int i = 0; i < 10; ++i) { double[] coeff = new double[degree + 1]; for (int k = 0; k < coeff.length; ++k) { coeff[k] = 2 * random.nextDouble() - 1; } PolynomialFunction p = new PolynomialFunction(coeff); double result = integrator.integrate(p, -5.0, 15.0); double reference = exactIntegration(p, -5.0, 15.0); assertEquals(n + " " + degree + " " + i, reference, result, 1.0e-12 * (1.0 + Math.abs(reference))); } } } }
Example #27
Source File: LinearInterpolatorTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testInterpolateLinearDegenerateThreeSegment() throws Exception { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateRealInterpolator i = new LinearInterpolator(); UnivariateRealFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // 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); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation Assert.assertEquals(0,f.value(0), interpolationTolerance); Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance); Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance); }
Example #28
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() throws OptimizationException { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); assertEquals(0.0, error, 1.0e-6); } } }
Example #29
Source File: LaguerreSolverTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Test of solver for the quintic function. */ public void testQuinticFunction() throws MathException { double min, max, expected, result, tolerance; // p(x) = x^5 - x^4 - 12x^3 + x^2 - x - 12 = (x+1)(x+3)(x-4)(x^2-x+1) double coefficients[] = { -12.0, -1.0, 1.0, -12.0, -1.0, 1.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); UnivariateRealSolver solver = new LaguerreSolver(); min = -2.0; max = 2.0; expected = -1.0; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(f, min, max); assertEquals(expected, result, tolerance); min = -5.0; max = -2.5; expected = -3.0; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(f, min, max); assertEquals(expected, result, tolerance); min = 3.0; max = 6.0; expected = 4.0; tolerance = Math.max(solver.getAbsoluteAccuracy(), Math.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(f, min, max); assertEquals(expected, result, tolerance); }
Example #30
Source File: SplineInterpolatorTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void testInterpolateLinear() throws Exception { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 0.0 }; UnivariateRealInterpolator i = new SplineInterpolator(); UnivariateRealFunction 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); }