From 458e9594d40b92a0dd1e34415838c8c24c6f98fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jan=20Niemier?= Date: Tue, 29 Dec 2015 16:36:08 +0100 Subject: [PATCH] Add complex `i` constant function --- src/complex.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/complex.rs b/src/complex.rs index 51c20f0..4a271c9 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -59,6 +59,14 @@ impl Complex { } } +impl Complex { + /// Returns `i` + #[inline] + pub fn i() -> Complex { + Self::new(T::zero(), T::one()) + } +} + impl> Complex { /// Returns the complex conjugate. i.e. `re - i im` #[inline] @@ -166,7 +174,7 @@ impl Complex { #[inline] pub fn asin(&self) -> Complex { // formula: arcsin(z) = -i ln(sqrt(1-z^2) + iz) - let i = Complex::new(T::zero(), T::one()); + let i = Complex::i(); -i*((Complex::one() - self*self).sqrt() + i*self).ln() } @@ -181,7 +189,7 @@ impl Complex { #[inline] pub fn acos(&self) -> Complex { // formula: arccos(z) = -i ln(i sqrt(1-z^2) + z) - let i = Complex::new(T::zero(), T::one()); + let i = Complex::i(); -i*(i*(Complex::one() - self*self).sqrt() + self).ln() } @@ -196,7 +204,7 @@ impl Complex { #[inline] pub fn atan(&self) -> Complex { // formula: arctan(z) = (ln(1+iz) - ln(1-iz))/(2i) - let i = Complex::new(T::zero(), T::one()); + let i = Complex::i(); let one = Complex::one(); let two = one + one; if *self == i {