rust-num / num-complex

Complex numbers for Rust
Apache License 2.0
230 stars 47 forks source link

Arithmetic operations with reference semantics #35

Open cohomology opened 5 years ago

cohomology commented 5 years ago

In the implementation of complex, the Add, Sub, etc. for references are forwarded via clone() for values. This is very bad for types which are expensive to copy. In my example, I use Complex<Ratio<BigInt>>.

Can the code be refactored in such a way, that no clone() is necessary? I guess the different arithmetic operations have to be coded out and not forwarded.

I could try that, if there is no objection against that ...

cuviper commented 5 years ago

The main problem is that we added all of those implementations with just T: Clone + Num, and it's a breaking change to add additional constraints. To implement ops directly on references, we would probably want to use NumRef, NumAssignRef, and RefNum.

cuviper commented 4 years ago

In my example, I use Complex<Ratio<BigInt>>.

Note, Ratio also implements reference ops by cloning, which means that changing Complex alone wouldn't really help you.

I started looking at this for 0.3 (#70), but I'm skeptical. It requires a lot of code duplication on our part to directly implement each op with/without clones, and the additional T: NumRef + ... requirements end up propagating widely. The latter will affect all generic users too, sadly.