josdejong/mathjs

Extend `mod` to complex numbers properly

Open

#3.410 geöffnet am 2. März 2025

Auf GitHub ansehen
 (2 Kommentare) (1 Reaktion) (0 zugewiesene Personen)JavaScript (13.832 Stars) (1.298 Forks)batch import
featurehelp wanted

Beschreibung

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.

Contributor Guide