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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis.integration.gauss;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  
27  import org.hipparchus.util.FastMath;
28  import org.junit.runner.RunWith;
29  import org.junit.runners.Parameterized;
30  import org.junit.runners.Parameterized.Parameters;
31  
32  /**
33   * Test of the {@link HermiteRuleFactory}.
34   * This parameterized test extends the standard test for Gaussian quadrature
35   * rule, where each monomial is tested in turn.
36   * Parametrization allows to test automatically 0, 1, ... , {@link #MAX_NUM_POINTS}
37   * quadrature rules.
38   *
39   */
40  @RunWith(value=Parameterized.class)
41  public class HermiteParametricTest extends GaussianQuadratureAbstractTest {
42      private static final double SQRT_PI = FastMath.sqrt(Math.PI);
43      private static final GaussIntegratorFactory factory = new GaussIntegratorFactory();
44  
45      /**
46       * The highest order quadrature rule to be tested.
47       */
48      public static final int MAX_NUM_POINTS = 30;
49  
50      /**
51       * Creates a new instance of this test, with the specified number of nodes
52       * for the Gauss-Hermite quadrature rule.
53       *
54       * @param numberOfPoints Order of integration rule.
55       * @param maxDegree Maximum degree of monomials to be tested.
56       * @param eps Value of ε.
57       * @param numUlps Value of the maximum relative error (in ulps).
58       */
59      public HermiteParametricTest(int numberOfPoints,
60                                   int maxDegree,
61                                   double eps,
62                                   double numUlps) {
63          super(factory.hermite(numberOfPoints),
64                maxDegree, eps, numUlps);
65      }
66  
67      /**
68       * Returns the collection of parameters to be passed to the constructor of
69       * this class.
70       * Gauss-Hermite quadrature rules of order 1, ..., {@link #MAX_NUM_POINTS}
71       * will be constructed.
72       *
73       * @return the collection of parameters for this parameterized test.
74       */
75      @Parameters
76      public static Collection<Object[]> getParameters() {
77          final ArrayList<Object[]> parameters = new ArrayList<Object[]>();
78          final int [] numUlps = {
79               10,  10,  10,  10,  20,
80               20,  20,  20,  30,  40,
81               40,  50, 150, 150, 150,
82              150, 150, 150, 150, 200,
83              250, 250, 300, 300, 300,
84              300, 300, 300, 350, 350
85          };
86          for (int k = 1; k <= MAX_NUM_POINTS; k++) {
87              parameters.add(new Object[] { k, 2 * k - 1, FastMath.ulp(1d), numUlps[k - 1] });
88          }
89          return parameters;
90      }
91  
92      @Override
93      public double getExpectedValue(final int n) {
94          if (n % 2 == 1) {
95              return 0;
96          }
97  
98          final int iMax = n / 2;
99          double p = 1;
100         double q = 1;
101         for (int i = 0; i < iMax; i++) {
102             p *= 2 * i + 1;
103             q *= 2;
104         }
105 
106         return p / q * SQRT_PI;
107     }
108 }