Skip to content
Merged
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
109 changes: 106 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use core::str::FromStr;
#[cfg(feature = "std")]
use std::error::Error;

use traits::{Inv, Num, One, Signed, Zero};
use traits::{Inv, MulAdd, Num, One, Signed, Zero};

#[cfg(feature = "std")]
use traits::float::Float;
Expand Down Expand Up @@ -602,6 +602,27 @@ impl<T: Clone + Num> Mul<Complex<T>> for Complex<T> {
}
}

// (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>;

#[inline]
fn mul_add(self, other: Complex<T>, add: Complex<T>) -> Complex<T> {
let re = self.re.clone().mul_add(other.re.clone(), add.re)
- (self.im.clone() * other.im.clone()); // FIXME: use mulsub when available in rust
let im = self.re.mul_add(other.im, self.im.mul_add(other.re, add.im));
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>;

#[inline]
fn mul_add(self, other: &Complex<T>, add: &Complex<T>) -> Complex<T> {
self.clone().mul_add(other.clone(), add.clone())
}
}

forward_all_binop!(impl Div, div);

// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d)
Expand Down Expand Up @@ -640,7 +661,7 @@ impl<T: Clone + Num> Rem<Complex<T>> for Complex<T> {
mod opassign {
use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};

use traits::NumAssign;
use traits::{MulAddAssign, NumAssign};

use Complex;

Expand All @@ -664,6 +685,28 @@ mod opassign {
}
}

// (a + i b) * (c + i d) + (e + i f) == ((a*c + e) - b*d) + i (b*c + (a*d + f))
impl<T: Clone + NumAssign + MulAddAssign> MulAddAssign for Complex<T> {
fn mul_add_assign(&mut self, other: Complex<T>, add: Complex<T>) {
let a = self.re.clone();

self.re.mul_add_assign(other.re.clone(), add.re); // (a*c + e)
self.re -= self.im.clone() * other.im.clone(); // ((a*c + e) - b*d)

let mut adf = a;
adf.mul_add_assign(other.im, add.im); // (a*d + f)
self.im.mul_add_assign(other.re, adf); // (b*c + (a*d + f))
}
}

impl<'a, 'b, T: Clone + NumAssign + MulAddAssign> MulAddAssign<&'a Complex<T>, &'b Complex<T>>
for Complex<T>
{
fn mul_add_assign(&mut self, other: &Complex<T>, add: &Complex<T>) {
self.mul_add_assign(other.clone(), add.clone());
}
}

impl<T: Clone + NumAssign> DivAssign for Complex<T> {
fn div_assign(&mut self, other: Self) {
*self = self.clone() / other;
Expand Down Expand Up @@ -1121,6 +1164,7 @@ where
}
}

#[allow(deprecated)] // `trim_left_matches` and `trim_right_matches` since 1.33
fn from_str_generic<T, E, F>(s: &str, from: F) -> Result<Complex<T>, ParseComplexError<E>>
where
F: Fn(&str) -> Result<T, E>,
Expand Down Expand Up @@ -1959,7 +2003,7 @@ mod test {

mod complex_arithmetic {
use super::{_05_05i, _0_0i, _0_1i, _1_0i, _1_1i, _4_2i, _neg1_1i, all_consts};
use traits::Zero;
use traits::{MulAdd, MulAddAssign, Zero};

#[test]
fn test_add() {
Expand Down Expand Up @@ -2000,6 +2044,65 @@ mod test {
}
}

#[test]
#[cfg(feature = "std")]
fn test_mul_add_float() {
assert_eq!(_05_05i.mul_add(_05_05i, _0_0i), _05_05i * _05_05i + _0_0i);
assert_eq!(_05_05i * _05_05i + _0_0i, _05_05i.mul_add(_05_05i, _0_0i));
assert_eq!(_0_1i.mul_add(_0_1i, _0_1i), _neg1_1i);
assert_eq!(_1_0i.mul_add(_1_0i, _1_0i), _1_0i * _1_0i + _1_0i);
assert_eq!(_1_0i * _1_0i + _1_0i, _1_0i.mul_add(_1_0i, _1_0i));

let mut x = _1_0i;
x.mul_add_assign(_1_0i, _1_0i);
assert_eq!(x, _1_0i * _1_0i + _1_0i);

for &a in &all_consts {
for &b in &all_consts {
for &c in &all_consts {
let abc = a * b + c;
assert_eq!(a.mul_add(b, c), abc);
let mut x = a;
x.mul_add_assign(b, c);
assert_eq!(x, abc);
}
}
}
}

#[test]
fn test_mul_add() {
use super::Complex;
const _0_0i: Complex<i32> = Complex { re: 0, im: 0 };
const _1_0i: Complex<i32> = Complex { re: 1, im: 0 };
const _1_1i: Complex<i32> = Complex { re: 1, im: 1 };
const _0_1i: Complex<i32> = Complex { re: 0, im: 1 };
const _neg1_1i: Complex<i32> = Complex { re: -1, im: 1 };
const all_consts: [Complex<i32>; 5] = [_0_0i, _1_0i, _1_1i, _0_1i, _neg1_1i];

assert_eq!(_1_0i.mul_add(_1_0i, _0_0i), _1_0i * _1_0i + _0_0i);
assert_eq!(_1_0i * _1_0i + _0_0i, _1_0i.mul_add(_1_0i, _0_0i));
assert_eq!(_0_1i.mul_add(_0_1i, _0_1i), _neg1_1i);
assert_eq!(_1_0i.mul_add(_1_0i, _1_0i), _1_0i * _1_0i + _1_0i);
assert_eq!(_1_0i * _1_0i + _1_0i, _1_0i.mul_add(_1_0i, _1_0i));

let mut x = _1_0i;
x.mul_add_assign(_1_0i, _1_0i);
assert_eq!(x, _1_0i * _1_0i + _1_0i);

for &a in &all_consts {
for &b in &all_consts {
for &c in &all_consts {
let abc = a * b + c;
assert_eq!(a.mul_add(b, c), abc);
let mut x = a;
x.mul_add_assign(b, c);
assert_eq!(x, abc);
}
}
}
}

#[test]
fn test_div() {
test_op!(_neg1_1i / _0_1i, _1_1i);
Expand Down