Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ use core::str::FromStr;
#[cfg(feature = "std")]
use std::error::Error;

use num_traits::{ConstOne, ConstZero, Inv, MulAdd, Num, One, Pow, Signed, Zero};
use num_traits::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedRem, CheckedSub, ConstOne, ConstZero, Inv, MulAdd,
Num, One, Pow, Signed, Zero,
};

use num_traits::float::FloatCore;
#[cfg(any(feature = "std", feature = "libm"))]
Expand Down Expand Up @@ -761,6 +764,16 @@ impl<T: Clone + Num> Add<Complex<T>> for Complex<T> {
}
}

impl<T: Clone + Num + CheckedAdd> CheckedAdd for Complex<T> {
#[inline]
fn checked_add(&self, other: &Self) -> Option<Self> {
Some(Self::new(
self.re.checked_add(&other.re)?,
self.im.checked_add(&other.im)?,
))
}
}

forward_all_binop!(impl Sub, sub);

// (a + i b) - (c + i d) == (a - c) + i (b - d)
Expand All @@ -773,6 +786,16 @@ impl<T: Clone + Num> Sub<Complex<T>> for Complex<T> {
}
}

impl<T: Clone + Num + CheckedSub> CheckedSub for Complex<T> {
#[inline]
fn checked_sub(&self, other: &Self) -> Option<Self> {
Some(Self::new(
self.re.checked_sub(&other.re)?,
self.im.checked_sub(&other.im)?,
))
}
}

forward_all_binop!(impl Mul, mul);

// (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c)
Expand All @@ -787,6 +810,23 @@ impl<T: Clone + Num> Mul<Complex<T>> for Complex<T> {
}
}

impl<T: Clone + Num + CheckedMul + CheckedAdd + CheckedSub> CheckedMul for Complex<T> {
#[inline]
fn checked_mul(&self, other: &Self) -> Option<Self> {
let re = self
.re
.checked_mul(&other.re)?
.checked_sub(&self.im.checked_mul(&other.im)?)?;

let im = self
.re
.checked_mul(&other.im)?
.checked_add(&self.im.checked_mul(&other.re)?)?;

Some(Self::new(re, im))
}
}

// (a + i b) * (c + i d) + (e + i f) == ((a*c + e) - b*d) + i (a*d + (b*c + f))
impl<T: Clone + Num + MulAdd<Output = T>> MulAdd<Complex<T>> for Complex<T> {
type Output = Complex<T>;
Expand All @@ -799,6 +839,7 @@ impl<T: Clone + Num + MulAdd<Output = T>> MulAdd<Complex<T>> for Complex<T> {
Complex::new(re, im)
}
}

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

Expand All @@ -824,6 +865,30 @@ impl<T: Clone + Num> Div<Complex<T>> for Complex<T> {
}
}

impl<T: Clone + Num + CheckedAdd + CheckedSub + CheckedMul + CheckedDiv> CheckedDiv for Complex<T> {
#[inline]
fn checked_div(&self, other: &Self) -> Option<Self> {
let norm_sqr = other
.re
.checked_mul(&other.re)?
.checked_add(&other.im.checked_mul(&other.im)?)?;

let re = self
.re
.checked_mul(&other.re)?
.checked_add(&self.im.checked_mul(&other.im)?)?;

let im = self
.im
.checked_mul(&other.re)?
.checked_sub(&self.re.checked_mul(&other.im)?)?;
Some(Self::new(
re.checked_div(&norm_sqr)?,
im.checked_div(&norm_sqr)?,
))
}
}

Comment on lines +868 to +891

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO I find it far easier to read if one uses aliases for the real/imag parts as in the formula of the documentation block above, e.g.

let (a, b) = (&self.re, &self.im);
let (c, d) = (&other.re, &other.im);
let norm_sqr = c.checked_mul(c)?.checked_add(&d.checked_mul(d)?)?;
...

Inconsistency to other code sections needs to be weighted in though...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nowhere else in the arithmetic code does this as far as I can tell.

forward_all_binop!(impl Rem, rem);

impl<T: Clone + Num> Complex<T> {
Expand All @@ -834,6 +899,16 @@ impl<T: Clone + Num> Complex<T> {
}
}

impl<T: Clone + Num + CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + CheckedRem> Complex<T> {
fn checked_div_trunc(&self, divisor: &Self) -> Option<Self> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be added to the mr description.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It parallels the implementation for Rem, which has its own private method div_trunc which is right above it and right beside the Rem impl. I don't believe it's specifically noteworthy to merit being added to the PR description.

let Complex { re, im } = self.checked_div(divisor)?;
let re = re.checked_sub(&re.checked_rem(&T::one())?)?;
let im = im.checked_sub(&im.checked_rem(&T::one())?)?;

Some(Self::new(re, im))
}
}

impl<T: Clone + Num> Rem<Complex<T>> for Complex<T> {
type Output = Self;

Expand All @@ -844,6 +919,17 @@ impl<T: Clone + Num> Rem<Complex<T>> for Complex<T> {
}
}

impl<T: Clone + Num + CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + CheckedRem> CheckedRem
for Complex<T>
{
#[inline]
fn checked_rem(&self, modulus: &Self) -> Option<Self> {
let gaussian = self.checked_div_trunc(modulus)?;

self.checked_sub(&modulus.checked_mul(&gaussian)?)
}
}

// Op Assign

mod opassign {
Expand Down