mdarray_linalg_lapack/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 matrix)
7//! This decomposition is used to solve linear equations, least squares problems, and eigenvalue problems.
8//! The function `geqrf` (LAPACK) computes the QR factorization of a general m-by-n matrix A using a blocking algorithm.
9//! The matrix Q is orthogonal, and R is upper triangular.
10
11use super::scalar::NeedsRwork;
12use super::simple::geqrf;
13use mdarray_linalg::get_dims;
14
15use super::scalar::LapackScalar;
16use mdarray::{DSlice, DTensor, Layout, tensor};
17use mdarray_linalg::into_i32;
18use mdarray_linalg::qr::QR;
19use num_complex::ComplexFloat;
20
21use crate::Lapack;
22
23impl<T> QR<T> for Lapack
24where
25    T: ComplexFloat + Default + LapackScalar + NeedsRwork,
26    T::Real: Into<T>,
27{
28    fn qr_overwrite<L: Layout, Lq: Layout, Lr: Layout>(
29        &self,
30        a: &mut DSlice<T, 2, L>,
31        q: &mut DSlice<T, 2, Lq>,
32        r: &mut DSlice<T, 2, Lr>,
33    ) {
34        geqrf(a, q, r)
35    }
36
37    fn qr<L: Layout>(&self, a: &mut DSlice<T, 2, L>) -> (DTensor<T, 2>, DTensor<T, 2>) {
38        let (m, n) = get_dims!(a);
39        let mut q = tensor![[T::default(); m as usize]; m as usize];
40        let mut r = tensor![[T::default(); n as usize]; m as usize];
41
42        geqrf(a, &mut q, &mut r);
43
44        (q, r)
45    }
46}