josdejong/mathjs

Extend `mod` to complex numbers properly

Open

#3 410 ouverte le 2 mars 2025

Voir sur GitHub
 (2 commentaires) (1 réaction) (0 assignés)JavaScript (13 832 stars) (1 298 forks)batch import
featurehelp wanted

Description

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.

Guide contributeur