mdarray_linalg_lapack/lib.rs
1//! ```rust
2//! use mdarray::{DTensor, tensor};
3//! use mdarray_linalg::prelude::*; // Imports only traits
4//! use mdarray_linalg::eig::EigDecomp;
5//! use mdarray_linalg::svd::SVDDecomp;
6//!
7//! // Backends
8//! use mdarray_linalg_lapack::Lapack;
9//! use mdarray_linalg_lapack::SVDConfig;
10//!
11//! let a = tensor![[1., 2.], [3., 4.]];
12//!
13//! // ----- Eigenvalue decomposition -----
14//! // Note: we must clone `a` here because decomposition routines destroy the input.
15//! let bd = Lapack::new(); // Unlike Blas, Lapack is not a zero-sized backend so `new` must be called.
16//! let EigDecomp {
17//! eigenvalues,
18//! right_eigenvectors,
19//! ..
20//! } = bd.eig(&mut a.clone()).expect("Eigenvalue decomposition failed");
21//!
22//! println!("Eigenvalues: {:?}", eigenvalues);
23//! if let Some(vectors) = right_eigenvectors {
24//! println!("Right eigenvectors: {:?}", vectors);
25//! }
26//!
27//! // ----- Singular Value Decomposition (SVD) -----
28//! let bd = Lapack::new().config_svd(SVDConfig::DivideConquer);
29//! let SVDDecomp { s, u, vt } = bd.svd(&mut a.clone()).expect("SVD failed");
30//! println!("Singular values: {:?}", s);
31//! println!("Left singular vectors U: {:?}", u);
32//! println!("Right singular vectors V^T: {:?}", vt);
33//!
34//! // ----- QR Decomposition -----
35//! let (m, n) = *a.shape();
36//! let mut q = DTensor::<f64, 2>::zeros([m, m]);
37//! let mut r = DTensor::<f64, 2>::zeros([m, n]);
38//!
39//! let bd = Lapack::new();
40//! bd.qr_overwrite(&mut a.clone(), &mut q, &mut r); //
41//! println!("Q: {:?}", q);
42//! println!("R: {:?}", r);
43//! ```
44
45pub mod eig;
46pub mod lu;
47pub mod qr;
48pub mod solve;
49pub mod svd;
50
51#[derive(Default, Debug, Clone, Copy, PartialEq)]
52pub enum SVDConfig {
53 #[default]
54 Auto,
55 DivideConquer,
56 Jacobi,
57}
58
59#[derive(Debug, Default, Clone)]
60pub struct Lapack {
61 svd_config: SVDConfig,
62 _qr_config: LapackQRConfig,
63}
64
65#[derive(Default, Debug, Clone)]
66pub enum LapackQRConfig {
67 #[default]
68 Full,
69 Pivoting,
70 TallSkinny,
71}
72
73impl Lapack {
74 pub fn new() -> Self {
75 Self {
76 svd_config: SVDConfig::default(),
77 _qr_config: LapackQRConfig::default(),
78 }
79 }
80
81 pub fn config_svd(mut self, config: SVDConfig) -> Self {
82 self.svd_config = config;
83 self
84 }
85
86 pub fn config_qr(self, _config: LapackQRConfig) -> Self {
87 todo!()
88 }
89}