Skip to content

Conversation

@Aryan2727-debug
Copy link

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, Complex signature to the mod function
  • Implemented Gaussian integer division algorithm that:
    • Computes quotient w/z for complex inputs w and z
    • 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*q where norm(r) < norm(z)

Mathematical Properties

  • Division Algorithm: For complex numbers w and z≠0, finds q and r such that w = z*q + r
  • Norm Condition: norm(r) < norm(z) where norm(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)

@gwhitney
Copy link
Collaborator

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.

  1. It contains an extraneous, irrelevant Dockerfile.
  2. It does not update the TypeScript type definition for mod in types/index.d.ts, or add any typescript type tests for it.
  3. It does not add any complex example to the embedded docs for mod.
  4. The "test with fractional results" test makes no sense.
  5. 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.
  6. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants