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

mul_add between real and complex numbers #134

Open
isomer opened this issue Oct 31, 2024 · 0 comments
Open

mul_add between real and complex numbers #134

isomer opened this issue Oct 31, 2024 · 0 comments

Comments

@isomer
Copy link

isomer commented Oct 31, 2024

I'm attempting to use:

 println!("{:?}", (2.0_f32).mul_add(
      Complex::new(3.0_f32, 4.0_f32), 
      Complex::new(5.0_f32, 6.0_f32)));

which doesn't exist.

I think the equivalent would be:

fn fma<T: num::Float>(a: T, b: Complex<T>, c: Complex<T>) -> Complex<T> {
    let re = a.mul_add(b.re, c.re);
    let im = a.mul_add(b.im, c.im);
    Complex::new(re, im)
}

which does compile using two fused multiply adds which hopefully the compiler could reorder, vectorise or whatever optimisations it believes fruitful.

The code I'm writing (a convolution kernel) is attempting to be generic over real and complex numbers which relies on the num::Complex crate to provide this implementation.

My preferred solution would be for num::Complex to implement T::mul_add(Complex<T>, Complex<T>) -> Complex<T> where T: MulAdd<T, T, Output=T>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant