Skip to content

Add PolynomialMod for an integer modulus - #2005

Open
tombouctou wants to merge 1 commit into
Mathics3:masterfrom
tombouctou:fix-polynomialmod-general
Open

tombouctou wants to merge 1 commit into
Mathics3:masterfrom
tombouctou:fix-polynomialmod-general

Conversation

@tombouctou

Copy link
Copy Markdown
Contributor

PolynomialMod is not defined in Mathics3, so even PolynomialMod[3 x ^ 2 + 2 x + 1, 2] stays unevaluated. This adds it for the common case of a positive integer modulus.

What it does. PolynomialMod[poly, m] with a positive integer m expands poly (with the existing expand_polynomial) and reduces each integer coefficient to 0..m-1. As in WMA, parts that are not polynomials, such as f[1] or Sin[x], are treated as variables:

PolynomialMod[3 x ^ 3 + 21 x ^ 2 - 7 x + 55, 9]     (* 1 + 2 x + 3 x ^ 2 + 3 x ^ 3 *)
PolynomialMod[1234 (x + y) ^ 7 - (x - y) ^ 5, 7]    (* 6 x ^ 5 + 2 x ^ 7 + 5 x ^ 4 y + ... + 2 y ^ 7 *)
PolynomialMod[3 f[1] + 2 f[2] - f[3], 2]           (* f[1] + f[3] *)

Code that works with formal sums mod 2 relies on this, e.g. PolynomialMod[del /@ x, 2] where the terms are words like w[3, 1, 1].

What it does not do yet. It leaves these unevaluated rather than guess at WMA's conventions:

  • coefficients that are Rational, Real or Complex;
  • a modulus that is a polynomial, or a list of moduli;
  • a non-positive modulus, and a list as poly.

Tests. The doc examples above, plus test_polynomialmod in test/builtin/numbers/test_algebra.py, which covers negative coefficients, terms that cancel to 0, non-polynomial parts, and each case left unevaluated. pytest and the doctests give the same results as on master. The only failures are environmental ones that also fail on master: XML/ZIP/JPEG FileFormat, and the CLI tests that need mathics on the PATH. mypy is clean on both changed modules.

🤖 Generated with Claude Code

PolynomialMod[poly, m] with a positive integer m expands poly and reduces
each integer coefficient to 0..m-1. As in WMA, parts that are not
polynomials, such as f[1] or Sin[x], are treated as variables, so
PolynomialMod[3 f[1] + 2 f[2], 2] gives f[1].

Rational, Real and Complex coefficients, a polynomial modulus and lists
are not handled yet; those cases stay unevaluated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant