1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.analysis.integration.gauss;
18
19 import java.util.Arrays;
20 import java.util.SortedMap;
21 import java.util.TreeMap;
22
23 import org.hipparchus.CalculusFieldElement;
24 import org.hipparchus.Field;
25 import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
26 import org.hipparchus.exception.LocalizedCoreFormats;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.Incrementor;
30 import org.hipparchus.util.MathArrays;
31 import org.hipparchus.util.Pair;
32
33
34
35
36
37
38
39
40
41
42 public abstract class FieldAbstractRuleFactory<T extends CalculusFieldElement<T>> implements FieldRuleFactory<T> {
43
44
45 private final Field<T> field;
46
47
48 private final SortedMap<Integer, Pair<T[], T[]>> pointsAndWeights;
49
50
51
52
53 public FieldAbstractRuleFactory(final Field<T> field) {
54 this.field = field;
55 this.pointsAndWeights = new TreeMap<>();
56 }
57
58
59
60
61 public Field<T> getField() {
62 return field;
63 }
64
65
66 @Override
67 public Pair<T[], T[]> getRule(int numberOfPoints)
68 throws MathIllegalArgumentException {
69
70 if (numberOfPoints <= 0) {
71 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS,
72 numberOfPoints);
73 }
74 if (numberOfPoints > 1000) {
75 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
76 numberOfPoints, 1000);
77 }
78
79 Pair<T[], T[]> rule;
80 synchronized (pointsAndWeights) {
81
82 rule = pointsAndWeights.get(numberOfPoints);
83
84 if (rule == null) {
85
86
87
88 rule = computeRule(numberOfPoints);
89
90
91 pointsAndWeights.put(numberOfPoints, rule);
92 }
93 }
94
95
96 return new Pair<>(rule.getFirst().clone(), rule.getSecond().clone());
97
98 }
99
100
101
102
103
104
105
106
107
108 protected abstract Pair<T[], T[]> computeRule(int numberOfPoints)
109 throws MathIllegalArgumentException;
110
111
112
113
114
115
116
117
118
119
120
121
122 protected T[] findRoots(final int n, final CalculusFieldUnivariateFunction<T> ratioEvaluator) {
123
124 final T[] roots = MathArrays.buildArray(field, n);
125
126
127 if (n == 1) {
128
129 roots[0] = field.getZero();
130 } else if (n == 2) {
131
132 roots[0] = field.getOne().negate();
133 roots[1] = field.getOne();
134 } else {
135
136
137
138 final T[] previousPoints = getRule(n - 1).getFirst();
139
140
141 roots[0] = previousPoints[0];
142
143
144 for (int i = 1; i < n - 1; ++i) {
145 roots[i] = previousPoints[i - 1].add(previousPoints[i]).multiply(0.5);
146 }
147
148
149 roots[n - 1] = previousPoints[n - 2];
150
151 }
152
153
154 final T[] ratio = MathArrays.buildArray(field, n);
155 final Incrementor incrementor = new Incrementor(1000);
156 double tol;
157 double maxOffset;
158 do {
159
160
161 incrementor.increment();
162
163
164 for (int i = 0; i < n; ++i) {
165 ratio[i] = ratioEvaluator.value(roots[i]);
166 }
167
168
169 maxOffset = 0;
170 for (int i = 0; i < n; ++i) {
171 T sum = field.getZero();
172 for (int j = 0; j < n; ++j) {
173 if (j != i) {
174 sum = sum.add(roots[i].subtract(roots[j]).reciprocal());
175 }
176 }
177 final T offset = ratio[i].divide(sum.multiply(ratio[i]).negate().add(1));
178 maxOffset = FastMath.max(maxOffset, FastMath.abs(offset).getReal());
179 roots[i] = roots[i].subtract(offset);
180 }
181
182
183 tol = 0;
184 for (final T r : roots) {
185 tol = FastMath.max(tol, FastMath.ulp(r.getReal()));
186 }
187
188 } while (maxOffset > tol);
189
190
191 Arrays.sort(roots, (r1, r2) -> Double.compare(r1.getReal(), r2.getReal()));
192
193 return roots;
194
195 }
196
197
198
199
200 protected void enforceSymmetry(final T[] roots) {
201
202 final int n = roots.length;
203
204
205 for (int i = 0; i < n / 2; ++i) {
206 final int idx = n - i - 1;
207 final T c = roots[i].subtract(roots[idx]).multiply(0.5);
208 roots[i] = c;
209 roots[idx] = c.negate();
210 }
211
212
213
214
215
216 if (n % 2 != 0) {
217 roots[n / 2] = field.getZero();
218 }
219
220 }
221
222 }