josdejong/mathjs

Extend `mod` to complex numbers properly

Open

#3,410 创建于 2025年3月2日

在 GitHub 查看
 (2 评论) (1 反应) (0 负责人)JavaScript (13,832 star) (1,298 fork)batch import
featurehelp wanted

描述

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.

贡献者指南