1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.linear;
19
20 import java.util.Arrays;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 class TriDiagonalTransformer {
39
40
41 private final double householderVectors[][];
42
43
44 private final double[] main;
45
46
47 private final double[] secondary;
48
49
50 private RealMatrix cachedQ;
51
52
53 private RealMatrix cachedQt;
54
55
56 private RealMatrix cachedT;
57
58
59
60
61
62
63
64
65 public TriDiagonalTransformer(RealMatrix matrix)
66 throws InvalidMatrixException {
67 if (!matrix.isSquare()) {
68 throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension());
69 }
70
71 final int m = matrix.getRowDimension();
72 householderVectors = matrix.getData();
73 main = new double[m];
74 secondary = new double[m - 1];
75 cachedQ = null;
76 cachedQt = null;
77 cachedT = null;
78
79
80 transform();
81
82 }
83
84
85
86
87
88
89 public RealMatrix getQ() {
90 if (cachedQ == null) {
91 cachedQ = getQT().transpose();
92 }
93 return cachedQ;
94 }
95
96
97
98
99
100
101 public RealMatrix getQT() {
102
103 if (cachedQt == null) {
104
105 final int m = householderVectors.length;
106 cachedQt = MatrixUtils.createRealMatrix(m, m);
107
108
109 for (int k = m - 1; k >= 1; --k) {
110 final double[] hK = householderVectors[k - 1];
111 final double inv = 1.0 / (secondary[k - 1] * hK[k]);
112 cachedQt.setEntry(k, k, 1);
113 if (hK[k] != 0.0) {
114 double beta = 1.0 / secondary[k - 1];
115 cachedQt.setEntry(k, k, 1 + beta * hK[k]);
116 for (int i = k + 1; i < m; ++i) {
117 cachedQt.setEntry(k, i, beta * hK[i]);
118 }
119 for (int j = k + 1; j < m; ++j) {
120 beta = 0;
121 for (int i = k + 1; i < m; ++i) {
122 beta += cachedQt.getEntry(j, i) * hK[i];
123 }
124 beta *= inv;
125 cachedQt.setEntry(j, k, beta * hK[k]);
126 for (int i = k + 1; i < m; ++i) {
127 cachedQt.addToEntry(j, i, beta * hK[i]);
128 }
129 }
130 }
131 }
132 cachedQt.setEntry(0, 0, 1);
133
134 }
135
136
137 return cachedQt;
138
139 }
140
141
142
143
144
145 public RealMatrix getT() {
146
147 if (cachedT == null) {
148
149 final int m = main.length;
150 cachedT = MatrixUtils.createRealMatrix(m, m);
151 for (int i = 0; i < m; ++i) {
152 cachedT.setEntry(i, i, main[i]);
153 if (i > 0) {
154 cachedT.setEntry(i, i - 1, secondary[i - 1]);
155 }
156 if (i < main.length - 1) {
157 cachedT.setEntry(i, i + 1, secondary[i]);
158 }
159 }
160
161 }
162
163
164 return cachedT;
165
166 }
167
168
169
170
171
172
173
174 double[][] getHouseholderVectorsRef() {
175 return householderVectors;
176 }
177
178
179
180
181
182
183
184 double[] getMainDiagonalRef() {
185 return main;
186 }
187
188
189
190
191
192
193
194 double[] getSecondaryDiagonalRef() {
195 return secondary;
196 }
197
198
199
200
201
202 private void transform() {
203
204 final int m = householderVectors.length;
205 final double[] z = new double[m];
206 for (int k = 0; k < m - 1; k++) {
207
208
209 final double[] hK = householderVectors[k];
210 main[k] = hK[k];
211 double xNormSqr = 0;
212 for (int j = k + 1; j < m; ++j) {
213 final double c = hK[j];
214 xNormSqr += c * c;
215 }
216 final double a = (hK[k + 1] > 0) ? -Math.sqrt(xNormSqr) : Math.sqrt(xNormSqr);
217 secondary[k] = a;
218 if (a != 0.0) {
219
220
221 hK[k + 1] -= a;
222 final double beta = -1 / (a * hK[k + 1]);
223
224
225
226
227
228 Arrays.fill(z, k + 1, m, 0);
229 for (int i = k + 1; i < m; ++i) {
230 final double[] hI = householderVectors[i];
231 final double hKI = hK[i];
232 double zI = hI[i] * hKI;
233 for (int j = i + 1; j < m; ++j) {
234 final double hIJ = hI[j];
235 zI += hIJ * hK[j];
236 z[j] += hIJ * hKI;
237 }
238 z[i] = beta * (z[i] + zI);
239 }
240
241
242 double gamma = 0;
243 for (int i = k + 1; i < m; ++i) {
244 gamma += z[i] * hK[i];
245 }
246 gamma *= beta / 2;
247
248
249 for (int i = k + 1; i < m; ++i) {
250 z[i] -= gamma * hK[i];
251 }
252
253
254
255 for (int i = k + 1; i < m; ++i) {
256 final double[] hI = householderVectors[i];
257 for (int j = i; j < m; ++j) {
258 hI[j] -= hK[i] * z[j] + z[i] * hK[j];
259 }
260 }
261
262 }
263
264 }
265 main[m - 1] = householderVectors[m - 1][m - 1];
266 }
267
268 }