Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- ckpts/universal/global_step80/zero/22.post_attention_layernorm.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/22.post_attention_layernorm.weight/fp32.pt +3 -0
- ckpts/universal/global_step80/zero/6.mlp.dense_h_to_4h.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/6.mlp.dense_h_to_4h.weight/fp32.pt +3 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__init__.py +57 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/characteristiczero.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/complexfield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/compositedomain.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/domainelement.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/expressiondomain.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/expressionrawdomain.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/field.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/finitefield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/fractionfield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gmpyfinitefield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gmpyintegerring.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gmpyrationalfield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/groundtypes.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/integerring.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/modularinteger.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/mpelements.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_fractionfield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_polynomialring.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonfinitefield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonintegerring.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonrationalfield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/realfield.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/ring.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/simpledomain.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/algebraicfield.py +605 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/characteristiczero.py +15 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/complexfield.py +151 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/compositedomain.py +32 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/domain.py +1304 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/domainelement.py +38 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/expressiondomain.py +254 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/expressionrawdomain.py +57 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/field.py +104 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/finitefield.py +206 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/fractionfield.py +184 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/gaussiandomains.py +645 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/gmpyfinitefield.py +16 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/gmpyintegerring.py +104 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/gmpyrationalfield.py +100 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/groundtypes.py +73 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/integerring.py +231 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/modularinteger.py +205 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/mpelements.py +172 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/old_fractionfield.py +185 -0
- venv/lib/python3.10/site-packages/sympy/polys/domains/old_polynomialring.py +462 -0
ckpts/universal/global_step80/zero/22.post_attention_layernorm.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:441abfa43e2c1e93175b97964a1c6bcc84b91862dcd146b3db1535d627004a96
|
3 |
+
size 9372
|
ckpts/universal/global_step80/zero/22.post_attention_layernorm.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b5064e2b9250a23009be259a8bf16c13c7d6af4897748888e3cd3a3bb261aaa8
|
3 |
+
size 9293
|
ckpts/universal/global_step80/zero/6.mlp.dense_h_to_4h.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a3eb456bc1da38aa2f00da526f3550bef1e265c8a8128179981353250ccb3f3a
|
3 |
+
size 33555612
|
ckpts/universal/global_step80/zero/6.mlp.dense_h_to_4h.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9fd363f771318c639c7078fa215ff32c9f953ed4517280f03aebadca4e48c45f
|
3 |
+
size 33555533
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__init__.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of mathematical domains. """
|
2 |
+
|
3 |
+
__all__ = [
|
4 |
+
'Domain', 'FiniteField', 'IntegerRing', 'RationalField', 'RealField',
|
5 |
+
'ComplexField', 'AlgebraicField', 'PolynomialRing', 'FractionField',
|
6 |
+
'ExpressionDomain', 'PythonRational',
|
7 |
+
|
8 |
+
'GF', 'FF', 'ZZ', 'QQ', 'ZZ_I', 'QQ_I', 'RR', 'CC', 'EX', 'EXRAW',
|
9 |
+
]
|
10 |
+
|
11 |
+
from .domain import Domain
|
12 |
+
from .finitefield import FiniteField, FF, GF
|
13 |
+
from .integerring import IntegerRing, ZZ
|
14 |
+
from .rationalfield import RationalField, QQ
|
15 |
+
from .algebraicfield import AlgebraicField
|
16 |
+
from .gaussiandomains import ZZ_I, QQ_I
|
17 |
+
from .realfield import RealField, RR
|
18 |
+
from .complexfield import ComplexField, CC
|
19 |
+
from .polynomialring import PolynomialRing
|
20 |
+
from .fractionfield import FractionField
|
21 |
+
from .expressiondomain import ExpressionDomain, EX
|
22 |
+
from .expressionrawdomain import EXRAW
|
23 |
+
from .pythonrational import PythonRational
|
24 |
+
|
25 |
+
|
26 |
+
# This is imported purely for backwards compatibility because some parts of
|
27 |
+
# the codebase used to import this from here and it's possible that downstream
|
28 |
+
# does as well:
|
29 |
+
from sympy.external.gmpy import GROUND_TYPES # noqa: F401
|
30 |
+
|
31 |
+
#
|
32 |
+
# The rest of these are obsolete and provided only for backwards
|
33 |
+
# compatibility:
|
34 |
+
#
|
35 |
+
|
36 |
+
from .pythonfinitefield import PythonFiniteField
|
37 |
+
from .gmpyfinitefield import GMPYFiniteField
|
38 |
+
from .pythonintegerring import PythonIntegerRing
|
39 |
+
from .gmpyintegerring import GMPYIntegerRing
|
40 |
+
from .pythonrationalfield import PythonRationalField
|
41 |
+
from .gmpyrationalfield import GMPYRationalField
|
42 |
+
|
43 |
+
FF_python = PythonFiniteField
|
44 |
+
FF_gmpy = GMPYFiniteField
|
45 |
+
|
46 |
+
ZZ_python = PythonIntegerRing
|
47 |
+
ZZ_gmpy = GMPYIntegerRing
|
48 |
+
|
49 |
+
QQ_python = PythonRationalField
|
50 |
+
QQ_gmpy = GMPYRationalField
|
51 |
+
|
52 |
+
__all__.extend((
|
53 |
+
'PythonFiniteField', 'GMPYFiniteField', 'PythonIntegerRing',
|
54 |
+
'GMPYIntegerRing', 'PythonRational', 'GMPYRationalField',
|
55 |
+
|
56 |
+
'FF_python', 'FF_gmpy', 'ZZ_python', 'ZZ_gmpy', 'QQ_python', 'QQ_gmpy',
|
57 |
+
))
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/characteristiczero.cpython-310.pyc
ADDED
Binary file (822 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/complexfield.cpython-310.pyc
ADDED
Binary file (6.21 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/compositedomain.cpython-310.pyc
ADDED
Binary file (1.47 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/domainelement.cpython-310.pyc
ADDED
Binary file (1.28 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/expressiondomain.cpython-310.pyc
ADDED
Binary file (9.94 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/expressionrawdomain.cpython-310.pyc
ADDED
Binary file (2.34 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/field.cpython-310.pyc
ADDED
Binary file (3.28 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/finitefield.cpython-310.pyc
ADDED
Binary file (6.94 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/fractionfield.cpython-310.pyc
ADDED
Binary file (7.16 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gmpyfinitefield.cpython-310.pyc
ADDED
Binary file (898 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gmpyintegerring.cpython-310.pyc
ADDED
Binary file (4.32 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gmpyrationalfield.cpython-310.pyc
ADDED
Binary file (4.48 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/groundtypes.cpython-310.pyc
ADDED
Binary file (1.71 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/integerring.cpython-310.pyc
ADDED
Binary file (7.6 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/modularinteger.cpython-310.pyc
ADDED
Binary file (6.63 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/mpelements.cpython-310.pyc
ADDED
Binary file (5.07 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_fractionfield.cpython-310.pyc
ADDED
Binary file (7.51 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_polynomialring.cpython-310.pyc
ADDED
Binary file (17.6 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonfinitefield.cpython-310.pyc
ADDED
Binary file (916 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonintegerring.cpython-310.pyc
ADDED
Binary file (4.14 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonrationalfield.cpython-310.pyc
ADDED
Binary file (3.31 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/realfield.cpython-310.pyc
ADDED
Binary file (5.2 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/ring.cpython-310.pyc
ADDED
Binary file (4.24 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/simpledomain.cpython-310.pyc
ADDED
Binary file (777 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/polys/domains/algebraicfield.py
ADDED
@@ -0,0 +1,605 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`AlgebraicField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.core.add import Add
|
5 |
+
from sympy.core.mul import Mul
|
6 |
+
from sympy.core.singleton import S
|
7 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
8 |
+
from sympy.polys.domains.field import Field
|
9 |
+
from sympy.polys.domains.simpledomain import SimpleDomain
|
10 |
+
from sympy.polys.polyclasses import ANP
|
11 |
+
from sympy.polys.polyerrors import CoercionFailed, DomainError, NotAlgebraic, IsomorphismFailed
|
12 |
+
from sympy.utilities import public
|
13 |
+
|
14 |
+
@public
|
15 |
+
class AlgebraicField(Field, CharacteristicZero, SimpleDomain):
|
16 |
+
r"""Algebraic number field :ref:`QQ(a)`
|
17 |
+
|
18 |
+
A :ref:`QQ(a)` domain represents an `algebraic number field`_
|
19 |
+
`\mathbb{Q}(a)` as a :py:class:`~.Domain` in the domain system (see
|
20 |
+
:ref:`polys-domainsintro`).
|
21 |
+
|
22 |
+
A :py:class:`~.Poly` created from an expression involving `algebraic
|
23 |
+
numbers`_ will treat the algebraic numbers as generators if the generators
|
24 |
+
argument is not specified.
|
25 |
+
|
26 |
+
>>> from sympy import Poly, Symbol, sqrt
|
27 |
+
>>> x = Symbol('x')
|
28 |
+
>>> Poly(x**2 + sqrt(2))
|
29 |
+
Poly(x**2 + (sqrt(2)), x, sqrt(2), domain='ZZ')
|
30 |
+
|
31 |
+
That is a multivariate polynomial with ``sqrt(2)`` treated as one of the
|
32 |
+
generators (variables). If the generators are explicitly specified then
|
33 |
+
``sqrt(2)`` will be considered to be a coefficient but by default the
|
34 |
+
:ref:`EX` domain is used. To make a :py:class:`~.Poly` with a :ref:`QQ(a)`
|
35 |
+
domain the argument ``extension=True`` can be given.
|
36 |
+
|
37 |
+
>>> Poly(x**2 + sqrt(2), x)
|
38 |
+
Poly(x**2 + sqrt(2), x, domain='EX')
|
39 |
+
>>> Poly(x**2 + sqrt(2), x, extension=True)
|
40 |
+
Poly(x**2 + sqrt(2), x, domain='QQ<sqrt(2)>')
|
41 |
+
|
42 |
+
A generator of the algebraic field extension can also be specified
|
43 |
+
explicitly which is particularly useful if the coefficients are all
|
44 |
+
rational but an extension field is needed (e.g. to factor the
|
45 |
+
polynomial).
|
46 |
+
|
47 |
+
>>> Poly(x**2 + 1)
|
48 |
+
Poly(x**2 + 1, x, domain='ZZ')
|
49 |
+
>>> Poly(x**2 + 1, extension=sqrt(2))
|
50 |
+
Poly(x**2 + 1, x, domain='QQ<sqrt(2)>')
|
51 |
+
|
52 |
+
It is possible to factorise a polynomial over a :ref:`QQ(a)` domain using
|
53 |
+
the ``extension`` argument to :py:func:`~.factor` or by specifying the domain
|
54 |
+
explicitly.
|
55 |
+
|
56 |
+
>>> from sympy import factor, QQ
|
57 |
+
>>> factor(x**2 - 2)
|
58 |
+
x**2 - 2
|
59 |
+
>>> factor(x**2 - 2, extension=sqrt(2))
|
60 |
+
(x - sqrt(2))*(x + sqrt(2))
|
61 |
+
>>> factor(x**2 - 2, domain='QQ<sqrt(2)>')
|
62 |
+
(x - sqrt(2))*(x + sqrt(2))
|
63 |
+
>>> factor(x**2 - 2, domain=QQ.algebraic_field(sqrt(2)))
|
64 |
+
(x - sqrt(2))*(x + sqrt(2))
|
65 |
+
|
66 |
+
The ``extension=True`` argument can be used but will only create an
|
67 |
+
extension that contains the coefficients which is usually not enough to
|
68 |
+
factorise the polynomial.
|
69 |
+
|
70 |
+
>>> p = x**3 + sqrt(2)*x**2 - 2*x - 2*sqrt(2)
|
71 |
+
>>> factor(p) # treats sqrt(2) as a symbol
|
72 |
+
(x + sqrt(2))*(x**2 - 2)
|
73 |
+
>>> factor(p, extension=True)
|
74 |
+
(x - sqrt(2))*(x + sqrt(2))**2
|
75 |
+
>>> factor(x**2 - 2, extension=True) # all rational coefficients
|
76 |
+
x**2 - 2
|
77 |
+
|
78 |
+
It is also possible to use :ref:`QQ(a)` with the :py:func:`~.cancel`
|
79 |
+
and :py:func:`~.gcd` functions.
|
80 |
+
|
81 |
+
>>> from sympy import cancel, gcd
|
82 |
+
>>> cancel((x**2 - 2)/(x - sqrt(2)))
|
83 |
+
(x**2 - 2)/(x - sqrt(2))
|
84 |
+
>>> cancel((x**2 - 2)/(x - sqrt(2)), extension=sqrt(2))
|
85 |
+
x + sqrt(2)
|
86 |
+
>>> gcd(x**2 - 2, x - sqrt(2))
|
87 |
+
1
|
88 |
+
>>> gcd(x**2 - 2, x - sqrt(2), extension=sqrt(2))
|
89 |
+
x - sqrt(2)
|
90 |
+
|
91 |
+
When using the domain directly :ref:`QQ(a)` can be used as a constructor
|
92 |
+
to create instances which then support the operations ``+,-,*,**,/``. The
|
93 |
+
:py:meth:`~.Domain.algebraic_field` method is used to construct a
|
94 |
+
particular :ref:`QQ(a)` domain. The :py:meth:`~.Domain.from_sympy` method
|
95 |
+
can be used to create domain elements from normal SymPy expressions.
|
96 |
+
|
97 |
+
>>> K = QQ.algebraic_field(sqrt(2))
|
98 |
+
>>> K
|
99 |
+
QQ<sqrt(2)>
|
100 |
+
>>> xk = K.from_sympy(3 + 4*sqrt(2))
|
101 |
+
>>> xk # doctest: +SKIP
|
102 |
+
ANP([4, 3], [1, 0, -2], QQ)
|
103 |
+
|
104 |
+
Elements of :ref:`QQ(a)` are instances of :py:class:`~.ANP` which have
|
105 |
+
limited printing support. The raw display shows the internal
|
106 |
+
representation of the element as the list ``[4, 3]`` representing the
|
107 |
+
coefficients of ``1`` and ``sqrt(2)`` for this element in the form
|
108 |
+
``a * sqrt(2) + b * 1`` where ``a`` and ``b`` are elements of :ref:`QQ`.
|
109 |
+
The minimal polynomial for the generator ``(x**2 - 2)`` is also shown in
|
110 |
+
the :ref:`dup-representation` as the list ``[1, 0, -2]``. We can use
|
111 |
+
:py:meth:`~.Domain.to_sympy` to get a better printed form for the
|
112 |
+
elements and to see the results of operations.
|
113 |
+
|
114 |
+
>>> xk = K.from_sympy(3 + 4*sqrt(2))
|
115 |
+
>>> yk = K.from_sympy(2 + 3*sqrt(2))
|
116 |
+
>>> xk * yk # doctest: +SKIP
|
117 |
+
ANP([17, 30], [1, 0, -2], QQ)
|
118 |
+
>>> K.to_sympy(xk * yk)
|
119 |
+
17*sqrt(2) + 30
|
120 |
+
>>> K.to_sympy(xk + yk)
|
121 |
+
5 + 7*sqrt(2)
|
122 |
+
>>> K.to_sympy(xk ** 2)
|
123 |
+
24*sqrt(2) + 41
|
124 |
+
>>> K.to_sympy(xk / yk)
|
125 |
+
sqrt(2)/14 + 9/7
|
126 |
+
|
127 |
+
Any expression representing an algebraic number can be used to generate
|
128 |
+
a :ref:`QQ(a)` domain provided its `minimal polynomial`_ can be computed.
|
129 |
+
The function :py:func:`~.minpoly` function is used for this.
|
130 |
+
|
131 |
+
>>> from sympy import exp, I, pi, minpoly
|
132 |
+
>>> g = exp(2*I*pi/3)
|
133 |
+
>>> g
|
134 |
+
exp(2*I*pi/3)
|
135 |
+
>>> g.is_algebraic
|
136 |
+
True
|
137 |
+
>>> minpoly(g, x)
|
138 |
+
x**2 + x + 1
|
139 |
+
>>> factor(x**3 - 1, extension=g)
|
140 |
+
(x - 1)*(x - exp(2*I*pi/3))*(x + 1 + exp(2*I*pi/3))
|
141 |
+
|
142 |
+
It is also possible to make an algebraic field from multiple extension
|
143 |
+
elements.
|
144 |
+
|
145 |
+
>>> K = QQ.algebraic_field(sqrt(2), sqrt(3))
|
146 |
+
>>> K
|
147 |
+
QQ<sqrt(2) + sqrt(3)>
|
148 |
+
>>> p = x**4 - 5*x**2 + 6
|
149 |
+
>>> factor(p)
|
150 |
+
(x**2 - 3)*(x**2 - 2)
|
151 |
+
>>> factor(p, domain=K)
|
152 |
+
(x - sqrt(2))*(x + sqrt(2))*(x - sqrt(3))*(x + sqrt(3))
|
153 |
+
>>> factor(p, extension=[sqrt(2), sqrt(3)])
|
154 |
+
(x - sqrt(2))*(x + sqrt(2))*(x - sqrt(3))*(x + sqrt(3))
|
155 |
+
|
156 |
+
Multiple extension elements are always combined together to make a single
|
157 |
+
`primitive element`_. In the case of ``[sqrt(2), sqrt(3)]`` the primitive
|
158 |
+
element chosen is ``sqrt(2) + sqrt(3)`` which is why the domain displays
|
159 |
+
as ``QQ<sqrt(2) + sqrt(3)>``. The minimal polynomial for the primitive
|
160 |
+
element is computed using the :py:func:`~.primitive_element` function.
|
161 |
+
|
162 |
+
>>> from sympy import primitive_element
|
163 |
+
>>> primitive_element([sqrt(2), sqrt(3)], x)
|
164 |
+
(x**4 - 10*x**2 + 1, [1, 1])
|
165 |
+
>>> minpoly(sqrt(2) + sqrt(3), x)
|
166 |
+
x**4 - 10*x**2 + 1
|
167 |
+
|
168 |
+
The extension elements that generate the domain can be accessed from the
|
169 |
+
domain using the :py:attr:`~.ext` and :py:attr:`~.orig_ext` attributes as
|
170 |
+
instances of :py:class:`~.AlgebraicNumber`. The minimal polynomial for
|
171 |
+
the primitive element as a :py:class:`~.DMP` instance is available as
|
172 |
+
:py:attr:`~.mod`.
|
173 |
+
|
174 |
+
>>> K = QQ.algebraic_field(sqrt(2), sqrt(3))
|
175 |
+
>>> K
|
176 |
+
QQ<sqrt(2) + sqrt(3)>
|
177 |
+
>>> K.ext
|
178 |
+
sqrt(2) + sqrt(3)
|
179 |
+
>>> K.orig_ext
|
180 |
+
(sqrt(2), sqrt(3))
|
181 |
+
>>> K.mod
|
182 |
+
DMP([1, 0, -10, 0, 1], QQ, None)
|
183 |
+
|
184 |
+
The `discriminant`_ of the field can be obtained from the
|
185 |
+
:py:meth:`~.discriminant` method, and an `integral basis`_ from the
|
186 |
+
:py:meth:`~.integral_basis` method. The latter returns a list of
|
187 |
+
:py:class:`~.ANP` instances by default, but can be made to return instances
|
188 |
+
of :py:class:`~.Expr` or :py:class:`~.AlgebraicNumber` by passing a ``fmt``
|
189 |
+
argument. The maximal order, or ring of integers, of the field can also be
|
190 |
+
obtained from the :py:meth:`~.maximal_order` method, as a
|
191 |
+
:py:class:`~sympy.polys.numberfields.modules.Submodule`.
|
192 |
+
|
193 |
+
>>> zeta5 = exp(2*I*pi/5)
|
194 |
+
>>> K = QQ.algebraic_field(zeta5)
|
195 |
+
>>> K
|
196 |
+
QQ<exp(2*I*pi/5)>
|
197 |
+
>>> K.discriminant()
|
198 |
+
125
|
199 |
+
>>> K = QQ.algebraic_field(sqrt(5))
|
200 |
+
>>> K
|
201 |
+
QQ<sqrt(5)>
|
202 |
+
>>> K.integral_basis(fmt='sympy')
|
203 |
+
[1, 1/2 + sqrt(5)/2]
|
204 |
+
>>> K.maximal_order()
|
205 |
+
Submodule[[2, 0], [1, 1]]/2
|
206 |
+
|
207 |
+
The factorization of a rational prime into prime ideals of the field is
|
208 |
+
computed by the :py:meth:`~.primes_above` method, which returns a list
|
209 |
+
of :py:class:`~sympy.polys.numberfields.primes.PrimeIdeal` instances.
|
210 |
+
|
211 |
+
>>> zeta7 = exp(2*I*pi/7)
|
212 |
+
>>> K = QQ.algebraic_field(zeta7)
|
213 |
+
>>> K
|
214 |
+
QQ<exp(2*I*pi/7)>
|
215 |
+
>>> K.primes_above(11)
|
216 |
+
[(11, _x**3 + 5*_x**2 + 4*_x - 1), (11, _x**3 - 4*_x**2 - 5*_x - 1)]
|
217 |
+
|
218 |
+
The Galois group of the Galois closure of the field can be computed (when
|
219 |
+
the minimal polynomial of the field is of sufficiently small degree).
|
220 |
+
|
221 |
+
>>> K.galois_group(by_name=True)[0]
|
222 |
+
S6TransitiveSubgroups.C6
|
223 |
+
|
224 |
+
Notes
|
225 |
+
=====
|
226 |
+
|
227 |
+
It is not currently possible to generate an algebraic extension over any
|
228 |
+
domain other than :ref:`QQ`. Ideally it would be possible to generate
|
229 |
+
extensions like ``QQ(x)(sqrt(x**2 - 2))``. This is equivalent to the
|
230 |
+
quotient ring ``QQ(x)[y]/(y**2 - x**2 + 2)`` and there are two
|
231 |
+
implementations of this kind of quotient ring/extension in the
|
232 |
+
:py:class:`~.QuotientRing` and :py:class:`~.MonogenicFiniteExtension`
|
233 |
+
classes. Each of those implementations needs some work to make them fully
|
234 |
+
usable though.
|
235 |
+
|
236 |
+
.. _algebraic number field: https://en.wikipedia.org/wiki/Algebraic_number_field
|
237 |
+
.. _algebraic numbers: https://en.wikipedia.org/wiki/Algebraic_number
|
238 |
+
.. _discriminant: https://en.wikipedia.org/wiki/Discriminant_of_an_algebraic_number_field
|
239 |
+
.. _integral basis: https://en.wikipedia.org/wiki/Algebraic_number_field#Integral_basis
|
240 |
+
.. _minimal polynomial: https://en.wikipedia.org/wiki/Minimal_polynomial_(field_theory)
|
241 |
+
.. _primitive element: https://en.wikipedia.org/wiki/Primitive_element_theorem
|
242 |
+
"""
|
243 |
+
|
244 |
+
dtype = ANP
|
245 |
+
|
246 |
+
is_AlgebraicField = is_Algebraic = True
|
247 |
+
is_Numerical = True
|
248 |
+
|
249 |
+
has_assoc_Ring = False
|
250 |
+
has_assoc_Field = True
|
251 |
+
|
252 |
+
def __init__(self, dom, *ext, alias=None):
|
253 |
+
r"""
|
254 |
+
Parameters
|
255 |
+
==========
|
256 |
+
|
257 |
+
dom : :py:class:`~.Domain`
|
258 |
+
The base field over which this is an extension field.
|
259 |
+
Currently only :ref:`QQ` is accepted.
|
260 |
+
|
261 |
+
*ext : One or more :py:class:`~.Expr`
|
262 |
+
Generators of the extension. These should be expressions that are
|
263 |
+
algebraic over `\mathbb{Q}`.
|
264 |
+
|
265 |
+
alias : str, :py:class:`~.Symbol`, None, optional (default=None)
|
266 |
+
If provided, this will be used as the alias symbol for the
|
267 |
+
primitive element of the :py:class:`~.AlgebraicField`.
|
268 |
+
If ``None``, while ``ext`` consists of exactly one
|
269 |
+
:py:class:`~.AlgebraicNumber`, its alias (if any) will be used.
|
270 |
+
"""
|
271 |
+
if not dom.is_QQ:
|
272 |
+
raise DomainError("ground domain must be a rational field")
|
273 |
+
|
274 |
+
from sympy.polys.numberfields import to_number_field
|
275 |
+
if len(ext) == 1 and isinstance(ext[0], tuple):
|
276 |
+
orig_ext = ext[0][1:]
|
277 |
+
else:
|
278 |
+
orig_ext = ext
|
279 |
+
|
280 |
+
if alias is None and len(ext) == 1:
|
281 |
+
alias = getattr(ext[0], 'alias', None)
|
282 |
+
|
283 |
+
self.orig_ext = orig_ext
|
284 |
+
"""
|
285 |
+
Original elements given to generate the extension.
|
286 |
+
|
287 |
+
>>> from sympy import QQ, sqrt
|
288 |
+
>>> K = QQ.algebraic_field(sqrt(2), sqrt(3))
|
289 |
+
>>> K.orig_ext
|
290 |
+
(sqrt(2), sqrt(3))
|
291 |
+
"""
|
292 |
+
|
293 |
+
self.ext = to_number_field(ext, alias=alias)
|
294 |
+
"""
|
295 |
+
Primitive element used for the extension.
|
296 |
+
|
297 |
+
>>> from sympy import QQ, sqrt
|
298 |
+
>>> K = QQ.algebraic_field(sqrt(2), sqrt(3))
|
299 |
+
>>> K.ext
|
300 |
+
sqrt(2) + sqrt(3)
|
301 |
+
"""
|
302 |
+
|
303 |
+
self.mod = self.ext.minpoly.rep
|
304 |
+
"""
|
305 |
+
Minimal polynomial for the primitive element of the extension.
|
306 |
+
|
307 |
+
>>> from sympy import QQ, sqrt
|
308 |
+
>>> K = QQ.algebraic_field(sqrt(2))
|
309 |
+
>>> K.mod
|
310 |
+
DMP([1, 0, -2], QQ, None)
|
311 |
+
"""
|
312 |
+
|
313 |
+
self.domain = self.dom = dom
|
314 |
+
|
315 |
+
self.ngens = 1
|
316 |
+
self.symbols = self.gens = (self.ext,)
|
317 |
+
self.unit = self([dom(1), dom(0)])
|
318 |
+
|
319 |
+
self.zero = self.dtype.zero(self.mod.rep, dom)
|
320 |
+
self.one = self.dtype.one(self.mod.rep, dom)
|
321 |
+
|
322 |
+
self._maximal_order = None
|
323 |
+
self._discriminant = None
|
324 |
+
self._nilradicals_mod_p = {}
|
325 |
+
|
326 |
+
def new(self, element):
|
327 |
+
return self.dtype(element, self.mod.rep, self.dom)
|
328 |
+
|
329 |
+
def __str__(self):
|
330 |
+
return str(self.dom) + '<' + str(self.ext) + '>'
|
331 |
+
|
332 |
+
def __hash__(self):
|
333 |
+
return hash((self.__class__.__name__, self.dtype, self.dom, self.ext))
|
334 |
+
|
335 |
+
def __eq__(self, other):
|
336 |
+
"""Returns ``True`` if two domains are equivalent. """
|
337 |
+
return isinstance(other, AlgebraicField) and \
|
338 |
+
self.dtype == other.dtype and self.ext == other.ext
|
339 |
+
|
340 |
+
def algebraic_field(self, *extension, alias=None):
|
341 |
+
r"""Returns an algebraic field, i.e. `\mathbb{Q}(\alpha, \ldots)`. """
|
342 |
+
return AlgebraicField(self.dom, *((self.ext,) + extension), alias=alias)
|
343 |
+
|
344 |
+
def to_alg_num(self, a):
|
345 |
+
"""Convert ``a`` of ``dtype`` to an :py:class:`~.AlgebraicNumber`. """
|
346 |
+
return self.ext.field_element(a)
|
347 |
+
|
348 |
+
def to_sympy(self, a):
|
349 |
+
"""Convert ``a`` of ``dtype`` to a SymPy object. """
|
350 |
+
# Precompute a converter to be reused:
|
351 |
+
if not hasattr(self, '_converter'):
|
352 |
+
self._converter = _make_converter(self)
|
353 |
+
|
354 |
+
return self._converter(a)
|
355 |
+
|
356 |
+
def from_sympy(self, a):
|
357 |
+
"""Convert SymPy's expression to ``dtype``. """
|
358 |
+
try:
|
359 |
+
return self([self.dom.from_sympy(a)])
|
360 |
+
except CoercionFailed:
|
361 |
+
pass
|
362 |
+
|
363 |
+
from sympy.polys.numberfields import to_number_field
|
364 |
+
|
365 |
+
try:
|
366 |
+
return self(to_number_field(a, self.ext).native_coeffs())
|
367 |
+
except (NotAlgebraic, IsomorphismFailed):
|
368 |
+
raise CoercionFailed(
|
369 |
+
"%s is not a valid algebraic number in %s" % (a, self))
|
370 |
+
|
371 |
+
def from_ZZ(K1, a, K0):
|
372 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
373 |
+
return K1(K1.dom.convert(a, K0))
|
374 |
+
|
375 |
+
def from_ZZ_python(K1, a, K0):
|
376 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
377 |
+
return K1(K1.dom.convert(a, K0))
|
378 |
+
|
379 |
+
def from_QQ(K1, a, K0):
|
380 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
381 |
+
return K1(K1.dom.convert(a, K0))
|
382 |
+
|
383 |
+
def from_QQ_python(K1, a, K0):
|
384 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
385 |
+
return K1(K1.dom.convert(a, K0))
|
386 |
+
|
387 |
+
def from_ZZ_gmpy(K1, a, K0):
|
388 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
389 |
+
return K1(K1.dom.convert(a, K0))
|
390 |
+
|
391 |
+
def from_QQ_gmpy(K1, a, K0):
|
392 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
393 |
+
return K1(K1.dom.convert(a, K0))
|
394 |
+
|
395 |
+
def from_RealField(K1, a, K0):
|
396 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
397 |
+
return K1(K1.dom.convert(a, K0))
|
398 |
+
|
399 |
+
def get_ring(self):
|
400 |
+
"""Returns a ring associated with ``self``. """
|
401 |
+
raise DomainError('there is no ring associated with %s' % self)
|
402 |
+
|
403 |
+
def is_positive(self, a):
|
404 |
+
"""Returns True if ``a`` is positive. """
|
405 |
+
return self.dom.is_positive(a.LC())
|
406 |
+
|
407 |
+
def is_negative(self, a):
|
408 |
+
"""Returns True if ``a`` is negative. """
|
409 |
+
return self.dom.is_negative(a.LC())
|
410 |
+
|
411 |
+
def is_nonpositive(self, a):
|
412 |
+
"""Returns True if ``a`` is non-positive. """
|
413 |
+
return self.dom.is_nonpositive(a.LC())
|
414 |
+
|
415 |
+
def is_nonnegative(self, a):
|
416 |
+
"""Returns True if ``a`` is non-negative. """
|
417 |
+
return self.dom.is_nonnegative(a.LC())
|
418 |
+
|
419 |
+
def numer(self, a):
|
420 |
+
"""Returns numerator of ``a``. """
|
421 |
+
return a
|
422 |
+
|
423 |
+
def denom(self, a):
|
424 |
+
"""Returns denominator of ``a``. """
|
425 |
+
return self.one
|
426 |
+
|
427 |
+
def from_AlgebraicField(K1, a, K0):
|
428 |
+
"""Convert AlgebraicField element 'a' to another AlgebraicField """
|
429 |
+
return K1.from_sympy(K0.to_sympy(a))
|
430 |
+
|
431 |
+
def from_GaussianIntegerRing(K1, a, K0):
|
432 |
+
"""Convert a GaussianInteger element 'a' to ``dtype``. """
|
433 |
+
return K1.from_sympy(K0.to_sympy(a))
|
434 |
+
|
435 |
+
def from_GaussianRationalField(K1, a, K0):
|
436 |
+
"""Convert a GaussianRational element 'a' to ``dtype``. """
|
437 |
+
return K1.from_sympy(K0.to_sympy(a))
|
438 |
+
|
439 |
+
def _do_round_two(self):
|
440 |
+
from sympy.polys.numberfields.basis import round_two
|
441 |
+
ZK, dK = round_two(self, radicals=self._nilradicals_mod_p)
|
442 |
+
self._maximal_order = ZK
|
443 |
+
self._discriminant = dK
|
444 |
+
|
445 |
+
def maximal_order(self):
|
446 |
+
"""
|
447 |
+
Compute the maximal order, or ring of integers, of the field.
|
448 |
+
|
449 |
+
Returns
|
450 |
+
=======
|
451 |
+
|
452 |
+
:py:class:`~sympy.polys.numberfields.modules.Submodule`.
|
453 |
+
|
454 |
+
See Also
|
455 |
+
========
|
456 |
+
|
457 |
+
integral_basis
|
458 |
+
|
459 |
+
"""
|
460 |
+
if self._maximal_order is None:
|
461 |
+
self._do_round_two()
|
462 |
+
return self._maximal_order
|
463 |
+
|
464 |
+
def integral_basis(self, fmt=None):
|
465 |
+
r"""
|
466 |
+
Get an integral basis for the field.
|
467 |
+
|
468 |
+
Parameters
|
469 |
+
==========
|
470 |
+
|
471 |
+
fmt : str, None, optional (default=None)
|
472 |
+
If ``None``, return a list of :py:class:`~.ANP` instances.
|
473 |
+
If ``"sympy"``, convert each element of the list to an
|
474 |
+
:py:class:`~.Expr`, using ``self.to_sympy()``.
|
475 |
+
If ``"alg"``, convert each element of the list to an
|
476 |
+
:py:class:`~.AlgebraicNumber`, using ``self.to_alg_num()``.
|
477 |
+
|
478 |
+
Examples
|
479 |
+
========
|
480 |
+
|
481 |
+
>>> from sympy import QQ, AlgebraicNumber, sqrt
|
482 |
+
>>> alpha = AlgebraicNumber(sqrt(5), alias='alpha')
|
483 |
+
>>> k = QQ.algebraic_field(alpha)
|
484 |
+
>>> B0 = k.integral_basis()
|
485 |
+
>>> B1 = k.integral_basis(fmt='sympy')
|
486 |
+
>>> B2 = k.integral_basis(fmt='alg')
|
487 |
+
>>> print(B0[1]) # doctest: +SKIP
|
488 |
+
ANP([mpq(1,2), mpq(1,2)], [mpq(1,1), mpq(0,1), mpq(-5,1)], QQ)
|
489 |
+
>>> print(B1[1])
|
490 |
+
1/2 + alpha/2
|
491 |
+
>>> print(B2[1])
|
492 |
+
alpha/2 + 1/2
|
493 |
+
|
494 |
+
In the last two cases we get legible expressions, which print somewhat
|
495 |
+
differently because of the different types involved:
|
496 |
+
|
497 |
+
>>> print(type(B1[1]))
|
498 |
+
<class 'sympy.core.add.Add'>
|
499 |
+
>>> print(type(B2[1]))
|
500 |
+
<class 'sympy.core.numbers.AlgebraicNumber'>
|
501 |
+
|
502 |
+
See Also
|
503 |
+
========
|
504 |
+
|
505 |
+
to_sympy
|
506 |
+
to_alg_num
|
507 |
+
maximal_order
|
508 |
+
"""
|
509 |
+
ZK = self.maximal_order()
|
510 |
+
M = ZK.QQ_matrix
|
511 |
+
n = M.shape[1]
|
512 |
+
B = [self.new(list(reversed(M[:, j].flat()))) for j in range(n)]
|
513 |
+
if fmt == 'sympy':
|
514 |
+
return [self.to_sympy(b) for b in B]
|
515 |
+
elif fmt == 'alg':
|
516 |
+
return [self.to_alg_num(b) for b in B]
|
517 |
+
return B
|
518 |
+
|
519 |
+
def discriminant(self):
|
520 |
+
"""Get the discriminant of the field."""
|
521 |
+
if self._discriminant is None:
|
522 |
+
self._do_round_two()
|
523 |
+
return self._discriminant
|
524 |
+
|
525 |
+
def primes_above(self, p):
|
526 |
+
"""Compute the prime ideals lying above a given rational prime *p*."""
|
527 |
+
from sympy.polys.numberfields.primes import prime_decomp
|
528 |
+
ZK = self.maximal_order()
|
529 |
+
dK = self.discriminant()
|
530 |
+
rad = self._nilradicals_mod_p.get(p)
|
531 |
+
return prime_decomp(p, ZK=ZK, dK=dK, radical=rad)
|
532 |
+
|
533 |
+
def galois_group(self, by_name=False, max_tries=30, randomize=False):
|
534 |
+
"""
|
535 |
+
Compute the Galois group of the Galois closure of this field.
|
536 |
+
|
537 |
+
Examples
|
538 |
+
========
|
539 |
+
|
540 |
+
If the field is Galois, the order of the group will equal the degree
|
541 |
+
of the field:
|
542 |
+
|
543 |
+
>>> from sympy import QQ
|
544 |
+
>>> from sympy.abc import x
|
545 |
+
>>> k = QQ.alg_field_from_poly(x**4 + 1)
|
546 |
+
>>> G, _ = k.galois_group()
|
547 |
+
>>> G.order()
|
548 |
+
4
|
549 |
+
|
550 |
+
If the field is not Galois, then its Galois closure is a proper
|
551 |
+
extension, and the order of the Galois group will be greater than the
|
552 |
+
degree of the field:
|
553 |
+
|
554 |
+
>>> k = QQ.alg_field_from_poly(x**4 - 2)
|
555 |
+
>>> G, _ = k.galois_group()
|
556 |
+
>>> G.order()
|
557 |
+
8
|
558 |
+
|
559 |
+
See Also
|
560 |
+
========
|
561 |
+
|
562 |
+
sympy.polys.numberfields.galoisgroups.galois_group
|
563 |
+
|
564 |
+
"""
|
565 |
+
return self.ext.minpoly_of_element().galois_group(
|
566 |
+
by_name=by_name, max_tries=max_tries, randomize=randomize)
|
567 |
+
|
568 |
+
|
569 |
+
def _make_converter(K):
|
570 |
+
"""Construct the converter to convert back to Expr"""
|
571 |
+
# Precompute the effect of converting to SymPy and expanding expressions
|
572 |
+
# like (sqrt(2) + sqrt(3))**2. Asking Expr to do the expansion on every
|
573 |
+
# conversion from K to Expr is slow. Here we compute the expansions for
|
574 |
+
# each power of the generator and collect together the resulting algebraic
|
575 |
+
# terms and the rational coefficients into a matrix.
|
576 |
+
|
577 |
+
gen = K.ext.as_expr()
|
578 |
+
todom = K.dom.from_sympy
|
579 |
+
|
580 |
+
# We'll let Expr compute the expansions. We won't make any presumptions
|
581 |
+
# about what this results in except that it is QQ-linear in some terms
|
582 |
+
# that we will call algebraics. The final result will be expressed in
|
583 |
+
# terms of those.
|
584 |
+
powers = [S.One, gen]
|
585 |
+
for n in range(2, K.mod.degree()):
|
586 |
+
powers.append((gen * powers[-1]).expand())
|
587 |
+
|
588 |
+
# Collect the rational coefficients and algebraic Expr that can
|
589 |
+
# map the ANP coefficients into an expanded SymPy expression
|
590 |
+
terms = [dict(t.as_coeff_Mul()[::-1] for t in Add.make_args(p)) for p in powers]
|
591 |
+
algebraics = set().union(*terms)
|
592 |
+
matrix = [[todom(t.get(a, S.Zero)) for t in terms] for a in algebraics]
|
593 |
+
|
594 |
+
# Create a function to do the conversion efficiently:
|
595 |
+
|
596 |
+
def converter(a):
|
597 |
+
"""Convert a to Expr using converter"""
|
598 |
+
ai = a.rep[::-1]
|
599 |
+
tosympy = K.dom.to_sympy
|
600 |
+
coeffs_dom = [sum(mij*aj for mij, aj in zip(mi, ai)) for mi in matrix]
|
601 |
+
coeffs_sympy = [tosympy(c) for c in coeffs_dom]
|
602 |
+
res = Add(*(Mul(c, a) for c, a in zip(coeffs_sympy, algebraics)))
|
603 |
+
return res
|
604 |
+
|
605 |
+
return converter
|
venv/lib/python3.10/site-packages/sympy/polys/domains/characteristiczero.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`CharacteristicZero` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.domain import Domain
|
5 |
+
from sympy.utilities import public
|
6 |
+
|
7 |
+
@public
|
8 |
+
class CharacteristicZero(Domain):
|
9 |
+
"""Domain that has infinite number of elements. """
|
10 |
+
|
11 |
+
has_CharacteristicZero = True
|
12 |
+
|
13 |
+
def characteristic(self):
|
14 |
+
"""Return the characteristic of this domain. """
|
15 |
+
return 0
|
venv/lib/python3.10/site-packages/sympy/polys/domains/complexfield.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`ComplexField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.core.numbers import Float, I
|
5 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
6 |
+
from sympy.polys.domains.field import Field
|
7 |
+
from sympy.polys.domains.mpelements import MPContext
|
8 |
+
from sympy.polys.domains.simpledomain import SimpleDomain
|
9 |
+
from sympy.polys.polyerrors import DomainError, CoercionFailed
|
10 |
+
from sympy.utilities import public
|
11 |
+
|
12 |
+
@public
|
13 |
+
class ComplexField(Field, CharacteristicZero, SimpleDomain):
|
14 |
+
"""Complex numbers up to the given precision. """
|
15 |
+
|
16 |
+
rep = 'CC'
|
17 |
+
|
18 |
+
is_ComplexField = is_CC = True
|
19 |
+
|
20 |
+
is_Exact = False
|
21 |
+
is_Numerical = True
|
22 |
+
|
23 |
+
has_assoc_Ring = False
|
24 |
+
has_assoc_Field = True
|
25 |
+
|
26 |
+
_default_precision = 53
|
27 |
+
|
28 |
+
@property
|
29 |
+
def has_default_precision(self):
|
30 |
+
return self.precision == self._default_precision
|
31 |
+
|
32 |
+
@property
|
33 |
+
def precision(self):
|
34 |
+
return self._context.prec
|
35 |
+
|
36 |
+
@property
|
37 |
+
def dps(self):
|
38 |
+
return self._context.dps
|
39 |
+
|
40 |
+
@property
|
41 |
+
def tolerance(self):
|
42 |
+
return self._context.tolerance
|
43 |
+
|
44 |
+
def __init__(self, prec=_default_precision, dps=None, tol=None):
|
45 |
+
context = MPContext(prec, dps, tol, False)
|
46 |
+
context._parent = self
|
47 |
+
self._context = context
|
48 |
+
|
49 |
+
self.dtype = context.mpc
|
50 |
+
self.zero = self.dtype(0)
|
51 |
+
self.one = self.dtype(1)
|
52 |
+
|
53 |
+
def __eq__(self, other):
|
54 |
+
return (isinstance(other, ComplexField)
|
55 |
+
and self.precision == other.precision
|
56 |
+
and self.tolerance == other.tolerance)
|
57 |
+
|
58 |
+
def __hash__(self):
|
59 |
+
return hash((self.__class__.__name__, self.dtype, self.precision, self.tolerance))
|
60 |
+
|
61 |
+
def to_sympy(self, element):
|
62 |
+
"""Convert ``element`` to SymPy number. """
|
63 |
+
return Float(element.real, self.dps) + I*Float(element.imag, self.dps)
|
64 |
+
|
65 |
+
def from_sympy(self, expr):
|
66 |
+
"""Convert SymPy's number to ``dtype``. """
|
67 |
+
number = expr.evalf(n=self.dps)
|
68 |
+
real, imag = number.as_real_imag()
|
69 |
+
|
70 |
+
if real.is_Number and imag.is_Number:
|
71 |
+
return self.dtype(real, imag)
|
72 |
+
else:
|
73 |
+
raise CoercionFailed("expected complex number, got %s" % expr)
|
74 |
+
|
75 |
+
def from_ZZ(self, element, base):
|
76 |
+
return self.dtype(element)
|
77 |
+
|
78 |
+
def from_QQ(self, element, base):
|
79 |
+
return self.dtype(int(element.numerator)) / int(element.denominator)
|
80 |
+
|
81 |
+
def from_ZZ_python(self, element, base):
|
82 |
+
return self.dtype(element)
|
83 |
+
|
84 |
+
def from_QQ_python(self, element, base):
|
85 |
+
return self.dtype(element.numerator) / element.denominator
|
86 |
+
|
87 |
+
def from_ZZ_gmpy(self, element, base):
|
88 |
+
return self.dtype(int(element))
|
89 |
+
|
90 |
+
def from_QQ_gmpy(self, element, base):
|
91 |
+
return self.dtype(int(element.numerator)) / int(element.denominator)
|
92 |
+
|
93 |
+
def from_GaussianIntegerRing(self, element, base):
|
94 |
+
return self.dtype(int(element.x), int(element.y))
|
95 |
+
|
96 |
+
def from_GaussianRationalField(self, element, base):
|
97 |
+
x = element.x
|
98 |
+
y = element.y
|
99 |
+
return (self.dtype(int(x.numerator)) / int(x.denominator) +
|
100 |
+
self.dtype(0, int(y.numerator)) / int(y.denominator))
|
101 |
+
|
102 |
+
def from_AlgebraicField(self, element, base):
|
103 |
+
return self.from_sympy(base.to_sympy(element).evalf(self.dps))
|
104 |
+
|
105 |
+
def from_RealField(self, element, base):
|
106 |
+
return self.dtype(element)
|
107 |
+
|
108 |
+
def from_ComplexField(self, element, base):
|
109 |
+
if self == base:
|
110 |
+
return element
|
111 |
+
else:
|
112 |
+
return self.dtype(element)
|
113 |
+
|
114 |
+
def get_ring(self):
|
115 |
+
"""Returns a ring associated with ``self``. """
|
116 |
+
raise DomainError("there is no ring associated with %s" % self)
|
117 |
+
|
118 |
+
def get_exact(self):
|
119 |
+
"""Returns an exact domain associated with ``self``. """
|
120 |
+
raise DomainError("there is no exact domain associated with %s" % self)
|
121 |
+
|
122 |
+
def is_negative(self, element):
|
123 |
+
"""Returns ``False`` for any ``ComplexElement``. """
|
124 |
+
return False
|
125 |
+
|
126 |
+
def is_positive(self, element):
|
127 |
+
"""Returns ``False`` for any ``ComplexElement``. """
|
128 |
+
return False
|
129 |
+
|
130 |
+
def is_nonnegative(self, element):
|
131 |
+
"""Returns ``False`` for any ``ComplexElement``. """
|
132 |
+
return False
|
133 |
+
|
134 |
+
def is_nonpositive(self, element):
|
135 |
+
"""Returns ``False`` for any ``ComplexElement``. """
|
136 |
+
return False
|
137 |
+
|
138 |
+
def gcd(self, a, b):
|
139 |
+
"""Returns GCD of ``a`` and ``b``. """
|
140 |
+
return self.one
|
141 |
+
|
142 |
+
def lcm(self, a, b):
|
143 |
+
"""Returns LCM of ``a`` and ``b``. """
|
144 |
+
return a*b
|
145 |
+
|
146 |
+
def almosteq(self, a, b, tolerance=None):
|
147 |
+
"""Check if ``a`` and ``b`` are almost equal. """
|
148 |
+
return self._context.almosteq(a, b, tolerance)
|
149 |
+
|
150 |
+
|
151 |
+
CC = ComplexField()
|
venv/lib/python3.10/site-packages/sympy/polys/domains/compositedomain.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`CompositeDomain` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.domain import Domain
|
5 |
+
from sympy.polys.polyerrors import GeneratorsError
|
6 |
+
|
7 |
+
from sympy.utilities import public
|
8 |
+
|
9 |
+
@public
|
10 |
+
class CompositeDomain(Domain):
|
11 |
+
"""Base class for composite domains, e.g. ZZ[x], ZZ(X). """
|
12 |
+
|
13 |
+
is_Composite = True
|
14 |
+
|
15 |
+
gens, ngens, symbols, domain = [None]*4
|
16 |
+
|
17 |
+
def inject(self, *symbols):
|
18 |
+
"""Inject generators into this domain. """
|
19 |
+
if not (set(self.symbols) & set(symbols)):
|
20 |
+
return self.__class__(self.domain, self.symbols + symbols, self.order)
|
21 |
+
else:
|
22 |
+
raise GeneratorsError("common generators in %s and %s" % (self.symbols, symbols))
|
23 |
+
|
24 |
+
def drop(self, *symbols):
|
25 |
+
"""Drop generators from this domain. """
|
26 |
+
symset = set(symbols)
|
27 |
+
newsyms = tuple(s for s in self.symbols if s not in symset)
|
28 |
+
domain = self.domain.drop(*symbols)
|
29 |
+
if not newsyms:
|
30 |
+
return domain
|
31 |
+
else:
|
32 |
+
return self.__class__(domain, newsyms, self.order)
|
venv/lib/python3.10/site-packages/sympy/polys/domains/domain.py
ADDED
@@ -0,0 +1,1304 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`Domain` class. """
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
from typing import Any
|
5 |
+
|
6 |
+
from sympy.core.numbers import AlgebraicNumber
|
7 |
+
from sympy.core import Basic, sympify
|
8 |
+
from sympy.core.sorting import default_sort_key, ordered
|
9 |
+
from sympy.external.gmpy import HAS_GMPY
|
10 |
+
from sympy.polys.domains.domainelement import DomainElement
|
11 |
+
from sympy.polys.orderings import lex
|
12 |
+
from sympy.polys.polyerrors import UnificationFailed, CoercionFailed, DomainError
|
13 |
+
from sympy.polys.polyutils import _unify_gens, _not_a_coeff
|
14 |
+
from sympy.utilities import public
|
15 |
+
from sympy.utilities.iterables import is_sequence
|
16 |
+
|
17 |
+
|
18 |
+
@public
|
19 |
+
class Domain:
|
20 |
+
"""Superclass for all domains in the polys domains system.
|
21 |
+
|
22 |
+
See :ref:`polys-domainsintro` for an introductory explanation of the
|
23 |
+
domains system.
|
24 |
+
|
25 |
+
The :py:class:`~.Domain` class is an abstract base class for all of the
|
26 |
+
concrete domain types. There are many different :py:class:`~.Domain`
|
27 |
+
subclasses each of which has an associated ``dtype`` which is a class
|
28 |
+
representing the elements of the domain. The coefficients of a
|
29 |
+
:py:class:`~.Poly` are elements of a domain which must be a subclass of
|
30 |
+
:py:class:`~.Domain`.
|
31 |
+
|
32 |
+
Examples
|
33 |
+
========
|
34 |
+
|
35 |
+
The most common example domains are the integers :ref:`ZZ` and the
|
36 |
+
rationals :ref:`QQ`.
|
37 |
+
|
38 |
+
>>> from sympy import Poly, symbols, Domain
|
39 |
+
>>> x, y = symbols('x, y')
|
40 |
+
>>> p = Poly(x**2 + y)
|
41 |
+
>>> p
|
42 |
+
Poly(x**2 + y, x, y, domain='ZZ')
|
43 |
+
>>> p.domain
|
44 |
+
ZZ
|
45 |
+
>>> isinstance(p.domain, Domain)
|
46 |
+
True
|
47 |
+
>>> Poly(x**2 + y/2)
|
48 |
+
Poly(x**2 + 1/2*y, x, y, domain='QQ')
|
49 |
+
|
50 |
+
The domains can be used directly in which case the domain object e.g.
|
51 |
+
(:ref:`ZZ` or :ref:`QQ`) can be used as a constructor for elements of
|
52 |
+
``dtype``.
|
53 |
+
|
54 |
+
>>> from sympy import ZZ, QQ
|
55 |
+
>>> ZZ(2)
|
56 |
+
2
|
57 |
+
>>> ZZ.dtype # doctest: +SKIP
|
58 |
+
<class 'int'>
|
59 |
+
>>> type(ZZ(2)) # doctest: +SKIP
|
60 |
+
<class 'int'>
|
61 |
+
>>> QQ(1, 2)
|
62 |
+
1/2
|
63 |
+
>>> type(QQ(1, 2)) # doctest: +SKIP
|
64 |
+
<class 'sympy.polys.domains.pythonrational.PythonRational'>
|
65 |
+
|
66 |
+
The corresponding domain elements can be used with the arithmetic
|
67 |
+
operations ``+,-,*,**`` and depending on the domain some combination of
|
68 |
+
``/,//,%`` might be usable. For example in :ref:`ZZ` both ``//`` (floor
|
69 |
+
division) and ``%`` (modulo division) can be used but ``/`` (true
|
70 |
+
division) cannot. Since :ref:`QQ` is a :py:class:`~.Field` its elements
|
71 |
+
can be used with ``/`` but ``//`` and ``%`` should not be used. Some
|
72 |
+
domains have a :py:meth:`~.Domain.gcd` method.
|
73 |
+
|
74 |
+
>>> ZZ(2) + ZZ(3)
|
75 |
+
5
|
76 |
+
>>> ZZ(5) // ZZ(2)
|
77 |
+
2
|
78 |
+
>>> ZZ(5) % ZZ(2)
|
79 |
+
1
|
80 |
+
>>> QQ(1, 2) / QQ(2, 3)
|
81 |
+
3/4
|
82 |
+
>>> ZZ.gcd(ZZ(4), ZZ(2))
|
83 |
+
2
|
84 |
+
>>> QQ.gcd(QQ(2,7), QQ(5,3))
|
85 |
+
1/21
|
86 |
+
>>> ZZ.is_Field
|
87 |
+
False
|
88 |
+
>>> QQ.is_Field
|
89 |
+
True
|
90 |
+
|
91 |
+
There are also many other domains including:
|
92 |
+
|
93 |
+
1. :ref:`GF(p)` for finite fields of prime order.
|
94 |
+
2. :ref:`RR` for real (floating point) numbers.
|
95 |
+
3. :ref:`CC` for complex (floating point) numbers.
|
96 |
+
4. :ref:`QQ(a)` for algebraic number fields.
|
97 |
+
5. :ref:`K[x]` for polynomial rings.
|
98 |
+
6. :ref:`K(x)` for rational function fields.
|
99 |
+
7. :ref:`EX` for arbitrary expressions.
|
100 |
+
|
101 |
+
Each domain is represented by a domain object and also an implementation
|
102 |
+
class (``dtype``) for the elements of the domain. For example the
|
103 |
+
:ref:`K[x]` domains are represented by a domain object which is an
|
104 |
+
instance of :py:class:`~.PolynomialRing` and the elements are always
|
105 |
+
instances of :py:class:`~.PolyElement`. The implementation class
|
106 |
+
represents particular types of mathematical expressions in a way that is
|
107 |
+
more efficient than a normal SymPy expression which is of type
|
108 |
+
:py:class:`~.Expr`. The domain methods :py:meth:`~.Domain.from_sympy` and
|
109 |
+
:py:meth:`~.Domain.to_sympy` are used to convert from :py:class:`~.Expr`
|
110 |
+
to a domain element and vice versa.
|
111 |
+
|
112 |
+
>>> from sympy import Symbol, ZZ, Expr
|
113 |
+
>>> x = Symbol('x')
|
114 |
+
>>> K = ZZ[x] # polynomial ring domain
|
115 |
+
>>> K
|
116 |
+
ZZ[x]
|
117 |
+
>>> type(K) # class of the domain
|
118 |
+
<class 'sympy.polys.domains.polynomialring.PolynomialRing'>
|
119 |
+
>>> K.dtype # class of the elements
|
120 |
+
<class 'sympy.polys.rings.PolyElement'>
|
121 |
+
>>> p_expr = x**2 + 1 # Expr
|
122 |
+
>>> p_expr
|
123 |
+
x**2 + 1
|
124 |
+
>>> type(p_expr)
|
125 |
+
<class 'sympy.core.add.Add'>
|
126 |
+
>>> isinstance(p_expr, Expr)
|
127 |
+
True
|
128 |
+
>>> p_domain = K.from_sympy(p_expr)
|
129 |
+
>>> p_domain # domain element
|
130 |
+
x**2 + 1
|
131 |
+
>>> type(p_domain)
|
132 |
+
<class 'sympy.polys.rings.PolyElement'>
|
133 |
+
>>> K.to_sympy(p_domain) == p_expr
|
134 |
+
True
|
135 |
+
|
136 |
+
The :py:meth:`~.Domain.convert_from` method is used to convert domain
|
137 |
+
elements from one domain to another.
|
138 |
+
|
139 |
+
>>> from sympy import ZZ, QQ
|
140 |
+
>>> ez = ZZ(2)
|
141 |
+
>>> eq = QQ.convert_from(ez, ZZ)
|
142 |
+
>>> type(ez) # doctest: +SKIP
|
143 |
+
<class 'int'>
|
144 |
+
>>> type(eq) # doctest: +SKIP
|
145 |
+
<class 'sympy.polys.domains.pythonrational.PythonRational'>
|
146 |
+
|
147 |
+
Elements from different domains should not be mixed in arithmetic or other
|
148 |
+
operations: they should be converted to a common domain first. The domain
|
149 |
+
method :py:meth:`~.Domain.unify` is used to find a domain that can
|
150 |
+
represent all the elements of two given domains.
|
151 |
+
|
152 |
+
>>> from sympy import ZZ, QQ, symbols
|
153 |
+
>>> x, y = symbols('x, y')
|
154 |
+
>>> ZZ.unify(QQ)
|
155 |
+
QQ
|
156 |
+
>>> ZZ[x].unify(QQ)
|
157 |
+
QQ[x]
|
158 |
+
>>> ZZ[x].unify(QQ[y])
|
159 |
+
QQ[x,y]
|
160 |
+
|
161 |
+
If a domain is a :py:class:`~.Ring` then is might have an associated
|
162 |
+
:py:class:`~.Field` and vice versa. The :py:meth:`~.Domain.get_field` and
|
163 |
+
:py:meth:`~.Domain.get_ring` methods will find or create the associated
|
164 |
+
domain.
|
165 |
+
|
166 |
+
>>> from sympy import ZZ, QQ, Symbol
|
167 |
+
>>> x = Symbol('x')
|
168 |
+
>>> ZZ.has_assoc_Field
|
169 |
+
True
|
170 |
+
>>> ZZ.get_field()
|
171 |
+
QQ
|
172 |
+
>>> QQ.has_assoc_Ring
|
173 |
+
True
|
174 |
+
>>> QQ.get_ring()
|
175 |
+
ZZ
|
176 |
+
>>> K = QQ[x]
|
177 |
+
>>> K
|
178 |
+
QQ[x]
|
179 |
+
>>> K.get_field()
|
180 |
+
QQ(x)
|
181 |
+
|
182 |
+
See also
|
183 |
+
========
|
184 |
+
|
185 |
+
DomainElement: abstract base class for domain elements
|
186 |
+
construct_domain: construct a minimal domain for some expressions
|
187 |
+
|
188 |
+
"""
|
189 |
+
|
190 |
+
dtype: type | None = None
|
191 |
+
"""The type (class) of the elements of this :py:class:`~.Domain`:
|
192 |
+
|
193 |
+
>>> from sympy import ZZ, QQ, Symbol
|
194 |
+
>>> ZZ.dtype
|
195 |
+
<class 'int'>
|
196 |
+
>>> z = ZZ(2)
|
197 |
+
>>> z
|
198 |
+
2
|
199 |
+
>>> type(z)
|
200 |
+
<class 'int'>
|
201 |
+
>>> type(z) == ZZ.dtype
|
202 |
+
True
|
203 |
+
|
204 |
+
Every domain has an associated **dtype** ("datatype") which is the
|
205 |
+
class of the associated domain elements.
|
206 |
+
|
207 |
+
See also
|
208 |
+
========
|
209 |
+
|
210 |
+
of_type
|
211 |
+
"""
|
212 |
+
|
213 |
+
zero: Any = None
|
214 |
+
"""The zero element of the :py:class:`~.Domain`:
|
215 |
+
|
216 |
+
>>> from sympy import QQ
|
217 |
+
>>> QQ.zero
|
218 |
+
0
|
219 |
+
>>> QQ.of_type(QQ.zero)
|
220 |
+
True
|
221 |
+
|
222 |
+
See also
|
223 |
+
========
|
224 |
+
|
225 |
+
of_type
|
226 |
+
one
|
227 |
+
"""
|
228 |
+
|
229 |
+
one: Any = None
|
230 |
+
"""The one element of the :py:class:`~.Domain`:
|
231 |
+
|
232 |
+
>>> from sympy import QQ
|
233 |
+
>>> QQ.one
|
234 |
+
1
|
235 |
+
>>> QQ.of_type(QQ.one)
|
236 |
+
True
|
237 |
+
|
238 |
+
See also
|
239 |
+
========
|
240 |
+
|
241 |
+
of_type
|
242 |
+
zero
|
243 |
+
"""
|
244 |
+
|
245 |
+
is_Ring = False
|
246 |
+
"""Boolean flag indicating if the domain is a :py:class:`~.Ring`.
|
247 |
+
|
248 |
+
>>> from sympy import ZZ
|
249 |
+
>>> ZZ.is_Ring
|
250 |
+
True
|
251 |
+
|
252 |
+
Basically every :py:class:`~.Domain` represents a ring so this flag is
|
253 |
+
not that useful.
|
254 |
+
|
255 |
+
See also
|
256 |
+
========
|
257 |
+
|
258 |
+
is_PID
|
259 |
+
is_Field
|
260 |
+
get_ring
|
261 |
+
has_assoc_Ring
|
262 |
+
"""
|
263 |
+
|
264 |
+
is_Field = False
|
265 |
+
"""Boolean flag indicating if the domain is a :py:class:`~.Field`.
|
266 |
+
|
267 |
+
>>> from sympy import ZZ, QQ
|
268 |
+
>>> ZZ.is_Field
|
269 |
+
False
|
270 |
+
>>> QQ.is_Field
|
271 |
+
True
|
272 |
+
|
273 |
+
See also
|
274 |
+
========
|
275 |
+
|
276 |
+
is_PID
|
277 |
+
is_Ring
|
278 |
+
get_field
|
279 |
+
has_assoc_Field
|
280 |
+
"""
|
281 |
+
|
282 |
+
has_assoc_Ring = False
|
283 |
+
"""Boolean flag indicating if the domain has an associated
|
284 |
+
:py:class:`~.Ring`.
|
285 |
+
|
286 |
+
>>> from sympy import QQ
|
287 |
+
>>> QQ.has_assoc_Ring
|
288 |
+
True
|
289 |
+
>>> QQ.get_ring()
|
290 |
+
ZZ
|
291 |
+
|
292 |
+
See also
|
293 |
+
========
|
294 |
+
|
295 |
+
is_Field
|
296 |
+
get_ring
|
297 |
+
"""
|
298 |
+
|
299 |
+
has_assoc_Field = False
|
300 |
+
"""Boolean flag indicating if the domain has an associated
|
301 |
+
:py:class:`~.Field`.
|
302 |
+
|
303 |
+
>>> from sympy import ZZ
|
304 |
+
>>> ZZ.has_assoc_Field
|
305 |
+
True
|
306 |
+
>>> ZZ.get_field()
|
307 |
+
QQ
|
308 |
+
|
309 |
+
See also
|
310 |
+
========
|
311 |
+
|
312 |
+
is_Field
|
313 |
+
get_field
|
314 |
+
"""
|
315 |
+
|
316 |
+
is_FiniteField = is_FF = False
|
317 |
+
is_IntegerRing = is_ZZ = False
|
318 |
+
is_RationalField = is_QQ = False
|
319 |
+
is_GaussianRing = is_ZZ_I = False
|
320 |
+
is_GaussianField = is_QQ_I = False
|
321 |
+
is_RealField = is_RR = False
|
322 |
+
is_ComplexField = is_CC = False
|
323 |
+
is_AlgebraicField = is_Algebraic = False
|
324 |
+
is_PolynomialRing = is_Poly = False
|
325 |
+
is_FractionField = is_Frac = False
|
326 |
+
is_SymbolicDomain = is_EX = False
|
327 |
+
is_SymbolicRawDomain = is_EXRAW = False
|
328 |
+
is_FiniteExtension = False
|
329 |
+
|
330 |
+
is_Exact = True
|
331 |
+
is_Numerical = False
|
332 |
+
|
333 |
+
is_Simple = False
|
334 |
+
is_Composite = False
|
335 |
+
|
336 |
+
is_PID = False
|
337 |
+
"""Boolean flag indicating if the domain is a `principal ideal domain`_.
|
338 |
+
|
339 |
+
>>> from sympy import ZZ
|
340 |
+
>>> ZZ.has_assoc_Field
|
341 |
+
True
|
342 |
+
>>> ZZ.get_field()
|
343 |
+
QQ
|
344 |
+
|
345 |
+
.. _principal ideal domain: https://en.wikipedia.org/wiki/Principal_ideal_domain
|
346 |
+
|
347 |
+
See also
|
348 |
+
========
|
349 |
+
|
350 |
+
is_Field
|
351 |
+
get_field
|
352 |
+
"""
|
353 |
+
|
354 |
+
has_CharacteristicZero = False
|
355 |
+
|
356 |
+
rep: str | None = None
|
357 |
+
alias: str | None = None
|
358 |
+
|
359 |
+
def __init__(self):
|
360 |
+
raise NotImplementedError
|
361 |
+
|
362 |
+
def __str__(self):
|
363 |
+
return self.rep
|
364 |
+
|
365 |
+
def __repr__(self):
|
366 |
+
return str(self)
|
367 |
+
|
368 |
+
def __hash__(self):
|
369 |
+
return hash((self.__class__.__name__, self.dtype))
|
370 |
+
|
371 |
+
def new(self, *args):
|
372 |
+
return self.dtype(*args)
|
373 |
+
|
374 |
+
@property
|
375 |
+
def tp(self):
|
376 |
+
"""Alias for :py:attr:`~.Domain.dtype`"""
|
377 |
+
return self.dtype
|
378 |
+
|
379 |
+
def __call__(self, *args):
|
380 |
+
"""Construct an element of ``self`` domain from ``args``. """
|
381 |
+
return self.new(*args)
|
382 |
+
|
383 |
+
def normal(self, *args):
|
384 |
+
return self.dtype(*args)
|
385 |
+
|
386 |
+
def convert_from(self, element, base):
|
387 |
+
"""Convert ``element`` to ``self.dtype`` given the base domain. """
|
388 |
+
if base.alias is not None:
|
389 |
+
method = "from_" + base.alias
|
390 |
+
else:
|
391 |
+
method = "from_" + base.__class__.__name__
|
392 |
+
|
393 |
+
_convert = getattr(self, method)
|
394 |
+
|
395 |
+
if _convert is not None:
|
396 |
+
result = _convert(element, base)
|
397 |
+
|
398 |
+
if result is not None:
|
399 |
+
return result
|
400 |
+
|
401 |
+
raise CoercionFailed("Cannot convert %s of type %s from %s to %s" % (element, type(element), base, self))
|
402 |
+
|
403 |
+
def convert(self, element, base=None):
|
404 |
+
"""Convert ``element`` to ``self.dtype``. """
|
405 |
+
|
406 |
+
if base is not None:
|
407 |
+
if _not_a_coeff(element):
|
408 |
+
raise CoercionFailed('%s is not in any domain' % element)
|
409 |
+
return self.convert_from(element, base)
|
410 |
+
|
411 |
+
if self.of_type(element):
|
412 |
+
return element
|
413 |
+
|
414 |
+
if _not_a_coeff(element):
|
415 |
+
raise CoercionFailed('%s is not in any domain' % element)
|
416 |
+
|
417 |
+
from sympy.polys.domains import ZZ, QQ, RealField, ComplexField
|
418 |
+
|
419 |
+
if ZZ.of_type(element):
|
420 |
+
return self.convert_from(element, ZZ)
|
421 |
+
|
422 |
+
if isinstance(element, int):
|
423 |
+
return self.convert_from(ZZ(element), ZZ)
|
424 |
+
|
425 |
+
if HAS_GMPY:
|
426 |
+
integers = ZZ
|
427 |
+
if isinstance(element, integers.tp):
|
428 |
+
return self.convert_from(element, integers)
|
429 |
+
|
430 |
+
rationals = QQ
|
431 |
+
if isinstance(element, rationals.tp):
|
432 |
+
return self.convert_from(element, rationals)
|
433 |
+
|
434 |
+
if isinstance(element, float):
|
435 |
+
parent = RealField(tol=False)
|
436 |
+
return self.convert_from(parent(element), parent)
|
437 |
+
|
438 |
+
if isinstance(element, complex):
|
439 |
+
parent = ComplexField(tol=False)
|
440 |
+
return self.convert_from(parent(element), parent)
|
441 |
+
|
442 |
+
if isinstance(element, DomainElement):
|
443 |
+
return self.convert_from(element, element.parent())
|
444 |
+
|
445 |
+
# TODO: implement this in from_ methods
|
446 |
+
if self.is_Numerical and getattr(element, 'is_ground', False):
|
447 |
+
return self.convert(element.LC())
|
448 |
+
|
449 |
+
if isinstance(element, Basic):
|
450 |
+
try:
|
451 |
+
return self.from_sympy(element)
|
452 |
+
except (TypeError, ValueError):
|
453 |
+
pass
|
454 |
+
else: # TODO: remove this branch
|
455 |
+
if not is_sequence(element):
|
456 |
+
try:
|
457 |
+
element = sympify(element, strict=True)
|
458 |
+
if isinstance(element, Basic):
|
459 |
+
return self.from_sympy(element)
|
460 |
+
except (TypeError, ValueError):
|
461 |
+
pass
|
462 |
+
|
463 |
+
raise CoercionFailed("Cannot convert %s of type %s to %s" % (element, type(element), self))
|
464 |
+
|
465 |
+
def of_type(self, element):
|
466 |
+
"""Check if ``a`` is of type ``dtype``. """
|
467 |
+
return isinstance(element, self.tp) # XXX: this isn't correct, e.g. PolyElement
|
468 |
+
|
469 |
+
def __contains__(self, a):
|
470 |
+
"""Check if ``a`` belongs to this domain. """
|
471 |
+
try:
|
472 |
+
if _not_a_coeff(a):
|
473 |
+
raise CoercionFailed
|
474 |
+
self.convert(a) # this might raise, too
|
475 |
+
except CoercionFailed:
|
476 |
+
return False
|
477 |
+
|
478 |
+
return True
|
479 |
+
|
480 |
+
def to_sympy(self, a):
|
481 |
+
"""Convert domain element *a* to a SymPy expression (Expr).
|
482 |
+
|
483 |
+
Explanation
|
484 |
+
===========
|
485 |
+
|
486 |
+
Convert a :py:class:`~.Domain` element *a* to :py:class:`~.Expr`. Most
|
487 |
+
public SymPy functions work with objects of type :py:class:`~.Expr`.
|
488 |
+
The elements of a :py:class:`~.Domain` have a different internal
|
489 |
+
representation. It is not possible to mix domain elements with
|
490 |
+
:py:class:`~.Expr` so each domain has :py:meth:`~.Domain.to_sympy` and
|
491 |
+
:py:meth:`~.Domain.from_sympy` methods to convert its domain elements
|
492 |
+
to and from :py:class:`~.Expr`.
|
493 |
+
|
494 |
+
Parameters
|
495 |
+
==========
|
496 |
+
|
497 |
+
a: domain element
|
498 |
+
An element of this :py:class:`~.Domain`.
|
499 |
+
|
500 |
+
Returns
|
501 |
+
=======
|
502 |
+
|
503 |
+
expr: Expr
|
504 |
+
A normal SymPy expression of type :py:class:`~.Expr`.
|
505 |
+
|
506 |
+
Examples
|
507 |
+
========
|
508 |
+
|
509 |
+
Construct an element of the :ref:`QQ` domain and then convert it to
|
510 |
+
:py:class:`~.Expr`.
|
511 |
+
|
512 |
+
>>> from sympy import QQ, Expr
|
513 |
+
>>> q_domain = QQ(2)
|
514 |
+
>>> q_domain
|
515 |
+
2
|
516 |
+
>>> q_expr = QQ.to_sympy(q_domain)
|
517 |
+
>>> q_expr
|
518 |
+
2
|
519 |
+
|
520 |
+
Although the printed forms look similar these objects are not of the
|
521 |
+
same type.
|
522 |
+
|
523 |
+
>>> isinstance(q_domain, Expr)
|
524 |
+
False
|
525 |
+
>>> isinstance(q_expr, Expr)
|
526 |
+
True
|
527 |
+
|
528 |
+
Construct an element of :ref:`K[x]` and convert to
|
529 |
+
:py:class:`~.Expr`.
|
530 |
+
|
531 |
+
>>> from sympy import Symbol
|
532 |
+
>>> x = Symbol('x')
|
533 |
+
>>> K = QQ[x]
|
534 |
+
>>> x_domain = K.gens[0] # generator x as a domain element
|
535 |
+
>>> p_domain = x_domain**2/3 + 1
|
536 |
+
>>> p_domain
|
537 |
+
1/3*x**2 + 1
|
538 |
+
>>> p_expr = K.to_sympy(p_domain)
|
539 |
+
>>> p_expr
|
540 |
+
x**2/3 + 1
|
541 |
+
|
542 |
+
The :py:meth:`~.Domain.from_sympy` method is used for the opposite
|
543 |
+
conversion from a normal SymPy expression to a domain element.
|
544 |
+
|
545 |
+
>>> p_domain == p_expr
|
546 |
+
False
|
547 |
+
>>> K.from_sympy(p_expr) == p_domain
|
548 |
+
True
|
549 |
+
>>> K.to_sympy(p_domain) == p_expr
|
550 |
+
True
|
551 |
+
>>> K.from_sympy(K.to_sympy(p_domain)) == p_domain
|
552 |
+
True
|
553 |
+
>>> K.to_sympy(K.from_sympy(p_expr)) == p_expr
|
554 |
+
True
|
555 |
+
|
556 |
+
The :py:meth:`~.Domain.from_sympy` method makes it easier to construct
|
557 |
+
domain elements interactively.
|
558 |
+
|
559 |
+
>>> from sympy import Symbol
|
560 |
+
>>> x = Symbol('x')
|
561 |
+
>>> K = QQ[x]
|
562 |
+
>>> K.from_sympy(x**2/3 + 1)
|
563 |
+
1/3*x**2 + 1
|
564 |
+
|
565 |
+
See also
|
566 |
+
========
|
567 |
+
|
568 |
+
from_sympy
|
569 |
+
convert_from
|
570 |
+
"""
|
571 |
+
raise NotImplementedError
|
572 |
+
|
573 |
+
def from_sympy(self, a):
|
574 |
+
"""Convert a SymPy expression to an element of this domain.
|
575 |
+
|
576 |
+
Explanation
|
577 |
+
===========
|
578 |
+
|
579 |
+
See :py:meth:`~.Domain.to_sympy` for explanation and examples.
|
580 |
+
|
581 |
+
Parameters
|
582 |
+
==========
|
583 |
+
|
584 |
+
expr: Expr
|
585 |
+
A normal SymPy expression of type :py:class:`~.Expr`.
|
586 |
+
|
587 |
+
Returns
|
588 |
+
=======
|
589 |
+
|
590 |
+
a: domain element
|
591 |
+
An element of this :py:class:`~.Domain`.
|
592 |
+
|
593 |
+
See also
|
594 |
+
========
|
595 |
+
|
596 |
+
to_sympy
|
597 |
+
convert_from
|
598 |
+
"""
|
599 |
+
raise NotImplementedError
|
600 |
+
|
601 |
+
def sum(self, args):
|
602 |
+
return sum(args)
|
603 |
+
|
604 |
+
def from_FF(K1, a, K0):
|
605 |
+
"""Convert ``ModularInteger(int)`` to ``dtype``. """
|
606 |
+
return None
|
607 |
+
|
608 |
+
def from_FF_python(K1, a, K0):
|
609 |
+
"""Convert ``ModularInteger(int)`` to ``dtype``. """
|
610 |
+
return None
|
611 |
+
|
612 |
+
def from_ZZ_python(K1, a, K0):
|
613 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
614 |
+
return None
|
615 |
+
|
616 |
+
def from_QQ_python(K1, a, K0):
|
617 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
618 |
+
return None
|
619 |
+
|
620 |
+
def from_FF_gmpy(K1, a, K0):
|
621 |
+
"""Convert ``ModularInteger(mpz)`` to ``dtype``. """
|
622 |
+
return None
|
623 |
+
|
624 |
+
def from_ZZ_gmpy(K1, a, K0):
|
625 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
626 |
+
return None
|
627 |
+
|
628 |
+
def from_QQ_gmpy(K1, a, K0):
|
629 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
630 |
+
return None
|
631 |
+
|
632 |
+
def from_RealField(K1, a, K0):
|
633 |
+
"""Convert a real element object to ``dtype``. """
|
634 |
+
return None
|
635 |
+
|
636 |
+
def from_ComplexField(K1, a, K0):
|
637 |
+
"""Convert a complex element to ``dtype``. """
|
638 |
+
return None
|
639 |
+
|
640 |
+
def from_AlgebraicField(K1, a, K0):
|
641 |
+
"""Convert an algebraic number to ``dtype``. """
|
642 |
+
return None
|
643 |
+
|
644 |
+
def from_PolynomialRing(K1, a, K0):
|
645 |
+
"""Convert a polynomial to ``dtype``. """
|
646 |
+
if a.is_ground:
|
647 |
+
return K1.convert(a.LC, K0.dom)
|
648 |
+
|
649 |
+
def from_FractionField(K1, a, K0):
|
650 |
+
"""Convert a rational function to ``dtype``. """
|
651 |
+
return None
|
652 |
+
|
653 |
+
def from_MonogenicFiniteExtension(K1, a, K0):
|
654 |
+
"""Convert an ``ExtensionElement`` to ``dtype``. """
|
655 |
+
return K1.convert_from(a.rep, K0.ring)
|
656 |
+
|
657 |
+
def from_ExpressionDomain(K1, a, K0):
|
658 |
+
"""Convert a ``EX`` object to ``dtype``. """
|
659 |
+
return K1.from_sympy(a.ex)
|
660 |
+
|
661 |
+
def from_ExpressionRawDomain(K1, a, K0):
|
662 |
+
"""Convert a ``EX`` object to ``dtype``. """
|
663 |
+
return K1.from_sympy(a)
|
664 |
+
|
665 |
+
def from_GlobalPolynomialRing(K1, a, K0):
|
666 |
+
"""Convert a polynomial to ``dtype``. """
|
667 |
+
if a.degree() <= 0:
|
668 |
+
return K1.convert(a.LC(), K0.dom)
|
669 |
+
|
670 |
+
def from_GeneralizedPolynomialRing(K1, a, K0):
|
671 |
+
return K1.from_FractionField(a, K0)
|
672 |
+
|
673 |
+
def unify_with_symbols(K0, K1, symbols):
|
674 |
+
if (K0.is_Composite and (set(K0.symbols) & set(symbols))) or (K1.is_Composite and (set(K1.symbols) & set(symbols))):
|
675 |
+
raise UnificationFailed("Cannot unify %s with %s, given %s generators" % (K0, K1, tuple(symbols)))
|
676 |
+
|
677 |
+
return K0.unify(K1)
|
678 |
+
|
679 |
+
def unify(K0, K1, symbols=None):
|
680 |
+
"""
|
681 |
+
Construct a minimal domain that contains elements of ``K0`` and ``K1``.
|
682 |
+
|
683 |
+
Known domains (from smallest to largest):
|
684 |
+
|
685 |
+
- ``GF(p)``
|
686 |
+
- ``ZZ``
|
687 |
+
- ``QQ``
|
688 |
+
- ``RR(prec, tol)``
|
689 |
+
- ``CC(prec, tol)``
|
690 |
+
- ``ALG(a, b, c)``
|
691 |
+
- ``K[x, y, z]``
|
692 |
+
- ``K(x, y, z)``
|
693 |
+
- ``EX``
|
694 |
+
|
695 |
+
"""
|
696 |
+
if symbols is not None:
|
697 |
+
return K0.unify_with_symbols(K1, symbols)
|
698 |
+
|
699 |
+
if K0 == K1:
|
700 |
+
return K0
|
701 |
+
|
702 |
+
if K0.is_EXRAW:
|
703 |
+
return K0
|
704 |
+
if K1.is_EXRAW:
|
705 |
+
return K1
|
706 |
+
|
707 |
+
if K0.is_EX:
|
708 |
+
return K0
|
709 |
+
if K1.is_EX:
|
710 |
+
return K1
|
711 |
+
|
712 |
+
if K0.is_FiniteExtension or K1.is_FiniteExtension:
|
713 |
+
if K1.is_FiniteExtension:
|
714 |
+
K0, K1 = K1, K0
|
715 |
+
if K1.is_FiniteExtension:
|
716 |
+
# Unifying two extensions.
|
717 |
+
# Try to ensure that K0.unify(K1) == K1.unify(K0)
|
718 |
+
if list(ordered([K0.modulus, K1.modulus]))[1] == K0.modulus:
|
719 |
+
K0, K1 = K1, K0
|
720 |
+
return K1.set_domain(K0)
|
721 |
+
else:
|
722 |
+
# Drop the generator from other and unify with the base domain
|
723 |
+
K1 = K1.drop(K0.symbol)
|
724 |
+
K1 = K0.domain.unify(K1)
|
725 |
+
return K0.set_domain(K1)
|
726 |
+
|
727 |
+
if K0.is_Composite or K1.is_Composite:
|
728 |
+
K0_ground = K0.dom if K0.is_Composite else K0
|
729 |
+
K1_ground = K1.dom if K1.is_Composite else K1
|
730 |
+
|
731 |
+
K0_symbols = K0.symbols if K0.is_Composite else ()
|
732 |
+
K1_symbols = K1.symbols if K1.is_Composite else ()
|
733 |
+
|
734 |
+
domain = K0_ground.unify(K1_ground)
|
735 |
+
symbols = _unify_gens(K0_symbols, K1_symbols)
|
736 |
+
order = K0.order if K0.is_Composite else K1.order
|
737 |
+
|
738 |
+
if ((K0.is_FractionField and K1.is_PolynomialRing or
|
739 |
+
K1.is_FractionField and K0.is_PolynomialRing) and
|
740 |
+
(not K0_ground.is_Field or not K1_ground.is_Field) and domain.is_Field
|
741 |
+
and domain.has_assoc_Ring):
|
742 |
+
domain = domain.get_ring()
|
743 |
+
|
744 |
+
if K0.is_Composite and (not K1.is_Composite or K0.is_FractionField or K1.is_PolynomialRing):
|
745 |
+
cls = K0.__class__
|
746 |
+
else:
|
747 |
+
cls = K1.__class__
|
748 |
+
|
749 |
+
from sympy.polys.domains.old_polynomialring import GlobalPolynomialRing
|
750 |
+
if cls == GlobalPolynomialRing:
|
751 |
+
return cls(domain, symbols)
|
752 |
+
|
753 |
+
return cls(domain, symbols, order)
|
754 |
+
|
755 |
+
def mkinexact(cls, K0, K1):
|
756 |
+
prec = max(K0.precision, K1.precision)
|
757 |
+
tol = max(K0.tolerance, K1.tolerance)
|
758 |
+
return cls(prec=prec, tol=tol)
|
759 |
+
|
760 |
+
if K1.is_ComplexField:
|
761 |
+
K0, K1 = K1, K0
|
762 |
+
if K0.is_ComplexField:
|
763 |
+
if K1.is_ComplexField or K1.is_RealField:
|
764 |
+
return mkinexact(K0.__class__, K0, K1)
|
765 |
+
else:
|
766 |
+
return K0
|
767 |
+
|
768 |
+
if K1.is_RealField:
|
769 |
+
K0, K1 = K1, K0
|
770 |
+
if K0.is_RealField:
|
771 |
+
if K1.is_RealField:
|
772 |
+
return mkinexact(K0.__class__, K0, K1)
|
773 |
+
elif K1.is_GaussianRing or K1.is_GaussianField:
|
774 |
+
from sympy.polys.domains.complexfield import ComplexField
|
775 |
+
return ComplexField(prec=K0.precision, tol=K0.tolerance)
|
776 |
+
else:
|
777 |
+
return K0
|
778 |
+
|
779 |
+
if K1.is_AlgebraicField:
|
780 |
+
K0, K1 = K1, K0
|
781 |
+
if K0.is_AlgebraicField:
|
782 |
+
if K1.is_GaussianRing:
|
783 |
+
K1 = K1.get_field()
|
784 |
+
if K1.is_GaussianField:
|
785 |
+
K1 = K1.as_AlgebraicField()
|
786 |
+
if K1.is_AlgebraicField:
|
787 |
+
return K0.__class__(K0.dom.unify(K1.dom), *_unify_gens(K0.orig_ext, K1.orig_ext))
|
788 |
+
else:
|
789 |
+
return K0
|
790 |
+
|
791 |
+
if K0.is_GaussianField:
|
792 |
+
return K0
|
793 |
+
if K1.is_GaussianField:
|
794 |
+
return K1
|
795 |
+
|
796 |
+
if K0.is_GaussianRing:
|
797 |
+
if K1.is_RationalField:
|
798 |
+
K0 = K0.get_field()
|
799 |
+
return K0
|
800 |
+
if K1.is_GaussianRing:
|
801 |
+
if K0.is_RationalField:
|
802 |
+
K1 = K1.get_field()
|
803 |
+
return K1
|
804 |
+
|
805 |
+
if K0.is_RationalField:
|
806 |
+
return K0
|
807 |
+
if K1.is_RationalField:
|
808 |
+
return K1
|
809 |
+
|
810 |
+
if K0.is_IntegerRing:
|
811 |
+
return K0
|
812 |
+
if K1.is_IntegerRing:
|
813 |
+
return K1
|
814 |
+
|
815 |
+
if K0.is_FiniteField and K1.is_FiniteField:
|
816 |
+
return K0.__class__(max(K0.mod, K1.mod, key=default_sort_key))
|
817 |
+
|
818 |
+
from sympy.polys.domains import EX
|
819 |
+
return EX
|
820 |
+
|
821 |
+
def __eq__(self, other):
|
822 |
+
"""Returns ``True`` if two domains are equivalent. """
|
823 |
+
return isinstance(other, Domain) and self.dtype == other.dtype
|
824 |
+
|
825 |
+
def __ne__(self, other):
|
826 |
+
"""Returns ``False`` if two domains are equivalent. """
|
827 |
+
return not self == other
|
828 |
+
|
829 |
+
def map(self, seq):
|
830 |
+
"""Rersively apply ``self`` to all elements of ``seq``. """
|
831 |
+
result = []
|
832 |
+
|
833 |
+
for elt in seq:
|
834 |
+
if isinstance(elt, list):
|
835 |
+
result.append(self.map(elt))
|
836 |
+
else:
|
837 |
+
result.append(self(elt))
|
838 |
+
|
839 |
+
return result
|
840 |
+
|
841 |
+
def get_ring(self):
|
842 |
+
"""Returns a ring associated with ``self``. """
|
843 |
+
raise DomainError('there is no ring associated with %s' % self)
|
844 |
+
|
845 |
+
def get_field(self):
|
846 |
+
"""Returns a field associated with ``self``. """
|
847 |
+
raise DomainError('there is no field associated with %s' % self)
|
848 |
+
|
849 |
+
def get_exact(self):
|
850 |
+
"""Returns an exact domain associated with ``self``. """
|
851 |
+
return self
|
852 |
+
|
853 |
+
def __getitem__(self, symbols):
|
854 |
+
"""The mathematical way to make a polynomial ring. """
|
855 |
+
if hasattr(symbols, '__iter__'):
|
856 |
+
return self.poly_ring(*symbols)
|
857 |
+
else:
|
858 |
+
return self.poly_ring(symbols)
|
859 |
+
|
860 |
+
def poly_ring(self, *symbols, order=lex):
|
861 |
+
"""Returns a polynomial ring, i.e. `K[X]`. """
|
862 |
+
from sympy.polys.domains.polynomialring import PolynomialRing
|
863 |
+
return PolynomialRing(self, symbols, order)
|
864 |
+
|
865 |
+
def frac_field(self, *symbols, order=lex):
|
866 |
+
"""Returns a fraction field, i.e. `K(X)`. """
|
867 |
+
from sympy.polys.domains.fractionfield import FractionField
|
868 |
+
return FractionField(self, symbols, order)
|
869 |
+
|
870 |
+
def old_poly_ring(self, *symbols, **kwargs):
|
871 |
+
"""Returns a polynomial ring, i.e. `K[X]`. """
|
872 |
+
from sympy.polys.domains.old_polynomialring import PolynomialRing
|
873 |
+
return PolynomialRing(self, *symbols, **kwargs)
|
874 |
+
|
875 |
+
def old_frac_field(self, *symbols, **kwargs):
|
876 |
+
"""Returns a fraction field, i.e. `K(X)`. """
|
877 |
+
from sympy.polys.domains.old_fractionfield import FractionField
|
878 |
+
return FractionField(self, *symbols, **kwargs)
|
879 |
+
|
880 |
+
def algebraic_field(self, *extension, alias=None):
|
881 |
+
r"""Returns an algebraic field, i.e. `K(\alpha, \ldots)`. """
|
882 |
+
raise DomainError("Cannot create algebraic field over %s" % self)
|
883 |
+
|
884 |
+
def alg_field_from_poly(self, poly, alias=None, root_index=-1):
|
885 |
+
r"""
|
886 |
+
Convenience method to construct an algebraic extension on a root of a
|
887 |
+
polynomial, chosen by root index.
|
888 |
+
|
889 |
+
Parameters
|
890 |
+
==========
|
891 |
+
|
892 |
+
poly : :py:class:`~.Poly`
|
893 |
+
The polynomial whose root generates the extension.
|
894 |
+
alias : str, optional (default=None)
|
895 |
+
Symbol name for the generator of the extension.
|
896 |
+
E.g. "alpha" or "theta".
|
897 |
+
root_index : int, optional (default=-1)
|
898 |
+
Specifies which root of the polynomial is desired. The ordering is
|
899 |
+
as defined by the :py:class:`~.ComplexRootOf` class. The default of
|
900 |
+
``-1`` selects the most natural choice in the common cases of
|
901 |
+
quadratic and cyclotomic fields (the square root on the positive
|
902 |
+
real or imaginary axis, resp. $\mathrm{e}^{2\pi i/n}$).
|
903 |
+
|
904 |
+
Examples
|
905 |
+
========
|
906 |
+
|
907 |
+
>>> from sympy import QQ, Poly
|
908 |
+
>>> from sympy.abc import x
|
909 |
+
>>> f = Poly(x**2 - 2)
|
910 |
+
>>> K = QQ.alg_field_from_poly(f)
|
911 |
+
>>> K.ext.minpoly == f
|
912 |
+
True
|
913 |
+
>>> g = Poly(8*x**3 - 6*x - 1)
|
914 |
+
>>> L = QQ.alg_field_from_poly(g, "alpha")
|
915 |
+
>>> L.ext.minpoly == g
|
916 |
+
True
|
917 |
+
>>> L.to_sympy(L([1, 1, 1]))
|
918 |
+
alpha**2 + alpha + 1
|
919 |
+
|
920 |
+
"""
|
921 |
+
from sympy.polys.rootoftools import CRootOf
|
922 |
+
root = CRootOf(poly, root_index)
|
923 |
+
alpha = AlgebraicNumber(root, alias=alias)
|
924 |
+
return self.algebraic_field(alpha, alias=alias)
|
925 |
+
|
926 |
+
def cyclotomic_field(self, n, ss=False, alias="zeta", gen=None, root_index=-1):
|
927 |
+
r"""
|
928 |
+
Convenience method to construct a cyclotomic field.
|
929 |
+
|
930 |
+
Parameters
|
931 |
+
==========
|
932 |
+
|
933 |
+
n : int
|
934 |
+
Construct the nth cyclotomic field.
|
935 |
+
ss : boolean, optional (default=False)
|
936 |
+
If True, append *n* as a subscript on the alias string.
|
937 |
+
alias : str, optional (default="zeta")
|
938 |
+
Symbol name for the generator.
|
939 |
+
gen : :py:class:`~.Symbol`, optional (default=None)
|
940 |
+
Desired variable for the cyclotomic polynomial that defines the
|
941 |
+
field. If ``None``, a dummy variable will be used.
|
942 |
+
root_index : int, optional (default=-1)
|
943 |
+
Specifies which root of the polynomial is desired. The ordering is
|
944 |
+
as defined by the :py:class:`~.ComplexRootOf` class. The default of
|
945 |
+
``-1`` selects the root $\mathrm{e}^{2\pi i/n}$.
|
946 |
+
|
947 |
+
Examples
|
948 |
+
========
|
949 |
+
|
950 |
+
>>> from sympy import QQ, latex
|
951 |
+
>>> K = QQ.cyclotomic_field(5)
|
952 |
+
>>> K.to_sympy(K([-1, 1]))
|
953 |
+
1 - zeta
|
954 |
+
>>> L = QQ.cyclotomic_field(7, True)
|
955 |
+
>>> a = L.to_sympy(L([-1, 1]))
|
956 |
+
>>> print(a)
|
957 |
+
1 - zeta7
|
958 |
+
>>> print(latex(a))
|
959 |
+
1 - \zeta_{7}
|
960 |
+
|
961 |
+
"""
|
962 |
+
from sympy.polys.specialpolys import cyclotomic_poly
|
963 |
+
if ss:
|
964 |
+
alias += str(n)
|
965 |
+
return self.alg_field_from_poly(cyclotomic_poly(n, gen), alias=alias,
|
966 |
+
root_index=root_index)
|
967 |
+
|
968 |
+
def inject(self, *symbols):
|
969 |
+
"""Inject generators into this domain. """
|
970 |
+
raise NotImplementedError
|
971 |
+
|
972 |
+
def drop(self, *symbols):
|
973 |
+
"""Drop generators from this domain. """
|
974 |
+
if self.is_Simple:
|
975 |
+
return self
|
976 |
+
raise NotImplementedError # pragma: no cover
|
977 |
+
|
978 |
+
def is_zero(self, a):
|
979 |
+
"""Returns True if ``a`` is zero. """
|
980 |
+
return not a
|
981 |
+
|
982 |
+
def is_one(self, a):
|
983 |
+
"""Returns True if ``a`` is one. """
|
984 |
+
return a == self.one
|
985 |
+
|
986 |
+
def is_positive(self, a):
|
987 |
+
"""Returns True if ``a`` is positive. """
|
988 |
+
return a > 0
|
989 |
+
|
990 |
+
def is_negative(self, a):
|
991 |
+
"""Returns True if ``a`` is negative. """
|
992 |
+
return a < 0
|
993 |
+
|
994 |
+
def is_nonpositive(self, a):
|
995 |
+
"""Returns True if ``a`` is non-positive. """
|
996 |
+
return a <= 0
|
997 |
+
|
998 |
+
def is_nonnegative(self, a):
|
999 |
+
"""Returns True if ``a`` is non-negative. """
|
1000 |
+
return a >= 0
|
1001 |
+
|
1002 |
+
def canonical_unit(self, a):
|
1003 |
+
if self.is_negative(a):
|
1004 |
+
return -self.one
|
1005 |
+
else:
|
1006 |
+
return self.one
|
1007 |
+
|
1008 |
+
def abs(self, a):
|
1009 |
+
"""Absolute value of ``a``, implies ``__abs__``. """
|
1010 |
+
return abs(a)
|
1011 |
+
|
1012 |
+
def neg(self, a):
|
1013 |
+
"""Returns ``a`` negated, implies ``__neg__``. """
|
1014 |
+
return -a
|
1015 |
+
|
1016 |
+
def pos(self, a):
|
1017 |
+
"""Returns ``a`` positive, implies ``__pos__``. """
|
1018 |
+
return +a
|
1019 |
+
|
1020 |
+
def add(self, a, b):
|
1021 |
+
"""Sum of ``a`` and ``b``, implies ``__add__``. """
|
1022 |
+
return a + b
|
1023 |
+
|
1024 |
+
def sub(self, a, b):
|
1025 |
+
"""Difference of ``a`` and ``b``, implies ``__sub__``. """
|
1026 |
+
return a - b
|
1027 |
+
|
1028 |
+
def mul(self, a, b):
|
1029 |
+
"""Product of ``a`` and ``b``, implies ``__mul__``. """
|
1030 |
+
return a * b
|
1031 |
+
|
1032 |
+
def pow(self, a, b):
|
1033 |
+
"""Raise ``a`` to power ``b``, implies ``__pow__``. """
|
1034 |
+
return a ** b
|
1035 |
+
|
1036 |
+
def exquo(self, a, b):
|
1037 |
+
"""Exact quotient of *a* and *b*. Analogue of ``a / b``.
|
1038 |
+
|
1039 |
+
Explanation
|
1040 |
+
===========
|
1041 |
+
|
1042 |
+
This is essentially the same as ``a / b`` except that an error will be
|
1043 |
+
raised if the division is inexact (if there is any remainder) and the
|
1044 |
+
result will always be a domain element. When working in a
|
1045 |
+
:py:class:`~.Domain` that is not a :py:class:`~.Field` (e.g. :ref:`ZZ`
|
1046 |
+
or :ref:`K[x]`) ``exquo`` should be used instead of ``/``.
|
1047 |
+
|
1048 |
+
The key invariant is that if ``q = K.exquo(a, b)`` (and ``exquo`` does
|
1049 |
+
not raise an exception) then ``a == b*q``.
|
1050 |
+
|
1051 |
+
Examples
|
1052 |
+
========
|
1053 |
+
|
1054 |
+
We can use ``K.exquo`` instead of ``/`` for exact division.
|
1055 |
+
|
1056 |
+
>>> from sympy import ZZ
|
1057 |
+
>>> ZZ.exquo(ZZ(4), ZZ(2))
|
1058 |
+
2
|
1059 |
+
>>> ZZ.exquo(ZZ(5), ZZ(2))
|
1060 |
+
Traceback (most recent call last):
|
1061 |
+
...
|
1062 |
+
ExactQuotientFailed: 2 does not divide 5 in ZZ
|
1063 |
+
|
1064 |
+
Over a :py:class:`~.Field` such as :ref:`QQ`, division (with nonzero
|
1065 |
+
divisor) is always exact so in that case ``/`` can be used instead of
|
1066 |
+
:py:meth:`~.Domain.exquo`.
|
1067 |
+
|
1068 |
+
>>> from sympy import QQ
|
1069 |
+
>>> QQ.exquo(QQ(5), QQ(2))
|
1070 |
+
5/2
|
1071 |
+
>>> QQ(5) / QQ(2)
|
1072 |
+
5/2
|
1073 |
+
|
1074 |
+
Parameters
|
1075 |
+
==========
|
1076 |
+
|
1077 |
+
a: domain element
|
1078 |
+
The dividend
|
1079 |
+
b: domain element
|
1080 |
+
The divisor
|
1081 |
+
|
1082 |
+
Returns
|
1083 |
+
=======
|
1084 |
+
|
1085 |
+
q: domain element
|
1086 |
+
The exact quotient
|
1087 |
+
|
1088 |
+
Raises
|
1089 |
+
======
|
1090 |
+
|
1091 |
+
ExactQuotientFailed: if exact division is not possible.
|
1092 |
+
ZeroDivisionError: when the divisor is zero.
|
1093 |
+
|
1094 |
+
See also
|
1095 |
+
========
|
1096 |
+
|
1097 |
+
quo: Analogue of ``a // b``
|
1098 |
+
rem: Analogue of ``a % b``
|
1099 |
+
div: Analogue of ``divmod(a, b)``
|
1100 |
+
|
1101 |
+
Notes
|
1102 |
+
=====
|
1103 |
+
|
1104 |
+
Since the default :py:attr:`~.Domain.dtype` for :ref:`ZZ` is ``int``
|
1105 |
+
(or ``mpz``) division as ``a / b`` should not be used as it would give
|
1106 |
+
a ``float``.
|
1107 |
+
|
1108 |
+
>>> ZZ(4) / ZZ(2)
|
1109 |
+
2.0
|
1110 |
+
>>> ZZ(5) / ZZ(2)
|
1111 |
+
2.5
|
1112 |
+
|
1113 |
+
Using ``/`` with :ref:`ZZ` will lead to incorrect results so
|
1114 |
+
:py:meth:`~.Domain.exquo` should be used instead.
|
1115 |
+
|
1116 |
+
"""
|
1117 |
+
raise NotImplementedError
|
1118 |
+
|
1119 |
+
def quo(self, a, b):
|
1120 |
+
"""Quotient of *a* and *b*. Analogue of ``a // b``.
|
1121 |
+
|
1122 |
+
``K.quo(a, b)`` is equivalent to ``K.div(a, b)[0]``. See
|
1123 |
+
:py:meth:`~.Domain.div` for more explanation.
|
1124 |
+
|
1125 |
+
See also
|
1126 |
+
========
|
1127 |
+
|
1128 |
+
rem: Analogue of ``a % b``
|
1129 |
+
div: Analogue of ``divmod(a, b)``
|
1130 |
+
exquo: Analogue of ``a / b``
|
1131 |
+
"""
|
1132 |
+
raise NotImplementedError
|
1133 |
+
|
1134 |
+
def rem(self, a, b):
|
1135 |
+
"""Modulo division of *a* and *b*. Analogue of ``a % b``.
|
1136 |
+
|
1137 |
+
``K.rem(a, b)`` is equivalent to ``K.div(a, b)[1]``. See
|
1138 |
+
:py:meth:`~.Domain.div` for more explanation.
|
1139 |
+
|
1140 |
+
See also
|
1141 |
+
========
|
1142 |
+
|
1143 |
+
quo: Analogue of ``a // b``
|
1144 |
+
div: Analogue of ``divmod(a, b)``
|
1145 |
+
exquo: Analogue of ``a / b``
|
1146 |
+
"""
|
1147 |
+
raise NotImplementedError
|
1148 |
+
|
1149 |
+
def div(self, a, b):
|
1150 |
+
"""Quotient and remainder for *a* and *b*. Analogue of ``divmod(a, b)``
|
1151 |
+
|
1152 |
+
Explanation
|
1153 |
+
===========
|
1154 |
+
|
1155 |
+
This is essentially the same as ``divmod(a, b)`` except that is more
|
1156 |
+
consistent when working over some :py:class:`~.Field` domains such as
|
1157 |
+
:ref:`QQ`. When working over an arbitrary :py:class:`~.Domain` the
|
1158 |
+
:py:meth:`~.Domain.div` method should be used instead of ``divmod``.
|
1159 |
+
|
1160 |
+
The key invariant is that if ``q, r = K.div(a, b)`` then
|
1161 |
+
``a == b*q + r``.
|
1162 |
+
|
1163 |
+
The result of ``K.div(a, b)`` is the same as the tuple
|
1164 |
+
``(K.quo(a, b), K.rem(a, b))`` except that if both quotient and
|
1165 |
+
remainder are needed then it is more efficient to use
|
1166 |
+
:py:meth:`~.Domain.div`.
|
1167 |
+
|
1168 |
+
Examples
|
1169 |
+
========
|
1170 |
+
|
1171 |
+
We can use ``K.div`` instead of ``divmod`` for floor division and
|
1172 |
+
remainder.
|
1173 |
+
|
1174 |
+
>>> from sympy import ZZ, QQ
|
1175 |
+
>>> ZZ.div(ZZ(5), ZZ(2))
|
1176 |
+
(2, 1)
|
1177 |
+
|
1178 |
+
If ``K`` is a :py:class:`~.Field` then the division is always exact
|
1179 |
+
with a remainder of :py:attr:`~.Domain.zero`.
|
1180 |
+
|
1181 |
+
>>> QQ.div(QQ(5), QQ(2))
|
1182 |
+
(5/2, 0)
|
1183 |
+
|
1184 |
+
Parameters
|
1185 |
+
==========
|
1186 |
+
|
1187 |
+
a: domain element
|
1188 |
+
The dividend
|
1189 |
+
b: domain element
|
1190 |
+
The divisor
|
1191 |
+
|
1192 |
+
Returns
|
1193 |
+
=======
|
1194 |
+
|
1195 |
+
(q, r): tuple of domain elements
|
1196 |
+
The quotient and remainder
|
1197 |
+
|
1198 |
+
Raises
|
1199 |
+
======
|
1200 |
+
|
1201 |
+
ZeroDivisionError: when the divisor is zero.
|
1202 |
+
|
1203 |
+
See also
|
1204 |
+
========
|
1205 |
+
|
1206 |
+
quo: Analogue of ``a // b``
|
1207 |
+
rem: Analogue of ``a % b``
|
1208 |
+
exquo: Analogue of ``a / b``
|
1209 |
+
|
1210 |
+
Notes
|
1211 |
+
=====
|
1212 |
+
|
1213 |
+
If ``gmpy`` is installed then the ``gmpy.mpq`` type will be used as
|
1214 |
+
the :py:attr:`~.Domain.dtype` for :ref:`QQ`. The ``gmpy.mpq`` type
|
1215 |
+
defines ``divmod`` in a way that is undesirable so
|
1216 |
+
:py:meth:`~.Domain.div` should be used instead of ``divmod``.
|
1217 |
+
|
1218 |
+
>>> a = QQ(1)
|
1219 |
+
>>> b = QQ(3, 2)
|
1220 |
+
>>> a # doctest: +SKIP
|
1221 |
+
mpq(1,1)
|
1222 |
+
>>> b # doctest: +SKIP
|
1223 |
+
mpq(3,2)
|
1224 |
+
>>> divmod(a, b) # doctest: +SKIP
|
1225 |
+
(mpz(0), mpq(1,1))
|
1226 |
+
>>> QQ.div(a, b) # doctest: +SKIP
|
1227 |
+
(mpq(2,3), mpq(0,1))
|
1228 |
+
|
1229 |
+
Using ``//`` or ``%`` with :ref:`QQ` will lead to incorrect results so
|
1230 |
+
:py:meth:`~.Domain.div` should be used instead.
|
1231 |
+
|
1232 |
+
"""
|
1233 |
+
raise NotImplementedError
|
1234 |
+
|
1235 |
+
def invert(self, a, b):
|
1236 |
+
"""Returns inversion of ``a mod b``, implies something. """
|
1237 |
+
raise NotImplementedError
|
1238 |
+
|
1239 |
+
def revert(self, a):
|
1240 |
+
"""Returns ``a**(-1)`` if possible. """
|
1241 |
+
raise NotImplementedError
|
1242 |
+
|
1243 |
+
def numer(self, a):
|
1244 |
+
"""Returns numerator of ``a``. """
|
1245 |
+
raise NotImplementedError
|
1246 |
+
|
1247 |
+
def denom(self, a):
|
1248 |
+
"""Returns denominator of ``a``. """
|
1249 |
+
raise NotImplementedError
|
1250 |
+
|
1251 |
+
def half_gcdex(self, a, b):
|
1252 |
+
"""Half extended GCD of ``a`` and ``b``. """
|
1253 |
+
s, t, h = self.gcdex(a, b)
|
1254 |
+
return s, h
|
1255 |
+
|
1256 |
+
def gcdex(self, a, b):
|
1257 |
+
"""Extended GCD of ``a`` and ``b``. """
|
1258 |
+
raise NotImplementedError
|
1259 |
+
|
1260 |
+
def cofactors(self, a, b):
|
1261 |
+
"""Returns GCD and cofactors of ``a`` and ``b``. """
|
1262 |
+
gcd = self.gcd(a, b)
|
1263 |
+
cfa = self.quo(a, gcd)
|
1264 |
+
cfb = self.quo(b, gcd)
|
1265 |
+
return gcd, cfa, cfb
|
1266 |
+
|
1267 |
+
def gcd(self, a, b):
|
1268 |
+
"""Returns GCD of ``a`` and ``b``. """
|
1269 |
+
raise NotImplementedError
|
1270 |
+
|
1271 |
+
def lcm(self, a, b):
|
1272 |
+
"""Returns LCM of ``a`` and ``b``. """
|
1273 |
+
raise NotImplementedError
|
1274 |
+
|
1275 |
+
def log(self, a, b):
|
1276 |
+
"""Returns b-base logarithm of ``a``. """
|
1277 |
+
raise NotImplementedError
|
1278 |
+
|
1279 |
+
def sqrt(self, a):
|
1280 |
+
"""Returns square root of ``a``. """
|
1281 |
+
raise NotImplementedError
|
1282 |
+
|
1283 |
+
def evalf(self, a, prec=None, **options):
|
1284 |
+
"""Returns numerical approximation of ``a``. """
|
1285 |
+
return self.to_sympy(a).evalf(prec, **options)
|
1286 |
+
|
1287 |
+
n = evalf
|
1288 |
+
|
1289 |
+
def real(self, a):
|
1290 |
+
return a
|
1291 |
+
|
1292 |
+
def imag(self, a):
|
1293 |
+
return self.zero
|
1294 |
+
|
1295 |
+
def almosteq(self, a, b, tolerance=None):
|
1296 |
+
"""Check if ``a`` and ``b`` are almost equal. """
|
1297 |
+
return a == b
|
1298 |
+
|
1299 |
+
def characteristic(self):
|
1300 |
+
"""Return the characteristic of this domain. """
|
1301 |
+
raise NotImplementedError('characteristic()')
|
1302 |
+
|
1303 |
+
|
1304 |
+
__all__ = ['Domain']
|
venv/lib/python3.10/site-packages/sympy/polys/domains/domainelement.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Trait for implementing domain elements. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.utilities import public
|
5 |
+
|
6 |
+
@public
|
7 |
+
class DomainElement:
|
8 |
+
"""
|
9 |
+
Represents an element of a domain.
|
10 |
+
|
11 |
+
Mix in this trait into a class whose instances should be recognized as
|
12 |
+
elements of a domain. Method ``parent()`` gives that domain.
|
13 |
+
"""
|
14 |
+
|
15 |
+
__slots__ = ()
|
16 |
+
|
17 |
+
def parent(self):
|
18 |
+
"""Get the domain associated with ``self``
|
19 |
+
|
20 |
+
Examples
|
21 |
+
========
|
22 |
+
|
23 |
+
>>> from sympy import ZZ, symbols
|
24 |
+
>>> x, y = symbols('x, y')
|
25 |
+
>>> K = ZZ[x,y]
|
26 |
+
>>> p = K(x)**2 + K(y)**2
|
27 |
+
>>> p
|
28 |
+
x**2 + y**2
|
29 |
+
>>> p.parent()
|
30 |
+
ZZ[x,y]
|
31 |
+
|
32 |
+
Notes
|
33 |
+
=====
|
34 |
+
|
35 |
+
This is used by :py:meth:`~.Domain.convert` to identify the domain
|
36 |
+
associated with a domain element.
|
37 |
+
"""
|
38 |
+
raise NotImplementedError("abstract method")
|
venv/lib/python3.10/site-packages/sympy/polys/domains/expressiondomain.py
ADDED
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`ExpressionDomain` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.core import sympify, SympifyError
|
5 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
6 |
+
from sympy.polys.domains.field import Field
|
7 |
+
from sympy.polys.domains.simpledomain import SimpleDomain
|
8 |
+
from sympy.polys.polyutils import PicklableWithSlots
|
9 |
+
from sympy.utilities import public
|
10 |
+
|
11 |
+
eflags = {"deep": False, "mul": True, "power_exp": False, "power_base": False,
|
12 |
+
"basic": False, "multinomial": False, "log": False}
|
13 |
+
|
14 |
+
@public
|
15 |
+
class ExpressionDomain(Field, CharacteristicZero, SimpleDomain):
|
16 |
+
"""A class for arbitrary expressions. """
|
17 |
+
|
18 |
+
is_SymbolicDomain = is_EX = True
|
19 |
+
|
20 |
+
class Expression(PicklableWithSlots):
|
21 |
+
"""An arbitrary expression. """
|
22 |
+
|
23 |
+
__slots__ = ('ex',)
|
24 |
+
|
25 |
+
def __init__(self, ex):
|
26 |
+
if not isinstance(ex, self.__class__):
|
27 |
+
self.ex = sympify(ex)
|
28 |
+
else:
|
29 |
+
self.ex = ex.ex
|
30 |
+
|
31 |
+
def __repr__(f):
|
32 |
+
return 'EX(%s)' % repr(f.ex)
|
33 |
+
|
34 |
+
def __str__(f):
|
35 |
+
return 'EX(%s)' % str(f.ex)
|
36 |
+
|
37 |
+
def __hash__(self):
|
38 |
+
return hash((self.__class__.__name__, self.ex))
|
39 |
+
|
40 |
+
def as_expr(f):
|
41 |
+
return f.ex
|
42 |
+
|
43 |
+
def numer(f):
|
44 |
+
return f.__class__(f.ex.as_numer_denom()[0])
|
45 |
+
|
46 |
+
def denom(f):
|
47 |
+
return f.__class__(f.ex.as_numer_denom()[1])
|
48 |
+
|
49 |
+
def simplify(f, ex):
|
50 |
+
return f.__class__(ex.cancel().expand(**eflags))
|
51 |
+
|
52 |
+
def __abs__(f):
|
53 |
+
return f.__class__(abs(f.ex))
|
54 |
+
|
55 |
+
def __neg__(f):
|
56 |
+
return f.__class__(-f.ex)
|
57 |
+
|
58 |
+
def _to_ex(f, g):
|
59 |
+
try:
|
60 |
+
return f.__class__(g)
|
61 |
+
except SympifyError:
|
62 |
+
return None
|
63 |
+
|
64 |
+
def __add__(f, g):
|
65 |
+
g = f._to_ex(g)
|
66 |
+
|
67 |
+
if g is None:
|
68 |
+
return NotImplemented
|
69 |
+
elif g == EX.zero:
|
70 |
+
return f
|
71 |
+
elif f == EX.zero:
|
72 |
+
return g
|
73 |
+
else:
|
74 |
+
return f.simplify(f.ex + g.ex)
|
75 |
+
|
76 |
+
def __radd__(f, g):
|
77 |
+
return f.simplify(f.__class__(g).ex + f.ex)
|
78 |
+
|
79 |
+
def __sub__(f, g):
|
80 |
+
g = f._to_ex(g)
|
81 |
+
|
82 |
+
if g is None:
|
83 |
+
return NotImplemented
|
84 |
+
elif g == EX.zero:
|
85 |
+
return f
|
86 |
+
elif f == EX.zero:
|
87 |
+
return -g
|
88 |
+
else:
|
89 |
+
return f.simplify(f.ex - g.ex)
|
90 |
+
|
91 |
+
def __rsub__(f, g):
|
92 |
+
return f.simplify(f.__class__(g).ex - f.ex)
|
93 |
+
|
94 |
+
def __mul__(f, g):
|
95 |
+
g = f._to_ex(g)
|
96 |
+
|
97 |
+
if g is None:
|
98 |
+
return NotImplemented
|
99 |
+
|
100 |
+
if EX.zero in (f, g):
|
101 |
+
return EX.zero
|
102 |
+
elif f.ex.is_Number and g.ex.is_Number:
|
103 |
+
return f.__class__(f.ex*g.ex)
|
104 |
+
|
105 |
+
return f.simplify(f.ex*g.ex)
|
106 |
+
|
107 |
+
def __rmul__(f, g):
|
108 |
+
return f.simplify(f.__class__(g).ex*f.ex)
|
109 |
+
|
110 |
+
def __pow__(f, n):
|
111 |
+
n = f._to_ex(n)
|
112 |
+
|
113 |
+
if n is not None:
|
114 |
+
return f.simplify(f.ex**n.ex)
|
115 |
+
else:
|
116 |
+
return NotImplemented
|
117 |
+
|
118 |
+
def __truediv__(f, g):
|
119 |
+
g = f._to_ex(g)
|
120 |
+
|
121 |
+
if g is not None:
|
122 |
+
return f.simplify(f.ex/g.ex)
|
123 |
+
else:
|
124 |
+
return NotImplemented
|
125 |
+
|
126 |
+
def __rtruediv__(f, g):
|
127 |
+
return f.simplify(f.__class__(g).ex/f.ex)
|
128 |
+
|
129 |
+
def __eq__(f, g):
|
130 |
+
return f.ex == f.__class__(g).ex
|
131 |
+
|
132 |
+
def __ne__(f, g):
|
133 |
+
return not f == g
|
134 |
+
|
135 |
+
def __bool__(f):
|
136 |
+
return not f.ex.is_zero
|
137 |
+
|
138 |
+
def gcd(f, g):
|
139 |
+
from sympy.polys import gcd
|
140 |
+
return f.__class__(gcd(f.ex, f.__class__(g).ex))
|
141 |
+
|
142 |
+
def lcm(f, g):
|
143 |
+
from sympy.polys import lcm
|
144 |
+
return f.__class__(lcm(f.ex, f.__class__(g).ex))
|
145 |
+
|
146 |
+
dtype = Expression
|
147 |
+
|
148 |
+
zero = Expression(0)
|
149 |
+
one = Expression(1)
|
150 |
+
|
151 |
+
rep = 'EX'
|
152 |
+
|
153 |
+
has_assoc_Ring = False
|
154 |
+
has_assoc_Field = True
|
155 |
+
|
156 |
+
def __init__(self):
|
157 |
+
pass
|
158 |
+
|
159 |
+
def to_sympy(self, a):
|
160 |
+
"""Convert ``a`` to a SymPy object. """
|
161 |
+
return a.as_expr()
|
162 |
+
|
163 |
+
def from_sympy(self, a):
|
164 |
+
"""Convert SymPy's expression to ``dtype``. """
|
165 |
+
return self.dtype(a)
|
166 |
+
|
167 |
+
def from_ZZ(K1, a, K0):
|
168 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
169 |
+
return K1(K0.to_sympy(a))
|
170 |
+
|
171 |
+
def from_ZZ_python(K1, a, K0):
|
172 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
173 |
+
return K1(K0.to_sympy(a))
|
174 |
+
|
175 |
+
def from_QQ(K1, a, K0):
|
176 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
177 |
+
return K1(K0.to_sympy(a))
|
178 |
+
|
179 |
+
def from_QQ_python(K1, a, K0):
|
180 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
181 |
+
return K1(K0.to_sympy(a))
|
182 |
+
|
183 |
+
def from_ZZ_gmpy(K1, a, K0):
|
184 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
185 |
+
return K1(K0.to_sympy(a))
|
186 |
+
|
187 |
+
def from_QQ_gmpy(K1, a, K0):
|
188 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
189 |
+
return K1(K0.to_sympy(a))
|
190 |
+
|
191 |
+
def from_GaussianIntegerRing(K1, a, K0):
|
192 |
+
"""Convert a ``GaussianRational`` object to ``dtype``. """
|
193 |
+
return K1(K0.to_sympy(a))
|
194 |
+
|
195 |
+
def from_GaussianRationalField(K1, a, K0):
|
196 |
+
"""Convert a ``GaussianRational`` object to ``dtype``. """
|
197 |
+
return K1(K0.to_sympy(a))
|
198 |
+
|
199 |
+
def from_RealField(K1, a, K0):
|
200 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
201 |
+
return K1(K0.to_sympy(a))
|
202 |
+
|
203 |
+
def from_PolynomialRing(K1, a, K0):
|
204 |
+
"""Convert a ``DMP`` object to ``dtype``. """
|
205 |
+
return K1(K0.to_sympy(a))
|
206 |
+
|
207 |
+
def from_FractionField(K1, a, K0):
|
208 |
+
"""Convert a ``DMF`` object to ``dtype``. """
|
209 |
+
return K1(K0.to_sympy(a))
|
210 |
+
|
211 |
+
def from_ExpressionDomain(K1, a, K0):
|
212 |
+
"""Convert a ``EX`` object to ``dtype``. """
|
213 |
+
return a
|
214 |
+
|
215 |
+
def get_ring(self):
|
216 |
+
"""Returns a ring associated with ``self``. """
|
217 |
+
return self # XXX: EX is not a ring but we don't have much choice here.
|
218 |
+
|
219 |
+
def get_field(self):
|
220 |
+
"""Returns a field associated with ``self``. """
|
221 |
+
return self
|
222 |
+
|
223 |
+
def is_positive(self, a):
|
224 |
+
"""Returns True if ``a`` is positive. """
|
225 |
+
return a.ex.as_coeff_mul()[0].is_positive
|
226 |
+
|
227 |
+
def is_negative(self, a):
|
228 |
+
"""Returns True if ``a`` is negative. """
|
229 |
+
return a.ex.could_extract_minus_sign()
|
230 |
+
|
231 |
+
def is_nonpositive(self, a):
|
232 |
+
"""Returns True if ``a`` is non-positive. """
|
233 |
+
return a.ex.as_coeff_mul()[0].is_nonpositive
|
234 |
+
|
235 |
+
def is_nonnegative(self, a):
|
236 |
+
"""Returns True if ``a`` is non-negative. """
|
237 |
+
return a.ex.as_coeff_mul()[0].is_nonnegative
|
238 |
+
|
239 |
+
def numer(self, a):
|
240 |
+
"""Returns numerator of ``a``. """
|
241 |
+
return a.numer()
|
242 |
+
|
243 |
+
def denom(self, a):
|
244 |
+
"""Returns denominator of ``a``. """
|
245 |
+
return a.denom()
|
246 |
+
|
247 |
+
def gcd(self, a, b):
|
248 |
+
return self(1)
|
249 |
+
|
250 |
+
def lcm(self, a, b):
|
251 |
+
return a.lcm(b)
|
252 |
+
|
253 |
+
|
254 |
+
EX = ExpressionDomain()
|
venv/lib/python3.10/site-packages/sympy/polys/domains/expressionrawdomain.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`ExpressionRawDomain` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.core import Expr, S, sympify, Add
|
5 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
6 |
+
from sympy.polys.domains.field import Field
|
7 |
+
from sympy.polys.domains.simpledomain import SimpleDomain
|
8 |
+
from sympy.polys.polyerrors import CoercionFailed
|
9 |
+
from sympy.utilities import public
|
10 |
+
|
11 |
+
|
12 |
+
@public
|
13 |
+
class ExpressionRawDomain(Field, CharacteristicZero, SimpleDomain):
|
14 |
+
"""A class for arbitrary expressions but without automatic simplification. """
|
15 |
+
|
16 |
+
is_SymbolicRawDomain = is_EXRAW = True
|
17 |
+
|
18 |
+
dtype = Expr
|
19 |
+
|
20 |
+
zero = S.Zero
|
21 |
+
one = S.One
|
22 |
+
|
23 |
+
rep = 'EXRAW'
|
24 |
+
|
25 |
+
has_assoc_Ring = False
|
26 |
+
has_assoc_Field = True
|
27 |
+
|
28 |
+
def __init__(self):
|
29 |
+
pass
|
30 |
+
|
31 |
+
@classmethod
|
32 |
+
def new(self, a):
|
33 |
+
return sympify(a)
|
34 |
+
|
35 |
+
def to_sympy(self, a):
|
36 |
+
"""Convert ``a`` to a SymPy object. """
|
37 |
+
return a
|
38 |
+
|
39 |
+
def from_sympy(self, a):
|
40 |
+
"""Convert SymPy's expression to ``dtype``. """
|
41 |
+
if not isinstance(a, Expr):
|
42 |
+
raise CoercionFailed(f"Expecting an Expr instance but found: {type(a).__name__}")
|
43 |
+
return a
|
44 |
+
|
45 |
+
def convert_from(self, a, K):
|
46 |
+
"""Convert a domain element from another domain to EXRAW"""
|
47 |
+
return K.to_sympy(a)
|
48 |
+
|
49 |
+
def get_field(self):
|
50 |
+
"""Returns a field associated with ``self``. """
|
51 |
+
return self
|
52 |
+
|
53 |
+
def sum(self, items):
|
54 |
+
return Add(*items)
|
55 |
+
|
56 |
+
|
57 |
+
EXRAW = ExpressionRawDomain()
|
venv/lib/python3.10/site-packages/sympy/polys/domains/field.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`Field` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.ring import Ring
|
5 |
+
from sympy.polys.polyerrors import NotReversible, DomainError
|
6 |
+
from sympy.utilities import public
|
7 |
+
|
8 |
+
@public
|
9 |
+
class Field(Ring):
|
10 |
+
"""Represents a field domain. """
|
11 |
+
|
12 |
+
is_Field = True
|
13 |
+
is_PID = True
|
14 |
+
|
15 |
+
def get_ring(self):
|
16 |
+
"""Returns a ring associated with ``self``. """
|
17 |
+
raise DomainError('there is no ring associated with %s' % self)
|
18 |
+
|
19 |
+
def get_field(self):
|
20 |
+
"""Returns a field associated with ``self``. """
|
21 |
+
return self
|
22 |
+
|
23 |
+
def exquo(self, a, b):
|
24 |
+
"""Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
25 |
+
return a / b
|
26 |
+
|
27 |
+
def quo(self, a, b):
|
28 |
+
"""Quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
29 |
+
return a / b
|
30 |
+
|
31 |
+
def rem(self, a, b):
|
32 |
+
"""Remainder of ``a`` and ``b``, implies nothing. """
|
33 |
+
return self.zero
|
34 |
+
|
35 |
+
def div(self, a, b):
|
36 |
+
"""Division of ``a`` and ``b``, implies ``__truediv__``. """
|
37 |
+
return a / b, self.zero
|
38 |
+
|
39 |
+
def gcd(self, a, b):
|
40 |
+
"""
|
41 |
+
Returns GCD of ``a`` and ``b``.
|
42 |
+
|
43 |
+
This definition of GCD over fields allows to clear denominators
|
44 |
+
in `primitive()`.
|
45 |
+
|
46 |
+
Examples
|
47 |
+
========
|
48 |
+
|
49 |
+
>>> from sympy.polys.domains import QQ
|
50 |
+
>>> from sympy import S, gcd, primitive
|
51 |
+
>>> from sympy.abc import x
|
52 |
+
|
53 |
+
>>> QQ.gcd(QQ(2, 3), QQ(4, 9))
|
54 |
+
2/9
|
55 |
+
>>> gcd(S(2)/3, S(4)/9)
|
56 |
+
2/9
|
57 |
+
>>> primitive(2*x/3 + S(4)/9)
|
58 |
+
(2/9, 3*x + 2)
|
59 |
+
|
60 |
+
"""
|
61 |
+
try:
|
62 |
+
ring = self.get_ring()
|
63 |
+
except DomainError:
|
64 |
+
return self.one
|
65 |
+
|
66 |
+
p = ring.gcd(self.numer(a), self.numer(b))
|
67 |
+
q = ring.lcm(self.denom(a), self.denom(b))
|
68 |
+
|
69 |
+
return self.convert(p, ring)/q
|
70 |
+
|
71 |
+
def lcm(self, a, b):
|
72 |
+
"""
|
73 |
+
Returns LCM of ``a`` and ``b``.
|
74 |
+
|
75 |
+
>>> from sympy.polys.domains import QQ
|
76 |
+
>>> from sympy import S, lcm
|
77 |
+
|
78 |
+
>>> QQ.lcm(QQ(2, 3), QQ(4, 9))
|
79 |
+
4/3
|
80 |
+
>>> lcm(S(2)/3, S(4)/9)
|
81 |
+
4/3
|
82 |
+
|
83 |
+
"""
|
84 |
+
|
85 |
+
try:
|
86 |
+
ring = self.get_ring()
|
87 |
+
except DomainError:
|
88 |
+
return a*b
|
89 |
+
|
90 |
+
p = ring.lcm(self.numer(a), self.numer(b))
|
91 |
+
q = ring.gcd(self.denom(a), self.denom(b))
|
92 |
+
|
93 |
+
return self.convert(p, ring)/q
|
94 |
+
|
95 |
+
def revert(self, a):
|
96 |
+
"""Returns ``a**(-1)`` if possible. """
|
97 |
+
if a:
|
98 |
+
return 1/a
|
99 |
+
else:
|
100 |
+
raise NotReversible('zero is not reversible')
|
101 |
+
|
102 |
+
def is_unit(self, a):
|
103 |
+
"""Return true if ``a`` is a invertible"""
|
104 |
+
return bool(a)
|
venv/lib/python3.10/site-packages/sympy/polys/domains/finitefield.py
ADDED
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`FiniteField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.field import Field
|
5 |
+
|
6 |
+
from sympy.polys.domains.modularinteger import ModularIntegerFactory
|
7 |
+
from sympy.polys.domains.simpledomain import SimpleDomain
|
8 |
+
from sympy.polys.polyerrors import CoercionFailed
|
9 |
+
from sympy.utilities import public
|
10 |
+
from sympy.polys.domains.groundtypes import SymPyInteger
|
11 |
+
|
12 |
+
@public
|
13 |
+
class FiniteField(Field, SimpleDomain):
|
14 |
+
r"""Finite field of prime order :ref:`GF(p)`
|
15 |
+
|
16 |
+
A :ref:`GF(p)` domain represents a `finite field`_ `\mathbb{F}_p` of prime
|
17 |
+
order as :py:class:`~.Domain` in the domain system (see
|
18 |
+
:ref:`polys-domainsintro`).
|
19 |
+
|
20 |
+
A :py:class:`~.Poly` created from an expression with integer
|
21 |
+
coefficients will have the domain :ref:`ZZ`. However, if the ``modulus=p``
|
22 |
+
option is given then the domain will be a finite field instead.
|
23 |
+
|
24 |
+
>>> from sympy import Poly, Symbol
|
25 |
+
>>> x = Symbol('x')
|
26 |
+
>>> p = Poly(x**2 + 1)
|
27 |
+
>>> p
|
28 |
+
Poly(x**2 + 1, x, domain='ZZ')
|
29 |
+
>>> p.domain
|
30 |
+
ZZ
|
31 |
+
>>> p2 = Poly(x**2 + 1, modulus=2)
|
32 |
+
>>> p2
|
33 |
+
Poly(x**2 + 1, x, modulus=2)
|
34 |
+
>>> p2.domain
|
35 |
+
GF(2)
|
36 |
+
|
37 |
+
It is possible to factorise a polynomial over :ref:`GF(p)` using the
|
38 |
+
modulus argument to :py:func:`~.factor` or by specifying the domain
|
39 |
+
explicitly. The domain can also be given as a string.
|
40 |
+
|
41 |
+
>>> from sympy import factor, GF
|
42 |
+
>>> factor(x**2 + 1)
|
43 |
+
x**2 + 1
|
44 |
+
>>> factor(x**2 + 1, modulus=2)
|
45 |
+
(x + 1)**2
|
46 |
+
>>> factor(x**2 + 1, domain=GF(2))
|
47 |
+
(x + 1)**2
|
48 |
+
>>> factor(x**2 + 1, domain='GF(2)')
|
49 |
+
(x + 1)**2
|
50 |
+
|
51 |
+
It is also possible to use :ref:`GF(p)` with the :py:func:`~.cancel`
|
52 |
+
and :py:func:`~.gcd` functions.
|
53 |
+
|
54 |
+
>>> from sympy import cancel, gcd
|
55 |
+
>>> cancel((x**2 + 1)/(x + 1))
|
56 |
+
(x**2 + 1)/(x + 1)
|
57 |
+
>>> cancel((x**2 + 1)/(x + 1), domain=GF(2))
|
58 |
+
x + 1
|
59 |
+
>>> gcd(x**2 + 1, x + 1)
|
60 |
+
1
|
61 |
+
>>> gcd(x**2 + 1, x + 1, domain=GF(2))
|
62 |
+
x + 1
|
63 |
+
|
64 |
+
When using the domain directly :ref:`GF(p)` can be used as a constructor
|
65 |
+
to create instances which then support the operations ``+,-,*,**,/``
|
66 |
+
|
67 |
+
>>> from sympy import GF
|
68 |
+
>>> K = GF(5)
|
69 |
+
>>> K
|
70 |
+
GF(5)
|
71 |
+
>>> x = K(3)
|
72 |
+
>>> y = K(2)
|
73 |
+
>>> x
|
74 |
+
3 mod 5
|
75 |
+
>>> y
|
76 |
+
2 mod 5
|
77 |
+
>>> x * y
|
78 |
+
1 mod 5
|
79 |
+
>>> x / y
|
80 |
+
4 mod 5
|
81 |
+
|
82 |
+
Notes
|
83 |
+
=====
|
84 |
+
|
85 |
+
It is also possible to create a :ref:`GF(p)` domain of **non-prime**
|
86 |
+
order but the resulting ring is **not** a field: it is just the ring of
|
87 |
+
the integers modulo ``n``.
|
88 |
+
|
89 |
+
>>> K = GF(9)
|
90 |
+
>>> z = K(3)
|
91 |
+
>>> z
|
92 |
+
3 mod 9
|
93 |
+
>>> z**2
|
94 |
+
0 mod 9
|
95 |
+
|
96 |
+
It would be good to have a proper implementation of prime power fields
|
97 |
+
(``GF(p**n)``) but these are not yet implemented in SymPY.
|
98 |
+
|
99 |
+
.. _finite field: https://en.wikipedia.org/wiki/Finite_field
|
100 |
+
"""
|
101 |
+
|
102 |
+
rep = 'FF'
|
103 |
+
alias = 'FF'
|
104 |
+
|
105 |
+
is_FiniteField = is_FF = True
|
106 |
+
is_Numerical = True
|
107 |
+
|
108 |
+
has_assoc_Ring = False
|
109 |
+
has_assoc_Field = True
|
110 |
+
|
111 |
+
dom = None
|
112 |
+
mod = None
|
113 |
+
|
114 |
+
def __init__(self, mod, symmetric=True):
|
115 |
+
from sympy.polys.domains import ZZ
|
116 |
+
dom = ZZ
|
117 |
+
|
118 |
+
if mod <= 0:
|
119 |
+
raise ValueError('modulus must be a positive integer, got %s' % mod)
|
120 |
+
|
121 |
+
self.dtype = ModularIntegerFactory(mod, dom, symmetric, self)
|
122 |
+
self.zero = self.dtype(0)
|
123 |
+
self.one = self.dtype(1)
|
124 |
+
self.dom = dom
|
125 |
+
self.mod = mod
|
126 |
+
|
127 |
+
def __str__(self):
|
128 |
+
return 'GF(%s)' % self.mod
|
129 |
+
|
130 |
+
def __hash__(self):
|
131 |
+
return hash((self.__class__.__name__, self.dtype, self.mod, self.dom))
|
132 |
+
|
133 |
+
def __eq__(self, other):
|
134 |
+
"""Returns ``True`` if two domains are equivalent. """
|
135 |
+
return isinstance(other, FiniteField) and \
|
136 |
+
self.mod == other.mod and self.dom == other.dom
|
137 |
+
|
138 |
+
def characteristic(self):
|
139 |
+
"""Return the characteristic of this domain. """
|
140 |
+
return self.mod
|
141 |
+
|
142 |
+
def get_field(self):
|
143 |
+
"""Returns a field associated with ``self``. """
|
144 |
+
return self
|
145 |
+
|
146 |
+
def to_sympy(self, a):
|
147 |
+
"""Convert ``a`` to a SymPy object. """
|
148 |
+
return SymPyInteger(int(a))
|
149 |
+
|
150 |
+
def from_sympy(self, a):
|
151 |
+
"""Convert SymPy's Integer to SymPy's ``Integer``. """
|
152 |
+
if a.is_Integer:
|
153 |
+
return self.dtype(self.dom.dtype(int(a)))
|
154 |
+
elif a.is_Float and int(a) == a:
|
155 |
+
return self.dtype(self.dom.dtype(int(a)))
|
156 |
+
else:
|
157 |
+
raise CoercionFailed("expected an integer, got %s" % a)
|
158 |
+
|
159 |
+
def from_FF(K1, a, K0=None):
|
160 |
+
"""Convert ``ModularInteger(int)`` to ``dtype``. """
|
161 |
+
return K1.dtype(K1.dom.from_ZZ(a.val, K0.dom))
|
162 |
+
|
163 |
+
def from_FF_python(K1, a, K0=None):
|
164 |
+
"""Convert ``ModularInteger(int)`` to ``dtype``. """
|
165 |
+
return K1.dtype(K1.dom.from_ZZ_python(a.val, K0.dom))
|
166 |
+
|
167 |
+
def from_ZZ(K1, a, K0=None):
|
168 |
+
"""Convert Python's ``int`` to ``dtype``. """
|
169 |
+
return K1.dtype(K1.dom.from_ZZ_python(a, K0))
|
170 |
+
|
171 |
+
def from_ZZ_python(K1, a, K0=None):
|
172 |
+
"""Convert Python's ``int`` to ``dtype``. """
|
173 |
+
return K1.dtype(K1.dom.from_ZZ_python(a, K0))
|
174 |
+
|
175 |
+
def from_QQ(K1, a, K0=None):
|
176 |
+
"""Convert Python's ``Fraction`` to ``dtype``. """
|
177 |
+
if a.denominator == 1:
|
178 |
+
return K1.from_ZZ_python(a.numerator)
|
179 |
+
|
180 |
+
def from_QQ_python(K1, a, K0=None):
|
181 |
+
"""Convert Python's ``Fraction`` to ``dtype``. """
|
182 |
+
if a.denominator == 1:
|
183 |
+
return K1.from_ZZ_python(a.numerator)
|
184 |
+
|
185 |
+
def from_FF_gmpy(K1, a, K0=None):
|
186 |
+
"""Convert ``ModularInteger(mpz)`` to ``dtype``. """
|
187 |
+
return K1.dtype(K1.dom.from_ZZ_gmpy(a.val, K0.dom))
|
188 |
+
|
189 |
+
def from_ZZ_gmpy(K1, a, K0=None):
|
190 |
+
"""Convert GMPY's ``mpz`` to ``dtype``. """
|
191 |
+
return K1.dtype(K1.dom.from_ZZ_gmpy(a, K0))
|
192 |
+
|
193 |
+
def from_QQ_gmpy(K1, a, K0=None):
|
194 |
+
"""Convert GMPY's ``mpq`` to ``dtype``. """
|
195 |
+
if a.denominator == 1:
|
196 |
+
return K1.from_ZZ_gmpy(a.numerator)
|
197 |
+
|
198 |
+
def from_RealField(K1, a, K0):
|
199 |
+
"""Convert mpmath's ``mpf`` to ``dtype``. """
|
200 |
+
p, q = K0.to_rational(a)
|
201 |
+
|
202 |
+
if q == 1:
|
203 |
+
return K1.dtype(K1.dom.dtype(p))
|
204 |
+
|
205 |
+
|
206 |
+
FF = GF = FiniteField
|
venv/lib/python3.10/site-packages/sympy/polys/domains/fractionfield.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`FractionField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.compositedomain import CompositeDomain
|
5 |
+
from sympy.polys.domains.field import Field
|
6 |
+
from sympy.polys.polyerrors import CoercionFailed, GeneratorsError
|
7 |
+
from sympy.utilities import public
|
8 |
+
|
9 |
+
@public
|
10 |
+
class FractionField(Field, CompositeDomain):
|
11 |
+
"""A class for representing multivariate rational function fields. """
|
12 |
+
|
13 |
+
is_FractionField = is_Frac = True
|
14 |
+
|
15 |
+
has_assoc_Ring = True
|
16 |
+
has_assoc_Field = True
|
17 |
+
|
18 |
+
def __init__(self, domain_or_field, symbols=None, order=None):
|
19 |
+
from sympy.polys.fields import FracField
|
20 |
+
|
21 |
+
if isinstance(domain_or_field, FracField) and symbols is None and order is None:
|
22 |
+
field = domain_or_field
|
23 |
+
else:
|
24 |
+
field = FracField(symbols, domain_or_field, order)
|
25 |
+
|
26 |
+
self.field = field
|
27 |
+
self.dtype = field.dtype
|
28 |
+
|
29 |
+
self.gens = field.gens
|
30 |
+
self.ngens = field.ngens
|
31 |
+
self.symbols = field.symbols
|
32 |
+
self.domain = field.domain
|
33 |
+
|
34 |
+
# TODO: remove this
|
35 |
+
self.dom = self.domain
|
36 |
+
|
37 |
+
def new(self, element):
|
38 |
+
return self.field.field_new(element)
|
39 |
+
|
40 |
+
@property
|
41 |
+
def zero(self):
|
42 |
+
return self.field.zero
|
43 |
+
|
44 |
+
@property
|
45 |
+
def one(self):
|
46 |
+
return self.field.one
|
47 |
+
|
48 |
+
@property
|
49 |
+
def order(self):
|
50 |
+
return self.field.order
|
51 |
+
|
52 |
+
@property
|
53 |
+
def is_Exact(self):
|
54 |
+
return self.domain.is_Exact
|
55 |
+
|
56 |
+
def get_exact(self):
|
57 |
+
return FractionField(self.domain.get_exact(), self.symbols)
|
58 |
+
|
59 |
+
def __str__(self):
|
60 |
+
return str(self.domain) + '(' + ','.join(map(str, self.symbols)) + ')'
|
61 |
+
|
62 |
+
def __hash__(self):
|
63 |
+
return hash((self.__class__.__name__, self.dtype.field, self.domain, self.symbols))
|
64 |
+
|
65 |
+
def __eq__(self, other):
|
66 |
+
"""Returns ``True`` if two domains are equivalent. """
|
67 |
+
return isinstance(other, FractionField) and \
|
68 |
+
(self.dtype.field, self.domain, self.symbols) ==\
|
69 |
+
(other.dtype.field, other.domain, other.symbols)
|
70 |
+
|
71 |
+
def to_sympy(self, a):
|
72 |
+
"""Convert ``a`` to a SymPy object. """
|
73 |
+
return a.as_expr()
|
74 |
+
|
75 |
+
def from_sympy(self, a):
|
76 |
+
"""Convert SymPy's expression to ``dtype``. """
|
77 |
+
return self.field.from_expr(a)
|
78 |
+
|
79 |
+
def from_ZZ(K1, a, K0):
|
80 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
81 |
+
return K1(K1.domain.convert(a, K0))
|
82 |
+
|
83 |
+
def from_ZZ_python(K1, a, K0):
|
84 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
85 |
+
return K1(K1.domain.convert(a, K0))
|
86 |
+
|
87 |
+
def from_QQ(K1, a, K0):
|
88 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
89 |
+
dom = K1.domain
|
90 |
+
conv = dom.convert_from
|
91 |
+
if dom.is_ZZ:
|
92 |
+
return K1(conv(K0.numer(a), K0)) / K1(conv(K0.denom(a), K0))
|
93 |
+
else:
|
94 |
+
return K1(conv(a, K0))
|
95 |
+
|
96 |
+
def from_QQ_python(K1, a, K0):
|
97 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
98 |
+
return K1(K1.domain.convert(a, K0))
|
99 |
+
|
100 |
+
def from_ZZ_gmpy(K1, a, K0):
|
101 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
102 |
+
return K1(K1.domain.convert(a, K0))
|
103 |
+
|
104 |
+
def from_QQ_gmpy(K1, a, K0):
|
105 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
106 |
+
return K1(K1.domain.convert(a, K0))
|
107 |
+
|
108 |
+
def from_GaussianRationalField(K1, a, K0):
|
109 |
+
"""Convert a ``GaussianRational`` object to ``dtype``. """
|
110 |
+
return K1(K1.domain.convert(a, K0))
|
111 |
+
|
112 |
+
def from_GaussianIntegerRing(K1, a, K0):
|
113 |
+
"""Convert a ``GaussianInteger`` object to ``dtype``. """
|
114 |
+
return K1(K1.domain.convert(a, K0))
|
115 |
+
|
116 |
+
def from_RealField(K1, a, K0):
|
117 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
118 |
+
return K1(K1.domain.convert(a, K0))
|
119 |
+
|
120 |
+
def from_ComplexField(K1, a, K0):
|
121 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
122 |
+
return K1(K1.domain.convert(a, K0))
|
123 |
+
|
124 |
+
def from_AlgebraicField(K1, a, K0):
|
125 |
+
"""Convert an algebraic number to ``dtype``. """
|
126 |
+
if K1.domain != K0:
|
127 |
+
a = K1.domain.convert_from(a, K0)
|
128 |
+
if a is not None:
|
129 |
+
return K1.new(a)
|
130 |
+
|
131 |
+
def from_PolynomialRing(K1, a, K0):
|
132 |
+
"""Convert a polynomial to ``dtype``. """
|
133 |
+
if a.is_ground:
|
134 |
+
return K1.convert_from(a.coeff(1), K0.domain)
|
135 |
+
try:
|
136 |
+
return K1.new(a.set_ring(K1.field.ring))
|
137 |
+
except (CoercionFailed, GeneratorsError):
|
138 |
+
# XXX: We get here if K1=ZZ(x,y) and K0=QQ[x,y]
|
139 |
+
# and the poly a in K0 has non-integer coefficients.
|
140 |
+
# It seems that K1.new can handle this but K1.new doesn't work
|
141 |
+
# when K0.domain is an algebraic field...
|
142 |
+
try:
|
143 |
+
return K1.new(a)
|
144 |
+
except (CoercionFailed, GeneratorsError):
|
145 |
+
return None
|
146 |
+
|
147 |
+
def from_FractionField(K1, a, K0):
|
148 |
+
"""Convert a rational function to ``dtype``. """
|
149 |
+
try:
|
150 |
+
return a.set_field(K1.field)
|
151 |
+
except (CoercionFailed, GeneratorsError):
|
152 |
+
return None
|
153 |
+
|
154 |
+
def get_ring(self):
|
155 |
+
"""Returns a field associated with ``self``. """
|
156 |
+
return self.field.to_ring().to_domain()
|
157 |
+
|
158 |
+
def is_positive(self, a):
|
159 |
+
"""Returns True if ``LC(a)`` is positive. """
|
160 |
+
return self.domain.is_positive(a.numer.LC)
|
161 |
+
|
162 |
+
def is_negative(self, a):
|
163 |
+
"""Returns True if ``LC(a)`` is negative. """
|
164 |
+
return self.domain.is_negative(a.numer.LC)
|
165 |
+
|
166 |
+
def is_nonpositive(self, a):
|
167 |
+
"""Returns True if ``LC(a)`` is non-positive. """
|
168 |
+
return self.domain.is_nonpositive(a.numer.LC)
|
169 |
+
|
170 |
+
def is_nonnegative(self, a):
|
171 |
+
"""Returns True if ``LC(a)`` is non-negative. """
|
172 |
+
return self.domain.is_nonnegative(a.numer.LC)
|
173 |
+
|
174 |
+
def numer(self, a):
|
175 |
+
"""Returns numerator of ``a``. """
|
176 |
+
return a.numer
|
177 |
+
|
178 |
+
def denom(self, a):
|
179 |
+
"""Returns denominator of ``a``. """
|
180 |
+
return a.denom
|
181 |
+
|
182 |
+
def factorial(self, a):
|
183 |
+
"""Returns factorial of ``a``. """
|
184 |
+
return self.dtype(self.domain.factorial(a))
|
venv/lib/python3.10/site-packages/sympy/polys/domains/gaussiandomains.py
ADDED
@@ -0,0 +1,645 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Domains of Gaussian type."""
|
2 |
+
|
3 |
+
from sympy.core.numbers import I
|
4 |
+
from sympy.polys.polyerrors import CoercionFailed
|
5 |
+
from sympy.polys.domains.integerring import ZZ
|
6 |
+
from sympy.polys.domains.rationalfield import QQ
|
7 |
+
from sympy.polys.domains.algebraicfield import AlgebraicField
|
8 |
+
from sympy.polys.domains.domain import Domain
|
9 |
+
from sympy.polys.domains.domainelement import DomainElement
|
10 |
+
from sympy.polys.domains.field import Field
|
11 |
+
from sympy.polys.domains.ring import Ring
|
12 |
+
|
13 |
+
|
14 |
+
class GaussianElement(DomainElement):
|
15 |
+
"""Base class for elements of Gaussian type domains."""
|
16 |
+
base: Domain
|
17 |
+
_parent: Domain
|
18 |
+
|
19 |
+
__slots__ = ('x', 'y')
|
20 |
+
|
21 |
+
def __new__(cls, x, y=0):
|
22 |
+
conv = cls.base.convert
|
23 |
+
return cls.new(conv(x), conv(y))
|
24 |
+
|
25 |
+
@classmethod
|
26 |
+
def new(cls, x, y):
|
27 |
+
"""Create a new GaussianElement of the same domain."""
|
28 |
+
obj = super().__new__(cls)
|
29 |
+
obj.x = x
|
30 |
+
obj.y = y
|
31 |
+
return obj
|
32 |
+
|
33 |
+
def parent(self):
|
34 |
+
"""The domain that this is an element of (ZZ_I or QQ_I)"""
|
35 |
+
return self._parent
|
36 |
+
|
37 |
+
def __hash__(self):
|
38 |
+
return hash((self.x, self.y))
|
39 |
+
|
40 |
+
def __eq__(self, other):
|
41 |
+
if isinstance(other, self.__class__):
|
42 |
+
return self.x == other.x and self.y == other.y
|
43 |
+
else:
|
44 |
+
return NotImplemented
|
45 |
+
|
46 |
+
def __lt__(self, other):
|
47 |
+
if not isinstance(other, GaussianElement):
|
48 |
+
return NotImplemented
|
49 |
+
return [self.y, self.x] < [other.y, other.x]
|
50 |
+
|
51 |
+
def __pos__(self):
|
52 |
+
return self
|
53 |
+
|
54 |
+
def __neg__(self):
|
55 |
+
return self.new(-self.x, -self.y)
|
56 |
+
|
57 |
+
def __repr__(self):
|
58 |
+
return "%s(%s, %s)" % (self._parent.rep, self.x, self.y)
|
59 |
+
|
60 |
+
def __str__(self):
|
61 |
+
return str(self._parent.to_sympy(self))
|
62 |
+
|
63 |
+
@classmethod
|
64 |
+
def _get_xy(cls, other):
|
65 |
+
if not isinstance(other, cls):
|
66 |
+
try:
|
67 |
+
other = cls._parent.convert(other)
|
68 |
+
except CoercionFailed:
|
69 |
+
return None, None
|
70 |
+
return other.x, other.y
|
71 |
+
|
72 |
+
def __add__(self, other):
|
73 |
+
x, y = self._get_xy(other)
|
74 |
+
if x is not None:
|
75 |
+
return self.new(self.x + x, self.y + y)
|
76 |
+
else:
|
77 |
+
return NotImplemented
|
78 |
+
|
79 |
+
__radd__ = __add__
|
80 |
+
|
81 |
+
def __sub__(self, other):
|
82 |
+
x, y = self._get_xy(other)
|
83 |
+
if x is not None:
|
84 |
+
return self.new(self.x - x, self.y - y)
|
85 |
+
else:
|
86 |
+
return NotImplemented
|
87 |
+
|
88 |
+
def __rsub__(self, other):
|
89 |
+
x, y = self._get_xy(other)
|
90 |
+
if x is not None:
|
91 |
+
return self.new(x - self.x, y - self.y)
|
92 |
+
else:
|
93 |
+
return NotImplemented
|
94 |
+
|
95 |
+
def __mul__(self, other):
|
96 |
+
x, y = self._get_xy(other)
|
97 |
+
if x is not None:
|
98 |
+
return self.new(self.x*x - self.y*y, self.x*y + self.y*x)
|
99 |
+
else:
|
100 |
+
return NotImplemented
|
101 |
+
|
102 |
+
__rmul__ = __mul__
|
103 |
+
|
104 |
+
def __pow__(self, exp):
|
105 |
+
if exp == 0:
|
106 |
+
return self.new(1, 0)
|
107 |
+
if exp < 0:
|
108 |
+
self, exp = 1/self, -exp
|
109 |
+
if exp == 1:
|
110 |
+
return self
|
111 |
+
pow2 = self
|
112 |
+
prod = self if exp % 2 else self._parent.one
|
113 |
+
exp //= 2
|
114 |
+
while exp:
|
115 |
+
pow2 *= pow2
|
116 |
+
if exp % 2:
|
117 |
+
prod *= pow2
|
118 |
+
exp //= 2
|
119 |
+
return prod
|
120 |
+
|
121 |
+
def __bool__(self):
|
122 |
+
return bool(self.x) or bool(self.y)
|
123 |
+
|
124 |
+
def quadrant(self):
|
125 |
+
"""Return quadrant index 0-3.
|
126 |
+
|
127 |
+
0 is included in quadrant 0.
|
128 |
+
"""
|
129 |
+
if self.y > 0:
|
130 |
+
return 0 if self.x > 0 else 1
|
131 |
+
elif self.y < 0:
|
132 |
+
return 2 if self.x < 0 else 3
|
133 |
+
else:
|
134 |
+
return 0 if self.x >= 0 else 2
|
135 |
+
|
136 |
+
def __rdivmod__(self, other):
|
137 |
+
try:
|
138 |
+
other = self._parent.convert(other)
|
139 |
+
except CoercionFailed:
|
140 |
+
return NotImplemented
|
141 |
+
else:
|
142 |
+
return other.__divmod__(self)
|
143 |
+
|
144 |
+
def __rtruediv__(self, other):
|
145 |
+
try:
|
146 |
+
other = QQ_I.convert(other)
|
147 |
+
except CoercionFailed:
|
148 |
+
return NotImplemented
|
149 |
+
else:
|
150 |
+
return other.__truediv__(self)
|
151 |
+
|
152 |
+
def __floordiv__(self, other):
|
153 |
+
qr = self.__divmod__(other)
|
154 |
+
return qr if qr is NotImplemented else qr[0]
|
155 |
+
|
156 |
+
def __rfloordiv__(self, other):
|
157 |
+
qr = self.__rdivmod__(other)
|
158 |
+
return qr if qr is NotImplemented else qr[0]
|
159 |
+
|
160 |
+
def __mod__(self, other):
|
161 |
+
qr = self.__divmod__(other)
|
162 |
+
return qr if qr is NotImplemented else qr[1]
|
163 |
+
|
164 |
+
def __rmod__(self, other):
|
165 |
+
qr = self.__rdivmod__(other)
|
166 |
+
return qr if qr is NotImplemented else qr[1]
|
167 |
+
|
168 |
+
|
169 |
+
class GaussianInteger(GaussianElement):
|
170 |
+
"""Gaussian integer: domain element for :ref:`ZZ_I`
|
171 |
+
|
172 |
+
>>> from sympy import ZZ_I
|
173 |
+
>>> z = ZZ_I(2, 3)
|
174 |
+
>>> z
|
175 |
+
(2 + 3*I)
|
176 |
+
>>> type(z)
|
177 |
+
<class 'sympy.polys.domains.gaussiandomains.GaussianInteger'>
|
178 |
+
"""
|
179 |
+
base = ZZ
|
180 |
+
|
181 |
+
def __truediv__(self, other):
|
182 |
+
"""Return a Gaussian rational."""
|
183 |
+
return QQ_I.convert(self)/other
|
184 |
+
|
185 |
+
def __divmod__(self, other):
|
186 |
+
if not other:
|
187 |
+
raise ZeroDivisionError('divmod({}, 0)'.format(self))
|
188 |
+
x, y = self._get_xy(other)
|
189 |
+
if x is None:
|
190 |
+
return NotImplemented
|
191 |
+
|
192 |
+
# multiply self and other by x - I*y
|
193 |
+
# self/other == (a + I*b)/c
|
194 |
+
a, b = self.x*x + self.y*y, -self.x*y + self.y*x
|
195 |
+
c = x*x + y*y
|
196 |
+
|
197 |
+
# find integers qx and qy such that
|
198 |
+
# |a - qx*c| <= c/2 and |b - qy*c| <= c/2
|
199 |
+
qx = (2*a + c) // (2*c) # -c <= 2*a - qx*2*c < c
|
200 |
+
qy = (2*b + c) // (2*c)
|
201 |
+
|
202 |
+
q = GaussianInteger(qx, qy)
|
203 |
+
# |self/other - q| < 1 since
|
204 |
+
# |a/c - qx|**2 + |b/c - qy|**2 <= 1/4 + 1/4 < 1
|
205 |
+
|
206 |
+
return q, self - q*other # |r| < |other|
|
207 |
+
|
208 |
+
|
209 |
+
class GaussianRational(GaussianElement):
|
210 |
+
"""Gaussian rational: domain element for :ref:`QQ_I`
|
211 |
+
|
212 |
+
>>> from sympy import QQ_I, QQ
|
213 |
+
>>> z = QQ_I(QQ(2, 3), QQ(4, 5))
|
214 |
+
>>> z
|
215 |
+
(2/3 + 4/5*I)
|
216 |
+
>>> type(z)
|
217 |
+
<class 'sympy.polys.domains.gaussiandomains.GaussianRational'>
|
218 |
+
"""
|
219 |
+
base = QQ
|
220 |
+
|
221 |
+
def __truediv__(self, other):
|
222 |
+
"""Return a Gaussian rational."""
|
223 |
+
if not other:
|
224 |
+
raise ZeroDivisionError('{} / 0'.format(self))
|
225 |
+
x, y = self._get_xy(other)
|
226 |
+
if x is None:
|
227 |
+
return NotImplemented
|
228 |
+
c = x*x + y*y
|
229 |
+
|
230 |
+
return GaussianRational((self.x*x + self.y*y)/c,
|
231 |
+
(-self.x*y + self.y*x)/c)
|
232 |
+
|
233 |
+
def __divmod__(self, other):
|
234 |
+
try:
|
235 |
+
other = self._parent.convert(other)
|
236 |
+
except CoercionFailed:
|
237 |
+
return NotImplemented
|
238 |
+
if not other:
|
239 |
+
raise ZeroDivisionError('{} % 0'.format(self))
|
240 |
+
else:
|
241 |
+
return self/other, QQ_I.zero
|
242 |
+
|
243 |
+
|
244 |
+
class GaussianDomain():
|
245 |
+
"""Base class for Gaussian domains."""
|
246 |
+
dom = None # type: Domain
|
247 |
+
|
248 |
+
is_Numerical = True
|
249 |
+
is_Exact = True
|
250 |
+
|
251 |
+
has_assoc_Ring = True
|
252 |
+
has_assoc_Field = True
|
253 |
+
|
254 |
+
def to_sympy(self, a):
|
255 |
+
"""Convert ``a`` to a SymPy object. """
|
256 |
+
conv = self.dom.to_sympy
|
257 |
+
return conv(a.x) + I*conv(a.y)
|
258 |
+
|
259 |
+
def from_sympy(self, a):
|
260 |
+
"""Convert a SymPy object to ``self.dtype``."""
|
261 |
+
r, b = a.as_coeff_Add()
|
262 |
+
x = self.dom.from_sympy(r) # may raise CoercionFailed
|
263 |
+
if not b:
|
264 |
+
return self.new(x, 0)
|
265 |
+
r, b = b.as_coeff_Mul()
|
266 |
+
y = self.dom.from_sympy(r)
|
267 |
+
if b is I:
|
268 |
+
return self.new(x, y)
|
269 |
+
else:
|
270 |
+
raise CoercionFailed("{} is not Gaussian".format(a))
|
271 |
+
|
272 |
+
def inject(self, *gens):
|
273 |
+
"""Inject generators into this domain. """
|
274 |
+
return self.poly_ring(*gens)
|
275 |
+
|
276 |
+
def canonical_unit(self, d):
|
277 |
+
unit = self.units[-d.quadrant()] # - for inverse power
|
278 |
+
return unit
|
279 |
+
|
280 |
+
def is_negative(self, element):
|
281 |
+
"""Returns ``False`` for any ``GaussianElement``. """
|
282 |
+
return False
|
283 |
+
|
284 |
+
def is_positive(self, element):
|
285 |
+
"""Returns ``False`` for any ``GaussianElement``. """
|
286 |
+
return False
|
287 |
+
|
288 |
+
def is_nonnegative(self, element):
|
289 |
+
"""Returns ``False`` for any ``GaussianElement``. """
|
290 |
+
return False
|
291 |
+
|
292 |
+
def is_nonpositive(self, element):
|
293 |
+
"""Returns ``False`` for any ``GaussianElement``. """
|
294 |
+
return False
|
295 |
+
|
296 |
+
def from_ZZ_gmpy(K1, a, K0):
|
297 |
+
"""Convert a GMPY mpz to ``self.dtype``."""
|
298 |
+
return K1(a)
|
299 |
+
|
300 |
+
def from_ZZ(K1, a, K0):
|
301 |
+
"""Convert a ZZ_python element to ``self.dtype``."""
|
302 |
+
return K1(a)
|
303 |
+
|
304 |
+
def from_ZZ_python(K1, a, K0):
|
305 |
+
"""Convert a ZZ_python element to ``self.dtype``."""
|
306 |
+
return K1(a)
|
307 |
+
|
308 |
+
def from_QQ(K1, a, K0):
|
309 |
+
"""Convert a GMPY mpq to ``self.dtype``."""
|
310 |
+
return K1(a)
|
311 |
+
|
312 |
+
def from_QQ_gmpy(K1, a, K0):
|
313 |
+
"""Convert a GMPY mpq to ``self.dtype``."""
|
314 |
+
return K1(a)
|
315 |
+
|
316 |
+
def from_QQ_python(K1, a, K0):
|
317 |
+
"""Convert a QQ_python element to ``self.dtype``."""
|
318 |
+
return K1(a)
|
319 |
+
|
320 |
+
def from_AlgebraicField(K1, a, K0):
|
321 |
+
"""Convert an element from ZZ<I> or QQ<I> to ``self.dtype``."""
|
322 |
+
if K0.ext.args[0] == I:
|
323 |
+
return K1.from_sympy(K0.to_sympy(a))
|
324 |
+
|
325 |
+
|
326 |
+
class GaussianIntegerRing(GaussianDomain, Ring):
|
327 |
+
r"""Ring of Gaussian integers ``ZZ_I``
|
328 |
+
|
329 |
+
The :ref:`ZZ_I` domain represents the `Gaussian integers`_ `\mathbb{Z}[i]`
|
330 |
+
as a :py:class:`~.Domain` in the domain system (see
|
331 |
+
:ref:`polys-domainsintro`).
|
332 |
+
|
333 |
+
By default a :py:class:`~.Poly` created from an expression with
|
334 |
+
coefficients that are combinations of integers and ``I`` (`\sqrt{-1}`)
|
335 |
+
will have the domain :ref:`ZZ_I`.
|
336 |
+
|
337 |
+
>>> from sympy import Poly, Symbol, I
|
338 |
+
>>> x = Symbol('x')
|
339 |
+
>>> p = Poly(x**2 + I)
|
340 |
+
>>> p
|
341 |
+
Poly(x**2 + I, x, domain='ZZ_I')
|
342 |
+
>>> p.domain
|
343 |
+
ZZ_I
|
344 |
+
|
345 |
+
The :ref:`ZZ_I` domain can be used to factorise polynomials that are
|
346 |
+
reducible over the Gaussian integers.
|
347 |
+
|
348 |
+
>>> from sympy import factor
|
349 |
+
>>> factor(x**2 + 1)
|
350 |
+
x**2 + 1
|
351 |
+
>>> factor(x**2 + 1, domain='ZZ_I')
|
352 |
+
(x - I)*(x + I)
|
353 |
+
|
354 |
+
The corresponding `field of fractions`_ is the domain of the Gaussian
|
355 |
+
rationals :ref:`QQ_I`. Conversely :ref:`ZZ_I` is the `ring of integers`_
|
356 |
+
of :ref:`QQ_I`.
|
357 |
+
|
358 |
+
>>> from sympy import ZZ_I, QQ_I
|
359 |
+
>>> ZZ_I.get_field()
|
360 |
+
QQ_I
|
361 |
+
>>> QQ_I.get_ring()
|
362 |
+
ZZ_I
|
363 |
+
|
364 |
+
When using the domain directly :ref:`ZZ_I` can be used as a constructor.
|
365 |
+
|
366 |
+
>>> ZZ_I(3, 4)
|
367 |
+
(3 + 4*I)
|
368 |
+
>>> ZZ_I(5)
|
369 |
+
(5 + 0*I)
|
370 |
+
|
371 |
+
The domain elements of :ref:`ZZ_I` are instances of
|
372 |
+
:py:class:`~.GaussianInteger` which support the rings operations
|
373 |
+
``+,-,*,**``.
|
374 |
+
|
375 |
+
>>> z1 = ZZ_I(5, 1)
|
376 |
+
>>> z2 = ZZ_I(2, 3)
|
377 |
+
>>> z1
|
378 |
+
(5 + 1*I)
|
379 |
+
>>> z2
|
380 |
+
(2 + 3*I)
|
381 |
+
>>> z1 + z2
|
382 |
+
(7 + 4*I)
|
383 |
+
>>> z1 * z2
|
384 |
+
(7 + 17*I)
|
385 |
+
>>> z1 ** 2
|
386 |
+
(24 + 10*I)
|
387 |
+
|
388 |
+
Both floor (``//``) and modulo (``%``) division work with
|
389 |
+
:py:class:`~.GaussianInteger` (see the :py:meth:`~.Domain.div` method).
|
390 |
+
|
391 |
+
>>> z3, z4 = ZZ_I(5), ZZ_I(1, 3)
|
392 |
+
>>> z3 // z4 # floor division
|
393 |
+
(1 + -1*I)
|
394 |
+
>>> z3 % z4 # modulo division (remainder)
|
395 |
+
(1 + -2*I)
|
396 |
+
>>> (z3//z4)*z4 + z3%z4 == z3
|
397 |
+
True
|
398 |
+
|
399 |
+
True division (``/``) in :ref:`ZZ_I` gives an element of :ref:`QQ_I`. The
|
400 |
+
:py:meth:`~.Domain.exquo` method can be used to divide in :ref:`ZZ_I` when
|
401 |
+
exact division is possible.
|
402 |
+
|
403 |
+
>>> z1 / z2
|
404 |
+
(1 + -1*I)
|
405 |
+
>>> ZZ_I.exquo(z1, z2)
|
406 |
+
(1 + -1*I)
|
407 |
+
>>> z3 / z4
|
408 |
+
(1/2 + -3/2*I)
|
409 |
+
>>> ZZ_I.exquo(z3, z4)
|
410 |
+
Traceback (most recent call last):
|
411 |
+
...
|
412 |
+
ExactQuotientFailed: (1 + 3*I) does not divide (5 + 0*I) in ZZ_I
|
413 |
+
|
414 |
+
The :py:meth:`~.Domain.gcd` method can be used to compute the `gcd`_ of any
|
415 |
+
two elements.
|
416 |
+
|
417 |
+
>>> ZZ_I.gcd(ZZ_I(10), ZZ_I(2))
|
418 |
+
(2 + 0*I)
|
419 |
+
>>> ZZ_I.gcd(ZZ_I(5), ZZ_I(2, 1))
|
420 |
+
(2 + 1*I)
|
421 |
+
|
422 |
+
.. _Gaussian integers: https://en.wikipedia.org/wiki/Gaussian_integer
|
423 |
+
.. _gcd: https://en.wikipedia.org/wiki/Greatest_common_divisor
|
424 |
+
|
425 |
+
"""
|
426 |
+
dom = ZZ
|
427 |
+
dtype = GaussianInteger
|
428 |
+
zero = dtype(ZZ(0), ZZ(0))
|
429 |
+
one = dtype(ZZ(1), ZZ(0))
|
430 |
+
imag_unit = dtype(ZZ(0), ZZ(1))
|
431 |
+
units = (one, imag_unit, -one, -imag_unit) # powers of i
|
432 |
+
|
433 |
+
rep = 'ZZ_I'
|
434 |
+
|
435 |
+
is_GaussianRing = True
|
436 |
+
is_ZZ_I = True
|
437 |
+
|
438 |
+
def __init__(self): # override Domain.__init__
|
439 |
+
"""For constructing ZZ_I."""
|
440 |
+
|
441 |
+
def get_ring(self):
|
442 |
+
"""Returns a ring associated with ``self``. """
|
443 |
+
return self
|
444 |
+
|
445 |
+
def get_field(self):
|
446 |
+
"""Returns a field associated with ``self``. """
|
447 |
+
return QQ_I
|
448 |
+
|
449 |
+
def normalize(self, d, *args):
|
450 |
+
"""Return first quadrant element associated with ``d``.
|
451 |
+
|
452 |
+
Also multiply the other arguments by the same power of i.
|
453 |
+
"""
|
454 |
+
unit = self.canonical_unit(d)
|
455 |
+
d *= unit
|
456 |
+
args = tuple(a*unit for a in args)
|
457 |
+
return (d,) + args if args else d
|
458 |
+
|
459 |
+
def gcd(self, a, b):
|
460 |
+
"""Greatest common divisor of a and b over ZZ_I."""
|
461 |
+
while b:
|
462 |
+
a, b = b, a % b
|
463 |
+
return self.normalize(a)
|
464 |
+
|
465 |
+
def lcm(self, a, b):
|
466 |
+
"""Least common multiple of a and b over ZZ_I."""
|
467 |
+
return (a * b) // self.gcd(a, b)
|
468 |
+
|
469 |
+
def from_GaussianIntegerRing(K1, a, K0):
|
470 |
+
"""Convert a ZZ_I element to ZZ_I."""
|
471 |
+
return a
|
472 |
+
|
473 |
+
def from_GaussianRationalField(K1, a, K0):
|
474 |
+
"""Convert a QQ_I element to ZZ_I."""
|
475 |
+
return K1.new(ZZ.convert(a.x), ZZ.convert(a.y))
|
476 |
+
|
477 |
+
ZZ_I = GaussianInteger._parent = GaussianIntegerRing()
|
478 |
+
|
479 |
+
|
480 |
+
class GaussianRationalField(GaussianDomain, Field):
|
481 |
+
r"""Field of Gaussian rationals ``QQ_I``
|
482 |
+
|
483 |
+
The :ref:`QQ_I` domain represents the `Gaussian rationals`_ `\mathbb{Q}(i)`
|
484 |
+
as a :py:class:`~.Domain` in the domain system (see
|
485 |
+
:ref:`polys-domainsintro`).
|
486 |
+
|
487 |
+
By default a :py:class:`~.Poly` created from an expression with
|
488 |
+
coefficients that are combinations of rationals and ``I`` (`\sqrt{-1}`)
|
489 |
+
will have the domain :ref:`QQ_I`.
|
490 |
+
|
491 |
+
>>> from sympy import Poly, Symbol, I
|
492 |
+
>>> x = Symbol('x')
|
493 |
+
>>> p = Poly(x**2 + I/2)
|
494 |
+
>>> p
|
495 |
+
Poly(x**2 + I/2, x, domain='QQ_I')
|
496 |
+
>>> p.domain
|
497 |
+
QQ_I
|
498 |
+
|
499 |
+
The polys option ``gaussian=True`` can be used to specify that the domain
|
500 |
+
should be :ref:`QQ_I` even if the coefficients do not contain ``I`` or are
|
501 |
+
all integers.
|
502 |
+
|
503 |
+
>>> Poly(x**2)
|
504 |
+
Poly(x**2, x, domain='ZZ')
|
505 |
+
>>> Poly(x**2 + I)
|
506 |
+
Poly(x**2 + I, x, domain='ZZ_I')
|
507 |
+
>>> Poly(x**2/2)
|
508 |
+
Poly(1/2*x**2, x, domain='QQ')
|
509 |
+
>>> Poly(x**2, gaussian=True)
|
510 |
+
Poly(x**2, x, domain='QQ_I')
|
511 |
+
>>> Poly(x**2 + I, gaussian=True)
|
512 |
+
Poly(x**2 + I, x, domain='QQ_I')
|
513 |
+
>>> Poly(x**2/2, gaussian=True)
|
514 |
+
Poly(1/2*x**2, x, domain='QQ_I')
|
515 |
+
|
516 |
+
The :ref:`QQ_I` domain can be used to factorise polynomials that are
|
517 |
+
reducible over the Gaussian rationals.
|
518 |
+
|
519 |
+
>>> from sympy import factor, QQ_I
|
520 |
+
>>> factor(x**2/4 + 1)
|
521 |
+
(x**2 + 4)/4
|
522 |
+
>>> factor(x**2/4 + 1, domain='QQ_I')
|
523 |
+
(x - 2*I)*(x + 2*I)/4
|
524 |
+
>>> factor(x**2/4 + 1, domain=QQ_I)
|
525 |
+
(x - 2*I)*(x + 2*I)/4
|
526 |
+
|
527 |
+
It is also possible to specify the :ref:`QQ_I` domain explicitly with
|
528 |
+
polys functions like :py:func:`~.apart`.
|
529 |
+
|
530 |
+
>>> from sympy import apart
|
531 |
+
>>> apart(1/(1 + x**2))
|
532 |
+
1/(x**2 + 1)
|
533 |
+
>>> apart(1/(1 + x**2), domain=QQ_I)
|
534 |
+
I/(2*(x + I)) - I/(2*(x - I))
|
535 |
+
|
536 |
+
The corresponding `ring of integers`_ is the domain of the Gaussian
|
537 |
+
integers :ref:`ZZ_I`. Conversely :ref:`QQ_I` is the `field of fractions`_
|
538 |
+
of :ref:`ZZ_I`.
|
539 |
+
|
540 |
+
>>> from sympy import ZZ_I, QQ_I, QQ
|
541 |
+
>>> ZZ_I.get_field()
|
542 |
+
QQ_I
|
543 |
+
>>> QQ_I.get_ring()
|
544 |
+
ZZ_I
|
545 |
+
|
546 |
+
When using the domain directly :ref:`QQ_I` can be used as a constructor.
|
547 |
+
|
548 |
+
>>> QQ_I(3, 4)
|
549 |
+
(3 + 4*I)
|
550 |
+
>>> QQ_I(5)
|
551 |
+
(5 + 0*I)
|
552 |
+
>>> QQ_I(QQ(2, 3), QQ(4, 5))
|
553 |
+
(2/3 + 4/5*I)
|
554 |
+
|
555 |
+
The domain elements of :ref:`QQ_I` are instances of
|
556 |
+
:py:class:`~.GaussianRational` which support the field operations
|
557 |
+
``+,-,*,**,/``.
|
558 |
+
|
559 |
+
>>> z1 = QQ_I(5, 1)
|
560 |
+
>>> z2 = QQ_I(2, QQ(1, 2))
|
561 |
+
>>> z1
|
562 |
+
(5 + 1*I)
|
563 |
+
>>> z2
|
564 |
+
(2 + 1/2*I)
|
565 |
+
>>> z1 + z2
|
566 |
+
(7 + 3/2*I)
|
567 |
+
>>> z1 * z2
|
568 |
+
(19/2 + 9/2*I)
|
569 |
+
>>> z2 ** 2
|
570 |
+
(15/4 + 2*I)
|
571 |
+
|
572 |
+
True division (``/``) in :ref:`QQ_I` gives an element of :ref:`QQ_I` and
|
573 |
+
is always exact.
|
574 |
+
|
575 |
+
>>> z1 / z2
|
576 |
+
(42/17 + -2/17*I)
|
577 |
+
>>> QQ_I.exquo(z1, z2)
|
578 |
+
(42/17 + -2/17*I)
|
579 |
+
>>> z1 == (z1/z2)*z2
|
580 |
+
True
|
581 |
+
|
582 |
+
Both floor (``//``) and modulo (``%``) division can be used with
|
583 |
+
:py:class:`~.GaussianRational` (see :py:meth:`~.Domain.div`)
|
584 |
+
but division is always exact so there is no remainder.
|
585 |
+
|
586 |
+
>>> z1 // z2
|
587 |
+
(42/17 + -2/17*I)
|
588 |
+
>>> z1 % z2
|
589 |
+
(0 + 0*I)
|
590 |
+
>>> QQ_I.div(z1, z2)
|
591 |
+
((42/17 + -2/17*I), (0 + 0*I))
|
592 |
+
>>> (z1//z2)*z2 + z1%z2 == z1
|
593 |
+
True
|
594 |
+
|
595 |
+
.. _Gaussian rationals: https://en.wikipedia.org/wiki/Gaussian_rational
|
596 |
+
"""
|
597 |
+
dom = QQ
|
598 |
+
dtype = GaussianRational
|
599 |
+
zero = dtype(QQ(0), QQ(0))
|
600 |
+
one = dtype(QQ(1), QQ(0))
|
601 |
+
imag_unit = dtype(QQ(0), QQ(1))
|
602 |
+
units = (one, imag_unit, -one, -imag_unit) # powers of i
|
603 |
+
|
604 |
+
rep = 'QQ_I'
|
605 |
+
|
606 |
+
is_GaussianField = True
|
607 |
+
is_QQ_I = True
|
608 |
+
|
609 |
+
def __init__(self): # override Domain.__init__
|
610 |
+
"""For constructing QQ_I."""
|
611 |
+
|
612 |
+
def get_ring(self):
|
613 |
+
"""Returns a ring associated with ``self``. """
|
614 |
+
return ZZ_I
|
615 |
+
|
616 |
+
def get_field(self):
|
617 |
+
"""Returns a field associated with ``self``. """
|
618 |
+
return self
|
619 |
+
|
620 |
+
def as_AlgebraicField(self):
|
621 |
+
"""Get equivalent domain as an ``AlgebraicField``. """
|
622 |
+
return AlgebraicField(self.dom, I)
|
623 |
+
|
624 |
+
def numer(self, a):
|
625 |
+
"""Get the numerator of ``a``."""
|
626 |
+
ZZ_I = self.get_ring()
|
627 |
+
return ZZ_I.convert(a * self.denom(a))
|
628 |
+
|
629 |
+
def denom(self, a):
|
630 |
+
"""Get the denominator of ``a``."""
|
631 |
+
ZZ = self.dom.get_ring()
|
632 |
+
QQ = self.dom
|
633 |
+
ZZ_I = self.get_ring()
|
634 |
+
denom_ZZ = ZZ.lcm(QQ.denom(a.x), QQ.denom(a.y))
|
635 |
+
return ZZ_I(denom_ZZ, ZZ.zero)
|
636 |
+
|
637 |
+
def from_GaussianIntegerRing(K1, a, K0):
|
638 |
+
"""Convert a ZZ_I element to QQ_I."""
|
639 |
+
return K1.new(a.x, a.y)
|
640 |
+
|
641 |
+
def from_GaussianRationalField(K1, a, K0):
|
642 |
+
"""Convert a QQ_I element to QQ_I."""
|
643 |
+
return a
|
644 |
+
|
645 |
+
QQ_I = GaussianRational._parent = GaussianRationalField()
|
venv/lib/python3.10/site-packages/sympy/polys/domains/gmpyfinitefield.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`GMPYFiniteField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.finitefield import FiniteField
|
5 |
+
from sympy.polys.domains.gmpyintegerring import GMPYIntegerRing
|
6 |
+
|
7 |
+
from sympy.utilities import public
|
8 |
+
|
9 |
+
@public
|
10 |
+
class GMPYFiniteField(FiniteField):
|
11 |
+
"""Finite field based on GMPY integers. """
|
12 |
+
|
13 |
+
alias = 'FF_gmpy'
|
14 |
+
|
15 |
+
def __init__(self, mod, symmetric=True):
|
16 |
+
return super().__init__(mod, GMPYIntegerRing(), symmetric)
|
venv/lib/python3.10/site-packages/sympy/polys/domains/gmpyintegerring.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`GMPYIntegerRing` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.groundtypes import (
|
5 |
+
GMPYInteger, SymPyInteger,
|
6 |
+
factorial as gmpy_factorial,
|
7 |
+
gmpy_gcdex, gmpy_gcd, gmpy_lcm, sqrt as gmpy_sqrt,
|
8 |
+
)
|
9 |
+
from sympy.polys.domains.integerring import IntegerRing
|
10 |
+
from sympy.polys.polyerrors import CoercionFailed
|
11 |
+
from sympy.utilities import public
|
12 |
+
|
13 |
+
@public
|
14 |
+
class GMPYIntegerRing(IntegerRing):
|
15 |
+
"""Integer ring based on GMPY's ``mpz`` type.
|
16 |
+
|
17 |
+
This will be the implementation of :ref:`ZZ` if ``gmpy`` or ``gmpy2`` is
|
18 |
+
installed. Elements will be of type ``gmpy.mpz``.
|
19 |
+
"""
|
20 |
+
|
21 |
+
dtype = GMPYInteger
|
22 |
+
zero = dtype(0)
|
23 |
+
one = dtype(1)
|
24 |
+
tp = type(one)
|
25 |
+
alias = 'ZZ_gmpy'
|
26 |
+
|
27 |
+
def __init__(self):
|
28 |
+
"""Allow instantiation of this domain. """
|
29 |
+
|
30 |
+
def to_sympy(self, a):
|
31 |
+
"""Convert ``a`` to a SymPy object. """
|
32 |
+
return SymPyInteger(int(a))
|
33 |
+
|
34 |
+
def from_sympy(self, a):
|
35 |
+
"""Convert SymPy's Integer to ``dtype``. """
|
36 |
+
if a.is_Integer:
|
37 |
+
return GMPYInteger(a.p)
|
38 |
+
elif a.is_Float and int(a) == a:
|
39 |
+
return GMPYInteger(int(a))
|
40 |
+
else:
|
41 |
+
raise CoercionFailed("expected an integer, got %s" % a)
|
42 |
+
|
43 |
+
def from_FF_python(K1, a, K0):
|
44 |
+
"""Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
|
45 |
+
return GMPYInteger(a.to_int())
|
46 |
+
|
47 |
+
def from_ZZ_python(K1, a, K0):
|
48 |
+
"""Convert Python's ``int`` to GMPY's ``mpz``. """
|
49 |
+
return GMPYInteger(a)
|
50 |
+
|
51 |
+
def from_QQ(K1, a, K0):
|
52 |
+
"""Convert Python's ``Fraction`` to GMPY's ``mpz``. """
|
53 |
+
if a.denominator == 1:
|
54 |
+
return GMPYInteger(a.numerator)
|
55 |
+
|
56 |
+
def from_QQ_python(K1, a, K0):
|
57 |
+
"""Convert Python's ``Fraction`` to GMPY's ``mpz``. """
|
58 |
+
if a.denominator == 1:
|
59 |
+
return GMPYInteger(a.numerator)
|
60 |
+
|
61 |
+
def from_FF_gmpy(K1, a, K0):
|
62 |
+
"""Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. """
|
63 |
+
return a.to_int()
|
64 |
+
|
65 |
+
def from_ZZ_gmpy(K1, a, K0):
|
66 |
+
"""Convert GMPY's ``mpz`` to GMPY's ``mpz``. """
|
67 |
+
return a
|
68 |
+
|
69 |
+
def from_QQ_gmpy(K1, a, K0):
|
70 |
+
"""Convert GMPY ``mpq`` to GMPY's ``mpz``. """
|
71 |
+
if a.denominator == 1:
|
72 |
+
return a.numerator
|
73 |
+
|
74 |
+
def from_RealField(K1, a, K0):
|
75 |
+
"""Convert mpmath's ``mpf`` to GMPY's ``mpz``. """
|
76 |
+
p, q = K0.to_rational(a)
|
77 |
+
|
78 |
+
if q == 1:
|
79 |
+
return GMPYInteger(p)
|
80 |
+
|
81 |
+
def from_GaussianIntegerRing(K1, a, K0):
|
82 |
+
if a.y == 0:
|
83 |
+
return a.x
|
84 |
+
|
85 |
+
def gcdex(self, a, b):
|
86 |
+
"""Compute extended GCD of ``a`` and ``b``. """
|
87 |
+
h, s, t = gmpy_gcdex(a, b)
|
88 |
+
return s, t, h
|
89 |
+
|
90 |
+
def gcd(self, a, b):
|
91 |
+
"""Compute GCD of ``a`` and ``b``. """
|
92 |
+
return gmpy_gcd(a, b)
|
93 |
+
|
94 |
+
def lcm(self, a, b):
|
95 |
+
"""Compute LCM of ``a`` and ``b``. """
|
96 |
+
return gmpy_lcm(a, b)
|
97 |
+
|
98 |
+
def sqrt(self, a):
|
99 |
+
"""Compute square root of ``a``. """
|
100 |
+
return gmpy_sqrt(a)
|
101 |
+
|
102 |
+
def factorial(self, a):
|
103 |
+
"""Compute factorial of ``a``. """
|
104 |
+
return gmpy_factorial(a)
|
venv/lib/python3.10/site-packages/sympy/polys/domains/gmpyrationalfield.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`GMPYRationalField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.groundtypes import (
|
5 |
+
GMPYRational, SymPyRational,
|
6 |
+
gmpy_numer, gmpy_denom, factorial as gmpy_factorial,
|
7 |
+
)
|
8 |
+
from sympy.polys.domains.rationalfield import RationalField
|
9 |
+
from sympy.polys.polyerrors import CoercionFailed
|
10 |
+
from sympy.utilities import public
|
11 |
+
|
12 |
+
@public
|
13 |
+
class GMPYRationalField(RationalField):
|
14 |
+
"""Rational field based on GMPY's ``mpq`` type.
|
15 |
+
|
16 |
+
This will be the implementation of :ref:`QQ` if ``gmpy`` or ``gmpy2`` is
|
17 |
+
installed. Elements will be of type ``gmpy.mpq``.
|
18 |
+
"""
|
19 |
+
|
20 |
+
dtype = GMPYRational
|
21 |
+
zero = dtype(0)
|
22 |
+
one = dtype(1)
|
23 |
+
tp = type(one)
|
24 |
+
alias = 'QQ_gmpy'
|
25 |
+
|
26 |
+
def __init__(self):
|
27 |
+
pass
|
28 |
+
|
29 |
+
def get_ring(self):
|
30 |
+
"""Returns ring associated with ``self``. """
|
31 |
+
from sympy.polys.domains import GMPYIntegerRing
|
32 |
+
return GMPYIntegerRing()
|
33 |
+
|
34 |
+
def to_sympy(self, a):
|
35 |
+
"""Convert ``a`` to a SymPy object. """
|
36 |
+
return SymPyRational(int(gmpy_numer(a)),
|
37 |
+
int(gmpy_denom(a)))
|
38 |
+
|
39 |
+
def from_sympy(self, a):
|
40 |
+
"""Convert SymPy's Integer to ``dtype``. """
|
41 |
+
if a.is_Rational:
|
42 |
+
return GMPYRational(a.p, a.q)
|
43 |
+
elif a.is_Float:
|
44 |
+
from sympy.polys.domains import RR
|
45 |
+
return GMPYRational(*map(int, RR.to_rational(a)))
|
46 |
+
else:
|
47 |
+
raise CoercionFailed("expected ``Rational`` object, got %s" % a)
|
48 |
+
|
49 |
+
def from_ZZ_python(K1, a, K0):
|
50 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
51 |
+
return GMPYRational(a)
|
52 |
+
|
53 |
+
def from_QQ_python(K1, a, K0):
|
54 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
55 |
+
return GMPYRational(a.numerator, a.denominator)
|
56 |
+
|
57 |
+
def from_ZZ_gmpy(K1, a, K0):
|
58 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
59 |
+
return GMPYRational(a)
|
60 |
+
|
61 |
+
def from_QQ_gmpy(K1, a, K0):
|
62 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
63 |
+
return a
|
64 |
+
|
65 |
+
def from_GaussianRationalField(K1, a, K0):
|
66 |
+
"""Convert a ``GaussianElement`` object to ``dtype``. """
|
67 |
+
if a.y == 0:
|
68 |
+
return GMPYRational(a.x)
|
69 |
+
|
70 |
+
def from_RealField(K1, a, K0):
|
71 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
72 |
+
return GMPYRational(*map(int, K0.to_rational(a)))
|
73 |
+
|
74 |
+
def exquo(self, a, b):
|
75 |
+
"""Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
76 |
+
return GMPYRational(a) / GMPYRational(b)
|
77 |
+
|
78 |
+
def quo(self, a, b):
|
79 |
+
"""Quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
80 |
+
return GMPYRational(a) / GMPYRational(b)
|
81 |
+
|
82 |
+
def rem(self, a, b):
|
83 |
+
"""Remainder of ``a`` and ``b``, implies nothing. """
|
84 |
+
return self.zero
|
85 |
+
|
86 |
+
def div(self, a, b):
|
87 |
+
"""Division of ``a`` and ``b``, implies ``__truediv__``. """
|
88 |
+
return GMPYRational(a) / GMPYRational(b), self.zero
|
89 |
+
|
90 |
+
def numer(self, a):
|
91 |
+
"""Returns numerator of ``a``. """
|
92 |
+
return a.numerator
|
93 |
+
|
94 |
+
def denom(self, a):
|
95 |
+
"""Returns denominator of ``a``. """
|
96 |
+
return a.denominator
|
97 |
+
|
98 |
+
def factorial(self, a):
|
99 |
+
"""Returns factorial of ``a``. """
|
100 |
+
return GMPYRational(gmpy_factorial(int(a)))
|
venv/lib/python3.10/site-packages/sympy/polys/domains/groundtypes.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Ground types for various mathematical domains in SymPy. """
|
2 |
+
|
3 |
+
import builtins
|
4 |
+
from sympy.external.gmpy import HAS_GMPY, factorial, sqrt
|
5 |
+
|
6 |
+
PythonInteger = builtins.int
|
7 |
+
PythonReal = builtins.float
|
8 |
+
PythonComplex = builtins.complex
|
9 |
+
|
10 |
+
from .pythonrational import PythonRational
|
11 |
+
|
12 |
+
from sympy.core.numbers import (
|
13 |
+
igcdex as python_gcdex,
|
14 |
+
igcd2 as python_gcd,
|
15 |
+
ilcm as python_lcm,
|
16 |
+
)
|
17 |
+
|
18 |
+
from sympy.core.numbers import (Float as SymPyReal, Integer as SymPyInteger, Rational as SymPyRational)
|
19 |
+
|
20 |
+
|
21 |
+
if HAS_GMPY == 2:
|
22 |
+
from gmpy2 import (
|
23 |
+
mpz as GMPYInteger,
|
24 |
+
mpq as GMPYRational,
|
25 |
+
numer as gmpy_numer,
|
26 |
+
denom as gmpy_denom,
|
27 |
+
gcdext as gmpy_gcdex,
|
28 |
+
gcd as gmpy_gcd,
|
29 |
+
lcm as gmpy_lcm,
|
30 |
+
qdiv as gmpy_qdiv,
|
31 |
+
)
|
32 |
+
gcdex = gmpy_gcdex
|
33 |
+
gcd = gmpy_gcd
|
34 |
+
lcm = gmpy_lcm
|
35 |
+
else:
|
36 |
+
class _GMPYInteger:
|
37 |
+
def __init__(self, obj):
|
38 |
+
pass
|
39 |
+
|
40 |
+
class _GMPYRational:
|
41 |
+
def __init__(self, obj):
|
42 |
+
pass
|
43 |
+
|
44 |
+
GMPYInteger = _GMPYInteger
|
45 |
+
GMPYRational = _GMPYRational
|
46 |
+
gmpy_numer = None
|
47 |
+
gmpy_denom = None
|
48 |
+
gmpy_gcdex = None
|
49 |
+
gmpy_gcd = None
|
50 |
+
gmpy_lcm = None
|
51 |
+
gmpy_qdiv = None
|
52 |
+
gcdex = python_gcdex
|
53 |
+
gcd = python_gcd
|
54 |
+
lcm = python_lcm
|
55 |
+
|
56 |
+
|
57 |
+
__all__ = [
|
58 |
+
'PythonInteger', 'PythonReal', 'PythonComplex',
|
59 |
+
|
60 |
+
'PythonRational',
|
61 |
+
|
62 |
+
'python_gcdex', 'python_gcd', 'python_lcm',
|
63 |
+
|
64 |
+
'SymPyReal', 'SymPyInteger', 'SymPyRational',
|
65 |
+
|
66 |
+
'GMPYInteger', 'GMPYRational', 'gmpy_numer',
|
67 |
+
'gmpy_denom', 'gmpy_gcdex', 'gmpy_gcd', 'gmpy_lcm',
|
68 |
+
'gmpy_qdiv',
|
69 |
+
|
70 |
+
'factorial', 'sqrt',
|
71 |
+
|
72 |
+
'GMPYInteger', 'GMPYRational',
|
73 |
+
]
|
venv/lib/python3.10/site-packages/sympy/polys/domains/integerring.py
ADDED
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`IntegerRing` class. """
|
2 |
+
|
3 |
+
from sympy.external.gmpy import MPZ, HAS_GMPY
|
4 |
+
|
5 |
+
from sympy.polys.domains.groundtypes import (
|
6 |
+
SymPyInteger,
|
7 |
+
factorial,
|
8 |
+
gcdex, gcd, lcm, sqrt,
|
9 |
+
)
|
10 |
+
|
11 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
12 |
+
from sympy.polys.domains.ring import Ring
|
13 |
+
from sympy.polys.domains.simpledomain import SimpleDomain
|
14 |
+
from sympy.polys.polyerrors import CoercionFailed
|
15 |
+
from sympy.utilities import public
|
16 |
+
|
17 |
+
import math
|
18 |
+
|
19 |
+
@public
|
20 |
+
class IntegerRing(Ring, CharacteristicZero, SimpleDomain):
|
21 |
+
r"""The domain ``ZZ`` representing the integers `\mathbb{Z}`.
|
22 |
+
|
23 |
+
The :py:class:`IntegerRing` class represents the ring of integers as a
|
24 |
+
:py:class:`~.Domain` in the domain system. :py:class:`IntegerRing` is a
|
25 |
+
super class of :py:class:`PythonIntegerRing` and
|
26 |
+
:py:class:`GMPYIntegerRing` one of which will be the implementation for
|
27 |
+
:ref:`ZZ` depending on whether or not ``gmpy`` or ``gmpy2`` is installed.
|
28 |
+
|
29 |
+
See also
|
30 |
+
========
|
31 |
+
|
32 |
+
Domain
|
33 |
+
"""
|
34 |
+
|
35 |
+
rep = 'ZZ'
|
36 |
+
alias = 'ZZ'
|
37 |
+
dtype = MPZ
|
38 |
+
zero = dtype(0)
|
39 |
+
one = dtype(1)
|
40 |
+
tp = type(one)
|
41 |
+
|
42 |
+
|
43 |
+
is_IntegerRing = is_ZZ = True
|
44 |
+
is_Numerical = True
|
45 |
+
is_PID = True
|
46 |
+
|
47 |
+
has_assoc_Ring = True
|
48 |
+
has_assoc_Field = True
|
49 |
+
|
50 |
+
def __init__(self):
|
51 |
+
"""Allow instantiation of this domain. """
|
52 |
+
|
53 |
+
def to_sympy(self, a):
|
54 |
+
"""Convert ``a`` to a SymPy object. """
|
55 |
+
return SymPyInteger(int(a))
|
56 |
+
|
57 |
+
def from_sympy(self, a):
|
58 |
+
"""Convert SymPy's Integer to ``dtype``. """
|
59 |
+
if a.is_Integer:
|
60 |
+
return MPZ(a.p)
|
61 |
+
elif a.is_Float and int(a) == a:
|
62 |
+
return MPZ(int(a))
|
63 |
+
else:
|
64 |
+
raise CoercionFailed("expected an integer, got %s" % a)
|
65 |
+
|
66 |
+
def get_field(self):
|
67 |
+
r"""Return the associated field of fractions :ref:`QQ`
|
68 |
+
|
69 |
+
Returns
|
70 |
+
=======
|
71 |
+
|
72 |
+
:ref:`QQ`:
|
73 |
+
The associated field of fractions :ref:`QQ`, a
|
74 |
+
:py:class:`~.Domain` representing the rational numbers
|
75 |
+
`\mathbb{Q}`.
|
76 |
+
|
77 |
+
Examples
|
78 |
+
========
|
79 |
+
|
80 |
+
>>> from sympy import ZZ
|
81 |
+
>>> ZZ.get_field()
|
82 |
+
QQ
|
83 |
+
"""
|
84 |
+
from sympy.polys.domains import QQ
|
85 |
+
return QQ
|
86 |
+
|
87 |
+
def algebraic_field(self, *extension, alias=None):
|
88 |
+
r"""Returns an algebraic field, i.e. `\mathbb{Q}(\alpha, \ldots)`.
|
89 |
+
|
90 |
+
Parameters
|
91 |
+
==========
|
92 |
+
|
93 |
+
*extension : One or more :py:class:`~.Expr`.
|
94 |
+
Generators of the extension. These should be expressions that are
|
95 |
+
algebraic over `\mathbb{Q}`.
|
96 |
+
|
97 |
+
alias : str, :py:class:`~.Symbol`, None, optional (default=None)
|
98 |
+
If provided, this will be used as the alias symbol for the
|
99 |
+
primitive element of the returned :py:class:`~.AlgebraicField`.
|
100 |
+
|
101 |
+
Returns
|
102 |
+
=======
|
103 |
+
|
104 |
+
:py:class:`~.AlgebraicField`
|
105 |
+
A :py:class:`~.Domain` representing the algebraic field extension.
|
106 |
+
|
107 |
+
Examples
|
108 |
+
========
|
109 |
+
|
110 |
+
>>> from sympy import ZZ, sqrt
|
111 |
+
>>> ZZ.algebraic_field(sqrt(2))
|
112 |
+
QQ<sqrt(2)>
|
113 |
+
"""
|
114 |
+
return self.get_field().algebraic_field(*extension, alias=alias)
|
115 |
+
|
116 |
+
def from_AlgebraicField(K1, a, K0):
|
117 |
+
"""Convert a :py:class:`~.ANP` object to :ref:`ZZ`.
|
118 |
+
|
119 |
+
See :py:meth:`~.Domain.convert`.
|
120 |
+
"""
|
121 |
+
if a.is_ground:
|
122 |
+
return K1.convert(a.LC(), K0.dom)
|
123 |
+
|
124 |
+
def log(self, a, b):
|
125 |
+
r"""Logarithm of *a* to the base *b*.
|
126 |
+
|
127 |
+
Parameters
|
128 |
+
==========
|
129 |
+
|
130 |
+
a: number
|
131 |
+
b: number
|
132 |
+
|
133 |
+
Returns
|
134 |
+
=======
|
135 |
+
|
136 |
+
$\\lfloor\log(a, b)\\rfloor$:
|
137 |
+
Floor of the logarithm of *a* to the base *b*
|
138 |
+
|
139 |
+
Examples
|
140 |
+
========
|
141 |
+
|
142 |
+
>>> from sympy import ZZ
|
143 |
+
>>> ZZ.log(ZZ(8), ZZ(2))
|
144 |
+
3
|
145 |
+
>>> ZZ.log(ZZ(9), ZZ(2))
|
146 |
+
3
|
147 |
+
|
148 |
+
Notes
|
149 |
+
=====
|
150 |
+
|
151 |
+
This function uses ``math.log`` which is based on ``float`` so it will
|
152 |
+
fail for large integer arguments.
|
153 |
+
"""
|
154 |
+
return self.dtype(math.log(int(a), b))
|
155 |
+
|
156 |
+
def from_FF(K1, a, K0):
|
157 |
+
"""Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
|
158 |
+
return MPZ(a.to_int())
|
159 |
+
|
160 |
+
def from_FF_python(K1, a, K0):
|
161 |
+
"""Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
|
162 |
+
return MPZ(a.to_int())
|
163 |
+
|
164 |
+
def from_ZZ(K1, a, K0):
|
165 |
+
"""Convert Python's ``int`` to GMPY's ``mpz``. """
|
166 |
+
return MPZ(a)
|
167 |
+
|
168 |
+
def from_ZZ_python(K1, a, K0):
|
169 |
+
"""Convert Python's ``int`` to GMPY's ``mpz``. """
|
170 |
+
return MPZ(a)
|
171 |
+
|
172 |
+
def from_QQ(K1, a, K0):
|
173 |
+
"""Convert Python's ``Fraction`` to GMPY's ``mpz``. """
|
174 |
+
if a.denominator == 1:
|
175 |
+
return MPZ(a.numerator)
|
176 |
+
|
177 |
+
def from_QQ_python(K1, a, K0):
|
178 |
+
"""Convert Python's ``Fraction`` to GMPY's ``mpz``. """
|
179 |
+
if a.denominator == 1:
|
180 |
+
return MPZ(a.numerator)
|
181 |
+
|
182 |
+
def from_FF_gmpy(K1, a, K0):
|
183 |
+
"""Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. """
|
184 |
+
return a.to_int()
|
185 |
+
|
186 |
+
def from_ZZ_gmpy(K1, a, K0):
|
187 |
+
"""Convert GMPY's ``mpz`` to GMPY's ``mpz``. """
|
188 |
+
return a
|
189 |
+
|
190 |
+
def from_QQ_gmpy(K1, a, K0):
|
191 |
+
"""Convert GMPY ``mpq`` to GMPY's ``mpz``. """
|
192 |
+
if a.denominator == 1:
|
193 |
+
return a.numerator
|
194 |
+
|
195 |
+
def from_RealField(K1, a, K0):
|
196 |
+
"""Convert mpmath's ``mpf`` to GMPY's ``mpz``. """
|
197 |
+
p, q = K0.to_rational(a)
|
198 |
+
|
199 |
+
if q == 1:
|
200 |
+
return MPZ(p)
|
201 |
+
|
202 |
+
def from_GaussianIntegerRing(K1, a, K0):
|
203 |
+
if a.y == 0:
|
204 |
+
return a.x
|
205 |
+
|
206 |
+
def gcdex(self, a, b):
|
207 |
+
"""Compute extended GCD of ``a`` and ``b``. """
|
208 |
+
h, s, t = gcdex(a, b)
|
209 |
+
if HAS_GMPY:
|
210 |
+
return s, t, h
|
211 |
+
else:
|
212 |
+
return h, s, t
|
213 |
+
|
214 |
+
def gcd(self, a, b):
|
215 |
+
"""Compute GCD of ``a`` and ``b``. """
|
216 |
+
return gcd(a, b)
|
217 |
+
|
218 |
+
def lcm(self, a, b):
|
219 |
+
"""Compute LCM of ``a`` and ``b``. """
|
220 |
+
return lcm(a, b)
|
221 |
+
|
222 |
+
def sqrt(self, a):
|
223 |
+
"""Compute square root of ``a``. """
|
224 |
+
return sqrt(a)
|
225 |
+
|
226 |
+
def factorial(self, a):
|
227 |
+
"""Compute factorial of ``a``. """
|
228 |
+
return factorial(a)
|
229 |
+
|
230 |
+
|
231 |
+
ZZ = IntegerRing()
|
venv/lib/python3.10/site-packages/sympy/polys/domains/modularinteger.py
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`ModularInteger` class. """
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
from typing import Any
|
5 |
+
|
6 |
+
import operator
|
7 |
+
|
8 |
+
from sympy.polys.polyutils import PicklableWithSlots
|
9 |
+
from sympy.polys.polyerrors import CoercionFailed
|
10 |
+
from sympy.polys.domains.domainelement import DomainElement
|
11 |
+
|
12 |
+
from sympy.utilities import public
|
13 |
+
|
14 |
+
@public
|
15 |
+
class ModularInteger(PicklableWithSlots, DomainElement):
|
16 |
+
"""A class representing a modular integer. """
|
17 |
+
|
18 |
+
mod, dom, sym, _parent = None, None, None, None
|
19 |
+
|
20 |
+
__slots__ = ('val',)
|
21 |
+
|
22 |
+
def parent(self):
|
23 |
+
return self._parent
|
24 |
+
|
25 |
+
def __init__(self, val):
|
26 |
+
if isinstance(val, self.__class__):
|
27 |
+
self.val = val.val % self.mod
|
28 |
+
else:
|
29 |
+
self.val = self.dom.convert(val) % self.mod
|
30 |
+
|
31 |
+
def __hash__(self):
|
32 |
+
return hash((self.val, self.mod))
|
33 |
+
|
34 |
+
def __repr__(self):
|
35 |
+
return "%s(%s)" % (self.__class__.__name__, self.val)
|
36 |
+
|
37 |
+
def __str__(self):
|
38 |
+
return "%s mod %s" % (self.val, self.mod)
|
39 |
+
|
40 |
+
def __int__(self):
|
41 |
+
return int(self.to_int())
|
42 |
+
|
43 |
+
def to_int(self):
|
44 |
+
if self.sym:
|
45 |
+
if self.val <= self.mod // 2:
|
46 |
+
return self.val
|
47 |
+
else:
|
48 |
+
return self.val - self.mod
|
49 |
+
else:
|
50 |
+
return self.val
|
51 |
+
|
52 |
+
def __pos__(self):
|
53 |
+
return self
|
54 |
+
|
55 |
+
def __neg__(self):
|
56 |
+
return self.__class__(-self.val)
|
57 |
+
|
58 |
+
@classmethod
|
59 |
+
def _get_val(cls, other):
|
60 |
+
if isinstance(other, cls):
|
61 |
+
return other.val
|
62 |
+
else:
|
63 |
+
try:
|
64 |
+
return cls.dom.convert(other)
|
65 |
+
except CoercionFailed:
|
66 |
+
return None
|
67 |
+
|
68 |
+
def __add__(self, other):
|
69 |
+
val = self._get_val(other)
|
70 |
+
|
71 |
+
if val is not None:
|
72 |
+
return self.__class__(self.val + val)
|
73 |
+
else:
|
74 |
+
return NotImplemented
|
75 |
+
|
76 |
+
def __radd__(self, other):
|
77 |
+
return self.__add__(other)
|
78 |
+
|
79 |
+
def __sub__(self, other):
|
80 |
+
val = self._get_val(other)
|
81 |
+
|
82 |
+
if val is not None:
|
83 |
+
return self.__class__(self.val - val)
|
84 |
+
else:
|
85 |
+
return NotImplemented
|
86 |
+
|
87 |
+
def __rsub__(self, other):
|
88 |
+
return (-self).__add__(other)
|
89 |
+
|
90 |
+
def __mul__(self, other):
|
91 |
+
val = self._get_val(other)
|
92 |
+
|
93 |
+
if val is not None:
|
94 |
+
return self.__class__(self.val * val)
|
95 |
+
else:
|
96 |
+
return NotImplemented
|
97 |
+
|
98 |
+
def __rmul__(self, other):
|
99 |
+
return self.__mul__(other)
|
100 |
+
|
101 |
+
def __truediv__(self, other):
|
102 |
+
val = self._get_val(other)
|
103 |
+
|
104 |
+
if val is not None:
|
105 |
+
return self.__class__(self.val * self._invert(val))
|
106 |
+
else:
|
107 |
+
return NotImplemented
|
108 |
+
|
109 |
+
def __rtruediv__(self, other):
|
110 |
+
return self.invert().__mul__(other)
|
111 |
+
|
112 |
+
def __mod__(self, other):
|
113 |
+
val = self._get_val(other)
|
114 |
+
|
115 |
+
if val is not None:
|
116 |
+
return self.__class__(self.val % val)
|
117 |
+
else:
|
118 |
+
return NotImplemented
|
119 |
+
|
120 |
+
def __rmod__(self, other):
|
121 |
+
val = self._get_val(other)
|
122 |
+
|
123 |
+
if val is not None:
|
124 |
+
return self.__class__(val % self.val)
|
125 |
+
else:
|
126 |
+
return NotImplemented
|
127 |
+
|
128 |
+
def __pow__(self, exp):
|
129 |
+
if not exp:
|
130 |
+
return self.__class__(self.dom.one)
|
131 |
+
|
132 |
+
if exp < 0:
|
133 |
+
val, exp = self.invert().val, -exp
|
134 |
+
else:
|
135 |
+
val = self.val
|
136 |
+
|
137 |
+
return self.__class__(pow(val, int(exp), self.mod))
|
138 |
+
|
139 |
+
def _compare(self, other, op):
|
140 |
+
val = self._get_val(other)
|
141 |
+
|
142 |
+
if val is not None:
|
143 |
+
return op(self.val, val % self.mod)
|
144 |
+
else:
|
145 |
+
return NotImplemented
|
146 |
+
|
147 |
+
def __eq__(self, other):
|
148 |
+
return self._compare(other, operator.eq)
|
149 |
+
|
150 |
+
def __ne__(self, other):
|
151 |
+
return self._compare(other, operator.ne)
|
152 |
+
|
153 |
+
def __lt__(self, other):
|
154 |
+
return self._compare(other, operator.lt)
|
155 |
+
|
156 |
+
def __le__(self, other):
|
157 |
+
return self._compare(other, operator.le)
|
158 |
+
|
159 |
+
def __gt__(self, other):
|
160 |
+
return self._compare(other, operator.gt)
|
161 |
+
|
162 |
+
def __ge__(self, other):
|
163 |
+
return self._compare(other, operator.ge)
|
164 |
+
|
165 |
+
def __bool__(self):
|
166 |
+
return bool(self.val)
|
167 |
+
|
168 |
+
@classmethod
|
169 |
+
def _invert(cls, value):
|
170 |
+
return cls.dom.invert(value, cls.mod)
|
171 |
+
|
172 |
+
def invert(self):
|
173 |
+
return self.__class__(self._invert(self.val))
|
174 |
+
|
175 |
+
_modular_integer_cache: dict[tuple[Any, Any, Any], type[ModularInteger]] = {}
|
176 |
+
|
177 |
+
def ModularIntegerFactory(_mod, _dom, _sym, parent):
|
178 |
+
"""Create custom class for specific integer modulus."""
|
179 |
+
try:
|
180 |
+
_mod = _dom.convert(_mod)
|
181 |
+
except CoercionFailed:
|
182 |
+
ok = False
|
183 |
+
else:
|
184 |
+
ok = True
|
185 |
+
|
186 |
+
if not ok or _mod < 1:
|
187 |
+
raise ValueError("modulus must be a positive integer, got %s" % _mod)
|
188 |
+
|
189 |
+
key = _mod, _dom, _sym
|
190 |
+
|
191 |
+
try:
|
192 |
+
cls = _modular_integer_cache[key]
|
193 |
+
except KeyError:
|
194 |
+
class cls(ModularInteger):
|
195 |
+
mod, dom, sym = _mod, _dom, _sym
|
196 |
+
_parent = parent
|
197 |
+
|
198 |
+
if _sym:
|
199 |
+
cls.__name__ = "SymmetricModularIntegerMod%s" % _mod
|
200 |
+
else:
|
201 |
+
cls.__name__ = "ModularIntegerMod%s" % _mod
|
202 |
+
|
203 |
+
_modular_integer_cache[key] = cls
|
204 |
+
|
205 |
+
return cls
|
venv/lib/python3.10/site-packages/sympy/polys/domains/mpelements.py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Real and complex elements. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.domainelement import DomainElement
|
5 |
+
from sympy.utilities import public
|
6 |
+
|
7 |
+
from mpmath.ctx_mp_python import PythonMPContext, _mpf, _mpc, _constant
|
8 |
+
from mpmath.libmp import (MPZ_ONE, fzero, fone, finf, fninf, fnan,
|
9 |
+
round_nearest, mpf_mul, repr_dps, int_types,
|
10 |
+
from_int, from_float, from_str, to_rational)
|
11 |
+
from mpmath.rational import mpq
|
12 |
+
|
13 |
+
|
14 |
+
@public
|
15 |
+
class RealElement(_mpf, DomainElement):
|
16 |
+
"""An element of a real domain. """
|
17 |
+
|
18 |
+
__slots__ = ('__mpf__',)
|
19 |
+
|
20 |
+
def _set_mpf(self, val):
|
21 |
+
self.__mpf__ = val
|
22 |
+
|
23 |
+
_mpf_ = property(lambda self: self.__mpf__, _set_mpf)
|
24 |
+
|
25 |
+
def parent(self):
|
26 |
+
return self.context._parent
|
27 |
+
|
28 |
+
@public
|
29 |
+
class ComplexElement(_mpc, DomainElement):
|
30 |
+
"""An element of a complex domain. """
|
31 |
+
|
32 |
+
__slots__ = ('__mpc__',)
|
33 |
+
|
34 |
+
def _set_mpc(self, val):
|
35 |
+
self.__mpc__ = val
|
36 |
+
|
37 |
+
_mpc_ = property(lambda self: self.__mpc__, _set_mpc)
|
38 |
+
|
39 |
+
def parent(self):
|
40 |
+
return self.context._parent
|
41 |
+
|
42 |
+
new = object.__new__
|
43 |
+
|
44 |
+
@public
|
45 |
+
class MPContext(PythonMPContext):
|
46 |
+
|
47 |
+
def __init__(ctx, prec=53, dps=None, tol=None, real=False):
|
48 |
+
ctx._prec_rounding = [prec, round_nearest]
|
49 |
+
|
50 |
+
if dps is None:
|
51 |
+
ctx._set_prec(prec)
|
52 |
+
else:
|
53 |
+
ctx._set_dps(dps)
|
54 |
+
|
55 |
+
ctx.mpf = RealElement
|
56 |
+
ctx.mpc = ComplexElement
|
57 |
+
ctx.mpf._ctxdata = [ctx.mpf, new, ctx._prec_rounding]
|
58 |
+
ctx.mpc._ctxdata = [ctx.mpc, new, ctx._prec_rounding]
|
59 |
+
|
60 |
+
if real:
|
61 |
+
ctx.mpf.context = ctx
|
62 |
+
else:
|
63 |
+
ctx.mpc.context = ctx
|
64 |
+
|
65 |
+
ctx.constant = _constant
|
66 |
+
ctx.constant._ctxdata = [ctx.mpf, new, ctx._prec_rounding]
|
67 |
+
ctx.constant.context = ctx
|
68 |
+
|
69 |
+
ctx.types = [ctx.mpf, ctx.mpc, ctx.constant]
|
70 |
+
ctx.trap_complex = True
|
71 |
+
ctx.pretty = True
|
72 |
+
|
73 |
+
if tol is None:
|
74 |
+
ctx.tol = ctx._make_tol()
|
75 |
+
elif tol is False:
|
76 |
+
ctx.tol = fzero
|
77 |
+
else:
|
78 |
+
ctx.tol = ctx._convert_tol(tol)
|
79 |
+
|
80 |
+
ctx.tolerance = ctx.make_mpf(ctx.tol)
|
81 |
+
|
82 |
+
if not ctx.tolerance:
|
83 |
+
ctx.max_denom = 1000000
|
84 |
+
else:
|
85 |
+
ctx.max_denom = int(1/ctx.tolerance)
|
86 |
+
|
87 |
+
ctx.zero = ctx.make_mpf(fzero)
|
88 |
+
ctx.one = ctx.make_mpf(fone)
|
89 |
+
ctx.j = ctx.make_mpc((fzero, fone))
|
90 |
+
ctx.inf = ctx.make_mpf(finf)
|
91 |
+
ctx.ninf = ctx.make_mpf(fninf)
|
92 |
+
ctx.nan = ctx.make_mpf(fnan)
|
93 |
+
|
94 |
+
def _make_tol(ctx):
|
95 |
+
hundred = (0, 25, 2, 5)
|
96 |
+
eps = (0, MPZ_ONE, 1-ctx.prec, 1)
|
97 |
+
return mpf_mul(hundred, eps)
|
98 |
+
|
99 |
+
def make_tol(ctx):
|
100 |
+
return ctx.make_mpf(ctx._make_tol())
|
101 |
+
|
102 |
+
def _convert_tol(ctx, tol):
|
103 |
+
if isinstance(tol, int_types):
|
104 |
+
return from_int(tol)
|
105 |
+
if isinstance(tol, float):
|
106 |
+
return from_float(tol)
|
107 |
+
if hasattr(tol, "_mpf_"):
|
108 |
+
return tol._mpf_
|
109 |
+
prec, rounding = ctx._prec_rounding
|
110 |
+
if isinstance(tol, str):
|
111 |
+
return from_str(tol, prec, rounding)
|
112 |
+
raise ValueError("expected a real number, got %s" % tol)
|
113 |
+
|
114 |
+
def _convert_fallback(ctx, x, strings):
|
115 |
+
raise TypeError("cannot create mpf from " + repr(x))
|
116 |
+
|
117 |
+
@property
|
118 |
+
def _repr_digits(ctx):
|
119 |
+
return repr_dps(ctx._prec)
|
120 |
+
|
121 |
+
@property
|
122 |
+
def _str_digits(ctx):
|
123 |
+
return ctx._dps
|
124 |
+
|
125 |
+
def to_rational(ctx, s, limit=True):
|
126 |
+
p, q = to_rational(s._mpf_)
|
127 |
+
|
128 |
+
if not limit or q <= ctx.max_denom:
|
129 |
+
return p, q
|
130 |
+
|
131 |
+
p0, q0, p1, q1 = 0, 1, 1, 0
|
132 |
+
n, d = p, q
|
133 |
+
|
134 |
+
while True:
|
135 |
+
a = n//d
|
136 |
+
q2 = q0 + a*q1
|
137 |
+
if q2 > ctx.max_denom:
|
138 |
+
break
|
139 |
+
p0, q0, p1, q1 = p1, q1, p0 + a*p1, q2
|
140 |
+
n, d = d, n - a*d
|
141 |
+
|
142 |
+
k = (ctx.max_denom - q0)//q1
|
143 |
+
|
144 |
+
number = mpq(p, q)
|
145 |
+
bound1 = mpq(p0 + k*p1, q0 + k*q1)
|
146 |
+
bound2 = mpq(p1, q1)
|
147 |
+
|
148 |
+
if not bound2 or not bound1:
|
149 |
+
return p, q
|
150 |
+
elif abs(bound2 - number) <= abs(bound1 - number):
|
151 |
+
return bound2._mpq_
|
152 |
+
else:
|
153 |
+
return bound1._mpq_
|
154 |
+
|
155 |
+
def almosteq(ctx, s, t, rel_eps=None, abs_eps=None):
|
156 |
+
t = ctx.convert(t)
|
157 |
+
if abs_eps is None and rel_eps is None:
|
158 |
+
rel_eps = abs_eps = ctx.tolerance or ctx.make_tol()
|
159 |
+
if abs_eps is None:
|
160 |
+
abs_eps = ctx.convert(rel_eps)
|
161 |
+
elif rel_eps is None:
|
162 |
+
rel_eps = ctx.convert(abs_eps)
|
163 |
+
diff = abs(s-t)
|
164 |
+
if diff <= abs_eps:
|
165 |
+
return True
|
166 |
+
abss = abs(s)
|
167 |
+
abst = abs(t)
|
168 |
+
if abss < abst:
|
169 |
+
err = diff/abst
|
170 |
+
else:
|
171 |
+
err = diff/abss
|
172 |
+
return err <= rel_eps
|
venv/lib/python3.10/site-packages/sympy/polys/domains/old_fractionfield.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`FractionField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.field import Field
|
5 |
+
from sympy.polys.domains.compositedomain import CompositeDomain
|
6 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
7 |
+
from sympy.polys.polyclasses import DMF
|
8 |
+
from sympy.polys.polyerrors import GeneratorsNeeded
|
9 |
+
from sympy.polys.polyutils import dict_from_basic, basic_from_dict, _dict_reorder
|
10 |
+
from sympy.utilities import public
|
11 |
+
|
12 |
+
@public
|
13 |
+
class FractionField(Field, CharacteristicZero, CompositeDomain):
|
14 |
+
"""A class for representing rational function fields. """
|
15 |
+
|
16 |
+
dtype = DMF
|
17 |
+
is_FractionField = is_Frac = True
|
18 |
+
|
19 |
+
has_assoc_Ring = True
|
20 |
+
has_assoc_Field = True
|
21 |
+
|
22 |
+
def __init__(self, dom, *gens):
|
23 |
+
if not gens:
|
24 |
+
raise GeneratorsNeeded("generators not specified")
|
25 |
+
|
26 |
+
lev = len(gens) - 1
|
27 |
+
self.ngens = len(gens)
|
28 |
+
|
29 |
+
self.zero = self.dtype.zero(lev, dom, ring=self)
|
30 |
+
self.one = self.dtype.one(lev, dom, ring=self)
|
31 |
+
|
32 |
+
self.domain = self.dom = dom
|
33 |
+
self.symbols = self.gens = gens
|
34 |
+
|
35 |
+
def new(self, element):
|
36 |
+
return self.dtype(element, self.dom, len(self.gens) - 1, ring=self)
|
37 |
+
|
38 |
+
def __str__(self):
|
39 |
+
return str(self.dom) + '(' + ','.join(map(str, self.gens)) + ')'
|
40 |
+
|
41 |
+
def __hash__(self):
|
42 |
+
return hash((self.__class__.__name__, self.dtype, self.dom, self.gens))
|
43 |
+
|
44 |
+
def __eq__(self, other):
|
45 |
+
"""Returns ``True`` if two domains are equivalent. """
|
46 |
+
return isinstance(other, FractionField) and \
|
47 |
+
self.dtype == other.dtype and self.dom == other.dom and self.gens == other.gens
|
48 |
+
|
49 |
+
def to_sympy(self, a):
|
50 |
+
"""Convert ``a`` to a SymPy object. """
|
51 |
+
return (basic_from_dict(a.numer().to_sympy_dict(), *self.gens) /
|
52 |
+
basic_from_dict(a.denom().to_sympy_dict(), *self.gens))
|
53 |
+
|
54 |
+
def from_sympy(self, a):
|
55 |
+
"""Convert SymPy's expression to ``dtype``. """
|
56 |
+
p, q = a.as_numer_denom()
|
57 |
+
|
58 |
+
num, _ = dict_from_basic(p, gens=self.gens)
|
59 |
+
den, _ = dict_from_basic(q, gens=self.gens)
|
60 |
+
|
61 |
+
for k, v in num.items():
|
62 |
+
num[k] = self.dom.from_sympy(v)
|
63 |
+
|
64 |
+
for k, v in den.items():
|
65 |
+
den[k] = self.dom.from_sympy(v)
|
66 |
+
|
67 |
+
return self((num, den)).cancel()
|
68 |
+
|
69 |
+
def from_ZZ(K1, a, K0):
|
70 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
71 |
+
return K1(K1.dom.convert(a, K0))
|
72 |
+
|
73 |
+
def from_ZZ_python(K1, a, K0):
|
74 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
75 |
+
return K1(K1.dom.convert(a, K0))
|
76 |
+
|
77 |
+
def from_QQ_python(K1, a, K0):
|
78 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
79 |
+
return K1(K1.dom.convert(a, K0))
|
80 |
+
|
81 |
+
def from_ZZ_gmpy(K1, a, K0):
|
82 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
83 |
+
return K1(K1.dom.convert(a, K0))
|
84 |
+
|
85 |
+
def from_QQ_gmpy(K1, a, K0):
|
86 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
87 |
+
return K1(K1.dom.convert(a, K0))
|
88 |
+
|
89 |
+
def from_RealField(K1, a, K0):
|
90 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
91 |
+
return K1(K1.dom.convert(a, K0))
|
92 |
+
|
93 |
+
def from_GlobalPolynomialRing(K1, a, K0):
|
94 |
+
"""Convert a ``DMF`` object to ``dtype``. """
|
95 |
+
if K1.gens == K0.gens:
|
96 |
+
if K1.dom == K0.dom:
|
97 |
+
return K1(a.rep)
|
98 |
+
else:
|
99 |
+
return K1(a.convert(K1.dom).rep)
|
100 |
+
else:
|
101 |
+
monoms, coeffs = _dict_reorder(a.to_dict(), K0.gens, K1.gens)
|
102 |
+
|
103 |
+
if K1.dom != K0.dom:
|
104 |
+
coeffs = [ K1.dom.convert(c, K0.dom) for c in coeffs ]
|
105 |
+
|
106 |
+
return K1(dict(zip(monoms, coeffs)))
|
107 |
+
|
108 |
+
def from_FractionField(K1, a, K0):
|
109 |
+
"""
|
110 |
+
Convert a fraction field element to another fraction field.
|
111 |
+
|
112 |
+
Examples
|
113 |
+
========
|
114 |
+
|
115 |
+
>>> from sympy.polys.polyclasses import DMF
|
116 |
+
>>> from sympy.polys.domains import ZZ, QQ
|
117 |
+
>>> from sympy.abc import x
|
118 |
+
|
119 |
+
>>> f = DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(1)]), ZZ)
|
120 |
+
|
121 |
+
>>> QQx = QQ.old_frac_field(x)
|
122 |
+
>>> ZZx = ZZ.old_frac_field(x)
|
123 |
+
|
124 |
+
>>> QQx.from_FractionField(f, ZZx)
|
125 |
+
(x + 2)/(x + 1)
|
126 |
+
|
127 |
+
"""
|
128 |
+
if K1.gens == K0.gens:
|
129 |
+
if K1.dom == K0.dom:
|
130 |
+
return a
|
131 |
+
else:
|
132 |
+
return K1((a.numer().convert(K1.dom).rep,
|
133 |
+
a.denom().convert(K1.dom).rep))
|
134 |
+
elif set(K0.gens).issubset(K1.gens):
|
135 |
+
nmonoms, ncoeffs = _dict_reorder(
|
136 |
+
a.numer().to_dict(), K0.gens, K1.gens)
|
137 |
+
dmonoms, dcoeffs = _dict_reorder(
|
138 |
+
a.denom().to_dict(), K0.gens, K1.gens)
|
139 |
+
|
140 |
+
if K1.dom != K0.dom:
|
141 |
+
ncoeffs = [ K1.dom.convert(c, K0.dom) for c in ncoeffs ]
|
142 |
+
dcoeffs = [ K1.dom.convert(c, K0.dom) for c in dcoeffs ]
|
143 |
+
|
144 |
+
return K1((dict(zip(nmonoms, ncoeffs)), dict(zip(dmonoms, dcoeffs))))
|
145 |
+
|
146 |
+
def get_ring(self):
|
147 |
+
"""Returns a ring associated with ``self``. """
|
148 |
+
from sympy.polys.domains import PolynomialRing
|
149 |
+
return PolynomialRing(self.dom, *self.gens)
|
150 |
+
|
151 |
+
def poly_ring(self, *gens):
|
152 |
+
"""Returns a polynomial ring, i.e. `K[X]`. """
|
153 |
+
raise NotImplementedError('nested domains not allowed')
|
154 |
+
|
155 |
+
def frac_field(self, *gens):
|
156 |
+
"""Returns a fraction field, i.e. `K(X)`. """
|
157 |
+
raise NotImplementedError('nested domains not allowed')
|
158 |
+
|
159 |
+
def is_positive(self, a):
|
160 |
+
"""Returns True if ``a`` is positive. """
|
161 |
+
return self.dom.is_positive(a.numer().LC())
|
162 |
+
|
163 |
+
def is_negative(self, a):
|
164 |
+
"""Returns True if ``a`` is negative. """
|
165 |
+
return self.dom.is_negative(a.numer().LC())
|
166 |
+
|
167 |
+
def is_nonpositive(self, a):
|
168 |
+
"""Returns True if ``a`` is non-positive. """
|
169 |
+
return self.dom.is_nonpositive(a.numer().LC())
|
170 |
+
|
171 |
+
def is_nonnegative(self, a):
|
172 |
+
"""Returns True if ``a`` is non-negative. """
|
173 |
+
return self.dom.is_nonnegative(a.numer().LC())
|
174 |
+
|
175 |
+
def numer(self, a):
|
176 |
+
"""Returns numerator of ``a``. """
|
177 |
+
return a.numer()
|
178 |
+
|
179 |
+
def denom(self, a):
|
180 |
+
"""Returns denominator of ``a``. """
|
181 |
+
return a.denom()
|
182 |
+
|
183 |
+
def factorial(self, a):
|
184 |
+
"""Returns factorial of ``a``. """
|
185 |
+
return self.dtype(self.dom.factorial(a))
|
venv/lib/python3.10/site-packages/sympy/polys/domains/old_polynomialring.py
ADDED
@@ -0,0 +1,462 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`PolynomialRing` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.agca.modules import FreeModulePolyRing
|
5 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
6 |
+
from sympy.polys.domains.compositedomain import CompositeDomain
|
7 |
+
from sympy.polys.domains.old_fractionfield import FractionField
|
8 |
+
from sympy.polys.domains.ring import Ring
|
9 |
+
from sympy.polys.orderings import monomial_key, build_product_order
|
10 |
+
from sympy.polys.polyclasses import DMP, DMF
|
11 |
+
from sympy.polys.polyerrors import (GeneratorsNeeded, PolynomialError,
|
12 |
+
CoercionFailed, ExactQuotientFailed, NotReversible)
|
13 |
+
from sympy.polys.polyutils import dict_from_basic, basic_from_dict, _dict_reorder
|
14 |
+
from sympy.utilities import public
|
15 |
+
from sympy.utilities.iterables import iterable
|
16 |
+
|
17 |
+
# XXX why does this derive from CharacteristicZero???
|
18 |
+
|
19 |
+
@public
|
20 |
+
class PolynomialRingBase(Ring, CharacteristicZero, CompositeDomain):
|
21 |
+
"""
|
22 |
+
Base class for generalized polynomial rings.
|
23 |
+
|
24 |
+
This base class should be used for uniform access to generalized polynomial
|
25 |
+
rings. Subclasses only supply information about the element storage etc.
|
26 |
+
|
27 |
+
Do not instantiate.
|
28 |
+
"""
|
29 |
+
|
30 |
+
has_assoc_Ring = True
|
31 |
+
has_assoc_Field = True
|
32 |
+
|
33 |
+
default_order = "grevlex"
|
34 |
+
|
35 |
+
def __init__(self, dom, *gens, **opts):
|
36 |
+
if not gens:
|
37 |
+
raise GeneratorsNeeded("generators not specified")
|
38 |
+
|
39 |
+
lev = len(gens) - 1
|
40 |
+
self.ngens = len(gens)
|
41 |
+
|
42 |
+
self.zero = self.dtype.zero(lev, dom, ring=self)
|
43 |
+
self.one = self.dtype.one(lev, dom, ring=self)
|
44 |
+
|
45 |
+
self.domain = self.dom = dom
|
46 |
+
self.symbols = self.gens = gens
|
47 |
+
# NOTE 'order' may not be set if inject was called through CompositeDomain
|
48 |
+
self.order = opts.get('order', monomial_key(self.default_order))
|
49 |
+
|
50 |
+
def new(self, element):
|
51 |
+
return self.dtype(element, self.dom, len(self.gens) - 1, ring=self)
|
52 |
+
|
53 |
+
def __str__(self):
|
54 |
+
s_order = str(self.order)
|
55 |
+
orderstr = (
|
56 |
+
" order=" + s_order) if s_order != self.default_order else ""
|
57 |
+
return str(self.dom) + '[' + ','.join(map(str, self.gens)) + orderstr + ']'
|
58 |
+
|
59 |
+
def __hash__(self):
|
60 |
+
return hash((self.__class__.__name__, self.dtype, self.dom,
|
61 |
+
self.gens, self.order))
|
62 |
+
|
63 |
+
def __eq__(self, other):
|
64 |
+
"""Returns ``True`` if two domains are equivalent. """
|
65 |
+
return isinstance(other, PolynomialRingBase) and \
|
66 |
+
self.dtype == other.dtype and self.dom == other.dom and \
|
67 |
+
self.gens == other.gens and self.order == other.order
|
68 |
+
|
69 |
+
def from_ZZ(K1, a, K0):
|
70 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
71 |
+
return K1(K1.dom.convert(a, K0))
|
72 |
+
|
73 |
+
def from_ZZ_python(K1, a, K0):
|
74 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
75 |
+
return K1(K1.dom.convert(a, K0))
|
76 |
+
|
77 |
+
def from_QQ(K1, a, K0):
|
78 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
79 |
+
return K1(K1.dom.convert(a, K0))
|
80 |
+
|
81 |
+
def from_QQ_python(K1, a, K0):
|
82 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
83 |
+
return K1(K1.dom.convert(a, K0))
|
84 |
+
|
85 |
+
def from_ZZ_gmpy(K1, a, K0):
|
86 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
87 |
+
return K1(K1.dom.convert(a, K0))
|
88 |
+
|
89 |
+
def from_QQ_gmpy(K1, a, K0):
|
90 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
91 |
+
return K1(K1.dom.convert(a, K0))
|
92 |
+
|
93 |
+
def from_RealField(K1, a, K0):
|
94 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
95 |
+
return K1(K1.dom.convert(a, K0))
|
96 |
+
|
97 |
+
def from_AlgebraicField(K1, a, K0):
|
98 |
+
"""Convert a ``ANP`` object to ``dtype``. """
|
99 |
+
if K1.dom == K0:
|
100 |
+
return K1(a)
|
101 |
+
|
102 |
+
def from_PolynomialRing(K1, a, K0):
|
103 |
+
"""Convert a ``PolyElement`` object to ``dtype``. """
|
104 |
+
if K1.gens == K0.symbols:
|
105 |
+
if K1.dom == K0.dom:
|
106 |
+
return K1(dict(a)) # set the correct ring
|
107 |
+
else:
|
108 |
+
convert_dom = lambda c: K1.dom.convert_from(c, K0.dom)
|
109 |
+
return K1({m: convert_dom(c) for m, c in a.items()})
|
110 |
+
else:
|
111 |
+
monoms, coeffs = _dict_reorder(a.to_dict(), K0.symbols, K1.gens)
|
112 |
+
|
113 |
+
if K1.dom != K0.dom:
|
114 |
+
coeffs = [ K1.dom.convert(c, K0.dom) for c in coeffs ]
|
115 |
+
|
116 |
+
return K1(dict(zip(monoms, coeffs)))
|
117 |
+
|
118 |
+
def from_GlobalPolynomialRing(K1, a, K0):
|
119 |
+
"""Convert a ``DMP`` object to ``dtype``. """
|
120 |
+
if K1.gens == K0.gens:
|
121 |
+
if K1.dom == K0.dom:
|
122 |
+
return K1(a.rep) # set the correct ring
|
123 |
+
else:
|
124 |
+
return K1(a.convert(K1.dom).rep)
|
125 |
+
else:
|
126 |
+
monoms, coeffs = _dict_reorder(a.to_dict(), K0.gens, K1.gens)
|
127 |
+
|
128 |
+
if K1.dom != K0.dom:
|
129 |
+
coeffs = [ K1.dom.convert(c, K0.dom) for c in coeffs ]
|
130 |
+
|
131 |
+
return K1(dict(zip(monoms, coeffs)))
|
132 |
+
|
133 |
+
def get_field(self):
|
134 |
+
"""Returns a field associated with ``self``. """
|
135 |
+
return FractionField(self.dom, *self.gens)
|
136 |
+
|
137 |
+
def poly_ring(self, *gens):
|
138 |
+
"""Returns a polynomial ring, i.e. ``K[X]``. """
|
139 |
+
raise NotImplementedError('nested domains not allowed')
|
140 |
+
|
141 |
+
def frac_field(self, *gens):
|
142 |
+
"""Returns a fraction field, i.e. ``K(X)``. """
|
143 |
+
raise NotImplementedError('nested domains not allowed')
|
144 |
+
|
145 |
+
def revert(self, a):
|
146 |
+
try:
|
147 |
+
return 1/a
|
148 |
+
except (ExactQuotientFailed, ZeroDivisionError):
|
149 |
+
raise NotReversible('%s is not a unit' % a)
|
150 |
+
|
151 |
+
def gcdex(self, a, b):
|
152 |
+
"""Extended GCD of ``a`` and ``b``. """
|
153 |
+
return a.gcdex(b)
|
154 |
+
|
155 |
+
def gcd(self, a, b):
|
156 |
+
"""Returns GCD of ``a`` and ``b``. """
|
157 |
+
return a.gcd(b)
|
158 |
+
|
159 |
+
def lcm(self, a, b):
|
160 |
+
"""Returns LCM of ``a`` and ``b``. """
|
161 |
+
return a.lcm(b)
|
162 |
+
|
163 |
+
def factorial(self, a):
|
164 |
+
"""Returns factorial of ``a``. """
|
165 |
+
return self.dtype(self.dom.factorial(a))
|
166 |
+
|
167 |
+
def _vector_to_sdm(self, v, order):
|
168 |
+
"""
|
169 |
+
For internal use by the modules class.
|
170 |
+
|
171 |
+
Convert an iterable of elements of this ring into a sparse distributed
|
172 |
+
module element.
|
173 |
+
"""
|
174 |
+
raise NotImplementedError
|
175 |
+
|
176 |
+
def _sdm_to_dics(self, s, n):
|
177 |
+
"""Helper for _sdm_to_vector."""
|
178 |
+
from sympy.polys.distributedmodules import sdm_to_dict
|
179 |
+
dic = sdm_to_dict(s)
|
180 |
+
res = [{} for _ in range(n)]
|
181 |
+
for k, v in dic.items():
|
182 |
+
res[k[0]][k[1:]] = v
|
183 |
+
return res
|
184 |
+
|
185 |
+
def _sdm_to_vector(self, s, n):
|
186 |
+
"""
|
187 |
+
For internal use by the modules class.
|
188 |
+
|
189 |
+
Convert a sparse distributed module into a list of length ``n``.
|
190 |
+
|
191 |
+
Examples
|
192 |
+
========
|
193 |
+
|
194 |
+
>>> from sympy import QQ, ilex
|
195 |
+
>>> from sympy.abc import x, y
|
196 |
+
>>> R = QQ.old_poly_ring(x, y, order=ilex)
|
197 |
+
>>> L = [((1, 1, 1), QQ(1)), ((0, 1, 0), QQ(1)), ((0, 0, 1), QQ(2))]
|
198 |
+
>>> R._sdm_to_vector(L, 2)
|
199 |
+
[x + 2*y, x*y]
|
200 |
+
"""
|
201 |
+
dics = self._sdm_to_dics(s, n)
|
202 |
+
# NOTE this works for global and local rings!
|
203 |
+
return [self(x) for x in dics]
|
204 |
+
|
205 |
+
def free_module(self, rank):
|
206 |
+
"""
|
207 |
+
Generate a free module of rank ``rank`` over ``self``.
|
208 |
+
|
209 |
+
Examples
|
210 |
+
========
|
211 |
+
|
212 |
+
>>> from sympy.abc import x
|
213 |
+
>>> from sympy import QQ
|
214 |
+
>>> QQ.old_poly_ring(x).free_module(2)
|
215 |
+
QQ[x]**2
|
216 |
+
"""
|
217 |
+
return FreeModulePolyRing(self, rank)
|
218 |
+
|
219 |
+
|
220 |
+
def _vector_to_sdm_helper(v, order):
|
221 |
+
"""Helper method for common code in Global and Local poly rings."""
|
222 |
+
from sympy.polys.distributedmodules import sdm_from_dict
|
223 |
+
d = {}
|
224 |
+
for i, e in enumerate(v):
|
225 |
+
for key, value in e.to_dict().items():
|
226 |
+
d[(i,) + key] = value
|
227 |
+
return sdm_from_dict(d, order)
|
228 |
+
|
229 |
+
|
230 |
+
@public
|
231 |
+
class GlobalPolynomialRing(PolynomialRingBase):
|
232 |
+
"""A true polynomial ring, with objects DMP. """
|
233 |
+
|
234 |
+
is_PolynomialRing = is_Poly = True
|
235 |
+
dtype = DMP
|
236 |
+
|
237 |
+
def from_FractionField(K1, a, K0):
|
238 |
+
"""
|
239 |
+
Convert a ``DMF`` object to ``DMP``.
|
240 |
+
|
241 |
+
Examples
|
242 |
+
========
|
243 |
+
|
244 |
+
>>> from sympy.polys.polyclasses import DMP, DMF
|
245 |
+
>>> from sympy.polys.domains import ZZ
|
246 |
+
>>> from sympy.abc import x
|
247 |
+
|
248 |
+
>>> f = DMF(([ZZ(1), ZZ(1)], [ZZ(1)]), ZZ)
|
249 |
+
>>> K = ZZ.old_frac_field(x)
|
250 |
+
|
251 |
+
>>> F = ZZ.old_poly_ring(x).from_FractionField(f, K)
|
252 |
+
|
253 |
+
>>> F == DMP([ZZ(1), ZZ(1)], ZZ)
|
254 |
+
True
|
255 |
+
>>> type(F)
|
256 |
+
<class 'sympy.polys.polyclasses.DMP'>
|
257 |
+
|
258 |
+
"""
|
259 |
+
if a.denom().is_one:
|
260 |
+
return K1.from_GlobalPolynomialRing(a.numer(), K0)
|
261 |
+
|
262 |
+
def to_sympy(self, a):
|
263 |
+
"""Convert ``a`` to a SymPy object. """
|
264 |
+
return basic_from_dict(a.to_sympy_dict(), *self.gens)
|
265 |
+
|
266 |
+
def from_sympy(self, a):
|
267 |
+
"""Convert SymPy's expression to ``dtype``. """
|
268 |
+
try:
|
269 |
+
rep, _ = dict_from_basic(a, gens=self.gens)
|
270 |
+
except PolynomialError:
|
271 |
+
raise CoercionFailed("Cannot convert %s to type %s" % (a, self))
|
272 |
+
|
273 |
+
for k, v in rep.items():
|
274 |
+
rep[k] = self.dom.from_sympy(v)
|
275 |
+
|
276 |
+
return self(rep)
|
277 |
+
|
278 |
+
def is_positive(self, a):
|
279 |
+
"""Returns True if ``LC(a)`` is positive. """
|
280 |
+
return self.dom.is_positive(a.LC())
|
281 |
+
|
282 |
+
def is_negative(self, a):
|
283 |
+
"""Returns True if ``LC(a)`` is negative. """
|
284 |
+
return self.dom.is_negative(a.LC())
|
285 |
+
|
286 |
+
def is_nonpositive(self, a):
|
287 |
+
"""Returns True if ``LC(a)`` is non-positive. """
|
288 |
+
return self.dom.is_nonpositive(a.LC())
|
289 |
+
|
290 |
+
def is_nonnegative(self, a):
|
291 |
+
"""Returns True if ``LC(a)`` is non-negative. """
|
292 |
+
return self.dom.is_nonnegative(a.LC())
|
293 |
+
|
294 |
+
def _vector_to_sdm(self, v, order):
|
295 |
+
"""
|
296 |
+
Examples
|
297 |
+
========
|
298 |
+
|
299 |
+
>>> from sympy import lex, QQ
|
300 |
+
>>> from sympy.abc import x, y
|
301 |
+
>>> R = QQ.old_poly_ring(x, y)
|
302 |
+
>>> f = R.convert(x + 2*y)
|
303 |
+
>>> g = R.convert(x * y)
|
304 |
+
>>> R._vector_to_sdm([f, g], lex)
|
305 |
+
[((1, 1, 1), 1), ((0, 1, 0), 1), ((0, 0, 1), 2)]
|
306 |
+
"""
|
307 |
+
return _vector_to_sdm_helper(v, order)
|
308 |
+
|
309 |
+
|
310 |
+
class GeneralizedPolynomialRing(PolynomialRingBase):
|
311 |
+
"""A generalized polynomial ring, with objects DMF. """
|
312 |
+
|
313 |
+
dtype = DMF
|
314 |
+
|
315 |
+
def new(self, a):
|
316 |
+
"""Construct an element of ``self`` domain from ``a``. """
|
317 |
+
res = self.dtype(a, self.dom, len(self.gens) - 1, ring=self)
|
318 |
+
|
319 |
+
# make sure res is actually in our ring
|
320 |
+
if res.denom().terms(order=self.order)[0][0] != (0,)*len(self.gens):
|
321 |
+
from sympy.printing.str import sstr
|
322 |
+
raise CoercionFailed("denominator %s not allowed in %s"
|
323 |
+
% (sstr(res), self))
|
324 |
+
return res
|
325 |
+
|
326 |
+
def __contains__(self, a):
|
327 |
+
try:
|
328 |
+
a = self.convert(a)
|
329 |
+
except CoercionFailed:
|
330 |
+
return False
|
331 |
+
return a.denom().terms(order=self.order)[0][0] == (0,)*len(self.gens)
|
332 |
+
|
333 |
+
def from_FractionField(K1, a, K0):
|
334 |
+
dmf = K1.get_field().from_FractionField(a, K0)
|
335 |
+
return K1((dmf.num, dmf.den))
|
336 |
+
|
337 |
+
def to_sympy(self, a):
|
338 |
+
"""Convert ``a`` to a SymPy object. """
|
339 |
+
return (basic_from_dict(a.numer().to_sympy_dict(), *self.gens) /
|
340 |
+
basic_from_dict(a.denom().to_sympy_dict(), *self.gens))
|
341 |
+
|
342 |
+
def from_sympy(self, a):
|
343 |
+
"""Convert SymPy's expression to ``dtype``. """
|
344 |
+
p, q = a.as_numer_denom()
|
345 |
+
|
346 |
+
num, _ = dict_from_basic(p, gens=self.gens)
|
347 |
+
den, _ = dict_from_basic(q, gens=self.gens)
|
348 |
+
|
349 |
+
for k, v in num.items():
|
350 |
+
num[k] = self.dom.from_sympy(v)
|
351 |
+
|
352 |
+
for k, v in den.items():
|
353 |
+
den[k] = self.dom.from_sympy(v)
|
354 |
+
|
355 |
+
return self((num, den)).cancel()
|
356 |
+
|
357 |
+
def _vector_to_sdm(self, v, order):
|
358 |
+
"""
|
359 |
+
Turn an iterable into a sparse distributed module.
|
360 |
+
|
361 |
+
Note that the vector is multiplied by a unit first to make all entries
|
362 |
+
polynomials.
|
363 |
+
|
364 |
+
Examples
|
365 |
+
========
|
366 |
+
|
367 |
+
>>> from sympy import ilex, QQ
|
368 |
+
>>> from sympy.abc import x, y
|
369 |
+
>>> R = QQ.old_poly_ring(x, y, order=ilex)
|
370 |
+
>>> f = R.convert((x + 2*y) / (1 + x))
|
371 |
+
>>> g = R.convert(x * y)
|
372 |
+
>>> R._vector_to_sdm([f, g], ilex)
|
373 |
+
[((0, 0, 1), 2), ((0, 1, 0), 1), ((1, 1, 1), 1), ((1,
|
374 |
+
2, 1), 1)]
|
375 |
+
"""
|
376 |
+
# NOTE this is quite inefficient...
|
377 |
+
u = self.one.numer()
|
378 |
+
for x in v:
|
379 |
+
u *= x.denom()
|
380 |
+
return _vector_to_sdm_helper([x.numer()*u/x.denom() for x in v], order)
|
381 |
+
|
382 |
+
|
383 |
+
@public
|
384 |
+
def PolynomialRing(dom, *gens, **opts):
|
385 |
+
r"""
|
386 |
+
Create a generalized multivariate polynomial ring.
|
387 |
+
|
388 |
+
A generalized polynomial ring is defined by a ground field `K`, a set
|
389 |
+
of generators (typically `x_1, \ldots, x_n`) and a monomial order `<`.
|
390 |
+
The monomial order can be global, local or mixed. In any case it induces
|
391 |
+
a total ordering on the monomials, and there exists for every (non-zero)
|
392 |
+
polynomial `f \in K[x_1, \ldots, x_n]` a well-defined "leading monomial"
|
393 |
+
`LM(f) = LM(f, >)`. One can then define a multiplicative subset
|
394 |
+
`S = S_> = \{f \in K[x_1, \ldots, x_n] | LM(f) = 1\}`. The generalized
|
395 |
+
polynomial ring corresponding to the monomial order is
|
396 |
+
`R = S^{-1}K[x_1, \ldots, x_n]`.
|
397 |
+
|
398 |
+
If `>` is a so-called global order, that is `1` is the smallest monomial,
|
399 |
+
then we just have `S = K` and `R = K[x_1, \ldots, x_n]`.
|
400 |
+
|
401 |
+
Examples
|
402 |
+
========
|
403 |
+
|
404 |
+
A few examples may make this clearer.
|
405 |
+
|
406 |
+
>>> from sympy.abc import x, y
|
407 |
+
>>> from sympy import QQ
|
408 |
+
|
409 |
+
Our first ring uses global lexicographic order.
|
410 |
+
|
411 |
+
>>> R1 = QQ.old_poly_ring(x, y, order=(("lex", x, y),))
|
412 |
+
|
413 |
+
The second ring uses local lexicographic order. Note that when using a
|
414 |
+
single (non-product) order, you can just specify the name and omit the
|
415 |
+
variables:
|
416 |
+
|
417 |
+
>>> R2 = QQ.old_poly_ring(x, y, order="ilex")
|
418 |
+
|
419 |
+
The third and fourth rings use a mixed orders:
|
420 |
+
|
421 |
+
>>> o1 = (("ilex", x), ("lex", y))
|
422 |
+
>>> o2 = (("lex", x), ("ilex", y))
|
423 |
+
>>> R3 = QQ.old_poly_ring(x, y, order=o1)
|
424 |
+
>>> R4 = QQ.old_poly_ring(x, y, order=o2)
|
425 |
+
|
426 |
+
We will investigate what elements of `K(x, y)` are contained in the various
|
427 |
+
rings.
|
428 |
+
|
429 |
+
>>> L = [x, 1/x, y/(1 + x), 1/(1 + y), 1/(1 + x*y)]
|
430 |
+
>>> test = lambda R: [f in R for f in L]
|
431 |
+
|
432 |
+
The first ring is just `K[x, y]`:
|
433 |
+
|
434 |
+
>>> test(R1)
|
435 |
+
[True, False, False, False, False]
|
436 |
+
|
437 |
+
The second ring is R1 localised at the maximal ideal (x, y):
|
438 |
+
|
439 |
+
>>> test(R2)
|
440 |
+
[True, False, True, True, True]
|
441 |
+
|
442 |
+
The third ring is R1 localised at the prime ideal (x):
|
443 |
+
|
444 |
+
>>> test(R3)
|
445 |
+
[True, False, True, False, True]
|
446 |
+
|
447 |
+
Finally the fourth ring is R1 localised at `S = K[x, y] \setminus yK[y]`:
|
448 |
+
|
449 |
+
>>> test(R4)
|
450 |
+
[True, False, False, True, False]
|
451 |
+
"""
|
452 |
+
|
453 |
+
order = opts.get("order", GeneralizedPolynomialRing.default_order)
|
454 |
+
if iterable(order):
|
455 |
+
order = build_product_order(order, gens)
|
456 |
+
order = monomial_key(order)
|
457 |
+
opts['order'] = order
|
458 |
+
|
459 |
+
if order.is_global:
|
460 |
+
return GlobalPolynomialRing(dom, *gens, **opts)
|
461 |
+
else:
|
462 |
+
return GeneralizedPolynomialRing(dom, *gens, **opts)
|