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.core.Pair;
21
22 /**
23 * This class's implements {@link #integrate(UnivariateFunction) integrate}
24 * method assuming that the integral is symmetric about 0.
25 * This allows to reduce numerical errors.
26 *
27 * @since 3.3
28 */
29 public class SymmetricGaussIntegrator extends GaussIntegrator {
30 /**
31 * Creates an integrator from the given {@code points} and {@code weights}.
32 * The integration interval is defined by the first and last value of
33 * {@code points} which must be sorted in increasing order.
34 *
35 * @param points Integration points.
36 * @param weights Weights of the corresponding integration nodes.
37 * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException if the {@code points} are not
38 * sorted in increasing order.
39 * @throws org.apache.commons.math4.legacy.exception.DimensionMismatchException if points and weights don't have the same length
40 */
41 public SymmetricGaussIntegrator(double[] points,
42 double[] weights) {
43 super(points, weights);
44 }
45
46 /**
47 * Creates an integrator from the given pair of points (first element of
48 * the pair) and weights (second element of the pair.
49 *
50 * @param pointsAndWeights Integration points and corresponding weights.
51 * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException if the {@code points} are not
52 * sorted in increasing order.
53 *
54 * @see #SymmetricGaussIntegrator(double[], double[])
55 */
56 public SymmetricGaussIntegrator(Pair<double[], double[]> pointsAndWeights) {
57 this(pointsAndWeights.getFirst(), pointsAndWeights.getSecond());
58 }
59
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 public double integrate(UnivariateFunction f) {
65 final int ruleLength = getNumberOfPoints();
66
67 if (ruleLength == 1) {
68 return getWeight(0) * f.value(0d);
69 }
70
71 final int iMax = ruleLength / 2;
72 double s = 0;
73 double c = 0;
74 for (int i = 0; i < iMax; i++) {
75 final double p = getPoint(i);
76 final double w = getWeight(i);
77
78 final double f1 = f.value(p);
79 final double f2 = f.value(-p);
80
81 final double y = w * (f1 + f2) - c;
82 final double t = s + y;
83
84 c = (t - s) - y;
85 s = t;
86 }
87
88 if ((ruleLength & 1) != 0) {
89 final double w = getWeight(iMax);
90
91 final double y = w * f.value(0d) - c;
92 final double t = s + y;
93
94 s = t;
95 }
96
97 return s;
98 }
99 }