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.math.analysis.integration;
018    
019    import org.apache.commons.math.MathException;
020    import org.apache.commons.math.analysis.QuinticFunction;
021    import org.apache.commons.math.analysis.SinFunction;
022    import org.apache.commons.math.analysis.UnivariateRealFunction;
023    
024    import junit.framework.TestCase;
025    
026    /**
027     * Testcase for Simpson integrator.
028     * <p>
029     * Test runs show that for a default relative accuracy of 1E-6, it
030     * generally takes 5 to 10 iterations for the integral to converge.
031     * 
032     * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $ 
033     */
034    public final class SimpsonIntegratorTest extends TestCase {
035    
036        /**
037         * Test of integrator for the sine function.
038         */
039        public void testSinFunction() throws MathException {
040            UnivariateRealFunction f = new SinFunction();
041            UnivariateRealIntegrator integrator = new SimpsonIntegrator();
042            double min, max, expected, result, tolerance;
043    
044            min = 0; max = Math.PI; expected = 2;
045            tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
046            result = integrator.integrate(f, min, max);
047            assertEquals(expected, result, tolerance);
048    
049            min = -Math.PI/3; max = 0; expected = -0.5;
050            tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
051            result = integrator.integrate(f, min, max);
052            assertEquals(expected, result, tolerance);
053        }
054    
055        /**
056         * Test of integrator for the quintic function.
057         */
058        public void testQuinticFunction() throws MathException {
059            UnivariateRealFunction f = new QuinticFunction();
060            UnivariateRealIntegrator integrator = new SimpsonIntegrator();
061            double min, max, expected, result, tolerance;
062    
063            min = 0; max = 1; expected = -1.0/48;
064            tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
065            result = integrator.integrate(f, min, max);
066            assertEquals(expected, result, tolerance);
067    
068            min = 0; max = 0.5; expected = 11.0/768;
069            tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
070            result = integrator.integrate(f, min, max);
071            assertEquals(expected, result, tolerance);
072    
073            min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
074            tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
075            result = integrator.integrate(f, min, max);
076            assertEquals(expected, result, tolerance);
077        }
078    
079        /**
080         * Test of parameters for the integrator.
081         */
082        public void testParameters() throws Exception {
083            UnivariateRealFunction f = new SinFunction();
084            UnivariateRealIntegrator integrator = new SimpsonIntegrator();
085    
086            try {
087                // bad interval
088                integrator.integrate(f, 1, -1);
089                fail("Expecting IllegalArgumentException - bad interval");
090            } catch (IllegalArgumentException ex) {
091                // expected
092            }
093            try {
094                // bad iteration limits
095                integrator.setMinimalIterationCount(5);
096                integrator.setMaximalIterationCount(4);
097                integrator.integrate(f, -1, 1);
098                fail("Expecting IllegalArgumentException - bad iteration limits");
099            } catch (IllegalArgumentException ex) {
100                // expected
101            }
102            try {
103                // bad iteration limits
104                integrator.setMinimalIterationCount(10);
105                integrator.setMaximalIterationCount(99);
106                integrator.integrate(f, -1, 1);
107                fail("Expecting IllegalArgumentException - bad iteration limits");
108            } catch (IllegalArgumentException ex) {
109                // expected
110            }
111        }
112    }