forked from diofant/diofant
/
polynomialring.py
141 lines (103 loc) · 4.11 KB
/
polynomialring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Implementation of :class:`PolynomialRing` class. """
from ..polys.polyerrors import CoercionFailed, GeneratorsError
from .compositedomain import CompositeDomain
from .ring import Ring
__all__ = ('PolynomialRing',)
class PolynomialRing(Ring, CompositeDomain):
"""A class for representing multivariate polynomial rings. """
is_PolynomialRing = is_Poly = True
has_assoc_Ring = True
has_assoc_Field = True
def __init__(self, domain_or_ring, symbols=None, order=None):
from ..polys.rings import PolyRing
if isinstance(domain_or_ring, PolyRing) and symbols is None and order is None:
ring = domain_or_ring
else:
ring = PolyRing(symbols, domain_or_ring, order)
self.ring = ring
self.dtype = ring.dtype
self.gens = ring.gens
self.ngens = ring.ngens
self.symbols = ring.symbols
self.domain = ring.domain
self.rep = str(self.domain) + '[' + ','.join(map(str, self.symbols)) + ']'
def new(self, element):
return self.ring.ring_new(element)
@property
def zero(self):
return self.ring.zero
@property
def one(self):
return self.ring.one
@property
def order(self):
return self.ring.order
def __hash__(self):
return hash((self.__class__.__name__, self.dtype, self.domain, self.symbols))
def __eq__(self, other):
"""Returns `True` if two domains are equivalent. """
return isinstance(other, PolynomialRing) and \
self.dtype == other.dtype and self.ring == other.ring
def to_diofant(self, a):
"""Convert `a` to a Diofant object. """
return a.as_expr()
def from_diofant(self, a):
"""Convert Diofant's expression to `dtype`. """
return self.ring.from_expr(a)
def from_ZZ_python(self, a, K0):
"""Convert a Python `int` object to `dtype`. """
return self(self.domain.convert(a, K0))
def from_QQ_python(self, a, K0):
"""Convert a Python `Fraction` object to `dtype`. """
return self(self.domain.convert(a, K0))
def from_ZZ_gmpy(self, a, K0):
"""Convert a GMPY `mpz` object to `dtype`. """
return self(self.domain.convert(a, K0))
def from_QQ_gmpy(self, a, K0):
"""Convert a GMPY `mpq` object to `dtype`. """
return self(self.domain.convert(a, K0))
def from_RealField(self, a, K0):
"""Convert a mpmath `mpf` object to `dtype`. """
return self(self.domain.convert(a, K0))
def from_AlgebraicField(self, a, K0):
"""Convert an algebraic number to ``dtype``. """
if self.domain == K0:
return self.new(a)
def from_PolynomialRing(self, a, K0):
"""Convert a polynomial to ``dtype``. """
try:
return a.set_ring(self.ring)
except (CoercionFailed, GeneratorsError):
return
def from_FractionField(self, a, K0):
"""Convert a rational function to ``dtype``. """
denom = K0.denom(a)
if denom.is_ground:
return self.from_PolynomialRing(K0.numer(a)/denom, K0.ring.to_domain())
def get_field(self):
"""Returns a field associated with `self`. """
return self.ring.to_field()
def is_positive(self, a):
"""Returns True if `LC(a)` is positive. """
return self.domain.is_positive(a.LC)
def is_negative(self, a):
"""Returns True if `LC(a)` is negative. """
return self.domain.is_negative(a.LC)
def is_nonpositive(self, a):
"""Returns True if `LC(a)` is non-positive. """
return self.domain.is_nonpositive(a.LC)
def is_nonnegative(self, a):
"""Returns True if `LC(a)` is non-negative. """
return self.domain.is_nonnegative(a.LC)
def gcdex(self, a, b):
"""Extended GCD of `a` and `b`. """
return a.gcdex(b)
def gcd(self, a, b):
"""Returns GCD of `a` and `b`. """
return a.gcd(b)
def lcm(self, a, b):
"""Returns LCM of `a` and `b`. """
return a.lcm(b)
def factorial(self, a):
"""Returns factorial of `a`. """
return self.new(self.domain.factorial(a))