Search code examples
pythonsympypari-gp

Sympy support for Modular groups


Does Sympy support modular groups?

I'm looking for something akin to pari/gp's Mod function.


Solution

  • Somewhat confusingly, the ring of integers modulo n is implemented as GF(n) (the notation suggesting finite fields, which these in general are not). So, 5 mod 12 would be represented by

    >>> from sympy import GF
    >>> x = GF(12)(5)
    >>> print(x)
    5 mod 12
    >>> print(x*4)
    8 mod 12
    >>> print(-x)
    7 mod 12
    >>> print(x+10)
    3 mod 12
    >>> print(x**2)
    1 mod 12