novacrazy / dual_num

Dual Number library for Rust
https://docs.rs/dual_num/
17 stars 4 forks source link

MulAdd should use `mul_add` functions #21

Open benruijl opened 5 years ago

benruijl commented 5 years ago

At the moment the dual implementation of the MulAdd trait simply does self * a + b, which actually doesn't honor the trait:

Fused multiply-add. Computes (self * a) + b with only one rounding error, yielding a more accurate result than an unfused multiply-add.

Instead we should use something like:

impl<T: Scalar + Num + Mul + Add + MulAdd<Output = T>, N: Dim + DimName> MulAdd<T, T> for DualN<T, N>
where
    DefaultAllocator: Allocator<T, N>,
    Owned<T, N>: Copy,
{
    type Output = DualN<T, N>;

    #[inline]
    fn mul_add(self, a: T, b: T) -> DualN<T, N> {
        self.map_dual(self.real().mul_add(a, b), |x| *x * a)
    }
}