josdejong/mathjs

Extend `mod` to complex numbers properly

Open

#3410 aperta il 2 mar 2025

Vedi su GitHub
 (2 commenti) (1 reazione) (0 assegnatari)JavaScript (1298 fork)batch import
featurehelp wanted

Metriche repository

Star
 (13.832 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

Describe the bug The mod function has a reasonable definition for complex numbers: for any complex numbers w and nonzero z, there are at most four Gaussian integers q (complex numbers with integer coefficients) such that w = zq + r, where norm(r) < norm(z). Typically, we can choose the unique r of minimum norm, and define mod(w,z) to be this r (and the corresponding q to be the quotient). Occasionally two or four of the possible r values tie for smallest norm. In such a case, we can break the tie by taking the one that has the corresponding q of smallest norm.

To Reproduce math.mod(math.complex(1,4), math.complex(1,1))

Currently this throws a TypeError; it should return i because 1 + 4i = (2+i)(1+i) + i. Note that Wolfram Alpha returns 1 because it is also the case that 1 + 4i = (2+2i)(1+i) + 1. However, the possible quotient 2+i has smaller norm than 2+2i, so it is preferable to choose that quotient and return remainder i.

Guida contributor