josdejong/mathjs

Extend `mod` to complex numbers properly

Open

#3,410 opened on Mar 2, 2025

View on GitHub
 (2 comments) (1 reaction) (0 assignees)JavaScript (1,298 forks)batch import
featurehelp wanted

Repository metrics

Stars
 (13,832 stars)
PR merge metrics
 (No merged PRs in 30d)

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.

Contributor guide