lax/
flags.rs

1//! Charactor flags, e.g. `'T'`, used in LAPACK API
2use core::ffi::c_char;
3
4/// Upper/Lower specification for seveal usages
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[repr(u8)]
7pub enum UPLO {
8    Upper = b'U',
9    Lower = b'L',
10}
11
12impl UPLO {
13    pub fn t(self) -> Self {
14        match self {
15            UPLO::Upper => UPLO::Lower,
16            UPLO::Lower => UPLO::Upper,
17        }
18    }
19
20    /// To use Fortran LAPACK API in lapack-sys crate
21    pub fn as_ptr(&self) -> *const c_char {
22        self as *const UPLO as *const c_char
23    }
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
27#[repr(u8)]
28pub enum Transpose {
29    No = b'N',
30    Transpose = b'T',
31    Hermite = b'C',
32}
33
34impl Transpose {
35    /// To use Fortran LAPACK API in lapack-sys crate
36    pub fn as_ptr(&self) -> *const c_char {
37        self as *const Transpose as *const c_char
38    }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
42#[repr(u8)]
43pub enum NormType {
44    One = b'O',
45    Infinity = b'I',
46    Frobenius = b'F',
47}
48
49impl NormType {
50    pub fn transpose(self) -> Self {
51        match self {
52            NormType::One => NormType::Infinity,
53            NormType::Infinity => NormType::One,
54            NormType::Frobenius => NormType::Frobenius,
55        }
56    }
57
58    /// To use Fortran LAPACK API in lapack-sys crate
59    pub fn as_ptr(&self) -> *const c_char {
60        self as *const NormType as *const c_char
61    }
62}
63
64/// Flag for calculating eigenvectors or not
65#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
66#[repr(u8)]
67pub enum JobEv {
68    /// Calculate eigenvectors in addition to eigenvalues
69    All = b'V',
70    /// Do not calculate eigenvectors. Only calculate eigenvalues.
71    None = b'N',
72}
73
74impl JobEv {
75    pub fn is_calc(&self) -> bool {
76        match self {
77            JobEv::All => true,
78            JobEv::None => false,
79        }
80    }
81
82    pub fn then<T, F: FnOnce() -> T>(&self, f: F) -> Option<T> {
83        if self.is_calc() {
84            Some(f())
85        } else {
86            None
87        }
88    }
89
90    /// To use Fortran LAPACK API in lapack-sys crate
91    pub fn as_ptr(&self) -> *const c_char {
92        self as *const JobEv as *const c_char
93    }
94}
95
96/// Specifies how many singular vectors are computed
97///
98/// For an input matrix $A$ of shape $m \times n$,
99/// the following are computed on the singular value decomposition $A = U\Sigma V^T$:
100#[cfg_attr(doc, katexit::katexit)]
101#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
102#[repr(u8)]
103pub enum JobSvd {
104    /// All $m$ columns of $U$, and/or all $n$ rows of $V^T$.
105    All = b'A',
106    /// The first $\min(m, n)$ columns of $U$ and/or the first $\min(m, n)$ rows of $V^T$.
107    Some = b'S',
108    /// No columns of $U$ and/or rows of $V^T$.
109    None = b'N',
110}
111
112impl JobSvd {
113    pub fn from_bool(calc_uv: bool) -> Self {
114        if calc_uv {
115            JobSvd::All
116        } else {
117            JobSvd::None
118        }
119    }
120
121    pub fn as_ptr(&self) -> *const c_char {
122        self as *const JobSvd as *const c_char
123    }
124}
125
126/// Specify whether input triangular matrix is unit or not
127#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
128#[repr(u8)]
129pub enum Diag {
130    /// Unit triangular matrix, i.e. all diagonal elements of the matrix are `1`
131    Unit = b'U',
132    /// Non-unit triangular matrix. Its diagonal elements may be different from `1`
133    NonUnit = b'N',
134}
135
136impl Diag {
137    pub fn as_ptr(&self) -> *const c_char {
138        self as *const Diag as *const c_char
139    }
140}