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.gauss;
18  
19  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
20  import org.apache.commons.math4.legacy.analysis.function.Constant;
21  import org.apache.commons.math4.legacy.core.Pair;
22  import org.junit.Test;
23  import org.junit.Assert;
24  
25  /**
26   * Test for {@link GaussIntegrator} class.
27   *
28   */
29  public class GaussIntegratorTest {
30      @Test
31      public void testGetWeights() {
32          final double[] points = { 0, 1.2, 3.4 };
33          final double[] weights = { 9.8, 7.6, 5.4 };
34  
35          final GaussIntegrator integrator
36              = new GaussIntegrator(new Pair<>(points, weights));
37  
38          Assert.assertEquals(weights.length, integrator.getNumberOfPoints());
39  
40          for (int i = 0; i < integrator.getNumberOfPoints(); i++) {
41              Assert.assertEquals(weights[i], integrator.getWeight(i), 0d);
42          }
43      }
44  
45      @Test
46      public void testGetPoints() {
47          final double[] points = { 0, 1.2, 3.4 };
48          final double[] weights = { 9.8, 7.6, 5.4 };
49  
50          final GaussIntegrator integrator
51              = new GaussIntegrator(new Pair<>(points, weights));
52  
53          Assert.assertEquals(points.length, integrator.getNumberOfPoints());
54  
55          for (int i = 0; i < integrator.getNumberOfPoints(); i++) {
56              Assert.assertEquals(points[i], integrator.getPoint(i), 0d);
57          }
58      }
59  
60      @Test
61      public void testIntegrate() {
62          final double[] points = { 0, 1, 2, 3, 4, 5 };
63          final double[] weights = { 1, 1, 1, 1, 1, 1 };
64  
65          final GaussIntegrator integrator
66              = new GaussIntegrator(new Pair<>(points, weights));
67  
68          final double val = 123.456;
69          final UnivariateFunction c = new Constant(val);
70  
71          final double s = integrator.integrate(c);
72          Assert.assertEquals(points.length * val, s, 0d);
73      }
74  }