Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Gimp/app/pdb/   (Gimp Bildeditor Version 3.2.0©)  Datei vom 14.2.2026 mit Größe 10 kB image not shown  

Quelle  gmpy.py   Sprache: Python

 

from __future__ import annotations
import os
from ctypes import c_long, sizeof
from functools import reduce
from typing import TYPE_CHECKING
from warnings import warn

from sympy.external import import_module

from .pythonmpq import PythonMPQ

from .ntheory import (
    bit_scan1 as python_bit_scan1,
    bit_scan0 as python_bit_scan0,
    remove as python_remove,
    factorial as python_factorial,
    sqrt as python_sqrt,
    sqrtrem as python_sqrtrem,
    gcd as python_gcd,
    lcm as python_lcm,
    gcdext as python_gcdext,
    is_square as python_is_square,
    invert as python_invert,
    fibonacci as python_fibonacci,
    legendre as python_legendre,
    jacobi as python_jacobi,
    kronecker as python_kronecker,
    iroot as python_iroot,
    is_fermat_prp as python_is_fermat_prp,
    is_euler_prp as python_is_euler_prp,
    is_strong_prp as python_is_strong_prp,
    is_fibonacci_prp as python_is_fibonacci_prp,
    is_lucas_prp as python_is_lucas_prp,
    is_selfridge_prp as python_is_selfridge_prp,
    is_strong_lucas_prp as python_is_strong_lucas_prp,
    is_strong_selfridge_prp as python_is_strong_selfridge_prp,
    is_bpsw_prp as python_is_bpsw_prp,
    is_strong_bpsw_prp as python_is_strong_bpsw_prp,
)

if TYPE_CHECKING:
    from typing import Generic, Iterator, Protocol, Self, Sequence, TypeVar, overload
    from types import ModuleType


__all__ = [
    # GROUND_TYPES is either 'gmpy' or 'python' depending on which is used. If
    # gmpy is installed then it will be used unless the environment variable
    # SYMPY_GROUND_TYPES is set to something other than 'auto', 'gmpy', or
    # 'gmpy2'.
    'GROUND_TYPES',

    # If HAS_GMPY is 0, no supported version of gmpy is available. Otherwise,
    # HAS_GMPY will be 2 for gmpy2 if GROUND_TYPES is 'gmpy'. It used to be
    # possible for HAS_GMPY to be 1 for gmpy but gmpy is no longer supported.
    'HAS_GMPY',

    # SYMPY_INTS is a tuple containing the base types for valid integer types.
    # This is either (int,) or (int, type(mpz(0))) depending on GROUND_TYPES.
    'SYMPY_INTS',

    # MPQ is either gmpy.mpq or the Python equivalent from
    # sympy.external.pythonmpq
    'MPQ',

    # MPZ is either gmpy.mpz or int.
    'MPZ',

    'bit_scan1',
    'bit_scan0',
    'remove',
    'factorial',
    'sqrt',
    'is_square',
    'sqrtrem',
    'gcd',
    'lcm',
    'gcdext',
    'invert',
    'fibonacci',
    'legendre',
    'jacobi',
    'kronecker',
    'iroot',
    'is_fermat_prp',
    'is_euler_prp',
    'is_strong_prp',
    'is_fibonacci_prp',
    'is_lucas_prp',
    'is_selfridge_prp',
    'is_strong_lucas_prp',
    'is_strong_selfridge_prp',
    'is_bpsw_prp',
    'is_strong_bpsw_prp',
]


#
# Tested python-flint version. Future versions might work but we will only use
# them if explicitly requested by SYMPY_GROUND_TYPES=flint.
#
_PYTHON_FLINT_VERSION_NEEDED = ["0.6", "0.7", "0.8", "0.9"]


def _flint_version_okay(flint_version):
    major, minor = flint_version.split('.')[:2]
    flint_ver = f'{major}.{minor}'
    return flint_ver in _PYTHON_FLINT_VERSION_NEEDED

#
# We will only use gmpy2 >= 2.0.0
#
_GMPY2_MIN_VERSION = '2.0.0'


def _get_flint(sympy_ground_types):
    if sympy_ground_types not in ('auto', 'flint'):
        return None

    try:
        import flint
        # Earlier versions of python-flint may not have __version__.
        from flint import __version__ as _flint_version
    except ImportError:
        if sympy_ground_types == 'flint':
            warn("SYMPY_GROUND_TYPES was set to flint but python-flint is not "
                 "installed. Falling back to other ground types.")
        return None

    if _flint_version_okay(_flint_version):
        return flint
    elif sympy_ground_types == 'auto':
        return None
    else:
        warn(f"Using python-flint {_flint_version} because SYMPY_GROUND_TYPES "
             f"is set to flint but this version of SymPy is only tested "
             f"with python-flint versions {_PYTHON_FLINT_VERSION_NEEDED}.")
        return flint


def _get_gmpy2(sympy_ground_types):
    if sympy_ground_types not in ('auto', 'gmpy', 'gmpy2'):
        return None

    gmpy = import_module('gmpy2', min_module_version=_GMPY2_MIN_VERSION,
            module_version_attr='version', module_version_attr_call_args=())

    if sympy_ground_types != 'auto' and gmpy is None:
        warn("gmpy2 library is not installed, switching to 'python' ground types")

    return gmpy


