1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.field.linalg;
19
20 import org.apache.commons.numbers.field.Field;
21 import org.apache.commons.math4.legacy.linear.SingularMatrixException;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public final class FieldLUDecomposition<T> {
44
45 private final Field<T> field;
46
47 private final FieldDenseMatrix<T> mLU;
48
49 private final int[] pivot;
50
51 private final boolean isSingular;
52
53 private final boolean isEven;
54
55
56
57
58
59
60
61 private FieldLUDecomposition(FieldDenseMatrix<T> matrix) {
62 matrix.checkMultiply(matrix);
63
64 field = matrix.getField();
65 final int m = matrix.getRowDimension();
66 pivot = new int[m];
67
68
69 for (int row = 0; row < m; row++) {
70 pivot[row] = row;
71 }
72 mLU = matrix.copy();
73
74 boolean even = true;
75 boolean singular = false;
76
77 for (int col = 0; col < m; col++) {
78 T sum = field.zero();
79
80
81 for (int row = 0; row < col; row++) {
82 sum = mLU.get(row, col);
83 for (int i = 0; i < row; i++) {
84 sum = field.subtract(sum,
85 field.multiply(mLU.get(row, i),
86 mLU.get(i, col)));
87 }
88 mLU.set(row, col, sum);
89 }
90
91
92 int nonZero = col;
93 for (int row = col; row < m; row++) {
94 sum = mLU.get(row, col);
95 for (int i = 0; i < col; i++) {
96 sum = field.subtract(sum,
97 field.multiply(mLU.get(row, i),
98 mLU.get(i, col)));
99 }
100 mLU.set(row, col, sum);
101
102 if (mLU.get(nonZero, col).equals(field.zero())) {
103
104 ++nonZero;
105 }
106 }
107
108
109 if (nonZero >= m) {
110 singular = true;
111 } else {
112
113 if (nonZero != col) {
114 T tmp = field.zero();
115 for (int i = 0; i < m; i++) {
116 tmp = mLU.get(nonZero, i);
117 mLU.set(nonZero, i, mLU.get(col, i));
118 mLU.set(col, i, tmp);
119 }
120 int temp = pivot[nonZero];
121 pivot[nonZero] = pivot[col];
122 pivot[col] = temp;
123 even = !even;
124 }
125
126
127 final T luDiag = mLU.get(col, col);
128 for (int row = col + 1; row < m; row++) {
129 mLU.set(row, col, field.divide(mLU.get(row, col),
130 luDiag));
131 }
132 }
133 }
134
135 isSingular = singular;
136 isEven = even;
137 }
138
139
140
141
142
143
144
145
146 public static <T> FieldLUDecomposition<T> of(FieldDenseMatrix<T> m) {
147 return new FieldLUDecomposition<>(m);
148 }
149
150
151
152
153 public boolean isSingular() {
154 return isSingular;
155 }
156
157
158
159
160
161
162
163 public FieldDenseMatrix<T> getL() {
164 if (isSingular) {
165 throw new SingularMatrixException();
166 }
167
168 final int m = pivot.length;
169 final FieldDenseMatrix<T> mL = FieldDenseMatrix.zero(field, m, m);
170 for (int i = 0; i < m; i++) {
171 for (int j = 0; j < i; j++) {
172 mL.set(i, j, mLU.get(i, j));
173 }
174 mL.set(i, i, field.one());
175 }
176
177 return mL;
178 }
179
180
181
182
183
184
185
186 public FieldDenseMatrix<T> getU() {
187 if (isSingular) {
188 throw new SingularMatrixException();
189 }
190
191 final int m = pivot.length;
192 final FieldDenseMatrix<T> mU = FieldDenseMatrix.zero(field, m, m);
193 for (int i = 0; i < m; i++) {
194 for (int j = i; j < m; j++) {
195 mU.set(i, j, mLU.get(i, j));
196 }
197 }
198
199 return mU;
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214 public FieldDenseMatrix<T> getP() {
215 if (isSingular) {
216 throw new SingularMatrixException();
217 }
218
219 final int m = pivot.length;
220 final FieldDenseMatrix<T> mP = FieldDenseMatrix.zero(field, m, m);
221
222 for (int i = 0; i < m; i++) {
223 mP.set(i, pivot[i], field.one());
224 }
225
226 return mP;
227 }
228
229
230
231
232
233
234
235
236 public int[] getPivot() {
237 return pivot.clone();
238 }
239
240
241
242
243
244 public T getDeterminant() {
245 if (isSingular) {
246 return field.zero();
247 } else {
248 final int m = pivot.length;
249 T determinant = isEven ?
250 field.one() :
251 field.negate(field.one());
252
253 for (int i = 0; i < m; i++) {
254 determinant = field.multiply(determinant,
255 mLU.get(i, i));
256 }
257
258 return determinant;
259 }
260 }
261
262
263
264
265
266
267
268
269 public FieldDecompositionSolver<T> getSolver() {
270 if (isSingular) {
271 throw new SingularMatrixException();
272 }
273
274 return new Solver<>(mLU, pivot);
275 }
276
277
278
279
280
281
282 private static final class Solver<T> implements FieldDecompositionSolver<T> {
283
284 private final Field<T> field;
285
286 private final FieldDenseMatrix<T> mLU;
287
288 private final int[] pivot;
289
290
291
292
293
294
295
296 private Solver(final FieldDenseMatrix<T> mLU,
297 final int[] pivot) {
298 field = mLU.getField();
299 this.mLU = mLU.copy();
300 this.pivot = pivot.clone();
301 }
302
303
304 @Override
305 public FieldDenseMatrix<T> solve(final FieldDenseMatrix<T> b) {
306 mLU.checkMultiply(b);
307
308 final FieldDenseMatrix<T> bp = b.copy();
309 final int nColB = b.getColumnDimension();
310 final int m = pivot.length;
311
312
313 for (int row = 0; row < m; row++) {
314 final int pRow = pivot[row];
315 for (int col = 0; col < nColB; col++) {
316 bp.set(row, col,
317 b.get(row, col));
318 }
319 }
320
321
322 for (int col = 0; col < m; col++) {
323 for (int i = col + 1; i < m; i++) {
324 for (int j = 0; j < nColB; j++) {
325 bp.set(i, j,
326 field.subtract(bp.get(i, j),
327 field.multiply(bp.get(col, j),
328 mLU.get(i, col))));
329 }
330 }
331 }
332
333
334 for (int col = m - 1; col >= 0; col--) {
335 for (int j = 0; j < nColB; j++) {
336 bp.set(col, j,
337 field.divide(bp.get(col, j),
338 mLU.get(col, col)));
339 }
340 for (int i = 0; i < col; i++) {
341 for (int j = 0; j < nColB; j++) {
342 bp.set(i, j,
343 field.subtract(bp.get(i, j),
344 field.multiply(bp.get(col, j),
345 mLU.get(i, col))));
346 }
347 }
348 }
349
350 return bp;
351 }
352
353
354 @Override
355 public FieldDenseMatrix<T> getInverse() {
356 return solve(FieldDenseMatrix.identity(field, pivot.length));
357 }
358 }
359 }