feat: Add complex number support to math.mod function
Description
This PR implements support for complex numbers in the math.mod function using Gaussian integer division semantics.
Problem Solved
Previously, math.mod would throw a TypeError when given complex number inputs. This prevented users from performing modular arithmetic operations with complex numbers.
Implementation
- Added
Complex, Complexsignature to the mod function - Implemented Gaussian integer division algorithm that:
- Computes quotient
w/zfor complex inputswandz - Rounds to nearest Gaussian integer (complex number with integer real/imaginary parts)
- Handles tie-breaking by choosing quotient with smallest norm
- Returns remainder
r = w - z*qwherenorm(r) < norm(z)
- Computes quotient
Mathematical Properties
- Division Algorithm: For complex numbers
wandz≠0, findsqandrsuch thatw = z*q + r - Norm Condition:
norm(r) < norm(z)wherenorm(a+bi) = a² + b² - Tie Resolution: When multiple Gaussian integers have equal distance, chooses the one with smallest norm
Examples
math.mod(math.complex(5, 3), math.complex(2, 1)) // returns -1+0i
math.mod(math.complex(7, 3), 3) // returns 1+0i (auto-conversion)
Thanks for your interest in mathjs!
First a piece of advice: if you are going to try to use AI to submit pull requests to open-source projects, you may find that it actually takes more of your time than just understanding the code base yourself and making appropriate changes. That said, if you would like to make your own personal effort to hammer this starting point into shape, here are some first things to attend to. Note you should not expect this to be a complete list; this submission is not really ready for review.
- It contains an extraneous, irrelevant Dockerfile.
- It does not update the TypeScript type definition for mod in types/index.d.ts, or add any typescript type tests for it.
- It does not add any complex example to the embedded docs for mod.
- The "test with fractional results" test makes no sense.
- The mixed real/complex tests do not test the behavior when the real value is a bignumber, especially one out of range of an IEEE double.
- The implementation code makes no attempt to reuse existing mathjs operations (via dependencies) but implements mod on complex numbers completely from scratch, redundantly reimplementing everything from the base Complex class definition on up.
Thanks in advance for your efforts; if you prefer not to put in the personal effort to transform this initial submission into one coherent with the mathjs codebase, please just close this PR.
@Aryan2727-debug have you seen the feedback from Glen? Please let me know if you have questions or need help.