File size: 6,085 Bytes
ceb378e |
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 |
"""Implementation of :class:`IntegerRing` class. """
from sympy.external.gmpy import MPZ, HAS_GMPY
from sympy.polys.domains.groundtypes import (
SymPyInteger,
factorial,
gcdex, gcd, lcm, sqrt,
)
from sympy.polys.domains.characteristiczero import CharacteristicZero
from sympy.polys.domains.ring import Ring
from sympy.polys.domains.simpledomain import SimpleDomain
from sympy.polys.polyerrors import CoercionFailed
from sympy.utilities import public
import math
@public
class IntegerRing(Ring, CharacteristicZero, SimpleDomain):
r"""The domain ``ZZ`` representing the integers `\mathbb{Z}`.
The :py:class:`IntegerRing` class represents the ring of integers as a
:py:class:`~.Domain` in the domain system. :py:class:`IntegerRing` is a
super class of :py:class:`PythonIntegerRing` and
:py:class:`GMPYIntegerRing` one of which will be the implementation for
:ref:`ZZ` depending on whether or not ``gmpy`` or ``gmpy2`` is installed.
See also
========
Domain
"""
rep = 'ZZ'
alias = 'ZZ'
dtype = MPZ
zero = dtype(0)
one = dtype(1)
tp = type(one)
is_IntegerRing = is_ZZ = True
is_Numerical = True
is_PID = True
has_assoc_Ring = True
has_assoc_Field = True
def __init__(self):
"""Allow instantiation of this domain. """
def to_sympy(self, a):
"""Convert ``a`` to a SymPy object. """
return SymPyInteger(int(a))
def from_sympy(self, a):
"""Convert SymPy's Integer to ``dtype``. """
if a.is_Integer:
return MPZ(a.p)
elif a.is_Float and int(a) == a:
return MPZ(int(a))
else:
raise CoercionFailed("expected an integer, got %s" % a)
def get_field(self):
r"""Return the associated field of fractions :ref:`QQ`
Returns
=======
:ref:`QQ`:
The associated field of fractions :ref:`QQ`, a
:py:class:`~.Domain` representing the rational numbers
`\mathbb{Q}`.
Examples
========
>>> from sympy import ZZ
>>> ZZ.get_field()
QQ
"""
from sympy.polys.domains import QQ
return QQ
def algebraic_field(self, *extension, alias=None):
r"""Returns an algebraic field, i.e. `\mathbb{Q}(\alpha, \ldots)`.
Parameters
==========
*extension : One or more :py:class:`~.Expr`.
Generators of the extension. These should be expressions that are
algebraic over `\mathbb{Q}`.
alias : str, :py:class:`~.Symbol`, None, optional (default=None)
If provided, this will be used as the alias symbol for the
primitive element of the returned :py:class:`~.AlgebraicField`.
Returns
=======
:py:class:`~.AlgebraicField`
A :py:class:`~.Domain` representing the algebraic field extension.
Examples
========
>>> from sympy import ZZ, sqrt
>>> ZZ.algebraic_field(sqrt(2))
QQ<sqrt(2)>
"""
return self.get_field().algebraic_field(*extension, alias=alias)
def from_AlgebraicField(K1, a, K0):
"""Convert a :py:class:`~.ANP` object to :ref:`ZZ`.
See :py:meth:`~.Domain.convert`.
"""
if a.is_ground:
return K1.convert(a.LC(), K0.dom)
def log(self, a, b):
r"""Logarithm of *a* to the base *b*.
Parameters
==========
a: number
b: number
Returns
=======
$\\lfloor\log(a, b)\\rfloor$:
Floor of the logarithm of *a* to the base *b*
Examples
========
>>> from sympy import ZZ
>>> ZZ.log(ZZ(8), ZZ(2))
3
>>> ZZ.log(ZZ(9), ZZ(2))
3
Notes
=====
This function uses ``math.log`` which is based on ``float`` so it will
fail for large integer arguments.
"""
return self.dtype(math.log(int(a), b))
def from_FF(K1, a, K0):
"""Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
return MPZ(a.to_int())
def from_FF_python(K1, a, K0):
"""Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
return MPZ(a.to_int())
def from_ZZ(K1, a, K0):
"""Convert Python's ``int`` to GMPY's ``mpz``. """
return MPZ(a)
def from_ZZ_python(K1, a, K0):
"""Convert Python's ``int`` to GMPY's ``mpz``. """
return MPZ(a)
def from_QQ(K1, a, K0):
"""Convert Python's ``Fraction`` to GMPY's ``mpz``. """
if a.denominator == 1:
return MPZ(a.numerator)
def from_QQ_python(K1, a, K0):
"""Convert Python's ``Fraction`` to GMPY's ``mpz``. """
if a.denominator == 1:
return MPZ(a.numerator)
def from_FF_gmpy(K1, a, K0):
"""Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. """
return a.to_int()
def from_ZZ_gmpy(K1, a, K0):
"""Convert GMPY's ``mpz`` to GMPY's ``mpz``. """
return a
def from_QQ_gmpy(K1, a, K0):
"""Convert GMPY ``mpq`` to GMPY's ``mpz``. """
if a.denominator == 1:
return a.numerator
def from_RealField(K1, a, K0):
"""Convert mpmath's ``mpf`` to GMPY's ``mpz``. """
p, q = K0.to_rational(a)
if q == 1:
return MPZ(p)
def from_GaussianIntegerRing(K1, a, K0):
if a.y == 0:
return a.x
def gcdex(self, a, b):
"""Compute extended GCD of ``a`` and ``b``. """
h, s, t = gcdex(a, b)
if HAS_GMPY:
return s, t, h
else:
return h, s, t
def gcd(self, a, b):
"""Compute GCD of ``a`` and ``b``. """
return gcd(a, b)
def lcm(self, a, b):
"""Compute LCM of ``a`` and ``b``. """
return lcm(a, b)
def sqrt(self, a):
"""Compute square root of ``a``. """
return sqrt(a)
def factorial(self, a):
"""Compute factorial of ``a``. """
return factorial(a)
ZZ = IntegerRing()
|