1 package Jama;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 public class LUDecomposition implements java.io.Serializable {
18
19
20
21
22
23
24
25
26 private double[][] LU;
27
28
29
30
31
32
33 private int m, n, pivsign;
34
35
36
37
38 private int[] piv;
39
40
41
42
43
44
45
46
47
48 public LUDecomposition (Matrix A) {
49
50
51
52 LU = A.getArrayCopy();
53 m = A.getRowDimension();
54 n = A.getColumnDimension();
55 piv = new int[m];
56 for (int i = 0; i < m; i++) {
57 piv[i] = i;
58 }
59 pivsign = 1;
60 double[] LUrowi;
61 double[] LUcolj = new double[m];
62
63
64
65 for (int j = 0; j < n; j++) {
66
67
68
69 for (int i = 0; i < m; i++) {
70 LUcolj[i] = LU[i][j];
71 }
72
73
74
75 for (int i = 0; i < m; i++) {
76 LUrowi = LU[i];
77
78
79
80 int kmax = Math.min(i,j);
81 double s = 0.0;
82 for (int k = 0; k < kmax; k++) {
83 s += LUrowi[k]*LUcolj[k];
84 }
85
86 LUrowi[j] = LUcolj[i] -= s;
87 }
88
89
90
91 int p = j;
92 for (int i = j+1; i < m; i++) {
93 if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) {
94 p = i;
95 }
96 }
97 if (p != j) {
98 for (int k = 0; k < n; k++) {
99 double t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t;
100 }
101 int k = piv[p]; piv[p] = piv[j]; piv[j] = k;
102 pivsign = -pivsign;
103 }
104
105
106
107 if (j < m & LU[j][j] != 0.0) {
108 for (int i = j+1; i < m; i++) {
109 LU[i][j] /= LU[j][j];
110 }
111 }
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public boolean isNonsingular () {
183 for (int j = 0; j < n; j++) {
184 if (LU[j][j] == 0)
185 return false;
186 }
187 return true;
188 }
189
190
191
192
193
194 public Matrix getL () {
195 Matrix X = new Matrix(m,n);
196 double[][] L = X.getArray();
197 for (int i = 0; i < m; i++) {
198 for (int j = 0; j < n; j++) {
199 if (i > j) {
200 L[i][j] = LU[i][j];
201 } else if (i == j) {
202 L[i][j] = 1.0;
203 } else {
204 L[i][j] = 0.0;
205 }
206 }
207 }
208 return X;
209 }
210
211
212
213
214
215 public Matrix getU () {
216 Matrix X = new Matrix(n,n);
217 double[][] U = X.getArray();
218 for (int i = 0; i < n; i++) {
219 for (int j = 0; j < n; j++) {
220 if (i <= j) {
221 U[i][j] = LU[i][j];
222 } else {
223 U[i][j] = 0.0;
224 }
225 }
226 }
227 return X;
228 }
229
230
231
232
233
234 public int[] getPivot () {
235 int[] p = new int[m];
236 for (int i = 0; i < m; i++) {
237 p[i] = piv[i];
238 }
239 return p;
240 }
241
242
243
244
245
246 public double[] getDoublePivot () {
247 double[] vals = new double[m];
248 for (int i = 0; i < m; i++) {
249 vals[i] = (double) piv[i];
250 }
251 return vals;
252 }
253
254
255
256
257
258
259 public double det () {
260 if (m != n) {
261 throw new IllegalArgumentException("Matrix must be square.");
262 }
263 double d = (double) pivsign;
264 for (int j = 0; j < n; j++) {
265 d *= LU[j][j];
266 }
267 return d;
268 }
269
270
271
272
273
274
275
276
277 public Matrix solve (Matrix B) {
278 if (B.getRowDimension() != m) {
279 throw new IllegalArgumentException("Matrix row dimensions must agree.");
280 }
281 if (!this.isNonsingular()) {
282 throw new RuntimeException("Matrix is singular.");
283 }
284
285
286 int nx = B.getColumnDimension();
287 Matrix Xmat = B.getMatrix(piv,0,nx-1);
288 double[][] X = Xmat.getArray();
289
290
291 for (int k = 0; k < n; k++) {
292 for (int i = k+1; i < n; i++) {
293 for (int j = 0; j < nx; j++) {
294 X[i][j] -= X[k][j]*LU[i][k];
295 }
296 }
297 }
298
299 for (int k = n-1; k >= 0; k--) {
300 for (int j = 0; j < nx; j++) {
301 X[k][j] /= LU[k][k];
302 }
303 for (int i = 0; i < k; i++) {
304 for (int j = 0; j < nx; j++) {
305 X[i][j] -= X[k][j]*LU[i][k];
306 }
307 }
308 }
309 return Xmat;
310 }
311 }