Skip to main content

lib_jsl_linalg/
scalar.rs

1//! The scalar types the decompositions work over: `f64` and `Complex<f64>`.
2
3use num_complex::Complex;
4use std::fmt::Debug;
5use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
6
7/// A real or complex double-precision scalar.
8pub trait Scalar:
9    Copy
10    + Debug
11    + PartialEq
12    + Send
13    + Sync
14    + 'static
15    + Add<Output = Self>
16    + Sub<Output = Self>
17    + Mul<Output = Self>
18    + Div<Output = Self>
19    + Neg<Output = Self>
20    + AddAssign
21    + SubAssign
22    + MulAssign
23    + DivAssign
24{
25    /// `true` for complex scalars.
26    const IS_COMPLEX: bool;
27    fn zero() -> Self;
28    fn one() -> Self;
29    fn from_real(re: f64) -> Self;
30    /// The complex conjugate (the identity for reals).
31    fn conj(self) -> Self;
32    /// The real part.
33    fn re(self) -> f64;
34    /// The imaginary part (zero for reals).
35    fn im(self) -> f64;
36    /// The modulus `|z|`.
37    fn abs(self) -> f64;
38    /// `|z|²`.
39    fn abs_sqr(self) -> f64;
40    /// `z · r` for a real `r`.
41    fn scale(self, r: f64) -> Self;
42    fn is_finite(self) -> bool;
43    /// As a complex number.
44    fn to_complex(self) -> Complex<f64>;
45    /// From a complex number (the real part, for reals).
46    fn from_complex(z: Complex<f64>) -> Self;
47
48    /// `C ← C − A B` for an `m × k` matrix `A`, a `k × n` matrix `B` and an `m × n` matrix `C`,
49    /// each given by a pointer to its first element and its row and column strides.
50    ///
51    /// # Safety
52    /// The pointers and strides must describe valid matrices, and `C` must not overlap `A` or `B`.
53    #[allow(clippy::too_many_arguments)]
54    unsafe fn gemm_sub(
55        m: usize,
56        k: usize,
57        n: usize,
58        a: *const Self,
59        rsa: isize,
60        csa: isize,
61        b: *const Self,
62        rsb: isize,
63        csb: isize,
64        c: *mut Self,
65        rsc: isize,
66        csc: isize,
67    );
68}
69
70impl Scalar for f64 {
71    const IS_COMPLEX: bool = false;
72    fn zero() -> Self {
73        0.0
74    }
75    fn one() -> Self {
76        1.0
77    }
78    fn from_real(re: f64) -> Self {
79        re
80    }
81    fn conj(self) -> Self {
82        self
83    }
84    fn re(self) -> f64 {
85        self
86    }
87    fn im(self) -> f64 {
88        0.0
89    }
90    fn abs(self) -> f64 {
91        f64::abs(self)
92    }
93    fn abs_sqr(self) -> f64 {
94        self * self
95    }
96    fn scale(self, r: f64) -> Self {
97        self * r
98    }
99    fn is_finite(self) -> bool {
100        f64::is_finite(self)
101    }
102    fn to_complex(self) -> Complex<f64> {
103        Complex::new(self, 0.0)
104    }
105    fn from_complex(z: Complex<f64>) -> Self {
106        z.re
107    }
108    unsafe fn gemm_sub(
109        m: usize,
110        k: usize,
111        n: usize,
112        a: *const Self,
113        rsa: isize,
114        csa: isize,
115        b: *const Self,
116        rsb: isize,
117        csb: isize,
118        c: *mut Self,
119        rsc: isize,
120        csc: isize,
121    ) {
122        unsafe { matrixmultiply::dgemm(m, k, n, -1.0, a, rsa, csa, b, rsb, csb, 1.0, c, rsc, csc) }
123    }
124}
125
126impl Scalar for Complex<f64> {
127    const IS_COMPLEX: bool = true;
128    fn zero() -> Self {
129        Complex::new(0.0, 0.0)
130    }
131    fn one() -> Self {
132        Complex::new(1.0, 0.0)
133    }
134    fn from_real(re: f64) -> Self {
135        Complex::new(re, 0.0)
136    }
137    fn conj(self) -> Self {
138        Complex::conj(&self)
139    }
140    fn re(self) -> f64 {
141        self.re
142    }
143    fn im(self) -> f64 {
144        self.im
145    }
146    fn abs(self) -> f64 {
147        self.norm()
148    }
149    fn abs_sqr(self) -> f64 {
150        self.norm_sqr()
151    }
152    fn scale(self, r: f64) -> Self {
153        self * r
154    }
155    fn is_finite(self) -> bool {
156        self.re.is_finite() && self.im.is_finite()
157    }
158    fn to_complex(self) -> Complex<f64> {
159        self
160    }
161    fn from_complex(z: Complex<f64>) -> Self {
162        z
163    }
164    unsafe fn gemm_sub(
165        m: usize,
166        k: usize,
167        n: usize,
168        a: *const Self,
169        rsa: isize,
170        csa: isize,
171        b: *const Self,
172        rsb: isize,
173        csb: isize,
174        c: *mut Self,
175        rsc: isize,
176        csc: isize,
177    ) {
178        use matrixmultiply::CGemmOption::Standard;
179        // `Complex<f64>` is `#[repr(C)]` with fields (re, im), the layout of matrixmultiply's `c64`.
180        unsafe {
181            matrixmultiply::zgemm(
182                Standard,
183                Standard,
184                m,
185                k,
186                n,
187                [-1.0, 0.0],
188                a.cast(),
189                rsa,
190                csa,
191                b.cast(),
192                rsb,
193                csb,
194                [1.0, 0.0],
195                c.cast(),
196                rsc,
197                csc,
198            )
199        }
200    }
201}