Add complex `i` constant function

This commit is contained in:
Łukasz Jan Niemier 2015-12-29 16:36:08 +01:00
parent 661e225862
commit 458e9594d4
1 changed files with 11 additions and 3 deletions

View File

@ -59,6 +59,14 @@ impl<T: Clone + Num> Complex<T> {
} }
} }
impl<T: Clone + Num> Complex<T> {
/// Returns `i`
#[inline]
pub fn i() -> Complex<T> {
Self::new(T::zero(), T::one())
}
}
impl<T: Clone + Num + Neg<Output = T>> Complex<T> { impl<T: Clone + Num + Neg<Output = T>> Complex<T> {
/// Returns the complex conjugate. i.e. `re - i im` /// Returns the complex conjugate. i.e. `re - i im`
#[inline] #[inline]
@ -166,7 +174,7 @@ impl<T: Clone + Float> Complex<T> {
#[inline] #[inline]
pub fn asin(&self) -> Complex<T> { pub fn asin(&self) -> Complex<T> {
// formula: arcsin(z) = -i ln(sqrt(1-z^2) + iz) // 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() -i*((Complex::one() - self*self).sqrt() + i*self).ln()
} }
@ -181,7 +189,7 @@ impl<T: Clone + Float> Complex<T> {
#[inline] #[inline]
pub fn acos(&self) -> Complex<T> { pub fn acos(&self) -> Complex<T> {
// formula: arccos(z) = -i ln(i sqrt(1-z^2) + z) // 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() -i*(i*(Complex::one() - self*self).sqrt() + self).ln()
} }
@ -196,7 +204,7 @@ impl<T: Clone + Float> Complex<T> {
#[inline] #[inline]
pub fn atan(&self) -> Complex<T> { pub fn atan(&self) -> Complex<T> {
// formula: arctan(z) = (ln(1+iz) - ln(1-iz))/(2i) // 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 one = Complex::one();
let two = one + one; let two = one + one;
if *self == i { if *self == i {