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.math.analysis;
18  
19  import org.apache.commons.math.MathException;
20  import junit.framework.TestCase;
21  
22  /**
23   * Testcase for trapezoid integrator.
24   * <p>
25   * Test runs show that for a default relative accuracy of 1E-6, it
26   * generally takes 10 to 15 iterations for the integral to converge.
27   * 
28   * @version $Revision: 480442 $ $Date: 2006-11-29 00:21:22 -0700 (Wed, 29 Nov 2006) $ 
29   */
30  public final class TrapezoidIntegratorTest extends TestCase {
31  
32      /**
33       * Test of integrator for the sine function.
34       */
35      public void testSinFunction() throws MathException {
36          UnivariateRealFunction f = new SinFunction();
37          UnivariateRealIntegrator integrator = new TrapezoidIntegrator(f);
38          double min, max, expected, result, tolerance;
39  
40          min = 0; max = Math.PI; expected = 2;
41          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
42          result = integrator.integrate(min, max);
43          assertEquals(expected, result, tolerance);
44  
45          min = -Math.PI/3; max = 0; expected = -0.5;
46          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
47          result = integrator.integrate(min, max);
48          assertEquals(expected, result, tolerance);
49      }
50  
51      /**
52       * Test of integrator for the quintic function.
53       */
54      public void testQuinticFunction() throws MathException {
55          UnivariateRealFunction f = new QuinticFunction();
56          UnivariateRealIntegrator integrator = new TrapezoidIntegrator(f);
57          double min, max, expected, result, tolerance;
58  
59          min = 0; max = 1; expected = -1.0/48;
60          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
61          result = integrator.integrate(min, max);
62          assertEquals(expected, result, tolerance);
63  
64          min = 0; max = 0.5; expected = 11.0/768;
65          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
66          result = integrator.integrate(min, max);
67          assertEquals(expected, result, tolerance);
68  
69          min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
70          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
71          result = integrator.integrate(min, max);
72          assertEquals(expected, result, tolerance);
73      }
74  
75      /**
76       * Test of parameters for the integrator.
77       */
78      public void testParameters() throws Exception {
79          UnivariateRealFunction f = new SinFunction();
80          UnivariateRealIntegrator integrator = new TrapezoidIntegrator(f);
81  
82          try {
83              // bad interval
84              integrator.integrate(1, -1);
85              fail("Expecting IllegalArgumentException - bad interval");
86          } catch (IllegalArgumentException ex) {
87              // expected
88          }
89          try {
90              // bad iteration limits
91              integrator.setMinimalIterationCount(5);
92              integrator.setMaximalIterationCount(4);
93              integrator.integrate(-1, 1);
94              fail("Expecting IllegalArgumentException - bad iteration limits");
95          } catch (IllegalArgumentException ex) {
96              // expected
97          }
98          try {
99              // bad iteration limits
100             integrator.setMinimalIterationCount(10);
101             integrator.setMaximalIterationCount(99);
102             integrator.integrate(-1, 1);
103             fail("Expecting IllegalArgumentException - bad iteration limits");
104         } catch (IllegalArgumentException ex) {
105             // expected
106         }
107     }
108 }