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