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> {
/// Returns the complex conjugate. i.e. `re - i im`
#[inline]
@ -166,7 +174,7 @@ impl<T: Clone + Float> Complex<T> {
#[inline]
pub fn asin(&self) -> Complex<T> {
// 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<T: Clone + Float> Complex<T> {
#[inline]
pub fn acos(&self) -> Complex<T> {
// 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<T: Clone + Float> Complex<T> {
#[inline]
pub fn atan(&self) -> Complex<T> {
// 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 {