From bfcebe190191eaf6a6abfa240b775f510f67b853 Mon Sep 17 00:00:00 2001 From: axect Date: Thu, 18 Apr 2024 09:53:39 +0900 Subject: [PATCH] ADD: Add complex feature & complex module (#35) --- Cargo.toml | 2 ++ src/{structure/complex.rs => complex/mod.rs} | 14 +++++++------- src/lib.rs | 3 +++ 3 files changed, 12 insertions(+), 7 deletions(-) rename src/{structure/complex.rs => complex/mod.rs} (87%) diff --git a/Cargo.toml b/Cargo.toml index a1e0ef1e..94bd5585 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ lapack = { version = "0.19", optional = true } serde = { version = "1.0", features = ["derive"], optional = true } json = { version = "0.12", optional = true } arrow2 = { version = "0.18", features = ["io_parquet", "io_parquet_compression"], optional = true } +num-complex = { version = "0.4", optional = true } [package.metadata.docs.rs] rustdoc-args = [ "--html-in-header", "katex-header.html", "--cfg", "docsrs"] @@ -48,3 +49,4 @@ O3 = ["blas", "lapack"] plot = ["pyo3"] nc = ["netcdf"] parquet = ["arrow2"] +complex = ["num-complex", "matrixmultiply/cgemm"] diff --git a/src/structure/complex.rs b/src/complex/mod.rs similarity index 87% rename from src/structure/complex.rs rename to src/complex/mod.rs index 8fad51b2..b0f0654b 100644 --- a/src/structure/complex.rs +++ b/src/complex/mod.rs @@ -8,11 +8,11 @@ pub type C64 = Complex; impl Vector for Complex { type Scalar = Self; - fn add_vec<'a, 'b>(&'a self, rhs: &'b Self) -> Self { + fn add_vec(&self, rhs: &Self) -> Self { self + rhs } - fn sub_vec<'a, 'b>(&'a self, rhs: &'b Self) -> Self { + fn sub_vec(&self, rhs: &Self) -> Self { self - rhs } @@ -70,15 +70,15 @@ impl FPVector for Vec> { fn filter(&self, f: F) -> Self where F: Fn(Self::Scalar) -> bool { - self.into_iter().filter(|&x| f(*x)).map(|&t| t).collect() + self.iter().filter(|&x| f(*x)).cloned().collect() } fn take(&self, n: usize) -> Self { - self.iter().take(n).map(|&x| x).collect() + self.iter().take(n).cloned().collect() } fn skip(&self, n: usize) -> Self { - self.iter().skip(n).map(|&x| x).collect() + self.iter().skip(n).cloned().collect() } fn sum(&self) -> Self::Scalar { @@ -93,11 +93,11 @@ impl FPVector for Vec> { impl Vector for Vec> { type Scalar = Complex; - fn add_vec<'a, 'b>(&'a self, rhs: &'b Self) -> Self { + fn add_vec(&self, rhs: &Self) -> Self { self.zip_with(|x, y| x + y, rhs) } - fn sub_vec<'a, 'b>(&'a self, rhs: &'b Self) -> Self { + fn sub_vec(&self, rhs: &Self) -> Self { self.zip_with(|x, y| x - y, rhs) } diff --git a/src/lib.rs b/src/lib.rs index 6e312e70..e1929555 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -205,3 +205,6 @@ pub mod statistics; pub mod structure; pub mod traits; pub mod util; + +#[cfg(feature = "complex")] +pub mod complex;