001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math3.analysis.integration;
018    
019    import java.util.Random;
020    
021    import org.apache.commons.math3.analysis.QuinticFunction;
022    import org.apache.commons.math3.analysis.UnivariateFunction;
023    import org.apache.commons.math3.analysis.function.Sin;
024    import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
025    import org.apache.commons.math3.exception.TooManyEvaluationsException;
026    import org.apache.commons.math3.util.FastMath;
027    import org.junit.Assert;
028    import org.junit.Test;
029    
030    
031    @Deprecated
032    public class LegendreGaussIntegratorTest {
033    
034        @Test
035        public void testSinFunction() {
036            UnivariateFunction f = new Sin();
037            BaseAbstractUnivariateIntegrator integrator = new LegendreGaussIntegrator(5, 1.0e-14, 1.0e-10, 2, 15);
038            double min, max, expected, result, tolerance;
039    
040            min = 0; max = FastMath.PI; expected = 2;
041            tolerance = FastMath.max(integrator.getAbsoluteAccuracy(),
042                                 FastMath.abs(expected * integrator.getRelativeAccuracy()));
043            result = integrator.integrate(10000, f, min, max);
044            Assert.assertEquals(expected, result, tolerance);
045    
046            min = -FastMath.PI/3; max = 0; expected = -0.5;
047            tolerance = FastMath.max(integrator.getAbsoluteAccuracy(),
048                    FastMath.abs(expected * integrator.getRelativeAccuracy()));
049            result = integrator.integrate(10000, f, min, max);
050            Assert.assertEquals(expected, result, tolerance);
051        }
052    
053        @Test
054        public void testQuinticFunction() {
055            UnivariateFunction f = new QuinticFunction();
056            UnivariateIntegrator integrator =
057                    new LegendreGaussIntegrator(3,
058                                                BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY,
059                                                BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY,
060                                                BaseAbstractUnivariateIntegrator.DEFAULT_MIN_ITERATIONS_COUNT,
061                                                64);
062            double min, max, expected, result;
063    
064            min = 0; max = 1; expected = -1.0/48;
065            result = integrator.integrate(10000, f, min, max);
066            Assert.assertEquals(expected, result, 1.0e-16);
067    
068            min = 0; max = 0.5; expected = 11.0/768;
069            result = integrator.integrate(10000, f, min, max);
070            Assert.assertEquals(expected, result, 1.0e-16);
071    
072            min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
073            result = integrator.integrate(10000, f, min, max);
074            Assert.assertEquals(expected, result, 1.0e-16);
075        }
076    
077        @Test
078        public void testExactIntegration() {
079            Random random = new Random(86343623467878363l);
080            for (int n = 2; n < 6; ++n) {
081                LegendreGaussIntegrator integrator =
082                    new LegendreGaussIntegrator(n,
083                                                BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY,
084                                                BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY,
085                                                BaseAbstractUnivariateIntegrator.DEFAULT_MIN_ITERATIONS_COUNT,
086                                                64);
087    
088                // an n points Gauss-Legendre integrator integrates 2n-1 degree polynoms exactly
089                for (int degree = 0; degree <= 2 * n - 1; ++degree) {
090                    for (int i = 0; i < 10; ++i) {
091                        double[] coeff = new double[degree + 1];
092                        for (int k = 0; k < coeff.length; ++k) {
093                            coeff[k] = 2 * random.nextDouble() - 1;
094                        }
095                        PolynomialFunction p = new PolynomialFunction(coeff);
096                        double result    = integrator.integrate(10000, p, -5.0, 15.0);
097                        double reference = exactIntegration(p, -5.0, 15.0);
098                        Assert.assertEquals(n + " " + degree + " " + i, reference, result, 1.0e-12 * (1.0 + FastMath.abs(reference)));
099                    }
100                }
101    
102            }
103        }
104    
105        @Test
106        public void testIssue464() {
107            final double value = 0.2;
108            UnivariateFunction f = new UnivariateFunction() {
109                public double value(double x) {
110                    return (x >= 0 && x <= 5) ? value : 0.0;
111                }
112            };
113            LegendreGaussIntegrator gauss = new LegendreGaussIntegrator(5, 3, 100);
114    
115            // due to the discontinuity, integration implies *many* calls
116            double maxX = 0.32462367623786328;
117            Assert.assertEquals(maxX * value, gauss.integrate(Integer.MAX_VALUE, f, -10, maxX), 1.0e-7);
118            Assert.assertTrue(gauss.getEvaluations() > 37000000);
119            Assert.assertTrue(gauss.getIterations() < 30);
120    
121            // setting up limits prevents such large number of calls
122            try {
123                gauss.integrate(1000, f, -10, maxX);
124                Assert.fail("expected TooManyEvaluationsException");
125            } catch (TooManyEvaluationsException tmee) {
126                // expected
127                Assert.assertEquals(1000, tmee.getMax());
128            }
129    
130            // integrating on the two sides should be simpler
131            double sum1 = gauss.integrate(1000, f, -10, 0);
132            int eval1   = gauss.getEvaluations();
133            double sum2 = gauss.integrate(1000, f, 0, maxX);
134            int eval2   = gauss.getEvaluations();
135            Assert.assertEquals(maxX * value, sum1 + sum2, 1.0e-7);
136            Assert.assertTrue(eval1 + eval2 < 200);
137    
138        }
139    
140        private double exactIntegration(PolynomialFunction p, double a, double b) {
141            final double[] coeffs = p.getCoefficients();
142            double yb = coeffs[coeffs.length - 1] / coeffs.length;
143            double ya = yb;
144            for (int i = coeffs.length - 2; i >= 0; --i) {
145                yb = yb * b + coeffs[i] / (i + 1);
146                ya = ya * a + coeffs[i] / (i + 1);
147            }
148            return yb * b - ya * a;
149        }
150    
151    }