Skip to content

Commit

Permalink
Merge #57
Browse files Browse the repository at this point in the history
57: Implement Zero::set_zero and One::set_one r=cuviper a=cuviper



Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper committed Jun 6, 2019
2 parents 813e021 + 1ed6add commit 61a7f5b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ features = ["std", "serde", "rand"]
[dependencies]

[dependencies.num-traits]
version = "0.2.4"
version = "0.2.7"
default-features = false

[dependencies.serde]
Expand Down
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,12 @@ impl<T: Clone + Num> Zero for Complex<T> {
fn is_zero(&self) -> bool {
self.re.is_zero() && self.im.is_zero()
}

#[inline]
fn set_zero(&mut self) {
self.re.set_zero();
self.im.set_zero();
}
}

impl<T: Clone + Num> One for Complex<T> {
Expand All @@ -1008,6 +1014,12 @@ impl<T: Clone + Num> One for Complex<T> {
fn is_one(&self) -> bool {
self.re.is_one() && self.im.is_zero()
}

#[inline]
fn set_one(&mut self) {
self.re.set_one();
self.im.set_zero();
}
}

macro_rules! write_complex {
Expand Down Expand Up @@ -2384,4 +2396,30 @@ mod test {
assert_eq!(v.iter().product::<Complex64>(), _0_1i);
assert_eq!(v.into_iter().product::<Complex64>(), _0_1i);
}

#[test]
fn test_zero() {
let zero = Complex64::zero();
assert!(zero.is_zero());

let mut c = Complex::new(1.23, 4.56);
assert!(!c.is_zero());
assert_eq!(&c + &zero, c);

c.set_zero();
assert!(c.is_zero());
}

#[test]
fn test_one() {
let one = Complex64::one();
assert!(one.is_one());

let mut c = Complex::new(1.23, 4.56);
assert!(!c.is_one());
assert_eq!(&c * &one, c);

c.set_one();
assert!(c.is_one());
}
}

0 comments on commit 61a7f5b

Please sign in to comment.