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 java.util.ArrayList;
20  import java.util.Collection;
21  
22  import org.junit.runner.RunWith;
23  import org.junit.runners.Parameterized;
24  import org.junit.runners.Parameterized.Parameters;
25  import org.apache.commons.math4.core.jdkmath.JdkMath;
26  
27  /**
28   * Test of the {@link HermiteRuleFactory}.
29   * This parameterized test extends the standard test for Gaussian quadrature
30   * rule, where each monomial is tested in turn.
31   * Parametrization allows to test automatically 0, 1, ... , {@link #MAX_NUM_POINTS}
32   * quadrature rules.
33   *
34   */
35  @RunWith(value=Parameterized.class)
36  public class HermiteParametricTest extends GaussianQuadratureAbstractTest {
37      private static final double SQRT_PI = JdkMath.sqrt(Math.PI);
38      private static final GaussIntegratorFactory factory = new GaussIntegratorFactory();
39  
40      /**
41       * The highest order quadrature rule to be tested.
42       */
43      public static final int MAX_NUM_POINTS = 30;
44  
45      /**
46       * Creates a new instance of this test, with the specified number of nodes
47       * for the Gauss-Hermite quadrature rule.
48       *
49       * @param numberOfPoints Order of integration rule.
50       * @param maxDegree Maximum degree of monomials to be tested.
51       * @param eps Value of ε.
52       * @param numUlps Value of the maximum relative error (in ulps).
53       */
54      public HermiteParametricTest(int numberOfPoints,
55                                   int maxDegree,
56                                   double eps,
57                                   double numUlps) {
58          super(factory.hermite(numberOfPoints),
59                maxDegree, eps, numUlps);
60      }
61  
62      /**
63       * Returns the collection of parameters to be passed to the constructor of
64       * this class.
65       * Gauss-Hermite quadrature rules of order 1, ..., {@link #MAX_NUM_POINTS}
66       * will be constructed.
67       *
68       * @return the collection of parameters for this parameterized test.
69       */
70      @SuppressWarnings("boxing") // OK here
71      @Parameters
72      public static Collection<Object[]> getParameters() {
73          final ArrayList<Object[]> parameters = new ArrayList<>();
74          for (int k = 1; k <= MAX_NUM_POINTS; k++) {
75              parameters.add(new Object[] { k, 2 * k - 1, Math.ulp(1d), 195 });
76          }
77          return parameters;
78      }
79  
80      @Override
81      public double getExpectedValue(final int n) {
82          if ((n & 1) == 1) {
83              // n is odd
84              return 0;
85          }
86  
87          final int iMax = n / 2;
88          double p = 1;
89          double q = 1;
90          for (int i = 0; i < iMax; i++) {
91              p *= 2 * i + 1;
92              q *= 2;
93          }
94  
95          return p / q * SQRT_PI;
96      }
97  }