Spaces:
Running
Running
File size: 6,042 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
"""Implementation of :class:`ModularInteger` class. """
from __future__ import annotations
from typing import Any
import operator
from sympy.polys.polyutils import PicklableWithSlots
from sympy.polys.polyerrors import CoercionFailed
from sympy.polys.domains.domainelement import DomainElement
from sympy.utilities import public
from sympy.utilities.exceptions import sympy_deprecation_warning
@public
class ModularInteger(PicklableWithSlots, DomainElement):
"""A class representing a modular integer. """
mod, dom, sym, _parent = None, None, None, None
__slots__ = ('val',)
def parent(self):
return self._parent
def __init__(self, val):
if isinstance(val, self.__class__):
self.val = val.val % self.mod
else:
self.val = self.dom.convert(val) % self.mod
def modulus(self):
return self.mod
def __hash__(self):
return hash((self.val, self.mod))
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, self.val)
def __str__(self):
return "%s mod %s" % (self.val, self.mod)
def __int__(self):
return int(self.val)
def to_int(self):
sympy_deprecation_warning(
"""ModularInteger.to_int() is deprecated.
Use int(a) or K = GF(p) and K.to_int(a) instead of a.to_int().
""",
deprecated_since_version="1.13",
active_deprecations_target="modularinteger-to-int",
)
if self.sym:
if self.val <= self.mod // 2:
return self.val
else:
return self.val - self.mod
else:
return self.val
def __pos__(self):
return self
def __neg__(self):
return self.__class__(-self.val)
@classmethod
def _get_val(cls, other):
if isinstance(other, cls):
return other.val
else:
try:
return cls.dom.convert(other)
except CoercionFailed:
return None
def __add__(self, other):
val = self._get_val(other)
if val is not None:
return self.__class__(self.val + val)
else:
return NotImplemented
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
val = self._get_val(other)
if val is not None:
return self.__class__(self.val - val)
else:
return NotImplemented
def __rsub__(self, other):
return (-self).__add__(other)
def __mul__(self, other):
val = self._get_val(other)
if val is not None:
return self.__class__(self.val * val)
else:
return NotImplemented
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
val = self._get_val(other)
if val is not None:
return self.__class__(self.val * self._invert(val))
else:
return NotImplemented
def __rtruediv__(self, other):
return self.invert().__mul__(other)
def __mod__(self, other):
val = self._get_val(other)
if val is not None:
return self.__class__(self.val % val)
else:
return NotImplemented
def __rmod__(self, other):
val = self._get_val(other)
if val is not None:
return self.__class__(val % self.val)
else:
return NotImplemented
def __pow__(self, exp):
if not exp:
return self.__class__(self.dom.one)
if exp < 0:
val, exp = self.invert().val, -exp
else:
val = self.val
return self.__class__(pow(val, int(exp), self.mod))
def _compare(self, other, op):
val = self._get_val(other)
if val is None:
return NotImplemented
return op(self.val, val % self.mod)
def _compare_deprecated(self, other, op):
val = self._get_val(other)
if val is None:
return NotImplemented
sympy_deprecation_warning(
"""Ordered comparisons with modular integers are deprecated.
Use e.g. int(a) < int(b) instead of a < b.
""",
deprecated_since_version="1.13",
active_deprecations_target="modularinteger-compare",
stacklevel=4,
)
return op(self.val, val % self.mod)
def __eq__(self, other):
return self._compare(other, operator.eq)
def __ne__(self, other):
return self._compare(other, operator.ne)
def __lt__(self, other):
return self._compare_deprecated(other, operator.lt)
def __le__(self, other):
return self._compare_deprecated(other, operator.le)
def __gt__(self, other):
return self._compare_deprecated(other, operator.gt)
def __ge__(self, other):
return self._compare_deprecated(other, operator.ge)
def __bool__(self):
return bool(self.val)
@classmethod
def _invert(cls, value):
return cls.dom.invert(value, cls.mod)
def invert(self):
return self.__class__(self._invert(self.val))
_modular_integer_cache: dict[tuple[Any, Any, Any], type[ModularInteger]] = {}
def ModularIntegerFactory(_mod, _dom, _sym, parent):
"""Create custom class for specific integer modulus."""
try:
_mod = _dom.convert(_mod)
except CoercionFailed:
ok = False
else:
ok = True
if not ok or _mod < 1:
raise ValueError("modulus must be a positive integer, got %s" % _mod)
key = _mod, _dom, _sym
try:
cls = _modular_integer_cache[key]
except KeyError:
class cls(ModularInteger):
mod, dom, sym = _mod, _dom, _sym
_parent = parent
if _sym:
cls.__name__ = "SymmetricModularIntegerMod%s" % _mod
else:
cls.__name__ = "ModularIntegerMod%s" % _mod
_modular_integer_cache[key] = cls
return cls
|