sarah-quinones / faer-rs

Linear algebra foundation for the Rust programming language
https://faer-rs.github.io
MIT License
1.82k stars 61 forks source link

Issues with complex support #151

Open liss-h opened 1 month ago

liss-h commented 1 month ago

Describe the bug Hi :), I was fiddling around with faer and found the support around complex numbers really confusing & slightly buggy.

  1. Stack overflow on windows. The following code causes a stack overflow on windows (only in dev builds, release works)

    use num_complex::Complex;
    
    let m = Mat::<Complex<f64>>::zeros(4, 3);
    let v = Mat::<Complex<f64>>::zeros(3, 1);
    let r = m * v;
    
    println!("{r:?}");
  2. Additionally, for an unknown (to me) reason there is apparently no way to index a Mat<Complex<f64>>. The following code does not compile:

    let m = Mat::<Complex<f64>>::zeros(4, 3);
    let x = m[(0, 0)]; // trait error here

    At least to me it is not at all apparent why this doesn't work. I haven't found anything in the documentation about it.

  3. It took me a very long time to figure out that you are supposed to use c64 instead of Complex<f64>. Adding an example or some more documentation how to use complex numbers would be appreciated.

Thx :)

sarah-quinones commented 1 month ago

it's documented here, https://docs.rs/faer/latest/faer/complex_native/index.html

liss-h commented 1 month ago

Yeah, I saw that page. It took me quite a while to find it so I think it would be good to feature it more prominently. That page also doesn't really explain 1 & 2

sarah-quinones commented 1 month ago

the stack overflow is due to the stack size on windows being relatively small. faer uses a lot of abstraction layers which ends up being quite costly in non optimized builds. i recommend setting opt-level = 3 in your Cargo.toml if you still want debug assertions

as for 2, i can't return a reference to Complex<_> because the real and imaginary parts are stored separately, which native rust references don't really support

liss-h commented 1 month ago

the stack overflow is due to the stack size on windows being relatively small. faer uses a lot of abstraction layers which ends up being quite costly in non optimized builds. i recommend setting opt-level = 3 in your Cargo.toml if you still want debug assertions

ok thx

as for 2, i can't return a reference to Complex<_> because the real and imaginary parts are stored separately, which native rust references don't really support

Ah, right sorry, I totally forgot that Index has to return a reference

Could you still document (some of) these things? I think it would be beneficial for new users of faer if they don't have to figure this out on their own. Mainly talking about the limitations on windows and featuring the complex_native page more prominently or maybe adding an example for complex numbers in the repo (which links to the page). I think that would be very helpful

sarah-quinones commented 1 month ago

sure, im currently in the middle of a refactor/possible redesign phase, so this might take a while