1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.numerical.integration;
17
18 import com.irurueta.algebra.Matrix;
19 import com.irurueta.algebra.WrongSizeException;
20 import com.irurueta.numerical.EvaluationException;
21
22
23
24
25
26
27
28 public class TrapezoidalMatrixQuadrature extends MatrixQuadrature {
29
30
31
32
33 private final double a;
34
35
36
37
38 private final double b;
39
40
41
42
43 private final int rows;
44
45
46
47
48 private final int columns;
49
50
51
52
53 private final Matrix s;
54
55
56
57
58 private final MatrixSingleDimensionFunctionEvaluatorListener listener;
59
60
61
62
63 private final Matrix tmpA;
64
65
66
67
68 private final Matrix tmpB;
69
70
71
72
73 private final Matrix sum;
74
75
76
77
78 private final Matrix tmpX;
79
80
81
82
83
84
85
86
87
88 public TrapezoidalMatrixQuadrature(
89 final double a, final double b, final MatrixSingleDimensionFunctionEvaluatorListener listener)
90 throws WrongSizeException {
91 this.n = 0;
92 this.a = a;
93 this.b = b;
94
95 rows = listener.getRows();
96 columns = listener.getColumns();
97 s = new Matrix(rows, columns);
98 tmpA = new Matrix(rows, columns);
99 tmpB = new Matrix(rows, columns);
100 sum = new Matrix(rows, columns);
101 tmpX = new Matrix(rows, columns);
102
103 this.listener = listener;
104 }
105
106
107
108
109
110
111 public double getA() {
112 return a;
113 }
114
115
116
117
118
119
120 public double getB() {
121 return b;
122 }
123
124
125
126
127
128
129 public Matrix getS() {
130 return s;
131 }
132
133
134
135
136
137
138
139
140 @Override
141 public void next(final Matrix result) throws EvaluationException {
142 try {
143 double x;
144 double tnm;
145 double del;
146 int it;
147 int j;
148 n++;
149 if (n == 1) {
150
151 listener.evaluate(a, tmpA);
152 listener.evaluate(b, tmpB);
153 s.copyFrom(tmpA);
154 s.add(tmpB);
155 s.multiplyByScalar(0.5 * (b - a));
156 result.copyFrom(s);
157 } else {
158 for (it = 1, j = 1; j < n - 1; j++) {
159 it <<= 1;
160 }
161 tnm = it;
162
163 del = (b - a) / tnm;
164 x = a + 0.5 * del;
165 sum.initialize(0.0);
166 for (j = 0; j < it; j++, x += del) {
167 listener.evaluate(x, tmpX);
168 sum.add(tmpX);
169 }
170
171
172 sum.multiplyByScalar((b - a) / tnm);
173 s.add(sum);
174 s.multiplyByScalar(0.5);
175 result.copyFrom(s);
176 }
177 } catch (final WrongSizeException ex) {
178 throw new EvaluationException(ex);
179 }
180 }
181
182
183
184
185
186
187 @Override
188 public QuadratureType getType() {
189 return QuadratureType.TRAPEZOIDAL;
190 }
191
192
193
194
195
196
197 @Override
198 protected int getRows() {
199 return rows;
200 }
201
202
203
204
205
206
207 @Override
208 protected int getColumns() {
209 return columns;
210 }
211 }