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 org.hipparchus.util.Binary64;
25  import org.hipparchus.util.FastMath;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  
29  /**
30   * Base class for standard testing of Gaussian quadrature rules,
31   * which are exact for polynomials up to a certain degree. In this test, each
32   * monomial in turn is tested against the specified quadrature rule.
33   *
34   */
35  public abstract class FieldGaussianQuadratureAbstractTest {
36  
37      /**
38       * Returns the expected value of the integral of the specified monomial.
39       * The integration is carried out on the natural interval of the quadrature
40       * rule under test.
41       *
42       * @param n Degree of the monomial.
43       * @return the expected value of the integral of x<sup>n</sup>.
44       */
45      public abstract double getExpectedValue(final int n);
46  
47      /**
48       * Checks that the value of the integral of each monomial
49       * <code>x<sup>0</sup>, ... , x<sup>p</sup></code>
50       * returned by the quadrature rule under test conforms with the expected
51       * value. Here {@code p} denotes the degree of the highest polynomial for
52       * which exactness is to be expected.
53       */
54      public void testAllMonomials(FieldGaussIntegrator<Binary64> integrator,
55                                   int maxDegree, double eps, double numUlps) {
56          for (int n = 0; n <= maxDegree; n++) {
57              final double expected = getExpectedValue(n);
58  
59              final int p = n;
60              final double actual = integrator.integrate(x -> FastMath.pow(x, p))
61                              .getReal();
62  
63              // System.out.println(n + "/" + maxDegree + " " + integrator.getNumberOfPoints()
64              //                    + " " + expected + " " + actual + " " + Math.ulp(expected));
65              if (expected == 0) {
66                  assertEquals(expected, actual, eps,
67                                          "while integrating monomial x**" + n + " with a " + integrator.getNumberOfPoints() + "-point quadrature rule");
68              } else {
69                  double err = FastMath.abs(actual - expected) / Math.ulp(
70                                  expected);
71                  assertEquals(expected, actual,
72                                          Math.ulp(expected) * numUlps,
73                                          "while integrating monomial x**" + n + " with a " + +integrator.getNumberOfPoints() + "-point quadrature rule, " + " error was " + err + " ulps");
74              }
75          }
76      }
77  }