1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.linear;
19
20 import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
21 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
22 import org.apache.commons.math4.core.jdkmath.JdkMath;
23 import org.apache.commons.numbers.core.Precision;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 class SchurTransformer {
45
46 private static final int MAX_ITERATIONS = 100;
47
48
49 private final double[][] matrixP;
50
51 private final double[][] matrixT;
52
53 private RealMatrix cachedP;
54
55 private RealMatrix cachedT;
56
57 private RealMatrix cachedPt;
58
59
60 private final double epsilon = Precision.EPSILON;
61
62
63
64
65
66
67
68 SchurTransformer(final RealMatrix matrix) {
69 if (!matrix.isSquare()) {
70 throw new NonSquareMatrixException(matrix.getRowDimension(),
71 matrix.getColumnDimension());
72 }
73
74 HessenbergTransformer transformer = new HessenbergTransformer(matrix);
75 matrixT = transformer.getH().getData();
76 matrixP = transformer.getP().getData();
77 cachedT = null;
78 cachedP = null;
79 cachedPt = null;
80
81
82 transform();
83 }
84
85
86
87
88
89
90
91 public RealMatrix getP() {
92 if (cachedP == null) {
93 cachedP = MatrixUtils.createRealMatrix(matrixP);
94 }
95 return cachedP;
96 }
97
98
99
100
101
102
103
104 public RealMatrix getPT() {
105 if (cachedPt == null) {
106 cachedPt = getP().transpose();
107 }
108
109
110 return cachedPt;
111 }
112
113
114
115
116
117
118 public RealMatrix getT() {
119 if (cachedT == null) {
120 cachedT = MatrixUtils.createRealMatrix(matrixT);
121 }
122
123
124 return cachedT;
125 }
126
127
128
129
130
131 private void transform() {
132 final int n = matrixT.length;
133
134
135 final double norm = getNorm();
136
137
138 final ShiftInfo shift = new ShiftInfo();
139
140
141 int iteration = 0;
142 int iu = n - 1;
143 while (iu >= 0) {
144
145
146 final int il = findSmallSubDiagonalElement(iu, norm);
147
148
149 if (il == iu) {
150
151 matrixT[iu][iu] += shift.exShift;
152 iu--;
153 iteration = 0;
154 } else if (il == iu - 1) {
155
156 double p = (matrixT[iu - 1][iu - 1] - matrixT[iu][iu]) / 2.0;
157 double q = p * p + matrixT[iu][iu - 1] * matrixT[iu - 1][iu];
158 matrixT[iu][iu] += shift.exShift;
159 matrixT[iu - 1][iu - 1] += shift.exShift;
160
161 if (q >= 0) {
162 double z = JdkMath.sqrt(JdkMath.abs(q));
163 if (p >= 0) {
164 z = p + z;
165 } else {
166 z = p - z;
167 }
168 final double x = matrixT[iu][iu - 1];
169 final double s = JdkMath.abs(x) + JdkMath.abs(z);
170 p = x / s;
171 q = z / s;
172 final double r = JdkMath.sqrt(p * p + q * q);
173 p /= r;
174 q /= r;
175
176
177 for (int j = iu - 1; j < n; j++) {
178 z = matrixT[iu - 1][j];
179 matrixT[iu - 1][j] = q * z + p * matrixT[iu][j];
180 matrixT[iu][j] = q * matrixT[iu][j] - p * z;
181 }
182
183
184 for (int i = 0; i <= iu; i++) {
185 z = matrixT[i][iu - 1];
186 matrixT[i][iu - 1] = q * z + p * matrixT[i][iu];
187 matrixT[i][iu] = q * matrixT[i][iu] - p * z;
188 }
189
190
191 for (int i = 0; i <= n - 1; i++) {
192 z = matrixP[i][iu - 1];
193 matrixP[i][iu - 1] = q * z + p * matrixP[i][iu];
194 matrixP[i][iu] = q * matrixP[i][iu] - p * z;
195 }
196 }
197 iu -= 2;
198 iteration = 0;
199 } else {
200
201 computeShift(il, iu, iteration, shift);
202
203
204 if (++iteration > MAX_ITERATIONS) {
205 throw new MaxCountExceededException(LocalizedFormats.CONVERGENCE_FAILED,
206 MAX_ITERATIONS);
207 }
208
209
210 final double[] hVec = new double[3];
211
212 final int im = initQRStep(il, iu, shift, hVec);
213 performDoubleQRStep(il, im, iu, shift, hVec);
214 }
215 }
216 }
217
218
219
220
221
222
223 private double getNorm() {
224 double norm = 0.0;
225 for (int i = 0; i < matrixT.length; i++) {
226
227 for (int j = JdkMath.max(i - 1, 0); j < matrixT.length; j++) {
228 norm += JdkMath.abs(matrixT[i][j]);
229 }
230 }
231 return norm;
232 }
233
234
235
236
237
238
239
240
241 private int findSmallSubDiagonalElement(final int startIdx, final double norm) {
242 int l = startIdx;
243 while (l > 0) {
244 double s = JdkMath.abs(matrixT[l - 1][l - 1]) + JdkMath.abs(matrixT[l][l]);
245 if (s == 0.0) {
246 s = norm;
247 }
248 if (JdkMath.abs(matrixT[l][l - 1]) < epsilon * s) {
249 break;
250 }
251 l--;
252 }
253 return l;
254 }
255
256
257
258
259
260
261
262
263
264 private void computeShift(final int l, final int idx, final int iteration, final ShiftInfo shift) {
265
266 shift.x = matrixT[idx][idx];
267 shift.y = shift.w = 0.0;
268 if (l < idx) {
269 shift.y = matrixT[idx - 1][idx - 1];
270 shift.w = matrixT[idx][idx - 1] * matrixT[idx - 1][idx];
271 }
272
273
274 if (iteration == 10) {
275 shift.exShift += shift.x;
276 for (int i = 0; i <= idx; i++) {
277 matrixT[i][i] -= shift.x;
278 }
279 final double s = JdkMath.abs(matrixT[idx][idx - 1]) + JdkMath.abs(matrixT[idx - 1][idx - 2]);
280 shift.x = 0.75 * s;
281 shift.y = 0.75 * s;
282 shift.w = -0.4375 * s * s;
283 }
284
285
286 if (iteration == 30) {
287 double s = (shift.y - shift.x) / 2.0;
288 s = s * s + shift.w;
289 if (s > 0.0) {
290 s = JdkMath.sqrt(s);
291 if (shift.y < shift.x) {
292 s = -s;
293 }
294 s = shift.x - shift.w / ((shift.y - shift.x) / 2.0 + s);
295 for (int i = 0; i <= idx; i++) {
296 matrixT[i][i] -= s;
297 }
298 shift.exShift += s;
299 shift.x = shift.y = shift.w = 0.964;
300 }
301 }
302 }
303
304
305
306
307
308
309
310
311
312
313 private int initQRStep(int il, final int iu, final ShiftInfo shift, double[] hVec) {
314
315 int im = iu - 2;
316 while (im >= il) {
317 final double z = matrixT[im][im];
318 final double r = shift.x - z;
319 double s = shift.y - z;
320 hVec[0] = (r * s - shift.w) / matrixT[im + 1][im] + matrixT[im][im + 1];
321 hVec[1] = matrixT[im + 1][im + 1] - z - r - s;
322 hVec[2] = matrixT[im + 2][im + 1];
323
324 if (im == il) {
325 break;
326 }
327
328 final double lhs = JdkMath.abs(matrixT[im][im - 1]) * (JdkMath.abs(hVec[1]) + JdkMath.abs(hVec[2]));
329 final double rhs = JdkMath.abs(hVec[0]) * (JdkMath.abs(matrixT[im - 1][im - 1]) +
330 JdkMath.abs(z) +
331 JdkMath.abs(matrixT[im + 1][im + 1]));
332
333 if (lhs < epsilon * rhs) {
334 break;
335 }
336 im--;
337 }
338
339 return im;
340 }
341
342
343
344
345
346
347
348
349
350
351 private void performDoubleQRStep(final int il, final int im, final int iu,
352 final ShiftInfo shift, final double[] hVec) {
353
354 final int n = matrixT.length;
355 double p = hVec[0];
356 double q = hVec[1];
357 double r = hVec[2];
358
359 for (int k = im; k <= iu - 1; k++) {
360 boolean notlast = k != (iu - 1);
361 if (k != im) {
362 p = matrixT[k][k - 1];
363 q = matrixT[k + 1][k - 1];
364 r = notlast ? matrixT[k + 2][k - 1] : 0.0;
365 shift.x = JdkMath.abs(p) + JdkMath.abs(q) + JdkMath.abs(r);
366 if (Precision.equals(shift.x, 0.0, epsilon)) {
367 continue;
368 }
369 p /= shift.x;
370 q /= shift.x;
371 r /= shift.x;
372 }
373 double s = JdkMath.sqrt(p * p + q * q + r * r);
374 if (p < 0.0) {
375 s = -s;
376 }
377 if (s != 0.0) {
378 if (k != im) {
379 matrixT[k][k - 1] = -s * shift.x;
380 } else if (il != im) {
381 matrixT[k][k - 1] = -matrixT[k][k - 1];
382 }
383 p += s;
384 shift.x = p / s;
385 shift.y = q / s;
386 double z = r / s;
387 q /= p;
388 r /= p;
389
390
391 for (int j = k; j < n; j++) {
392 p = matrixT[k][j] + q * matrixT[k + 1][j];
393 if (notlast) {
394 p += r * matrixT[k + 2][j];
395 matrixT[k + 2][j] -= p * z;
396 }
397 matrixT[k][j] -= p * shift.x;
398 matrixT[k + 1][j] -= p * shift.y;
399 }
400
401
402 for (int i = 0; i <= JdkMath.min(iu, k + 3); i++) {
403 p = shift.x * matrixT[i][k] + shift.y * matrixT[i][k + 1];
404 if (notlast) {
405 p += z * matrixT[i][k + 2];
406 matrixT[i][k + 2] -= p * r;
407 }
408 matrixT[i][k] -= p;
409 matrixT[i][k + 1] -= p * q;
410 }
411
412
413 final int high = matrixT.length - 1;
414 for (int i = 0; i <= high; i++) {
415 p = shift.x * matrixP[i][k] + shift.y * matrixP[i][k + 1];
416 if (notlast) {
417 p += z * matrixP[i][k + 2];
418 matrixP[i][k + 2] -= p * r;
419 }
420 matrixP[i][k] -= p;
421 matrixP[i][k + 1] -= p * q;
422 }
423 }
424 }
425
426
427 for (int i = im + 2; i <= iu; i++) {
428 matrixT[i][i-2] = 0.0;
429 if (i > im + 2) {
430 matrixT[i][i-3] = 0.0;
431 }
432 }
433 }
434
435
436
437
438
439 private static final class ShiftInfo {
440
441
442
443 double x;
444
445 double y;
446
447 double w;
448
449 double exShift;
450
451
452 }
453 }