Skip to content

Commit c0c3a37

Browse files
committed
Introduce EigenVectorFlag
1 parent b964625 commit c0c3a37

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

lax/src/eig.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ macro_rules! impl_eig_complex {
3535
// eigenvalues are the eigenvalues computed with `A`.
3636
let (jobvl, jobvr) = if calc_v {
3737
match l {
38-
MatrixLayout::C { .. } => (b'V', b'N'),
39-
MatrixLayout::F { .. } => (b'N', b'V'),
38+
MatrixLayout::C { .. } => (EigenVectorFlag::Calc, EigenVectorFlag::Not),
39+
MatrixLayout::F { .. } => (EigenVectorFlag::Not, EigenVectorFlag::Calc),
4040
}
4141
} else {
42-
(b'N', b'N')
42+
(EigenVectorFlag::Not, EigenVectorFlag::Not)
4343
};
4444
let mut eigs = unsafe { vec_uninit(n as usize) };
4545
let mut rwork = unsafe { vec_uninit(2 * n as usize) };
4646

47-
let mut vl = if jobvl == b'V' {
47+
let mut vl = if jobvl == EigenVectorFlag::Calc {
4848
Some(unsafe { vec_uninit((n * n) as usize) })
4949
} else {
5050
None
5151
};
52-
let mut vr = if jobvr == b'V' {
52+
let mut vr = if jobvr == EigenVectorFlag::Calc {
5353
Some(unsafe { vec_uninit((n * n) as usize) })
5454
} else {
5555
None
@@ -60,8 +60,8 @@ macro_rules! impl_eig_complex {
6060
let mut work_size = [Self::zero()];
6161
unsafe {
6262
$ev(
63-
jobvl,
64-
jobvr,
63+
jobvl as u8,
64+
jobvr as u8,
6565
n,
6666
&mut a,
6767
n,
@@ -83,8 +83,8 @@ macro_rules! impl_eig_complex {
8383
let mut work = unsafe { vec_uninit(lwork) };
8484
unsafe {
8585
$ev(
86-
jobvl,
87-
jobvr,
86+
jobvl as u8,
87+
jobvr as u8,
8888
n,
8989
&mut a,
9090
n,
@@ -102,7 +102,7 @@ macro_rules! impl_eig_complex {
102102
info.as_lapack_result()?;
103103

104104
// Hermite conjugate
105-
if jobvl == b'V' {
105+
if jobvl == EigenVectorFlag::Calc {
106106
for c in vl.as_mut().unwrap().iter_mut() {
107107
c.im = -c.im
108108
}
@@ -144,21 +144,21 @@ macro_rules! impl_eig_real {
144144
// `sgeev`/`dgeev`.
145145
let (jobvl, jobvr) = if calc_v {
146146
match l {
147-
MatrixLayout::C { .. } => (b'V', b'N'),
148-
MatrixLayout::F { .. } => (b'N', b'V'),
147+
MatrixLayout::C { .. } => (EigenVectorFlag::Calc, EigenVectorFlag::Not),
148+
MatrixLayout::F { .. } => (EigenVectorFlag::Not, EigenVectorFlag::Calc),
149149
}
150150
} else {
151-
(b'N', b'N')
151+
(EigenVectorFlag::Not, EigenVectorFlag::Not)
152152
};
153153
let mut eig_re = unsafe { vec_uninit(n as usize) };
154154
let mut eig_im = unsafe { vec_uninit(n as usize) };
155155

156-
let mut vl = if jobvl == b'V' {
156+
let mut vl = if jobvl == EigenVectorFlag::Calc {
157157
Some(unsafe { vec_uninit((n * n) as usize) })
158158
} else {
159159
None
160160
};
161-
let mut vr = if jobvr == b'V' {
161+
let mut vr = if jobvr == EigenVectorFlag::Calc {
162162
Some(unsafe { vec_uninit((n * n) as usize) })
163163
} else {
164164
None
@@ -169,8 +169,8 @@ macro_rules! impl_eig_real {
169169
let mut work_size = [0.0];
170170
unsafe {
171171
$ev(
172-
jobvl,
173-
jobvr,
172+
jobvl as u8,
173+
jobvr as u8,
174174
n,
175175
&mut a,
176176
n,
@@ -192,8 +192,8 @@ macro_rules! impl_eig_real {
192192
let mut work = unsafe { vec_uninit(lwork) };
193193
unsafe {
194194
$ev(
195-
jobvl,
196-
jobvr,
195+
jobvl as u8,
196+
jobvr as u8,
197197
n,
198198
&mut a,
199199
n,
@@ -254,7 +254,7 @@ macro_rules! impl_eig_real {
254254
for row in 0..n {
255255
let re = v[row + col * n];
256256
let mut im = v[row + (col + 1) * n];
257-
if jobvl == b'V' {
257+
if jobvl == EigenVectorFlag::Calc {
258258
im = -im;
259259
}
260260
eigvecs[row + col * n] = Self::complex(re, im);

lax/src/eigh.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ macro_rules! impl_eigh {
4141
) -> Result<Vec<Self::Real>> {
4242
assert_eq!(layout.len(), layout.lda());
4343
let n = layout.len();
44-
let jobz = if calc_v { b'V' } else { b'N' };
44+
let jobz = if calc_v { EigenVectorFlag::Calc } else { EigenVectorFlag::Not };
4545
let mut eigs = unsafe { vec_uninit(n as usize) };
4646

4747
$(
@@ -53,7 +53,7 @@ macro_rules! impl_eigh {
5353
let mut work_size = [Self::zero()];
5454
unsafe {
5555
$ev(
56-
jobz,
56+
jobz as u8,
5757
uplo as u8,
5858
n,
5959
&mut a,
@@ -72,7 +72,7 @@ macro_rules! impl_eigh {
7272
let mut work = unsafe { vec_uninit(lwork) };
7373
unsafe {
7474
$ev(
75-
jobz,
75+
jobz as u8,
7676
uplo as u8,
7777
n,
7878
&mut a,
@@ -97,7 +97,7 @@ macro_rules! impl_eigh {
9797
) -> Result<Vec<Self::Real>> {
9898
assert_eq!(layout.len(), layout.lda());
9999
let n = layout.len();
100-
let jobz = if calc_v { b'V' } else { b'N' };
100+
let jobz = if calc_v { EigenVectorFlag::Calc } else { EigenVectorFlag::Not };
101101
let mut eigs = unsafe { vec_uninit(n as usize) };
102102

103103
$(
@@ -110,7 +110,7 @@ macro_rules! impl_eigh {
110110
unsafe {
111111
$evg(
112112
&[1],
113-
jobz,
113+
jobz as u8,
114114
uplo as u8,
115115
n,
116116
&mut a,
@@ -132,7 +132,7 @@ macro_rules! impl_eigh {
132132
unsafe {
133133
$evg(
134134
&[1],
135-
jobz,
135+
jobz as u8,
136136
uplo as u8,
137137
n,
138138
&mut a,

lax/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ impl NormType {
211211
}
212212
}
213213

214+
/// Flag for calculating eigenvectors or not
215+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
216+
#[repr(u8)]
217+
pub enum EigenVectorFlag {
218+
Calc = b'V',
219+
Not = b'N',
220+
}
221+
222+
impl EigenVectorFlag {
223+
/// To use Fortran LAPACK API in lapack-sys crate
224+
pub fn as_ptr(&self) -> *const i8 {
225+
self as *const EigenVectorFlag as *const i8
226+
}
227+
}
228+
214229
/// Create a vector without initialization
215230
///
216231
/// Safety

0 commit comments

Comments
 (0)