View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.analysis.integration;
18  
19  import org.apache.commons.math4.legacy.analysis.QuinticFunction;
20  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
21  import org.apache.commons.math4.legacy.analysis.function.Sin;
22  import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
23  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
24  import org.apache.commons.math4.core.jdkmath.JdkMath;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  
29  /**
30   * Test case for trapezoid integrator.
31   * <p>
32   * Test runs show that for a default relative accuracy of 1E-6, it
33   * generally takes 10 to 15 iterations for the integral to converge.
34   *
35   */
36  public final class TrapezoidIntegratorTest {
37  
38      /**
39       * Test of integrator for the sine function.
40       */
41      @Test
42      public void testSinFunction() {
43          UnivariateFunction f = new Sin();
44          UnivariateIntegrator integrator = new TrapezoidIntegrator();
45          double min;
46          double max;
47          double expected;
48          double result;
49          double tolerance;
50  
51          min = 0; max = JdkMath.PI; expected = 2;
52          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
53          result = integrator.integrate(10000, f, min, max);
54          Assert.assertTrue(integrator.getEvaluations() < 2500);
55          Assert.assertTrue(integrator.getIterations()  < 15);
56          Assert.assertEquals(expected, result, tolerance);
57  
58          min = -JdkMath.PI/3; max = 0; expected = -0.5;
59          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
60          result = integrator.integrate(10000, f, min, max);
61          Assert.assertTrue(integrator.getEvaluations() < 2500);
62          Assert.assertTrue(integrator.getIterations()  < 15);
63          Assert.assertEquals(expected, result, tolerance);
64      }
65  
66      /**
67       * Test of integrator for the quintic function.
68       */
69      @Test
70      public void testQuinticFunction() {
71          UnivariateFunction f = new QuinticFunction();
72          UnivariateIntegrator integrator = new TrapezoidIntegrator();
73          double min;
74          double max;
75          double expected;
76          double result;
77          double tolerance;
78  
79          min = 0; max = 1; expected = -1.0/48;
80          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
81          result = integrator.integrate(10000, f, min, max);
82          Assert.assertTrue(integrator.getEvaluations() < 5000);
83          Assert.assertTrue(integrator.getIterations()  < 15);
84          Assert.assertEquals(expected, result, tolerance);
85  
86          min = 0; max = 0.5; expected = 11.0/768;
87          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
88          result = integrator.integrate(10000, f, min, max);
89          Assert.assertTrue(integrator.getEvaluations() < 2500);
90          Assert.assertTrue(integrator.getIterations()  < 15);
91          Assert.assertEquals(expected, result, tolerance);
92  
93          min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
94          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
95          result = integrator.integrate(10000, f, min, max);
96          Assert.assertTrue(integrator.getEvaluations() < 5000);
97          Assert.assertTrue(integrator.getIterations()  < 15);
98          Assert.assertEquals(expected, result, tolerance);
99      }
100 
101     /**
102      * Test of parameters for the integrator.
103      */
104     @Test
105     public void testParameters() {
106         UnivariateFunction f = new Sin();
107 
108         try {
109             // bad interval
110             new TrapezoidIntegrator().integrate(1000, f, 1, -1);
111             Assert.fail("Expecting NumberIsTooLargeException - bad interval");
112         } catch (NumberIsTooLargeException ex) {
113             // expected
114         }
115         try {
116             // bad iteration limits
117             new TrapezoidIntegrator(5, 4);
118             Assert.fail("Expecting NumberIsTooSmallException - bad iteration limits");
119         } catch (NumberIsTooSmallException ex) {
120             // expected
121         }
122         try {
123             // bad iteration limits
124             new TrapezoidIntegrator(10,99);
125             Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits");
126         } catch (NumberIsTooLargeException ex) {
127             // expected
128         }
129     }
130 }