|
| 1 | +use ndarray::*; |
| 2 | +use ndarray_linalg::{krylov::*, *}; |
| 3 | + |
| 4 | +fn over<A: Scalar + Lapack>(rtol: A::Real) { |
| 5 | + const N: usize = 4; |
| 6 | + let a: Array2<A> = random((N, N * 2)); |
| 7 | + |
| 8 | + // Terminate |
| 9 | + let (q, r) = householder(a.axis_iter(Axis(1)), N, rtol, Strategy::Terminate); |
| 10 | + let a_sub = a.slice(s![.., 0..N]); |
| 11 | + let qc: Array2<A> = conjugate(&q); |
| 12 | + assert_close_l2!(&qc.dot(&q), &Array::eye(N), rtol; "Check Q^H Q = I"); |
| 13 | + assert_close_l2!(&q.dot(&r), &a_sub, rtol; "Check A = QR"); |
| 14 | + |
| 15 | + // Skip |
| 16 | + let (q, r) = householder(a.axis_iter(Axis(1)), N, rtol, Strategy::Skip); |
| 17 | + let a_sub = a.slice(s![.., 0..N]); |
| 18 | + let qc: Array2<A> = conjugate(&q); |
| 19 | + assert_close_l2!(&qc.dot(&q), &Array::eye(N), rtol); |
| 20 | + assert_close_l2!(&q.dot(&r), &a_sub, rtol); |
| 21 | + |
| 22 | + // Full |
| 23 | + let (q, r) = householder(a.axis_iter(Axis(1)), N, rtol, Strategy::Full); |
| 24 | + let qc: Array2<A> = conjugate(&q); |
| 25 | + assert_close_l2!(&qc.dot(&q), &Array::eye(N), rtol); |
| 26 | + assert_close_l2!(&q.dot(&r), &a, rtol); |
| 27 | +} |
| 28 | + |
| 29 | +#[test] |
| 30 | +fn over_f32() { |
| 31 | + over::<f32>(1e-5); |
| 32 | +} |
| 33 | +#[test] |
| 34 | +fn over_f64() { |
| 35 | + over::<f64>(1e-9); |
| 36 | +} |
| 37 | +#[test] |
| 38 | +fn over_c32() { |
| 39 | + over::<c32>(1e-5); |
| 40 | +} |
| 41 | +#[test] |
| 42 | +fn over_c64() { |
| 43 | + over::<c64>(1e-9); |
| 44 | +} |
| 45 | + |
| 46 | +fn full<A: Scalar + Lapack>(rtol: A::Real) { |
| 47 | + const N: usize = 5; |
| 48 | + let a: Array2<A> = random((N, N)); |
| 49 | + let (q, r) = householder(a.axis_iter(Axis(1)), N, rtol, Strategy::Terminate); |
| 50 | + let qc: Array2<A> = conjugate(&q); |
| 51 | + assert_close_l2!(&qc.dot(&q), &Array::eye(N), rtol; "Check Q^H Q = I"); |
| 52 | + assert_close_l2!(&q.dot(&r), &a, rtol; "Check A = QR"); |
| 53 | +} |
| 54 | + |
| 55 | +#[test] |
| 56 | +fn full_f32() { |
| 57 | + full::<f32>(1e-5); |
| 58 | +} |
| 59 | +#[test] |
| 60 | +fn full_f64() { |
| 61 | + full::<f64>(1e-9); |
| 62 | +} |
| 63 | +#[test] |
| 64 | +fn full_c32() { |
| 65 | + full::<c32>(1e-5); |
| 66 | +} |
| 67 | +#[test] |
| 68 | +fn full_c64() { |
| 69 | + full::<c64>(1e-9); |
| 70 | +} |
| 71 | + |
| 72 | +fn half<A: Scalar + Lapack>(rtol: A::Real) { |
| 73 | + const N: usize = 4; |
| 74 | + let a: Array2<A> = random((N, N / 2)); |
| 75 | + let (q, r) = householder(a.axis_iter(Axis(1)), N, rtol, Strategy::Terminate); |
| 76 | + let qc: Array2<A> = conjugate(&q); |
| 77 | + assert_close_l2!(&qc.dot(&q), &Array::eye(N / 2), rtol; "Check Q^H Q = I"); |
| 78 | + assert_close_l2!(&q.dot(&r), &a, rtol; "Check A = QR"); |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn half_f32() { |
| 83 | + half::<f32>(1e-5); |
| 84 | +} |
| 85 | +#[test] |
| 86 | +fn half_f64() { |
| 87 | + half::<f64>(1e-9); |
| 88 | +} |
| 89 | +#[test] |
| 90 | +fn half_c32() { |
| 91 | + half::<c32>(1e-5); |
| 92 | +} |
| 93 | +#[test] |
| 94 | +fn half_c64() { |
| 95 | + half::<c64>(1e-9); |
| 96 | +} |
0 commit comments