1use arpack_ng_sys::*;
2use lazy_static::lazy_static;
3use std::{fmt, sync::Mutex};
4
5#[cfg(feature = "ndarray")]
6mod ndarray;
7#[cfg(feature = "ndarray")]
8pub use crate::ndarray::*;
9
10#[cfg(feature = "nalgebra")]
11mod nalgebra;
12#[cfg(feature = "nalgebra")]
13pub use crate::nalgebra::*;
14
15lazy_static! {
16 static ref MUTEX: Mutex<()> = Mutex::new(());
17}
18
19#[derive(Debug)]
20pub enum Error {
21 NonSquare,
22 IllegalParameters(String),
23 Other(i32),
24}
25
26impl fmt::Display for Error {
27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28 match self {
29 Error::NonSquare => f.write_str("Non square matrix."),
30 Error::IllegalParameters(s) => write!(f, "Invalid parameters: {}", s),
31 Error::Other(i) => write!(f, "Arpack error (code {}", i),
32 }
33 }
34}
35
36#[derive(Debug, Clone)]
37pub enum Which {
38 LargestMagnitude,
39 SmallestMagnitude,
40 LargestRealPart,
41 SmallestRealPart,
42 LargestImaginaryPart,
43 SmallestImaginaryPart,
44}
45
46impl Which {
47 fn as_str(&self) -> &'static str {
48 match self {
49 Which::LargestMagnitude => "LM",
50 Which::SmallestMagnitude => "SM",
51 Which::LargestRealPart => "LR",
52 Which::SmallestRealPart => "SR",
53 Which::LargestImaginaryPart => "LI",
54 Which::SmallestImaginaryPart => "SI",
55 }
56 }
57}
58
59impl std::error::Error for Error {}
60
61pub trait Arpack {
62 type Result;
63 type ResultVec;
64
65 fn eigenvalues(
66 &self,
67 which: &Which,
68 nev: usize,
69 ncv: usize,
70 maxiter: usize,
71 ) -> Result<Self::Result, Error>;
72 fn eigenvectors(
73 &self,
74 which: &Which,
75 nev: usize,
76 ncv: usize,
77 maxiter: usize,
78 ) -> Result<Self::ResultVec, Error>;
79}
80
81const ZERO: __BindgenComplex<f64> = __BindgenComplex { re: 0., im: 0. };