Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/constructor.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/densearith.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/densetools.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/dispersion.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/distributedmodules.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/galoistools.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyerrors.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyquinticconst.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rootisolation.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rootoftools.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__init__.py +5 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/extensions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/homomorphisms.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/ideals.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/modules.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/extensions.py +346 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/homomorphisms.py +691 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/ideals.py +394 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/modules.py +1484 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_extensions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_homomorphisms.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_ideals.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_modules.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_extensions.py +196 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_homomorphisms.py +113 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_ideals.py +131 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_modules.py +408 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__init__.py +15 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/_typing.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/ddm.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/dense.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainscalar.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/eigen.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/exceptions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/linsolve.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/lll.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/normalforms.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/sdm.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/_typing.py +12 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/ddm.py +496 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/dense.py +348 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/domainmatrix.py +1791 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/domainscalar.py +116 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/eigen.py +90 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/exceptions.py +67 -0
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/constructor.cpython-310.pyc
ADDED
Binary file (9.27 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/densearith.cpython-310.pyc
ADDED
Binary file (34.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/densetools.cpython-310.pyc
ADDED
Binary file (26.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/dispersion.cpython-310.pyc
ADDED
Binary file (5.18 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/distributedmodules.cpython-310.pyc
ADDED
Binary file (23 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/galoistools.cpython-310.pyc
ADDED
Binary file (53.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyerrors.cpython-310.pyc
ADDED
Binary file (7.17 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyquinticconst.cpython-310.pyc
ADDED
Binary file (132 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rootisolation.cpython-310.pyc
ADDED
Binary file (51.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rootoftools.cpython-310.pyc
ADDED
Binary file (34.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__init__.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Module for algebraic geometry and commutative algebra."""
|
2 |
+
|
3 |
+
from .homomorphisms import homomorphism
|
4 |
+
|
5 |
+
__all__ = ['homomorphism']
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (324 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/extensions.cpython-310.pyc
ADDED
Binary file (10.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/homomorphisms.cpython-310.pyc
ADDED
Binary file (25.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/ideals.cpython-310.pyc
ADDED
Binary file (13.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/__pycache__/modules.cpython-310.pyc
ADDED
Binary file (52.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/extensions.py
ADDED
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Finite extensions of ring domains."""
|
2 |
+
|
3 |
+
from sympy.polys.domains.domain import Domain
|
4 |
+
from sympy.polys.domains.domainelement import DomainElement
|
5 |
+
from sympy.polys.polyerrors import (CoercionFailed, NotInvertible,
|
6 |
+
GeneratorsError)
|
7 |
+
from sympy.polys.polytools import Poly
|
8 |
+
from sympy.printing.defaults import DefaultPrinting
|
9 |
+
|
10 |
+
|
11 |
+
class ExtensionElement(DomainElement, DefaultPrinting):
|
12 |
+
"""
|
13 |
+
Element of a finite extension.
|
14 |
+
|
15 |
+
A class of univariate polynomials modulo the ``modulus``
|
16 |
+
of the extension ``ext``. It is represented by the
|
17 |
+
unique polynomial ``rep`` of lowest degree. Both
|
18 |
+
``rep`` and the representation ``mod`` of ``modulus``
|
19 |
+
are of class DMP.
|
20 |
+
|
21 |
+
"""
|
22 |
+
__slots__ = ('rep', 'ext')
|
23 |
+
|
24 |
+
def __init__(self, rep, ext):
|
25 |
+
self.rep = rep
|
26 |
+
self.ext = ext
|
27 |
+
|
28 |
+
def parent(f):
|
29 |
+
return f.ext
|
30 |
+
|
31 |
+
def __bool__(f):
|
32 |
+
return bool(f.rep)
|
33 |
+
|
34 |
+
def __pos__(f):
|
35 |
+
return f
|
36 |
+
|
37 |
+
def __neg__(f):
|
38 |
+
return ExtElem(-f.rep, f.ext)
|
39 |
+
|
40 |
+
def _get_rep(f, g):
|
41 |
+
if isinstance(g, ExtElem):
|
42 |
+
if g.ext == f.ext:
|
43 |
+
return g.rep
|
44 |
+
else:
|
45 |
+
return None
|
46 |
+
else:
|
47 |
+
try:
|
48 |
+
g = f.ext.convert(g)
|
49 |
+
return g.rep
|
50 |
+
except CoercionFailed:
|
51 |
+
return None
|
52 |
+
|
53 |
+
def __add__(f, g):
|
54 |
+
rep = f._get_rep(g)
|
55 |
+
if rep is not None:
|
56 |
+
return ExtElem(f.rep + rep, f.ext)
|
57 |
+
else:
|
58 |
+
return NotImplemented
|
59 |
+
|
60 |
+
__radd__ = __add__
|
61 |
+
|
62 |
+
def __sub__(f, g):
|
63 |
+
rep = f._get_rep(g)
|
64 |
+
if rep is not None:
|
65 |
+
return ExtElem(f.rep - rep, f.ext)
|
66 |
+
else:
|
67 |
+
return NotImplemented
|
68 |
+
|
69 |
+
def __rsub__(f, g):
|
70 |
+
rep = f._get_rep(g)
|
71 |
+
if rep is not None:
|
72 |
+
return ExtElem(rep - f.rep, f.ext)
|
73 |
+
else:
|
74 |
+
return NotImplemented
|
75 |
+
|
76 |
+
def __mul__(f, g):
|
77 |
+
rep = f._get_rep(g)
|
78 |
+
if rep is not None:
|
79 |
+
return ExtElem((f.rep * rep) % f.ext.mod, f.ext)
|
80 |
+
else:
|
81 |
+
return NotImplemented
|
82 |
+
|
83 |
+
__rmul__ = __mul__
|
84 |
+
|
85 |
+
def _divcheck(f):
|
86 |
+
"""Raise if division is not implemented for this divisor"""
|
87 |
+
if not f:
|
88 |
+
raise NotInvertible('Zero divisor')
|
89 |
+
elif f.ext.is_Field:
|
90 |
+
return True
|
91 |
+
elif f.rep.is_ground and f.ext.domain.is_unit(f.rep.rep[0]):
|
92 |
+
return True
|
93 |
+
else:
|
94 |
+
# Some cases like (2*x + 2)/2 over ZZ will fail here. It is
|
95 |
+
# unclear how to implement division in general if the ground
|
96 |
+
# domain is not a field so for now it was decided to restrict the
|
97 |
+
# implementation to division by invertible constants.
|
98 |
+
msg = (f"Can not invert {f} in {f.ext}. "
|
99 |
+
"Only division by invertible constants is implemented.")
|
100 |
+
raise NotImplementedError(msg)
|
101 |
+
|
102 |
+
def inverse(f):
|
103 |
+
"""Multiplicative inverse.
|
104 |
+
|
105 |
+
Raises
|
106 |
+
======
|
107 |
+
|
108 |
+
NotInvertible
|
109 |
+
If the element is a zero divisor.
|
110 |
+
|
111 |
+
"""
|
112 |
+
f._divcheck()
|
113 |
+
|
114 |
+
if f.ext.is_Field:
|
115 |
+
invrep = f.rep.invert(f.ext.mod)
|
116 |
+
else:
|
117 |
+
R = f.ext.ring
|
118 |
+
invrep = R.exquo(R.one, f.rep)
|
119 |
+
|
120 |
+
return ExtElem(invrep, f.ext)
|
121 |
+
|
122 |
+
def __truediv__(f, g):
|
123 |
+
rep = f._get_rep(g)
|
124 |
+
if rep is None:
|
125 |
+
return NotImplemented
|
126 |
+
g = ExtElem(rep, f.ext)
|
127 |
+
|
128 |
+
try:
|
129 |
+
ginv = g.inverse()
|
130 |
+
except NotInvertible:
|
131 |
+
raise ZeroDivisionError(f"{f} / {g}")
|
132 |
+
|
133 |
+
return f * ginv
|
134 |
+
|
135 |
+
__floordiv__ = __truediv__
|
136 |
+
|
137 |
+
def __rtruediv__(f, g):
|
138 |
+
try:
|
139 |
+
g = f.ext.convert(g)
|
140 |
+
except CoercionFailed:
|
141 |
+
return NotImplemented
|
142 |
+
return g / f
|
143 |
+
|
144 |
+
__rfloordiv__ = __rtruediv__
|
145 |
+
|
146 |
+
def __mod__(f, g):
|
147 |
+
rep = f._get_rep(g)
|
148 |
+
if rep is None:
|
149 |
+
return NotImplemented
|
150 |
+
g = ExtElem(rep, f.ext)
|
151 |
+
|
152 |
+
try:
|
153 |
+
g._divcheck()
|
154 |
+
except NotInvertible:
|
155 |
+
raise ZeroDivisionError(f"{f} % {g}")
|
156 |
+
|
157 |
+
# Division where defined is always exact so there is no remainder
|
158 |
+
return f.ext.zero
|
159 |
+
|
160 |
+
def __rmod__(f, g):
|
161 |
+
try:
|
162 |
+
g = f.ext.convert(g)
|
163 |
+
except CoercionFailed:
|
164 |
+
return NotImplemented
|
165 |
+
return g % f
|
166 |
+
|
167 |
+
def __pow__(f, n):
|
168 |
+
if not isinstance(n, int):
|
169 |
+
raise TypeError("exponent of type 'int' expected")
|
170 |
+
if n < 0:
|
171 |
+
try:
|
172 |
+
f, n = f.inverse(), -n
|
173 |
+
except NotImplementedError:
|
174 |
+
raise ValueError("negative powers are not defined")
|
175 |
+
|
176 |
+
b = f.rep
|
177 |
+
m = f.ext.mod
|
178 |
+
r = f.ext.one.rep
|
179 |
+
while n > 0:
|
180 |
+
if n % 2:
|
181 |
+
r = (r*b) % m
|
182 |
+
b = (b*b) % m
|
183 |
+
n //= 2
|
184 |
+
|
185 |
+
return ExtElem(r, f.ext)
|
186 |
+
|
187 |
+
def __eq__(f, g):
|
188 |
+
if isinstance(g, ExtElem):
|
189 |
+
return f.rep == g.rep and f.ext == g.ext
|
190 |
+
else:
|
191 |
+
return NotImplemented
|
192 |
+
|
193 |
+
def __ne__(f, g):
|
194 |
+
return not f == g
|
195 |
+
|
196 |
+
def __hash__(f):
|
197 |
+
return hash((f.rep, f.ext))
|
198 |
+
|
199 |
+
def __str__(f):
|
200 |
+
from sympy.printing.str import sstr
|
201 |
+
return sstr(f.rep)
|
202 |
+
|
203 |
+
__repr__ = __str__
|
204 |
+
|
205 |
+
@property
|
206 |
+
def is_ground(f):
|
207 |
+
return f.rep.is_ground
|
208 |
+
|
209 |
+
def to_ground(f):
|
210 |
+
[c] = f.rep.to_list()
|
211 |
+
return c
|
212 |
+
|
213 |
+
ExtElem = ExtensionElement
|
214 |
+
|
215 |
+
|
216 |
+
class MonogenicFiniteExtension(Domain):
|
217 |
+
r"""
|
218 |
+
Finite extension generated by an integral element.
|
219 |
+
|
220 |
+
The generator is defined by a monic univariate
|
221 |
+
polynomial derived from the argument ``mod``.
|
222 |
+
|
223 |
+
A shorter alias is ``FiniteExtension``.
|
224 |
+
|
225 |
+
Examples
|
226 |
+
========
|
227 |
+
|
228 |
+
Quadratic integer ring $\mathbb{Z}[\sqrt2]$:
|
229 |
+
|
230 |
+
>>> from sympy import Symbol, Poly
|
231 |
+
>>> from sympy.polys.agca.extensions import FiniteExtension
|
232 |
+
>>> x = Symbol('x')
|
233 |
+
>>> R = FiniteExtension(Poly(x**2 - 2)); R
|
234 |
+
ZZ[x]/(x**2 - 2)
|
235 |
+
>>> R.rank
|
236 |
+
2
|
237 |
+
>>> R(1 + x)*(3 - 2*x)
|
238 |
+
x - 1
|
239 |
+
|
240 |
+
Finite field $GF(5^3)$ defined by the primitive
|
241 |
+
polynomial $x^3 + x^2 + 2$ (over $\mathbb{Z}_5$).
|
242 |
+
|
243 |
+
>>> F = FiniteExtension(Poly(x**3 + x**2 + 2, modulus=5)); F
|
244 |
+
GF(5)[x]/(x**3 + x**2 + 2)
|
245 |
+
>>> F.basis
|
246 |
+
(1, x, x**2)
|
247 |
+
>>> F(x + 3)/(x**2 + 2)
|
248 |
+
-2*x**2 + x + 2
|
249 |
+
|
250 |
+
Function field of an elliptic curve:
|
251 |
+
|
252 |
+
>>> t = Symbol('t')
|
253 |
+
>>> FiniteExtension(Poly(t**2 - x**3 - x + 1, t, field=True))
|
254 |
+
ZZ(x)[t]/(t**2 - x**3 - x + 1)
|
255 |
+
|
256 |
+
"""
|
257 |
+
is_FiniteExtension = True
|
258 |
+
|
259 |
+
dtype = ExtensionElement
|
260 |
+
|
261 |
+
def __init__(self, mod):
|
262 |
+
if not (isinstance(mod, Poly) and mod.is_univariate):
|
263 |
+
raise TypeError("modulus must be a univariate Poly")
|
264 |
+
|
265 |
+
# Using auto=True (default) potentially changes the ground domain to a
|
266 |
+
# field whereas auto=False raises if division is not exact. We'll let
|
267 |
+
# the caller decide whether or not they want to put the ground domain
|
268 |
+
# over a field. In most uses mod is already monic.
|
269 |
+
mod = mod.monic(auto=False)
|
270 |
+
|
271 |
+
self.rank = mod.degree()
|
272 |
+
self.modulus = mod
|
273 |
+
self.mod = mod.rep # DMP representation
|
274 |
+
|
275 |
+
self.domain = dom = mod.domain
|
276 |
+
self.ring = mod.rep.ring or dom.old_poly_ring(*mod.gens)
|
277 |
+
|
278 |
+
self.zero = self.convert(self.ring.zero)
|
279 |
+
self.one = self.convert(self.ring.one)
|
280 |
+
|
281 |
+
gen = self.ring.gens[0]
|
282 |
+
self.symbol = self.ring.symbols[0]
|
283 |
+
self.generator = self.convert(gen)
|
284 |
+
self.basis = tuple(self.convert(gen**i) for i in range(self.rank))
|
285 |
+
|
286 |
+
# XXX: It might be necessary to check mod.is_irreducible here
|
287 |
+
self.is_Field = self.domain.is_Field
|
288 |
+
|
289 |
+
def new(self, arg):
|
290 |
+
rep = self.ring.convert(arg)
|
291 |
+
return ExtElem(rep % self.mod, self)
|
292 |
+
|
293 |
+
def __eq__(self, other):
|
294 |
+
if not isinstance(other, FiniteExtension):
|
295 |
+
return False
|
296 |
+
return self.modulus == other.modulus
|
297 |
+
|
298 |
+
def __hash__(self):
|
299 |
+
return hash((self.__class__.__name__, self.modulus))
|
300 |
+
|
301 |
+
def __str__(self):
|
302 |
+
return "%s/(%s)" % (self.ring, self.modulus.as_expr())
|
303 |
+
|
304 |
+
__repr__ = __str__
|
305 |
+
|
306 |
+
def convert(self, f, base=None):
|
307 |
+
rep = self.ring.convert(f, base)
|
308 |
+
return ExtElem(rep % self.mod, self)
|
309 |
+
|
310 |
+
def convert_from(self, f, base):
|
311 |
+
rep = self.ring.convert(f, base)
|
312 |
+
return ExtElem(rep % self.mod, self)
|
313 |
+
|
314 |
+
def to_sympy(self, f):
|
315 |
+
return self.ring.to_sympy(f.rep)
|
316 |
+
|
317 |
+
def from_sympy(self, f):
|
318 |
+
return self.convert(f)
|
319 |
+
|
320 |
+
def set_domain(self, K):
|
321 |
+
mod = self.modulus.set_domain(K)
|
322 |
+
return self.__class__(mod)
|
323 |
+
|
324 |
+
def drop(self, *symbols):
|
325 |
+
if self.symbol in symbols:
|
326 |
+
raise GeneratorsError('Can not drop generator from FiniteExtension')
|
327 |
+
K = self.domain.drop(*symbols)
|
328 |
+
return self.set_domain(K)
|
329 |
+
|
330 |
+
def quo(self, f, g):
|
331 |
+
return self.exquo(f, g)
|
332 |
+
|
333 |
+
def exquo(self, f, g):
|
334 |
+
rep = self.ring.exquo(f.rep, g.rep)
|
335 |
+
return ExtElem(rep % self.mod, self)
|
336 |
+
|
337 |
+
def is_negative(self, a):
|
338 |
+
return False
|
339 |
+
|
340 |
+
def is_unit(self, a):
|
341 |
+
if self.is_Field:
|
342 |
+
return bool(a)
|
343 |
+
elif a.is_ground:
|
344 |
+
return self.domain.is_unit(a.to_ground())
|
345 |
+
|
346 |
+
FiniteExtension = MonogenicFiniteExtension
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/homomorphisms.py
ADDED
@@ -0,0 +1,691 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Computations with homomorphisms of modules and rings.
|
3 |
+
|
4 |
+
This module implements classes for representing homomorphisms of rings and
|
5 |
+
their modules. Instead of instantiating the classes directly, you should use
|
6 |
+
the function ``homomorphism(from, to, matrix)`` to create homomorphism objects.
|
7 |
+
"""
|
8 |
+
|
9 |
+
|
10 |
+
from sympy.polys.agca.modules import (Module, FreeModule, QuotientModule,
|
11 |
+
SubModule, SubQuotientModule)
|
12 |
+
from sympy.polys.polyerrors import CoercionFailed
|
13 |
+
|
14 |
+
# The main computational task for module homomorphisms is kernels.
|
15 |
+
# For this reason, the concrete classes are organised by domain module type.
|
16 |
+
|
17 |
+
|
18 |
+
class ModuleHomomorphism:
|
19 |
+
"""
|
20 |
+
Abstract base class for module homomoprhisms. Do not instantiate.
|
21 |
+
|
22 |
+
Instead, use the ``homomorphism`` function:
|
23 |
+
|
24 |
+
>>> from sympy import QQ
|
25 |
+
>>> from sympy.abc import x
|
26 |
+
>>> from sympy.polys.agca import homomorphism
|
27 |
+
|
28 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
29 |
+
>>> homomorphism(F, F, [[1, 0], [0, 1]])
|
30 |
+
Matrix([
|
31 |
+
[1, 0], : QQ[x]**2 -> QQ[x]**2
|
32 |
+
[0, 1]])
|
33 |
+
|
34 |
+
Attributes:
|
35 |
+
|
36 |
+
- ring - the ring over which we are considering modules
|
37 |
+
- domain - the domain module
|
38 |
+
- codomain - the codomain module
|
39 |
+
- _ker - cached kernel
|
40 |
+
- _img - cached image
|
41 |
+
|
42 |
+
Non-implemented methods:
|
43 |
+
|
44 |
+
- _kernel
|
45 |
+
- _image
|
46 |
+
- _restrict_domain
|
47 |
+
- _restrict_codomain
|
48 |
+
- _quotient_domain
|
49 |
+
- _quotient_codomain
|
50 |
+
- _apply
|
51 |
+
- _mul_scalar
|
52 |
+
- _compose
|
53 |
+
- _add
|
54 |
+
"""
|
55 |
+
|
56 |
+
def __init__(self, domain, codomain):
|
57 |
+
if not isinstance(domain, Module):
|
58 |
+
raise TypeError('Source must be a module, got %s' % domain)
|
59 |
+
if not isinstance(codomain, Module):
|
60 |
+
raise TypeError('Target must be a module, got %s' % codomain)
|
61 |
+
if domain.ring != codomain.ring:
|
62 |
+
raise ValueError('Source and codomain must be over same ring, '
|
63 |
+
'got %s != %s' % (domain, codomain))
|
64 |
+
self.domain = domain
|
65 |
+
self.codomain = codomain
|
66 |
+
self.ring = domain.ring
|
67 |
+
self._ker = None
|
68 |
+
self._img = None
|
69 |
+
|
70 |
+
def kernel(self):
|
71 |
+
r"""
|
72 |
+
Compute the kernel of ``self``.
|
73 |
+
|
74 |
+
That is, if ``self`` is the homomorphism `\phi: M \to N`, then compute
|
75 |
+
`ker(\phi) = \{x \in M | \phi(x) = 0\}`. This is a submodule of `M`.
|
76 |
+
|
77 |
+
Examples
|
78 |
+
========
|
79 |
+
|
80 |
+
>>> from sympy import QQ
|
81 |
+
>>> from sympy.abc import x
|
82 |
+
>>> from sympy.polys.agca import homomorphism
|
83 |
+
|
84 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
85 |
+
>>> homomorphism(F, F, [[1, 0], [x, 0]]).kernel()
|
86 |
+
<[x, -1]>
|
87 |
+
"""
|
88 |
+
if self._ker is None:
|
89 |
+
self._ker = self._kernel()
|
90 |
+
return self._ker
|
91 |
+
|
92 |
+
def image(self):
|
93 |
+
r"""
|
94 |
+
Compute the image of ``self``.
|
95 |
+
|
96 |
+
That is, if ``self`` is the homomorphism `\phi: M \to N`, then compute
|
97 |
+
`im(\phi) = \{\phi(x) | x \in M \}`. This is a submodule of `N`.
|
98 |
+
|
99 |
+
Examples
|
100 |
+
========
|
101 |
+
|
102 |
+
>>> from sympy import QQ
|
103 |
+
>>> from sympy.abc import x
|
104 |
+
>>> from sympy.polys.agca import homomorphism
|
105 |
+
|
106 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
107 |
+
>>> homomorphism(F, F, [[1, 0], [x, 0]]).image() == F.submodule([1, 0])
|
108 |
+
True
|
109 |
+
"""
|
110 |
+
if self._img is None:
|
111 |
+
self._img = self._image()
|
112 |
+
return self._img
|
113 |
+
|
114 |
+
def _kernel(self):
|
115 |
+
"""Compute the kernel of ``self``."""
|
116 |
+
raise NotImplementedError
|
117 |
+
|
118 |
+
def _image(self):
|
119 |
+
"""Compute the image of ``self``."""
|
120 |
+
raise NotImplementedError
|
121 |
+
|
122 |
+
def _restrict_domain(self, sm):
|
123 |
+
"""Implementation of domain restriction."""
|
124 |
+
raise NotImplementedError
|
125 |
+
|
126 |
+
def _restrict_codomain(self, sm):
|
127 |
+
"""Implementation of codomain restriction."""
|
128 |
+
raise NotImplementedError
|
129 |
+
|
130 |
+
def _quotient_domain(self, sm):
|
131 |
+
"""Implementation of domain quotient."""
|
132 |
+
raise NotImplementedError
|
133 |
+
|
134 |
+
def _quotient_codomain(self, sm):
|
135 |
+
"""Implementation of codomain quotient."""
|
136 |
+
raise NotImplementedError
|
137 |
+
|
138 |
+
def restrict_domain(self, sm):
|
139 |
+
"""
|
140 |
+
Return ``self``, with the domain restricted to ``sm``.
|
141 |
+
|
142 |
+
Here ``sm`` has to be a submodule of ``self.domain``.
|
143 |
+
|
144 |
+
Examples
|
145 |
+
========
|
146 |
+
|
147 |
+
>>> from sympy import QQ
|
148 |
+
>>> from sympy.abc import x
|
149 |
+
>>> from sympy.polys.agca import homomorphism
|
150 |
+
|
151 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
152 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
153 |
+
>>> h
|
154 |
+
Matrix([
|
155 |
+
[1, x], : QQ[x]**2 -> QQ[x]**2
|
156 |
+
[0, 0]])
|
157 |
+
>>> h.restrict_domain(F.submodule([1, 0]))
|
158 |
+
Matrix([
|
159 |
+
[1, x], : <[1, 0]> -> QQ[x]**2
|
160 |
+
[0, 0]])
|
161 |
+
|
162 |
+
This is the same as just composing on the right with the submodule
|
163 |
+
inclusion:
|
164 |
+
|
165 |
+
>>> h * F.submodule([1, 0]).inclusion_hom()
|
166 |
+
Matrix([
|
167 |
+
[1, x], : <[1, 0]> -> QQ[x]**2
|
168 |
+
[0, 0]])
|
169 |
+
"""
|
170 |
+
if not self.domain.is_submodule(sm):
|
171 |
+
raise ValueError('sm must be a submodule of %s, got %s'
|
172 |
+
% (self.domain, sm))
|
173 |
+
if sm == self.domain:
|
174 |
+
return self
|
175 |
+
return self._restrict_domain(sm)
|
176 |
+
|
177 |
+
def restrict_codomain(self, sm):
|
178 |
+
"""
|
179 |
+
Return ``self``, with codomain restricted to to ``sm``.
|
180 |
+
|
181 |
+
Here ``sm`` has to be a submodule of ``self.codomain`` containing the
|
182 |
+
image.
|
183 |
+
|
184 |
+
Examples
|
185 |
+
========
|
186 |
+
|
187 |
+
>>> from sympy import QQ
|
188 |
+
>>> from sympy.abc import x
|
189 |
+
>>> from sympy.polys.agca import homomorphism
|
190 |
+
|
191 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
192 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
193 |
+
>>> h
|
194 |
+
Matrix([
|
195 |
+
[1, x], : QQ[x]**2 -> QQ[x]**2
|
196 |
+
[0, 0]])
|
197 |
+
>>> h.restrict_codomain(F.submodule([1, 0]))
|
198 |
+
Matrix([
|
199 |
+
[1, x], : QQ[x]**2 -> <[1, 0]>
|
200 |
+
[0, 0]])
|
201 |
+
"""
|
202 |
+
if not sm.is_submodule(self.image()):
|
203 |
+
raise ValueError('the image %s must contain sm, got %s'
|
204 |
+
% (self.image(), sm))
|
205 |
+
if sm == self.codomain:
|
206 |
+
return self
|
207 |
+
return self._restrict_codomain(sm)
|
208 |
+
|
209 |
+
def quotient_domain(self, sm):
|
210 |
+
"""
|
211 |
+
Return ``self`` with domain replaced by ``domain/sm``.
|
212 |
+
|
213 |
+
Here ``sm`` must be a submodule of ``self.kernel()``.
|
214 |
+
|
215 |
+
Examples
|
216 |
+
========
|
217 |
+
|
218 |
+
>>> from sympy import QQ
|
219 |
+
>>> from sympy.abc import x
|
220 |
+
>>> from sympy.polys.agca import homomorphism
|
221 |
+
|
222 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
223 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
224 |
+
>>> h
|
225 |
+
Matrix([
|
226 |
+
[1, x], : QQ[x]**2 -> QQ[x]**2
|
227 |
+
[0, 0]])
|
228 |
+
>>> h.quotient_domain(F.submodule([-x, 1]))
|
229 |
+
Matrix([
|
230 |
+
[1, x], : QQ[x]**2/<[-x, 1]> -> QQ[x]**2
|
231 |
+
[0, 0]])
|
232 |
+
"""
|
233 |
+
if not self.kernel().is_submodule(sm):
|
234 |
+
raise ValueError('kernel %s must contain sm, got %s' %
|
235 |
+
(self.kernel(), sm))
|
236 |
+
if sm.is_zero():
|
237 |
+
return self
|
238 |
+
return self._quotient_domain(sm)
|
239 |
+
|
240 |
+
def quotient_codomain(self, sm):
|
241 |
+
"""
|
242 |
+
Return ``self`` with codomain replaced by ``codomain/sm``.
|
243 |
+
|
244 |
+
Here ``sm`` must be a submodule of ``self.codomain``.
|
245 |
+
|
246 |
+
Examples
|
247 |
+
========
|
248 |
+
|
249 |
+
>>> from sympy import QQ
|
250 |
+
>>> from sympy.abc import x
|
251 |
+
>>> from sympy.polys.agca import homomorphism
|
252 |
+
|
253 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
254 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
255 |
+
>>> h
|
256 |
+
Matrix([
|
257 |
+
[1, x], : QQ[x]**2 -> QQ[x]**2
|
258 |
+
[0, 0]])
|
259 |
+
>>> h.quotient_codomain(F.submodule([1, 1]))
|
260 |
+
Matrix([
|
261 |
+
[1, x], : QQ[x]**2 -> QQ[x]**2/<[1, 1]>
|
262 |
+
[0, 0]])
|
263 |
+
|
264 |
+
This is the same as composing with the quotient map on the left:
|
265 |
+
|
266 |
+
>>> (F/[(1, 1)]).quotient_hom() * h
|
267 |
+
Matrix([
|
268 |
+
[1, x], : QQ[x]**2 -> QQ[x]**2/<[1, 1]>
|
269 |
+
[0, 0]])
|
270 |
+
"""
|
271 |
+
if not self.codomain.is_submodule(sm):
|
272 |
+
raise ValueError('sm must be a submodule of codomain %s, got %s'
|
273 |
+
% (self.codomain, sm))
|
274 |
+
if sm.is_zero():
|
275 |
+
return self
|
276 |
+
return self._quotient_codomain(sm)
|
277 |
+
|
278 |
+
def _apply(self, elem):
|
279 |
+
"""Apply ``self`` to ``elem``."""
|
280 |
+
raise NotImplementedError
|
281 |
+
|
282 |
+
def __call__(self, elem):
|
283 |
+
return self.codomain.convert(self._apply(self.domain.convert(elem)))
|
284 |
+
|
285 |
+
def _compose(self, oth):
|
286 |
+
"""
|
287 |
+
Compose ``self`` with ``oth``, that is, return the homomorphism
|
288 |
+
obtained by first applying then ``self``, then ``oth``.
|
289 |
+
|
290 |
+
(This method is private since in this syntax, it is non-obvious which
|
291 |
+
homomorphism is executed first.)
|
292 |
+
"""
|
293 |
+
raise NotImplementedError
|
294 |
+
|
295 |
+
def _mul_scalar(self, c):
|
296 |
+
"""Scalar multiplication. ``c`` is guaranteed in self.ring."""
|
297 |
+
raise NotImplementedError
|
298 |
+
|
299 |
+
def _add(self, oth):
|
300 |
+
"""
|
301 |
+
Homomorphism addition.
|
302 |
+
``oth`` is guaranteed to be a homomorphism with same domain/codomain.
|
303 |
+
"""
|
304 |
+
raise NotImplementedError
|
305 |
+
|
306 |
+
def _check_hom(self, oth):
|
307 |
+
"""Helper to check that oth is a homomorphism with same domain/codomain."""
|
308 |
+
if not isinstance(oth, ModuleHomomorphism):
|
309 |
+
return False
|
310 |
+
return oth.domain == self.domain and oth.codomain == self.codomain
|
311 |
+
|
312 |
+
def __mul__(self, oth):
|
313 |
+
if isinstance(oth, ModuleHomomorphism) and self.domain == oth.codomain:
|
314 |
+
return oth._compose(self)
|
315 |
+
try:
|
316 |
+
return self._mul_scalar(self.ring.convert(oth))
|
317 |
+
except CoercionFailed:
|
318 |
+
return NotImplemented
|
319 |
+
|
320 |
+
# NOTE: _compose will never be called from rmul
|
321 |
+
__rmul__ = __mul__
|
322 |
+
|
323 |
+
def __truediv__(self, oth):
|
324 |
+
try:
|
325 |
+
return self._mul_scalar(1/self.ring.convert(oth))
|
326 |
+
except CoercionFailed:
|
327 |
+
return NotImplemented
|
328 |
+
|
329 |
+
def __add__(self, oth):
|
330 |
+
if self._check_hom(oth):
|
331 |
+
return self._add(oth)
|
332 |
+
return NotImplemented
|
333 |
+
|
334 |
+
def __sub__(self, oth):
|
335 |
+
if self._check_hom(oth):
|
336 |
+
return self._add(oth._mul_scalar(self.ring.convert(-1)))
|
337 |
+
return NotImplemented
|
338 |
+
|
339 |
+
def is_injective(self):
|
340 |
+
"""
|
341 |
+
Return True if ``self`` is injective.
|
342 |
+
|
343 |
+
That is, check if the elements of the domain are mapped to the same
|
344 |
+
codomain element.
|
345 |
+
|
346 |
+
Examples
|
347 |
+
========
|
348 |
+
|
349 |
+
>>> from sympy import QQ
|
350 |
+
>>> from sympy.abc import x
|
351 |
+
>>> from sympy.polys.agca import homomorphism
|
352 |
+
|
353 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
354 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
355 |
+
>>> h.is_injective()
|
356 |
+
False
|
357 |
+
>>> h.quotient_domain(h.kernel()).is_injective()
|
358 |
+
True
|
359 |
+
"""
|
360 |
+
return self.kernel().is_zero()
|
361 |
+
|
362 |
+
def is_surjective(self):
|
363 |
+
"""
|
364 |
+
Return True if ``self`` is surjective.
|
365 |
+
|
366 |
+
That is, check if every element of the codomain has at least one
|
367 |
+
preimage.
|
368 |
+
|
369 |
+
Examples
|
370 |
+
========
|
371 |
+
|
372 |
+
>>> from sympy import QQ
|
373 |
+
>>> from sympy.abc import x
|
374 |
+
>>> from sympy.polys.agca import homomorphism
|
375 |
+
|
376 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
377 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
378 |
+
>>> h.is_surjective()
|
379 |
+
False
|
380 |
+
>>> h.restrict_codomain(h.image()).is_surjective()
|
381 |
+
True
|
382 |
+
"""
|
383 |
+
return self.image() == self.codomain
|
384 |
+
|
385 |
+
def is_isomorphism(self):
|
386 |
+
"""
|
387 |
+
Return True if ``self`` is an isomorphism.
|
388 |
+
|
389 |
+
That is, check if every element of the codomain has precisely one
|
390 |
+
preimage. Equivalently, ``self`` is both injective and surjective.
|
391 |
+
|
392 |
+
Examples
|
393 |
+
========
|
394 |
+
|
395 |
+
>>> from sympy import QQ
|
396 |
+
>>> from sympy.abc import x
|
397 |
+
>>> from sympy.polys.agca import homomorphism
|
398 |
+
|
399 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
400 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
401 |
+
>>> h = h.restrict_codomain(h.image())
|
402 |
+
>>> h.is_isomorphism()
|
403 |
+
False
|
404 |
+
>>> h.quotient_domain(h.kernel()).is_isomorphism()
|
405 |
+
True
|
406 |
+
"""
|
407 |
+
return self.is_injective() and self.is_surjective()
|
408 |
+
|
409 |
+
def is_zero(self):
|
410 |
+
"""
|
411 |
+
Return True if ``self`` is a zero morphism.
|
412 |
+
|
413 |
+
That is, check if every element of the domain is mapped to zero
|
414 |
+
under self.
|
415 |
+
|
416 |
+
Examples
|
417 |
+
========
|
418 |
+
|
419 |
+
>>> from sympy import QQ
|
420 |
+
>>> from sympy.abc import x
|
421 |
+
>>> from sympy.polys.agca import homomorphism
|
422 |
+
|
423 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
424 |
+
>>> h = homomorphism(F, F, [[1, 0], [x, 0]])
|
425 |
+
>>> h.is_zero()
|
426 |
+
False
|
427 |
+
>>> h.restrict_domain(F.submodule()).is_zero()
|
428 |
+
True
|
429 |
+
>>> h.quotient_codomain(h.image()).is_zero()
|
430 |
+
True
|
431 |
+
"""
|
432 |
+
return self.image().is_zero()
|
433 |
+
|
434 |
+
def __eq__(self, oth):
|
435 |
+
try:
|
436 |
+
return (self - oth).is_zero()
|
437 |
+
except TypeError:
|
438 |
+
return False
|
439 |
+
|
440 |
+
def __ne__(self, oth):
|
441 |
+
return not (self == oth)
|
442 |
+
|
443 |
+
|
444 |
+
class MatrixHomomorphism(ModuleHomomorphism):
|
445 |
+
r"""
|
446 |
+
Helper class for all homomoprhisms which are expressed via a matrix.
|
447 |
+
|
448 |
+
That is, for such homomorphisms ``domain`` is contained in a module
|
449 |
+
generated by finitely many elements `e_1, \ldots, e_n`, so that the
|
450 |
+
homomorphism is determined uniquely by its action on the `e_i`. It
|
451 |
+
can thus be represented as a vector of elements of the codomain module,
|
452 |
+
or potentially a supermodule of the codomain module
|
453 |
+
(and hence conventionally as a matrix, if there is a similar interpretation
|
454 |
+
for elements of the codomain module).
|
455 |
+
|
456 |
+
Note that this class does *not* assume that the `e_i` freely generate a
|
457 |
+
submodule, nor that ``domain`` is even all of this submodule. It exists
|
458 |
+
only to unify the interface.
|
459 |
+
|
460 |
+
Do not instantiate.
|
461 |
+
|
462 |
+
Attributes:
|
463 |
+
|
464 |
+
- matrix - the list of images determining the homomorphism.
|
465 |
+
NOTE: the elements of matrix belong to either self.codomain or
|
466 |
+
self.codomain.container
|
467 |
+
|
468 |
+
Still non-implemented methods:
|
469 |
+
|
470 |
+
- kernel
|
471 |
+
- _apply
|
472 |
+
"""
|
473 |
+
|
474 |
+
def __init__(self, domain, codomain, matrix):
|
475 |
+
ModuleHomomorphism.__init__(self, domain, codomain)
|
476 |
+
if len(matrix) != domain.rank:
|
477 |
+
raise ValueError('Need to provide %s elements, got %s'
|
478 |
+
% (domain.rank, len(matrix)))
|
479 |
+
|
480 |
+
converter = self.codomain.convert
|
481 |
+
if isinstance(self.codomain, (SubModule, SubQuotientModule)):
|
482 |
+
converter = self.codomain.container.convert
|
483 |
+
self.matrix = tuple(converter(x) for x in matrix)
|
484 |
+
|
485 |
+
def _sympy_matrix(self):
|
486 |
+
"""Helper function which returns a SymPy matrix ``self.matrix``."""
|
487 |
+
from sympy.matrices import Matrix
|
488 |
+
c = lambda x: x
|
489 |
+
if isinstance(self.codomain, (QuotientModule, SubQuotientModule)):
|
490 |
+
c = lambda x: x.data
|
491 |
+
return Matrix([[self.ring.to_sympy(y) for y in c(x)] for x in self.matrix]).T
|
492 |
+
|
493 |
+
def __repr__(self):
|
494 |
+
lines = repr(self._sympy_matrix()).split('\n')
|
495 |
+
t = " : %s -> %s" % (self.domain, self.codomain)
|
496 |
+
s = ' '*len(t)
|
497 |
+
n = len(lines)
|
498 |
+
for i in range(n // 2):
|
499 |
+
lines[i] += s
|
500 |
+
lines[n // 2] += t
|
501 |
+
for i in range(n//2 + 1, n):
|
502 |
+
lines[i] += s
|
503 |
+
return '\n'.join(lines)
|
504 |
+
|
505 |
+
def _restrict_domain(self, sm):
|
506 |
+
"""Implementation of domain restriction."""
|
507 |
+
return SubModuleHomomorphism(sm, self.codomain, self.matrix)
|
508 |
+
|
509 |
+
def _restrict_codomain(self, sm):
|
510 |
+
"""Implementation of codomain restriction."""
|
511 |
+
return self.__class__(self.domain, sm, self.matrix)
|
512 |
+
|
513 |
+
def _quotient_domain(self, sm):
|
514 |
+
"""Implementation of domain quotient."""
|
515 |
+
return self.__class__(self.domain/sm, self.codomain, self.matrix)
|
516 |
+
|
517 |
+
def _quotient_codomain(self, sm):
|
518 |
+
"""Implementation of codomain quotient."""
|
519 |
+
Q = self.codomain/sm
|
520 |
+
converter = Q.convert
|
521 |
+
if isinstance(self.codomain, SubModule):
|
522 |
+
converter = Q.container.convert
|
523 |
+
return self.__class__(self.domain, self.codomain/sm,
|
524 |
+
[converter(x) for x in self.matrix])
|
525 |
+
|
526 |
+
def _add(self, oth):
|
527 |
+
return self.__class__(self.domain, self.codomain,
|
528 |
+
[x + y for x, y in zip(self.matrix, oth.matrix)])
|
529 |
+
|
530 |
+
def _mul_scalar(self, c):
|
531 |
+
return self.__class__(self.domain, self.codomain, [c*x for x in self.matrix])
|
532 |
+
|
533 |
+
def _compose(self, oth):
|
534 |
+
return self.__class__(self.domain, oth.codomain, [oth(x) for x in self.matrix])
|
535 |
+
|
536 |
+
|
537 |
+
class FreeModuleHomomorphism(MatrixHomomorphism):
|
538 |
+
"""
|
539 |
+
Concrete class for homomorphisms with domain a free module or a quotient
|
540 |
+
thereof.
|
541 |
+
|
542 |
+
Do not instantiate; the constructor does not check that your data is well
|
543 |
+
defined. Use the ``homomorphism`` function instead:
|
544 |
+
|
545 |
+
>>> from sympy import QQ
|
546 |
+
>>> from sympy.abc import x
|
547 |
+
>>> from sympy.polys.agca import homomorphism
|
548 |
+
|
549 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
550 |
+
>>> homomorphism(F, F, [[1, 0], [0, 1]])
|
551 |
+
Matrix([
|
552 |
+
[1, 0], : QQ[x]**2 -> QQ[x]**2
|
553 |
+
[0, 1]])
|
554 |
+
"""
|
555 |
+
|
556 |
+
def _apply(self, elem):
|
557 |
+
if isinstance(self.domain, QuotientModule):
|
558 |
+
elem = elem.data
|
559 |
+
return sum(x * e for x, e in zip(elem, self.matrix))
|
560 |
+
|
561 |
+
def _image(self):
|
562 |
+
return self.codomain.submodule(*self.matrix)
|
563 |
+
|
564 |
+
def _kernel(self):
|
565 |
+
# The domain is either a free module or a quotient thereof.
|
566 |
+
# It does not matter if it is a quotient, because that won't increase
|
567 |
+
# the kernel.
|
568 |
+
# Our generators {e_i} are sent to the matrix entries {b_i}.
|
569 |
+
# The kernel is essentially the syzygy module of these {b_i}.
|
570 |
+
syz = self.image().syzygy_module()
|
571 |
+
return self.domain.submodule(*syz.gens)
|
572 |
+
|
573 |
+
|
574 |
+
class SubModuleHomomorphism(MatrixHomomorphism):
|
575 |
+
"""
|
576 |
+
Concrete class for homomorphism with domain a submodule of a free module
|
577 |
+
or a quotient thereof.
|
578 |
+
|
579 |
+
Do not instantiate; the constructor does not check that your data is well
|
580 |
+
defined. Use the ``homomorphism`` function instead:
|
581 |
+
|
582 |
+
>>> from sympy import QQ
|
583 |
+
>>> from sympy.abc import x
|
584 |
+
>>> from sympy.polys.agca import homomorphism
|
585 |
+
|
586 |
+
>>> M = QQ.old_poly_ring(x).free_module(2)*x
|
587 |
+
>>> homomorphism(M, M, [[1, 0], [0, 1]])
|
588 |
+
Matrix([
|
589 |
+
[1, 0], : <[x, 0], [0, x]> -> <[x, 0], [0, x]>
|
590 |
+
[0, 1]])
|
591 |
+
"""
|
592 |
+
|
593 |
+
def _apply(self, elem):
|
594 |
+
if isinstance(self.domain, SubQuotientModule):
|
595 |
+
elem = elem.data
|
596 |
+
return sum(x * e for x, e in zip(elem, self.matrix))
|
597 |
+
|
598 |
+
def _image(self):
|
599 |
+
return self.codomain.submodule(*[self(x) for x in self.domain.gens])
|
600 |
+
|
601 |
+
def _kernel(self):
|
602 |
+
syz = self.image().syzygy_module()
|
603 |
+
return self.domain.submodule(
|
604 |
+
*[sum(xi*gi for xi, gi in zip(s, self.domain.gens))
|
605 |
+
for s in syz.gens])
|
606 |
+
|
607 |
+
|
608 |
+
def homomorphism(domain, codomain, matrix):
|
609 |
+
r"""
|
610 |
+
Create a homomorphism object.
|
611 |
+
|
612 |
+
This function tries to build a homomorphism from ``domain`` to ``codomain``
|
613 |
+
via the matrix ``matrix``.
|
614 |
+
|
615 |
+
Examples
|
616 |
+
========
|
617 |
+
|
618 |
+
>>> from sympy import QQ
|
619 |
+
>>> from sympy.abc import x
|
620 |
+
>>> from sympy.polys.agca import homomorphism
|
621 |
+
|
622 |
+
>>> R = QQ.old_poly_ring(x)
|
623 |
+
>>> T = R.free_module(2)
|
624 |
+
|
625 |
+
If ``domain`` is a free module generated by `e_1, \ldots, e_n`, then
|
626 |
+
``matrix`` should be an n-element iterable `(b_1, \ldots, b_n)` where
|
627 |
+
the `b_i` are elements of ``codomain``. The constructed homomorphism is the
|
628 |
+
unique homomorphism sending `e_i` to `b_i`.
|
629 |
+
|
630 |
+
>>> F = R.free_module(2)
|
631 |
+
>>> h = homomorphism(F, T, [[1, x], [x**2, 0]])
|
632 |
+
>>> h
|
633 |
+
Matrix([
|
634 |
+
[1, x**2], : QQ[x]**2 -> QQ[x]**2
|
635 |
+
[x, 0]])
|
636 |
+
>>> h([1, 0])
|
637 |
+
[1, x]
|
638 |
+
>>> h([0, 1])
|
639 |
+
[x**2, 0]
|
640 |
+
>>> h([1, 1])
|
641 |
+
[x**2 + 1, x]
|
642 |
+
|
643 |
+
If ``domain`` is a submodule of a free module, them ``matrix`` determines
|
644 |
+
a homomoprhism from the containing free module to ``codomain``, and the
|
645 |
+
homomorphism returned is obtained by restriction to ``domain``.
|
646 |
+
|
647 |
+
>>> S = F.submodule([1, 0], [0, x])
|
648 |
+
>>> homomorphism(S, T, [[1, x], [x**2, 0]])
|
649 |
+
Matrix([
|
650 |
+
[1, x**2], : <[1, 0], [0, x]> -> QQ[x]**2
|
651 |
+
[x, 0]])
|
652 |
+
|
653 |
+
If ``domain`` is a (sub)quotient `N/K`, then ``matrix`` determines a
|
654 |
+
homomorphism from `N` to ``codomain``. If the kernel contains `K`, this
|
655 |
+
homomorphism descends to ``domain`` and is returned; otherwise an exception
|
656 |
+
is raised.
|
657 |
+
|
658 |
+
>>> homomorphism(S/[(1, 0)], T, [0, [x**2, 0]])
|
659 |
+
Matrix([
|
660 |
+
[0, x**2], : <[1, 0] + <[1, 0]>, [0, x] + <[1, 0]>, [1, 0] + <[1, 0]>> -> QQ[x]**2
|
661 |
+
[0, 0]])
|
662 |
+
>>> homomorphism(S/[(0, x)], T, [0, [x**2, 0]])
|
663 |
+
Traceback (most recent call last):
|
664 |
+
...
|
665 |
+
ValueError: kernel <[1, 0], [0, 0]> must contain sm, got <[0,x]>
|
666 |
+
|
667 |
+
"""
|
668 |
+
def freepres(module):
|
669 |
+
"""
|
670 |
+
Return a tuple ``(F, S, Q, c)`` where ``F`` is a free module, ``S`` is a
|
671 |
+
submodule of ``F``, and ``Q`` a submodule of ``S``, such that
|
672 |
+
``module = S/Q``, and ``c`` is a conversion function.
|
673 |
+
"""
|
674 |
+
if isinstance(module, FreeModule):
|
675 |
+
return module, module, module.submodule(), lambda x: module.convert(x)
|
676 |
+
if isinstance(module, QuotientModule):
|
677 |
+
return (module.base, module.base, module.killed_module,
|
678 |
+
lambda x: module.convert(x).data)
|
679 |
+
if isinstance(module, SubQuotientModule):
|
680 |
+
return (module.base.container, module.base, module.killed_module,
|
681 |
+
lambda x: module.container.convert(x).data)
|
682 |
+
# an ordinary submodule
|
683 |
+
return (module.container, module, module.submodule(),
|
684 |
+
lambda x: module.container.convert(x))
|
685 |
+
|
686 |
+
SF, SS, SQ, _ = freepres(domain)
|
687 |
+
TF, TS, TQ, c = freepres(codomain)
|
688 |
+
# NOTE this is probably a bit inefficient (redundant checks)
|
689 |
+
return FreeModuleHomomorphism(SF, TF, [c(x) for x in matrix]
|
690 |
+
).restrict_domain(SS).restrict_codomain(TS
|
691 |
+
).quotient_codomain(TQ).quotient_domain(SQ)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/ideals.py
ADDED
@@ -0,0 +1,394 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Computations with ideals of polynomial rings."""
|
2 |
+
|
3 |
+
from sympy.polys.polyerrors import CoercionFailed
|
4 |
+
from sympy.polys.polyutils import IntegerPowerable
|
5 |
+
|
6 |
+
|
7 |
+
class Ideal(IntegerPowerable):
|
8 |
+
"""
|
9 |
+
Abstract base class for ideals.
|
10 |
+
|
11 |
+
Do not instantiate - use explicit constructors in the ring class instead:
|
12 |
+
|
13 |
+
>>> from sympy import QQ
|
14 |
+
>>> from sympy.abc import x
|
15 |
+
>>> QQ.old_poly_ring(x).ideal(x+1)
|
16 |
+
<x + 1>
|
17 |
+
|
18 |
+
Attributes
|
19 |
+
|
20 |
+
- ring - the ring this ideal belongs to
|
21 |
+
|
22 |
+
Non-implemented methods:
|
23 |
+
|
24 |
+
- _contains_elem
|
25 |
+
- _contains_ideal
|
26 |
+
- _quotient
|
27 |
+
- _intersect
|
28 |
+
- _union
|
29 |
+
- _product
|
30 |
+
- is_whole_ring
|
31 |
+
- is_zero
|
32 |
+
- is_prime, is_maximal, is_primary, is_radical
|
33 |
+
- is_principal
|
34 |
+
- height, depth
|
35 |
+
- radical
|
36 |
+
|
37 |
+
Methods that likely should be overridden in subclasses:
|
38 |
+
|
39 |
+
- reduce_element
|
40 |
+
"""
|
41 |
+
|
42 |
+
def _contains_elem(self, x):
|
43 |
+
"""Implementation of element containment."""
|
44 |
+
raise NotImplementedError
|
45 |
+
|
46 |
+
def _contains_ideal(self, I):
|
47 |
+
"""Implementation of ideal containment."""
|
48 |
+
raise NotImplementedError
|
49 |
+
|
50 |
+
def _quotient(self, J):
|
51 |
+
"""Implementation of ideal quotient."""
|
52 |
+
raise NotImplementedError
|
53 |
+
|
54 |
+
def _intersect(self, J):
|
55 |
+
"""Implementation of ideal intersection."""
|
56 |
+
raise NotImplementedError
|
57 |
+
|
58 |
+
def is_whole_ring(self):
|
59 |
+
"""Return True if ``self`` is the whole ring."""
|
60 |
+
raise NotImplementedError
|
61 |
+
|
62 |
+
def is_zero(self):
|
63 |
+
"""Return True if ``self`` is the zero ideal."""
|
64 |
+
raise NotImplementedError
|
65 |
+
|
66 |
+
def _equals(self, J):
|
67 |
+
"""Implementation of ideal equality."""
|
68 |
+
return self._contains_ideal(J) and J._contains_ideal(self)
|
69 |
+
|
70 |
+
def is_prime(self):
|
71 |
+
"""Return True if ``self`` is a prime ideal."""
|
72 |
+
raise NotImplementedError
|
73 |
+
|
74 |
+
def is_maximal(self):
|
75 |
+
"""Return True if ``self`` is a maximal ideal."""
|
76 |
+
raise NotImplementedError
|
77 |
+
|
78 |
+
def is_radical(self):
|
79 |
+
"""Return True if ``self`` is a radical ideal."""
|
80 |
+
raise NotImplementedError
|
81 |
+
|
82 |
+
def is_primary(self):
|
83 |
+
"""Return True if ``self`` is a primary ideal."""
|
84 |
+
raise NotImplementedError
|
85 |
+
|
86 |
+
def is_principal(self):
|
87 |
+
"""Return True if ``self`` is a principal ideal."""
|
88 |
+
raise NotImplementedError
|
89 |
+
|
90 |
+
def radical(self):
|
91 |
+
"""Compute the radical of ``self``."""
|
92 |
+
raise NotImplementedError
|
93 |
+
|
94 |
+
def depth(self):
|
95 |
+
"""Compute the depth of ``self``."""
|
96 |
+
raise NotImplementedError
|
97 |
+
|
98 |
+
def height(self):
|
99 |
+
"""Compute the height of ``self``."""
|
100 |
+
raise NotImplementedError
|
101 |
+
|
102 |
+
# TODO more
|
103 |
+
|
104 |
+
# non-implemented methods end here
|
105 |
+
|
106 |
+
def __init__(self, ring):
|
107 |
+
self.ring = ring
|
108 |
+
|
109 |
+
def _check_ideal(self, J):
|
110 |
+
"""Helper to check ``J`` is an ideal of our ring."""
|
111 |
+
if not isinstance(J, Ideal) or J.ring != self.ring:
|
112 |
+
raise ValueError(
|
113 |
+
'J must be an ideal of %s, got %s' % (self.ring, J))
|
114 |
+
|
115 |
+
def contains(self, elem):
|
116 |
+
"""
|
117 |
+
Return True if ``elem`` is an element of this ideal.
|
118 |
+
|
119 |
+
Examples
|
120 |
+
========
|
121 |
+
|
122 |
+
>>> from sympy.abc import x
|
123 |
+
>>> from sympy import QQ
|
124 |
+
>>> QQ.old_poly_ring(x).ideal(x+1, x-1).contains(3)
|
125 |
+
True
|
126 |
+
>>> QQ.old_poly_ring(x).ideal(x**2, x**3).contains(x)
|
127 |
+
False
|
128 |
+
"""
|
129 |
+
return self._contains_elem(self.ring.convert(elem))
|
130 |
+
|
131 |
+
def subset(self, other):
|
132 |
+
"""
|
133 |
+
Returns True if ``other`` is is a subset of ``self``.
|
134 |
+
|
135 |
+
Here ``other`` may be an ideal.
|
136 |
+
|
137 |
+
Examples
|
138 |
+
========
|
139 |
+
|
140 |
+
>>> from sympy.abc import x
|
141 |
+
>>> from sympy import QQ
|
142 |
+
>>> I = QQ.old_poly_ring(x).ideal(x+1)
|
143 |
+
>>> I.subset([x**2 - 1, x**2 + 2*x + 1])
|
144 |
+
True
|
145 |
+
>>> I.subset([x**2 + 1, x + 1])
|
146 |
+
False
|
147 |
+
>>> I.subset(QQ.old_poly_ring(x).ideal(x**2 - 1))
|
148 |
+
True
|
149 |
+
"""
|
150 |
+
if isinstance(other, Ideal):
|
151 |
+
return self._contains_ideal(other)
|
152 |
+
return all(self._contains_elem(x) for x in other)
|
153 |
+
|
154 |
+
def quotient(self, J, **opts):
|
155 |
+
r"""
|
156 |
+
Compute the ideal quotient of ``self`` by ``J``.
|
157 |
+
|
158 |
+
That is, if ``self`` is the ideal `I`, compute the set
|
159 |
+
`I : J = \{x \in R | xJ \subset I \}`.
|
160 |
+
|
161 |
+
Examples
|
162 |
+
========
|
163 |
+
|
164 |
+
>>> from sympy.abc import x, y
|
165 |
+
>>> from sympy import QQ
|
166 |
+
>>> R = QQ.old_poly_ring(x, y)
|
167 |
+
>>> R.ideal(x*y).quotient(R.ideal(x))
|
168 |
+
<y>
|
169 |
+
"""
|
170 |
+
self._check_ideal(J)
|
171 |
+
return self._quotient(J, **opts)
|
172 |
+
|
173 |
+
def intersect(self, J):
|
174 |
+
"""
|
175 |
+
Compute the intersection of self with ideal J.
|
176 |
+
|
177 |
+
Examples
|
178 |
+
========
|
179 |
+
|
180 |
+
>>> from sympy.abc import x, y
|
181 |
+
>>> from sympy import QQ
|
182 |
+
>>> R = QQ.old_poly_ring(x, y)
|
183 |
+
>>> R.ideal(x).intersect(R.ideal(y))
|
184 |
+
<x*y>
|
185 |
+
"""
|
186 |
+
self._check_ideal(J)
|
187 |
+
return self._intersect(J)
|
188 |
+
|
189 |
+
def saturate(self, J):
|
190 |
+
r"""
|
191 |
+
Compute the ideal saturation of ``self`` by ``J``.
|
192 |
+
|
193 |
+
That is, if ``self`` is the ideal `I`, compute the set
|
194 |
+
`I : J^\infty = \{x \in R | xJ^n \subset I \text{ for some } n\}`.
|
195 |
+
"""
|
196 |
+
raise NotImplementedError
|
197 |
+
# Note this can be implemented using repeated quotient
|
198 |
+
|
199 |
+
def union(self, J):
|
200 |
+
"""
|
201 |
+
Compute the ideal generated by the union of ``self`` and ``J``.
|
202 |
+
|
203 |
+
Examples
|
204 |
+
========
|
205 |
+
|
206 |
+
>>> from sympy.abc import x
|
207 |
+
>>> from sympy import QQ
|
208 |
+
>>> QQ.old_poly_ring(x).ideal(x**2 - 1).union(QQ.old_poly_ring(x).ideal((x+1)**2)) == QQ.old_poly_ring(x).ideal(x+1)
|
209 |
+
True
|
210 |
+
"""
|
211 |
+
self._check_ideal(J)
|
212 |
+
return self._union(J)
|
213 |
+
|
214 |
+
def product(self, J):
|
215 |
+
r"""
|
216 |
+
Compute the ideal product of ``self`` and ``J``.
|
217 |
+
|
218 |
+
That is, compute the ideal generated by products `xy`, for `x` an element
|
219 |
+
of ``self`` and `y \in J`.
|
220 |
+
|
221 |
+
Examples
|
222 |
+
========
|
223 |
+
|
224 |
+
>>> from sympy.abc import x, y
|
225 |
+
>>> from sympy import QQ
|
226 |
+
>>> QQ.old_poly_ring(x, y).ideal(x).product(QQ.old_poly_ring(x, y).ideal(y))
|
227 |
+
<x*y>
|
228 |
+
"""
|
229 |
+
self._check_ideal(J)
|
230 |
+
return self._product(J)
|
231 |
+
|
232 |
+
def reduce_element(self, x):
|
233 |
+
"""
|
234 |
+
Reduce the element ``x`` of our ring modulo the ideal ``self``.
|
235 |
+
|
236 |
+
Here "reduce" has no specific meaning: it could return a unique normal
|
237 |
+
form, simplify the expression a bit, or just do nothing.
|
238 |
+
"""
|
239 |
+
return x
|
240 |
+
|
241 |
+
def __add__(self, e):
|
242 |
+
if not isinstance(e, Ideal):
|
243 |
+
R = self.ring.quotient_ring(self)
|
244 |
+
if isinstance(e, R.dtype):
|
245 |
+
return e
|
246 |
+
if isinstance(e, R.ring.dtype):
|
247 |
+
return R(e)
|
248 |
+
return R.convert(e)
|
249 |
+
self._check_ideal(e)
|
250 |
+
return self.union(e)
|
251 |
+
|
252 |
+
__radd__ = __add__
|
253 |
+
|
254 |
+
def __mul__(self, e):
|
255 |
+
if not isinstance(e, Ideal):
|
256 |
+
try:
|
257 |
+
e = self.ring.ideal(e)
|
258 |
+
except CoercionFailed:
|
259 |
+
return NotImplemented
|
260 |
+
self._check_ideal(e)
|
261 |
+
return self.product(e)
|
262 |
+
|
263 |
+
__rmul__ = __mul__
|
264 |
+
|
265 |
+
def _zeroth_power(self):
|
266 |
+
return self.ring.ideal(1)
|
267 |
+
|
268 |
+
def _first_power(self):
|
269 |
+
# Raising to any power but 1 returns a new instance. So we mult by 1
|
270 |
+
# here so that the first power is no exception.
|
271 |
+
return self * 1
|
272 |
+
|
273 |
+
def __eq__(self, e):
|
274 |
+
if not isinstance(e, Ideal) or e.ring != self.ring:
|
275 |
+
return False
|
276 |
+
return self._equals(e)
|
277 |
+
|
278 |
+
def __ne__(self, e):
|
279 |
+
return not (self == e)
|
280 |
+
|
281 |
+
|
282 |
+
class ModuleImplementedIdeal(Ideal):
|
283 |
+
"""
|
284 |
+
Ideal implementation relying on the modules code.
|
285 |
+
|
286 |
+
Attributes:
|
287 |
+
|
288 |
+
- _module - the underlying module
|
289 |
+
"""
|
290 |
+
|
291 |
+
def __init__(self, ring, module):
|
292 |
+
Ideal.__init__(self, ring)
|
293 |
+
self._module = module
|
294 |
+
|
295 |
+
def _contains_elem(self, x):
|
296 |
+
return self._module.contains([x])
|
297 |
+
|
298 |
+
def _contains_ideal(self, J):
|
299 |
+
if not isinstance(J, ModuleImplementedIdeal):
|
300 |
+
raise NotImplementedError
|
301 |
+
return self._module.is_submodule(J._module)
|
302 |
+
|
303 |
+
def _intersect(self, J):
|
304 |
+
if not isinstance(J, ModuleImplementedIdeal):
|
305 |
+
raise NotImplementedError
|
306 |
+
return self.__class__(self.ring, self._module.intersect(J._module))
|
307 |
+
|
308 |
+
def _quotient(self, J, **opts):
|
309 |
+
if not isinstance(J, ModuleImplementedIdeal):
|
310 |
+
raise NotImplementedError
|
311 |
+
return self._module.module_quotient(J._module, **opts)
|
312 |
+
|
313 |
+
def _union(self, J):
|
314 |
+
if not isinstance(J, ModuleImplementedIdeal):
|
315 |
+
raise NotImplementedError
|
316 |
+
return self.__class__(self.ring, self._module.union(J._module))
|
317 |
+
|
318 |
+
@property
|
319 |
+
def gens(self):
|
320 |
+
"""
|
321 |
+
Return generators for ``self``.
|
322 |
+
|
323 |
+
Examples
|
324 |
+
========
|
325 |
+
|
326 |
+
>>> from sympy import QQ
|
327 |
+
>>> from sympy.abc import x, y
|
328 |
+
>>> list(QQ.old_poly_ring(x, y).ideal(x, y, x**2 + y).gens)
|
329 |
+
[x, y, x**2 + y]
|
330 |
+
"""
|
331 |
+
return (x[0] for x in self._module.gens)
|
332 |
+
|
333 |
+
def is_zero(self):
|
334 |
+
"""
|
335 |
+
Return True if ``self`` is the zero ideal.
|
336 |
+
|
337 |
+
Examples
|
338 |
+
========
|
339 |
+
|
340 |
+
>>> from sympy.abc import x
|
341 |
+
>>> from sympy import QQ
|
342 |
+
>>> QQ.old_poly_ring(x).ideal(x).is_zero()
|
343 |
+
False
|
344 |
+
>>> QQ.old_poly_ring(x).ideal().is_zero()
|
345 |
+
True
|
346 |
+
"""
|
347 |
+
return self._module.is_zero()
|
348 |
+
|
349 |
+
def is_whole_ring(self):
|
350 |
+
"""
|
351 |
+
Return True if ``self`` is the whole ring, i.e. one generator is a unit.
|
352 |
+
|
353 |
+
Examples
|
354 |
+
========
|
355 |
+
|
356 |
+
>>> from sympy.abc import x
|
357 |
+
>>> from sympy import QQ, ilex
|
358 |
+
>>> QQ.old_poly_ring(x).ideal(x).is_whole_ring()
|
359 |
+
False
|
360 |
+
>>> QQ.old_poly_ring(x).ideal(3).is_whole_ring()
|
361 |
+
True
|
362 |
+
>>> QQ.old_poly_ring(x, order=ilex).ideal(2 + x).is_whole_ring()
|
363 |
+
True
|
364 |
+
"""
|
365 |
+
return self._module.is_full_module()
|
366 |
+
|
367 |
+
def __repr__(self):
|
368 |
+
from sympy.printing.str import sstr
|
369 |
+
return '<' + ','.join(sstr(x) for [x] in self._module.gens) + '>'
|
370 |
+
|
371 |
+
# NOTE this is the only method using the fact that the module is a SubModule
|
372 |
+
def _product(self, J):
|
373 |
+
if not isinstance(J, ModuleImplementedIdeal):
|
374 |
+
raise NotImplementedError
|
375 |
+
return self.__class__(self.ring, self._module.submodule(
|
376 |
+
*[[x*y] for [x] in self._module.gens for [y] in J._module.gens]))
|
377 |
+
|
378 |
+
def in_terms_of_generators(self, e):
|
379 |
+
"""
|
380 |
+
Express ``e`` in terms of the generators of ``self``.
|
381 |
+
|
382 |
+
Examples
|
383 |
+
========
|
384 |
+
|
385 |
+
>>> from sympy.abc import x
|
386 |
+
>>> from sympy import QQ
|
387 |
+
>>> I = QQ.old_poly_ring(x).ideal(x**2 + 1, x)
|
388 |
+
>>> I.in_terms_of_generators(1)
|
389 |
+
[1, -x]
|
390 |
+
"""
|
391 |
+
return self._module.in_terms_of_generators([e])
|
392 |
+
|
393 |
+
def reduce_element(self, x, **options):
|
394 |
+
return self._module.reduce_element([x], **options)[0]
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/modules.py
ADDED
@@ -0,0 +1,1484 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
3 |
+
|
4 |
+
This module implements various classes that encapsulate groebner basis
|
5 |
+
computations for modules. Most of them should not be instantiated by hand.
|
6 |
+
Instead, use the constructing routines on objects you already have.
|
7 |
+
|
8 |
+
For example, to construct a free module over ``QQ[x, y]``, call
|
9 |
+
``QQ[x, y].free_module(rank)`` instead of the ``FreeModule`` constructor.
|
10 |
+
In fact ``FreeModule`` is an abstract base class that should not be
|
11 |
+
instantiated, the ``free_module`` method instead returns the implementing class
|
12 |
+
``FreeModulePolyRing``.
|
13 |
+
|
14 |
+
In general, the abstract base classes implement most functionality in terms of
|
15 |
+
a few non-implemented methods. The concrete base classes supply only these
|
16 |
+
non-implemented methods. They may also supply new implementations of the
|
17 |
+
convenience methods, for example if there are faster algorithms available.
|
18 |
+
"""
|
19 |
+
|
20 |
+
|
21 |
+
from copy import copy
|
22 |
+
from functools import reduce
|
23 |
+
|
24 |
+
from sympy.polys.agca.ideals import Ideal
|
25 |
+
from sympy.polys.domains.field import Field
|
26 |
+
from sympy.polys.orderings import ProductOrder, monomial_key
|
27 |
+
from sympy.polys.polyerrors import CoercionFailed
|
28 |
+
from sympy.core.basic import _aresame
|
29 |
+
from sympy.utilities.iterables import iterable
|
30 |
+
|
31 |
+
# TODO
|
32 |
+
# - module saturation
|
33 |
+
# - module quotient/intersection for quotient rings
|
34 |
+
# - free resoltutions / syzygies
|
35 |
+
# - finding small/minimal generating sets
|
36 |
+
# - ...
|
37 |
+
|
38 |
+
##########################################################################
|
39 |
+
## Abstract base classes #################################################
|
40 |
+
##########################################################################
|
41 |
+
|
42 |
+
|
43 |
+
class Module:
|
44 |
+
"""
|
45 |
+
Abstract base class for modules.
|
46 |
+
|
47 |
+
Do not instantiate - use ring explicit constructors instead:
|
48 |
+
|
49 |
+
>>> from sympy import QQ
|
50 |
+
>>> from sympy.abc import x
|
51 |
+
>>> QQ.old_poly_ring(x).free_module(2)
|
52 |
+
QQ[x]**2
|
53 |
+
|
54 |
+
Attributes:
|
55 |
+
|
56 |
+
- dtype - type of elements
|
57 |
+
- ring - containing ring
|
58 |
+
|
59 |
+
Non-implemented methods:
|
60 |
+
|
61 |
+
- submodule
|
62 |
+
- quotient_module
|
63 |
+
- is_zero
|
64 |
+
- is_submodule
|
65 |
+
- multiply_ideal
|
66 |
+
|
67 |
+
The method convert likely needs to be changed in subclasses.
|
68 |
+
"""
|
69 |
+
|
70 |
+
def __init__(self, ring):
|
71 |
+
self.ring = ring
|
72 |
+
|
73 |
+
def convert(self, elem, M=None):
|
74 |
+
"""
|
75 |
+
Convert ``elem`` into internal representation of this module.
|
76 |
+
|
77 |
+
If ``M`` is not None, it should be a module containing it.
|
78 |
+
"""
|
79 |
+
if not isinstance(elem, self.dtype):
|
80 |
+
raise CoercionFailed
|
81 |
+
return elem
|
82 |
+
|
83 |
+
def submodule(self, *gens):
|
84 |
+
"""Generate a submodule."""
|
85 |
+
raise NotImplementedError
|
86 |
+
|
87 |
+
def quotient_module(self, other):
|
88 |
+
"""Generate a quotient module."""
|
89 |
+
raise NotImplementedError
|
90 |
+
|
91 |
+
def __truediv__(self, e):
|
92 |
+
if not isinstance(e, Module):
|
93 |
+
e = self.submodule(*e)
|
94 |
+
return self.quotient_module(e)
|
95 |
+
|
96 |
+
def contains(self, elem):
|
97 |
+
"""Return True if ``elem`` is an element of this module."""
|
98 |
+
try:
|
99 |
+
self.convert(elem)
|
100 |
+
return True
|
101 |
+
except CoercionFailed:
|
102 |
+
return False
|
103 |
+
|
104 |
+
def __contains__(self, elem):
|
105 |
+
return self.contains(elem)
|
106 |
+
|
107 |
+
def subset(self, other):
|
108 |
+
"""
|
109 |
+
Returns True if ``other`` is is a subset of ``self``.
|
110 |
+
|
111 |
+
Examples
|
112 |
+
========
|
113 |
+
|
114 |
+
>>> from sympy.abc import x
|
115 |
+
>>> from sympy import QQ
|
116 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
117 |
+
>>> F.subset([(1, x), (x, 2)])
|
118 |
+
True
|
119 |
+
>>> F.subset([(1/x, x), (x, 2)])
|
120 |
+
False
|
121 |
+
"""
|
122 |
+
return all(self.contains(x) for x in other)
|
123 |
+
|
124 |
+
def __eq__(self, other):
|
125 |
+
return self.is_submodule(other) and other.is_submodule(self)
|
126 |
+
|
127 |
+
def __ne__(self, other):
|
128 |
+
return not (self == other)
|
129 |
+
|
130 |
+
def is_zero(self):
|
131 |
+
"""Returns True if ``self`` is a zero module."""
|
132 |
+
raise NotImplementedError
|
133 |
+
|
134 |
+
def is_submodule(self, other):
|
135 |
+
"""Returns True if ``other`` is a submodule of ``self``."""
|
136 |
+
raise NotImplementedError
|
137 |
+
|
138 |
+
def multiply_ideal(self, other):
|
139 |
+
"""
|
140 |
+
Multiply ``self`` by the ideal ``other``.
|
141 |
+
"""
|
142 |
+
raise NotImplementedError
|
143 |
+
|
144 |
+
def __mul__(self, e):
|
145 |
+
if not isinstance(e, Ideal):
|
146 |
+
try:
|
147 |
+
e = self.ring.ideal(e)
|
148 |
+
except (CoercionFailed, NotImplementedError):
|
149 |
+
return NotImplemented
|
150 |
+
return self.multiply_ideal(e)
|
151 |
+
|
152 |
+
__rmul__ = __mul__
|
153 |
+
|
154 |
+
def identity_hom(self):
|
155 |
+
"""Return the identity homomorphism on ``self``."""
|
156 |
+
raise NotImplementedError
|
157 |
+
|
158 |
+
|
159 |
+
class ModuleElement:
|
160 |
+
"""
|
161 |
+
Base class for module element wrappers.
|
162 |
+
|
163 |
+
Use this class to wrap primitive data types as module elements. It stores
|
164 |
+
a reference to the containing module, and implements all the arithmetic
|
165 |
+
operators.
|
166 |
+
|
167 |
+
Attributes:
|
168 |
+
|
169 |
+
- module - containing module
|
170 |
+
- data - internal data
|
171 |
+
|
172 |
+
Methods that likely need change in subclasses:
|
173 |
+
|
174 |
+
- add
|
175 |
+
- mul
|
176 |
+
- div
|
177 |
+
- eq
|
178 |
+
"""
|
179 |
+
|
180 |
+
def __init__(self, module, data):
|
181 |
+
self.module = module
|
182 |
+
self.data = data
|
183 |
+
|
184 |
+
def add(self, d1, d2):
|
185 |
+
"""Add data ``d1`` and ``d2``."""
|
186 |
+
return d1 + d2
|
187 |
+
|
188 |
+
def mul(self, m, d):
|
189 |
+
"""Multiply module data ``m`` by coefficient d."""
|
190 |
+
return m * d
|
191 |
+
|
192 |
+
def div(self, m, d):
|
193 |
+
"""Divide module data ``m`` by coefficient d."""
|
194 |
+
return m / d
|
195 |
+
|
196 |
+
def eq(self, d1, d2):
|
197 |
+
"""Return true if d1 and d2 represent the same element."""
|
198 |
+
return d1 == d2
|
199 |
+
|
200 |
+
def __add__(self, om):
|
201 |
+
if not isinstance(om, self.__class__) or om.module != self.module:
|
202 |
+
try:
|
203 |
+
om = self.module.convert(om)
|
204 |
+
except CoercionFailed:
|
205 |
+
return NotImplemented
|
206 |
+
return self.__class__(self.module, self.add(self.data, om.data))
|
207 |
+
|
208 |
+
__radd__ = __add__
|
209 |
+
|
210 |
+
def __neg__(self):
|
211 |
+
return self.__class__(self.module, self.mul(self.data,
|
212 |
+
self.module.ring.convert(-1)))
|
213 |
+
|
214 |
+
def __sub__(self, om):
|
215 |
+
if not isinstance(om, self.__class__) or om.module != self.module:
|
216 |
+
try:
|
217 |
+
om = self.module.convert(om)
|
218 |
+
except CoercionFailed:
|
219 |
+
return NotImplemented
|
220 |
+
return self.__add__(-om)
|
221 |
+
|
222 |
+
def __rsub__(self, om):
|
223 |
+
return (-self).__add__(om)
|
224 |
+
|
225 |
+
def __mul__(self, o):
|
226 |
+
if not isinstance(o, self.module.ring.dtype):
|
227 |
+
try:
|
228 |
+
o = self.module.ring.convert(o)
|
229 |
+
except CoercionFailed:
|
230 |
+
return NotImplemented
|
231 |
+
return self.__class__(self.module, self.mul(self.data, o))
|
232 |
+
|
233 |
+
__rmul__ = __mul__
|
234 |
+
|
235 |
+
def __truediv__(self, o):
|
236 |
+
if not isinstance(o, self.module.ring.dtype):
|
237 |
+
try:
|
238 |
+
o = self.module.ring.convert(o)
|
239 |
+
except CoercionFailed:
|
240 |
+
return NotImplemented
|
241 |
+
return self.__class__(self.module, self.div(self.data, o))
|
242 |
+
|
243 |
+
def __eq__(self, om):
|
244 |
+
if not isinstance(om, self.__class__) or om.module != self.module:
|
245 |
+
try:
|
246 |
+
om = self.module.convert(om)
|
247 |
+
except CoercionFailed:
|
248 |
+
return False
|
249 |
+
return self.eq(self.data, om.data)
|
250 |
+
|
251 |
+
def __ne__(self, om):
|
252 |
+
return not self == om
|
253 |
+
|
254 |
+
##########################################################################
|
255 |
+
## Free Modules ##########################################################
|
256 |
+
##########################################################################
|
257 |
+
|
258 |
+
|
259 |
+
class FreeModuleElement(ModuleElement):
|
260 |
+
"""Element of a free module. Data stored as a tuple."""
|
261 |
+
|
262 |
+
def add(self, d1, d2):
|
263 |
+
return tuple(x + y for x, y in zip(d1, d2))
|
264 |
+
|
265 |
+
def mul(self, d, p):
|
266 |
+
return tuple(x * p for x in d)
|
267 |
+
|
268 |
+
def div(self, d, p):
|
269 |
+
return tuple(x / p for x in d)
|
270 |
+
|
271 |
+
def __repr__(self):
|
272 |
+
from sympy.printing.str import sstr
|
273 |
+
return '[' + ', '.join(sstr(x) for x in self.data) + ']'
|
274 |
+
|
275 |
+
def __iter__(self):
|
276 |
+
return self.data.__iter__()
|
277 |
+
|
278 |
+
def __getitem__(self, idx):
|
279 |
+
return self.data[idx]
|
280 |
+
|
281 |
+
|
282 |
+
class FreeModule(Module):
|
283 |
+
"""
|
284 |
+
Abstract base class for free modules.
|
285 |
+
|
286 |
+
Additional attributes:
|
287 |
+
|
288 |
+
- rank - rank of the free module
|
289 |
+
|
290 |
+
Non-implemented methods:
|
291 |
+
|
292 |
+
- submodule
|
293 |
+
"""
|
294 |
+
|
295 |
+
dtype = FreeModuleElement
|
296 |
+
|
297 |
+
def __init__(self, ring, rank):
|
298 |
+
Module.__init__(self, ring)
|
299 |
+
self.rank = rank
|
300 |
+
|
301 |
+
def __repr__(self):
|
302 |
+
return repr(self.ring) + "**" + repr(self.rank)
|
303 |
+
|
304 |
+
def is_submodule(self, other):
|
305 |
+
"""
|
306 |
+
Returns True if ``other`` is a submodule of ``self``.
|
307 |
+
|
308 |
+
Examples
|
309 |
+
========
|
310 |
+
|
311 |
+
>>> from sympy.abc import x
|
312 |
+
>>> from sympy import QQ
|
313 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
314 |
+
>>> M = F.submodule([2, x])
|
315 |
+
>>> F.is_submodule(F)
|
316 |
+
True
|
317 |
+
>>> F.is_submodule(M)
|
318 |
+
True
|
319 |
+
>>> M.is_submodule(F)
|
320 |
+
False
|
321 |
+
"""
|
322 |
+
if isinstance(other, SubModule):
|
323 |
+
return other.container == self
|
324 |
+
if isinstance(other, FreeModule):
|
325 |
+
return other.ring == self.ring and other.rank == self.rank
|
326 |
+
return False
|
327 |
+
|
328 |
+
def convert(self, elem, M=None):
|
329 |
+
"""
|
330 |
+
Convert ``elem`` into the internal representation.
|
331 |
+
|
332 |
+
This method is called implicitly whenever computations involve elements
|
333 |
+
not in the internal representation.
|
334 |
+
|
335 |
+
Examples
|
336 |
+
========
|
337 |
+
|
338 |
+
>>> from sympy.abc import x
|
339 |
+
>>> from sympy import QQ
|
340 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
341 |
+
>>> F.convert([1, 0])
|
342 |
+
[1, 0]
|
343 |
+
"""
|
344 |
+
if isinstance(elem, FreeModuleElement):
|
345 |
+
if elem.module is self:
|
346 |
+
return elem
|
347 |
+
if elem.module.rank != self.rank:
|
348 |
+
raise CoercionFailed
|
349 |
+
return FreeModuleElement(self,
|
350 |
+
tuple(self.ring.convert(x, elem.module.ring) for x in elem.data))
|
351 |
+
elif iterable(elem):
|
352 |
+
tpl = tuple(self.ring.convert(x) for x in elem)
|
353 |
+
if len(tpl) != self.rank:
|
354 |
+
raise CoercionFailed
|
355 |
+
return FreeModuleElement(self, tpl)
|
356 |
+
elif _aresame(elem, 0):
|
357 |
+
return FreeModuleElement(self, (self.ring.convert(0),)*self.rank)
|
358 |
+
else:
|
359 |
+
raise CoercionFailed
|
360 |
+
|
361 |
+
def is_zero(self):
|
362 |
+
"""
|
363 |
+
Returns True if ``self`` is a zero module.
|
364 |
+
|
365 |
+
(If, as this implementation assumes, the coefficient ring is not the
|
366 |
+
zero ring, then this is equivalent to the rank being zero.)
|
367 |
+
|
368 |
+
Examples
|
369 |
+
========
|
370 |
+
|
371 |
+
>>> from sympy.abc import x
|
372 |
+
>>> from sympy import QQ
|
373 |
+
>>> QQ.old_poly_ring(x).free_module(0).is_zero()
|
374 |
+
True
|
375 |
+
>>> QQ.old_poly_ring(x).free_module(1).is_zero()
|
376 |
+
False
|
377 |
+
"""
|
378 |
+
return self.rank == 0
|
379 |
+
|
380 |
+
def basis(self):
|
381 |
+
"""
|
382 |
+
Return a set of basis elements.
|
383 |
+
|
384 |
+
Examples
|
385 |
+
========
|
386 |
+
|
387 |
+
>>> from sympy.abc import x
|
388 |
+
>>> from sympy import QQ
|
389 |
+
>>> QQ.old_poly_ring(x).free_module(3).basis()
|
390 |
+
([1, 0, 0], [0, 1, 0], [0, 0, 1])
|
391 |
+
"""
|
392 |
+
from sympy.matrices import eye
|
393 |
+
M = eye(self.rank)
|
394 |
+
return tuple(self.convert(M.row(i)) for i in range(self.rank))
|
395 |
+
|
396 |
+
def quotient_module(self, submodule):
|
397 |
+
"""
|
398 |
+
Return a quotient module.
|
399 |
+
|
400 |
+
Examples
|
401 |
+
========
|
402 |
+
|
403 |
+
>>> from sympy.abc import x
|
404 |
+
>>> from sympy import QQ
|
405 |
+
>>> M = QQ.old_poly_ring(x).free_module(2)
|
406 |
+
>>> M.quotient_module(M.submodule([1, x], [x, 2]))
|
407 |
+
QQ[x]**2/<[1, x], [x, 2]>
|
408 |
+
|
409 |
+
Or more conicisely, using the overloaded division operator:
|
410 |
+
|
411 |
+
>>> QQ.old_poly_ring(x).free_module(2) / [[1, x], [x, 2]]
|
412 |
+
QQ[x]**2/<[1, x], [x, 2]>
|
413 |
+
"""
|
414 |
+
return QuotientModule(self.ring, self, submodule)
|
415 |
+
|
416 |
+
def multiply_ideal(self, other):
|
417 |
+
"""
|
418 |
+
Multiply ``self`` by the ideal ``other``.
|
419 |
+
|
420 |
+
Examples
|
421 |
+
========
|
422 |
+
|
423 |
+
>>> from sympy.abc import x
|
424 |
+
>>> from sympy import QQ
|
425 |
+
>>> I = QQ.old_poly_ring(x).ideal(x)
|
426 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
427 |
+
>>> F.multiply_ideal(I)
|
428 |
+
<[x, 0], [0, x]>
|
429 |
+
"""
|
430 |
+
return self.submodule(*self.basis()).multiply_ideal(other)
|
431 |
+
|
432 |
+
def identity_hom(self):
|
433 |
+
"""
|
434 |
+
Return the identity homomorphism on ``self``.
|
435 |
+
|
436 |
+
Examples
|
437 |
+
========
|
438 |
+
|
439 |
+
>>> from sympy.abc import x
|
440 |
+
>>> from sympy import QQ
|
441 |
+
>>> QQ.old_poly_ring(x).free_module(2).identity_hom()
|
442 |
+
Matrix([
|
443 |
+
[1, 0], : QQ[x]**2 -> QQ[x]**2
|
444 |
+
[0, 1]])
|
445 |
+
"""
|
446 |
+
from sympy.polys.agca.homomorphisms import homomorphism
|
447 |
+
return homomorphism(self, self, self.basis())
|
448 |
+
|
449 |
+
|
450 |
+
class FreeModulePolyRing(FreeModule):
|
451 |
+
"""
|
452 |
+
Free module over a generalized polynomial ring.
|
453 |
+
|
454 |
+
Do not instantiate this, use the constructor method of the ring instead:
|
455 |
+
|
456 |
+
Examples
|
457 |
+
========
|
458 |
+
|
459 |
+
>>> from sympy.abc import x
|
460 |
+
>>> from sympy import QQ
|
461 |
+
>>> F = QQ.old_poly_ring(x).free_module(3)
|
462 |
+
>>> F
|
463 |
+
QQ[x]**3
|
464 |
+
>>> F.contains([x, 1, 0])
|
465 |
+
True
|
466 |
+
>>> F.contains([1/x, 0, 1])
|
467 |
+
False
|
468 |
+
"""
|
469 |
+
|
470 |
+
def __init__(self, ring, rank):
|
471 |
+
from sympy.polys.domains.old_polynomialring import PolynomialRingBase
|
472 |
+
FreeModule.__init__(self, ring, rank)
|
473 |
+
if not isinstance(ring, PolynomialRingBase):
|
474 |
+
raise NotImplementedError('This implementation only works over '
|
475 |
+
+ 'polynomial rings, got %s' % ring)
|
476 |
+
if not isinstance(ring.dom, Field):
|
477 |
+
raise NotImplementedError('Ground domain must be a field, '
|
478 |
+
+ 'got %s' % ring.dom)
|
479 |
+
|
480 |
+
def submodule(self, *gens, **opts):
|
481 |
+
"""
|
482 |
+
Generate a submodule.
|
483 |
+
|
484 |
+
Examples
|
485 |
+
========
|
486 |
+
|
487 |
+
>>> from sympy.abc import x, y
|
488 |
+
>>> from sympy import QQ
|
489 |
+
>>> M = QQ.old_poly_ring(x, y).free_module(2).submodule([x, x + y])
|
490 |
+
>>> M
|
491 |
+
<[x, x + y]>
|
492 |
+
>>> M.contains([2*x, 2*x + 2*y])
|
493 |
+
True
|
494 |
+
>>> M.contains([x, y])
|
495 |
+
False
|
496 |
+
"""
|
497 |
+
return SubModulePolyRing(gens, self, **opts)
|
498 |
+
|
499 |
+
|
500 |
+
class FreeModuleQuotientRing(FreeModule):
|
501 |
+
"""
|
502 |
+
Free module over a quotient ring.
|
503 |
+
|
504 |
+
Do not instantiate this, use the constructor method of the ring instead:
|
505 |
+
|
506 |
+
Examples
|
507 |
+
========
|
508 |
+
|
509 |
+
>>> from sympy.abc import x
|
510 |
+
>>> from sympy import QQ
|
511 |
+
>>> F = (QQ.old_poly_ring(x)/[x**2 + 1]).free_module(3)
|
512 |
+
>>> F
|
513 |
+
(QQ[x]/<x**2 + 1>)**3
|
514 |
+
|
515 |
+
Attributes
|
516 |
+
|
517 |
+
- quot - the quotient module `R^n / IR^n`, where `R/I` is our ring
|
518 |
+
"""
|
519 |
+
|
520 |
+
def __init__(self, ring, rank):
|
521 |
+
from sympy.polys.domains.quotientring import QuotientRing
|
522 |
+
FreeModule.__init__(self, ring, rank)
|
523 |
+
if not isinstance(ring, QuotientRing):
|
524 |
+
raise NotImplementedError('This implementation only works over '
|
525 |
+
+ 'quotient rings, got %s' % ring)
|
526 |
+
F = self.ring.ring.free_module(self.rank)
|
527 |
+
self.quot = F / (self.ring.base_ideal*F)
|
528 |
+
|
529 |
+
def __repr__(self):
|
530 |
+
return "(" + repr(self.ring) + ")" + "**" + repr(self.rank)
|
531 |
+
|
532 |
+
def submodule(self, *gens, **opts):
|
533 |
+
"""
|
534 |
+
Generate a submodule.
|
535 |
+
|
536 |
+
Examples
|
537 |
+
========
|
538 |
+
|
539 |
+
>>> from sympy.abc import x, y
|
540 |
+
>>> from sympy import QQ
|
541 |
+
>>> M = (QQ.old_poly_ring(x, y)/[x**2 - y**2]).free_module(2).submodule([x, x + y])
|
542 |
+
>>> M
|
543 |
+
<[x + <x**2 - y**2>, x + y + <x**2 - y**2>]>
|
544 |
+
>>> M.contains([y**2, x**2 + x*y])
|
545 |
+
True
|
546 |
+
>>> M.contains([x, y])
|
547 |
+
False
|
548 |
+
"""
|
549 |
+
return SubModuleQuotientRing(gens, self, **opts)
|
550 |
+
|
551 |
+
def lift(self, elem):
|
552 |
+
"""
|
553 |
+
Lift the element ``elem`` of self to the module self.quot.
|
554 |
+
|
555 |
+
Note that self.quot is the same set as self, just as an R-module
|
556 |
+
and not as an R/I-module, so this makes sense.
|
557 |
+
|
558 |
+
Examples
|
559 |
+
========
|
560 |
+
|
561 |
+
>>> from sympy.abc import x
|
562 |
+
>>> from sympy import QQ
|
563 |
+
>>> F = (QQ.old_poly_ring(x)/[x**2 + 1]).free_module(2)
|
564 |
+
>>> e = F.convert([1, 0])
|
565 |
+
>>> e
|
566 |
+
[1 + <x**2 + 1>, 0 + <x**2 + 1>]
|
567 |
+
>>> L = F.quot
|
568 |
+
>>> l = F.lift(e)
|
569 |
+
>>> l
|
570 |
+
[1, 0] + <[x**2 + 1, 0], [0, x**2 + 1]>
|
571 |
+
>>> L.contains(l)
|
572 |
+
True
|
573 |
+
"""
|
574 |
+
return self.quot.convert([x.data for x in elem])
|
575 |
+
|
576 |
+
def unlift(self, elem):
|
577 |
+
"""
|
578 |
+
Push down an element of self.quot to self.
|
579 |
+
|
580 |
+
This undoes ``lift``.
|
581 |
+
|
582 |
+
Examples
|
583 |
+
========
|
584 |
+
|
585 |
+
>>> from sympy.abc import x
|
586 |
+
>>> from sympy import QQ
|
587 |
+
>>> F = (QQ.old_poly_ring(x)/[x**2 + 1]).free_module(2)
|
588 |
+
>>> e = F.convert([1, 0])
|
589 |
+
>>> l = F.lift(e)
|
590 |
+
>>> e == l
|
591 |
+
False
|
592 |
+
>>> e == F.unlift(l)
|
593 |
+
True
|
594 |
+
"""
|
595 |
+
return self.convert(elem.data)
|
596 |
+
|
597 |
+
##########################################################################
|
598 |
+
## Submodules and subquotients ###########################################
|
599 |
+
##########################################################################
|
600 |
+
|
601 |
+
|
602 |
+
class SubModule(Module):
|
603 |
+
"""
|
604 |
+
Base class for submodules.
|
605 |
+
|
606 |
+
Attributes:
|
607 |
+
|
608 |
+
- container - containing module
|
609 |
+
- gens - generators (subset of containing module)
|
610 |
+
- rank - rank of containing module
|
611 |
+
|
612 |
+
Non-implemented methods:
|
613 |
+
|
614 |
+
- _contains
|
615 |
+
- _syzygies
|
616 |
+
- _in_terms_of_generators
|
617 |
+
- _intersect
|
618 |
+
- _module_quotient
|
619 |
+
|
620 |
+
Methods that likely need change in subclasses:
|
621 |
+
|
622 |
+
- reduce_element
|
623 |
+
"""
|
624 |
+
|
625 |
+
def __init__(self, gens, container):
|
626 |
+
Module.__init__(self, container.ring)
|
627 |
+
self.gens = tuple(container.convert(x) for x in gens)
|
628 |
+
self.container = container
|
629 |
+
self.rank = container.rank
|
630 |
+
self.ring = container.ring
|
631 |
+
self.dtype = container.dtype
|
632 |
+
|
633 |
+
def __repr__(self):
|
634 |
+
return "<" + ", ".join(repr(x) for x in self.gens) + ">"
|
635 |
+
|
636 |
+
def _contains(self, other):
|
637 |
+
"""Implementation of containment.
|
638 |
+
Other is guaranteed to be FreeModuleElement."""
|
639 |
+
raise NotImplementedError
|
640 |
+
|
641 |
+
def _syzygies(self):
|
642 |
+
"""Implementation of syzygy computation wrt self generators."""
|
643 |
+
raise NotImplementedError
|
644 |
+
|
645 |
+
def _in_terms_of_generators(self, e):
|
646 |
+
"""Implementation of expression in terms of generators."""
|
647 |
+
raise NotImplementedError
|
648 |
+
|
649 |
+
def convert(self, elem, M=None):
|
650 |
+
"""
|
651 |
+
Convert ``elem`` into the internal represantition.
|
652 |
+
|
653 |
+
Mostly called implicitly.
|
654 |
+
|
655 |
+
Examples
|
656 |
+
========
|
657 |
+
|
658 |
+
>>> from sympy.abc import x
|
659 |
+
>>> from sympy import QQ
|
660 |
+
>>> M = QQ.old_poly_ring(x).free_module(2).submodule([1, x])
|
661 |
+
>>> M.convert([2, 2*x])
|
662 |
+
[2, 2*x]
|
663 |
+
"""
|
664 |
+
if isinstance(elem, self.container.dtype) and elem.module is self:
|
665 |
+
return elem
|
666 |
+
r = copy(self.container.convert(elem, M))
|
667 |
+
r.module = self
|
668 |
+
if not self._contains(r):
|
669 |
+
raise CoercionFailed
|
670 |
+
return r
|
671 |
+
|
672 |
+
def _intersect(self, other):
|
673 |
+
"""Implementation of intersection.
|
674 |
+
Other is guaranteed to be a submodule of same free module."""
|
675 |
+
raise NotImplementedError
|
676 |
+
|
677 |
+
def _module_quotient(self, other):
|
678 |
+
"""Implementation of quotient.
|
679 |
+
Other is guaranteed to be a submodule of same free module."""
|
680 |
+
raise NotImplementedError
|
681 |
+
|
682 |
+
def intersect(self, other, **options):
|
683 |
+
"""
|
684 |
+
Returns the intersection of ``self`` with submodule ``other``.
|
685 |
+
|
686 |
+
Examples
|
687 |
+
========
|
688 |
+
|
689 |
+
>>> from sympy.abc import x, y
|
690 |
+
>>> from sympy import QQ
|
691 |
+
>>> F = QQ.old_poly_ring(x, y).free_module(2)
|
692 |
+
>>> F.submodule([x, x]).intersect(F.submodule([y, y]))
|
693 |
+
<[x*y, x*y]>
|
694 |
+
|
695 |
+
Some implementation allow further options to be passed. Currently, to
|
696 |
+
only one implemented is ``relations=True``, in which case the function
|
697 |
+
will return a triple ``(res, rela, relb)``, where ``res`` is the
|
698 |
+
intersection module, and ``rela`` and ``relb`` are lists of coefficient
|
699 |
+
vectors, expressing the generators of ``res`` in terms of the
|
700 |
+
generators of ``self`` (``rela``) and ``other`` (``relb``).
|
701 |
+
|
702 |
+
>>> F.submodule([x, x]).intersect(F.submodule([y, y]), relations=True)
|
703 |
+
(<[x*y, x*y]>, [(y,)], [(x,)])
|
704 |
+
|
705 |
+
The above result says: the intersection module is generated by the
|
706 |
+
single element `(-xy, -xy) = -y (x, x) = -x (y, y)`, where
|
707 |
+
`(x, x)` and `(y, y)` respectively are the unique generators of
|
708 |
+
the two modules being intersected.
|
709 |
+
"""
|
710 |
+
if not isinstance(other, SubModule):
|
711 |
+
raise TypeError('%s is not a SubModule' % other)
|
712 |
+
if other.container != self.container:
|
713 |
+
raise ValueError(
|
714 |
+
'%s is contained in a different free module' % other)
|
715 |
+
return self._intersect(other, **options)
|
716 |
+
|
717 |
+
def module_quotient(self, other, **options):
|
718 |
+
r"""
|
719 |
+
Returns the module quotient of ``self`` by submodule ``other``.
|
720 |
+
|
721 |
+
That is, if ``self`` is the module `M` and ``other`` is `N`, then
|
722 |
+
return the ideal `\{f \in R | fN \subset M\}`.
|
723 |
+
|
724 |
+
Examples
|
725 |
+
========
|
726 |
+
|
727 |
+
>>> from sympy import QQ
|
728 |
+
>>> from sympy.abc import x, y
|
729 |
+
>>> F = QQ.old_poly_ring(x, y).free_module(2)
|
730 |
+
>>> S = F.submodule([x*y, x*y])
|
731 |
+
>>> T = F.submodule([x, x])
|
732 |
+
>>> S.module_quotient(T)
|
733 |
+
<y>
|
734 |
+
|
735 |
+
Some implementations allow further options to be passed. Currently, the
|
736 |
+
only one implemented is ``relations=True``, which may only be passed
|
737 |
+
if ``other`` is principal. In this case the function
|
738 |
+
will return a pair ``(res, rel)`` where ``res`` is the ideal, and
|
739 |
+
``rel`` is a list of coefficient vectors, expressing the generators of
|
740 |
+
the ideal, multiplied by the generator of ``other`` in terms of
|
741 |
+
generators of ``self``.
|
742 |
+
|
743 |
+
>>> S.module_quotient(T, relations=True)
|
744 |
+
(<y>, [[1]])
|
745 |
+
|
746 |
+
This means that the quotient ideal is generated by the single element
|
747 |
+
`y`, and that `y (x, x) = 1 (xy, xy)`, `(x, x)` and `(xy, xy)` being
|
748 |
+
the generators of `T` and `S`, respectively.
|
749 |
+
"""
|
750 |
+
if not isinstance(other, SubModule):
|
751 |
+
raise TypeError('%s is not a SubModule' % other)
|
752 |
+
if other.container != self.container:
|
753 |
+
raise ValueError(
|
754 |
+
'%s is contained in a different free module' % other)
|
755 |
+
return self._module_quotient(other, **options)
|
756 |
+
|
757 |
+
def union(self, other):
|
758 |
+
"""
|
759 |
+
Returns the module generated by the union of ``self`` and ``other``.
|
760 |
+
|
761 |
+
Examples
|
762 |
+
========
|
763 |
+
|
764 |
+
>>> from sympy.abc import x
|
765 |
+
>>> from sympy import QQ
|
766 |
+
>>> F = QQ.old_poly_ring(x).free_module(1)
|
767 |
+
>>> M = F.submodule([x**2 + x]) # <x(x+1)>
|
768 |
+
>>> N = F.submodule([x**2 - 1]) # <(x-1)(x+1)>
|
769 |
+
>>> M.union(N) == F.submodule([x+1])
|
770 |
+
True
|
771 |
+
"""
|
772 |
+
if not isinstance(other, SubModule):
|
773 |
+
raise TypeError('%s is not a SubModule' % other)
|
774 |
+
if other.container != self.container:
|
775 |
+
raise ValueError(
|
776 |
+
'%s is contained in a different free module' % other)
|
777 |
+
return self.__class__(self.gens + other.gens, self.container)
|
778 |
+
|
779 |
+
def is_zero(self):
|
780 |
+
"""
|
781 |
+
Return True if ``self`` is a zero module.
|
782 |
+
|
783 |
+
Examples
|
784 |
+
========
|
785 |
+
|
786 |
+
>>> from sympy.abc import x
|
787 |
+
>>> from sympy import QQ
|
788 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
789 |
+
>>> F.submodule([x, 1]).is_zero()
|
790 |
+
False
|
791 |
+
>>> F.submodule([0, 0]).is_zero()
|
792 |
+
True
|
793 |
+
"""
|
794 |
+
return all(x == 0 for x in self.gens)
|
795 |
+
|
796 |
+
def submodule(self, *gens):
|
797 |
+
"""
|
798 |
+
Generate a submodule.
|
799 |
+
|
800 |
+
Examples
|
801 |
+
========
|
802 |
+
|
803 |
+
>>> from sympy.abc import x
|
804 |
+
>>> from sympy import QQ
|
805 |
+
>>> M = QQ.old_poly_ring(x).free_module(2).submodule([x, 1])
|
806 |
+
>>> M.submodule([x**2, x])
|
807 |
+
<[x**2, x]>
|
808 |
+
"""
|
809 |
+
if not self.subset(gens):
|
810 |
+
raise ValueError('%s not a subset of %s' % (gens, self))
|
811 |
+
return self.__class__(gens, self.container)
|
812 |
+
|
813 |
+
def is_full_module(self):
|
814 |
+
"""
|
815 |
+
Return True if ``self`` is the entire free module.
|
816 |
+
|
817 |
+
Examples
|
818 |
+
========
|
819 |
+
|
820 |
+
>>> from sympy.abc import x
|
821 |
+
>>> from sympy import QQ
|
822 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
823 |
+
>>> F.submodule([x, 1]).is_full_module()
|
824 |
+
False
|
825 |
+
>>> F.submodule([1, 1], [1, 2]).is_full_module()
|
826 |
+
True
|
827 |
+
"""
|
828 |
+
return all(self.contains(x) for x in self.container.basis())
|
829 |
+
|
830 |
+
def is_submodule(self, other):
|
831 |
+
"""
|
832 |
+
Returns True if ``other`` is a submodule of ``self``.
|
833 |
+
|
834 |
+
>>> from sympy.abc import x
|
835 |
+
>>> from sympy import QQ
|
836 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
837 |
+
>>> M = F.submodule([2, x])
|
838 |
+
>>> N = M.submodule([2*x, x**2])
|
839 |
+
>>> M.is_submodule(M)
|
840 |
+
True
|
841 |
+
>>> M.is_submodule(N)
|
842 |
+
True
|
843 |
+
>>> N.is_submodule(M)
|
844 |
+
False
|
845 |
+
"""
|
846 |
+
if isinstance(other, SubModule):
|
847 |
+
return self.container == other.container and \
|
848 |
+
all(self.contains(x) for x in other.gens)
|
849 |
+
if isinstance(other, (FreeModule, QuotientModule)):
|
850 |
+
return self.container == other and self.is_full_module()
|
851 |
+
return False
|
852 |
+
|
853 |
+
def syzygy_module(self, **opts):
|
854 |
+
r"""
|
855 |
+
Compute the syzygy module of the generators of ``self``.
|
856 |
+
|
857 |
+
Suppose `M` is generated by `f_1, \ldots, f_n` over the ring
|
858 |
+
`R`. Consider the homomorphism `\phi: R^n \to M`, given by
|
859 |
+
sending `(r_1, \ldots, r_n) \to r_1 f_1 + \cdots + r_n f_n`.
|
860 |
+
The syzygy module is defined to be the kernel of `\phi`.
|
861 |
+
|
862 |
+
Examples
|
863 |
+
========
|
864 |
+
|
865 |
+
The syzygy module is zero iff the generators generate freely a free
|
866 |
+
submodule:
|
867 |
+
|
868 |
+
>>> from sympy.abc import x, y
|
869 |
+
>>> from sympy import QQ
|
870 |
+
>>> QQ.old_poly_ring(x).free_module(2).submodule([1, 0], [1, 1]).syzygy_module().is_zero()
|
871 |
+
True
|
872 |
+
|
873 |
+
A slightly more interesting example:
|
874 |
+
|
875 |
+
>>> M = QQ.old_poly_ring(x, y).free_module(2).submodule([x, 2*x], [y, 2*y])
|
876 |
+
>>> S = QQ.old_poly_ring(x, y).free_module(2).submodule([y, -x])
|
877 |
+
>>> M.syzygy_module() == S
|
878 |
+
True
|
879 |
+
"""
|
880 |
+
F = self.ring.free_module(len(self.gens))
|
881 |
+
# NOTE we filter out zero syzygies. This is for convenience of the
|
882 |
+
# _syzygies function and not meant to replace any real "generating set
|
883 |
+
# reduction" algorithm
|
884 |
+
return F.submodule(*[x for x in self._syzygies() if F.convert(x) != 0],
|
885 |
+
**opts)
|
886 |
+
|
887 |
+
def in_terms_of_generators(self, e):
|
888 |
+
"""
|
889 |
+
Express element ``e`` of ``self`` in terms of the generators.
|
890 |
+
|
891 |
+
Examples
|
892 |
+
========
|
893 |
+
|
894 |
+
>>> from sympy.abc import x
|
895 |
+
>>> from sympy import QQ
|
896 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
897 |
+
>>> M = F.submodule([1, 0], [1, 1])
|
898 |
+
>>> M.in_terms_of_generators([x, x**2])
|
899 |
+
[-x**2 + x, x**2]
|
900 |
+
"""
|
901 |
+
try:
|
902 |
+
e = self.convert(e)
|
903 |
+
except CoercionFailed:
|
904 |
+
raise ValueError('%s is not an element of %s' % (e, self))
|
905 |
+
return self._in_terms_of_generators(e)
|
906 |
+
|
907 |
+
def reduce_element(self, x):
|
908 |
+
"""
|
909 |
+
Reduce the element ``x`` of our ring modulo the ideal ``self``.
|
910 |
+
|
911 |
+
Here "reduce" has no specific meaning, it could return a unique normal
|
912 |
+
form, simplify the expression a bit, or just do nothing.
|
913 |
+
"""
|
914 |
+
return x
|
915 |
+
|
916 |
+
def quotient_module(self, other, **opts):
|
917 |
+
"""
|
918 |
+
Return a quotient module.
|
919 |
+
|
920 |
+
This is the same as taking a submodule of a quotient of the containing
|
921 |
+
module.
|
922 |
+
|
923 |
+
Examples
|
924 |
+
========
|
925 |
+
|
926 |
+
>>> from sympy.abc import x
|
927 |
+
>>> from sympy import QQ
|
928 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
929 |
+
>>> S1 = F.submodule([x, 1])
|
930 |
+
>>> S2 = F.submodule([x**2, x])
|
931 |
+
>>> S1.quotient_module(S2)
|
932 |
+
<[x, 1] + <[x**2, x]>>
|
933 |
+
|
934 |
+
Or more coincisely, using the overloaded division operator:
|
935 |
+
|
936 |
+
>>> F.submodule([x, 1]) / [(x**2, x)]
|
937 |
+
<[x, 1] + <[x**2, x]>>
|
938 |
+
"""
|
939 |
+
if not self.is_submodule(other):
|
940 |
+
raise ValueError('%s not a submodule of %s' % (other, self))
|
941 |
+
return SubQuotientModule(self.gens,
|
942 |
+
self.container.quotient_module(other), **opts)
|
943 |
+
|
944 |
+
def __add__(self, oth):
|
945 |
+
return self.container.quotient_module(self).convert(oth)
|
946 |
+
|
947 |
+
__radd__ = __add__
|
948 |
+
|
949 |
+
def multiply_ideal(self, I):
|
950 |
+
"""
|
951 |
+
Multiply ``self`` by the ideal ``I``.
|
952 |
+
|
953 |
+
Examples
|
954 |
+
========
|
955 |
+
|
956 |
+
>>> from sympy.abc import x
|
957 |
+
>>> from sympy import QQ
|
958 |
+
>>> I = QQ.old_poly_ring(x).ideal(x**2)
|
959 |
+
>>> M = QQ.old_poly_ring(x).free_module(2).submodule([1, 1])
|
960 |
+
>>> I*M
|
961 |
+
<[x**2, x**2]>
|
962 |
+
"""
|
963 |
+
return self.submodule(*[x*g for [x] in I._module.gens for g in self.gens])
|
964 |
+
|
965 |
+
def inclusion_hom(self):
|
966 |
+
"""
|
967 |
+
Return a homomorphism representing the inclusion map of ``self``.
|
968 |
+
|
969 |
+
That is, the natural map from ``self`` to ``self.container``.
|
970 |
+
|
971 |
+
Examples
|
972 |
+
========
|
973 |
+
|
974 |
+
>>> from sympy.abc import x
|
975 |
+
>>> from sympy import QQ
|
976 |
+
>>> QQ.old_poly_ring(x).free_module(2).submodule([x, x]).inclusion_hom()
|
977 |
+
Matrix([
|
978 |
+
[1, 0], : <[x, x]> -> QQ[x]**2
|
979 |
+
[0, 1]])
|
980 |
+
"""
|
981 |
+
return self.container.identity_hom().restrict_domain(self)
|
982 |
+
|
983 |
+
def identity_hom(self):
|
984 |
+
"""
|
985 |
+
Return the identity homomorphism on ``self``.
|
986 |
+
|
987 |
+
Examples
|
988 |
+
========
|
989 |
+
|
990 |
+
>>> from sympy.abc import x
|
991 |
+
>>> from sympy import QQ
|
992 |
+
>>> QQ.old_poly_ring(x).free_module(2).submodule([x, x]).identity_hom()
|
993 |
+
Matrix([
|
994 |
+
[1, 0], : <[x, x]> -> <[x, x]>
|
995 |
+
[0, 1]])
|
996 |
+
"""
|
997 |
+
return self.container.identity_hom().restrict_domain(
|
998 |
+
self).restrict_codomain(self)
|
999 |
+
|
1000 |
+
|
1001 |
+
class SubQuotientModule(SubModule):
|
1002 |
+
"""
|
1003 |
+
Submodule of a quotient module.
|
1004 |
+
|
1005 |
+
Equivalently, quotient module of a submodule.
|
1006 |
+
|
1007 |
+
Do not instantiate this, instead use the submodule or quotient_module
|
1008 |
+
constructing methods:
|
1009 |
+
|
1010 |
+
>>> from sympy.abc import x
|
1011 |
+
>>> from sympy import QQ
|
1012 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
1013 |
+
>>> S = F.submodule([1, 0], [1, x])
|
1014 |
+
>>> Q = F/[(1, 0)]
|
1015 |
+
>>> S/[(1, 0)] == Q.submodule([5, x])
|
1016 |
+
True
|
1017 |
+
|
1018 |
+
Attributes:
|
1019 |
+
|
1020 |
+
- base - base module we are quotient of
|
1021 |
+
- killed_module - submodule used to form the quotient
|
1022 |
+
"""
|
1023 |
+
def __init__(self, gens, container, **opts):
|
1024 |
+
SubModule.__init__(self, gens, container)
|
1025 |
+
self.killed_module = self.container.killed_module
|
1026 |
+
# XXX it is important for some code below that the generators of base
|
1027 |
+
# are in this particular order!
|
1028 |
+
self.base = self.container.base.submodule(
|
1029 |
+
*[x.data for x in self.gens], **opts).union(self.killed_module)
|
1030 |
+
|
1031 |
+
def _contains(self, elem):
|
1032 |
+
return self.base.contains(elem.data)
|
1033 |
+
|
1034 |
+
def _syzygies(self):
|
1035 |
+
# let N = self.killed_module be generated by e_1, ..., e_r
|
1036 |
+
# let F = self.base be generated by f_1, ..., f_s and e_1, ..., e_r
|
1037 |
+
# Then self = F/N.
|
1038 |
+
# Let phi: R**s --> self be the evident surjection.
|
1039 |
+
# Similarly psi: R**(s + r) --> F.
|
1040 |
+
# We need to find generators for ker(phi). Let chi: R**s --> F be the
|
1041 |
+
# evident lift of phi. For X in R**s, phi(X) = 0 iff chi(X) is
|
1042 |
+
# contained in N, iff there exists Y in R**r such that
|
1043 |
+
# psi(X, Y) = 0.
|
1044 |
+
# Hence if alpha: R**(s + r) --> R**s is the projection map, then
|
1045 |
+
# ker(phi) = alpha ker(psi).
|
1046 |
+
return [X[:len(self.gens)] for X in self.base._syzygies()]
|
1047 |
+
|
1048 |
+
def _in_terms_of_generators(self, e):
|
1049 |
+
return self.base._in_terms_of_generators(e.data)[:len(self.gens)]
|
1050 |
+
|
1051 |
+
def is_full_module(self):
|
1052 |
+
"""
|
1053 |
+
Return True if ``self`` is the entire free module.
|
1054 |
+
|
1055 |
+
Examples
|
1056 |
+
========
|
1057 |
+
|
1058 |
+
>>> from sympy.abc import x
|
1059 |
+
>>> from sympy import QQ
|
1060 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
1061 |
+
>>> F.submodule([x, 1]).is_full_module()
|
1062 |
+
False
|
1063 |
+
>>> F.submodule([1, 1], [1, 2]).is_full_module()
|
1064 |
+
True
|
1065 |
+
"""
|
1066 |
+
return self.base.is_full_module()
|
1067 |
+
|
1068 |
+
def quotient_hom(self):
|
1069 |
+
"""
|
1070 |
+
Return the quotient homomorphism to self.
|
1071 |
+
|
1072 |
+
That is, return the natural map from ``self.base`` to ``self``.
|
1073 |
+
|
1074 |
+
Examples
|
1075 |
+
========
|
1076 |
+
|
1077 |
+
>>> from sympy.abc import x
|
1078 |
+
>>> from sympy import QQ
|
1079 |
+
>>> M = (QQ.old_poly_ring(x).free_module(2) / [(1, x)]).submodule([1, 0])
|
1080 |
+
>>> M.quotient_hom()
|
1081 |
+
Matrix([
|
1082 |
+
[1, 0], : <[1, 0], [1, x]> -> <[1, 0] + <[1, x]>, [1, x] + <[1, x]>>
|
1083 |
+
[0, 1]])
|
1084 |
+
"""
|
1085 |
+
return self.base.identity_hom().quotient_codomain(self.killed_module)
|
1086 |
+
|
1087 |
+
|
1088 |
+
_subs0 = lambda x: x[0]
|
1089 |
+
_subs1 = lambda x: x[1:]
|
1090 |
+
|
1091 |
+
|
1092 |
+
class ModuleOrder(ProductOrder):
|
1093 |
+
"""A product monomial order with a zeroth term as module index."""
|
1094 |
+
|
1095 |
+
def __init__(self, o1, o2, TOP):
|
1096 |
+
if TOP:
|
1097 |
+
ProductOrder.__init__(self, (o2, _subs1), (o1, _subs0))
|
1098 |
+
else:
|
1099 |
+
ProductOrder.__init__(self, (o1, _subs0), (o2, _subs1))
|
1100 |
+
|
1101 |
+
|
1102 |
+
class SubModulePolyRing(SubModule):
|
1103 |
+
"""
|
1104 |
+
Submodule of a free module over a generalized polynomial ring.
|
1105 |
+
|
1106 |
+
Do not instantiate this, use the constructor method of FreeModule instead:
|
1107 |
+
|
1108 |
+
>>> from sympy.abc import x, y
|
1109 |
+
>>> from sympy import QQ
|
1110 |
+
>>> F = QQ.old_poly_ring(x, y).free_module(2)
|
1111 |
+
>>> F.submodule([x, y], [1, 0])
|
1112 |
+
<[x, y], [1, 0]>
|
1113 |
+
|
1114 |
+
Attributes:
|
1115 |
+
|
1116 |
+
- order - monomial order used
|
1117 |
+
"""
|
1118 |
+
|
1119 |
+
#self._gb - cached groebner basis
|
1120 |
+
#self._gbe - cached groebner basis relations
|
1121 |
+
|
1122 |
+
def __init__(self, gens, container, order="lex", TOP=True):
|
1123 |
+
SubModule.__init__(self, gens, container)
|
1124 |
+
if not isinstance(container, FreeModulePolyRing):
|
1125 |
+
raise NotImplementedError('This implementation is for submodules of '
|
1126 |
+
+ 'FreeModulePolyRing, got %s' % container)
|
1127 |
+
self.order = ModuleOrder(monomial_key(order), self.ring.order, TOP)
|
1128 |
+
self._gb = None
|
1129 |
+
self._gbe = None
|
1130 |
+
|
1131 |
+
def __eq__(self, other):
|
1132 |
+
if isinstance(other, SubModulePolyRing) and self.order != other.order:
|
1133 |
+
return False
|
1134 |
+
return SubModule.__eq__(self, other)
|
1135 |
+
|
1136 |
+
def _groebner(self, extended=False):
|
1137 |
+
"""Returns a standard basis in sdm form."""
|
1138 |
+
from sympy.polys.distributedmodules import sdm_groebner, sdm_nf_mora
|
1139 |
+
if self._gbe is None and extended:
|
1140 |
+
gb, gbe = sdm_groebner(
|
1141 |
+
[self.ring._vector_to_sdm(x, self.order) for x in self.gens],
|
1142 |
+
sdm_nf_mora, self.order, self.ring.dom, extended=True)
|
1143 |
+
self._gb, self._gbe = tuple(gb), tuple(gbe)
|
1144 |
+
if self._gb is None:
|
1145 |
+
self._gb = tuple(sdm_groebner(
|
1146 |
+
[self.ring._vector_to_sdm(x, self.order) for x in self.gens],
|
1147 |
+
sdm_nf_mora, self.order, self.ring.dom))
|
1148 |
+
if extended:
|
1149 |
+
return self._gb, self._gbe
|
1150 |
+
else:
|
1151 |
+
return self._gb
|
1152 |
+
|
1153 |
+
def _groebner_vec(self, extended=False):
|
1154 |
+
"""Returns a standard basis in element form."""
|
1155 |
+
if not extended:
|
1156 |
+
return [FreeModuleElement(self,
|
1157 |
+
tuple(self.ring._sdm_to_vector(x, self.rank)))
|
1158 |
+
for x in self._groebner()]
|
1159 |
+
gb, gbe = self._groebner(extended=True)
|
1160 |
+
return ([self.convert(self.ring._sdm_to_vector(x, self.rank))
|
1161 |
+
for x in gb],
|
1162 |
+
[self.ring._sdm_to_vector(x, len(self.gens)) for x in gbe])
|
1163 |
+
|
1164 |
+
def _contains(self, x):
|
1165 |
+
from sympy.polys.distributedmodules import sdm_zero, sdm_nf_mora
|
1166 |
+
return sdm_nf_mora(self.ring._vector_to_sdm(x, self.order),
|
1167 |
+
self._groebner(), self.order, self.ring.dom) == \
|
1168 |
+
sdm_zero()
|
1169 |
+
|
1170 |
+
def _syzygies(self):
|
1171 |
+
"""Compute syzygies. See [SCA, algorithm 2.5.4]."""
|
1172 |
+
# NOTE if self.gens is a standard basis, this can be done more
|
1173 |
+
# efficiently using Schreyer's theorem
|
1174 |
+
|
1175 |
+
# First bullet point
|
1176 |
+
k = len(self.gens)
|
1177 |
+
r = self.rank
|
1178 |
+
zero = self.ring.convert(0)
|
1179 |
+
one = self.ring.convert(1)
|
1180 |
+
Rkr = self.ring.free_module(r + k)
|
1181 |
+
newgens = []
|
1182 |
+
for j, f in enumerate(self.gens):
|
1183 |
+
m = [0]*(r + k)
|
1184 |
+
for i, v in enumerate(f):
|
1185 |
+
m[i] = f[i]
|
1186 |
+
for i in range(k):
|
1187 |
+
m[r + i] = one if j == i else zero
|
1188 |
+
m = FreeModuleElement(Rkr, tuple(m))
|
1189 |
+
newgens.append(m)
|
1190 |
+
# Note: we need *descending* order on module index, and TOP=False to
|
1191 |
+
# get an elimination order
|
1192 |
+
F = Rkr.submodule(*newgens, order='ilex', TOP=False)
|
1193 |
+
|
1194 |
+
# Second bullet point: standard basis of F
|
1195 |
+
G = F._groebner_vec()
|
1196 |
+
|
1197 |
+
# Third bullet point: G0 = G intersect the new k components
|
1198 |
+
G0 = [x[r:] for x in G if all(y == zero for y in x[:r])]
|
1199 |
+
|
1200 |
+
# Fourth and fifth bullet points: we are done
|
1201 |
+
return G0
|
1202 |
+
|
1203 |
+
def _in_terms_of_generators(self, e):
|
1204 |
+
"""Expression in terms of generators. See [SCA, 2.8.1]."""
|
1205 |
+
# NOTE: if gens is a standard basis, this can be done more efficiently
|
1206 |
+
M = self.ring.free_module(self.rank).submodule(*((e,) + self.gens))
|
1207 |
+
S = M.syzygy_module(
|
1208 |
+
order="ilex", TOP=False) # We want decreasing order!
|
1209 |
+
G = S._groebner_vec()
|
1210 |
+
# This list cannot not be empty since e is an element
|
1211 |
+
e = [x for x in G if self.ring.is_unit(x[0])][0]
|
1212 |
+
return [-x/e[0] for x in e[1:]]
|
1213 |
+
|
1214 |
+
def reduce_element(self, x, NF=None):
|
1215 |
+
"""
|
1216 |
+
Reduce the element ``x`` of our container modulo ``self``.
|
1217 |
+
|
1218 |
+
This applies the normal form ``NF`` to ``x``. If ``NF`` is passed
|
1219 |
+
as none, the default Mora normal form is used (which is not unique!).
|
1220 |
+
"""
|
1221 |
+
from sympy.polys.distributedmodules import sdm_nf_mora
|
1222 |
+
if NF is None:
|
1223 |
+
NF = sdm_nf_mora
|
1224 |
+
return self.container.convert(self.ring._sdm_to_vector(NF(
|
1225 |
+
self.ring._vector_to_sdm(x, self.order), self._groebner(),
|
1226 |
+
self.order, self.ring.dom),
|
1227 |
+
self.rank))
|
1228 |
+
|
1229 |
+
def _intersect(self, other, relations=False):
|
1230 |
+
# See: [SCA, section 2.8.2]
|
1231 |
+
fi = self.gens
|
1232 |
+
hi = other.gens
|
1233 |
+
r = self.rank
|
1234 |
+
ci = [[0]*(2*r) for _ in range(r)]
|
1235 |
+
for k in range(r):
|
1236 |
+
ci[k][k] = 1
|
1237 |
+
ci[k][r + k] = 1
|
1238 |
+
di = [list(f) + [0]*r for f in fi]
|
1239 |
+
ei = [[0]*r + list(h) for h in hi]
|
1240 |
+
syz = self.ring.free_module(2*r).submodule(*(ci + di + ei))._syzygies()
|
1241 |
+
nonzero = [x for x in syz if any(y != self.ring.zero for y in x[:r])]
|
1242 |
+
res = self.container.submodule(*([-y for y in x[:r]] for x in nonzero))
|
1243 |
+
reln1 = [x[r:r + len(fi)] for x in nonzero]
|
1244 |
+
reln2 = [x[r + len(fi):] for x in nonzero]
|
1245 |
+
if relations:
|
1246 |
+
return res, reln1, reln2
|
1247 |
+
return res
|
1248 |
+
|
1249 |
+
def _module_quotient(self, other, relations=False):
|
1250 |
+
# See: [SCA, section 2.8.4]
|
1251 |
+
if relations and len(other.gens) != 1:
|
1252 |
+
raise NotImplementedError
|
1253 |
+
if len(other.gens) == 0:
|
1254 |
+
return self.ring.ideal(1)
|
1255 |
+
elif len(other.gens) == 1:
|
1256 |
+
# We do some trickery. Let f be the (vector!) generating ``other``
|
1257 |
+
# and f1, .., fn be the (vectors) generating self.
|
1258 |
+
# Consider the submodule of R^{r+1} generated by (f, 1) and
|
1259 |
+
# {(fi, 0) | i}. Then the intersection with the last module
|
1260 |
+
# component yields the quotient.
|
1261 |
+
g1 = list(other.gens[0]) + [1]
|
1262 |
+
gi = [list(x) + [0] for x in self.gens]
|
1263 |
+
# NOTE: We *need* to use an elimination order
|
1264 |
+
M = self.ring.free_module(self.rank + 1).submodule(*([g1] + gi),
|
1265 |
+
order='ilex', TOP=False)
|
1266 |
+
if not relations:
|
1267 |
+
return self.ring.ideal(*[x[-1] for x in M._groebner_vec() if
|
1268 |
+
all(y == self.ring.zero for y in x[:-1])])
|
1269 |
+
else:
|
1270 |
+
G, R = M._groebner_vec(extended=True)
|
1271 |
+
indices = [i for i, x in enumerate(G) if
|
1272 |
+
all(y == self.ring.zero for y in x[:-1])]
|
1273 |
+
return (self.ring.ideal(*[G[i][-1] for i in indices]),
|
1274 |
+
[[-x for x in R[i][1:]] for i in indices])
|
1275 |
+
# For more generators, we use I : <h1, .., hn> = intersection of
|
1276 |
+
# {I : <hi> | i}
|
1277 |
+
# TODO this can be done more efficiently
|
1278 |
+
return reduce(lambda x, y: x.intersect(y),
|
1279 |
+
(self._module_quotient(self.container.submodule(x)) for x in other.gens))
|
1280 |
+
|
1281 |
+
|
1282 |
+
class SubModuleQuotientRing(SubModule):
|
1283 |
+
"""
|
1284 |
+
Class for submodules of free modules over quotient rings.
|
1285 |
+
|
1286 |
+
Do not instantiate this. Instead use the submodule methods.
|
1287 |
+
|
1288 |
+
>>> from sympy.abc import x, y
|
1289 |
+
>>> from sympy import QQ
|
1290 |
+
>>> M = (QQ.old_poly_ring(x, y)/[x**2 - y**2]).free_module(2).submodule([x, x + y])
|
1291 |
+
>>> M
|
1292 |
+
<[x + <x**2 - y**2>, x + y + <x**2 - y**2>]>
|
1293 |
+
>>> M.contains([y**2, x**2 + x*y])
|
1294 |
+
True
|
1295 |
+
>>> M.contains([x, y])
|
1296 |
+
False
|
1297 |
+
|
1298 |
+
Attributes:
|
1299 |
+
|
1300 |
+
- quot - the subquotient of `R^n/IR^n` generated by lifts of our generators
|
1301 |
+
"""
|
1302 |
+
|
1303 |
+
def __init__(self, gens, container):
|
1304 |
+
SubModule.__init__(self, gens, container)
|
1305 |
+
self.quot = self.container.quot.submodule(
|
1306 |
+
*[self.container.lift(x) for x in self.gens])
|
1307 |
+
|
1308 |
+
def _contains(self, elem):
|
1309 |
+
return self.quot._contains(self.container.lift(elem))
|
1310 |
+
|
1311 |
+
def _syzygies(self):
|
1312 |
+
return [tuple(self.ring.convert(y, self.quot.ring) for y in x)
|
1313 |
+
for x in self.quot._syzygies()]
|
1314 |
+
|
1315 |
+
def _in_terms_of_generators(self, elem):
|
1316 |
+
return [self.ring.convert(x, self.quot.ring) for x in
|
1317 |
+
self.quot._in_terms_of_generators(self.container.lift(elem))]
|
1318 |
+
|
1319 |
+
##########################################################################
|
1320 |
+
## Quotient Modules ######################################################
|
1321 |
+
##########################################################################
|
1322 |
+
|
1323 |
+
|
1324 |
+
class QuotientModuleElement(ModuleElement):
|
1325 |
+
"""Element of a quotient module."""
|
1326 |
+
|
1327 |
+
def eq(self, d1, d2):
|
1328 |
+
"""Equality comparison."""
|
1329 |
+
return self.module.killed_module.contains(d1 - d2)
|
1330 |
+
|
1331 |
+
def __repr__(self):
|
1332 |
+
return repr(self.data) + " + " + repr(self.module.killed_module)
|
1333 |
+
|
1334 |
+
|
1335 |
+
class QuotientModule(Module):
|
1336 |
+
"""
|
1337 |
+
Class for quotient modules.
|
1338 |
+
|
1339 |
+
Do not instantiate this directly. For subquotients, see the
|
1340 |
+
SubQuotientModule class.
|
1341 |
+
|
1342 |
+
Attributes:
|
1343 |
+
|
1344 |
+
- base - the base module we are a quotient of
|
1345 |
+
- killed_module - the submodule used to form the quotient
|
1346 |
+
- rank of the base
|
1347 |
+
"""
|
1348 |
+
|
1349 |
+
dtype = QuotientModuleElement
|
1350 |
+
|
1351 |
+
def __init__(self, ring, base, submodule):
|
1352 |
+
Module.__init__(self, ring)
|
1353 |
+
if not base.is_submodule(submodule):
|
1354 |
+
raise ValueError('%s is not a submodule of %s' % (submodule, base))
|
1355 |
+
self.base = base
|
1356 |
+
self.killed_module = submodule
|
1357 |
+
self.rank = base.rank
|
1358 |
+
|
1359 |
+
def __repr__(self):
|
1360 |
+
return repr(self.base) + "/" + repr(self.killed_module)
|
1361 |
+
|
1362 |
+
def is_zero(self):
|
1363 |
+
"""
|
1364 |
+
Return True if ``self`` is a zero module.
|
1365 |
+
|
1366 |
+
This happens if and only if the base module is the same as the
|
1367 |
+
submodule being killed.
|
1368 |
+
|
1369 |
+
Examples
|
1370 |
+
========
|
1371 |
+
|
1372 |
+
>>> from sympy.abc import x
|
1373 |
+
>>> from sympy import QQ
|
1374 |
+
>>> F = QQ.old_poly_ring(x).free_module(2)
|
1375 |
+
>>> (F/[(1, 0)]).is_zero()
|
1376 |
+
False
|
1377 |
+
>>> (F/[(1, 0), (0, 1)]).is_zero()
|
1378 |
+
True
|
1379 |
+
"""
|
1380 |
+
return self.base == self.killed_module
|
1381 |
+
|
1382 |
+
def is_submodule(self, other):
|
1383 |
+
"""
|
1384 |
+
Return True if ``other`` is a submodule of ``self``.
|
1385 |
+
|
1386 |
+
Examples
|
1387 |
+
========
|
1388 |
+
|
1389 |
+
>>> from sympy.abc import x
|
1390 |
+
>>> from sympy import QQ
|
1391 |
+
>>> Q = QQ.old_poly_ring(x).free_module(2) / [(x, x)]
|
1392 |
+
>>> S = Q.submodule([1, 0])
|
1393 |
+
>>> Q.is_submodule(S)
|
1394 |
+
True
|
1395 |
+
>>> S.is_submodule(Q)
|
1396 |
+
False
|
1397 |
+
"""
|
1398 |
+
if isinstance(other, QuotientModule):
|
1399 |
+
return self.killed_module == other.killed_module and \
|
1400 |
+
self.base.is_submodule(other.base)
|
1401 |
+
if isinstance(other, SubQuotientModule):
|
1402 |
+
return other.container == self
|
1403 |
+
return False
|
1404 |
+
|
1405 |
+
def submodule(self, *gens, **opts):
|
1406 |
+
"""
|
1407 |
+
Generate a submodule.
|
1408 |
+
|
1409 |
+
This is the same as taking a quotient of a submodule of the base
|
1410 |
+
module.
|
1411 |
+
|
1412 |
+
Examples
|
1413 |
+
========
|
1414 |
+
|
1415 |
+
>>> from sympy.abc import x
|
1416 |
+
>>> from sympy import QQ
|
1417 |
+
>>> Q = QQ.old_poly_ring(x).free_module(2) / [(x, x)]
|
1418 |
+
>>> Q.submodule([x, 0])
|
1419 |
+
<[x, 0] + <[x, x]>>
|
1420 |
+
"""
|
1421 |
+
return SubQuotientModule(gens, self, **opts)
|
1422 |
+
|
1423 |
+
def convert(self, elem, M=None):
|
1424 |
+
"""
|
1425 |
+
Convert ``elem`` into the internal representation.
|
1426 |
+
|
1427 |
+
This method is called implicitly whenever computations involve elements
|
1428 |
+
not in the internal representation.
|
1429 |
+
|
1430 |
+
Examples
|
1431 |
+
========
|
1432 |
+
|
1433 |
+
>>> from sympy.abc import x
|
1434 |
+
>>> from sympy import QQ
|
1435 |
+
>>> F = QQ.old_poly_ring(x).free_module(2) / [(1, 2), (1, x)]
|
1436 |
+
>>> F.convert([1, 0])
|
1437 |
+
[1, 0] + <[1, 2], [1, x]>
|
1438 |
+
"""
|
1439 |
+
if isinstance(elem, QuotientModuleElement):
|
1440 |
+
if elem.module is self:
|
1441 |
+
return elem
|
1442 |
+
if self.killed_module.is_submodule(elem.module.killed_module):
|
1443 |
+
return QuotientModuleElement(self, self.base.convert(elem.data))
|
1444 |
+
raise CoercionFailed
|
1445 |
+
return QuotientModuleElement(self, self.base.convert(elem))
|
1446 |
+
|
1447 |
+
def identity_hom(self):
|
1448 |
+
"""
|
1449 |
+
Return the identity homomorphism on ``self``.
|
1450 |
+
|
1451 |
+
Examples
|
1452 |
+
========
|
1453 |
+
|
1454 |
+
>>> from sympy.abc import x
|
1455 |
+
>>> from sympy import QQ
|
1456 |
+
>>> M = QQ.old_poly_ring(x).free_module(2) / [(1, 2), (1, x)]
|
1457 |
+
>>> M.identity_hom()
|
1458 |
+
Matrix([
|
1459 |
+
[1, 0], : QQ[x]**2/<[1, 2], [1, x]> -> QQ[x]**2/<[1, 2], [1, x]>
|
1460 |
+
[0, 1]])
|
1461 |
+
"""
|
1462 |
+
return self.base.identity_hom().quotient_codomain(
|
1463 |
+
self.killed_module).quotient_domain(self.killed_module)
|
1464 |
+
|
1465 |
+
def quotient_hom(self):
|
1466 |
+
"""
|
1467 |
+
Return the quotient homomorphism to ``self``.
|
1468 |
+
|
1469 |
+
That is, return a homomorphism representing the natural map from
|
1470 |
+
``self.base`` to ``self``.
|
1471 |
+
|
1472 |
+
Examples
|
1473 |
+
========
|
1474 |
+
|
1475 |
+
>>> from sympy.abc import x
|
1476 |
+
>>> from sympy import QQ
|
1477 |
+
>>> M = QQ.old_poly_ring(x).free_module(2) / [(1, 2), (1, x)]
|
1478 |
+
>>> M.quotient_hom()
|
1479 |
+
Matrix([
|
1480 |
+
[1, 0], : QQ[x]**2 -> QQ[x]**2/<[1, 2], [1, x]>
|
1481 |
+
[0, 1]])
|
1482 |
+
"""
|
1483 |
+
return self.base.identity_hom().quotient_codomain(
|
1484 |
+
self.killed_module)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (187 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_extensions.cpython-310.pyc
ADDED
Binary file (7.86 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_homomorphisms.cpython-310.pyc
ADDED
Binary file (5.62 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_ideals.cpython-310.pyc
ADDED
Binary file (4.73 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/__pycache__/test_modules.cpython-310.pyc
ADDED
Binary file (15.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_extensions.py
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.symbol import symbols
|
2 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
3 |
+
from sympy.polys import QQ, ZZ
|
4 |
+
from sympy.polys.polytools import Poly
|
5 |
+
from sympy.polys.polyerrors import NotInvertible
|
6 |
+
from sympy.polys.agca.extensions import FiniteExtension
|
7 |
+
from sympy.polys.domainmatrix import DomainMatrix
|
8 |
+
|
9 |
+
from sympy.testing.pytest import raises
|
10 |
+
|
11 |
+
from sympy.abc import x, y, t
|
12 |
+
|
13 |
+
|
14 |
+
def test_FiniteExtension():
|
15 |
+
# Gaussian integers
|
16 |
+
A = FiniteExtension(Poly(x**2 + 1, x))
|
17 |
+
assert A.rank == 2
|
18 |
+
assert str(A) == 'ZZ[x]/(x**2 + 1)'
|
19 |
+
i = A.generator
|
20 |
+
assert i.parent() is A
|
21 |
+
|
22 |
+
assert i*i == A(-1)
|
23 |
+
raises(TypeError, lambda: i*())
|
24 |
+
|
25 |
+
assert A.basis == (A.one, i)
|
26 |
+
assert A(1) == A.one
|
27 |
+
assert i**2 == A(-1)
|
28 |
+
assert i**2 != -1 # no coercion
|
29 |
+
assert (2 + i)*(1 - i) == 3 - i
|
30 |
+
assert (1 + i)**8 == A(16)
|
31 |
+
assert A(1).inverse() == A(1)
|
32 |
+
raises(NotImplementedError, lambda: A(2).inverse())
|
33 |
+
|
34 |
+
# Finite field of order 27
|
35 |
+
F = FiniteExtension(Poly(x**3 - x + 1, x, modulus=3))
|
36 |
+
assert F.rank == 3
|
37 |
+
a = F.generator # also generates the cyclic group F - {0}
|
38 |
+
assert F.basis == (F(1), a, a**2)
|
39 |
+
assert a**27 == a
|
40 |
+
assert a**26 == F(1)
|
41 |
+
assert a**13 == F(-1)
|
42 |
+
assert a**9 == a + 1
|
43 |
+
assert a**3 == a - 1
|
44 |
+
assert a**6 == a**2 + a + 1
|
45 |
+
assert F(x**2 + x).inverse() == 1 - a
|
46 |
+
assert F(x + 2)**(-1) == F(x + 2).inverse()
|
47 |
+
assert a**19 * a**(-19) == F(1)
|
48 |
+
assert (a - 1) / (2*a**2 - 1) == a**2 + 1
|
49 |
+
assert (a - 1) // (2*a**2 - 1) == a**2 + 1
|
50 |
+
assert 2/(a**2 + 1) == a**2 - a + 1
|
51 |
+
assert (a**2 + 1)/2 == -a**2 - 1
|
52 |
+
raises(NotInvertible, lambda: F(0).inverse())
|
53 |
+
|
54 |
+
# Function field of an elliptic curve
|
55 |
+
K = FiniteExtension(Poly(t**2 - x**3 - x + 1, t, field=True))
|
56 |
+
assert K.rank == 2
|
57 |
+
assert str(K) == 'ZZ(x)[t]/(t**2 - x**3 - x + 1)'
|
58 |
+
y = K.generator
|
59 |
+
c = 1/(x**3 - x**2 + x - 1)
|
60 |
+
assert ((y + x)*(y - x)).inverse() == K(c)
|
61 |
+
assert (y + x)*(y - x)*c == K(1) # explicit inverse of y + x
|
62 |
+
|
63 |
+
|
64 |
+
def test_FiniteExtension_eq_hash():
|
65 |
+
# Test eq and hash
|
66 |
+
p1 = Poly(x**2 - 2, x, domain=ZZ)
|
67 |
+
p2 = Poly(x**2 - 2, x, domain=QQ)
|
68 |
+
K1 = FiniteExtension(p1)
|
69 |
+
K2 = FiniteExtension(p2)
|
70 |
+
assert K1 == FiniteExtension(Poly(x**2 - 2))
|
71 |
+
assert K2 != FiniteExtension(Poly(x**2 - 2))
|
72 |
+
assert len({K1, K2, FiniteExtension(p1)}) == 2
|
73 |
+
|
74 |
+
|
75 |
+
def test_FiniteExtension_mod():
|
76 |
+
# Test mod
|
77 |
+
K = FiniteExtension(Poly(x**3 + 1, x, domain=QQ))
|
78 |
+
xf = K(x)
|
79 |
+
assert (xf**2 - 1) % 1 == K.zero
|
80 |
+
assert 1 % (xf**2 - 1) == K.zero
|
81 |
+
assert (xf**2 - 1) / (xf - 1) == xf + 1
|
82 |
+
assert (xf**2 - 1) // (xf - 1) == xf + 1
|
83 |
+
assert (xf**2 - 1) % (xf - 1) == K.zero
|
84 |
+
raises(ZeroDivisionError, lambda: (xf**2 - 1) % 0)
|
85 |
+
raises(TypeError, lambda: xf % [])
|
86 |
+
raises(TypeError, lambda: [] % xf)
|
87 |
+
|
88 |
+
# Test mod over ring
|
89 |
+
K = FiniteExtension(Poly(x**3 + 1, x, domain=ZZ))
|
90 |
+
xf = K(x)
|
91 |
+
assert (xf**2 - 1) % 1 == K.zero
|
92 |
+
raises(NotImplementedError, lambda: (xf**2 - 1) % (xf - 1))
|
93 |
+
|
94 |
+
|
95 |
+
def test_FiniteExtension_from_sympy():
|
96 |
+
# Test to_sympy/from_sympy
|
97 |
+
K = FiniteExtension(Poly(x**3 + 1, x, domain=ZZ))
|
98 |
+
xf = K(x)
|
99 |
+
assert K.from_sympy(x) == xf
|
100 |
+
assert K.to_sympy(xf) == x
|
101 |
+
|
102 |
+
|
103 |
+
def test_FiniteExtension_set_domain():
|
104 |
+
KZ = FiniteExtension(Poly(x**2 + 1, x, domain='ZZ'))
|
105 |
+
KQ = FiniteExtension(Poly(x**2 + 1, x, domain='QQ'))
|
106 |
+
assert KZ.set_domain(QQ) == KQ
|
107 |
+
|
108 |
+
|
109 |
+
def test_FiniteExtension_exquo():
|
110 |
+
# Test exquo
|
111 |
+
K = FiniteExtension(Poly(x**4 + 1))
|
112 |
+
xf = K(x)
|
113 |
+
assert K.exquo(xf**2 - 1, xf - 1) == xf + 1
|
114 |
+
|
115 |
+
|
116 |
+
def test_FiniteExtension_convert():
|
117 |
+
# Test from_MonogenicFiniteExtension
|
118 |
+
K1 = FiniteExtension(Poly(x**2 + 1))
|
119 |
+
K2 = QQ[x]
|
120 |
+
x1, x2 = K1(x), K2(x)
|
121 |
+
assert K1.convert(x2) == x1
|
122 |
+
assert K2.convert(x1) == x2
|
123 |
+
|
124 |
+
K = FiniteExtension(Poly(x**2 - 1, domain=QQ))
|
125 |
+
assert K.convert_from(QQ(1, 2), QQ) == K.one/2
|
126 |
+
|
127 |
+
|
128 |
+
def test_FiniteExtension_division_ring():
|
129 |
+
# Test division in FiniteExtension over a ring
|
130 |
+
KQ = FiniteExtension(Poly(x**2 - 1, x, domain=QQ))
|
131 |
+
KZ = FiniteExtension(Poly(x**2 - 1, x, domain=ZZ))
|
132 |
+
KQt = FiniteExtension(Poly(x**2 - 1, x, domain=QQ[t]))
|
133 |
+
KQtf = FiniteExtension(Poly(x**2 - 1, x, domain=QQ.frac_field(t)))
|
134 |
+
assert KQ.is_Field is True
|
135 |
+
assert KZ.is_Field is False
|
136 |
+
assert KQt.is_Field is False
|
137 |
+
assert KQtf.is_Field is True
|
138 |
+
for K in KQ, KZ, KQt, KQtf:
|
139 |
+
xK = K.convert(x)
|
140 |
+
assert xK / K.one == xK
|
141 |
+
assert xK // K.one == xK
|
142 |
+
assert xK % K.one == K.zero
|
143 |
+
raises(ZeroDivisionError, lambda: xK / K.zero)
|
144 |
+
raises(ZeroDivisionError, lambda: xK // K.zero)
|
145 |
+
raises(ZeroDivisionError, lambda: xK % K.zero)
|
146 |
+
if K.is_Field:
|
147 |
+
assert xK / xK == K.one
|
148 |
+
assert xK // xK == K.one
|
149 |
+
assert xK % xK == K.zero
|
150 |
+
else:
|
151 |
+
raises(NotImplementedError, lambda: xK / xK)
|
152 |
+
raises(NotImplementedError, lambda: xK // xK)
|
153 |
+
raises(NotImplementedError, lambda: xK % xK)
|
154 |
+
|
155 |
+
|
156 |
+
def test_FiniteExtension_Poly():
|
157 |
+
K = FiniteExtension(Poly(x**2 - 2))
|
158 |
+
p = Poly(x, y, domain=K)
|
159 |
+
assert p.domain == K
|
160 |
+
assert p.as_expr() == x
|
161 |
+
assert (p**2).as_expr() == 2
|
162 |
+
|
163 |
+
K = FiniteExtension(Poly(x**2 - 2, x, domain=QQ))
|
164 |
+
K2 = FiniteExtension(Poly(t**2 - 2, t, domain=K))
|
165 |
+
assert str(K2) == 'QQ[x]/(x**2 - 2)[t]/(t**2 - 2)'
|
166 |
+
|
167 |
+
eK = K2.convert(x + t)
|
168 |
+
assert K2.to_sympy(eK) == x + t
|
169 |
+
assert K2.to_sympy(eK ** 2) == 4 + 2*x*t
|
170 |
+
p = Poly(x + t, y, domain=K2)
|
171 |
+
assert p**2 == Poly(4 + 2*x*t, y, domain=K2)
|
172 |
+
|
173 |
+
|
174 |
+
def test_FiniteExtension_sincos_jacobian():
|
175 |
+
# Use FiniteExtensino to compute the Jacobian of a matrix involving sin
|
176 |
+
# and cos of different symbols.
|
177 |
+
r, p, t = symbols('rho, phi, theta')
|
178 |
+
elements = [
|
179 |
+
[sin(p)*cos(t), r*cos(p)*cos(t), -r*sin(p)*sin(t)],
|
180 |
+
[sin(p)*sin(t), r*cos(p)*sin(t), r*sin(p)*cos(t)],
|
181 |
+
[ cos(p), -r*sin(p), 0],
|
182 |
+
]
|
183 |
+
|
184 |
+
def make_extension(K):
|
185 |
+
K = FiniteExtension(Poly(sin(p)**2+cos(p)**2-1, sin(p), domain=K[cos(p)]))
|
186 |
+
K = FiniteExtension(Poly(sin(t)**2+cos(t)**2-1, sin(t), domain=K[cos(t)]))
|
187 |
+
return K
|
188 |
+
|
189 |
+
Ksc1 = make_extension(ZZ[r])
|
190 |
+
Ksc2 = make_extension(ZZ)[r]
|
191 |
+
|
192 |
+
for K in [Ksc1, Ksc2]:
|
193 |
+
elements_K = [[K.convert(e) for e in row] for row in elements]
|
194 |
+
J = DomainMatrix(elements_K, (3, 3), K)
|
195 |
+
det = J.charpoly()[-1] * (-K.one)**3
|
196 |
+
assert det == K.convert(r**2*sin(p))
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_homomorphisms.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Tests for homomorphisms."""
|
2 |
+
|
3 |
+
from sympy.core.singleton import S
|
4 |
+
from sympy.polys.domains.rationalfield import QQ
|
5 |
+
from sympy.abc import x, y
|
6 |
+
from sympy.polys.agca import homomorphism
|
7 |
+
from sympy.testing.pytest import raises
|
8 |
+
|
9 |
+
|
10 |
+
def test_printing():
|
11 |
+
R = QQ.old_poly_ring(x)
|
12 |
+
|
13 |
+
assert str(homomorphism(R.free_module(1), R.free_module(1), [0])) == \
|
14 |
+
'Matrix([[0]]) : QQ[x]**1 -> QQ[x]**1'
|
15 |
+
assert str(homomorphism(R.free_module(2), R.free_module(2), [0, 0])) == \
|
16 |
+
'Matrix([ \n[0, 0], : QQ[x]**2 -> QQ[x]**2\n[0, 0]]) '
|
17 |
+
assert str(homomorphism(R.free_module(1), R.free_module(1) / [[x]], [0])) == \
|
18 |
+
'Matrix([[0]]) : QQ[x]**1 -> QQ[x]**1/<[x]>'
|
19 |
+
assert str(R.free_module(0).identity_hom()) == 'Matrix(0, 0, []) : QQ[x]**0 -> QQ[x]**0'
|
20 |
+
|
21 |
+
def test_operations():
|
22 |
+
F = QQ.old_poly_ring(x).free_module(2)
|
23 |
+
G = QQ.old_poly_ring(x).free_module(3)
|
24 |
+
f = F.identity_hom()
|
25 |
+
g = homomorphism(F, F, [0, [1, x]])
|
26 |
+
h = homomorphism(F, F, [[1, 0], 0])
|
27 |
+
i = homomorphism(F, G, [[1, 0, 0], [0, 1, 0]])
|
28 |
+
|
29 |
+
assert f == f
|
30 |
+
assert f != g
|
31 |
+
assert f != i
|
32 |
+
assert (f != F.identity_hom()) is False
|
33 |
+
assert 2*f == f*2 == homomorphism(F, F, [[2, 0], [0, 2]])
|
34 |
+
assert f/2 == homomorphism(F, F, [[S.Half, 0], [0, S.Half]])
|
35 |
+
assert f + g == homomorphism(F, F, [[1, 0], [1, x + 1]])
|
36 |
+
assert f - g == homomorphism(F, F, [[1, 0], [-1, 1 - x]])
|
37 |
+
assert f*g == g == g*f
|
38 |
+
assert h*g == homomorphism(F, F, [0, [1, 0]])
|
39 |
+
assert g*h == homomorphism(F, F, [0, 0])
|
40 |
+
assert i*f == i
|
41 |
+
assert f([1, 2]) == [1, 2]
|
42 |
+
assert g([1, 2]) == [2, 2*x]
|
43 |
+
|
44 |
+
assert i.restrict_domain(F.submodule([x, x]))([x, x]) == i([x, x])
|
45 |
+
h1 = h.quotient_domain(F.submodule([0, 1]))
|
46 |
+
assert h1([1, 0]) == h([1, 0])
|
47 |
+
assert h1.restrict_domain(h1.domain.submodule([x, 0]))([x, 0]) == h([x, 0])
|
48 |
+
|
49 |
+
raises(TypeError, lambda: f/g)
|
50 |
+
raises(TypeError, lambda: f + 1)
|
51 |
+
raises(TypeError, lambda: f + i)
|
52 |
+
raises(TypeError, lambda: f - 1)
|
53 |
+
raises(TypeError, lambda: f*i)
|
54 |
+
|
55 |
+
|
56 |
+
def test_creation():
|
57 |
+
F = QQ.old_poly_ring(x).free_module(3)
|
58 |
+
G = QQ.old_poly_ring(x).free_module(2)
|
59 |
+
SM = F.submodule([1, 1, 1])
|
60 |
+
Q = F / SM
|
61 |
+
SQ = Q.submodule([1, 0, 0])
|
62 |
+
|
63 |
+
matrix = [[1, 0], [0, 1], [-1, -1]]
|
64 |
+
h = homomorphism(F, G, matrix)
|
65 |
+
h2 = homomorphism(Q, G, matrix)
|
66 |
+
assert h.quotient_domain(SM) == h2
|
67 |
+
raises(ValueError, lambda: h.quotient_domain(F.submodule([1, 0, 0])))
|
68 |
+
assert h2.restrict_domain(SQ) == homomorphism(SQ, G, matrix)
|
69 |
+
raises(ValueError, lambda: h.restrict_domain(G))
|
70 |
+
raises(ValueError, lambda: h.restrict_codomain(G.submodule([1, 0])))
|
71 |
+
raises(ValueError, lambda: h.quotient_codomain(F))
|
72 |
+
|
73 |
+
im = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
74 |
+
for M in [F, SM, Q, SQ]:
|
75 |
+
assert M.identity_hom() == homomorphism(M, M, im)
|
76 |
+
assert SM.inclusion_hom() == homomorphism(SM, F, im)
|
77 |
+
assert SQ.inclusion_hom() == homomorphism(SQ, Q, im)
|
78 |
+
assert Q.quotient_hom() == homomorphism(F, Q, im)
|
79 |
+
assert SQ.quotient_hom() == homomorphism(SQ.base, SQ, im)
|
80 |
+
|
81 |
+
class conv:
|
82 |
+
def convert(x, y=None):
|
83 |
+
return x
|
84 |
+
|
85 |
+
class dummy:
|
86 |
+
container = conv()
|
87 |
+
|
88 |
+
def submodule(*args):
|
89 |
+
return None
|
90 |
+
raises(TypeError, lambda: homomorphism(dummy(), G, matrix))
|
91 |
+
raises(TypeError, lambda: homomorphism(F, dummy(), matrix))
|
92 |
+
raises(
|
93 |
+
ValueError, lambda: homomorphism(QQ.old_poly_ring(x, y).free_module(3), G, matrix))
|
94 |
+
raises(ValueError, lambda: homomorphism(F, G, [0, 0]))
|
95 |
+
|
96 |
+
|
97 |
+
def test_properties():
|
98 |
+
R = QQ.old_poly_ring(x, y)
|
99 |
+
F = R.free_module(2)
|
100 |
+
h = homomorphism(F, F, [[x, 0], [y, 0]])
|
101 |
+
assert h.kernel() == F.submodule([-y, x])
|
102 |
+
assert h.image() == F.submodule([x, 0], [y, 0])
|
103 |
+
assert not h.is_injective()
|
104 |
+
assert not h.is_surjective()
|
105 |
+
assert h.restrict_codomain(h.image()).is_surjective()
|
106 |
+
assert h.restrict_domain(F.submodule([1, 0])).is_injective()
|
107 |
+
assert h.quotient_domain(
|
108 |
+
h.kernel()).restrict_codomain(h.image()).is_isomorphism()
|
109 |
+
|
110 |
+
R2 = QQ.old_poly_ring(x, y, order=(("lex", x), ("ilex", y))) / [x**2 + 1]
|
111 |
+
F = R2.free_module(2)
|
112 |
+
h = homomorphism(F, F, [[x, 0], [y, y + 1]])
|
113 |
+
assert h.is_isomorphism()
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_ideals.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Test ideals.py code."""
|
2 |
+
|
3 |
+
from sympy.polys import QQ, ilex
|
4 |
+
from sympy.abc import x, y, z
|
5 |
+
from sympy.testing.pytest import raises
|
6 |
+
|
7 |
+
|
8 |
+
def test_ideal_operations():
|
9 |
+
R = QQ.old_poly_ring(x, y)
|
10 |
+
I = R.ideal(x)
|
11 |
+
J = R.ideal(y)
|
12 |
+
S = R.ideal(x*y)
|
13 |
+
T = R.ideal(x, y)
|
14 |
+
|
15 |
+
assert not (I == J)
|
16 |
+
assert I == I
|
17 |
+
|
18 |
+
assert I.union(J) == T
|
19 |
+
assert I + J == T
|
20 |
+
assert I + T == T
|
21 |
+
|
22 |
+
assert not I.subset(T)
|
23 |
+
assert T.subset(I)
|
24 |
+
|
25 |
+
assert I.product(J) == S
|
26 |
+
assert I*J == S
|
27 |
+
assert x*J == S
|
28 |
+
assert I*y == S
|
29 |
+
assert R.convert(x)*J == S
|
30 |
+
assert I*R.convert(y) == S
|
31 |
+
|
32 |
+
assert not I.is_zero()
|
33 |
+
assert not J.is_whole_ring()
|
34 |
+
|
35 |
+
assert R.ideal(x**2 + 1, x).is_whole_ring()
|
36 |
+
assert R.ideal() == R.ideal(0)
|
37 |
+
assert R.ideal().is_zero()
|
38 |
+
|
39 |
+
assert T.contains(x*y)
|
40 |
+
assert T.subset([x, y])
|
41 |
+
|
42 |
+
assert T.in_terms_of_generators(x) == [R(1), R(0)]
|
43 |
+
|
44 |
+
assert T**0 == R.ideal(1)
|
45 |
+
assert T**1 == T
|
46 |
+
assert T**2 == R.ideal(x**2, y**2, x*y)
|
47 |
+
assert I**5 == R.ideal(x**5)
|
48 |
+
|
49 |
+
|
50 |
+
def test_exceptions():
|
51 |
+
I = QQ.old_poly_ring(x).ideal(x)
|
52 |
+
J = QQ.old_poly_ring(y).ideal(1)
|
53 |
+
raises(ValueError, lambda: I.union(x))
|
54 |
+
raises(ValueError, lambda: I + J)
|
55 |
+
raises(ValueError, lambda: I * J)
|
56 |
+
raises(ValueError, lambda: I.union(J))
|
57 |
+
assert (I == J) is False
|
58 |
+
assert I != J
|
59 |
+
|
60 |
+
|
61 |
+
def test_nontriv_global():
|
62 |
+
R = QQ.old_poly_ring(x, y, z)
|
63 |
+
|
64 |
+
def contains(I, f):
|
65 |
+
return R.ideal(*I).contains(f)
|
66 |
+
|
67 |
+
assert contains([x, y], x)
|
68 |
+
assert contains([x, y], x + y)
|
69 |
+
assert not contains([x, y], 1)
|
70 |
+
assert not contains([x, y], z)
|
71 |
+
assert contains([x**2 + y, x**2 + x], x - y)
|
72 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
|
73 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**3)
|
74 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4)
|
75 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y**2)
|
76 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4 + y**3 + 2*z*y*x)
|
77 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y*z)
|
78 |
+
assert contains([x, 1 + x + y, 5 - 7*y], 1)
|
79 |
+
assert contains(
|
80 |
+
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
|
81 |
+
x**3)
|
82 |
+
assert not contains(
|
83 |
+
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
|
84 |
+
x**2 + y**2)
|
85 |
+
|
86 |
+
# compare local order
|
87 |
+
assert not contains([x*(1 + x + y), y*(1 + z)], x)
|
88 |
+
assert not contains([x*(1 + x + y), y*(1 + z)], x + y)
|
89 |
+
|
90 |
+
|
91 |
+
def test_nontriv_local():
|
92 |
+
R = QQ.old_poly_ring(x, y, z, order=ilex)
|
93 |
+
|
94 |
+
def contains(I, f):
|
95 |
+
return R.ideal(*I).contains(f)
|
96 |
+
|
97 |
+
assert contains([x, y], x)
|
98 |
+
assert contains([x, y], x + y)
|
99 |
+
assert not contains([x, y], 1)
|
100 |
+
assert not contains([x, y], z)
|
101 |
+
assert contains([x**2 + y, x**2 + x], x - y)
|
102 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
|
103 |
+
assert contains([x*(1 + x + y), y*(1 + z)], x)
|
104 |
+
assert contains([x*(1 + x + y), y*(1 + z)], x + y)
|
105 |
+
|
106 |
+
|
107 |
+
def test_intersection():
|
108 |
+
R = QQ.old_poly_ring(x, y, z)
|
109 |
+
# SCA, example 1.8.11
|
110 |
+
assert R.ideal(x, y).intersect(R.ideal(y**2, z)) == R.ideal(y**2, y*z, x*z)
|
111 |
+
|
112 |
+
assert R.ideal(x, y).intersect(R.ideal()).is_zero()
|
113 |
+
|
114 |
+
R = QQ.old_poly_ring(x, y, z, order="ilex")
|
115 |
+
assert R.ideal(x, y).intersect(R.ideal(y**2 + y**2*z, z + z*x**3*y)) == \
|
116 |
+
R.ideal(y**2, y*z, x*z)
|
117 |
+
|
118 |
+
|
119 |
+
def test_quotient():
|
120 |
+
# SCA, example 1.8.13
|
121 |
+
R = QQ.old_poly_ring(x, y, z)
|
122 |
+
assert R.ideal(x, y).quotient(R.ideal(y**2, z)) == R.ideal(x, y)
|
123 |
+
|
124 |
+
|
125 |
+
def test_reduction():
|
126 |
+
from sympy.polys.distributedmodules import sdm_nf_buchberger_reduced
|
127 |
+
R = QQ.old_poly_ring(x, y)
|
128 |
+
I = R.ideal(x**5, y)
|
129 |
+
e = R.convert(x**3 + y**2)
|
130 |
+
assert I.reduce_element(e) == e
|
131 |
+
assert I.reduce_element(e, NF=sdm_nf_buchberger_reduced) == R.convert(x**3)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/agca/tests/test_modules.py
ADDED
@@ -0,0 +1,408 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Test modules.py code."""
|
2 |
+
|
3 |
+
from sympy.polys.agca.modules import FreeModule, ModuleOrder, FreeModulePolyRing
|
4 |
+
from sympy.polys import CoercionFailed, QQ, lex, grlex, ilex, ZZ
|
5 |
+
from sympy.abc import x, y, z
|
6 |
+
from sympy.testing.pytest import raises
|
7 |
+
from sympy.core.numbers import Rational
|
8 |
+
|
9 |
+
|
10 |
+
def test_FreeModuleElement():
|
11 |
+
M = QQ.old_poly_ring(x).free_module(3)
|
12 |
+
e = M.convert([1, x, x**2])
|
13 |
+
f = [QQ.old_poly_ring(x).convert(1), QQ.old_poly_ring(x).convert(x), QQ.old_poly_ring(x).convert(x**2)]
|
14 |
+
assert list(e) == f
|
15 |
+
assert f[0] == e[0]
|
16 |
+
assert f[1] == e[1]
|
17 |
+
assert f[2] == e[2]
|
18 |
+
raises(IndexError, lambda: e[3])
|
19 |
+
|
20 |
+
g = M.convert([x, 0, 0])
|
21 |
+
assert e + g == M.convert([x + 1, x, x**2])
|
22 |
+
assert f + g == M.convert([x + 1, x, x**2])
|
23 |
+
assert -e == M.convert([-1, -x, -x**2])
|
24 |
+
assert e - g == M.convert([1 - x, x, x**2])
|
25 |
+
assert e != g
|
26 |
+
|
27 |
+
assert M.convert([x, x, x]) / QQ.old_poly_ring(x).convert(x) == [1, 1, 1]
|
28 |
+
R = QQ.old_poly_ring(x, order="ilex")
|
29 |
+
assert R.free_module(1).convert([x]) / R.convert(x) == [1]
|
30 |
+
|
31 |
+
|
32 |
+
def test_FreeModule():
|
33 |
+
M1 = FreeModule(QQ.old_poly_ring(x), 2)
|
34 |
+
assert M1 == FreeModule(QQ.old_poly_ring(x), 2)
|
35 |
+
assert M1 != FreeModule(QQ.old_poly_ring(y), 2)
|
36 |
+
assert M1 != FreeModule(QQ.old_poly_ring(x), 3)
|
37 |
+
M2 = FreeModule(QQ.old_poly_ring(x, order="ilex"), 2)
|
38 |
+
|
39 |
+
assert [x, 1] in M1
|
40 |
+
assert [x] not in M1
|
41 |
+
assert [2, y] not in M1
|
42 |
+
assert [1/(x + 1), 2] not in M1
|
43 |
+
|
44 |
+
e = M1.convert([x, x**2 + 1])
|
45 |
+
X = QQ.old_poly_ring(x).convert(x)
|
46 |
+
assert e == [X, X**2 + 1]
|
47 |
+
assert e == [x, x**2 + 1]
|
48 |
+
assert 2*e == [2*x, 2*x**2 + 2]
|
49 |
+
assert e*2 == [2*x, 2*x**2 + 2]
|
50 |
+
assert e/2 == [x/2, (x**2 + 1)/2]
|
51 |
+
assert x*e == [x**2, x**3 + x]
|
52 |
+
assert e*x == [x**2, x**3 + x]
|
53 |
+
assert X*e == [x**2, x**3 + x]
|
54 |
+
assert e*X == [x**2, x**3 + x]
|
55 |
+
|
56 |
+
assert [x, 1] in M2
|
57 |
+
assert [x] not in M2
|
58 |
+
assert [2, y] not in M2
|
59 |
+
assert [1/(x + 1), 2] in M2
|
60 |
+
|
61 |
+
e = M2.convert([x, x**2 + 1])
|
62 |
+
X = QQ.old_poly_ring(x, order="ilex").convert(x)
|
63 |
+
assert e == [X, X**2 + 1]
|
64 |
+
assert e == [x, x**2 + 1]
|
65 |
+
assert 2*e == [2*x, 2*x**2 + 2]
|
66 |
+
assert e*2 == [2*x, 2*x**2 + 2]
|
67 |
+
assert e/2 == [x/2, (x**2 + 1)/2]
|
68 |
+
assert x*e == [x**2, x**3 + x]
|
69 |
+
assert e*x == [x**2, x**3 + x]
|
70 |
+
assert e/(1 + x) == [x/(1 + x), (x**2 + 1)/(1 + x)]
|
71 |
+
assert X*e == [x**2, x**3 + x]
|
72 |
+
assert e*X == [x**2, x**3 + x]
|
73 |
+
|
74 |
+
M3 = FreeModule(QQ.old_poly_ring(x, y), 2)
|
75 |
+
assert M3.convert(e) == M3.convert([x, x**2 + 1])
|
76 |
+
|
77 |
+
assert not M3.is_submodule(0)
|
78 |
+
assert not M3.is_zero()
|
79 |
+
|
80 |
+
raises(NotImplementedError, lambda: ZZ.old_poly_ring(x).free_module(2))
|
81 |
+
raises(NotImplementedError, lambda: FreeModulePolyRing(ZZ, 2))
|
82 |
+
raises(CoercionFailed, lambda: M1.convert(QQ.old_poly_ring(x).free_module(3)
|
83 |
+
.convert([1, 2, 3])))
|
84 |
+
raises(CoercionFailed, lambda: M3.convert(1))
|
85 |
+
|
86 |
+
|
87 |
+
def test_ModuleOrder():
|
88 |
+
o1 = ModuleOrder(lex, grlex, False)
|
89 |
+
o2 = ModuleOrder(ilex, lex, False)
|
90 |
+
|
91 |
+
assert o1 == ModuleOrder(lex, grlex, False)
|
92 |
+
assert (o1 != ModuleOrder(lex, grlex, False)) is False
|
93 |
+
assert o1 != o2
|
94 |
+
|
95 |
+
assert o1((1, 2, 3)) == (1, (5, (2, 3)))
|
96 |
+
assert o2((1, 2, 3)) == (-1, (2, 3))
|
97 |
+
|
98 |
+
|
99 |
+
def test_SubModulePolyRing_global():
|
100 |
+
R = QQ.old_poly_ring(x, y)
|
101 |
+
F = R.free_module(3)
|
102 |
+
Fd = F.submodule([1, 0, 0], [1, 2, 0], [1, 2, 3])
|
103 |
+
M = F.submodule([x**2 + y**2, 1, 0], [x, y, 1])
|
104 |
+
|
105 |
+
assert F == Fd
|
106 |
+
assert Fd == F
|
107 |
+
assert F != M
|
108 |
+
assert M != F
|
109 |
+
assert Fd != M
|
110 |
+
assert M != Fd
|
111 |
+
assert Fd == F.submodule(*F.basis())
|
112 |
+
|
113 |
+
assert Fd.is_full_module()
|
114 |
+
assert not M.is_full_module()
|
115 |
+
assert not Fd.is_zero()
|
116 |
+
assert not M.is_zero()
|
117 |
+
assert Fd.submodule().is_zero()
|
118 |
+
|
119 |
+
assert M.contains([x**2 + y**2 + x, 1 + y, 1])
|
120 |
+
assert not M.contains([x**2 + y**2 + x, 1 + y, 2])
|
121 |
+
assert M.contains([y**2, 1 - x*y, -x])
|
122 |
+
|
123 |
+
assert not F.submodule([1 + x, 0, 0]) == F.submodule([1, 0, 0])
|
124 |
+
assert F.submodule([1, 0, 0], [0, 1, 0]).union(F.submodule([0, 0, 1])) == F
|
125 |
+
assert not M.is_submodule(0)
|
126 |
+
|
127 |
+
m = F.convert([x**2 + y**2, 1, 0])
|
128 |
+
n = M.convert(m)
|
129 |
+
assert m.module is F
|
130 |
+
assert n.module is M
|
131 |
+
|
132 |
+
raises(ValueError, lambda: M.submodule([1, 0, 0]))
|
133 |
+
raises(TypeError, lambda: M.union(1))
|
134 |
+
raises(ValueError, lambda: M.union(R.free_module(1).submodule([x])))
|
135 |
+
|
136 |
+
assert F.submodule([x, x, x]) != F.submodule([x, x, x], order="ilex")
|
137 |
+
|
138 |
+
|
139 |
+
def test_SubModulePolyRing_local():
|
140 |
+
R = QQ.old_poly_ring(x, y, order=ilex)
|
141 |
+
F = R.free_module(3)
|
142 |
+
Fd = F.submodule([1 + x, 0, 0], [1 + y, 2 + 2*y, 0], [1, 2, 3])
|
143 |
+
M = F.submodule([x**2 + y**2, 1, 0], [x, y, 1])
|
144 |
+
|
145 |
+
assert F == Fd
|
146 |
+
assert Fd == F
|
147 |
+
assert F != M
|
148 |
+
assert M != F
|
149 |
+
assert Fd != M
|
150 |
+
assert M != Fd
|
151 |
+
assert Fd == F.submodule(*F.basis())
|
152 |
+
|
153 |
+
assert Fd.is_full_module()
|
154 |
+
assert not M.is_full_module()
|
155 |
+
assert not Fd.is_zero()
|
156 |
+
assert not M.is_zero()
|
157 |
+
assert Fd.submodule().is_zero()
|
158 |
+
|
159 |
+
assert M.contains([x**2 + y**2 + x, 1 + y, 1])
|
160 |
+
assert not M.contains([x**2 + y**2 + x, 1 + y, 2])
|
161 |
+
assert M.contains([y**2, 1 - x*y, -x])
|
162 |
+
|
163 |
+
assert F.submodule([1 + x, 0, 0]) == F.submodule([1, 0, 0])
|
164 |
+
assert F.submodule(
|
165 |
+
[1, 0, 0], [0, 1, 0]).union(F.submodule([0, 0, 1 + x*y])) == F
|
166 |
+
|
167 |
+
raises(ValueError, lambda: M.submodule([1, 0, 0]))
|
168 |
+
|
169 |
+
|
170 |
+
def test_SubModulePolyRing_nontriv_global():
|
171 |
+
R = QQ.old_poly_ring(x, y, z)
|
172 |
+
F = R.free_module(1)
|
173 |
+
|
174 |
+
def contains(I, f):
|
175 |
+
return F.submodule(*[[g] for g in I]).contains([f])
|
176 |
+
|
177 |
+
assert contains([x, y], x)
|
178 |
+
assert contains([x, y], x + y)
|
179 |
+
assert not contains([x, y], 1)
|
180 |
+
assert not contains([x, y], z)
|
181 |
+
assert contains([x**2 + y, x**2 + x], x - y)
|
182 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
|
183 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**3)
|
184 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4)
|
185 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y**2)
|
186 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4 + y**3 + 2*z*y*x)
|
187 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y*z)
|
188 |
+
assert contains([x, 1 + x + y, 5 - 7*y], 1)
|
189 |
+
assert contains(
|
190 |
+
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
|
191 |
+
x**3)
|
192 |
+
assert not contains(
|
193 |
+
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
|
194 |
+
x**2 + y**2)
|
195 |
+
|
196 |
+
# compare local order
|
197 |
+
assert not contains([x*(1 + x + y), y*(1 + z)], x)
|
198 |
+
assert not contains([x*(1 + x + y), y*(1 + z)], x + y)
|
199 |
+
|
200 |
+
|
201 |
+
def test_SubModulePolyRing_nontriv_local():
|
202 |
+
R = QQ.old_poly_ring(x, y, z, order=ilex)
|
203 |
+
F = R.free_module(1)
|
204 |
+
|
205 |
+
def contains(I, f):
|
206 |
+
return F.submodule(*[[g] for g in I]).contains([f])
|
207 |
+
|
208 |
+
assert contains([x, y], x)
|
209 |
+
assert contains([x, y], x + y)
|
210 |
+
assert not contains([x, y], 1)
|
211 |
+
assert not contains([x, y], z)
|
212 |
+
assert contains([x**2 + y, x**2 + x], x - y)
|
213 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
|
214 |
+
assert contains([x*(1 + x + y), y*(1 + z)], x)
|
215 |
+
assert contains([x*(1 + x + y), y*(1 + z)], x + y)
|
216 |
+
|
217 |
+
|
218 |
+
def test_syzygy():
|
219 |
+
R = QQ.old_poly_ring(x, y, z)
|
220 |
+
M = R.free_module(1).submodule([x*y], [y*z], [x*z])
|
221 |
+
S = R.free_module(3).submodule([0, x, -y], [z, -x, 0])
|
222 |
+
assert M.syzygy_module() == S
|
223 |
+
|
224 |
+
M2 = M / ([x*y*z],)
|
225 |
+
S2 = R.free_module(3).submodule([z, 0, 0], [0, x, 0], [0, 0, y])
|
226 |
+
assert M2.syzygy_module() == S2
|
227 |
+
|
228 |
+
F = R.free_module(3)
|
229 |
+
assert F.submodule(*F.basis()).syzygy_module() == F.submodule()
|
230 |
+
|
231 |
+
R2 = QQ.old_poly_ring(x, y, z) / [x*y*z]
|
232 |
+
M3 = R2.free_module(1).submodule([x*y], [y*z], [x*z])
|
233 |
+
S3 = R2.free_module(3).submodule([z, 0, 0], [0, x, 0], [0, 0, y])
|
234 |
+
assert M3.syzygy_module() == S3
|
235 |
+
|
236 |
+
|
237 |
+
def test_in_terms_of_generators():
|
238 |
+
R = QQ.old_poly_ring(x, order="ilex")
|
239 |
+
M = R.free_module(2).submodule([2*x, 0], [1, 2])
|
240 |
+
assert M.in_terms_of_generators(
|
241 |
+
[x, x]) == [R.convert(Rational(1, 4)), R.convert(x/2)]
|
242 |
+
raises(ValueError, lambda: M.in_terms_of_generators([1, 0]))
|
243 |
+
|
244 |
+
M = R.free_module(2) / ([x, 0], [1, 1])
|
245 |
+
SM = M.submodule([1, x])
|
246 |
+
assert SM.in_terms_of_generators([2, 0]) == [R.convert(-2/(x - 1))]
|
247 |
+
|
248 |
+
R = QQ.old_poly_ring(x, y) / [x**2 - y**2]
|
249 |
+
M = R.free_module(2)
|
250 |
+
SM = M.submodule([x, 0], [0, y])
|
251 |
+
assert SM.in_terms_of_generators(
|
252 |
+
[x**2, x**2]) == [R.convert(x), R.convert(y)]
|
253 |
+
|
254 |
+
|
255 |
+
def test_QuotientModuleElement():
|
256 |
+
R = QQ.old_poly_ring(x)
|
257 |
+
F = R.free_module(3)
|
258 |
+
N = F.submodule([1, x, x**2])
|
259 |
+
M = F/N
|
260 |
+
e = M.convert([x**2, 2, 0])
|
261 |
+
|
262 |
+
assert M.convert([x + 1, x**2 + x, x**3 + x**2]) == 0
|
263 |
+
assert e == [x**2, 2, 0] + N == F.convert([x**2, 2, 0]) + N == \
|
264 |
+
M.convert(F.convert([x**2, 2, 0]))
|
265 |
+
|
266 |
+
assert M.convert([x**2 + 1, 2*x + 2, x**2]) == e + [0, x, 0] == \
|
267 |
+
e + M.convert([0, x, 0]) == e + F.convert([0, x, 0])
|
268 |
+
assert M.convert([x**2 + 1, 2, x**2]) == e - [0, x, 0] == \
|
269 |
+
e - M.convert([0, x, 0]) == e - F.convert([0, x, 0])
|
270 |
+
assert M.convert([0, 2, 0]) == M.convert([x**2, 4, 0]) - e == \
|
271 |
+
[x**2, 4, 0] - e == F.convert([x**2, 4, 0]) - e
|
272 |
+
assert M.convert([x**3 + x**2, 2*x + 2, 0]) == (1 + x)*e == \
|
273 |
+
R.convert(1 + x)*e == e*(1 + x) == e*R.convert(1 + x)
|
274 |
+
assert -e == [-x**2, -2, 0]
|
275 |
+
|
276 |
+
f = [x, x, 0] + N
|
277 |
+
assert M.convert([1, 1, 0]) == f / x == f / R.convert(x)
|
278 |
+
|
279 |
+
M2 = F/[(2, 2*x, 2*x**2), (0, 0, 1)]
|
280 |
+
G = R.free_module(2)
|
281 |
+
M3 = G/[[1, x]]
|
282 |
+
M4 = F.submodule([1, x, x**2], [1, 0, 0]) / N
|
283 |
+
raises(CoercionFailed, lambda: M.convert(G.convert([1, x])))
|
284 |
+
raises(CoercionFailed, lambda: M.convert(M3.convert([1, x])))
|
285 |
+
raises(CoercionFailed, lambda: M.convert(M2.convert([1, x, x])))
|
286 |
+
assert M2.convert(M.convert([2, x, x**2])) == [2, x, 0]
|
287 |
+
assert M.convert(M4.convert([2, 0, 0])) == [2, 0, 0]
|
288 |
+
|
289 |
+
|
290 |
+
def test_QuotientModule():
|
291 |
+
R = QQ.old_poly_ring(x)
|
292 |
+
F = R.free_module(3)
|
293 |
+
N = F.submodule([1, x, x**2])
|
294 |
+
M = F/N
|
295 |
+
|
296 |
+
assert M != F
|
297 |
+
assert M != N
|
298 |
+
assert M == F / [(1, x, x**2)]
|
299 |
+
assert not M.is_zero()
|
300 |
+
assert (F / F.basis()).is_zero()
|
301 |
+
|
302 |
+
SQ = F.submodule([1, x, x**2], [2, 0, 0]) / N
|
303 |
+
assert SQ == M.submodule([2, x, x**2])
|
304 |
+
assert SQ != M.submodule([2, 1, 0])
|
305 |
+
assert SQ != M
|
306 |
+
assert M.is_submodule(SQ)
|
307 |
+
assert not SQ.is_full_module()
|
308 |
+
|
309 |
+
raises(ValueError, lambda: N/F)
|
310 |
+
raises(ValueError, lambda: F.submodule([2, 0, 0]) / N)
|
311 |
+
raises(ValueError, lambda: R.free_module(2)/F)
|
312 |
+
raises(CoercionFailed, lambda: F.convert(M.convert([1, x, x**2])))
|
313 |
+
|
314 |
+
M1 = F / [[1, 1, 1]]
|
315 |
+
M2 = M1.submodule([1, 0, 0], [0, 1, 0])
|
316 |
+
assert M1 == M2
|
317 |
+
|
318 |
+
|
319 |
+
def test_ModulesQuotientRing():
|
320 |
+
R = QQ.old_poly_ring(x, y, order=(("lex", x), ("ilex", y))) / [x**2 + 1]
|
321 |
+
M1 = R.free_module(2)
|
322 |
+
assert M1 == R.free_module(2)
|
323 |
+
assert M1 != QQ.old_poly_ring(x).free_module(2)
|
324 |
+
assert M1 != R.free_module(3)
|
325 |
+
|
326 |
+
assert [x, 1] in M1
|
327 |
+
assert [x] not in M1
|
328 |
+
assert [1/(R.convert(x) + 1), 2] in M1
|
329 |
+
assert [1, 2/(1 + y)] in M1
|
330 |
+
assert [1, 2/y] not in M1
|
331 |
+
|
332 |
+
assert M1.convert([x**2, y]) == [-1, y]
|
333 |
+
|
334 |
+
F = R.free_module(3)
|
335 |
+
Fd = F.submodule([x**2, 0, 0], [1, 2, 0], [1, 2, 3])
|
336 |
+
M = F.submodule([x**2 + y**2, 1, 0], [x, y, 1])
|
337 |
+
|
338 |
+
assert F == Fd
|
339 |
+
assert Fd == F
|
340 |
+
assert F != M
|
341 |
+
assert M != F
|
342 |
+
assert Fd != M
|
343 |
+
assert M != Fd
|
344 |
+
assert Fd == F.submodule(*F.basis())
|
345 |
+
|
346 |
+
assert Fd.is_full_module()
|
347 |
+
assert not M.is_full_module()
|
348 |
+
assert not Fd.is_zero()
|
349 |
+
assert not M.is_zero()
|
350 |
+
assert Fd.submodule().is_zero()
|
351 |
+
|
352 |
+
assert M.contains([x**2 + y**2 + x, -x**2 + y, 1])
|
353 |
+
assert not M.contains([x**2 + y**2 + x, 1 + y, 2])
|
354 |
+
assert M.contains([y**2, 1 - x*y, -x])
|
355 |
+
|
356 |
+
assert F.submodule([x, 0, 0]) == F.submodule([1, 0, 0])
|
357 |
+
assert not F.submodule([y, 0, 0]) == F.submodule([1, 0, 0])
|
358 |
+
assert F.submodule([1, 0, 0], [0, 1, 0]).union(F.submodule([0, 0, 1])) == F
|
359 |
+
assert not M.is_submodule(0)
|
360 |
+
|
361 |
+
|
362 |
+
def test_module_mul():
|
363 |
+
R = QQ.old_poly_ring(x)
|
364 |
+
M = R.free_module(2)
|
365 |
+
S1 = M.submodule([x, 0], [0, x])
|
366 |
+
S2 = M.submodule([x**2, 0], [0, x**2])
|
367 |
+
I = R.ideal(x)
|
368 |
+
|
369 |
+
assert I*M == M*I == S1 == x*M == M*x
|
370 |
+
assert I*S1 == S2 == x*S1
|
371 |
+
|
372 |
+
|
373 |
+
def test_intersection():
|
374 |
+
# SCA, example 2.8.5
|
375 |
+
F = QQ.old_poly_ring(x, y).free_module(2)
|
376 |
+
M1 = F.submodule([x, y], [y, 1])
|
377 |
+
M2 = F.submodule([0, y - 1], [x, 1], [y, x])
|
378 |
+
I = F.submodule([x, y], [y**2 - y, y - 1], [x*y + y, x + 1])
|
379 |
+
I1, rel1, rel2 = M1.intersect(M2, relations=True)
|
380 |
+
assert I1 == M2.intersect(M1) == I
|
381 |
+
for i, g in enumerate(I1.gens):
|
382 |
+
assert g == sum(c*x for c, x in zip(rel1[i], M1.gens)) \
|
383 |
+
== sum(d*y for d, y in zip(rel2[i], M2.gens))
|
384 |
+
|
385 |
+
assert F.submodule([x, y]).intersect(F.submodule([y, x])).is_zero()
|
386 |
+
|
387 |
+
|
388 |
+
def test_quotient():
|
389 |
+
# SCA, example 2.8.6
|
390 |
+
R = QQ.old_poly_ring(x, y, z)
|
391 |
+
F = R.free_module(2)
|
392 |
+
assert F.submodule([x*y, x*z], [y*z, x*y]).module_quotient(
|
393 |
+
F.submodule([y, z], [z, y])) == QQ.old_poly_ring(x, y, z).ideal(x**2*y**2 - x*y*z**2)
|
394 |
+
assert F.submodule([x, y]).module_quotient(F.submodule()).is_whole_ring()
|
395 |
+
|
396 |
+
M = F.submodule([x**2, x**2], [y**2, y**2])
|
397 |
+
N = F.submodule([x + y, x + y])
|
398 |
+
q, rel = M.module_quotient(N, relations=True)
|
399 |
+
assert q == R.ideal(y**2, x - y)
|
400 |
+
for i, g in enumerate(q.gens):
|
401 |
+
assert g*N.gens[0] == sum(c*x for c, x in zip(rel[i], M.gens))
|
402 |
+
|
403 |
+
|
404 |
+
def test_groebner_extendend():
|
405 |
+
M = QQ.old_poly_ring(x, y, z).free_module(3).submodule([x + 1, y, 1], [x*y, z, z**2])
|
406 |
+
G, R = M._groebner_vec(extended=True)
|
407 |
+
for i, g in enumerate(G):
|
408 |
+
assert g == sum(c*gen for c, gen in zip(R[i], M.gens))
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__init__.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
|
3 |
+
sympy.polys.matrices package.
|
4 |
+
|
5 |
+
The main export from this package is the DomainMatrix class which is a
|
6 |
+
lower-level implementation of matrices based on the polys Domains. This
|
7 |
+
implementation is typically a lot faster than SymPy's standard Matrix class
|
8 |
+
but is a work in progress and is still experimental.
|
9 |
+
|
10 |
+
"""
|
11 |
+
from .domainmatrix import DomainMatrix, DM
|
12 |
+
|
13 |
+
__all__ = [
|
14 |
+
'DomainMatrix', 'DM',
|
15 |
+
]
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (604 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/_typing.cpython-310.pyc
ADDED
Binary file (1.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/ddm.cpython-310.pyc
ADDED
Binary file (17.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/dense.cpython-310.pyc
ADDED
Binary file (8.91 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.cpython-310.pyc
ADDED
Binary file (51.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainscalar.cpython-310.pyc
ADDED
Binary file (4.37 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/eigen.cpython-310.pyc
ADDED
Binary file (3.78 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/exceptions.cpython-310.pyc
ADDED
Binary file (2.12 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/linsolve.cpython-310.pyc
ADDED
Binary file (5.45 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/lll.cpython-310.pyc
ADDED
Binary file (4.88 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/normalforms.cpython-310.pyc
ADDED
Binary file (11 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/sdm.cpython-310.pyc
ADDED
Binary file (34.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/_typing.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import TypeVar, Protocol
|
2 |
+
|
3 |
+
|
4 |
+
T = TypeVar('T')
|
5 |
+
|
6 |
+
|
7 |
+
class RingElement(Protocol):
|
8 |
+
def __add__(self: T, other: T, /) -> T: ...
|
9 |
+
def __sub__(self: T, other: T, /) -> T: ...
|
10 |
+
def __mul__(self: T, other: T, /) -> T: ...
|
11 |
+
def __pow__(self: T, other: int, /) -> T: ...
|
12 |
+
def __neg__(self: T, /) -> T: ...
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/ddm.py
ADDED
@@ -0,0 +1,496 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
|
3 |
+
Module for the DDM class.
|
4 |
+
|
5 |
+
The DDM class is an internal representation used by DomainMatrix. The letters
|
6 |
+
DDM stand for Dense Domain Matrix. A DDM instance represents a matrix using
|
7 |
+
elements from a polynomial Domain (e.g. ZZ, QQ, ...) in a dense-matrix
|
8 |
+
representation.
|
9 |
+
|
10 |
+
Basic usage:
|
11 |
+
|
12 |
+
>>> from sympy import ZZ, QQ
|
13 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
14 |
+
>>> A = DDM([[ZZ(0), ZZ(1)], [ZZ(-1), ZZ(0)]], (2, 2), ZZ)
|
15 |
+
>>> A.shape
|
16 |
+
(2, 2)
|
17 |
+
>>> A
|
18 |
+
[[0, 1], [-1, 0]]
|
19 |
+
>>> type(A)
|
20 |
+
<class 'sympy.polys.matrices.ddm.DDM'>
|
21 |
+
>>> A @ A
|
22 |
+
[[-1, 0], [0, -1]]
|
23 |
+
|
24 |
+
The ddm_* functions are designed to operate on DDM as well as on an ordinary
|
25 |
+
list of lists:
|
26 |
+
|
27 |
+
>>> from sympy.polys.matrices.dense import ddm_idet
|
28 |
+
>>> ddm_idet(A, QQ)
|
29 |
+
1
|
30 |
+
>>> ddm_idet([[0, 1], [-1, 0]], QQ)
|
31 |
+
1
|
32 |
+
>>> A
|
33 |
+
[[-1, 0], [0, -1]]
|
34 |
+
|
35 |
+
Note that ddm_idet modifies the input matrix in-place. It is recommended to
|
36 |
+
use the DDM.det method as a friendlier interface to this instead which takes
|
37 |
+
care of copying the matrix:
|
38 |
+
|
39 |
+
>>> B = DDM([[ZZ(0), ZZ(1)], [ZZ(-1), ZZ(0)]], (2, 2), ZZ)
|
40 |
+
>>> B.det()
|
41 |
+
1
|
42 |
+
|
43 |
+
Normally DDM would not be used directly and is just part of the internal
|
44 |
+
representation of DomainMatrix which adds further functionality including e.g.
|
45 |
+
unifying domains.
|
46 |
+
|
47 |
+
The dense format used by DDM is a list of lists of elements e.g. the 2x2
|
48 |
+
identity matrix is like [[1, 0], [0, 1]]. The DDM class itself is a subclass
|
49 |
+
of list and its list items are plain lists. Elements are accessed as e.g.
|
50 |
+
ddm[i][j] where ddm[i] gives the ith row and ddm[i][j] gets the element in the
|
51 |
+
jth column of that row. Subclassing list makes e.g. iteration and indexing
|
52 |
+
very efficient. We do not override __getitem__ because it would lose that
|
53 |
+
benefit.
|
54 |
+
|
55 |
+
The core routines are implemented by the ddm_* functions defined in dense.py.
|
56 |
+
Those functions are intended to be able to operate on a raw list-of-lists
|
57 |
+
representation of matrices with most functions operating in-place. The DDM
|
58 |
+
class takes care of copying etc and also stores a Domain object associated
|
59 |
+
with its elements. This makes it possible to implement things like A + B with
|
60 |
+
domain checking and also shape checking so that the list of lists
|
61 |
+
representation is friendlier.
|
62 |
+
|
63 |
+
"""
|
64 |
+
from itertools import chain
|
65 |
+
|
66 |
+
from .exceptions import DMBadInputError, DMShapeError, DMDomainError
|
67 |
+
|
68 |
+
from .dense import (
|
69 |
+
ddm_transpose,
|
70 |
+
ddm_iadd,
|
71 |
+
ddm_isub,
|
72 |
+
ddm_ineg,
|
73 |
+
ddm_imul,
|
74 |
+
ddm_irmul,
|
75 |
+
ddm_imatmul,
|
76 |
+
ddm_irref,
|
77 |
+
ddm_idet,
|
78 |
+
ddm_iinv,
|
79 |
+
ddm_ilu_split,
|
80 |
+
ddm_ilu_solve,
|
81 |
+
ddm_berk,
|
82 |
+
)
|
83 |
+
|
84 |
+
from sympy.polys.domains import QQ
|
85 |
+
from .lll import ddm_lll, ddm_lll_transform
|
86 |
+
|
87 |
+
|
88 |
+
class DDM(list):
|
89 |
+
"""Dense matrix based on polys domain elements
|
90 |
+
|
91 |
+
This is a list subclass and is a wrapper for a list of lists that supports
|
92 |
+
basic matrix arithmetic +, -, *, **.
|
93 |
+
"""
|
94 |
+
|
95 |
+
fmt = 'dense'
|
96 |
+
|
97 |
+
def __init__(self, rowslist, shape, domain):
|
98 |
+
super().__init__(rowslist)
|
99 |
+
self.shape = self.rows, self.cols = m, n = shape
|
100 |
+
self.domain = domain
|
101 |
+
|
102 |
+
if not (len(self) == m and all(len(row) == n for row in self)):
|
103 |
+
raise DMBadInputError("Inconsistent row-list/shape")
|
104 |
+
|
105 |
+
def getitem(self, i, j):
|
106 |
+
return self[i][j]
|
107 |
+
|
108 |
+
def setitem(self, i, j, value):
|
109 |
+
self[i][j] = value
|
110 |
+
|
111 |
+
def extract_slice(self, slice1, slice2):
|
112 |
+
ddm = [row[slice2] for row in self[slice1]]
|
113 |
+
rows = len(ddm)
|
114 |
+
cols = len(ddm[0]) if ddm else len(range(self.shape[1])[slice2])
|
115 |
+
return DDM(ddm, (rows, cols), self.domain)
|
116 |
+
|
117 |
+
def extract(self, rows, cols):
|
118 |
+
ddm = []
|
119 |
+
for i in rows:
|
120 |
+
rowi = self[i]
|
121 |
+
ddm.append([rowi[j] for j in cols])
|
122 |
+
return DDM(ddm, (len(rows), len(cols)), self.domain)
|
123 |
+
|
124 |
+
def to_list(self):
|
125 |
+
return list(self)
|
126 |
+
|
127 |
+
def to_list_flat(self):
|
128 |
+
flat = []
|
129 |
+
for row in self:
|
130 |
+
flat.extend(row)
|
131 |
+
return flat
|
132 |
+
|
133 |
+
def flatiter(self):
|
134 |
+
return chain.from_iterable(self)
|
135 |
+
|
136 |
+
def flat(self):
|
137 |
+
items = []
|
138 |
+
for row in self:
|
139 |
+
items.extend(row)
|
140 |
+
return items
|
141 |
+
|
142 |
+
def to_dok(self):
|
143 |
+
return {(i, j): e for i, row in enumerate(self) for j, e in enumerate(row)}
|
144 |
+
|
145 |
+
def to_ddm(self):
|
146 |
+
return self
|
147 |
+
|
148 |
+
def to_sdm(self):
|
149 |
+
return SDM.from_list(self, self.shape, self.domain)
|
150 |
+
|
151 |
+
def convert_to(self, K):
|
152 |
+
Kold = self.domain
|
153 |
+
if K == Kold:
|
154 |
+
return self.copy()
|
155 |
+
rows = ([K.convert_from(e, Kold) for e in row] for row in self)
|
156 |
+
return DDM(rows, self.shape, K)
|
157 |
+
|
158 |
+
def __str__(self):
|
159 |
+
rowsstr = ['[%s]' % ', '.join(map(str, row)) for row in self]
|
160 |
+
return '[%s]' % ', '.join(rowsstr)
|
161 |
+
|
162 |
+
def __repr__(self):
|
163 |
+
cls = type(self).__name__
|
164 |
+
rows = list.__repr__(self)
|
165 |
+
return '%s(%s, %s, %s)' % (cls, rows, self.shape, self.domain)
|
166 |
+
|
167 |
+
def __eq__(self, other):
|
168 |
+
if not isinstance(other, DDM):
|
169 |
+
return False
|
170 |
+
return (super().__eq__(other) and self.domain == other.domain)
|
171 |
+
|
172 |
+
def __ne__(self, other):
|
173 |
+
return not self.__eq__(other)
|
174 |
+
|
175 |
+
@classmethod
|
176 |
+
def zeros(cls, shape, domain):
|
177 |
+
z = domain.zero
|
178 |
+
m, n = shape
|
179 |
+
rowslist = ([z] * n for _ in range(m))
|
180 |
+
return DDM(rowslist, shape, domain)
|
181 |
+
|
182 |
+
@classmethod
|
183 |
+
def ones(cls, shape, domain):
|
184 |
+
one = domain.one
|
185 |
+
m, n = shape
|
186 |
+
rowlist = ([one] * n for _ in range(m))
|
187 |
+
return DDM(rowlist, shape, domain)
|
188 |
+
|
189 |
+
@classmethod
|
190 |
+
def eye(cls, size, domain):
|
191 |
+
one = domain.one
|
192 |
+
ddm = cls.zeros((size, size), domain)
|
193 |
+
for i in range(size):
|
194 |
+
ddm[i][i] = one
|
195 |
+
return ddm
|
196 |
+
|
197 |
+
def copy(self):
|
198 |
+
copyrows = (row[:] for row in self)
|
199 |
+
return DDM(copyrows, self.shape, self.domain)
|
200 |
+
|
201 |
+
def transpose(self):
|
202 |
+
rows, cols = self.shape
|
203 |
+
if rows:
|
204 |
+
ddmT = ddm_transpose(self)
|
205 |
+
else:
|
206 |
+
ddmT = [[]] * cols
|
207 |
+
return DDM(ddmT, (cols, rows), self.domain)
|
208 |
+
|
209 |
+
def __add__(a, b):
|
210 |
+
if not isinstance(b, DDM):
|
211 |
+
return NotImplemented
|
212 |
+
return a.add(b)
|
213 |
+
|
214 |
+
def __sub__(a, b):
|
215 |
+
if not isinstance(b, DDM):
|
216 |
+
return NotImplemented
|
217 |
+
return a.sub(b)
|
218 |
+
|
219 |
+
def __neg__(a):
|
220 |
+
return a.neg()
|
221 |
+
|
222 |
+
def __mul__(a, b):
|
223 |
+
if b in a.domain:
|
224 |
+
return a.mul(b)
|
225 |
+
else:
|
226 |
+
return NotImplemented
|
227 |
+
|
228 |
+
def __rmul__(a, b):
|
229 |
+
if b in a.domain:
|
230 |
+
return a.mul(b)
|
231 |
+
else:
|
232 |
+
return NotImplemented
|
233 |
+
|
234 |
+
def __matmul__(a, b):
|
235 |
+
if isinstance(b, DDM):
|
236 |
+
return a.matmul(b)
|
237 |
+
else:
|
238 |
+
return NotImplemented
|
239 |
+
|
240 |
+
@classmethod
|
241 |
+
def _check(cls, a, op, b, ashape, bshape):
|
242 |
+
if a.domain != b.domain:
|
243 |
+
msg = "Domain mismatch: %s %s %s" % (a.domain, op, b.domain)
|
244 |
+
raise DMDomainError(msg)
|
245 |
+
if ashape != bshape:
|
246 |
+
msg = "Shape mismatch: %s %s %s" % (a.shape, op, b.shape)
|
247 |
+
raise DMShapeError(msg)
|
248 |
+
|
249 |
+
def add(a, b):
|
250 |
+
"""a + b"""
|
251 |
+
a._check(a, '+', b, a.shape, b.shape)
|
252 |
+
c = a.copy()
|
253 |
+
ddm_iadd(c, b)
|
254 |
+
return c
|
255 |
+
|
256 |
+
def sub(a, b):
|
257 |
+
"""a - b"""
|
258 |
+
a._check(a, '-', b, a.shape, b.shape)
|
259 |
+
c = a.copy()
|
260 |
+
ddm_isub(c, b)
|
261 |
+
return c
|
262 |
+
|
263 |
+
def neg(a):
|
264 |
+
"""-a"""
|
265 |
+
b = a.copy()
|
266 |
+
ddm_ineg(b)
|
267 |
+
return b
|
268 |
+
|
269 |
+
def mul(a, b):
|
270 |
+
c = a.copy()
|
271 |
+
ddm_imul(c, b)
|
272 |
+
return c
|
273 |
+
|
274 |
+
def rmul(a, b):
|
275 |
+
c = a.copy()
|
276 |
+
ddm_irmul(c, b)
|
277 |
+
return c
|
278 |
+
|
279 |
+
def matmul(a, b):
|
280 |
+
"""a @ b (matrix product)"""
|
281 |
+
m, o = a.shape
|
282 |
+
o2, n = b.shape
|
283 |
+
a._check(a, '*', b, o, o2)
|
284 |
+
c = a.zeros((m, n), a.domain)
|
285 |
+
ddm_imatmul(c, a, b)
|
286 |
+
return c
|
287 |
+
|
288 |
+
def mul_elementwise(a, b):
|
289 |
+
assert a.shape == b.shape
|
290 |
+
assert a.domain == b.domain
|
291 |
+
c = [[aij * bij for aij, bij in zip(ai, bi)] for ai, bi in zip(a, b)]
|
292 |
+
return DDM(c, a.shape, a.domain)
|
293 |
+
|
294 |
+
def hstack(A, *B):
|
295 |
+
"""Horizontally stacks :py:class:`~.DDM` matrices.
|
296 |
+
|
297 |
+
Examples
|
298 |
+
========
|
299 |
+
|
300 |
+
>>> from sympy import ZZ
|
301 |
+
>>> from sympy.polys.matrices.sdm import DDM
|
302 |
+
|
303 |
+
>>> A = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
304 |
+
>>> B = DDM([[ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (2, 2), ZZ)
|
305 |
+
>>> A.hstack(B)
|
306 |
+
[[1, 2, 5, 6], [3, 4, 7, 8]]
|
307 |
+
|
308 |
+
>>> C = DDM([[ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (2, 2), ZZ)
|
309 |
+
>>> A.hstack(B, C)
|
310 |
+
[[1, 2, 5, 6, 9, 10], [3, 4, 7, 8, 11, 12]]
|
311 |
+
"""
|
312 |
+
Anew = list(A.copy())
|
313 |
+
rows, cols = A.shape
|
314 |
+
domain = A.domain
|
315 |
+
|
316 |
+
for Bk in B:
|
317 |
+
Bkrows, Bkcols = Bk.shape
|
318 |
+
assert Bkrows == rows
|
319 |
+
assert Bk.domain == domain
|
320 |
+
|
321 |
+
cols += Bkcols
|
322 |
+
|
323 |
+
for i, Bki in enumerate(Bk):
|
324 |
+
Anew[i].extend(Bki)
|
325 |
+
|
326 |
+
return DDM(Anew, (rows, cols), A.domain)
|
327 |
+
|
328 |
+
def vstack(A, *B):
|
329 |
+
"""Vertically stacks :py:class:`~.DDM` matrices.
|
330 |
+
|
331 |
+
Examples
|
332 |
+
========
|
333 |
+
|
334 |
+
>>> from sympy import ZZ
|
335 |
+
>>> from sympy.polys.matrices.sdm import DDM
|
336 |
+
|
337 |
+
>>> A = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
338 |
+
>>> B = DDM([[ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (2, 2), ZZ)
|
339 |
+
>>> A.vstack(B)
|
340 |
+
[[1, 2], [3, 4], [5, 6], [7, 8]]
|
341 |
+
|
342 |
+
>>> C = DDM([[ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (2, 2), ZZ)
|
343 |
+
>>> A.vstack(B, C)
|
344 |
+
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
|
345 |
+
"""
|
346 |
+
Anew = list(A.copy())
|
347 |
+
rows, cols = A.shape
|
348 |
+
domain = A.domain
|
349 |
+
|
350 |
+
for Bk in B:
|
351 |
+
Bkrows, Bkcols = Bk.shape
|
352 |
+
assert Bkcols == cols
|
353 |
+
assert Bk.domain == domain
|
354 |
+
|
355 |
+
rows += Bkrows
|
356 |
+
|
357 |
+
Anew.extend(Bk.copy())
|
358 |
+
|
359 |
+
return DDM(Anew, (rows, cols), A.domain)
|
360 |
+
|
361 |
+
def applyfunc(self, func, domain):
|
362 |
+
elements = (list(map(func, row)) for row in self)
|
363 |
+
return DDM(elements, self.shape, domain)
|
364 |
+
|
365 |
+
def scc(a):
|
366 |
+
"""Strongly connected components of a square matrix *a*.
|
367 |
+
|
368 |
+
Examples
|
369 |
+
========
|
370 |
+
|
371 |
+
>>> from sympy import ZZ
|
372 |
+
>>> from sympy.polys.matrices.sdm import DDM
|
373 |
+
>>> A = DDM([[ZZ(1), ZZ(0)], [ZZ(0), ZZ(1)]], (2, 2), ZZ)
|
374 |
+
>>> A.scc()
|
375 |
+
[[0], [1]]
|
376 |
+
|
377 |
+
See also
|
378 |
+
========
|
379 |
+
|
380 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.scc
|
381 |
+
|
382 |
+
"""
|
383 |
+
return a.to_sdm().scc()
|
384 |
+
|
385 |
+
def rref(a):
|
386 |
+
"""Reduced-row echelon form of a and list of pivots"""
|
387 |
+
b = a.copy()
|
388 |
+
K = a.domain
|
389 |
+
partial_pivot = K.is_RealField or K.is_ComplexField
|
390 |
+
pivots = ddm_irref(b, _partial_pivot=partial_pivot)
|
391 |
+
return b, pivots
|
392 |
+
|
393 |
+
def nullspace(a):
|
394 |
+
rref, pivots = a.rref()
|
395 |
+
rows, cols = a.shape
|
396 |
+
domain = a.domain
|
397 |
+
|
398 |
+
basis = []
|
399 |
+
nonpivots = []
|
400 |
+
for i in range(cols):
|
401 |
+
if i in pivots:
|
402 |
+
continue
|
403 |
+
nonpivots.append(i)
|
404 |
+
vec = [domain.one if i == j else domain.zero for j in range(cols)]
|
405 |
+
for ii, jj in enumerate(pivots):
|
406 |
+
vec[jj] -= rref[ii][i]
|
407 |
+
basis.append(vec)
|
408 |
+
|
409 |
+
return DDM(basis, (len(basis), cols), domain), nonpivots
|
410 |
+
|
411 |
+
def particular(a):
|
412 |
+
return a.to_sdm().particular().to_ddm()
|
413 |
+
|
414 |
+
def det(a):
|
415 |
+
"""Determinant of a"""
|
416 |
+
m, n = a.shape
|
417 |
+
if m != n:
|
418 |
+
raise DMShapeError("Determinant of non-square matrix")
|
419 |
+
b = a.copy()
|
420 |
+
K = b.domain
|
421 |
+
deta = ddm_idet(b, K)
|
422 |
+
return deta
|
423 |
+
|
424 |
+
def inv(a):
|
425 |
+
"""Inverse of a"""
|
426 |
+
m, n = a.shape
|
427 |
+
if m != n:
|
428 |
+
raise DMShapeError("Determinant of non-square matrix")
|
429 |
+
ainv = a.copy()
|
430 |
+
K = a.domain
|
431 |
+
ddm_iinv(ainv, a, K)
|
432 |
+
return ainv
|
433 |
+
|
434 |
+
def lu(a):
|
435 |
+
"""L, U decomposition of a"""
|
436 |
+
m, n = a.shape
|
437 |
+
K = a.domain
|
438 |
+
|
439 |
+
U = a.copy()
|
440 |
+
L = a.eye(m, K)
|
441 |
+
swaps = ddm_ilu_split(L, U, K)
|
442 |
+
|
443 |
+
return L, U, swaps
|
444 |
+
|
445 |
+
def lu_solve(a, b):
|
446 |
+
"""x where a*x = b"""
|
447 |
+
m, n = a.shape
|
448 |
+
m2, o = b.shape
|
449 |
+
a._check(a, 'lu_solve', b, m, m2)
|
450 |
+
|
451 |
+
L, U, swaps = a.lu()
|
452 |
+
x = a.zeros((n, o), a.domain)
|
453 |
+
ddm_ilu_solve(x, L, U, swaps, b)
|
454 |
+
return x
|
455 |
+
|
456 |
+
def charpoly(a):
|
457 |
+
"""Coefficients of characteristic polynomial of a"""
|
458 |
+
K = a.domain
|
459 |
+
m, n = a.shape
|
460 |
+
if m != n:
|
461 |
+
raise DMShapeError("Charpoly of non-square matrix")
|
462 |
+
vec = ddm_berk(a, K)
|
463 |
+
coeffs = [vec[i][0] for i in range(n+1)]
|
464 |
+
return coeffs
|
465 |
+
|
466 |
+
def is_zero_matrix(self):
|
467 |
+
"""
|
468 |
+
Says whether this matrix has all zero entries.
|
469 |
+
"""
|
470 |
+
zero = self.domain.zero
|
471 |
+
return all(Mij == zero for Mij in self.flatiter())
|
472 |
+
|
473 |
+
def is_upper(self):
|
474 |
+
"""
|
475 |
+
Says whether this matrix is upper-triangular. True can be returned
|
476 |
+
even if the matrix is not square.
|
477 |
+
"""
|
478 |
+
zero = self.domain.zero
|
479 |
+
return all(Mij == zero for i, Mi in enumerate(self) for Mij in Mi[:i])
|
480 |
+
|
481 |
+
def is_lower(self):
|
482 |
+
"""
|
483 |
+
Says whether this matrix is lower-triangular. True can be returned
|
484 |
+
even if the matrix is not square.
|
485 |
+
"""
|
486 |
+
zero = self.domain.zero
|
487 |
+
return all(Mij == zero for i, Mi in enumerate(self) for Mij in Mi[i+1:])
|
488 |
+
|
489 |
+
def lll(A, delta=QQ(3, 4)):
|
490 |
+
return ddm_lll(A, delta=delta)
|
491 |
+
|
492 |
+
def lll_transform(A, delta=QQ(3, 4)):
|
493 |
+
return ddm_lll_transform(A, delta=delta)
|
494 |
+
|
495 |
+
|
496 |
+
from .sdm import SDM
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/dense.py
ADDED
@@ -0,0 +1,348 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
|
3 |
+
Module for the ddm_* routines for operating on a matrix in list of lists
|
4 |
+
matrix representation.
|
5 |
+
|
6 |
+
These routines are used internally by the DDM class which also provides a
|
7 |
+
friendlier interface for them. The idea here is to implement core matrix
|
8 |
+
routines in a way that can be applied to any simple list representation
|
9 |
+
without the need to use any particular matrix class. For example we can
|
10 |
+
compute the RREF of a matrix like:
|
11 |
+
|
12 |
+
>>> from sympy.polys.matrices.dense import ddm_irref
|
13 |
+
>>> M = [[1, 2, 3], [4, 5, 6]]
|
14 |
+
>>> pivots = ddm_irref(M)
|
15 |
+
>>> M
|
16 |
+
[[1.0, 0.0, -1.0], [0, 1.0, 2.0]]
|
17 |
+
|
18 |
+
These are lower-level routines that work mostly in place.The routines at this
|
19 |
+
level should not need to know what the domain of the elements is but should
|
20 |
+
ideally document what operations they will use and what functions they need to
|
21 |
+
be provided with.
|
22 |
+
|
23 |
+
The next-level up is the DDM class which uses these routines but wraps them up
|
24 |
+
with an interface that handles copying etc and keeps track of the Domain of
|
25 |
+
the elements of the matrix:
|
26 |
+
|
27 |
+
>>> from sympy.polys.domains import QQ
|
28 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
29 |
+
>>> M = DDM([[QQ(1), QQ(2), QQ(3)], [QQ(4), QQ(5), QQ(6)]], (2, 3), QQ)
|
30 |
+
>>> M
|
31 |
+
[[1, 2, 3], [4, 5, 6]]
|
32 |
+
>>> Mrref, pivots = M.rref()
|
33 |
+
>>> Mrref
|
34 |
+
[[1, 0, -1], [0, 1, 2]]
|
35 |
+
|
36 |
+
"""
|
37 |
+
from __future__ import annotations
|
38 |
+
from operator import mul
|
39 |
+
from .exceptions import (
|
40 |
+
DMShapeError,
|
41 |
+
DMNonInvertibleMatrixError,
|
42 |
+
DMNonSquareMatrixError,
|
43 |
+
)
|
44 |
+
from typing import Sequence, TypeVar
|
45 |
+
from sympy.polys.matrices._typing import RingElement
|
46 |
+
|
47 |
+
|
48 |
+
T = TypeVar('T')
|
49 |
+
R = TypeVar('R', bound=RingElement)
|
50 |
+
|
51 |
+
|
52 |
+
def ddm_transpose(matrix: Sequence[Sequence[T]]) -> list[list[T]]:
|
53 |
+
"""matrix transpose"""
|
54 |
+
return list(map(list, zip(*matrix)))
|
55 |
+
|
56 |
+
|
57 |
+
def ddm_iadd(a: list[list[R]], b: Sequence[Sequence[R]]) -> None:
|
58 |
+
"""a += b"""
|
59 |
+
for ai, bi in zip(a, b):
|
60 |
+
for j, bij in enumerate(bi):
|
61 |
+
ai[j] += bij
|
62 |
+
|
63 |
+
|
64 |
+
def ddm_isub(a: list[list[R]], b: Sequence[Sequence[R]]) -> None:
|
65 |
+
"""a -= b"""
|
66 |
+
for ai, bi in zip(a, b):
|
67 |
+
for j, bij in enumerate(bi):
|
68 |
+
ai[j] -= bij
|
69 |
+
|
70 |
+
|
71 |
+
def ddm_ineg(a: list[list[R]]) -> None:
|
72 |
+
"""a <-- -a"""
|
73 |
+
for ai in a:
|
74 |
+
for j, aij in enumerate(ai):
|
75 |
+
ai[j] = -aij
|
76 |
+
|
77 |
+
|
78 |
+
def ddm_imul(a: list[list[R]], b: R) -> None:
|
79 |
+
for ai in a:
|
80 |
+
for j, aij in enumerate(ai):
|
81 |
+
ai[j] = aij * b
|
82 |
+
|
83 |
+
|
84 |
+
def ddm_irmul(a: list[list[R]], b: R) -> None:
|
85 |
+
for ai in a:
|
86 |
+
for j, aij in enumerate(ai):
|
87 |
+
ai[j] = b * aij
|
88 |
+
|
89 |
+
|
90 |
+
def ddm_imatmul(
|
91 |
+
a: list[list[R]], b: Sequence[Sequence[R]], c: Sequence[Sequence[R]]
|
92 |
+
) -> None:
|
93 |
+
"""a += b @ c"""
|
94 |
+
cT = list(zip(*c))
|
95 |
+
|
96 |
+
for bi, ai in zip(b, a):
|
97 |
+
for j, cTj in enumerate(cT):
|
98 |
+
ai[j] = sum(map(mul, bi, cTj), ai[j])
|
99 |
+
|
100 |
+
|
101 |
+
def ddm_irref(a, _partial_pivot=False):
|
102 |
+
"""a <-- rref(a)"""
|
103 |
+
# a is (m x n)
|
104 |
+
m = len(a)
|
105 |
+
if not m:
|
106 |
+
return []
|
107 |
+
n = len(a[0])
|
108 |
+
|
109 |
+
i = 0
|
110 |
+
pivots = []
|
111 |
+
|
112 |
+
for j in range(n):
|
113 |
+
# Proper pivoting should be used for all domains for performance
|
114 |
+
# reasons but it is only strictly needed for RR and CC (and possibly
|
115 |
+
# other domains like RR(x)). This path is used by DDM.rref() if the
|
116 |
+
# domain is RR or CC. It uses partial (row) pivoting based on the
|
117 |
+
# absolute value of the pivot candidates.
|
118 |
+
if _partial_pivot:
|
119 |
+
ip = max(range(i, m), key=lambda ip: abs(a[ip][j]))
|
120 |
+
a[i], a[ip] = a[ip], a[i]
|
121 |
+
|
122 |
+
# pivot
|
123 |
+
aij = a[i][j]
|
124 |
+
|
125 |
+
# zero-pivot
|
126 |
+
if not aij:
|
127 |
+
for ip in range(i+1, m):
|
128 |
+
aij = a[ip][j]
|
129 |
+
# row-swap
|
130 |
+
if aij:
|
131 |
+
a[i], a[ip] = a[ip], a[i]
|
132 |
+
break
|
133 |
+
else:
|
134 |
+
# next column
|
135 |
+
continue
|
136 |
+
|
137 |
+
# normalise row
|
138 |
+
ai = a[i]
|
139 |
+
aijinv = aij**-1
|
140 |
+
for l in range(j, n):
|
141 |
+
ai[l] *= aijinv # ai[j] = one
|
142 |
+
|
143 |
+
# eliminate above and below to the right
|
144 |
+
for k, ak in enumerate(a):
|
145 |
+
if k == i or not ak[j]:
|
146 |
+
continue
|
147 |
+
akj = ak[j]
|
148 |
+
ak[j] -= akj # ak[j] = zero
|
149 |
+
for l in range(j+1, n):
|
150 |
+
ak[l] -= akj * ai[l]
|
151 |
+
|
152 |
+
# next row
|
153 |
+
pivots.append(j)
|
154 |
+
i += 1
|
155 |
+
|
156 |
+
# no more rows?
|
157 |
+
if i >= m:
|
158 |
+
break
|
159 |
+
|
160 |
+
return pivots
|
161 |
+
|
162 |
+
|
163 |
+
def ddm_idet(a, K):
|
164 |
+
"""a <-- echelon(a); return det"""
|
165 |
+
# Bareiss algorithm
|
166 |
+
# https://www.math.usm.edu/perry/Research/Thesis_DRL.pdf
|
167 |
+
|
168 |
+
# a is (m x n)
|
169 |
+
m = len(a)
|
170 |
+
if not m:
|
171 |
+
return K.one
|
172 |
+
n = len(a[0])
|
173 |
+
|
174 |
+
exquo = K.exquo
|
175 |
+
# uf keeps track of the sign change from row swaps
|
176 |
+
uf = K.one
|
177 |
+
|
178 |
+
for k in range(n-1):
|
179 |
+
if not a[k][k]:
|
180 |
+
for i in range(k+1, n):
|
181 |
+
if a[i][k]:
|
182 |
+
a[k], a[i] = a[i], a[k]
|
183 |
+
uf = -uf
|
184 |
+
break
|
185 |
+
else:
|
186 |
+
return K.zero
|
187 |
+
|
188 |
+
akkm1 = a[k-1][k-1] if k else K.one
|
189 |
+
|
190 |
+
for i in range(k+1, n):
|
191 |
+
for j in range(k+1, n):
|
192 |
+
a[i][j] = exquo(a[i][j]*a[k][k] - a[i][k]*a[k][j], akkm1)
|
193 |
+
|
194 |
+
return uf * a[-1][-1]
|
195 |
+
|
196 |
+
|
197 |
+
def ddm_iinv(ainv, a, K):
|
198 |
+
if not K.is_Field:
|
199 |
+
raise ValueError('Not a field')
|
200 |
+
|
201 |
+
# a is (m x n)
|
202 |
+
m = len(a)
|
203 |
+
if not m:
|
204 |
+
return
|
205 |
+
n = len(a[0])
|
206 |
+
if m != n:
|
207 |
+
raise DMNonSquareMatrixError
|
208 |
+
|
209 |
+
eye = [[K.one if i==j else K.zero for j in range(n)] for i in range(n)]
|
210 |
+
Aaug = [row + eyerow for row, eyerow in zip(a, eye)]
|
211 |
+
pivots = ddm_irref(Aaug)
|
212 |
+
if pivots != list(range(n)):
|
213 |
+
raise DMNonInvertibleMatrixError('Matrix det == 0; not invertible.')
|
214 |
+
ainv[:] = [row[n:] for row in Aaug]
|
215 |
+
|
216 |
+
|
217 |
+
def ddm_ilu_split(L, U, K):
|
218 |
+
"""L, U <-- LU(U)"""
|
219 |
+
m = len(U)
|
220 |
+
if not m:
|
221 |
+
return []
|
222 |
+
n = len(U[0])
|
223 |
+
|
224 |
+
swaps = ddm_ilu(U)
|
225 |
+
|
226 |
+
zeros = [K.zero] * min(m, n)
|
227 |
+
for i in range(1, m):
|
228 |
+
j = min(i, n)
|
229 |
+
L[i][:j] = U[i][:j]
|
230 |
+
U[i][:j] = zeros[:j]
|
231 |
+
|
232 |
+
return swaps
|
233 |
+
|
234 |
+
|
235 |
+
def ddm_ilu(a):
|
236 |
+
"""a <-- LU(a)"""
|
237 |
+
m = len(a)
|
238 |
+
if not m:
|
239 |
+
return []
|
240 |
+
n = len(a[0])
|
241 |
+
|
242 |
+
swaps = []
|
243 |
+
|
244 |
+
for i in range(min(m, n)):
|
245 |
+
if not a[i][i]:
|
246 |
+
for ip in range(i+1, m):
|
247 |
+
if a[ip][i]:
|
248 |
+
swaps.append((i, ip))
|
249 |
+
a[i], a[ip] = a[ip], a[i]
|
250 |
+
break
|
251 |
+
else:
|
252 |
+
# M = Matrix([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 1], [0, 0, 1, 2]])
|
253 |
+
continue
|
254 |
+
for j in range(i+1, m):
|
255 |
+
l_ji = a[j][i] / a[i][i]
|
256 |
+
a[j][i] = l_ji
|
257 |
+
for k in range(i+1, n):
|
258 |
+
a[j][k] -= l_ji * a[i][k]
|
259 |
+
|
260 |
+
return swaps
|
261 |
+
|
262 |
+
|
263 |
+
def ddm_ilu_solve(x, L, U, swaps, b):
|
264 |
+
"""x <-- solve(L*U*x = swaps(b))"""
|
265 |
+
m = len(U)
|
266 |
+
if not m:
|
267 |
+
return
|
268 |
+
n = len(U[0])
|
269 |
+
|
270 |
+
m2 = len(b)
|
271 |
+
if not m2:
|
272 |
+
raise DMShapeError("Shape mismtch")
|
273 |
+
o = len(b[0])
|
274 |
+
|
275 |
+
if m != m2:
|
276 |
+
raise DMShapeError("Shape mismtch")
|
277 |
+
if m < n:
|
278 |
+
raise NotImplementedError("Underdetermined")
|
279 |
+
|
280 |
+
if swaps:
|
281 |
+
b = [row[:] for row in b]
|
282 |
+
for i1, i2 in swaps:
|
283 |
+
b[i1], b[i2] = b[i2], b[i1]
|
284 |
+
|
285 |
+
# solve Ly = b
|
286 |
+
y = [[None] * o for _ in range(m)]
|
287 |
+
for k in range(o):
|
288 |
+
for i in range(m):
|
289 |
+
rhs = b[i][k]
|
290 |
+
for j in range(i):
|
291 |
+
rhs -= L[i][j] * y[j][k]
|
292 |
+
y[i][k] = rhs
|
293 |
+
|
294 |
+
if m > n:
|
295 |
+
for i in range(n, m):
|
296 |
+
for j in range(o):
|
297 |
+
if y[i][j]:
|
298 |
+
raise DMNonInvertibleMatrixError
|
299 |
+
|
300 |
+
# Solve Ux = y
|
301 |
+
for k in range(o):
|
302 |
+
for i in reversed(range(n)):
|
303 |
+
if not U[i][i]:
|
304 |
+
raise DMNonInvertibleMatrixError
|
305 |
+
rhs = y[i][k]
|
306 |
+
for j in range(i+1, n):
|
307 |
+
rhs -= U[i][j] * x[j][k]
|
308 |
+
x[i][k] = rhs / U[i][i]
|
309 |
+
|
310 |
+
|
311 |
+
def ddm_berk(M, K):
|
312 |
+
m = len(M)
|
313 |
+
if not m:
|
314 |
+
return [[K.one]]
|
315 |
+
n = len(M[0])
|
316 |
+
|
317 |
+
if m != n:
|
318 |
+
raise DMShapeError("Not square")
|
319 |
+
|
320 |
+
if n == 1:
|
321 |
+
return [[K.one], [-M[0][0]]]
|
322 |
+
|
323 |
+
a = M[0][0]
|
324 |
+
R = [M[0][1:]]
|
325 |
+
C = [[row[0]] for row in M[1:]]
|
326 |
+
A = [row[1:] for row in M[1:]]
|
327 |
+
|
328 |
+
q = ddm_berk(A, K)
|
329 |
+
|
330 |
+
T = [[K.zero] * n for _ in range(n+1)]
|
331 |
+
for i in range(n):
|
332 |
+
T[i][i] = K.one
|
333 |
+
T[i+1][i] = -a
|
334 |
+
for i in range(2, n+1):
|
335 |
+
if i == 2:
|
336 |
+
AnC = C
|
337 |
+
else:
|
338 |
+
C = AnC
|
339 |
+
AnC = [[K.zero] for row in C]
|
340 |
+
ddm_imatmul(AnC, A, C)
|
341 |
+
RAnC = [[K.zero]]
|
342 |
+
ddm_imatmul(RAnC, R, AnC)
|
343 |
+
for j in range(0, n+1-i):
|
344 |
+
T[i+j][j] = -RAnC[0][0]
|
345 |
+
|
346 |
+
qout = [[K.zero] for _ in range(n+1)]
|
347 |
+
ddm_imatmul(qout, T, q)
|
348 |
+
return qout
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/domainmatrix.py
ADDED
@@ -0,0 +1,1791 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
|
3 |
+
Module for the DomainMatrix class.
|
4 |
+
|
5 |
+
A DomainMatrix represents a matrix with elements that are in a particular
|
6 |
+
Domain. Each DomainMatrix internally wraps a DDM which is used for the
|
7 |
+
lower-level operations. The idea is that the DomainMatrix class provides the
|
8 |
+
convenience routines for converting between Expr and the poly domains as well
|
9 |
+
as unifying matrices with different domains.
|
10 |
+
|
11 |
+
"""
|
12 |
+
from functools import reduce
|
13 |
+
from typing import Union as tUnion, Tuple as tTuple
|
14 |
+
|
15 |
+
from sympy.core.sympify import _sympify
|
16 |
+
|
17 |
+
from ..domains import Domain
|
18 |
+
|
19 |
+
from ..constructor import construct_domain
|
20 |
+
|
21 |
+
from .exceptions import (DMNonSquareMatrixError, DMShapeError,
|
22 |
+
DMDomainError, DMFormatError, DMBadInputError,
|
23 |
+
DMNotAField)
|
24 |
+
|
25 |
+
from .ddm import DDM
|
26 |
+
|
27 |
+
from .sdm import SDM
|
28 |
+
|
29 |
+
from .domainscalar import DomainScalar
|
30 |
+
|
31 |
+
from sympy.polys.domains import ZZ, EXRAW, QQ
|
32 |
+
|
33 |
+
|
34 |
+
def DM(rows, domain):
|
35 |
+
"""Convenient alias for DomainMatrix.from_list
|
36 |
+
|
37 |
+
Examples
|
38 |
+
=======
|
39 |
+
|
40 |
+
>>> from sympy import ZZ
|
41 |
+
>>> from sympy.polys.matrices import DM
|
42 |
+
>>> DM([[1, 2], [3, 4]], ZZ)
|
43 |
+
DomainMatrix([[1, 2], [3, 4]], (2, 2), ZZ)
|
44 |
+
|
45 |
+
See also
|
46 |
+
=======
|
47 |
+
|
48 |
+
DomainMatrix.from_list
|
49 |
+
"""
|
50 |
+
return DomainMatrix.from_list(rows, domain)
|
51 |
+
|
52 |
+
|
53 |
+
class DomainMatrix:
|
54 |
+
r"""
|
55 |
+
Associate Matrix with :py:class:`~.Domain`
|
56 |
+
|
57 |
+
Explanation
|
58 |
+
===========
|
59 |
+
|
60 |
+
DomainMatrix uses :py:class:`~.Domain` for its internal representation
|
61 |
+
which makes it faster than the SymPy Matrix class (currently) for many
|
62 |
+
common operations, but this advantage makes it not entirely compatible
|
63 |
+
with Matrix. DomainMatrix are analogous to numpy arrays with "dtype".
|
64 |
+
In the DomainMatrix, each element has a domain such as :ref:`ZZ`
|
65 |
+
or :ref:`QQ(a)`.
|
66 |
+
|
67 |
+
|
68 |
+
Examples
|
69 |
+
========
|
70 |
+
|
71 |
+
Creating a DomainMatrix from the existing Matrix class:
|
72 |
+
|
73 |
+
>>> from sympy import Matrix
|
74 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
75 |
+
>>> Matrix1 = Matrix([
|
76 |
+
... [1, 2],
|
77 |
+
... [3, 4]])
|
78 |
+
>>> A = DomainMatrix.from_Matrix(Matrix1)
|
79 |
+
>>> A
|
80 |
+
DomainMatrix({0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}, (2, 2), ZZ)
|
81 |
+
|
82 |
+
Directly forming a DomainMatrix:
|
83 |
+
|
84 |
+
>>> from sympy import ZZ
|
85 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
86 |
+
>>> A = DomainMatrix([
|
87 |
+
... [ZZ(1), ZZ(2)],
|
88 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
89 |
+
>>> A
|
90 |
+
DomainMatrix([[1, 2], [3, 4]], (2, 2), ZZ)
|
91 |
+
|
92 |
+
See Also
|
93 |
+
========
|
94 |
+
|
95 |
+
DDM
|
96 |
+
SDM
|
97 |
+
Domain
|
98 |
+
Poly
|
99 |
+
|
100 |
+
"""
|
101 |
+
rep: tUnion[SDM, DDM]
|
102 |
+
shape: tTuple[int, int]
|
103 |
+
domain: Domain
|
104 |
+
|
105 |
+
def __new__(cls, rows, shape, domain, *, fmt=None):
|
106 |
+
"""
|
107 |
+
Creates a :py:class:`~.DomainMatrix`.
|
108 |
+
|
109 |
+
Parameters
|
110 |
+
==========
|
111 |
+
|
112 |
+
rows : Represents elements of DomainMatrix as list of lists
|
113 |
+
shape : Represents dimension of DomainMatrix
|
114 |
+
domain : Represents :py:class:`~.Domain` of DomainMatrix
|
115 |
+
|
116 |
+
Raises
|
117 |
+
======
|
118 |
+
|
119 |
+
TypeError
|
120 |
+
If any of rows, shape and domain are not provided
|
121 |
+
|
122 |
+
"""
|
123 |
+
if isinstance(rows, (DDM, SDM)):
|
124 |
+
raise TypeError("Use from_rep to initialise from SDM/DDM")
|
125 |
+
elif isinstance(rows, list):
|
126 |
+
rep = DDM(rows, shape, domain)
|
127 |
+
elif isinstance(rows, dict):
|
128 |
+
rep = SDM(rows, shape, domain)
|
129 |
+
else:
|
130 |
+
msg = "Input should be list-of-lists or dict-of-dicts"
|
131 |
+
raise TypeError(msg)
|
132 |
+
|
133 |
+
if fmt is not None:
|
134 |
+
if fmt == 'sparse':
|
135 |
+
rep = rep.to_sdm()
|
136 |
+
elif fmt == 'dense':
|
137 |
+
rep = rep.to_ddm()
|
138 |
+
else:
|
139 |
+
raise ValueError("fmt should be 'sparse' or 'dense'")
|
140 |
+
|
141 |
+
return cls.from_rep(rep)
|
142 |
+
|
143 |
+
def __getnewargs__(self):
|
144 |
+
rep = self.rep
|
145 |
+
if isinstance(rep, DDM):
|
146 |
+
arg = list(rep)
|
147 |
+
elif isinstance(rep, SDM):
|
148 |
+
arg = dict(rep)
|
149 |
+
else:
|
150 |
+
raise RuntimeError # pragma: no cover
|
151 |
+
return arg, self.shape, self.domain
|
152 |
+
|
153 |
+
def __getitem__(self, key):
|
154 |
+
i, j = key
|
155 |
+
m, n = self.shape
|
156 |
+
if not (isinstance(i, slice) or isinstance(j, slice)):
|
157 |
+
return DomainScalar(self.rep.getitem(i, j), self.domain)
|
158 |
+
|
159 |
+
if not isinstance(i, slice):
|
160 |
+
if not -m <= i < m:
|
161 |
+
raise IndexError("Row index out of range")
|
162 |
+
i = i % m
|
163 |
+
i = slice(i, i+1)
|
164 |
+
if not isinstance(j, slice):
|
165 |
+
if not -n <= j < n:
|
166 |
+
raise IndexError("Column index out of range")
|
167 |
+
j = j % n
|
168 |
+
j = slice(j, j+1)
|
169 |
+
|
170 |
+
return self.from_rep(self.rep.extract_slice(i, j))
|
171 |
+
|
172 |
+
def getitem_sympy(self, i, j):
|
173 |
+
return self.domain.to_sympy(self.rep.getitem(i, j))
|
174 |
+
|
175 |
+
def extract(self, rowslist, colslist):
|
176 |
+
return self.from_rep(self.rep.extract(rowslist, colslist))
|
177 |
+
|
178 |
+
def __setitem__(self, key, value):
|
179 |
+
i, j = key
|
180 |
+
if not self.domain.of_type(value):
|
181 |
+
raise TypeError
|
182 |
+
if isinstance(i, int) and isinstance(j, int):
|
183 |
+
self.rep.setitem(i, j, value)
|
184 |
+
else:
|
185 |
+
raise NotImplementedError
|
186 |
+
|
187 |
+
@classmethod
|
188 |
+
def from_rep(cls, rep):
|
189 |
+
"""Create a new DomainMatrix efficiently from DDM/SDM.
|
190 |
+
|
191 |
+
Examples
|
192 |
+
========
|
193 |
+
|
194 |
+
Create a :py:class:`~.DomainMatrix` with an dense internal
|
195 |
+
representation as :py:class:`~.DDM`:
|
196 |
+
|
197 |
+
>>> from sympy.polys.domains import ZZ
|
198 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
199 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
200 |
+
>>> drep = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
201 |
+
>>> dM = DomainMatrix.from_rep(drep)
|
202 |
+
>>> dM
|
203 |
+
DomainMatrix([[1, 2], [3, 4]], (2, 2), ZZ)
|
204 |
+
|
205 |
+
Create a :py:class:`~.DomainMatrix` with a sparse internal
|
206 |
+
representation as :py:class:`~.SDM`:
|
207 |
+
|
208 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
209 |
+
>>> from sympy.polys.matrices.sdm import SDM
|
210 |
+
>>> from sympy import ZZ
|
211 |
+
>>> drep = SDM({0:{1:ZZ(1)},1:{0:ZZ(2)}}, (2, 2), ZZ)
|
212 |
+
>>> dM = DomainMatrix.from_rep(drep)
|
213 |
+
>>> dM
|
214 |
+
DomainMatrix({0: {1: 1}, 1: {0: 2}}, (2, 2), ZZ)
|
215 |
+
|
216 |
+
Parameters
|
217 |
+
==========
|
218 |
+
|
219 |
+
rep: SDM or DDM
|
220 |
+
The internal sparse or dense representation of the matrix.
|
221 |
+
|
222 |
+
Returns
|
223 |
+
=======
|
224 |
+
|
225 |
+
DomainMatrix
|
226 |
+
A :py:class:`~.DomainMatrix` wrapping *rep*.
|
227 |
+
|
228 |
+
Notes
|
229 |
+
=====
|
230 |
+
|
231 |
+
This takes ownership of rep as its internal representation. If rep is
|
232 |
+
being mutated elsewhere then a copy should be provided to
|
233 |
+
``from_rep``. Only minimal verification or checking is done on *rep*
|
234 |
+
as this is supposed to be an efficient internal routine.
|
235 |
+
|
236 |
+
"""
|
237 |
+
if not isinstance(rep, (DDM, SDM)):
|
238 |
+
raise TypeError("rep should be of type DDM or SDM")
|
239 |
+
self = super().__new__(cls)
|
240 |
+
self.rep = rep
|
241 |
+
self.shape = rep.shape
|
242 |
+
self.domain = rep.domain
|
243 |
+
return self
|
244 |
+
|
245 |
+
|
246 |
+
@classmethod
|
247 |
+
def from_list(cls, rows, domain):
|
248 |
+
r"""
|
249 |
+
Convert a list of lists into a DomainMatrix
|
250 |
+
|
251 |
+
Parameters
|
252 |
+
==========
|
253 |
+
|
254 |
+
rows: list of lists
|
255 |
+
Each element of the inner lists should be either the single arg,
|
256 |
+
or tuple of args, that would be passed to the domain constructor
|
257 |
+
in order to form an element of the domain. See examples.
|
258 |
+
|
259 |
+
Returns
|
260 |
+
=======
|
261 |
+
|
262 |
+
DomainMatrix containing elements defined in rows
|
263 |
+
|
264 |
+
Examples
|
265 |
+
========
|
266 |
+
|
267 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
268 |
+
>>> from sympy import FF, QQ, ZZ
|
269 |
+
>>> A = DomainMatrix.from_list([[1, 0, 1], [0, 0, 1]], ZZ)
|
270 |
+
>>> A
|
271 |
+
DomainMatrix([[1, 0, 1], [0, 0, 1]], (2, 3), ZZ)
|
272 |
+
>>> B = DomainMatrix.from_list([[1, 0, 1], [0, 0, 1]], FF(7))
|
273 |
+
>>> B
|
274 |
+
DomainMatrix([[1 mod 7, 0 mod 7, 1 mod 7], [0 mod 7, 0 mod 7, 1 mod 7]], (2, 3), GF(7))
|
275 |
+
>>> C = DomainMatrix.from_list([[(1, 2), (3, 1)], [(1, 4), (5, 1)]], QQ)
|
276 |
+
>>> C
|
277 |
+
DomainMatrix([[1/2, 3], [1/4, 5]], (2, 2), QQ)
|
278 |
+
|
279 |
+
See Also
|
280 |
+
========
|
281 |
+
|
282 |
+
from_list_sympy
|
283 |
+
|
284 |
+
"""
|
285 |
+
nrows = len(rows)
|
286 |
+
ncols = 0 if not nrows else len(rows[0])
|
287 |
+
conv = lambda e: domain(*e) if isinstance(e, tuple) else domain(e)
|
288 |
+
domain_rows = [[conv(e) for e in row] for row in rows]
|
289 |
+
return DomainMatrix(domain_rows, (nrows, ncols), domain)
|
290 |
+
|
291 |
+
|
292 |
+
@classmethod
|
293 |
+
def from_list_sympy(cls, nrows, ncols, rows, **kwargs):
|
294 |
+
r"""
|
295 |
+
Convert a list of lists of Expr into a DomainMatrix using construct_domain
|
296 |
+
|
297 |
+
Parameters
|
298 |
+
==========
|
299 |
+
|
300 |
+
nrows: number of rows
|
301 |
+
ncols: number of columns
|
302 |
+
rows: list of lists
|
303 |
+
|
304 |
+
Returns
|
305 |
+
=======
|
306 |
+
|
307 |
+
DomainMatrix containing elements of rows
|
308 |
+
|
309 |
+
Examples
|
310 |
+
========
|
311 |
+
|
312 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
313 |
+
>>> from sympy.abc import x, y, z
|
314 |
+
>>> A = DomainMatrix.from_list_sympy(1, 3, [[x, y, z]])
|
315 |
+
>>> A
|
316 |
+
DomainMatrix([[x, y, z]], (1, 3), ZZ[x,y,z])
|
317 |
+
|
318 |
+
See Also
|
319 |
+
========
|
320 |
+
|
321 |
+
sympy.polys.constructor.construct_domain, from_dict_sympy
|
322 |
+
|
323 |
+
"""
|
324 |
+
assert len(rows) == nrows
|
325 |
+
assert all(len(row) == ncols for row in rows)
|
326 |
+
|
327 |
+
items_sympy = [_sympify(item) for row in rows for item in row]
|
328 |
+
|
329 |
+
domain, items_domain = cls.get_domain(items_sympy, **kwargs)
|
330 |
+
|
331 |
+
domain_rows = [[items_domain[ncols*r + c] for c in range(ncols)] for r in range(nrows)]
|
332 |
+
|
333 |
+
return DomainMatrix(domain_rows, (nrows, ncols), domain)
|
334 |
+
|
335 |
+
@classmethod
|
336 |
+
def from_dict_sympy(cls, nrows, ncols, elemsdict, **kwargs):
|
337 |
+
"""
|
338 |
+
|
339 |
+
Parameters
|
340 |
+
==========
|
341 |
+
|
342 |
+
nrows: number of rows
|
343 |
+
ncols: number of cols
|
344 |
+
elemsdict: dict of dicts containing non-zero elements of the DomainMatrix
|
345 |
+
|
346 |
+
Returns
|
347 |
+
=======
|
348 |
+
|
349 |
+
DomainMatrix containing elements of elemsdict
|
350 |
+
|
351 |
+
Examples
|
352 |
+
========
|
353 |
+
|
354 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
355 |
+
>>> from sympy.abc import x,y,z
|
356 |
+
>>> elemsdict = {0: {0:x}, 1:{1: y}, 2: {2: z}}
|
357 |
+
>>> A = DomainMatrix.from_dict_sympy(3, 3, elemsdict)
|
358 |
+
>>> A
|
359 |
+
DomainMatrix({0: {0: x}, 1: {1: y}, 2: {2: z}}, (3, 3), ZZ[x,y,z])
|
360 |
+
|
361 |
+
See Also
|
362 |
+
========
|
363 |
+
|
364 |
+
from_list_sympy
|
365 |
+
|
366 |
+
"""
|
367 |
+
if not all(0 <= r < nrows for r in elemsdict):
|
368 |
+
raise DMBadInputError("Row out of range")
|
369 |
+
if not all(0 <= c < ncols for row in elemsdict.values() for c in row):
|
370 |
+
raise DMBadInputError("Column out of range")
|
371 |
+
|
372 |
+
items_sympy = [_sympify(item) for row in elemsdict.values() for item in row.values()]
|
373 |
+
domain, items_domain = cls.get_domain(items_sympy, **kwargs)
|
374 |
+
|
375 |
+
idx = 0
|
376 |
+
items_dict = {}
|
377 |
+
for i, row in elemsdict.items():
|
378 |
+
items_dict[i] = {}
|
379 |
+
for j in row:
|
380 |
+
items_dict[i][j] = items_domain[idx]
|
381 |
+
idx += 1
|
382 |
+
|
383 |
+
return DomainMatrix(items_dict, (nrows, ncols), domain)
|
384 |
+
|
385 |
+
@classmethod
|
386 |
+
def from_Matrix(cls, M, fmt='sparse',**kwargs):
|
387 |
+
r"""
|
388 |
+
Convert Matrix to DomainMatrix
|
389 |
+
|
390 |
+
Parameters
|
391 |
+
==========
|
392 |
+
|
393 |
+
M: Matrix
|
394 |
+
|
395 |
+
Returns
|
396 |
+
=======
|
397 |
+
|
398 |
+
Returns DomainMatrix with identical elements as M
|
399 |
+
|
400 |
+
Examples
|
401 |
+
========
|
402 |
+
|
403 |
+
>>> from sympy import Matrix
|
404 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
405 |
+
>>> M = Matrix([
|
406 |
+
... [1.0, 3.4],
|
407 |
+
... [2.4, 1]])
|
408 |
+
>>> A = DomainMatrix.from_Matrix(M)
|
409 |
+
>>> A
|
410 |
+
DomainMatrix({0: {0: 1.0, 1: 3.4}, 1: {0: 2.4, 1: 1.0}}, (2, 2), RR)
|
411 |
+
|
412 |
+
We can keep internal representation as ddm using fmt='dense'
|
413 |
+
>>> from sympy import Matrix, QQ
|
414 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
415 |
+
>>> A = DomainMatrix.from_Matrix(Matrix([[QQ(1, 2), QQ(3, 4)], [QQ(0, 1), QQ(0, 1)]]), fmt='dense')
|
416 |
+
>>> A.rep
|
417 |
+
[[1/2, 3/4], [0, 0]]
|
418 |
+
|
419 |
+
See Also
|
420 |
+
========
|
421 |
+
|
422 |
+
Matrix
|
423 |
+
|
424 |
+
"""
|
425 |
+
if fmt == 'dense':
|
426 |
+
return cls.from_list_sympy(*M.shape, M.tolist(), **kwargs)
|
427 |
+
|
428 |
+
return cls.from_dict_sympy(*M.shape, M.todod(), **kwargs)
|
429 |
+
|
430 |
+
@classmethod
|
431 |
+
def get_domain(cls, items_sympy, **kwargs):
|
432 |
+
K, items_K = construct_domain(items_sympy, **kwargs)
|
433 |
+
return K, items_K
|
434 |
+
|
435 |
+
def copy(self):
|
436 |
+
return self.from_rep(self.rep.copy())
|
437 |
+
|
438 |
+
def convert_to(self, K):
|
439 |
+
r"""
|
440 |
+
Change the domain of DomainMatrix to desired domain or field
|
441 |
+
|
442 |
+
Parameters
|
443 |
+
==========
|
444 |
+
|
445 |
+
K : Represents the desired domain or field.
|
446 |
+
Alternatively, ``None`` may be passed, in which case this method
|
447 |
+
just returns a copy of this DomainMatrix.
|
448 |
+
|
449 |
+
Returns
|
450 |
+
=======
|
451 |
+
|
452 |
+
DomainMatrix
|
453 |
+
DomainMatrix with the desired domain or field
|
454 |
+
|
455 |
+
Examples
|
456 |
+
========
|
457 |
+
|
458 |
+
>>> from sympy import ZZ, ZZ_I
|
459 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
460 |
+
>>> A = DomainMatrix([
|
461 |
+
... [ZZ(1), ZZ(2)],
|
462 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
463 |
+
|
464 |
+
>>> A.convert_to(ZZ_I)
|
465 |
+
DomainMatrix([[1, 2], [3, 4]], (2, 2), ZZ_I)
|
466 |
+
|
467 |
+
"""
|
468 |
+
if K is None:
|
469 |
+
return self.copy()
|
470 |
+
return self.from_rep(self.rep.convert_to(K))
|
471 |
+
|
472 |
+
def to_sympy(self):
|
473 |
+
return self.convert_to(EXRAW)
|
474 |
+
|
475 |
+
def to_field(self):
|
476 |
+
r"""
|
477 |
+
Returns a DomainMatrix with the appropriate field
|
478 |
+
|
479 |
+
Returns
|
480 |
+
=======
|
481 |
+
|
482 |
+
DomainMatrix
|
483 |
+
DomainMatrix with the appropriate field
|
484 |
+
|
485 |
+
Examples
|
486 |
+
========
|
487 |
+
|
488 |
+
>>> from sympy import ZZ
|
489 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
490 |
+
>>> A = DomainMatrix([
|
491 |
+
... [ZZ(1), ZZ(2)],
|
492 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
493 |
+
|
494 |
+
>>> A.to_field()
|
495 |
+
DomainMatrix([[1, 2], [3, 4]], (2, 2), QQ)
|
496 |
+
|
497 |
+
"""
|
498 |
+
K = self.domain.get_field()
|
499 |
+
return self.convert_to(K)
|
500 |
+
|
501 |
+
def to_sparse(self):
|
502 |
+
"""
|
503 |
+
Return a sparse DomainMatrix representation of *self*.
|
504 |
+
|
505 |
+
Examples
|
506 |
+
========
|
507 |
+
|
508 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
509 |
+
>>> from sympy import QQ
|
510 |
+
>>> A = DomainMatrix([[1, 0],[0, 2]], (2, 2), QQ)
|
511 |
+
>>> A.rep
|
512 |
+
[[1, 0], [0, 2]]
|
513 |
+
>>> B = A.to_sparse()
|
514 |
+
>>> B.rep
|
515 |
+
{0: {0: 1}, 1: {1: 2}}
|
516 |
+
"""
|
517 |
+
if self.rep.fmt == 'sparse':
|
518 |
+
return self
|
519 |
+
|
520 |
+
return self.from_rep(SDM.from_ddm(self.rep))
|
521 |
+
|
522 |
+
def to_dense(self):
|
523 |
+
"""
|
524 |
+
Return a dense DomainMatrix representation of *self*.
|
525 |
+
|
526 |
+
Examples
|
527 |
+
========
|
528 |
+
|
529 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
530 |
+
>>> from sympy import QQ
|
531 |
+
>>> A = DomainMatrix({0: {0: 1}, 1: {1: 2}}, (2, 2), QQ)
|
532 |
+
>>> A.rep
|
533 |
+
{0: {0: 1}, 1: {1: 2}}
|
534 |
+
>>> B = A.to_dense()
|
535 |
+
>>> B.rep
|
536 |
+
[[1, 0], [0, 2]]
|
537 |
+
|
538 |
+
"""
|
539 |
+
if self.rep.fmt == 'dense':
|
540 |
+
return self
|
541 |
+
|
542 |
+
return self.from_rep(SDM.to_ddm(self.rep))
|
543 |
+
|
544 |
+
@classmethod
|
545 |
+
def _unify_domain(cls, *matrices):
|
546 |
+
"""Convert matrices to a common domain"""
|
547 |
+
domains = {matrix.domain for matrix in matrices}
|
548 |
+
if len(domains) == 1:
|
549 |
+
return matrices
|
550 |
+
domain = reduce(lambda x, y: x.unify(y), domains)
|
551 |
+
return tuple(matrix.convert_to(domain) for matrix in matrices)
|
552 |
+
|
553 |
+
@classmethod
|
554 |
+
def _unify_fmt(cls, *matrices, fmt=None):
|
555 |
+
"""Convert matrices to the same format.
|
556 |
+
|
557 |
+
If all matrices have the same format, then return unmodified.
|
558 |
+
Otherwise convert both to the preferred format given as *fmt* which
|
559 |
+
should be 'dense' or 'sparse'.
|
560 |
+
"""
|
561 |
+
formats = {matrix.rep.fmt for matrix in matrices}
|
562 |
+
if len(formats) == 1:
|
563 |
+
return matrices
|
564 |
+
if fmt == 'sparse':
|
565 |
+
return tuple(matrix.to_sparse() for matrix in matrices)
|
566 |
+
elif fmt == 'dense':
|
567 |
+
return tuple(matrix.to_dense() for matrix in matrices)
|
568 |
+
else:
|
569 |
+
raise ValueError("fmt should be 'sparse' or 'dense'")
|
570 |
+
|
571 |
+
def unify(self, *others, fmt=None):
|
572 |
+
"""
|
573 |
+
Unifies the domains and the format of self and other
|
574 |
+
matrices.
|
575 |
+
|
576 |
+
Parameters
|
577 |
+
==========
|
578 |
+
|
579 |
+
others : DomainMatrix
|
580 |
+
|
581 |
+
fmt: string 'dense', 'sparse' or `None` (default)
|
582 |
+
The preferred format to convert to if self and other are not
|
583 |
+
already in the same format. If `None` or not specified then no
|
584 |
+
conversion if performed.
|
585 |
+
|
586 |
+
Returns
|
587 |
+
=======
|
588 |
+
|
589 |
+
Tuple[DomainMatrix]
|
590 |
+
Matrices with unified domain and format
|
591 |
+
|
592 |
+
Examples
|
593 |
+
========
|
594 |
+
|
595 |
+
Unify the domain of DomainMatrix that have different domains:
|
596 |
+
|
597 |
+
>>> from sympy import ZZ, QQ
|
598 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
599 |
+
>>> A = DomainMatrix([[ZZ(1), ZZ(2)]], (1, 2), ZZ)
|
600 |
+
>>> B = DomainMatrix([[QQ(1, 2), QQ(2)]], (1, 2), QQ)
|
601 |
+
>>> Aq, Bq = A.unify(B)
|
602 |
+
>>> Aq
|
603 |
+
DomainMatrix([[1, 2]], (1, 2), QQ)
|
604 |
+
>>> Bq
|
605 |
+
DomainMatrix([[1/2, 2]], (1, 2), QQ)
|
606 |
+
|
607 |
+
Unify the format (dense or sparse):
|
608 |
+
|
609 |
+
>>> A = DomainMatrix([[ZZ(1), ZZ(2)]], (1, 2), ZZ)
|
610 |
+
>>> B = DomainMatrix({0:{0: ZZ(1)}}, (2, 2), ZZ)
|
611 |
+
>>> B.rep
|
612 |
+
{0: {0: 1}}
|
613 |
+
|
614 |
+
>>> A2, B2 = A.unify(B, fmt='dense')
|
615 |
+
>>> B2.rep
|
616 |
+
[[1, 0], [0, 0]]
|
617 |
+
|
618 |
+
See Also
|
619 |
+
========
|
620 |
+
|
621 |
+
convert_to, to_dense, to_sparse
|
622 |
+
|
623 |
+
"""
|
624 |
+
matrices = (self,) + others
|
625 |
+
matrices = DomainMatrix._unify_domain(*matrices)
|
626 |
+
if fmt is not None:
|
627 |
+
matrices = DomainMatrix._unify_fmt(*matrices, fmt=fmt)
|
628 |
+
return matrices
|
629 |
+
|
630 |
+
def to_Matrix(self):
|
631 |
+
r"""
|
632 |
+
Convert DomainMatrix to Matrix
|
633 |
+
|
634 |
+
Returns
|
635 |
+
=======
|
636 |
+
|
637 |
+
Matrix
|
638 |
+
MutableDenseMatrix for the DomainMatrix
|
639 |
+
|
640 |
+
Examples
|
641 |
+
========
|
642 |
+
|
643 |
+
>>> from sympy import ZZ
|
644 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
645 |
+
>>> A = DomainMatrix([
|
646 |
+
... [ZZ(1), ZZ(2)],
|
647 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
648 |
+
|
649 |
+
>>> A.to_Matrix()
|
650 |
+
Matrix([
|
651 |
+
[1, 2],
|
652 |
+
[3, 4]])
|
653 |
+
|
654 |
+
See Also
|
655 |
+
========
|
656 |
+
|
657 |
+
from_Matrix
|
658 |
+
|
659 |
+
"""
|
660 |
+
from sympy.matrices.dense import MutableDenseMatrix
|
661 |
+
elemlist = self.rep.to_list()
|
662 |
+
elements_sympy = [self.domain.to_sympy(e) for row in elemlist for e in row]
|
663 |
+
return MutableDenseMatrix(*self.shape, elements_sympy)
|
664 |
+
|
665 |
+
def to_list(self):
|
666 |
+
return self.rep.to_list()
|
667 |
+
|
668 |
+
def to_list_flat(self):
|
669 |
+
return self.rep.to_list_flat()
|
670 |
+
|
671 |
+
def to_dok(self):
|
672 |
+
return self.rep.to_dok()
|
673 |
+
|
674 |
+
def __repr__(self):
|
675 |
+
return 'DomainMatrix(%s, %r, %r)' % (str(self.rep), self.shape, self.domain)
|
676 |
+
|
677 |
+
def transpose(self):
|
678 |
+
"""Matrix transpose of ``self``"""
|
679 |
+
return self.from_rep(self.rep.transpose())
|
680 |
+
|
681 |
+
def flat(self):
|
682 |
+
rows, cols = self.shape
|
683 |
+
return [self[i,j].element for i in range(rows) for j in range(cols)]
|
684 |
+
|
685 |
+
@property
|
686 |
+
def is_zero_matrix(self):
|
687 |
+
return self.rep.is_zero_matrix()
|
688 |
+
|
689 |
+
@property
|
690 |
+
def is_upper(self):
|
691 |
+
"""
|
692 |
+
Says whether this matrix is upper-triangular. True can be returned
|
693 |
+
even if the matrix is not square.
|
694 |
+
"""
|
695 |
+
return self.rep.is_upper()
|
696 |
+
|
697 |
+
@property
|
698 |
+
def is_lower(self):
|
699 |
+
"""
|
700 |
+
Says whether this matrix is lower-triangular. True can be returned
|
701 |
+
even if the matrix is not square.
|
702 |
+
"""
|
703 |
+
return self.rep.is_lower()
|
704 |
+
|
705 |
+
@property
|
706 |
+
def is_square(self):
|
707 |
+
return self.shape[0] == self.shape[1]
|
708 |
+
|
709 |
+
def rank(self):
|
710 |
+
rref, pivots = self.rref()
|
711 |
+
return len(pivots)
|
712 |
+
|
713 |
+
def hstack(A, *B):
|
714 |
+
r"""Horizontally stack the given matrices.
|
715 |
+
|
716 |
+
Parameters
|
717 |
+
==========
|
718 |
+
|
719 |
+
B: DomainMatrix
|
720 |
+
Matrices to stack horizontally.
|
721 |
+
|
722 |
+
Returns
|
723 |
+
=======
|
724 |
+
|
725 |
+
DomainMatrix
|
726 |
+
DomainMatrix by stacking horizontally.
|
727 |
+
|
728 |
+
Examples
|
729 |
+
========
|
730 |
+
|
731 |
+
>>> from sympy import ZZ
|
732 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
733 |
+
|
734 |
+
>>> A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
735 |
+
>>> B = DomainMatrix([[ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (2, 2), ZZ)
|
736 |
+
>>> A.hstack(B)
|
737 |
+
DomainMatrix([[1, 2, 5, 6], [3, 4, 7, 8]], (2, 4), ZZ)
|
738 |
+
|
739 |
+
>>> C = DomainMatrix([[ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (2, 2), ZZ)
|
740 |
+
>>> A.hstack(B, C)
|
741 |
+
DomainMatrix([[1, 2, 5, 6, 9, 10], [3, 4, 7, 8, 11, 12]], (2, 6), ZZ)
|
742 |
+
|
743 |
+
See Also
|
744 |
+
========
|
745 |
+
|
746 |
+
unify
|
747 |
+
"""
|
748 |
+
A, *B = A.unify(*B, fmt='dense')
|
749 |
+
return DomainMatrix.from_rep(A.rep.hstack(*(Bk.rep for Bk in B)))
|
750 |
+
|
751 |
+
def vstack(A, *B):
|
752 |
+
r"""Vertically stack the given matrices.
|
753 |
+
|
754 |
+
Parameters
|
755 |
+
==========
|
756 |
+
|
757 |
+
B: DomainMatrix
|
758 |
+
Matrices to stack vertically.
|
759 |
+
|
760 |
+
Returns
|
761 |
+
=======
|
762 |
+
|
763 |
+
DomainMatrix
|
764 |
+
DomainMatrix by stacking vertically.
|
765 |
+
|
766 |
+
Examples
|
767 |
+
========
|
768 |
+
|
769 |
+
>>> from sympy import ZZ
|
770 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
771 |
+
|
772 |
+
>>> A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
773 |
+
>>> B = DomainMatrix([[ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (2, 2), ZZ)
|
774 |
+
>>> A.vstack(B)
|
775 |
+
DomainMatrix([[1, 2], [3, 4], [5, 6], [7, 8]], (4, 2), ZZ)
|
776 |
+
|
777 |
+
>>> C = DomainMatrix([[ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (2, 2), ZZ)
|
778 |
+
>>> A.vstack(B, C)
|
779 |
+
DomainMatrix([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], (6, 2), ZZ)
|
780 |
+
|
781 |
+
See Also
|
782 |
+
========
|
783 |
+
|
784 |
+
unify
|
785 |
+
"""
|
786 |
+
A, *B = A.unify(*B, fmt='dense')
|
787 |
+
return DomainMatrix.from_rep(A.rep.vstack(*(Bk.rep for Bk in B)))
|
788 |
+
|
789 |
+
def applyfunc(self, func, domain=None):
|
790 |
+
if domain is None:
|
791 |
+
domain = self.domain
|
792 |
+
return self.from_rep(self.rep.applyfunc(func, domain))
|
793 |
+
|
794 |
+
def __add__(A, B):
|
795 |
+
if not isinstance(B, DomainMatrix):
|
796 |
+
return NotImplemented
|
797 |
+
A, B = A.unify(B, fmt='dense')
|
798 |
+
return A.add(B)
|
799 |
+
|
800 |
+
def __sub__(A, B):
|
801 |
+
if not isinstance(B, DomainMatrix):
|
802 |
+
return NotImplemented
|
803 |
+
A, B = A.unify(B, fmt='dense')
|
804 |
+
return A.sub(B)
|
805 |
+
|
806 |
+
def __neg__(A):
|
807 |
+
return A.neg()
|
808 |
+
|
809 |
+
def __mul__(A, B):
|
810 |
+
"""A * B"""
|
811 |
+
if isinstance(B, DomainMatrix):
|
812 |
+
A, B = A.unify(B, fmt='dense')
|
813 |
+
return A.matmul(B)
|
814 |
+
elif B in A.domain:
|
815 |
+
return A.scalarmul(B)
|
816 |
+
elif isinstance(B, DomainScalar):
|
817 |
+
A, B = A.unify(B)
|
818 |
+
return A.scalarmul(B.element)
|
819 |
+
else:
|
820 |
+
return NotImplemented
|
821 |
+
|
822 |
+
def __rmul__(A, B):
|
823 |
+
if B in A.domain:
|
824 |
+
return A.rscalarmul(B)
|
825 |
+
elif isinstance(B, DomainScalar):
|
826 |
+
A, B = A.unify(B)
|
827 |
+
return A.rscalarmul(B.element)
|
828 |
+
else:
|
829 |
+
return NotImplemented
|
830 |
+
|
831 |
+
def __pow__(A, n):
|
832 |
+
"""A ** n"""
|
833 |
+
if not isinstance(n, int):
|
834 |
+
return NotImplemented
|
835 |
+
return A.pow(n)
|
836 |
+
|
837 |
+
def _check(a, op, b, ashape, bshape):
|
838 |
+
if a.domain != b.domain:
|
839 |
+
msg = "Domain mismatch: %s %s %s" % (a.domain, op, b.domain)
|
840 |
+
raise DMDomainError(msg)
|
841 |
+
if ashape != bshape:
|
842 |
+
msg = "Shape mismatch: %s %s %s" % (a.shape, op, b.shape)
|
843 |
+
raise DMShapeError(msg)
|
844 |
+
if a.rep.fmt != b.rep.fmt:
|
845 |
+
msg = "Format mismatch: %s %s %s" % (a.rep.fmt, op, b.rep.fmt)
|
846 |
+
raise DMFormatError(msg)
|
847 |
+
|
848 |
+
def add(A, B):
|
849 |
+
r"""
|
850 |
+
Adds two DomainMatrix matrices of the same Domain
|
851 |
+
|
852 |
+
Parameters
|
853 |
+
==========
|
854 |
+
|
855 |
+
A, B: DomainMatrix
|
856 |
+
matrices to add
|
857 |
+
|
858 |
+
Returns
|
859 |
+
=======
|
860 |
+
|
861 |
+
DomainMatrix
|
862 |
+
DomainMatrix after Addition
|
863 |
+
|
864 |
+
Raises
|
865 |
+
======
|
866 |
+
|
867 |
+
DMShapeError
|
868 |
+
If the dimensions of the two DomainMatrix are not equal
|
869 |
+
|
870 |
+
ValueError
|
871 |
+
If the domain of the two DomainMatrix are not same
|
872 |
+
|
873 |
+
Examples
|
874 |
+
========
|
875 |
+
|
876 |
+
>>> from sympy import ZZ
|
877 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
878 |
+
>>> A = DomainMatrix([
|
879 |
+
... [ZZ(1), ZZ(2)],
|
880 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
881 |
+
>>> B = DomainMatrix([
|
882 |
+
... [ZZ(4), ZZ(3)],
|
883 |
+
... [ZZ(2), ZZ(1)]], (2, 2), ZZ)
|
884 |
+
|
885 |
+
>>> A.add(B)
|
886 |
+
DomainMatrix([[5, 5], [5, 5]], (2, 2), ZZ)
|
887 |
+
|
888 |
+
See Also
|
889 |
+
========
|
890 |
+
|
891 |
+
sub, matmul
|
892 |
+
|
893 |
+
"""
|
894 |
+
A._check('+', B, A.shape, B.shape)
|
895 |
+
return A.from_rep(A.rep.add(B.rep))
|
896 |
+
|
897 |
+
|
898 |
+
def sub(A, B):
|
899 |
+
r"""
|
900 |
+
Subtracts two DomainMatrix matrices of the same Domain
|
901 |
+
|
902 |
+
Parameters
|
903 |
+
==========
|
904 |
+
|
905 |
+
A, B: DomainMatrix
|
906 |
+
matrices to subtract
|
907 |
+
|
908 |
+
Returns
|
909 |
+
=======
|
910 |
+
|
911 |
+
DomainMatrix
|
912 |
+
DomainMatrix after Subtraction
|
913 |
+
|
914 |
+
Raises
|
915 |
+
======
|
916 |
+
|
917 |
+
DMShapeError
|
918 |
+
If the dimensions of the two DomainMatrix are not equal
|
919 |
+
|
920 |
+
ValueError
|
921 |
+
If the domain of the two DomainMatrix are not same
|
922 |
+
|
923 |
+
Examples
|
924 |
+
========
|
925 |
+
|
926 |
+
>>> from sympy import ZZ
|
927 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
928 |
+
>>> A = DomainMatrix([
|
929 |
+
... [ZZ(1), ZZ(2)],
|
930 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
931 |
+
>>> B = DomainMatrix([
|
932 |
+
... [ZZ(4), ZZ(3)],
|
933 |
+
... [ZZ(2), ZZ(1)]], (2, 2), ZZ)
|
934 |
+
|
935 |
+
>>> A.sub(B)
|
936 |
+
DomainMatrix([[-3, -1], [1, 3]], (2, 2), ZZ)
|
937 |
+
|
938 |
+
See Also
|
939 |
+
========
|
940 |
+
|
941 |
+
add, matmul
|
942 |
+
|
943 |
+
"""
|
944 |
+
A._check('-', B, A.shape, B.shape)
|
945 |
+
return A.from_rep(A.rep.sub(B.rep))
|
946 |
+
|
947 |
+
def neg(A):
|
948 |
+
r"""
|
949 |
+
Returns the negative of DomainMatrix
|
950 |
+
|
951 |
+
Parameters
|
952 |
+
==========
|
953 |
+
|
954 |
+
A : Represents a DomainMatrix
|
955 |
+
|
956 |
+
Returns
|
957 |
+
=======
|
958 |
+
|
959 |
+
DomainMatrix
|
960 |
+
DomainMatrix after Negation
|
961 |
+
|
962 |
+
Examples
|
963 |
+
========
|
964 |
+
|
965 |
+
>>> from sympy import ZZ
|
966 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
967 |
+
>>> A = DomainMatrix([
|
968 |
+
... [ZZ(1), ZZ(2)],
|
969 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
970 |
+
|
971 |
+
>>> A.neg()
|
972 |
+
DomainMatrix([[-1, -2], [-3, -4]], (2, 2), ZZ)
|
973 |
+
|
974 |
+
"""
|
975 |
+
return A.from_rep(A.rep.neg())
|
976 |
+
|
977 |
+
def mul(A, b):
|
978 |
+
r"""
|
979 |
+
Performs term by term multiplication for the second DomainMatrix
|
980 |
+
w.r.t first DomainMatrix. Returns a DomainMatrix whose rows are
|
981 |
+
list of DomainMatrix matrices created after term by term multiplication.
|
982 |
+
|
983 |
+
Parameters
|
984 |
+
==========
|
985 |
+
|
986 |
+
A, B: DomainMatrix
|
987 |
+
matrices to multiply term-wise
|
988 |
+
|
989 |
+
Returns
|
990 |
+
=======
|
991 |
+
|
992 |
+
DomainMatrix
|
993 |
+
DomainMatrix after term by term multiplication
|
994 |
+
|
995 |
+
Examples
|
996 |
+
========
|
997 |
+
|
998 |
+
>>> from sympy import ZZ
|
999 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1000 |
+
>>> A = DomainMatrix([
|
1001 |
+
... [ZZ(1), ZZ(2)],
|
1002 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
1003 |
+
>>> B = DomainMatrix([
|
1004 |
+
... [ZZ(1), ZZ(1)],
|
1005 |
+
... [ZZ(0), ZZ(1)]], (2, 2), ZZ)
|
1006 |
+
|
1007 |
+
>>> A.mul(B)
|
1008 |
+
DomainMatrix([[DomainMatrix([[1, 1], [0, 1]], (2, 2), ZZ),
|
1009 |
+
DomainMatrix([[2, 2], [0, 2]], (2, 2), ZZ)],
|
1010 |
+
[DomainMatrix([[3, 3], [0, 3]], (2, 2), ZZ),
|
1011 |
+
DomainMatrix([[4, 4], [0, 4]], (2, 2), ZZ)]], (2, 2), ZZ)
|
1012 |
+
|
1013 |
+
See Also
|
1014 |
+
========
|
1015 |
+
|
1016 |
+
matmul
|
1017 |
+
|
1018 |
+
"""
|
1019 |
+
return A.from_rep(A.rep.mul(b))
|
1020 |
+
|
1021 |
+
def rmul(A, b):
|
1022 |
+
return A.from_rep(A.rep.rmul(b))
|
1023 |
+
|
1024 |
+
def matmul(A, B):
|
1025 |
+
r"""
|
1026 |
+
Performs matrix multiplication of two DomainMatrix matrices
|
1027 |
+
|
1028 |
+
Parameters
|
1029 |
+
==========
|
1030 |
+
|
1031 |
+
A, B: DomainMatrix
|
1032 |
+
to multiply
|
1033 |
+
|
1034 |
+
Returns
|
1035 |
+
=======
|
1036 |
+
|
1037 |
+
DomainMatrix
|
1038 |
+
DomainMatrix after multiplication
|
1039 |
+
|
1040 |
+
Examples
|
1041 |
+
========
|
1042 |
+
|
1043 |
+
>>> from sympy import ZZ
|
1044 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1045 |
+
>>> A = DomainMatrix([
|
1046 |
+
... [ZZ(1), ZZ(2)],
|
1047 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
1048 |
+
>>> B = DomainMatrix([
|
1049 |
+
... [ZZ(1), ZZ(1)],
|
1050 |
+
... [ZZ(0), ZZ(1)]], (2, 2), ZZ)
|
1051 |
+
|
1052 |
+
>>> A.matmul(B)
|
1053 |
+
DomainMatrix([[1, 3], [3, 7]], (2, 2), ZZ)
|
1054 |
+
|
1055 |
+
See Also
|
1056 |
+
========
|
1057 |
+
|
1058 |
+
mul, pow, add, sub
|
1059 |
+
|
1060 |
+
"""
|
1061 |
+
|
1062 |
+
A._check('*', B, A.shape[1], B.shape[0])
|
1063 |
+
return A.from_rep(A.rep.matmul(B.rep))
|
1064 |
+
|
1065 |
+
def _scalarmul(A, lamda, reverse):
|
1066 |
+
if lamda == A.domain.zero:
|
1067 |
+
return DomainMatrix.zeros(A.shape, A.domain)
|
1068 |
+
elif lamda == A.domain.one:
|
1069 |
+
return A.copy()
|
1070 |
+
elif reverse:
|
1071 |
+
return A.rmul(lamda)
|
1072 |
+
else:
|
1073 |
+
return A.mul(lamda)
|
1074 |
+
|
1075 |
+
def scalarmul(A, lamda):
|
1076 |
+
return A._scalarmul(lamda, reverse=False)
|
1077 |
+
|
1078 |
+
def rscalarmul(A, lamda):
|
1079 |
+
return A._scalarmul(lamda, reverse=True)
|
1080 |
+
|
1081 |
+
def mul_elementwise(A, B):
|
1082 |
+
assert A.domain == B.domain
|
1083 |
+
return A.from_rep(A.rep.mul_elementwise(B.rep))
|
1084 |
+
|
1085 |
+
def __truediv__(A, lamda):
|
1086 |
+
""" Method for Scalar Division"""
|
1087 |
+
if isinstance(lamda, int) or ZZ.of_type(lamda):
|
1088 |
+
lamda = DomainScalar(ZZ(lamda), ZZ)
|
1089 |
+
|
1090 |
+
if not isinstance(lamda, DomainScalar):
|
1091 |
+
return NotImplemented
|
1092 |
+
|
1093 |
+
A, lamda = A.to_field().unify(lamda)
|
1094 |
+
if lamda.element == lamda.domain.zero:
|
1095 |
+
raise ZeroDivisionError
|
1096 |
+
if lamda.element == lamda.domain.one:
|
1097 |
+
return A.to_field()
|
1098 |
+
|
1099 |
+
return A.mul(1 / lamda.element)
|
1100 |
+
|
1101 |
+
def pow(A, n):
|
1102 |
+
r"""
|
1103 |
+
Computes A**n
|
1104 |
+
|
1105 |
+
Parameters
|
1106 |
+
==========
|
1107 |
+
|
1108 |
+
A : DomainMatrix
|
1109 |
+
|
1110 |
+
n : exponent for A
|
1111 |
+
|
1112 |
+
Returns
|
1113 |
+
=======
|
1114 |
+
|
1115 |
+
DomainMatrix
|
1116 |
+
DomainMatrix on computing A**n
|
1117 |
+
|
1118 |
+
Raises
|
1119 |
+
======
|
1120 |
+
|
1121 |
+
NotImplementedError
|
1122 |
+
if n is negative.
|
1123 |
+
|
1124 |
+
Examples
|
1125 |
+
========
|
1126 |
+
|
1127 |
+
>>> from sympy import ZZ
|
1128 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1129 |
+
>>> A = DomainMatrix([
|
1130 |
+
... [ZZ(1), ZZ(1)],
|
1131 |
+
... [ZZ(0), ZZ(1)]], (2, 2), ZZ)
|
1132 |
+
|
1133 |
+
>>> A.pow(2)
|
1134 |
+
DomainMatrix([[1, 2], [0, 1]], (2, 2), ZZ)
|
1135 |
+
|
1136 |
+
See Also
|
1137 |
+
========
|
1138 |
+
|
1139 |
+
matmul
|
1140 |
+
|
1141 |
+
"""
|
1142 |
+
nrows, ncols = A.shape
|
1143 |
+
if nrows != ncols:
|
1144 |
+
raise DMNonSquareMatrixError('Power of a nonsquare matrix')
|
1145 |
+
if n < 0:
|
1146 |
+
raise NotImplementedError('Negative powers')
|
1147 |
+
elif n == 0:
|
1148 |
+
return A.eye(nrows, A.domain)
|
1149 |
+
elif n == 1:
|
1150 |
+
return A
|
1151 |
+
elif n % 2 == 1:
|
1152 |
+
return A * A**(n - 1)
|
1153 |
+
else:
|
1154 |
+
sqrtAn = A ** (n // 2)
|
1155 |
+
return sqrtAn * sqrtAn
|
1156 |
+
|
1157 |
+
def scc(self):
|
1158 |
+
"""Compute the strongly connected components of a DomainMatrix
|
1159 |
+
|
1160 |
+
Explanation
|
1161 |
+
===========
|
1162 |
+
|
1163 |
+
A square matrix can be considered as the adjacency matrix for a
|
1164 |
+
directed graph where the row and column indices are the vertices. In
|
1165 |
+
this graph if there is an edge from vertex ``i`` to vertex ``j`` if
|
1166 |
+
``M[i, j]`` is nonzero. This routine computes the strongly connected
|
1167 |
+
components of that graph which are subsets of the rows and columns that
|
1168 |
+
are connected by some nonzero element of the matrix. The strongly
|
1169 |
+
connected components are useful because many operations such as the
|
1170 |
+
determinant can be computed by working with the submatrices
|
1171 |
+
corresponding to each component.
|
1172 |
+
|
1173 |
+
Examples
|
1174 |
+
========
|
1175 |
+
|
1176 |
+
Find the strongly connected components of a matrix:
|
1177 |
+
|
1178 |
+
>>> from sympy import ZZ
|
1179 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1180 |
+
>>> M = DomainMatrix([[ZZ(1), ZZ(0), ZZ(2)],
|
1181 |
+
... [ZZ(0), ZZ(3), ZZ(0)],
|
1182 |
+
... [ZZ(4), ZZ(6), ZZ(5)]], (3, 3), ZZ)
|
1183 |
+
>>> M.scc()
|
1184 |
+
[[1], [0, 2]]
|
1185 |
+
|
1186 |
+
Compute the determinant from the components:
|
1187 |
+
|
1188 |
+
>>> MM = M.to_Matrix()
|
1189 |
+
>>> MM
|
1190 |
+
Matrix([
|
1191 |
+
[1, 0, 2],
|
1192 |
+
[0, 3, 0],
|
1193 |
+
[4, 6, 5]])
|
1194 |
+
>>> MM[[1], [1]]
|
1195 |
+
Matrix([[3]])
|
1196 |
+
>>> MM[[0, 2], [0, 2]]
|
1197 |
+
Matrix([
|
1198 |
+
[1, 2],
|
1199 |
+
[4, 5]])
|
1200 |
+
>>> MM.det()
|
1201 |
+
-9
|
1202 |
+
>>> MM[[1], [1]].det() * MM[[0, 2], [0, 2]].det()
|
1203 |
+
-9
|
1204 |
+
|
1205 |
+
The components are given in reverse topological order and represent a
|
1206 |
+
permutation of the rows and columns that will bring the matrix into
|
1207 |
+
block lower-triangular form:
|
1208 |
+
|
1209 |
+
>>> MM[[1, 0, 2], [1, 0, 2]]
|
1210 |
+
Matrix([
|
1211 |
+
[3, 0, 0],
|
1212 |
+
[0, 1, 2],
|
1213 |
+
[6, 4, 5]])
|
1214 |
+
|
1215 |
+
Returns
|
1216 |
+
=======
|
1217 |
+
|
1218 |
+
List of lists of integers
|
1219 |
+
Each list represents a strongly connected component.
|
1220 |
+
|
1221 |
+
See also
|
1222 |
+
========
|
1223 |
+
|
1224 |
+
sympy.matrices.matrices.MatrixBase.strongly_connected_components
|
1225 |
+
sympy.utilities.iterables.strongly_connected_components
|
1226 |
+
|
1227 |
+
"""
|
1228 |
+
rows, cols = self.shape
|
1229 |
+
assert rows == cols
|
1230 |
+
return self.rep.scc()
|
1231 |
+
|
1232 |
+
def rref(self):
|
1233 |
+
r"""
|
1234 |
+
Returns reduced-row echelon form and list of pivots for the DomainMatrix
|
1235 |
+
|
1236 |
+
Returns
|
1237 |
+
=======
|
1238 |
+
|
1239 |
+
(DomainMatrix, list)
|
1240 |
+
reduced-row echelon form and list of pivots for the DomainMatrix
|
1241 |
+
|
1242 |
+
Raises
|
1243 |
+
======
|
1244 |
+
|
1245 |
+
ValueError
|
1246 |
+
If the domain of DomainMatrix not a Field
|
1247 |
+
|
1248 |
+
Examples
|
1249 |
+
========
|
1250 |
+
|
1251 |
+
>>> from sympy import QQ
|
1252 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1253 |
+
>>> A = DomainMatrix([
|
1254 |
+
... [QQ(2), QQ(-1), QQ(0)],
|
1255 |
+
... [QQ(-1), QQ(2), QQ(-1)],
|
1256 |
+
... [QQ(0), QQ(0), QQ(2)]], (3, 3), QQ)
|
1257 |
+
|
1258 |
+
>>> rref_matrix, rref_pivots = A.rref()
|
1259 |
+
>>> rref_matrix
|
1260 |
+
DomainMatrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], (3, 3), QQ)
|
1261 |
+
>>> rref_pivots
|
1262 |
+
(0, 1, 2)
|
1263 |
+
|
1264 |
+
See Also
|
1265 |
+
========
|
1266 |
+
|
1267 |
+
convert_to, lu
|
1268 |
+
|
1269 |
+
"""
|
1270 |
+
if not self.domain.is_Field:
|
1271 |
+
raise DMNotAField('Not a field')
|
1272 |
+
rref_ddm, pivots = self.rep.rref()
|
1273 |
+
return self.from_rep(rref_ddm), tuple(pivots)
|
1274 |
+
|
1275 |
+
def columnspace(self):
|
1276 |
+
r"""
|
1277 |
+
Returns the columnspace for the DomainMatrix
|
1278 |
+
|
1279 |
+
Returns
|
1280 |
+
=======
|
1281 |
+
|
1282 |
+
DomainMatrix
|
1283 |
+
The columns of this matrix form a basis for the columnspace.
|
1284 |
+
|
1285 |
+
Examples
|
1286 |
+
========
|
1287 |
+
|
1288 |
+
>>> from sympy import QQ
|
1289 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1290 |
+
>>> A = DomainMatrix([
|
1291 |
+
... [QQ(1), QQ(-1)],
|
1292 |
+
... [QQ(2), QQ(-2)]], (2, 2), QQ)
|
1293 |
+
>>> A.columnspace()
|
1294 |
+
DomainMatrix([[1], [2]], (2, 1), QQ)
|
1295 |
+
|
1296 |
+
"""
|
1297 |
+
if not self.domain.is_Field:
|
1298 |
+
raise DMNotAField('Not a field')
|
1299 |
+
rref, pivots = self.rref()
|
1300 |
+
rows, cols = self.shape
|
1301 |
+
return self.extract(range(rows), pivots)
|
1302 |
+
|
1303 |
+
def rowspace(self):
|
1304 |
+
r"""
|
1305 |
+
Returns the rowspace for the DomainMatrix
|
1306 |
+
|
1307 |
+
Returns
|
1308 |
+
=======
|
1309 |
+
|
1310 |
+
DomainMatrix
|
1311 |
+
The rows of this matrix form a basis for the rowspace.
|
1312 |
+
|
1313 |
+
Examples
|
1314 |
+
========
|
1315 |
+
|
1316 |
+
>>> from sympy import QQ
|
1317 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1318 |
+
>>> A = DomainMatrix([
|
1319 |
+
... [QQ(1), QQ(-1)],
|
1320 |
+
... [QQ(2), QQ(-2)]], (2, 2), QQ)
|
1321 |
+
>>> A.rowspace()
|
1322 |
+
DomainMatrix([[1, -1]], (1, 2), QQ)
|
1323 |
+
|
1324 |
+
"""
|
1325 |
+
if not self.domain.is_Field:
|
1326 |
+
raise DMNotAField('Not a field')
|
1327 |
+
rref, pivots = self.rref()
|
1328 |
+
rows, cols = self.shape
|
1329 |
+
return self.extract(range(len(pivots)), range(cols))
|
1330 |
+
|
1331 |
+
def nullspace(self):
|
1332 |
+
r"""
|
1333 |
+
Returns the nullspace for the DomainMatrix
|
1334 |
+
|
1335 |
+
Returns
|
1336 |
+
=======
|
1337 |
+
|
1338 |
+
DomainMatrix
|
1339 |
+
The rows of this matrix form a basis for the nullspace.
|
1340 |
+
|
1341 |
+
Examples
|
1342 |
+
========
|
1343 |
+
|
1344 |
+
>>> from sympy import QQ
|
1345 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1346 |
+
>>> A = DomainMatrix([
|
1347 |
+
... [QQ(1), QQ(-1)],
|
1348 |
+
... [QQ(2), QQ(-2)]], (2, 2), QQ)
|
1349 |
+
>>> A.nullspace()
|
1350 |
+
DomainMatrix([[1, 1]], (1, 2), QQ)
|
1351 |
+
|
1352 |
+
"""
|
1353 |
+
if not self.domain.is_Field:
|
1354 |
+
raise DMNotAField('Not a field')
|
1355 |
+
return self.from_rep(self.rep.nullspace()[0])
|
1356 |
+
|
1357 |
+
def inv(self):
|
1358 |
+
r"""
|
1359 |
+
Finds the inverse of the DomainMatrix if exists
|
1360 |
+
|
1361 |
+
Returns
|
1362 |
+
=======
|
1363 |
+
|
1364 |
+
DomainMatrix
|
1365 |
+
DomainMatrix after inverse
|
1366 |
+
|
1367 |
+
Raises
|
1368 |
+
======
|
1369 |
+
|
1370 |
+
ValueError
|
1371 |
+
If the domain of DomainMatrix not a Field
|
1372 |
+
|
1373 |
+
DMNonSquareMatrixError
|
1374 |
+
If the DomainMatrix is not a not Square DomainMatrix
|
1375 |
+
|
1376 |
+
Examples
|
1377 |
+
========
|
1378 |
+
|
1379 |
+
>>> from sympy import QQ
|
1380 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1381 |
+
>>> A = DomainMatrix([
|
1382 |
+
... [QQ(2), QQ(-1), QQ(0)],
|
1383 |
+
... [QQ(-1), QQ(2), QQ(-1)],
|
1384 |
+
... [QQ(0), QQ(0), QQ(2)]], (3, 3), QQ)
|
1385 |
+
>>> A.inv()
|
1386 |
+
DomainMatrix([[2/3, 1/3, 1/6], [1/3, 2/3, 1/3], [0, 0, 1/2]], (3, 3), QQ)
|
1387 |
+
|
1388 |
+
See Also
|
1389 |
+
========
|
1390 |
+
|
1391 |
+
neg
|
1392 |
+
|
1393 |
+
"""
|
1394 |
+
if not self.domain.is_Field:
|
1395 |
+
raise DMNotAField('Not a field')
|
1396 |
+
m, n = self.shape
|
1397 |
+
if m != n:
|
1398 |
+
raise DMNonSquareMatrixError
|
1399 |
+
inv = self.rep.inv()
|
1400 |
+
return self.from_rep(inv)
|
1401 |
+
|
1402 |
+
def det(self):
|
1403 |
+
r"""
|
1404 |
+
Returns the determinant of a Square DomainMatrix
|
1405 |
+
|
1406 |
+
Returns
|
1407 |
+
=======
|
1408 |
+
|
1409 |
+
S.Complexes
|
1410 |
+
determinant of Square DomainMatrix
|
1411 |
+
|
1412 |
+
Raises
|
1413 |
+
======
|
1414 |
+
|
1415 |
+
ValueError
|
1416 |
+
If the domain of DomainMatrix not a Field
|
1417 |
+
|
1418 |
+
Examples
|
1419 |
+
========
|
1420 |
+
|
1421 |
+
>>> from sympy import ZZ
|
1422 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1423 |
+
>>> A = DomainMatrix([
|
1424 |
+
... [ZZ(1), ZZ(2)],
|
1425 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
1426 |
+
|
1427 |
+
>>> A.det()
|
1428 |
+
-2
|
1429 |
+
|
1430 |
+
"""
|
1431 |
+
m, n = self.shape
|
1432 |
+
if m != n:
|
1433 |
+
raise DMNonSquareMatrixError
|
1434 |
+
return self.rep.det()
|
1435 |
+
|
1436 |
+
def lu(self):
|
1437 |
+
r"""
|
1438 |
+
Returns Lower and Upper decomposition of the DomainMatrix
|
1439 |
+
|
1440 |
+
Returns
|
1441 |
+
=======
|
1442 |
+
|
1443 |
+
(L, U, exchange)
|
1444 |
+
L, U are Lower and Upper decomposition of the DomainMatrix,
|
1445 |
+
exchange is the list of indices of rows exchanged in the decomposition.
|
1446 |
+
|
1447 |
+
Raises
|
1448 |
+
======
|
1449 |
+
|
1450 |
+
ValueError
|
1451 |
+
If the domain of DomainMatrix not a Field
|
1452 |
+
|
1453 |
+
Examples
|
1454 |
+
========
|
1455 |
+
|
1456 |
+
>>> from sympy import QQ
|
1457 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1458 |
+
>>> A = DomainMatrix([
|
1459 |
+
... [QQ(1), QQ(-1)],
|
1460 |
+
... [QQ(2), QQ(-2)]], (2, 2), QQ)
|
1461 |
+
>>> A.lu()
|
1462 |
+
(DomainMatrix([[1, 0], [2, 1]], (2, 2), QQ), DomainMatrix([[1, -1], [0, 0]], (2, 2), QQ), [])
|
1463 |
+
|
1464 |
+
See Also
|
1465 |
+
========
|
1466 |
+
|
1467 |
+
lu_solve
|
1468 |
+
|
1469 |
+
"""
|
1470 |
+
if not self.domain.is_Field:
|
1471 |
+
raise DMNotAField('Not a field')
|
1472 |
+
L, U, swaps = self.rep.lu()
|
1473 |
+
return self.from_rep(L), self.from_rep(U), swaps
|
1474 |
+
|
1475 |
+
def lu_solve(self, rhs):
|
1476 |
+
r"""
|
1477 |
+
Solver for DomainMatrix x in the A*x = B
|
1478 |
+
|
1479 |
+
Parameters
|
1480 |
+
==========
|
1481 |
+
|
1482 |
+
rhs : DomainMatrix B
|
1483 |
+
|
1484 |
+
Returns
|
1485 |
+
=======
|
1486 |
+
|
1487 |
+
DomainMatrix
|
1488 |
+
x in A*x = B
|
1489 |
+
|
1490 |
+
Raises
|
1491 |
+
======
|
1492 |
+
|
1493 |
+
DMShapeError
|
1494 |
+
If the DomainMatrix A and rhs have different number of rows
|
1495 |
+
|
1496 |
+
ValueError
|
1497 |
+
If the domain of DomainMatrix A not a Field
|
1498 |
+
|
1499 |
+
Examples
|
1500 |
+
========
|
1501 |
+
|
1502 |
+
>>> from sympy import QQ
|
1503 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1504 |
+
>>> A = DomainMatrix([
|
1505 |
+
... [QQ(1), QQ(2)],
|
1506 |
+
... [QQ(3), QQ(4)]], (2, 2), QQ)
|
1507 |
+
>>> B = DomainMatrix([
|
1508 |
+
... [QQ(1), QQ(1)],
|
1509 |
+
... [QQ(0), QQ(1)]], (2, 2), QQ)
|
1510 |
+
|
1511 |
+
>>> A.lu_solve(B)
|
1512 |
+
DomainMatrix([[-2, -1], [3/2, 1]], (2, 2), QQ)
|
1513 |
+
|
1514 |
+
See Also
|
1515 |
+
========
|
1516 |
+
|
1517 |
+
lu
|
1518 |
+
|
1519 |
+
"""
|
1520 |
+
if self.shape[0] != rhs.shape[0]:
|
1521 |
+
raise DMShapeError("Shape")
|
1522 |
+
if not self.domain.is_Field:
|
1523 |
+
raise DMNotAField('Not a field')
|
1524 |
+
sol = self.rep.lu_solve(rhs.rep)
|
1525 |
+
return self.from_rep(sol)
|
1526 |
+
|
1527 |
+
def _solve(A, b):
|
1528 |
+
# XXX: Not sure about this method or its signature. It is just created
|
1529 |
+
# because it is needed by the holonomic module.
|
1530 |
+
if A.shape[0] != b.shape[0]:
|
1531 |
+
raise DMShapeError("Shape")
|
1532 |
+
if A.domain != b.domain or not A.domain.is_Field:
|
1533 |
+
raise DMNotAField('Not a field')
|
1534 |
+
Aaug = A.hstack(b)
|
1535 |
+
Arref, pivots = Aaug.rref()
|
1536 |
+
particular = Arref.from_rep(Arref.rep.particular())
|
1537 |
+
nullspace_rep, nonpivots = Arref[:,:-1].rep.nullspace()
|
1538 |
+
nullspace = Arref.from_rep(nullspace_rep)
|
1539 |
+
return particular, nullspace
|
1540 |
+
|
1541 |
+
def charpoly(self):
|
1542 |
+
r"""
|
1543 |
+
Returns the coefficients of the characteristic polynomial
|
1544 |
+
of the DomainMatrix. These elements will be domain elements.
|
1545 |
+
The domain of the elements will be same as domain of the DomainMatrix.
|
1546 |
+
|
1547 |
+
Returns
|
1548 |
+
=======
|
1549 |
+
|
1550 |
+
list
|
1551 |
+
coefficients of the characteristic polynomial
|
1552 |
+
|
1553 |
+
Raises
|
1554 |
+
======
|
1555 |
+
|
1556 |
+
DMNonSquareMatrixError
|
1557 |
+
If the DomainMatrix is not a not Square DomainMatrix
|
1558 |
+
|
1559 |
+
Examples
|
1560 |
+
========
|
1561 |
+
|
1562 |
+
>>> from sympy import ZZ
|
1563 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1564 |
+
>>> A = DomainMatrix([
|
1565 |
+
... [ZZ(1), ZZ(2)],
|
1566 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
1567 |
+
|
1568 |
+
>>> A.charpoly()
|
1569 |
+
[1, -5, -2]
|
1570 |
+
|
1571 |
+
"""
|
1572 |
+
m, n = self.shape
|
1573 |
+
if m != n:
|
1574 |
+
raise DMNonSquareMatrixError("not square")
|
1575 |
+
return self.rep.charpoly()
|
1576 |
+
|
1577 |
+
@classmethod
|
1578 |
+
def eye(cls, shape, domain):
|
1579 |
+
r"""
|
1580 |
+
Return identity matrix of size n
|
1581 |
+
|
1582 |
+
Examples
|
1583 |
+
========
|
1584 |
+
|
1585 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1586 |
+
>>> from sympy import QQ
|
1587 |
+
>>> DomainMatrix.eye(3, QQ)
|
1588 |
+
DomainMatrix({0: {0: 1}, 1: {1: 1}, 2: {2: 1}}, (3, 3), QQ)
|
1589 |
+
|
1590 |
+
"""
|
1591 |
+
if isinstance(shape, int):
|
1592 |
+
shape = (shape, shape)
|
1593 |
+
return cls.from_rep(SDM.eye(shape, domain))
|
1594 |
+
|
1595 |
+
@classmethod
|
1596 |
+
def diag(cls, diagonal, domain, shape=None):
|
1597 |
+
r"""
|
1598 |
+
Return diagonal matrix with entries from ``diagonal``.
|
1599 |
+
|
1600 |
+
Examples
|
1601 |
+
========
|
1602 |
+
|
1603 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1604 |
+
>>> from sympy import ZZ
|
1605 |
+
>>> DomainMatrix.diag([ZZ(5), ZZ(6)], ZZ)
|
1606 |
+
DomainMatrix({0: {0: 5}, 1: {1: 6}}, (2, 2), ZZ)
|
1607 |
+
|
1608 |
+
"""
|
1609 |
+
if shape is None:
|
1610 |
+
N = len(diagonal)
|
1611 |
+
shape = (N, N)
|
1612 |
+
return cls.from_rep(SDM.diag(diagonal, domain, shape))
|
1613 |
+
|
1614 |
+
@classmethod
|
1615 |
+
def zeros(cls, shape, domain, *, fmt='sparse'):
|
1616 |
+
"""Returns a zero DomainMatrix of size shape, belonging to the specified domain
|
1617 |
+
|
1618 |
+
Examples
|
1619 |
+
========
|
1620 |
+
|
1621 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1622 |
+
>>> from sympy import QQ
|
1623 |
+
>>> DomainMatrix.zeros((2, 3), QQ)
|
1624 |
+
DomainMatrix({}, (2, 3), QQ)
|
1625 |
+
|
1626 |
+
"""
|
1627 |
+
return cls.from_rep(SDM.zeros(shape, domain))
|
1628 |
+
|
1629 |
+
@classmethod
|
1630 |
+
def ones(cls, shape, domain):
|
1631 |
+
"""Returns a DomainMatrix of 1s, of size shape, belonging to the specified domain
|
1632 |
+
|
1633 |
+
Examples
|
1634 |
+
========
|
1635 |
+
|
1636 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1637 |
+
>>> from sympy import QQ
|
1638 |
+
>>> DomainMatrix.ones((2,3), QQ)
|
1639 |
+
DomainMatrix([[1, 1, 1], [1, 1, 1]], (2, 3), QQ)
|
1640 |
+
|
1641 |
+
"""
|
1642 |
+
return cls.from_rep(DDM.ones(shape, domain))
|
1643 |
+
|
1644 |
+
def __eq__(A, B):
|
1645 |
+
r"""
|
1646 |
+
Checks for two DomainMatrix matrices to be equal or not
|
1647 |
+
|
1648 |
+
Parameters
|
1649 |
+
==========
|
1650 |
+
|
1651 |
+
A, B: DomainMatrix
|
1652 |
+
to check equality
|
1653 |
+
|
1654 |
+
Returns
|
1655 |
+
=======
|
1656 |
+
|
1657 |
+
Boolean
|
1658 |
+
True for equal, else False
|
1659 |
+
|
1660 |
+
Raises
|
1661 |
+
======
|
1662 |
+
|
1663 |
+
NotImplementedError
|
1664 |
+
If B is not a DomainMatrix
|
1665 |
+
|
1666 |
+
Examples
|
1667 |
+
========
|
1668 |
+
|
1669 |
+
>>> from sympy import ZZ
|
1670 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
1671 |
+
>>> A = DomainMatrix([
|
1672 |
+
... [ZZ(1), ZZ(2)],
|
1673 |
+
... [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
1674 |
+
>>> B = DomainMatrix([
|
1675 |
+
... [ZZ(1), ZZ(1)],
|
1676 |
+
... [ZZ(0), ZZ(1)]], (2, 2), ZZ)
|
1677 |
+
>>> A.__eq__(A)
|
1678 |
+
True
|
1679 |
+
>>> A.__eq__(B)
|
1680 |
+
False
|
1681 |
+
|
1682 |
+
"""
|
1683 |
+
if not isinstance(A, type(B)):
|
1684 |
+
return NotImplemented
|
1685 |
+
return A.domain == B.domain and A.rep == B.rep
|
1686 |
+
|
1687 |
+
def unify_eq(A, B):
|
1688 |
+
if A.shape != B.shape:
|
1689 |
+
return False
|
1690 |
+
if A.domain != B.domain:
|
1691 |
+
A, B = A.unify(B)
|
1692 |
+
return A == B
|
1693 |
+
|
1694 |
+
def lll(A, delta=QQ(3, 4)):
|
1695 |
+
"""
|
1696 |
+
Performs the Lenstra–Lenstra–Lovász (LLL) basis reduction algorithm.
|
1697 |
+
See [1]_ and [2]_.
|
1698 |
+
|
1699 |
+
Parameters
|
1700 |
+
==========
|
1701 |
+
|
1702 |
+
delta : QQ, optional
|
1703 |
+
The Lovász parameter. Must be in the interval (0.25, 1), with larger
|
1704 |
+
values producing a more reduced basis. The default is 0.75 for
|
1705 |
+
historical reasons.
|
1706 |
+
|
1707 |
+
Returns
|
1708 |
+
=======
|
1709 |
+
|
1710 |
+
The reduced basis as a DomainMatrix over ZZ.
|
1711 |
+
|
1712 |
+
Throws
|
1713 |
+
======
|
1714 |
+
|
1715 |
+
DMValueError: if delta is not in the range (0.25, 1)
|
1716 |
+
DMShapeError: if the matrix is not of shape (m, n) with m <= n
|
1717 |
+
DMDomainError: if the matrix domain is not ZZ
|
1718 |
+
DMRankError: if the matrix contains linearly dependent rows
|
1719 |
+
|
1720 |
+
Examples
|
1721 |
+
========
|
1722 |
+
|
1723 |
+
>>> from sympy.polys.domains import ZZ, QQ
|
1724 |
+
>>> from sympy.polys.matrices import DM
|
1725 |
+
>>> x = DM([[1, 0, 0, 0, -20160],
|
1726 |
+
... [0, 1, 0, 0, 33768],
|
1727 |
+
... [0, 0, 1, 0, 39578],
|
1728 |
+
... [0, 0, 0, 1, 47757]], ZZ)
|
1729 |
+
>>> y = DM([[10, -3, -2, 8, -4],
|
1730 |
+
... [3, -9, 8, 1, -11],
|
1731 |
+
... [-3, 13, -9, -3, -9],
|
1732 |
+
... [-12, -7, -11, 9, -1]], ZZ)
|
1733 |
+
>>> assert x.lll(delta=QQ(5, 6)) == y
|
1734 |
+
|
1735 |
+
Notes
|
1736 |
+
=====
|
1737 |
+
|
1738 |
+
The implementation is derived from the Maple code given in Figures 4.3
|
1739 |
+
and 4.4 of [3]_ (pp.68-69). It uses the efficient method of only calculating
|
1740 |
+
state updates as they are required.
|
1741 |
+
|
1742 |
+
See also
|
1743 |
+
========
|
1744 |
+
|
1745 |
+
lll_transform
|
1746 |
+
|
1747 |
+
References
|
1748 |
+
==========
|
1749 |
+
|
1750 |
+
.. [1] https://en.wikipedia.org/wiki/Lenstra–Lenstra–Lovász_lattice_basis_reduction_algorithm
|
1751 |
+
.. [2] https://web.archive.org/web/20221029115428/https://web.cs.elte.hu/~lovasz/scans/lll.pdf
|
1752 |
+
.. [3] Murray R. Bremner, "Lattice Basis Reduction: An Introduction to the LLL Algorithm and Its Applications"
|
1753 |
+
|
1754 |
+
"""
|
1755 |
+
return DomainMatrix.from_rep(A.rep.lll(delta=delta))
|
1756 |
+
|
1757 |
+
def lll_transform(A, delta=QQ(3, 4)):
|
1758 |
+
"""
|
1759 |
+
Performs the Lenstra–Lenstra–Lovász (LLL) basis reduction algorithm
|
1760 |
+
and returns the reduced basis and transformation matrix.
|
1761 |
+
|
1762 |
+
Explanation
|
1763 |
+
===========
|
1764 |
+
|
1765 |
+
Parameters, algorithm and basis are the same as for :meth:`lll` except that
|
1766 |
+
the return value is a tuple `(B, T)` with `B` the reduced basis and
|
1767 |
+
`T` a transformation matrix. The original basis `A` is transformed to
|
1768 |
+
`B` with `T*A == B`. If only `B` is needed then :meth:`lll` should be
|
1769 |
+
used as it is a little faster.
|
1770 |
+
|
1771 |
+
Examples
|
1772 |
+
========
|
1773 |
+
|
1774 |
+
>>> from sympy.polys.domains import ZZ, QQ
|
1775 |
+
>>> from sympy.polys.matrices import DM
|
1776 |
+
>>> X = DM([[1, 0, 0, 0, -20160],
|
1777 |
+
... [0, 1, 0, 0, 33768],
|
1778 |
+
... [0, 0, 1, 0, 39578],
|
1779 |
+
... [0, 0, 0, 1, 47757]], ZZ)
|
1780 |
+
>>> B, T = X.lll_transform(delta=QQ(5, 6))
|
1781 |
+
>>> T * X == B
|
1782 |
+
True
|
1783 |
+
|
1784 |
+
See also
|
1785 |
+
========
|
1786 |
+
|
1787 |
+
lll
|
1788 |
+
|
1789 |
+
"""
|
1790 |
+
reduced, transform = A.rep.lll_transform(delta=delta)
|
1791 |
+
return DomainMatrix.from_rep(reduced), DomainMatrix.from_rep(transform)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/domainscalar.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
|
3 |
+
Module for the DomainScalar class.
|
4 |
+
|
5 |
+
A DomainScalar represents an element which is in a particular
|
6 |
+
Domain. The idea is that the DomainScalar class provides the
|
7 |
+
convenience routines for unifying elements with different domains.
|
8 |
+
|
9 |
+
It assists in Scalar Multiplication and getitem for DomainMatrix.
|
10 |
+
|
11 |
+
"""
|
12 |
+
from ..constructor import construct_domain
|
13 |
+
|
14 |
+
from sympy.polys.domains import Domain, ZZ
|
15 |
+
|
16 |
+
|
17 |
+
class DomainScalar:
|
18 |
+
r"""
|
19 |
+
docstring
|
20 |
+
"""
|
21 |
+
|
22 |
+
def __new__(cls, element, domain):
|
23 |
+
if not isinstance(domain, Domain):
|
24 |
+
raise TypeError("domain should be of type Domain")
|
25 |
+
if not domain.of_type(element):
|
26 |
+
raise TypeError("element %s should be in domain %s" % (element, domain))
|
27 |
+
return cls.new(element, domain)
|
28 |
+
|
29 |
+
@classmethod
|
30 |
+
def new(cls, element, domain):
|
31 |
+
obj = super().__new__(cls)
|
32 |
+
obj.element = element
|
33 |
+
obj.domain = domain
|
34 |
+
return obj
|
35 |
+
|
36 |
+
def __repr__(self):
|
37 |
+
return repr(self.element)
|
38 |
+
|
39 |
+
@classmethod
|
40 |
+
def from_sympy(cls, expr):
|
41 |
+
[domain, [element]] = construct_domain([expr])
|
42 |
+
return cls.new(element, domain)
|
43 |
+
|
44 |
+
def to_sympy(self):
|
45 |
+
return self.domain.to_sympy(self.element)
|
46 |
+
|
47 |
+
def to_domain(self, domain):
|
48 |
+
element = domain.convert_from(self.element, self.domain)
|
49 |
+
return self.new(element, domain)
|
50 |
+
|
51 |
+
def convert_to(self, domain):
|
52 |
+
return self.to_domain(domain)
|
53 |
+
|
54 |
+
def unify(self, other):
|
55 |
+
domain = self.domain.unify(other.domain)
|
56 |
+
return self.to_domain(domain), other.to_domain(domain)
|
57 |
+
|
58 |
+
def __add__(self, other):
|
59 |
+
if not isinstance(other, DomainScalar):
|
60 |
+
return NotImplemented
|
61 |
+
self, other = self.unify(other)
|
62 |
+
return self.new(self.element + other.element, self.domain)
|
63 |
+
|
64 |
+
def __sub__(self, other):
|
65 |
+
if not isinstance(other, DomainScalar):
|
66 |
+
return NotImplemented
|
67 |
+
self, other = self.unify(other)
|
68 |
+
return self.new(self.element - other.element, self.domain)
|
69 |
+
|
70 |
+
def __mul__(self, other):
|
71 |
+
if not isinstance(other, DomainScalar):
|
72 |
+
if isinstance(other, int):
|
73 |
+
other = DomainScalar(ZZ(other), ZZ)
|
74 |
+
else:
|
75 |
+
return NotImplemented
|
76 |
+
|
77 |
+
self, other = self.unify(other)
|
78 |
+
return self.new(self.element * other.element, self.domain)
|
79 |
+
|
80 |
+
def __floordiv__(self, other):
|
81 |
+
if not isinstance(other, DomainScalar):
|
82 |
+
return NotImplemented
|
83 |
+
self, other = self.unify(other)
|
84 |
+
return self.new(self.domain.quo(self.element, other.element), self.domain)
|
85 |
+
|
86 |
+
def __mod__(self, other):
|
87 |
+
if not isinstance(other, DomainScalar):
|
88 |
+
return NotImplemented
|
89 |
+
self, other = self.unify(other)
|
90 |
+
return self.new(self.domain.rem(self.element, other.element), self.domain)
|
91 |
+
|
92 |
+
def __divmod__(self, other):
|
93 |
+
if not isinstance(other, DomainScalar):
|
94 |
+
return NotImplemented
|
95 |
+
self, other = self.unify(other)
|
96 |
+
q, r = self.domain.div(self.element, other.element)
|
97 |
+
return (self.new(q, self.domain), self.new(r, self.domain))
|
98 |
+
|
99 |
+
def __pow__(self, n):
|
100 |
+
if not isinstance(n, int):
|
101 |
+
return NotImplemented
|
102 |
+
return self.new(self.element**n, self.domain)
|
103 |
+
|
104 |
+
def __pos__(self):
|
105 |
+
return self.new(+self.element, self.domain)
|
106 |
+
|
107 |
+
def __eq__(self, other):
|
108 |
+
if not isinstance(other, DomainScalar):
|
109 |
+
return NotImplemented
|
110 |
+
return self.element == other.element and self.domain == other.domain
|
111 |
+
|
112 |
+
def is_zero(self):
|
113 |
+
return self.element == self.domain.zero
|
114 |
+
|
115 |
+
def is_one(self):
|
116 |
+
return self.element == self.domain.one
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/eigen.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
|
3 |
+
Routines for computing eigenvectors with DomainMatrix.
|
4 |
+
|
5 |
+
"""
|
6 |
+
from sympy.core.symbol import Dummy
|
7 |
+
|
8 |
+
from ..agca.extensions import FiniteExtension
|
9 |
+
from ..factortools import dup_factor_list
|
10 |
+
from ..polyroots import roots
|
11 |
+
from ..polytools import Poly
|
12 |
+
from ..rootoftools import CRootOf
|
13 |
+
|
14 |
+
from .domainmatrix import DomainMatrix
|
15 |
+
|
16 |
+
|
17 |
+
def dom_eigenvects(A, l=Dummy('lambda')):
|
18 |
+
charpoly = A.charpoly()
|
19 |
+
rows, cols = A.shape
|
20 |
+
domain = A.domain
|
21 |
+
_, factors = dup_factor_list(charpoly, domain)
|
22 |
+
|
23 |
+
rational_eigenvects = []
|
24 |
+
algebraic_eigenvects = []
|
25 |
+
for base, exp in factors:
|
26 |
+
if len(base) == 2:
|
27 |
+
field = domain
|
28 |
+
eigenval = -base[1] / base[0]
|
29 |
+
|
30 |
+
EE_items = [
|
31 |
+
[eigenval if i == j else field.zero for j in range(cols)]
|
32 |
+
for i in range(rows)]
|
33 |
+
EE = DomainMatrix(EE_items, (rows, cols), field)
|
34 |
+
|
35 |
+
basis = (A - EE).nullspace()
|
36 |
+
rational_eigenvects.append((field, eigenval, exp, basis))
|
37 |
+
else:
|
38 |
+
minpoly = Poly.from_list(base, l, domain=domain)
|
39 |
+
field = FiniteExtension(minpoly)
|
40 |
+
eigenval = field(l)
|
41 |
+
|
42 |
+
AA_items = [
|
43 |
+
[Poly.from_list([item], l, domain=domain).rep for item in row]
|
44 |
+
for row in A.rep.to_ddm()]
|
45 |
+
AA_items = [[field(item) for item in row] for row in AA_items]
|
46 |
+
AA = DomainMatrix(AA_items, (rows, cols), field)
|
47 |
+
EE_items = [
|
48 |
+
[eigenval if i == j else field.zero for j in range(cols)]
|
49 |
+
for i in range(rows)]
|
50 |
+
EE = DomainMatrix(EE_items, (rows, cols), field)
|
51 |
+
|
52 |
+
basis = (AA - EE).nullspace()
|
53 |
+
algebraic_eigenvects.append((field, minpoly, exp, basis))
|
54 |
+
|
55 |
+
return rational_eigenvects, algebraic_eigenvects
|
56 |
+
|
57 |
+
|
58 |
+
def dom_eigenvects_to_sympy(
|
59 |
+
rational_eigenvects, algebraic_eigenvects,
|
60 |
+
Matrix, **kwargs
|
61 |
+
):
|
62 |
+
result = []
|
63 |
+
|
64 |
+
for field, eigenvalue, multiplicity, eigenvects in rational_eigenvects:
|
65 |
+
eigenvects = eigenvects.rep.to_ddm()
|
66 |
+
eigenvalue = field.to_sympy(eigenvalue)
|
67 |
+
new_eigenvects = [
|
68 |
+
Matrix([field.to_sympy(x) for x in vect])
|
69 |
+
for vect in eigenvects]
|
70 |
+
result.append((eigenvalue, multiplicity, new_eigenvects))
|
71 |
+
|
72 |
+
for field, minpoly, multiplicity, eigenvects in algebraic_eigenvects:
|
73 |
+
eigenvects = eigenvects.rep.to_ddm()
|
74 |
+
l = minpoly.gens[0]
|
75 |
+
|
76 |
+
eigenvects = [[field.to_sympy(x) for x in vect] for vect in eigenvects]
|
77 |
+
|
78 |
+
degree = minpoly.degree()
|
79 |
+
minpoly = minpoly.as_expr()
|
80 |
+
eigenvals = roots(minpoly, l, **kwargs)
|
81 |
+
if len(eigenvals) != degree:
|
82 |
+
eigenvals = [CRootOf(minpoly, l, idx) for idx in range(degree)]
|
83 |
+
|
84 |
+
for eigenvalue in eigenvals:
|
85 |
+
new_eigenvects = [
|
86 |
+
Matrix([x.subs(l, eigenvalue) for x in vect])
|
87 |
+
for vect in eigenvects]
|
88 |
+
result.append((eigenvalue, multiplicity, new_eigenvects))
|
89 |
+
|
90 |
+
return result
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/matrices/exceptions.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
|
3 |
+
Module to define exceptions to be used in sympy.polys.matrices modules and
|
4 |
+
classes.
|
5 |
+
|
6 |
+
Ideally all exceptions raised in these modules would be defined and documented
|
7 |
+
here and not e.g. imported from matrices. Also ideally generic exceptions like
|
8 |
+
ValueError/TypeError would not be raised anywhere.
|
9 |
+
|
10 |
+
"""
|
11 |
+
|
12 |
+
|
13 |
+
class DMError(Exception):
|
14 |
+
"""Base class for errors raised by DomainMatrix"""
|
15 |
+
pass
|
16 |
+
|
17 |
+
|
18 |
+
class DMBadInputError(DMError):
|
19 |
+
"""list of lists is inconsistent with shape"""
|
20 |
+
pass
|
21 |
+
|
22 |
+
|
23 |
+
class DMDomainError(DMError):
|
24 |
+
"""domains do not match"""
|
25 |
+
pass
|
26 |
+
|
27 |
+
|
28 |
+
class DMNotAField(DMDomainError):
|
29 |
+
"""domain is not a field"""
|
30 |
+
pass
|
31 |
+
|
32 |
+
|
33 |
+
class DMFormatError(DMError):
|
34 |
+
"""mixed dense/sparse not supported"""
|
35 |
+
pass
|
36 |
+
|
37 |
+
|
38 |
+
class DMNonInvertibleMatrixError(DMError):
|
39 |
+
"""The matrix in not invertible"""
|
40 |
+
pass
|
41 |
+
|
42 |
+
|
43 |
+
class DMRankError(DMError):
|
44 |
+
"""matrix does not have expected rank"""
|
45 |
+
pass
|
46 |
+
|
47 |
+
|
48 |
+
class DMShapeError(DMError):
|
49 |
+
"""shapes are inconsistent"""
|
50 |
+
pass
|
51 |
+
|
52 |
+
|
53 |
+
class DMNonSquareMatrixError(DMShapeError):
|
54 |
+
"""The matrix is not square"""
|
55 |
+
pass
|
56 |
+
|
57 |
+
|
58 |
+
class DMValueError(DMError):
|
59 |
+
"""The value passed is invalid"""
|
60 |
+
pass
|
61 |
+
|
62 |
+
|
63 |
+
__all__ = [
|
64 |
+
'DMError', 'DMBadInputError', 'DMDomainError', 'DMFormatError',
|
65 |
+
'DMRankError', 'DMShapeError', 'DMNotAField',
|
66 |
+
'DMNonInvertibleMatrixError', 'DMNonSquareMatrixError', 'DMValueError'
|
67 |
+
]
|