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 1/2] 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 { From 8e267ee4cb81a5a1ab1b7dabe70d2fa8d6988a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jan=20Niemier?= Date: Wed, 30 Dec 2015 10:57:54 +0100 Subject: [PATCH 2/2] Fix docs and function position --- src/complex.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/complex.rs b/src/complex.rs index 4a271c9..6524eb9 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -39,6 +39,12 @@ impl Complex { Complex { re: re, im: im } } + /// Returns imaginary unit + #[inline] + pub fn i() -> Complex { + Self::new(T::zero(), T::one()) + } + /// Returns the square of the norm (since `T` doesn't necessarily /// have a sqrt function), i.e. `re^2 + im^2`. #[inline] @@ -59,14 +65,6 @@ 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]