#
# SYMPY_GROUND_TYPES can be flint, gmpy, gmpy2, python or auto (default)
#
_SYMPY_GROUND_TYPES = os.environ.get('SYMPY_GROUND_TYPES', 'auto').lower()

_flint:  ModuleType | None = None
_gmpy:  ModuleType | None = None


#
# First handle auto-detection of flint/gmpy2. We will prefer flint if available
# or otherwise gmpy2 if available and then lastly the python types.
#
if _SYMPY_GROUND_TYPES in ('auto', 'flint'):
    _flint = _get_flint(_SYMPY_GROUND_TYPES)
    if _flint is not None:
        _SYMPY_GROUND_TYPES = 'flint'
    else:
        _SYMPY_GROUND_TYPES = 'auto'

if _SYMPY_GROUND_TYPES in ('auto', 'gmpy', 'gmpy2'):
    _gmpy = _get_gmpy2(_SYMPY_GROUND_TYPES)
    if _gmpy is not None:
        _SYMPY_GROUND_TYPES = 'gmpy'
    else:
        _SYMPY_GROUND_TYPES = 'python'

if _SYMPY_GROUND_TYPES not in ('flint', 'gmpy', 'python'):
    warn("SYMPY_GROUND_TYPES environment variable unrecognised. "
         "Should be 'auto', 'flint', 'gmpy', 'gmpy2' or 'python'.")
    _SYMPY_GROUND_TYPES = 'python'

#
# At this point _SYMPY_GROUND_TYPES is either flint, gmpy or python. The blocks
# below define the values exported by this module in each case.
#

#
# In gmpy2 and flint, there are functions that take a long (or unsigned long)
# argument. That is, it is not possible to input a value larger than that.
#
LONG_MAX = (1 << (8*sizeof(c_long) - 1)) - 1

#
# Type checkers are confused by what SYMPY_INTS is. There may be a better type
# hint for this like Type[Integral] or something.
#
SYMPY_INTS: tuple[type, ...]
GROUND_TYPES: str


if TYPE_CHECKING:

    class MPZ:
        """
        Dummy class for type checking purposes. This will be either int,
        gmpy.mpz, or flint.fmpz depending on the ground types.
        "
""
        def __init__(self, arg: int | MPZ | str = 0, /) -> None: ...

        def __int__(self) -> int: ...
        def __index__(self) -> int: ...

        def __pos__(self) -> MPZ: ...
        def __neg__(self) -> MPZ: ...
        def __add__(self, other: MPZ | int, /) -> MPZ: ...
        def __radd__(self, other: int, /) -> MPZ: ...
        def __sub__(self, other: MPZ | int, /) -> MPZ: ...
        def __rsub__(self, other: int, /) -> MPZ: ...
        def __mul__(self, other: MPZ | int, /) -> MPZ: ...
        def __rmul__(self, other: int, /) -> MPZ: ...
        def __pow__(self, other: int, modulus: MPZ | int | None = None, /) -> MPZ: ...
        def __rpow__(self, other: int, /) -> MPZ: ...
        def __floordiv__(self, other: MPZ | int, /) -> MPZ: ...
        def __rfloordiv__(self, other: int, /) -> MPZ: ...
        def __mod__(self, other: MPZ | int, /) -> MPZ: ...
        def __rmod__(self, other: int, /) -> MPZ: ...
        def __divmod__(self, other: MPZ | int, /) -> tuple[MPZ, MPZ]: ...
        def __rdivmod__(self, other: int, /) -> tuple[MPZ, MPZ]: ...

        def __lshift__(self, other: int, /) -> MPZ: ...
        def __rlshift__(self, other: int, /) -> MPZ: ...
        def __rshift__(self, other: int, /) -> MPZ: ...
        def __rrshift__(self, other: int, /) -> MPZ: ...

        def __and__(self, other: MPZ | int, /) -> MPZ: ...
        def __rand__(self, other: int, /) -> MPZ: ...
        def __or__(self, other: MPZ | int, /) -> MPZ: ...
        def __ror__(self, other: int, /) -> MPZ: ...
        def __xor__(self, other: MPZ | int, /) -> MPZ: ...
        def __rxor__(self, other: int, /) -> MPZ: ...
        def __invert__(self) -> MPZ: ...

        def __eq__(self, other: object, /) -> bool: ...
        def __hash__(self, /) -> int: ...

        def __lt__(self, other: MPZ | int, /) -> bool: ...
        def __le__(self, other: MPZ | int, /) -> bool: ...
        def __gt__(self, other: MPZ | int, /) -> bool: ...
        def __ge__(self, other: MPZ | int, /) -> bool: ...

        def __bool__(self, /) -> bool: ...

    class MPQ:
        """
        Dummy class for type checking purposes. This will be either PythonMPQ,
        gmpy.mpq, or flint.fmpq depending on the ground types.
        "
