MethodicalAcceleratorDesign / MAD-NG

MAD Next-Generation official repository
GNU General Public License v3.0
32 stars 11 forks source link

`mad_ctpsa_hypot(3)` unexpected behavior #417

Closed mattsignorelli closed 9 months ago

mattsignorelli commented 9 months ago

For complex scalars as TPSAs, mad_ctpsa_hypot gives:

hypot(3+3i, 4+4i) = 5+5i
hypot(3+3i, 4) = 4.4767839548935457 +2.0103717513913431i

In Julia/MATLAB/etc, hypot with complex numbers always return a real scalar as sqrt(cabs(a)^2+cabs(b)^2), so I'm not sure if this is a bug, or if not, then what exactly does mad_ctpsa_hypot calculate?

I also found mad_tpsa_hypot and mad_ctpsa_hypot do not correctly calculate hypot when using one of the input tpsas as the destination tpsa (e.g. mad_tpsa_hypot(t1, t2, t1) does not give the correct answer), and wanted to confirm this was expected behavior for this function, because all of the other ones so far appear ok with this.

ldeniau commented 9 months ago

tpsa and ctpsa functions do not perform type promotion (except those in mad_ctpsa_conv), i.e. they do not change the type returned with respect to the input type. Such promotions are managed at the LUA level. Consequently, hypot computes sqrt(x^2+y^2) for real and complex. The scalar value can be obtained with abs.

I will check for the aliasing problem.

ldeniau commented 9 months ago

aliasing fixed in c785bf0f