Skip to content

Commit e21e932

Browse files
committed
Introduce EigenVectorFlag::then
1 parent 25dc071 commit e21e932

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

lax/src/eig.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,8 @@ macro_rules! impl_eig_complex {
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 == EigenVectorFlag::Calc {
48-
Some(unsafe { vec_uninit((n * n) as usize) })
49-
} else {
50-
None
51-
};
52-
let mut vr = if jobvr == EigenVectorFlag::Calc {
53-
Some(unsafe { vec_uninit((n * n) as usize) })
54-
} else {
55-
None
56-
};
47+
let mut vl = jobvl.then(|| unsafe { vec_uninit((n * n) as usize) });
48+
let mut vr = jobvr.then(|| unsafe { vec_uninit((n * n) as usize) });
5749

5850
// calc work size
5951
let mut info = 0;
@@ -102,7 +94,7 @@ macro_rules! impl_eig_complex {
10294
info.as_lapack_result()?;
10395

10496
// Hermite conjugate
105-
if jobvl == EigenVectorFlag::Calc {
97+
if jobvl.is_calc() {
10698
for c in vl.as_mut().unwrap().iter_mut() {
10799
c.im = -c.im
108100
}
@@ -153,16 +145,10 @@ macro_rules! impl_eig_real {
153145
let mut eig_re: Vec<Self> = unsafe { vec_uninit(n as usize) };
154146
let mut eig_im: Vec<Self> = unsafe { vec_uninit(n as usize) };
155147

156-
let mut vl: Option<Vec<Self>> = if jobvl == EigenVectorFlag::Calc {
157-
Some(unsafe { vec_uninit((n * n) as usize) })
158-
} else {
159-
None
160-
};
161-
let mut vr: Option<Vec<Self>> = if jobvr == EigenVectorFlag::Calc {
162-
Some(unsafe { vec_uninit((n * n) as usize) })
163-
} else {
164-
None
165-
};
148+
let mut vl: Option<Vec<Self>> =
149+
jobvl.then(|| unsafe { vec_uninit((n * n) as usize) });
150+
let mut vr: Option<Vec<Self>> =
151+
jobvr.then(|| unsafe { vec_uninit((n * n) as usize) });
166152

167153
// calc work size
168154
let mut info = 0;
@@ -255,7 +241,7 @@ macro_rules! impl_eig_real {
255241
for row in 0..n {
256242
let re = v[row + col * n];
257243
let mut im = v[row + (col + 1) * n];
258-
if jobvl == EigenVectorFlag::Calc {
244+
if jobvl.is_calc() {
259245
im = -im;
260246
}
261247
eigvecs[row + col * n] = Self::complex(re, im);

lax/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ pub enum EigenVectorFlag {
220220
}
221221

222222
impl EigenVectorFlag {
223+
pub fn is_calc(&self) -> bool {
224+
match self {
225+
EigenVectorFlag::Calc => true,
226+
EigenVectorFlag::Not => false,
227+
}
228+
}
229+
230+
pub fn then<T, F: FnOnce() -> T>(&self, f: F) -> Option<T> {
231+
if self.is_calc() {
232+
Some(f())
233+
} else {
234+
None
235+
}
236+
}
237+
223238
/// To use Fortran LAPACK API in lapack-sys crate
224239
pub fn as_ptr(&self) -> *const i8 {
225240
self as *const EigenVectorFlag as *const i8

0 commit comments

Comments
 (0)