""
        def __init__(self, arg1: int | MPZ | MPQ | str = 0,
                           arg2: int | MPZ = 1, /) -> None: ...

        def __int__(self) -> int: ...

        @property
        def numerator(self) -> MPZ: ...
        @property
        def denominator(self) -> MPZ: ...

        def __pos__(self) -> MPQ: ...
        def __neg__(self) -> MPQ: ...
        def __add__(self, other: MPQ | MPZ | int, /) -> MPQ: ...
        def __radd__(self, other: MPZ | int, /) -> MPQ: ...
        def __sub__(self, other: MPQ | MPZ | int, /) -> MPQ: ...
        def __rsub__(self, other: MPZ | int, /) -> MPQ: ...
        def __mul__(self, other: MPQ | MPZ | int, /) -> MPQ: ...
        def __rmul__(self, other: MPZ | int, /) -> MPQ: ...
        def __pow__(self, other: int, /) -> MPQ: ...

        def __truediv__(self, other: MPQ | MPZ | int, /) -> MPQ: ...
        def __rtruediv__(self, other: MPZ | int, /) -> MPQ: ...

        def __floordiv__(self, other: MPQ | MPZ | int, /) -> MPQ: ...
        def __rfloordiv__(self, other: MPZ | int, /) -> MPQ: ...
        def __mod__(self, other: MPQ | MPZ | int, /) -> MPQ: ...
        def __rmod__(self, other: MPZ | int, /) -> MPQ: ...
        def __divmod__(self, other: MPQ | MPZ | int, /) -> tuple[MPQ, MPQ]: ...
        def __rdivmod__(self, other: MPZ | int, /) -> tuple[MPQ, MPQ]: ...

        def __lt__(self, other: MPQ | MPZ | int, /) -> bool: ...
        def __le__(self, other: MPQ | MPZ | int, /) -> bool: ...
        def __gt__(self, other: MPQ | MPZ | int, /) -> bool: ...
        def __ge__(self, other: MPQ | MPZ | int, /) -> bool: ...

    class NMOD:
        """Dummy class for type checking purposes: flint.nmod-like type."""
        def __init__(self, val: NMOD | MPZ | int = 0, mod: int = 2, /) -> None: ...
        def __int__(self) -> int: ...
        def __index__(self) -> int: ...
        def __pos__(self) -> NMOD: ...
        def __neg__(self) -> NMOD: ...
        def __add__(self, other: NMOD | MPZ | int, /) -> NMOD: ...
        def __radd__(self, other: MPZ | int, /) -> NMOD: ...
        def __sub__(self, other: NMOD | MPZ | int, /) -> NMOD: ...
        def __rsub__(self, other: MPZ | int, /) -> NMOD: ...
        def __mul__(self, other: NMOD | MPZ | int, /) -> NMOD: ...
        def __rmul__(self, other: MPZ | int, /) -> NMOD: ...
        def __truediv__(self, other: NMOD | MPZ | int, /) -> NMOD: ...
        def __rtruediv__(self, other: MPZ | int, /) -> NMOD: ...
        def __pow__(self, other: int, /) -> NMOD: ...
        def __eq__(self, other: object, /) -> bool: ...
        def __hash__(self, /) -> int: ...
        def __bool__(self, /) -> bool: ...

    class FMPZ_MOD:
        """Dummy class for type checking purposes: flint.fmpz_mod-like type."""
        def __init__(self, val: FMPZ_MOD | MPZ | int = 0, mod: MPZ | int = 2, /) -> None: ...
        def __int__(self) -> int: ...
        def __index__(self) -> int: ...
        def __pos__(self) -> FMPZ_MOD: ...
        def __neg__(self) -> FMPZ_MOD: ...
        def __add__(self, other: FMPZ_MOD | MPZ | int, /) -> FMPZ_MOD: ...
        def __radd__(self, other: MPZ | int, /) -> FMPZ_MOD: ...
        def __sub__(self, other: FMPZ_MOD | MPZ | int, /) -> FMPZ_MOD: ...
        def __rsub__(self, other: MPZ | int, /) -> FMPZ_MOD: ...
        def __mul__(self, other: FMPZ_MOD | MPZ | int, /) -> FMPZ_MOD: ...
        def __rmul__(self, other: MPZ | int, /) -> FMPZ_MOD: ...
        def __truediv__(self, other: FMPZ_MOD | MPZ | int, /) -> FMPZ_MOD: ...
        def __rtruediv__(self, other: MPZ | int, /) -> FMPZ_MOD: ...
        def __pow__(self, other: int, /) -> FMPZ_MOD: ...
        def __eq__(self, other: object, /) -> bool: ...
        def __hash__(self, /) -> int: ...
        def __bool__(self, /) -> bool: ...

    class FMPZ_MOD_POLY_CTX:
        """Dummy class for type checking purposes: flint.fmpz_mod_poly_ctx-like type."""
        def __init__(self, mod: MPZ | int, /) -> None: ...
        def modulus(self) -> MPZ: ...
        def __call__(self, val: FMPZ_MOD_POLY | Sequence[FMPZ_MOD | MPZ | int], /) -> FMPZ_MOD_POLY: ...

    _Tground = TypeVar("_Tground")

    class POLY_P(Protocol[_Tground]):
        """
        Dummy base class for type checking purposes. This mirrors the relevant
        parts of python-flint's poly_p protocol and represents univariate
        polynomial-like objects.
        "
""
        def str(self, ascending: bool = False, var: str = "x") -> str: ...
        def __iter__(self) -> Iterator[_Tground]: ...
        def __getitem__(self, index: int, /) -> _Tground: ...
        def __setitem__(self, index: int, value: _Tground | int, /) -> None: ...
        def __len__(self) -> int: ...
        def length(self) -> int: ...
        def degree(self) -> int: ...
        def coeffs(self) -> list[_Tground]: ...
        @overload
        def __call__(self, other: _Tground | int, /) -> _Tground: ...
        @overload
        def __call__(self, other: Self, /) -> Self: ...

        def __pos__(self) -> Self: ...
        def __neg__(self) -> Self: ...
        def __add__(self, other: _Tground | int | Self, /) -> Self: ...
        def __radd__(self, other: _Tground | int, /) -> Self: ...
        def __sub__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rsub__(self, other: _Tground | int, /) -> Self: ...
        def __mul__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rmul__(self, other: _Tground | int, /) -> Self: ...
        def __truediv__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rtruediv__(self, other: _Tground | int, /) -> Self: ...
        def __floordiv__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rfloordiv__(self, other: _Tground | int, /) -> Self: ...
        def __mod__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rmod__(self, other: _Tground | int, /) -> Self: ...
        def __divmod__(self, other: _Tground | int | Self, /) -> tuple[Self, Self]: ...
        def __rdivmod__(self, other: _Tground | int, /) -> tuple[Self, Self]: ...
        def __pow__(self, other: int, /) -> Self: ...

        def left_shift(self, other: int, /) -> Self: ...
        def right_shift(self, other: int, /) -> Self: ...
        def truncate(self, n: int, /) -> Self: ...
        def derivative(self) -> Self: ...

    class FLINT_POLY_P(POLY_P[_Tground], Protocol[_Tground]):
        """Protocol for FLINT polynomial methods used in DUP_Flint."""
        def deflation(self) -> tuple[Self, int]: ...
        def gcd(self, other: Self | _Tground | int, /) -> Self: ...
        def xgcd(self, other: Self | _Tground | int, /) -> tuple[Self, Self, Self]: ...
        def factor(self) -> tuple[_Tground, list[tuple[Self, int]]]: ...
        def integral(self) -> Self: ...
        def numer(self) -> Self: ...
        def denom(self) -> _Tground: ...
        def is_cyclotomic(self) -> bool: ...

    class SERIES_P(Protocol[_Tground]):
        """Protocol for FLINT univariate power series-like types."""
        def repr(self) -> str: ...
        @property
        def prec(self) -> int: ...
        def __len__(self) -> int: ...
        def __getitem__(self, index: int, /) -> _Tground: ...
        def coeffs(self) -> list[_Tground]: ...

    class _POLY_DUMMY(Generic[_Tground]):
        def str(self, ascending: bool = False, var: str = "x") -> str: ...
        def __iter__(self) -> Iterator[_Tground]: ...
        def __getitem__(self, index: int, /) -> _Tground: ...
        def __setitem__(self, index: int, value: _Tground | int, /) -> None: ...
        def __len__(self) -> int: ...
        def length(self) -> int: ...
        def degree(self) -> int: ...
        def coeffs(self) -> list[_Tground]: ...

        def __pos__(self) -> Self: ...
        def __neg__(self) -> Self: ...
        def __add__(self, other: _Tground | int | Self, /) -> Self: ...
        def __radd__(self, other: _Tground | int, /) -> Self: ...
        def __sub__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rsub__(self, other: _Tground | int, /) -> Self: ...
        def __mul__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rmul__(self, other: _Tground | int, /) -> Self: ...
        def __truediv__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rtruediv__(self, other: _Tground | int, /) -> Self: ...
        def __floordiv__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rfloordiv__(self, other: _Tground | int, /) -> Self: ...
        def __mod__(self, other: _Tground | int | Self, /) -> Self: ...
        def __rmod__(self, other: _Tground | int, /) -> Self: ...
        def __divmod__(self, other: _Tground | int | Self, /) -> tuple[Self, Self]: ...
        def __rdivmod__(self, other: _Tground | int, /) -> tuple[Self, Self]: ...
        def __pow__(self, other: int, /) -> Self: ...

        def left_shift(self, other: int, /) -> Self: ...
        def right_shift(self, other: int, /) -> Self: ...
        def truncate(self, n: int, /) -> Self: ...
        def derivative(self) -> Self: ...

    class FMPZ_POLY(_POLY_DUMMY[MPZ]):
        """Dummy class for type checking: flint.fmpz_poly-like type."""
        def __init__(
            self, other: FMPZ_POLY | FMPZ_SERIES | Sequence[MPZ | int] | MPZ | int = 0, /
        ) -> None: ...
        @overload
        def __call__(self, other: MPZ | int, /) -> MPZ: ...
        @overload
        def __call__(self, other: FMPZ_POLY, /) -> FMPZ_POLY: ...
        def __call__(self, other: MPZ | int | FMPZ_POLY, /) -> MPZ | FMPZ_POLY: ...
        def deflation(self) -> tuple[FMPZ_POLY, int]: ...
        def gcd(self, other: FMPZ_POLY | MPZ | int, /) -> FMPZ_POLY: ...
        def is_cyclotomic(self) -> bool: ...

    class FMPQ_POLY(_POLY_DUMMY[MPQ]):
        """Dummy class for type checking: flint.fmpq_poly-like type."""
        def __init__(
            self,
            other: FMPQ_POLY | FMPQ_SERIES | Sequence[MPQ | MPZ | int] | MPQ | MPZ | int = 0,
            den: MPZ | int = 1,
            /,
        ) -> None: ...
        @overload
        def __call__(self, other: MPQ | MPZ | int, /) -> MPQ: ...
        @overload
        def __call__(self, other: FMPQ_POLY, /) -> FMPQ_POLY: ...
        @overload
        def __call__(self, other: FMPZ_POLY, /) -> FMPQ_POLY: ...
        def __call__(self, other: MPQ | MPZ | int | FMPQ_POLY | FMPZ_POLY, /) -> MPQ | FMPQ_POLY: ...
        def sqrt(self) -> FMPQ_POLY | FMPQ_SERIES: ...
        def deflation(self) -> tuple[FMPQ_POLY, int]: ...
        def gcd(self, other: FMPQ_POLY | MPQ | MPZ | int, /) -> FMPQ_POLY: ...
        def xgcd(self, other: FMPQ_POLY | MPQ | MPZ | int, /) -> tuple[FMPQ_POLY, FMPQ_POLY, FMPQ_POLY]: ...
        def factor(self, *, monic: bool = False) -> tuple[MPQ, list[tuple[FMPQ_POLY, int]]]: ...
        def factor_squarefree(self) -> tuple[MPQ, list[tuple[FMPQ_POLY, int]]]: ...
        def numer(self) -> FMPZ_POLY: ...
        def denom(self) -> MPZ: ...
        def integral(self) -> FMPQ_POLY: ...

    class NMOD_POLY(_POLY_DUMMY[NMOD]):
        """Dummy class for type checking: flint.nmod_poly-like type."""
        def __init__(self, val: NMOD_POLY | Sequence[NMOD | MPZ | int] | FMPZ_POLY | NMOD | MPZ | int = 0, mod: int = 2, /) -> None: ...
        @overload
        def __call__(self, other: NMOD | MPZ | int, /) -> NMOD: ...
        @overload
        def __call__(self, other: NMOD_POLY | FMPZ_POLY, /) -> NMOD_POLY: ...
        def __call__(self, other: NMOD | MPZ | int | NMOD_POLY | FMPZ_POLY, /) -> NMOD | NMOD_POLY: ...
        def modulus(self) -> int: ...
        def integral(self) -> NMOD_POLY: ...
        def sqrt(self) -> NMOD_POLY: ...
        def gcd(self, other: NMOD_POLY | FMPZ_POLY | NMOD | MPZ | int, /) -> NMOD_POLY: ...
        def xgcd(self, other: NMOD_POLY | FMPZ_POLY | NMOD | MPZ | int, /) -> tuple[NMOD_POLY, NMOD_POLY, NMOD_POLY]: ...
        def factor(self) -> tuple[NMOD, list[tuple[NMOD_POLY, int]]]: ...
        def factor_squarefree(self) -> tuple[NMOD, list[tuple[NMOD_POLY, int]]]: ...
        def deflation(self) -> tuple[NMOD_POLY, int]: ...

    class FMPZ_MOD_POLY(_POLY_DUMMY[FMPZ_MOD]):
        """Dummy class for type checking: flint.fmpz_mod_poly-like type."""
        def __init__(
            self,
            val: Sequence[FMPZ_MOD | MPZ | int] | FMPZ_MOD_POLY | FMPZ_POLY | FMPZ_MOD | MPZ | int,
            ctx: FMPZ_MOD_POLY_CTX,
            /
        ) -> None: ...
        @overload
        def __call__(self, other: FMPZ_MOD | MPZ | int, /) -> FMPZ_MOD: ...
        @overload
        def __call__(self, other: FMPZ_MOD_POLY | FMPZ_POLY, /) -> FMPZ_MOD_POLY: ...
        def __call__(self, other: FMPZ_MOD | MPZ | int | FMPZ_MOD_POLY | FMPZ_POLY, /) -> FMPZ_MOD | FMPZ_MOD_POLY: ...
        def context(self) -> FMPZ_MOD_POLY_CTX: ...
        def modulus(self) -> MPZ: ...
        def integral(self) -> FMPZ_MOD_POLY: ...
        def sqrt(self) -> FMPZ_MOD_POLY: ...
        def gcd(self, other: FMPZ_MOD_POLY | FMPZ_POLY | FMPZ_MOD | MPZ | int, /) -> FMPZ_MOD_POLY: ...
        def xgcd(self, other: FMPZ_MOD_POLY | FMPZ_POLY | FMPZ_MOD | MPZ | int, /) -> tuple[FMPZ_MOD_POLY, FMPZ_MOD_POLY, FMPZ_MOD_POLY]: ...
        def factor(self) -> tuple[FMPZ_MOD, list[tuple[FMPZ_MOD_POLY, int]]]: ...
        def factor_squarefree(self) -> tuple[FMPZ_MOD, list[tuple[FMPZ_MOD_POLY, int]]]: ...
        def deflation(self) -> tuple[FMPZ_MOD_POLY, int]: ...

    class FMPZ_SERIES:
        """Dummy class for type checking: flint.fmpz_series-like type."""
        def __init__(
            self,
            val: FMPZ_SERIES | FMPZ_POLY | Sequence[MPZ | int] | MPZ | int | None = None,
            *,
            prec: int | None = None
        ) -> None: ...
        @property
        def prec(self) -> int: ...
        def repr(self) -> str: ...
        def __len__(self) -> int: ...
        def __bool__(self) -> bool: ...
        def __getitem__(self, index: int, /) -> MPZ: ...
        def coeffs(self) -> list[MPZ]: ...
        def _equal_repr(self, other: FMPZ_SERIES, /) -> bool: ...
        def __call__(self, other: FMPZ_POLY | FMPZ_SERIES, /) -> FMPZ_SERIES: ...
        def __add__(self, other: MPZ | int | FMPZ_POLY | FMPZ_SERIES, /) -> FMPZ_SERIES: ...
        def __radd__(self, other: MPZ | int | FMPZ_POLY, /) -> FMPZ_SERIES: ...
        def __sub__(self, other: MPZ | int | FMPZ_POLY | FMPZ_SERIES, /) -> FMPZ_SERIES: ...
        def __rsub__(self, other: MPZ | int | FMPZ_POLY, /) -> FMPZ_SERIES: ...
        def __mul__(self, other: MPZ | int | FMPZ_POLY | FMPZ_SERIES, /) -> FMPZ_SERIES: ...
        def __rmul__(self, other: MPZ | int | FMPZ_POLY, /) -> FMPZ_SERIES: ...
        def __truediv__(self, other: MPZ | int | FMPZ_POLY | FMPZ_SERIES, /) -> FMPZ_SERIES: ...
        def __rtruediv__(self, other: MPZ | int | FMPZ_POLY, /) -> FMPZ_SERIES: ...
        def __pow__(self, n: int, /) -> FMPZ_SERIES: ...
        def __neg__(self) -> FMPZ_SERIES: ...
        def degree(self) -> int: ...
        def reversion(self) -> FMPZ_SERIES: ...

    class FMPQ_SERIES:
        """Dummy class for type checking: flint.fmpq_series-like type."""
        def __init__(
            self,
            val: FMPQ_SERIES | FMPQ_POLY | FMPZ_SERIES | FMPZ_POLY | Sequence[MPQ | MPZ | int] | MPQ | MPZ | int | None = None,
            *,
            prec: int | None = None
        ) -> None: ...
        @property
        def prec(self) -> int: ...
        def repr(self) -> str: ...
        def __len__(self) -> int: ...
        def __bool__(self) -> bool: ...
        def __getitem__(self, index: int, /) -> MPQ: ...
        def coeffs(self) -> list[MPQ]: ...
        def _equal_repr(self, other: FMPQ_SERIES, /) -> bool: ...
        def __call__(self, other: FMPQ_POLY | FMPQ_SERIES, /) -> FMPQ_SERIES: ...
        def __add__(self, other: MPQ | MPZ | int | FMPQ_POLY | FMPQ_SERIES, /) -> FMPQ_SERIES: ...
        def __radd__(self, other: MPQ | MPZ | int | FMPQ_POLY, /) -> FMPQ_SERIES: ...
        def __sub__(self, other: MPQ | MPZ | int | FMPQ_POLY | FMPQ_SERIES, /) -> FMPQ_SERIES: ...
        def __rsub__(self, other: MPQ | MPZ | int | FMPQ_POLY, /) -> FMPQ_SERIES: ...
        def __mul__(self, other: MPQ | MPZ | int | FMPQ_POLY | FMPQ_SERIES, /) -> FMPQ_SERIES: ...
        def __rmul__(self, other: MPQ | MPZ | int | FMPQ_POLY, /) -> FMPQ_SERIES: ...
        def __truediv__(self, other: MPQ | MPZ | int | FMPQ_POLY | FMPQ_SERIES, /) -> FMPQ_SERIES: ...
        def __rtruediv__(self, other: MPQ | MPZ | int | FMPQ_POLY, /) -> FMPQ_SERIES: ...
        def __pow__(self, n: int, /) -> FMPQ_SERIES: ...
        def __neg__(self) -> FMPQ_SERIES: ...
        def degree(self) -> int: ...
        def sqrt(self) -> FMPQ_SERIES: ...
        def inv(self) -> FMPQ_SERIES: ...
        def integral(self) -> FMPQ_SERIES: ...
        def reversion(self) -> FMPQ_SERIES: ...
        def log(self) -> FMPQ_SERIES: ...
        def exp(self) -> FMPQ_SERIES: ...
        def atan(self) -> FMPQ_SERIES: ...
        def atanh(self) -> FMPQ_SERIES: ...
        def asin(self) -> FMPQ_SERIES: ...
        def asinh(self) -> FMPQ_SERIES: ...
        def tan(self) -> FMPQ_SERIES: ...
        def tanh(self) -> FMPQ_SERIES: ...
        def sin(self) -> FMPQ_SERIES: ...
        def sinh(self) -> FMPQ_SERIES: ...
        def cos(self) -> FMPQ_SERIES: ...
        def cosh(self) -> FMPQ_SERIES: ...

    gmpy: ModuleType | None
    flint: ModuleType | None

    try:
        import gmpy2 as _gmpy2
        gmpy = _gmpy2
    except ImportError:
        gmpy = None

    try:
        import flint as _flint_mod
        flint = _flint_mod
    except ImportError:
        flint = None

    # Reverse-compatibility checks with python-flint stubs: ensure concrete
    # flint polynomial types satisfy our POLY_P protocol.
    try:
        from flint import fmpq as _flint_fmpq
        from flint import fmpq_poly as _flint_fmpq_poly
        from flint import fmpq_series as _flint_fmpq_series
        from flint import fmpz as _flint_fmpz
        from flint import fmpz_mod as _flint_fmpz_mod
        from flint import fmpz_mod_poly as _flint_fmpz_mod_poly
        from flint import fmpz_mod_poly_ctx as _flint_fmpz_mod_poly_ctx
        from flint import fmpz_poly as _flint_fmpz_poly
        from flint import fmpz_series as _flint_fmpz_series
        from flint import nmod as _flint_nmod
        from flint import nmod_poly as _flint_nmod_poly
    except ImportError:
        pass
    else:
        _fmpz_poly: POLY_P[_flint_fmpz] = _flint_fmpz_poly([1, 2])
        _fmpq_poly: POLY_P[_flint_fmpq] = _flint_fmpq_poly([1, 2])
        _nmod_poly: POLY_P[_flint_nmod] = _flint_nmod_poly(1, 2)
        _fmpz_mod_poly: POLY_P[_flint_fmpz_mod] = _flint_fmpz_mod_poly(1, _flint_fmpz_mod_poly_ctx(2))
        _fmpz_series: SERIES_P[_flint_fmpz] = _flint_fmpz_series([1, 2], prec=3)
        _fmpq_series: SERIES_P[_flint_fmpq] = _flint_fmpq_series([1, 2], prec=3)

    # Reverse-compatibility checks for SymPy dummy classes used under
    # TYPE_CHECKING in ringflint and related modules.
    _sympy_fmpz_poly: POLY_P[MPZ] = FMPZ_POLY([1, 2])
    _sympy_fmpq_poly: POLY_P[MPQ] = FMPQ_POLY([1, 2])
    _sympy_nmod_poly: POLY_P[NMOD] = NMOD_POLY([1, 2], 2)
    _sympy_fmpz_mod_poly: POLY_P[FMPZ_MOD] = FMPZ_MOD_POLY([1, 2], FMPZ_MOD_POLY_CTX(2))
    _sympy_fmpz_series: SERIES_P[MPZ] = FMPZ_SERIES([1, 2], prec=3)
    _sympy_fmpq_series: SERIES_P[MPQ] = FMPQ_SERIES([1, 2], prec=3)

    def bit_scan1(x: MPZ | int) -> int: ...
    def bit_scan0(x: MPZ | int) -> int: ...
    def factorial(n: MPZ | int) -> MPZ: ...
    def sqrt(x: MPZ | int) -> MPZ: ...
    def sqrtrem(x: MPZ | int) -> tuple[MPZ, MPZ]: ...

    def gcd(*args: MPZ | int) -> MPZ: ...
    def lcm(*args: MPZ | int) -> MPZ: ...
    def gcdext(x: MPZ | int, y: MPZ | int) -> tuple[MPZ, MPZ, MPZ]: ...

    def invert(x: MPZ | int, y: MPZ | int) -> MPZ: ...
    def remove(x: MPZ | int, y: MPZ | int) -> tuple[MPZ, MPZ]: ...
    def iroot(x: MPZ | int, n: int) -> tuple[MPZ, bool]: ...

    def fibonacci(x: MPZ | int) -> MPZ: ...
    def jacobi(x: MPZ | int, y: MPZ | int) -> int: ...
    def legendre(x: MPZ | int, y: MPZ | int) -> int: ...
    def kronecker(x: MPZ | int, y: MPZ | int) -> int: ...

    def is_square(x: MPZ | int) -> bool: ...
    def is_fermat_prp(n: MPZ | int, a: MPZ | int) -> bool: ...
    def is_euler_prp(n: MPZ | int, a: MPZ | int) -> bool: ...
    def is_strong_prp(x: MPZ | int) -> bool: ...
    def is_fibonacci_prp(x: MPZ | int) -> bool: ...
    def is_lucas_prp(x: MPZ | int) -> bool: ...
    def is_selfridge_prp(x: MPZ | int) -> bool: ...
    def is_strong_lucas_prp(x: MPZ | int) -> bool: ...
    def is_strong_selfridge_prp(x: MPZ | int) -> bool: ...
    def is_bpsw_prp(x: MPZ | int) -> bool: ...
    def is_strong_bpsw_prp(x: MPZ | int) -> bool: ...


elif _SYMPY_GROUND_TYPES == 'gmpy':

    assert _gmpy is not None

    flint = None
    gmpy = _gmpy

    HAS_GMPY = 2
    GROUND_TYPES = 'gmpy'
    SYMPY_INTS = (int, type(gmpy.mpz(0)))
    MPZ = gmpy.mpz
    MPQ = gmpy.mpq

    bit_scan1 = gmpy.bit_scan1
    bit_scan0 = gmpy.bit_scan0
    remove = gmpy.remove
    factorial = gmpy.fac
    sqrt = gmpy.isqrt
    is_square = gmpy.is_square
    sqrtrem = gmpy.isqrt_rem
    gcd = gmpy.gcd
    lcm = gmpy.lcm
    gcdext = gmpy.gcdext
    invert = gmpy.invert
    fibonacci = gmpy.fib
    legendre = gmpy.legendre
    jacobi = gmpy.jacobi
    kronecker = gmpy.kronecker

    def iroot(x, n):
        # In the latest gmpy2, the threshold for n is ULONG_MAX,
        # but adjust to the older one.
        if n <= LONG_MAX:
            return gmpy.iroot(x, n)
        return python_iroot(x, n)

    is_fermat_prp = gmpy.is_fermat_prp
    is_euler_prp = gmpy.is_euler_prp
    is_strong_prp = gmpy.is_strong_prp
    is_fibonacci_prp = gmpy.is_fibonacci_prp
    is_lucas_prp = gmpy.is_lucas_prp
    is_selfridge_prp = gmpy.is_selfridge_prp
    is_strong_lucas_prp = gmpy.is_strong_lucas_prp
    is_strong_selfridge_prp = gmpy.is_strong_selfridge_prp
    is_bpsw_prp = gmpy.is_bpsw_prp
    is_strong_bpsw_prp = gmpy.is_strong_bpsw_prp

elif _SYMPY_GROUND_TYPES == 'flint':

    assert _flint is not None

    flint = _flint
    gmpy = None

    HAS_GMPY = 0
    GROUND_TYPES = 'flint'
    SYMPY_INTS = (int, flint.fmpz) # type: ignore
    MPZ = flint.fmpz # type: ignore
    MPQ = flint.fmpq # type: ignore

    bit_scan1 = python_bit_scan1
    bit_scan0 = python_bit_scan0
    remove = python_remove
    fibonacci = flint.fmpz.fib_ui
    factorial = python_factorial

    def sqrt(x):
        return flint.fmpz(x).isqrt()

    def is_square(x):
        if x < 0:
            return False
        return flint.fmpz(x).sqrtrem()[1] == 0

    def sqrtrem(x):
        return flint.fmpz(x).sqrtrem()

    def gcd(*args):
        return reduce(flint.fmpz.gcd, args, flint.fmpz(0))

    def lcm(*args):
        return reduce(flint.fmpz.lcm, args, flint.fmpz(1))

    gcdext = python_gcdext
    invert = python_invert
    legendre = python_legendre

    def jacobi(x, y):
        if y <= 0 or not y % 2:
            raise ValueError("y should be an odd positive integer")
        return flint.fmpz(x).jacobi(y)

    kronecker = python_kronecker

    def iroot(x, n):
        if n <= LONG_MAX:
            y = flint.fmpz(x).root(n)
            return y, y**n == x
        return python_iroot(x, n)

    is_fermat_prp = python_is_fermat_prp
    is_euler_prp = python_is_euler_prp
    is_strong_prp = python_is_strong_prp
    is_fibonacci_prp = python_is_fibonacci_prp
    is_lucas_prp = python_is_lucas_prp
    is_selfridge_prp = python_is_selfridge_prp
    is_strong_lucas_prp = python_is_strong_lucas_prp
    is_strong_selfridge_prp = python_is_strong_selfridge_prp
    is_bpsw_prp = python_is_bpsw_prp
    is_strong_bpsw_prp = python_is_strong_bpsw_prp

elif _SYMPY_GROUND_TYPES == 'python':

    flint = None
    gmpy = None

    HAS_GMPY = 0
    GROUND_TYPES = 'python'
    SYMPY_INTS = (int,)
    MPZ = int
    MPQ = PythonMPQ

    bit_scan1 = python_bit_scan1
    bit_scan0 = python_bit_scan0
    remove = python_remove
    factorial = python_factorial
    sqrt = python_sqrt
    is_square = python_is_square
    sqrtrem = python_sqrtrem
    gcd = python_gcd
    lcm = python_lcm
    gcdext = python_gcdext
    invert = python_invert
    fibonacci = python_fibonacci
    legendre = python_legendre
    jacobi = python_jacobi
    kronecker = python_kronecker
    iroot = python_iroot
    is_fermat_prp = python_is_fermat_prp
    is_euler_prp = python_is_euler_prp
    is_strong_prp = python_is_strong_prp
    is_fibonacci_prp = python_is_fibonacci_prp
    is_lucas_prp = python_is_lucas_prp
    is_selfridge_prp = python_is_selfridge_prp
    is_strong_lucas_prp = python_is_strong_lucas_prp
    is_strong_selfridge_prp = python_is_strong_selfridge_prp
    is_bpsw_prp = python_is_bpsw_prp
    is_strong_bpsw_prp = python_is_strong_bpsw_prp

else:
    assert False

Messung
CHZG
839710093

¤ Dauer der Verarbeitung: 0.44 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*Bot Zugriff