1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Implementation of ???ev - Compute the eigenvalues and, optionally eigenvectors of a real
//! symmetric or hermitian matrix.
use lapack::{cheev, dsyev, ssyev, zheev};
use crate::dense::linalg::lapack::interface::lapack_return;
use crate::base_types::{LapackError, LapackResult, c32, c64};
use crate::traits::rlst_num::RlstScalar;
use num::{Zero, complex::ComplexFloat};
/// The job options for the eigenvectors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum JobZEv {
/// Do not compute the eigenvectors.
None = b'N',
/// Compute the eigenvectors.
Compute = b'V',
}
/// Use upper or lower triangular part of the matrix.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum EvUplo {
/// Use the upper triangular part of the matrix.
Upper = b'U',
/// Use the lower triangular part of the matrix.
Lower = b'L',
}
/// ???ev - Compute the eigenvalues and, optionally eigenvectors of a real symmetric or hermitian
/// matrix.
pub trait Ev: RlstScalar {
/// Compute the eigenvalues and, optionally eigenvectors of a real symmetric or hermitian
/// matrix.
/// **Arguments:**
/// - `jobz`: Specifies whether to compute the eigenvectors.
/// - `uplo`: Specifies whether to use the upper or lower triangular part of the matrix.
/// - `n`: The order of the matrix A.
/// - `a`: The matrix A to be factored.
/// - `lda`: The leading dimension of the matrix A.
/// - `w`: The matrix to store the eigenvalues.
fn ev(
jobz: JobZEv,
uplo: EvUplo,
n: usize,
a: &mut [Self],
lda: usize,
w: &mut [Self::Real],
) -> LapackResult<()>;
}
macro_rules! implement_ev {
($scalar:ty, $ev:expr) => {
impl Ev for $scalar {
fn ev(
jobz: JobZEv,
uplo: EvUplo,
n: usize,
a: &mut [Self],
lda: usize,
w: &mut [Self::Real],
) -> LapackResult<()> {
assert_eq!(
a.len(),
lda * n,
"Require `a.len()` {} == `lda * n` {}.",
a.len(),
lda * n
);
assert!(
lda >= std::cmp::max(1, n),
"Require `lda` {} >= `max(1, n)` {}.",
lda,
std::cmp::max(1, n)
);
assert_eq!(w.len(), n, "Require `w.len()` {} == `n` {}.", w.len(), n);
let mut info = 0;
let mut work = vec![<$scalar>::zero(); 1];
unsafe {
$ev(
jobz as u8, uplo as u8, n as i32, a, lda as i32, w, &mut work, -1,
&mut info,
);
}
if info != 0 {
return Err(LapackError::LapackInfoCode(info));
}
let lwork = work[0].re() as usize;
let mut work = vec![<$scalar>::zero(); lwork];
unsafe {
$ev(
jobz as u8,
uplo as u8,
n as i32,
a,
lda as i32,
w,
&mut work,
lwork as i32,
&mut info,
);
}
lapack_return(info, ())
}
}
};
}
macro_rules! implement_ev_complex {
($scalar:ty, $ev:expr) => {
impl Ev for $scalar {
fn ev(
jobz: JobZEv,
uplo: EvUplo,
n: usize,
a: &mut [Self],
lda: usize,
w: &mut [Self::Real],
) -> LapackResult<()> {
assert_eq!(
a.len(),
lda * n,
"Require `a.len()` {} == `lda * n` {}.",
a.len(),
lda * n
);
assert!(
lda >= std::cmp::max(1, n),
"Require `lda` {} >= `max(1, n)` {}.",
lda,
std::cmp::max(1, n)
);
assert_eq!(w.len(), n, "Require `w.len()` {} == `n` {}.", w.len(), n);
let mut info = 0;
let mut work = vec![<$scalar>::zero(); 1];
let mut rwork = vec![
<<$scalar as RlstScalar>::Real as Zero>::zero();
std::cmp::max(1, 3 * n - 2)
];
unsafe {
$ev(
jobz as u8, uplo as u8, n as i32, a, lda as i32, w, &mut work, -1,
&mut rwork, &mut info,
);
}
if info != 0 {
return Err(LapackError::LapackInfoCode(info));
}
let lwork = work[0].re() as usize;
let mut work = vec![<$scalar>::zero(); lwork];
unsafe {
$ev(
jobz as u8,
uplo as u8,
n as i32,
a,
lda as i32,
w,
&mut work,
lwork as i32,
&mut rwork,
&mut info,
);
}
lapack_return(info, ())
}
}
};
}
implement_ev!(f32, ssyev);
implement_ev!(f64, dsyev);
implement_ev_complex!(c32, cheev);
implement_ev_complex!(c64, zheev);