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.core.jdkmath.JdkMath;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 class BiDiagonalTransformer {
38
39
40 private final double[][] householderVectors;
41
42
43 private final double[] main;
44
45
46 private final double[] secondary;
47
48
49 private RealMatrix cachedU;
50
51
52 private RealMatrix cachedB;
53
54
55 private RealMatrix cachedV;
56
57
58
59
60
61 BiDiagonalTransformer(RealMatrix matrix) {
62
63 final int m = matrix.getRowDimension();
64 final int n = matrix.getColumnDimension();
65 final int p = JdkMath.min(m, n);
66 householderVectors = matrix.getData();
67 main = new double[p];
68 secondary = new double[p - 1];
69 cachedU = null;
70 cachedB = null;
71 cachedV = null;
72
73
74 if (m >= n) {
75 transformToUpperBiDiagonal();
76 } else {
77 transformToLowerBiDiagonal();
78 }
79 }
80
81
82
83
84
85
86 public RealMatrix getU() {
87
88 if (cachedU == null) {
89
90 final int m = householderVectors.length;
91 final int n = householderVectors[0].length;
92 final int p = main.length;
93 final int diagOffset = (m >= n) ? 0 : 1;
94 final double[] diagonal = (m >= n) ? main : secondary;
95 double[][] ua = new double[m][m];
96
97
98 for (int k = m - 1; k >= p; --k) {
99 ua[k][k] = 1;
100 }
101
102
103 for (int k = p - 1; k >= diagOffset; --k) {
104 final double[] hK = householderVectors[k];
105 ua[k][k] = 1;
106 if (hK[k - diagOffset] != 0.0) {
107 for (int j = k; j < m; ++j) {
108 double alpha = 0;
109 for (int i = k; i < m; ++i) {
110 alpha -= ua[i][j] * householderVectors[i][k - diagOffset];
111 }
112 alpha /= diagonal[k - diagOffset] * hK[k - diagOffset];
113
114 for (int i = k; i < m; ++i) {
115 ua[i][j] += -alpha * householderVectors[i][k - diagOffset];
116 }
117 }
118 }
119 }
120 if (diagOffset > 0) {
121 ua[0][0] = 1;
122 }
123 cachedU = MatrixUtils.createRealMatrix(ua);
124 }
125
126
127 return cachedU;
128 }
129
130
131
132
133
134 public RealMatrix getB() {
135
136 if (cachedB == null) {
137
138 final int m = householderVectors.length;
139 final int n = householderVectors[0].length;
140 double[][] ba = new double[m][n];
141 for (int i = 0; i < main.length; ++i) {
142 ba[i][i] = main[i];
143 if (m < n) {
144 if (i > 0) {
145 ba[i][i-1] = secondary[i - 1];
146 }
147 } else {
148 if (i < main.length - 1) {
149 ba[i][i+1] = secondary[i];
150 }
151 }
152 }
153 cachedB = MatrixUtils.createRealMatrix(ba);
154 }
155
156
157 return cachedB;
158 }
159
160
161
162
163
164
165 public RealMatrix getV() {
166
167 if (cachedV == null) {
168
169 final int m = householderVectors.length;
170 final int n = householderVectors[0].length;
171 final int p = main.length;
172 final int diagOffset = (m >= n) ? 1 : 0;
173 final double[] diagonal = (m >= n) ? secondary : main;
174 double[][] va = new double[n][n];
175
176
177 for (int k = n - 1; k >= p; --k) {
178 va[k][k] = 1;
179 }
180
181
182 for (int k = p - 1; k >= diagOffset; --k) {
183 final double[] hK = householderVectors[k - diagOffset];
184 va[k][k] = 1;
185 if (hK[k] != 0.0) {
186 for (int j = k; j < n; ++j) {
187 double beta = 0;
188 for (int i = k; i < n; ++i) {
189 beta -= va[i][j] * hK[i];
190 }
191 beta /= diagonal[k - diagOffset] * hK[k];
192
193 for (int i = k; i < n; ++i) {
194 va[i][j] += -beta * hK[i];
195 }
196 }
197 }
198 }
199 if (diagOffset > 0) {
200 va[0][0] = 1;
201 }
202 cachedV = MatrixUtils.createRealMatrix(va);
203 }
204
205
206 return cachedV;
207 }
208
209
210
211
212
213
214
215 double[][] getHouseholderVectorsRef() {
216 return householderVectors;
217 }
218
219
220
221
222
223
224
225 double[] getMainDiagonalRef() {
226 return main;
227 }
228
229
230
231
232
233
234
235 double[] getSecondaryDiagonalRef() {
236 return secondary;
237 }
238
239
240
241
242
243 boolean isUpperBiDiagonal() {
244 return householderVectors.length >= householderVectors[0].length;
245 }
246
247
248
249
250
251
252 private void transformToUpperBiDiagonal() {
253
254 final int m = householderVectors.length;
255 final int n = householderVectors[0].length;
256 for (int k = 0; k < n; k++) {
257
258
259 double xNormSqr = 0;
260 for (int i = k; i < m; ++i) {
261 final double c = householderVectors[i][k];
262 xNormSqr += c * c;
263 }
264 final double[] hK = householderVectors[k];
265 final double a = (hK[k] > 0) ? -JdkMath.sqrt(xNormSqr) : JdkMath.sqrt(xNormSqr);
266 main[k] = a;
267 if (a != 0.0) {
268 hK[k] -= a;
269 for (int j = k + 1; j < n; ++j) {
270 double alpha = 0;
271 for (int i = k; i < m; ++i) {
272 final double[] hI = householderVectors[i];
273 alpha -= hI[j] * hI[k];
274 }
275 alpha /= a * householderVectors[k][k];
276 for (int i = k; i < m; ++i) {
277 final double[] hI = householderVectors[i];
278 hI[j] -= alpha * hI[k];
279 }
280 }
281 }
282
283 if (k < n - 1) {
284
285 xNormSqr = 0;
286 for (int j = k + 1; j < n; ++j) {
287 final double c = hK[j];
288 xNormSqr += c * c;
289 }
290 final double b = (hK[k + 1] > 0) ? -JdkMath.sqrt(xNormSqr) : JdkMath.sqrt(xNormSqr);
291 secondary[k] = b;
292 if (b != 0.0) {
293 hK[k + 1] -= b;
294 for (int i = k + 1; i < m; ++i) {
295 final double[] hI = householderVectors[i];
296 double beta = 0;
297 for (int j = k + 1; j < n; ++j) {
298 beta -= hI[j] * hK[j];
299 }
300 beta /= b * hK[k + 1];
301 for (int j = k + 1; j < n; ++j) {
302 hI[j] -= beta * hK[j];
303 }
304 }
305 }
306 }
307 }
308 }
309
310
311
312
313
314
315 private void transformToLowerBiDiagonal() {
316
317 final int m = householderVectors.length;
318 final int n = householderVectors[0].length;
319 for (int k = 0; k < m; k++) {
320
321
322 final double[] hK = householderVectors[k];
323 double xNormSqr = 0;
324 for (int j = k; j < n; ++j) {
325 final double c = hK[j];
326 xNormSqr += c * c;
327 }
328 final double a = (hK[k] > 0) ? -JdkMath.sqrt(xNormSqr) : JdkMath.sqrt(xNormSqr);
329 main[k] = a;
330 if (a != 0.0) {
331 hK[k] -= a;
332 for (int i = k + 1; i < m; ++i) {
333 final double[] hI = householderVectors[i];
334 double alpha = 0;
335 for (int j = k; j < n; ++j) {
336 alpha -= hI[j] * hK[j];
337 }
338 alpha /= a * householderVectors[k][k];
339 for (int j = k; j < n; ++j) {
340 hI[j] -= alpha * hK[j];
341 }
342 }
343 }
344
345 if (k < m - 1) {
346
347 final double[] hKp1 = householderVectors[k + 1];
348 xNormSqr = 0;
349 for (int i = k + 1; i < m; ++i) {
350 final double c = householderVectors[i][k];
351 xNormSqr += c * c;
352 }
353 final double b = (hKp1[k] > 0) ? -JdkMath.sqrt(xNormSqr) : JdkMath.sqrt(xNormSqr);
354 secondary[k] = b;
355 if (b != 0.0) {
356 hKp1[k] -= b;
357 for (int j = k + 1; j < n; ++j) {
358 double beta = 0;
359 for (int i = k + 1; i < m; ++i) {
360 final double[] hI = householderVectors[i];
361 beta -= hI[j] * hI[k];
362 }
363 beta /= b * hKp1[k];
364 for (int i = k + 1; i < m; ++i) {
365 final double[] hI = householderVectors[i];
366 hI[j] -= beta * hI[k];
367 }
368 }
369 }
370 }
371 }
372 }
373 }