diff options
| author | hachem <im@hachem.wtf> | 2024-09-22 14:24:27 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2024-09-22 14:24:27 +0200 |
| commit | ae8abfa8335edf856f423e7a60c5bb830a79d5a7 (patch) | |
| tree | 621e34eef728b971428c20e7bf946289c7dedc67 /libpsi/src/maths/numeric_float.rs | |
| parent | 1a9ef4a7f0cbd4ed848ccf0903ebbbd4fdab6adf (diff) | |
Refactor and implement other gates
Diffstat (limited to 'libpsi/src/maths/numeric_float.rs')
| -rw-r--r-- | libpsi/src/maths/numeric_float.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/libpsi/src/maths/numeric_float.rs b/libpsi/src/maths/numeric_float.rs new file mode 100644 index 0000000..3591f24 --- /dev/null +++ b/libpsi/src/maths/numeric_float.rs @@ -0,0 +1,65 @@ +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), + ) + } +} |
