diff options
Diffstat (limited to 'libpsi-core/src')
| -rw-r--r-- | libpsi-core/src/lib.rs | 2 | ||||
| -rw-r--r-- | libpsi-core/src/maths/mod.rs | 6 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric.rs | 91 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric_float.rs | 65 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric_int.rs | 44 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric_traits.rs | 24 |
6 files changed, 94 insertions, 138 deletions
diff --git a/libpsi-core/src/lib.rs b/libpsi-core/src/lib.rs index 0be80cc..91b853e 100644 --- a/libpsi-core/src/lib.rs +++ b/libpsi-core/src/lib.rs @@ -4,7 +4,7 @@ pub mod runtimes; pub use maths::complex::*; pub use maths::matrix::*; -pub use maths::numeric_traits::*; +pub use maths::numeric::*; pub use maths::vector::*; pub use core::circuit::*; diff --git a/libpsi-core/src/maths/mod.rs b/libpsi-core/src/maths/mod.rs index a2b9b84..32c8f9c 100644 --- a/libpsi-core/src/maths/mod.rs +++ b/libpsi-core/src/maths/mod.rs @@ -4,13 +4,11 @@ pub mod complex_ops; pub mod matrix; pub mod matrix_ops; -pub mod numeric_float; -pub mod numeric_int; -pub mod numeric_traits; +pub mod numeric; pub mod vector; pub mod vector_ops; pub use complex::*; pub use matrix::*; -pub use numeric_traits::*; +pub use numeric::*; diff --git a/libpsi-core/src/maths/numeric.rs b/libpsi-core/src/maths/numeric.rs new file mode 100644 index 0000000..e6f3247 --- /dev/null +++ b/libpsi-core/src/maths/numeric.rs @@ -0,0 +1,91 @@ +use crate::Complex; +use core::ops; + +macro_rules! impl_numeric { + ($($t:ty),*) => { + $( + impl Numeric for $t { + fn zero() -> Self { + 0 as $t + } + + fn one() -> Self { + 1 as $t + } + } + )* + }; +} + +macro_rules! impl_float { + ($($t:ty, $sqrt_fn:path, $atan2_fn:path),*) => { + $( + impl Float for $t { + fn sqrt(self) -> Self { + $sqrt_fn(self) + } + + fn atan2(y: Self, x: Self) -> Self { + $atan2_fn(y, x) + } + } + )* + }; +} + +macro_rules! impl_complex { + ($($t:ty, $sqrt_fn:path, $atan2_fn:path, $cos_fn:path, $sin_fn:path),*) => { + $( + impl Float for Complex<$t> { + fn sqrt(self) -> Self { + let r = self.abs(); + let theta = self.phase(); + + let sqrt_r = $sqrt_fn(r); + let sqrt_theta = theta / 2.0; + + Complex::new( + sqrt_r * $cos_fn(sqrt_theta), + sqrt_r * $sin_fn(sqrt_theta), + ) + } + + fn atan2(y: Self, x: Self) -> Self { + Complex::new( + $atan2_fn(y.real, x.real), + $atan2_fn(y.imaginary, x.imaginary), + ) + } + } + )* + }; +} + +pub trait Numeric: + Copy + + PartialOrd + + ops::Add<Output = Self> + + ops::Mul<Output = Self> + + ops::Sub<Output = Self> + + ops::Div<Output = Self> + + ops::Neg<Output = Self> + + ops::AddAssign + + ops::SubAssign + + ops::MulAssign + + ops::DivAssign +{ + fn zero() -> Self; + fn one() -> Self; +} + +impl_numeric!(i32, i64, f32, f64); +impl_float!(f32, libm::sqrtf, libm::atan2f); +impl_float!(f64, libm::sqrt, libm::atan2); +impl_complex!(f32, libm::sqrtf, libm::atan2f, libm::cosf, libm::sinf); +impl_complex!(f64, libm::sqrt, libm::atan2, libm::cos, libm::sin); + +pub trait Integer: Numeric {} +pub trait Float: Numeric { + fn sqrt(self) -> Self; + fn atan2(y: Self, x: Self) -> Self; +} diff --git a/libpsi-core/src/maths/numeric_float.rs b/libpsi-core/src/maths/numeric_float.rs deleted file mode 100644 index 3591f24..0000000 --- a/libpsi-core/src/maths/numeric_float.rs +++ /dev/null @@ -1,65 +0,0 @@ -use super::{Complex, Float}; - -impl Float for f32 { - fn sqrt(self) -> Self { - libm::sqrtf(self) - } - - fn atan2(y: Self, x: Self) -> Self { - libm::atan2f(y, x) - } -} - -impl Float for f64 { - fn sqrt(self) -> Self { - libm::sqrt(self) - } - - fn atan2(y: Self, x: Self) -> Self { - libm::atan2(y, x) - } -} - -impl Float for Complex<f32> { - fn sqrt(self) -> Self { - let r = self.abs(); - let theta = self.phase(); - - let sqrt_r = libm::sqrtf(r); - let sqrt_theta = theta / 2.0; - - Complex::new( - sqrt_r * libm::cosf(sqrt_theta), - sqrt_r * libm::sinf(sqrt_theta), - ) - } - - fn atan2(y: Self, x: Self) -> Self { - Complex::new( - libm::atan2f(y.real, x.real), - libm::atan2f(y.imaginary, x.imaginary), - ) - } -} - -impl Float for Complex<f64> { - fn sqrt(self) -> Self { - let r = self.abs(); - let theta = self.phase(); - - let sqrt_r = libm::sqrt(r); - let sqrt_theta = theta / 2.0; - - Complex::new( - sqrt_r * libm::cos(sqrt_theta), - sqrt_r * libm::sin(sqrt_theta), - ) - } - - fn atan2(y: Self, x: Self) -> Self { - Complex::new( - libm::atan2(y.real, x.real), - libm::atan2(y.imaginary, x.imaginary), - ) - } -} diff --git a/libpsi-core/src/maths/numeric_int.rs b/libpsi-core/src/maths/numeric_int.rs deleted file mode 100644 index a9ecec7..0000000 --- a/libpsi-core/src/maths/numeric_int.rs +++ /dev/null @@ -1,44 +0,0 @@ -use super::{Integer, Numeric}; - -impl Integer for i64 {} -impl Integer for i32 {} - -impl Numeric for i32 { - fn zero() -> Self { - 0 - } - - fn one() -> Self { - 1 - } -} - -impl Numeric for i64 { - fn zero() -> Self { - 0 - } - - fn one() -> Self { - 1 - } -} - -impl Numeric for f32 { - fn zero() -> Self { - 0.0 - } - - fn one() -> Self { - 1.0 - } -} - -impl Numeric for f64 { - fn zero() -> Self { - 0.0 - } - - fn one() -> Self { - 1.0 - } -} diff --git a/libpsi-core/src/maths/numeric_traits.rs b/libpsi-core/src/maths/numeric_traits.rs deleted file mode 100644 index dd7fdec..0000000 --- a/libpsi-core/src/maths/numeric_traits.rs +++ /dev/null @@ -1,24 +0,0 @@ -use core::ops; - -pub trait Numeric: - Copy - + PartialOrd - + ops::Add<Output = Self> - + ops::Mul<Output = Self> - + ops::Sub<Output = Self> - + ops::Div<Output = Self> - + ops::Neg<Output = Self> - + ops::AddAssign - + ops::SubAssign - + ops::MulAssign - + ops::DivAssign -{ - fn zero() -> Self; - fn one() -> Self; -} - -pub trait Integer: Numeric {} -pub trait Float: Numeric { - fn sqrt(self) -> Self; - fn atan2(y: Self, x: Self) -> Self; -} |
