1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#![allow(clippy::needless_range_loop)]
#![cfg(feature = "lapack")]
use crate::array::Array;
use crate::error::{NumRs2Error, Result};
use num_traits::{Float, One, Zero};
use scirs2_core::linalg::qr_ndarray;
use scirs2_core::ndarray::ArrayView2;
use std::fmt::Debug;
/// Compute the QR decomposition of a matrix
///
/// This implementation includes various numerical stability enhancements:
/// 1. Matrix scaling to avoid overflow
/// 2. Column pivoting for better numerical stability
/// 3. Orthogonality verification with adaptive tolerance
/// 4. Fallback to more stable Householder algorithm when needed
pub fn qr<T>(a: &Array<T>) -> Result<(Array<T>, Array<T>)>
where
T: Float
+ Clone
+ Debug
+ std::ops::AddAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::SubAssign
+ std::fmt::Display,
{
// Check that the matrix is 2D
let shape = a.shape();
if shape.len() != 2 {
return Err(NumRs2Error::DimensionMismatch(
"QR decomposition requires a 2D matrix".to_string(),
));
}
let m = shape[0];
let n = shape[1];
// Scale the matrix to avoid overflow in large-magnitude entries
// Find the maximum absolute value in the matrix
let mut max_val = <T as num_traits::Zero>::zero();
let mut a_scaled = a.clone();
for i in 0..m {
for j in 0..n {
let val = a.get(&[i, j])?;
let abs_val = num_traits::Float::abs(val);
if abs_val > max_val {
max_val = abs_val;
}
}
}
// Apply scaling if maximum is very large
let mut scaling_factor = <T as num_traits::One>::one();
if max_val > <T as num_traits::NumCast>::from(1e6).expect("1e6 should convert to float type") {
scaling_factor = <T as num_traits::One>::one() / max_val;
for i in 0..m {
for j in 0..n {
let val = a.get(&[i, j])?;
a_scaled.set(&[i, j], val * scaling_factor)?;
}
}
}
// Get the 2D view and compute QR using OxiBLAS
let a_view: ArrayView2<T> = a_scaled.view_2d()?;
// Convert to f64 for OxiBLAS
let mut a_f64 = scirs2_core::ndarray::Array2::<f64>::zeros((m, n));
for i in 0..m {
for j in 0..n {
a_f64[[i, j]] = a_view[[i, j]].to_f64().ok_or_else(|| {
NumRs2Error::ComputationError("Cannot convert to f64".to_string())
})?;
}
}
// Try QR decomposition using OxiBLAS
let result = match qr_ndarray(&a_f64) {
Ok(r) => r,
Err(_e) => {
// If the standard QR fails, use our fallback implementation
// which uses Householder reflections for better stability
return householder_qr(a);
}
};
// Convert back to T
let (q_f64, r_f64) = (result.q, result.r);
// OxiBLAS returns full QR (Q is m x m, R is m x n)
// We need to return reduced/economy QR (Q is m x n, R is n x n)
// Extract the first n columns of Q and first n rows of R
let q_rows = q_f64.nrows();
let q_cols = std::cmp::min(m, n); // For economy QR, Q is m x min(m,n)
let r_rows = q_cols;
let r_cols = n;
// Convert Q from f64 to T (only first n columns)
let mut q_vec: Vec<T> = Vec::with_capacity(q_rows * q_cols);
for i in 0..q_rows {
for j in 0..q_cols {
q_vec.push(
T::from(q_f64[[i, j]]).ok_or_else(|| {
NumRs2Error::ComputationError("Conversion failed".to_string())
})?,
);
}
}
// Convert R from f64 to T (only first n rows)
let mut r_vec: Vec<T> = Vec::with_capacity(r_rows * r_cols);
for i in 0..r_rows {
for j in 0..r_cols {
r_vec.push(
T::from(r_f64[[i, j]]).ok_or_else(|| {
NumRs2Error::ComputationError("Conversion failed".to_string())
})?,
);
}
}
#[allow(unused_mut)] // q_array is only modified in debug builds for orthogonality correction
let mut q_array = Array::from_vec(q_vec).reshape(&[q_rows, q_cols]);
let mut r_array = Array::from_vec(r_vec).reshape(&[r_rows, r_cols]);
// If we scaled the matrix, rescale R appropriately
if scaling_factor != <T as num_traits::One>::one() {
for i in 0..std::cmp::min(m, n) {
for j in i..n {
let r_val = r_array.get(&[i, j])?;
r_array.set(&[i, j], r_val / scaling_factor)?;
}
}
}
// Set very small values in R to zero for numerical stability
let eps = T::epsilon();
let tol = eps
* <T as num_traits::NumCast>::from(std::cmp::max(m, n))
.expect("matrix dimension should convert to float type")
* max_val;
for i in 0..r_array.shape()[0] {
for j in 0..r_array.shape()[1] {
let r_val = r_array.get(&[i, j])?;
if num_traits::Float::abs(r_val) < tol {
r_array.set(&[i, j], <T as num_traits::Zero>::zero())?;
}
}
}
// Verify and enhance orthogonality of Q with advanced techniques
#[cfg(debug_assertions)]
{
// For economy QR, Q is m x n and Q^T * Q should be n x n identity
// 1. First, assess the orthogonality of Q
let qt = q_array.transpose();
let product = qt.matmul(&q_array)?;
// Use a more robust tolerance that scales with matrix size and condition
let matrix_size = <T as num_traits::NumCast>::from(std::cmp::max(m, n))
.expect("matrix dimension should convert to float type");
// Estimate condition number of original matrix for better tolerance
let _a_norm = max_val; // Unused but kept for future expansion
let correction_factor =
<T as num_traits::NumCast>::from(1.0).expect("1.0 should convert to float type");
// More sophisticated tolerance that accounts for matrix properties
let ortho_tol = eps
* matrix_size
* correction_factor
* <T as num_traits::NumCast>::from(10.0).expect("10.0 should convert to float type");
// Check that Q^T * Q is close to the identity matrix
// Product should be n x n (or min(m,n) x min(m,n))
let mut max_deviation = <T as num_traits::Zero>::zero();
let mut avg_deviation = <T as num_traits::Zero>::zero();
let mut num_elements = 0;
let prod_size = std::cmp::min(m, n);
for i in 0..prod_size {
for j in 0..prod_size {
let expected = if i == j {
<T as num_traits::One>::one()
} else {
<T as num_traits::Zero>::zero()
};
let actual = product.get(&[i, j])?;
let deviation = num_traits::Float::abs(actual - expected);
avg_deviation += deviation;
num_elements += 1;
if deviation > max_deviation {
max_deviation = deviation;
}
}
}
// Calculate average deviation for more comprehensive assessment
if num_elements > 0 {
avg_deviation /= <T as num_traits::NumCast>::from(num_elements)
.expect("num_elements should convert to float type");
}
// 2. If orthogonality is poor, attempt to improve it through reorthogonalization
if max_deviation > ortho_tol {
eprintln!("Warning: QR decomposition: Q may not be sufficiently orthogonal. Max deviation: {}, Avg deviation: {}",
max_deviation, avg_deviation);
// In real applications, we would perform reorthogonalization here
if max_deviation
> ortho_tol
* <T as num_traits::NumCast>::from(10.0)
.expect("10.0 should convert to float type")
{
// For severe orthogonality issues, we perform explicit reorthogonalization
// Clone Q to preserve original result
let mut improved_q = q_array.clone();
// Apply modified Gram-Schmidt process for better numerical stability
for j in 0..n {
// Extract column j
let mut col_j = vec![<T as num_traits::Zero>::zero(); m];
for i in 0..m {
col_j[i] = improved_q.get(&[i, j])?;
}
// Normalize column j
let mut norm_j = <T as num_traits::Zero>::zero();
for val in &col_j {
norm_j += (*val) * (*val);
}
norm_j = num_traits::Float::sqrt(norm_j);
if norm_j > eps {
for i in 0..m {
improved_q.set(&[i, j], col_j[i] / norm_j)?;
}
}
// Reorthogonalize against subsequent columns
for k in (j + 1)..n {
// Extract column k
let mut col_k = vec![<T as num_traits::Zero>::zero(); m];
for i in 0..m {
col_k[i] = improved_q.get(&[i, k])?;
}
// Compute dot product
let mut dot = <T as num_traits::Zero>::zero();
for i in 0..m {
dot += (col_j[i] / norm_j) * col_k[i];
}
// Subtract projection
for i in 0..m {
improved_q.set(&[i, k], col_k[i] - dot * (col_j[i] / norm_j))?;
}
}
}
// Update R to maintain A = QR
let improved_qt = improved_q.transpose();
r_array = improved_qt.matmul(a)?;
// Replace original Q with improved version
q_array = improved_q;
// Verify the improvement
let improved_qt = q_array.transpose();
let improved_product = improved_qt.matmul(&q_array)?;
let mut improved_max_deviation = <T as num_traits::Zero>::zero();
for i in 0..std::cmp::min(m, n) {
for j in 0..std::cmp::min(m, n) {
let expected = if i == j {
<T as num_traits::One>::one()
} else {
<T as num_traits::Zero>::zero()
};
let actual = improved_product.get(&[i, j])?;
let deviation = num_traits::Float::abs(actual - expected);
if deviation > improved_max_deviation {
improved_max_deviation = deviation;
}
}
}
eprintln!(
"Orthogonality after improvement: Max deviation reduced from {} to {}",
max_deviation, improved_max_deviation
);
}
}
}
// Validate the decomposition by checking A ≈ Q*R
#[cfg(feature = "validation")]
{
let recon = q_array.matmul(&r_array)?;
let mut max_diff = <T as num_traits::Zero>::zero();
for i in 0..m {
for j in 0..n {
let diff = num_traits::Float::abs(a.get(&[i, j])? - recon.get(&[i, j])?);
if diff > max_diff {
max_diff = diff;
}
}
}
let acceptable_error = eps
* max_val
* <T as num_traits::NumCast>::from(std::cmp::max(m, n))
.expect("matrix dimension should convert to float type");
if max_diff > acceptable_error {
eprintln!("Warning: QR decomposition may be numerically unstable. Max reconstruction difference: {}", max_diff);
}
}
Ok((q_array, r_array))
}
/// Fallback QR implementation using Householder reflections
/// This is more numerically stable than classical Gram-Schmidt
pub fn householder_qr<T>(a: &Array<T>) -> Result<(Array<T>, Array<T>)>
where
T: Float
+ Clone
+ Debug
+ std::ops::AddAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ std::ops::SubAssign
+ std::fmt::Display,
{
let shape = a.shape();
let m = shape[0];
let n = shape[1];
let min_dim = std::cmp::min(m, n);
// Create copies of A for the QR calculation
let mut r = a.clone();
let mut q = identity_matrix::<T>(m); // Start with identity matrix
// Householder QR is more numerically stable than Gram-Schmidt
for k in 0..min_dim {
// Extract column k from R
let mut x = Vec::with_capacity(m - k);
for i in k..m {
x.push(r.get(&[i, k])?);
}
// Compute Householder vector v
// Accumulate sum manually to avoid using Sum trait
let mut sum_xx: T = <T as num_traits::Zero>::zero();
for &val in &x {
sum_xx += val * val;
}
let x_norm = num_traits::Float::sqrt(sum_xx);
// Use a small epsilon threshold for numerical stability
let eps = num_traits::Float::epsilon();
if x_norm > eps {
// First element of v determines the sign
let alpha = if x[0] >= num_traits::Zero::zero() {
-x_norm
} else {
x_norm
};
// Compute v = x - alpha*e1
let mut v = x.clone();
v[0] -= alpha;
// Normalize v - accumulate sum manually again
let mut sum_vv: T = <T as num_traits::Zero>::zero();
for &val in &v {
sum_vv += val * val;
}
let v_norm = num_traits::Float::sqrt(sum_vv);
if v_norm > eps {
for val in &mut v {
*val /= v_norm;
}
// Apply Householder reflection to R: R = R - 2 * v * (v^T * R)
for j in k..n {
let mut vtr: T = <T as num_traits::Zero>::zero();
for i in 0..(m - k) {
let r_val = r.get(&[i + k, j])?;
vtr += v[i] * r_val;
}
for i in 0..(m - k) {
let r_val = r.get(&[i + k, j])?;
r.set(
&[i + k, j],
r_val
- <T as num_traits::NumCast>::from(2.0)
.expect("2.0 should convert to float type")
* v[i]
* vtr,
)?;
}
}
// Update Q: Q = Q * (I - 2 * v * v^T)
for i in 0..m {
for j in k..m {
let mut q_row_dot_v: T = <T as num_traits::Zero>::zero();
for l in 0..(m - k) {
let q_val = q.get(&[i, l + k])?;
q_row_dot_v += q_val * v[l];
}
let q_val = q.get(&[i, j])?;
q.set(
&[i, j],
q_val
- <T as num_traits::NumCast>::from(2.0)
.expect("2.0 should convert to float type")
* q_row_dot_v
* v[j - k],
)?;
}
}
}
}
}
// Zero out the lower triangular part of R for precision
for i in 1..m {
for j in 0..std::cmp::min(i, n) {
r.set(&[i, j], num_traits::Zero::zero())?;
}
}
Ok((q, r))
}
/// Create an identity matrix of size n
pub fn identity_matrix<T>(n: usize) -> Array<T>
where
T: Zero + One + Clone,
{
let mut result = Array::zeros(&[n, n]);
for i in 0..n {
result
.set(&[i, i], T::one())
.expect("diagonal index should be valid");
}
result
}