mdarray_linalg_faer/qr/
context.rs

1// QR Decomposition:
2//     A = Q * R
3// where:
4//     - A is m × n         (input matrix)
5//     - Q is m × m        (orthogonal matrix)
6//     - R is m × n         (upper triangular/trapezoidal matrix)
7//     - For thin QR: Q is m × min(m,n) and R is min(m,n) × n
8
9use super::simple::qr_faer;
10use faer_traits::ComplexField;
11use mdarray::{DSlice, DTensor, Layout, tensor};
12use mdarray_linalg::identity;
13use mdarray_linalg::qr::QR;
14use num_complex::ComplexFloat;
15
16use crate::Faer;
17
18impl<T> QR<T> for Faer
19where
20    T: ComplexFloat
21        + ComplexField
22        + Default
23        + std::convert::From<<T as num_complex::ComplexFloat>::Real>
24        + 'static,
25{
26    /// Compute full QR decomposition with new allocated matrices
27    fn qr<L: Layout>(&self, a: &mut DSlice<T, 2, L>) -> (DTensor<T, 2>, DTensor<T, 2>) {
28        let (m, n) = *a.shape();
29        let mut q_mda = identity(m);
30        let mut r_mda = tensor![[T::default(); n]; m];
31
32        qr_faer(a, Some(&mut q_mda), &mut r_mda);
33        (q_mda, r_mda)
34    }
35
36    /// Compute full QR decomposition, overwriting existing matrices
37    fn qr_overwrite<L: Layout, Lq: Layout, Lr: Layout>(
38        &self,
39        a: &mut DSlice<T, 2, L>,
40        q: &mut DSlice<T, 2, Lq>,
41        r: &mut DSlice<T, 2, Lr>,
42    ) {
43        qr_faer::<T, L, Lq, Lr>(a, Some(q), r)
44    }
45}