Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement is_one and Inv for Complex. #17

Merged
merged 3 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ readme = "README.md"
[dependencies]

[dependencies.num-traits]
version = "0.2.0"
version = "0.2.1"
default-features = false
features = ["std"]

Expand Down
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::hash;
use std::ops::{Add, Div, Mul, Neg, Sub, Rem};
use std::str::FromStr;

use traits::{Zero, One, Num, Float};
use traits::{Zero, One, Num, Inv, Float};

// FIXME #1284: handle complex NaN & infinity etc. This
// probably doesn't map to C's _Complex correctly.
Expand Down Expand Up @@ -664,6 +664,24 @@ impl<'a, T: Clone + Num + Neg<Output = T>> Neg for &'a Complex<T> {
}
}

impl<T: Clone + Num + Neg<Output = T>> Inv for Complex<T> {
type Output = Complex<T>;

#[inline]
fn inv(self) -> Complex<T> {
(&self).inv()
}
}

impl<'a, T: Clone + Num + Neg<Output = T>> Inv for &'a Complex<T> {
type Output = Complex<T>;

#[inline]
fn inv(self) -> Complex<T> {
self.inv()
}
}

macro_rules! real_arithmetic {
(@forward $imp:ident::$method:ident for $($real:ident),*) => (
impl<'a, T: Clone + Num> $imp<&'a T> for Complex<T> {
Expand Down Expand Up @@ -841,6 +859,11 @@ impl<T: Clone + Num> One for Complex<T> {
fn one() -> Complex<T> {
Complex::new(One::one(), Zero::zero())
}

#[inline]
fn is_one(&self) -> bool {
self.re.is_one() && self.im.is_zero()
}
}

macro_rules! write_complex {
Expand Down