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/algebras/__init__.py +3 -0
- env-llmeval/lib/python3.10/site-packages/sympy/algebras/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/algebras/__pycache__/quaternion.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/algebras/quaternion.py +1673 -0
- env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/__pycache__/test_quaternion.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/test_quaternion.py +409 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__init__.py +57 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/algebraicfield.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/compositedomain.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/domain.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/expressiondomain.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/field.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/finitefield.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gaussiandomains.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/groundtypes.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/integerring.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/modularinteger.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_fractionfield.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_polynomialring.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonfinitefield.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonrationalfield.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/quotientring.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/rationalfield.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/ring.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/field.py +104 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/groundtypes.py +73 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/pythonfinitefield.py +16 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/pythonrational.py +22 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/rationalfield.py +163 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/simpledomain.py +15 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/tests/test_domains.py +1270 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/tests/test_polynomialring.py +102 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/tests/test_quotientring.py +52 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__init__.py +27 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/basis.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/exceptions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/galois_resolvents.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/galoisgroups.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/minpoly.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/modules.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/primes.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/resolvent_lookup.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/subfield.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/utilities.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/basis.py +246 -0
- env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/exceptions.py +53 -0
env-llmeval/lib/python3.10/site-packages/sympy/algebras/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .quaternion import Quaternion
|
2 |
+
|
3 |
+
__all__ = ["Quaternion",]
|
env-llmeval/lib/python3.10/site-packages/sympy/algebras/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (246 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/algebras/__pycache__/quaternion.cpython-310.pyc
ADDED
Binary file (47.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/algebras/quaternion.py
ADDED
@@ -0,0 +1,1673 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import Rational
|
2 |
+
from sympy.core.singleton import S
|
3 |
+
from sympy.core.relational import is_eq
|
4 |
+
from sympy.functions.elementary.complexes import (conjugate, im, re, sign)
|
5 |
+
from sympy.functions.elementary.exponential import (exp, log as ln)
|
6 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
7 |
+
from sympy.functions.elementary.trigonometric import (acos, asin, atan2)
|
8 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
9 |
+
from sympy.simplify.trigsimp import trigsimp
|
10 |
+
from sympy.integrals.integrals import integrate
|
11 |
+
from sympy.matrices.dense import MutableDenseMatrix as Matrix
|
12 |
+
from sympy.core.sympify import sympify, _sympify
|
13 |
+
from sympy.core.expr import Expr
|
14 |
+
from sympy.core.logic import fuzzy_not, fuzzy_or
|
15 |
+
|
16 |
+
from mpmath.libmp.libmpf import prec_to_dps
|
17 |
+
|
18 |
+
|
19 |
+
def _check_norm(elements, norm):
|
20 |
+
"""validate if input norm is consistent"""
|
21 |
+
if norm is not None and norm.is_number:
|
22 |
+
if norm.is_positive is False:
|
23 |
+
raise ValueError("Input norm must be positive.")
|
24 |
+
|
25 |
+
numerical = all(i.is_number and i.is_real is True for i in elements)
|
26 |
+
if numerical and is_eq(norm**2, sum(i**2 for i in elements)) is False:
|
27 |
+
raise ValueError("Incompatible value for norm.")
|
28 |
+
|
29 |
+
|
30 |
+
def _is_extrinsic(seq):
|
31 |
+
"""validate seq and return True if seq is lowercase and False if uppercase"""
|
32 |
+
if type(seq) != str:
|
33 |
+
raise ValueError('Expected seq to be a string.')
|
34 |
+
if len(seq) != 3:
|
35 |
+
raise ValueError("Expected 3 axes, got `{}`.".format(seq))
|
36 |
+
|
37 |
+
intrinsic = seq.isupper()
|
38 |
+
extrinsic = seq.islower()
|
39 |
+
if not (intrinsic or extrinsic):
|
40 |
+
raise ValueError("seq must either be fully uppercase (for extrinsic "
|
41 |
+
"rotations), or fully lowercase, for intrinsic "
|
42 |
+
"rotations).")
|
43 |
+
|
44 |
+
i, j, k = seq.lower()
|
45 |
+
if (i == j) or (j == k):
|
46 |
+
raise ValueError("Consecutive axes must be different")
|
47 |
+
|
48 |
+
bad = set(seq) - set('xyzXYZ')
|
49 |
+
if bad:
|
50 |
+
raise ValueError("Expected axes from `seq` to be from "
|
51 |
+
"['x', 'y', 'z'] or ['X', 'Y', 'Z'], "
|
52 |
+
"got {}".format(''.join(bad)))
|
53 |
+
|
54 |
+
return extrinsic
|
55 |
+
|
56 |
+
|
57 |
+
class Quaternion(Expr):
|
58 |
+
"""Provides basic quaternion operations.
|
59 |
+
Quaternion objects can be instantiated as Quaternion(a, b, c, d)
|
60 |
+
as in (a + b*i + c*j + d*k).
|
61 |
+
|
62 |
+
Parameters
|
63 |
+
==========
|
64 |
+
|
65 |
+
norm : None or number
|
66 |
+
Pre-defined quaternion norm. If a value is given, Quaternion.norm
|
67 |
+
returns this pre-defined value instead of calculating the norm
|
68 |
+
|
69 |
+
Examples
|
70 |
+
========
|
71 |
+
|
72 |
+
>>> from sympy import Quaternion
|
73 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
74 |
+
>>> q
|
75 |
+
1 + 2*i + 3*j + 4*k
|
76 |
+
|
77 |
+
Quaternions over complex fields can be defined as :
|
78 |
+
|
79 |
+
>>> from sympy import Quaternion
|
80 |
+
>>> from sympy import symbols, I
|
81 |
+
>>> x = symbols('x')
|
82 |
+
>>> q1 = Quaternion(x, x**3, x, x**2, real_field = False)
|
83 |
+
>>> q2 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
84 |
+
>>> q1
|
85 |
+
x + x**3*i + x*j + x**2*k
|
86 |
+
>>> q2
|
87 |
+
(3 + 4*I) + (2 + 5*I)*i + 0*j + (7 + 8*I)*k
|
88 |
+
|
89 |
+
Defining symbolic unit quaternions:
|
90 |
+
>>> from sympy import Quaternion
|
91 |
+
>>> from sympy.abc import w, x, y, z
|
92 |
+
>>> q = Quaternion(w, x, y, z, norm=1)
|
93 |
+
>>> q
|
94 |
+
w + x*i + y*j + z*k
|
95 |
+
>>> q.norm()
|
96 |
+
1
|
97 |
+
|
98 |
+
References
|
99 |
+
==========
|
100 |
+
|
101 |
+
.. [1] https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/
|
102 |
+
.. [2] https://en.wikipedia.org/wiki/Quaternion
|
103 |
+
|
104 |
+
"""
|
105 |
+
_op_priority = 11.0
|
106 |
+
|
107 |
+
is_commutative = False
|
108 |
+
|
109 |
+
def __new__(cls, a=0, b=0, c=0, d=0, real_field=True, norm=None):
|
110 |
+
a, b, c, d = map(sympify, (a, b, c, d))
|
111 |
+
|
112 |
+
if any(i.is_commutative is False for i in [a, b, c, d]):
|
113 |
+
raise ValueError("arguments have to be commutative")
|
114 |
+
else:
|
115 |
+
obj = Expr.__new__(cls, a, b, c, d)
|
116 |
+
obj._a = a
|
117 |
+
obj._b = b
|
118 |
+
obj._c = c
|
119 |
+
obj._d = d
|
120 |
+
obj._real_field = real_field
|
121 |
+
obj.set_norm(norm)
|
122 |
+
return obj
|
123 |
+
|
124 |
+
def set_norm(self, norm):
|
125 |
+
"""Sets norm of an already instantiated quaternion.
|
126 |
+
|
127 |
+
Parameters
|
128 |
+
==========
|
129 |
+
|
130 |
+
norm : None or number
|
131 |
+
Pre-defined quaternion norm. If a value is given, Quaternion.norm
|
132 |
+
returns this pre-defined value instead of calculating the norm
|
133 |
+
|
134 |
+
Examples
|
135 |
+
========
|
136 |
+
|
137 |
+
>>> from sympy import Quaternion
|
138 |
+
>>> from sympy.abc import a, b, c, d
|
139 |
+
>>> q = Quaternion(a, b, c, d)
|
140 |
+
>>> q.norm()
|
141 |
+
sqrt(a**2 + b**2 + c**2 + d**2)
|
142 |
+
|
143 |
+
Setting the norm:
|
144 |
+
|
145 |
+
>>> q.set_norm(1)
|
146 |
+
>>> q.norm()
|
147 |
+
1
|
148 |
+
|
149 |
+
Removing set norm:
|
150 |
+
|
151 |
+
>>> q.set_norm(None)
|
152 |
+
>>> q.norm()
|
153 |
+
sqrt(a**2 + b**2 + c**2 + d**2)
|
154 |
+
|
155 |
+
"""
|
156 |
+
norm = sympify(norm)
|
157 |
+
_check_norm(self.args, norm)
|
158 |
+
self._norm = norm
|
159 |
+
|
160 |
+
@property
|
161 |
+
def a(self):
|
162 |
+
return self._a
|
163 |
+
|
164 |
+
@property
|
165 |
+
def b(self):
|
166 |
+
return self._b
|
167 |
+
|
168 |
+
@property
|
169 |
+
def c(self):
|
170 |
+
return self._c
|
171 |
+
|
172 |
+
@property
|
173 |
+
def d(self):
|
174 |
+
return self._d
|
175 |
+
|
176 |
+
@property
|
177 |
+
def real_field(self):
|
178 |
+
return self._real_field
|
179 |
+
|
180 |
+
@property
|
181 |
+
def product_matrix_left(self):
|
182 |
+
r"""Returns 4 x 4 Matrix equivalent to a Hamilton product from the
|
183 |
+
left. This can be useful when treating quaternion elements as column
|
184 |
+
vectors. Given a quaternion $q = a + bi + cj + dk$ where a, b, c and d
|
185 |
+
are real numbers, the product matrix from the left is:
|
186 |
+
|
187 |
+
.. math::
|
188 |
+
|
189 |
+
M = \begin{bmatrix} a &-b &-c &-d \\
|
190 |
+
b & a &-d & c \\
|
191 |
+
c & d & a &-b \\
|
192 |
+
d &-c & b & a \end{bmatrix}
|
193 |
+
|
194 |
+
Examples
|
195 |
+
========
|
196 |
+
|
197 |
+
>>> from sympy import Quaternion
|
198 |
+
>>> from sympy.abc import a, b, c, d
|
199 |
+
>>> q1 = Quaternion(1, 0, 0, 1)
|
200 |
+
>>> q2 = Quaternion(a, b, c, d)
|
201 |
+
>>> q1.product_matrix_left
|
202 |
+
Matrix([
|
203 |
+
[1, 0, 0, -1],
|
204 |
+
[0, 1, -1, 0],
|
205 |
+
[0, 1, 1, 0],
|
206 |
+
[1, 0, 0, 1]])
|
207 |
+
|
208 |
+
>>> q1.product_matrix_left * q2.to_Matrix()
|
209 |
+
Matrix([
|
210 |
+
[a - d],
|
211 |
+
[b - c],
|
212 |
+
[b + c],
|
213 |
+
[a + d]])
|
214 |
+
|
215 |
+
This is equivalent to:
|
216 |
+
|
217 |
+
>>> (q1 * q2).to_Matrix()
|
218 |
+
Matrix([
|
219 |
+
[a - d],
|
220 |
+
[b - c],
|
221 |
+
[b + c],
|
222 |
+
[a + d]])
|
223 |
+
"""
|
224 |
+
return Matrix([
|
225 |
+
[self.a, -self.b, -self.c, -self.d],
|
226 |
+
[self.b, self.a, -self.d, self.c],
|
227 |
+
[self.c, self.d, self.a, -self.b],
|
228 |
+
[self.d, -self.c, self.b, self.a]])
|
229 |
+
|
230 |
+
@property
|
231 |
+
def product_matrix_right(self):
|
232 |
+
r"""Returns 4 x 4 Matrix equivalent to a Hamilton product from the
|
233 |
+
right. This can be useful when treating quaternion elements as column
|
234 |
+
vectors. Given a quaternion $q = a + bi + cj + dk$ where a, b, c and d
|
235 |
+
are real numbers, the product matrix from the left is:
|
236 |
+
|
237 |
+
.. math::
|
238 |
+
|
239 |
+
M = \begin{bmatrix} a &-b &-c &-d \\
|
240 |
+
b & a & d &-c \\
|
241 |
+
c &-d & a & b \\
|
242 |
+
d & c &-b & a \end{bmatrix}
|
243 |
+
|
244 |
+
|
245 |
+
Examples
|
246 |
+
========
|
247 |
+
|
248 |
+
>>> from sympy import Quaternion
|
249 |
+
>>> from sympy.abc import a, b, c, d
|
250 |
+
>>> q1 = Quaternion(a, b, c, d)
|
251 |
+
>>> q2 = Quaternion(1, 0, 0, 1)
|
252 |
+
>>> q2.product_matrix_right
|
253 |
+
Matrix([
|
254 |
+
[1, 0, 0, -1],
|
255 |
+
[0, 1, 1, 0],
|
256 |
+
[0, -1, 1, 0],
|
257 |
+
[1, 0, 0, 1]])
|
258 |
+
|
259 |
+
Note the switched arguments: the matrix represents the quaternion on
|
260 |
+
the right, but is still considered as a matrix multiplication from the
|
261 |
+
left.
|
262 |
+
|
263 |
+
>>> q2.product_matrix_right * q1.to_Matrix()
|
264 |
+
Matrix([
|
265 |
+
[ a - d],
|
266 |
+
[ b + c],
|
267 |
+
[-b + c],
|
268 |
+
[ a + d]])
|
269 |
+
|
270 |
+
This is equivalent to:
|
271 |
+
|
272 |
+
>>> (q1 * q2).to_Matrix()
|
273 |
+
Matrix([
|
274 |
+
[ a - d],
|
275 |
+
[ b + c],
|
276 |
+
[-b + c],
|
277 |
+
[ a + d]])
|
278 |
+
"""
|
279 |
+
return Matrix([
|
280 |
+
[self.a, -self.b, -self.c, -self.d],
|
281 |
+
[self.b, self.a, self.d, -self.c],
|
282 |
+
[self.c, -self.d, self.a, self.b],
|
283 |
+
[self.d, self.c, -self.b, self.a]])
|
284 |
+
|
285 |
+
def to_Matrix(self, vector_only=False):
|
286 |
+
"""Returns elements of quaternion as a column vector.
|
287 |
+
By default, a Matrix of length 4 is returned, with the real part as the
|
288 |
+
first element.
|
289 |
+
If vector_only is True, returns only imaginary part as a Matrix of
|
290 |
+
length 3.
|
291 |
+
|
292 |
+
Parameters
|
293 |
+
==========
|
294 |
+
|
295 |
+
vector_only : bool
|
296 |
+
If True, only imaginary part is returned.
|
297 |
+
Default value: False
|
298 |
+
|
299 |
+
Returns
|
300 |
+
=======
|
301 |
+
|
302 |
+
Matrix
|
303 |
+
A column vector constructed by the elements of the quaternion.
|
304 |
+
|
305 |
+
Examples
|
306 |
+
========
|
307 |
+
|
308 |
+
>>> from sympy import Quaternion
|
309 |
+
>>> from sympy.abc import a, b, c, d
|
310 |
+
>>> q = Quaternion(a, b, c, d)
|
311 |
+
>>> q
|
312 |
+
a + b*i + c*j + d*k
|
313 |
+
|
314 |
+
>>> q.to_Matrix()
|
315 |
+
Matrix([
|
316 |
+
[a],
|
317 |
+
[b],
|
318 |
+
[c],
|
319 |
+
[d]])
|
320 |
+
|
321 |
+
|
322 |
+
>>> q.to_Matrix(vector_only=True)
|
323 |
+
Matrix([
|
324 |
+
[b],
|
325 |
+
[c],
|
326 |
+
[d]])
|
327 |
+
|
328 |
+
"""
|
329 |
+
if vector_only:
|
330 |
+
return Matrix(self.args[1:])
|
331 |
+
else:
|
332 |
+
return Matrix(self.args)
|
333 |
+
|
334 |
+
@classmethod
|
335 |
+
def from_Matrix(cls, elements):
|
336 |
+
"""Returns quaternion from elements of a column vector`.
|
337 |
+
If vector_only is True, returns only imaginary part as a Matrix of
|
338 |
+
length 3.
|
339 |
+
|
340 |
+
Parameters
|
341 |
+
==========
|
342 |
+
|
343 |
+
elements : Matrix, list or tuple of length 3 or 4. If length is 3,
|
344 |
+
assume real part is zero.
|
345 |
+
Default value: False
|
346 |
+
|
347 |
+
Returns
|
348 |
+
=======
|
349 |
+
|
350 |
+
Quaternion
|
351 |
+
A quaternion created from the input elements.
|
352 |
+
|
353 |
+
Examples
|
354 |
+
========
|
355 |
+
|
356 |
+
>>> from sympy import Quaternion
|
357 |
+
>>> from sympy.abc import a, b, c, d
|
358 |
+
>>> q = Quaternion.from_Matrix([a, b, c, d])
|
359 |
+
>>> q
|
360 |
+
a + b*i + c*j + d*k
|
361 |
+
|
362 |
+
>>> q = Quaternion.from_Matrix([b, c, d])
|
363 |
+
>>> q
|
364 |
+
0 + b*i + c*j + d*k
|
365 |
+
|
366 |
+
"""
|
367 |
+
length = len(elements)
|
368 |
+
if length != 3 and length != 4:
|
369 |
+
raise ValueError("Input elements must have length 3 or 4, got {} "
|
370 |
+
"elements".format(length))
|
371 |
+
|
372 |
+
if length == 3:
|
373 |
+
return Quaternion(0, *elements)
|
374 |
+
else:
|
375 |
+
return Quaternion(*elements)
|
376 |
+
|
377 |
+
@classmethod
|
378 |
+
def from_euler(cls, angles, seq):
|
379 |
+
"""Returns quaternion equivalent to rotation represented by the Euler
|
380 |
+
angles, in the sequence defined by ``seq``.
|
381 |
+
|
382 |
+
Parameters
|
383 |
+
==========
|
384 |
+
|
385 |
+
angles : list, tuple or Matrix of 3 numbers
|
386 |
+
The Euler angles (in radians).
|
387 |
+
seq : string of length 3
|
388 |
+
Represents the sequence of rotations.
|
389 |
+
For intrinsic rotations, seq must be all lowercase and its elements
|
390 |
+
must be from the set ``{'x', 'y', 'z'}``
|
391 |
+
For extrinsic rotations, seq must be all uppercase and its elements
|
392 |
+
must be from the set ``{'X', 'Y', 'Z'}``
|
393 |
+
|
394 |
+
Returns
|
395 |
+
=======
|
396 |
+
|
397 |
+
Quaternion
|
398 |
+
The normalized rotation quaternion calculated from the Euler angles
|
399 |
+
in the given sequence.
|
400 |
+
|
401 |
+
Examples
|
402 |
+
========
|
403 |
+
|
404 |
+
>>> from sympy import Quaternion
|
405 |
+
>>> from sympy import pi
|
406 |
+
>>> q = Quaternion.from_euler([pi/2, 0, 0], 'xyz')
|
407 |
+
>>> q
|
408 |
+
sqrt(2)/2 + sqrt(2)/2*i + 0*j + 0*k
|
409 |
+
|
410 |
+
>>> q = Quaternion.from_euler([0, pi/2, pi] , 'zyz')
|
411 |
+
>>> q
|
412 |
+
0 + (-sqrt(2)/2)*i + 0*j + sqrt(2)/2*k
|
413 |
+
|
414 |
+
>>> q = Quaternion.from_euler([0, pi/2, pi] , 'ZYZ')
|
415 |
+
>>> q
|
416 |
+
0 + sqrt(2)/2*i + 0*j + sqrt(2)/2*k
|
417 |
+
|
418 |
+
"""
|
419 |
+
|
420 |
+
if len(angles) != 3:
|
421 |
+
raise ValueError("3 angles must be given.")
|
422 |
+
|
423 |
+
extrinsic = _is_extrinsic(seq)
|
424 |
+
i, j, k = seq.lower()
|
425 |
+
|
426 |
+
# get elementary basis vectors
|
427 |
+
ei = [1 if n == i else 0 for n in 'xyz']
|
428 |
+
ej = [1 if n == j else 0 for n in 'xyz']
|
429 |
+
ek = [1 if n == k else 0 for n in 'xyz']
|
430 |
+
|
431 |
+
# calculate distinct quaternions
|
432 |
+
qi = cls.from_axis_angle(ei, angles[0])
|
433 |
+
qj = cls.from_axis_angle(ej, angles[1])
|
434 |
+
qk = cls.from_axis_angle(ek, angles[2])
|
435 |
+
|
436 |
+
if extrinsic:
|
437 |
+
return trigsimp(qk * qj * qi)
|
438 |
+
else:
|
439 |
+
return trigsimp(qi * qj * qk)
|
440 |
+
|
441 |
+
def to_euler(self, seq, angle_addition=True, avoid_square_root=False):
|
442 |
+
r"""Returns Euler angles representing same rotation as the quaternion,
|
443 |
+
in the sequence given by ``seq``. This implements the method described
|
444 |
+
in [1]_.
|
445 |
+
|
446 |
+
For degenerate cases (gymbal lock cases), the third angle is
|
447 |
+
set to zero.
|
448 |
+
|
449 |
+
Parameters
|
450 |
+
==========
|
451 |
+
|
452 |
+
seq : string of length 3
|
453 |
+
Represents the sequence of rotations.
|
454 |
+
For intrinsic rotations, seq must be all lowercase and its elements
|
455 |
+
must be from the set ``{'x', 'y', 'z'}``
|
456 |
+
For extrinsic rotations, seq must be all uppercase and its elements
|
457 |
+
must be from the set ``{'X', 'Y', 'Z'}``
|
458 |
+
|
459 |
+
angle_addition : bool
|
460 |
+
When True, first and third angles are given as an addition and
|
461 |
+
subtraction of two simpler ``atan2`` expressions. When False, the
|
462 |
+
first and third angles are each given by a single more complicated
|
463 |
+
``atan2`` expression. This equivalent expression is given by:
|
464 |
+
|
465 |
+
.. math::
|
466 |
+
|
467 |
+
\operatorname{atan_2} (b,a) \pm \operatorname{atan_2} (d,c) =
|
468 |
+
\operatorname{atan_2} (bc\pm ad, ac\mp bd)
|
469 |
+
|
470 |
+
Default value: True
|
471 |
+
|
472 |
+
avoid_square_root : bool
|
473 |
+
When True, the second angle is calculated with an expression based
|
474 |
+
on ``acos``, which is slightly more complicated but avoids a square
|
475 |
+
root. When False, second angle is calculated with ``atan2``, which
|
476 |
+
is simpler and can be better for numerical reasons (some
|
477 |
+
numerical implementations of ``acos`` have problems near zero).
|
478 |
+
Default value: False
|
479 |
+
|
480 |
+
|
481 |
+
Returns
|
482 |
+
=======
|
483 |
+
|
484 |
+
Tuple
|
485 |
+
The Euler angles calculated from the quaternion
|
486 |
+
|
487 |
+
Examples
|
488 |
+
========
|
489 |
+
|
490 |
+
>>> from sympy import Quaternion
|
491 |
+
>>> from sympy.abc import a, b, c, d
|
492 |
+
>>> euler = Quaternion(a, b, c, d).to_euler('zyz')
|
493 |
+
>>> euler
|
494 |
+
(-atan2(-b, c) + atan2(d, a),
|
495 |
+
2*atan2(sqrt(b**2 + c**2), sqrt(a**2 + d**2)),
|
496 |
+
atan2(-b, c) + atan2(d, a))
|
497 |
+
|
498 |
+
|
499 |
+
References
|
500 |
+
==========
|
501 |
+
|
502 |
+
.. [1] https://doi.org/10.1371/journal.pone.0276302
|
503 |
+
|
504 |
+
"""
|
505 |
+
if self.is_zero_quaternion():
|
506 |
+
raise ValueError('Cannot convert a quaternion with norm 0.')
|
507 |
+
|
508 |
+
angles = [0, 0, 0]
|
509 |
+
|
510 |
+
extrinsic = _is_extrinsic(seq)
|
511 |
+
i, j, k = seq.lower()
|
512 |
+
|
513 |
+
# get index corresponding to elementary basis vectors
|
514 |
+
i = 'xyz'.index(i) + 1
|
515 |
+
j = 'xyz'.index(j) + 1
|
516 |
+
k = 'xyz'.index(k) + 1
|
517 |
+
|
518 |
+
if not extrinsic:
|
519 |
+
i, k = k, i
|
520 |
+
|
521 |
+
# check if sequence is symmetric
|
522 |
+
symmetric = i == k
|
523 |
+
if symmetric:
|
524 |
+
k = 6 - i - j
|
525 |
+
|
526 |
+
# parity of the permutation
|
527 |
+
sign = (i - j) * (j - k) * (k - i) // 2
|
528 |
+
|
529 |
+
# permutate elements
|
530 |
+
elements = [self.a, self.b, self.c, self.d]
|
531 |
+
a = elements[0]
|
532 |
+
b = elements[i]
|
533 |
+
c = elements[j]
|
534 |
+
d = elements[k] * sign
|
535 |
+
|
536 |
+
if not symmetric:
|
537 |
+
a, b, c, d = a - c, b + d, c + a, d - b
|
538 |
+
|
539 |
+
if avoid_square_root:
|
540 |
+
if symmetric:
|
541 |
+
n2 = self.norm()**2
|
542 |
+
angles[1] = acos((a * a + b * b - c * c - d * d) / n2)
|
543 |
+
else:
|
544 |
+
n2 = 2 * self.norm()**2
|
545 |
+
angles[1] = asin((c * c + d * d - a * a - b * b) / n2)
|
546 |
+
else:
|
547 |
+
angles[1] = 2 * atan2(sqrt(c * c + d * d), sqrt(a * a + b * b))
|
548 |
+
if not symmetric:
|
549 |
+
angles[1] -= S.Pi / 2
|
550 |
+
|
551 |
+
# Check for singularities in numerical cases
|
552 |
+
case = 0
|
553 |
+
if is_eq(c, S.Zero) and is_eq(d, S.Zero):
|
554 |
+
case = 1
|
555 |
+
if is_eq(a, S.Zero) and is_eq(b, S.Zero):
|
556 |
+
case = 2
|
557 |
+
|
558 |
+
if case == 0:
|
559 |
+
if angle_addition:
|
560 |
+
angles[0] = atan2(b, a) + atan2(d, c)
|
561 |
+
angles[2] = atan2(b, a) - atan2(d, c)
|
562 |
+
else:
|
563 |
+
angles[0] = atan2(b*c + a*d, a*c - b*d)
|
564 |
+
angles[2] = atan2(b*c - a*d, a*c + b*d)
|
565 |
+
|
566 |
+
else: # any degenerate case
|
567 |
+
angles[2 * (not extrinsic)] = S.Zero
|
568 |
+
if case == 1:
|
569 |
+
angles[2 * extrinsic] = 2 * atan2(b, a)
|
570 |
+
else:
|
571 |
+
angles[2 * extrinsic] = 2 * atan2(d, c)
|
572 |
+
angles[2 * extrinsic] *= (-1 if extrinsic else 1)
|
573 |
+
|
574 |
+
# for Tait-Bryan angles
|
575 |
+
if not symmetric:
|
576 |
+
angles[0] *= sign
|
577 |
+
|
578 |
+
if extrinsic:
|
579 |
+
return tuple(angles[::-1])
|
580 |
+
else:
|
581 |
+
return tuple(angles)
|
582 |
+
|
583 |
+
@classmethod
|
584 |
+
def from_axis_angle(cls, vector, angle):
|
585 |
+
"""Returns a rotation quaternion given the axis and the angle of rotation.
|
586 |
+
|
587 |
+
Parameters
|
588 |
+
==========
|
589 |
+
|
590 |
+
vector : tuple of three numbers
|
591 |
+
The vector representation of the given axis.
|
592 |
+
angle : number
|
593 |
+
The angle by which axis is rotated (in radians).
|
594 |
+
|
595 |
+
Returns
|
596 |
+
=======
|
597 |
+
|
598 |
+
Quaternion
|
599 |
+
The normalized rotation quaternion calculated from the given axis and the angle of rotation.
|
600 |
+
|
601 |
+
Examples
|
602 |
+
========
|
603 |
+
|
604 |
+
>>> from sympy import Quaternion
|
605 |
+
>>> from sympy import pi, sqrt
|
606 |
+
>>> q = Quaternion.from_axis_angle((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3), 2*pi/3)
|
607 |
+
>>> q
|
608 |
+
1/2 + 1/2*i + 1/2*j + 1/2*k
|
609 |
+
|
610 |
+
"""
|
611 |
+
(x, y, z) = vector
|
612 |
+
norm = sqrt(x**2 + y**2 + z**2)
|
613 |
+
(x, y, z) = (x / norm, y / norm, z / norm)
|
614 |
+
s = sin(angle * S.Half)
|
615 |
+
a = cos(angle * S.Half)
|
616 |
+
b = x * s
|
617 |
+
c = y * s
|
618 |
+
d = z * s
|
619 |
+
|
620 |
+
# note that this quaternion is already normalized by construction:
|
621 |
+
# c^2 + (s*x)^2 + (s*y)^2 + (s*z)^2 = c^2 + s^2*(x^2 + y^2 + z^2) = c^2 + s^2 * 1 = c^2 + s^2 = 1
|
622 |
+
# so, what we return is a normalized quaternion
|
623 |
+
|
624 |
+
return cls(a, b, c, d)
|
625 |
+
|
626 |
+
@classmethod
|
627 |
+
def from_rotation_matrix(cls, M):
|
628 |
+
"""Returns the equivalent quaternion of a matrix. The quaternion will be normalized
|
629 |
+
only if the matrix is special orthogonal (orthogonal and det(M) = 1).
|
630 |
+
|
631 |
+
Parameters
|
632 |
+
==========
|
633 |
+
|
634 |
+
M : Matrix
|
635 |
+
Input matrix to be converted to equivalent quaternion. M must be special
|
636 |
+
orthogonal (orthogonal and det(M) = 1) for the quaternion to be normalized.
|
637 |
+
|
638 |
+
Returns
|
639 |
+
=======
|
640 |
+
|
641 |
+
Quaternion
|
642 |
+
The quaternion equivalent to given matrix.
|
643 |
+
|
644 |
+
Examples
|
645 |
+
========
|
646 |
+
|
647 |
+
>>> from sympy import Quaternion
|
648 |
+
>>> from sympy import Matrix, symbols, cos, sin, trigsimp
|
649 |
+
>>> x = symbols('x')
|
650 |
+
>>> M = Matrix([[cos(x), -sin(x), 0], [sin(x), cos(x), 0], [0, 0, 1]])
|
651 |
+
>>> q = trigsimp(Quaternion.from_rotation_matrix(M))
|
652 |
+
>>> q
|
653 |
+
sqrt(2)*sqrt(cos(x) + 1)/2 + 0*i + 0*j + sqrt(2 - 2*cos(x))*sign(sin(x))/2*k
|
654 |
+
|
655 |
+
"""
|
656 |
+
|
657 |
+
absQ = M.det()**Rational(1, 3)
|
658 |
+
|
659 |
+
a = sqrt(absQ + M[0, 0] + M[1, 1] + M[2, 2]) / 2
|
660 |
+
b = sqrt(absQ + M[0, 0] - M[1, 1] - M[2, 2]) / 2
|
661 |
+
c = sqrt(absQ - M[0, 0] + M[1, 1] - M[2, 2]) / 2
|
662 |
+
d = sqrt(absQ - M[0, 0] - M[1, 1] + M[2, 2]) / 2
|
663 |
+
|
664 |
+
b = b * sign(M[2, 1] - M[1, 2])
|
665 |
+
c = c * sign(M[0, 2] - M[2, 0])
|
666 |
+
d = d * sign(M[1, 0] - M[0, 1])
|
667 |
+
|
668 |
+
return Quaternion(a, b, c, d)
|
669 |
+
|
670 |
+
def __add__(self, other):
|
671 |
+
return self.add(other)
|
672 |
+
|
673 |
+
def __radd__(self, other):
|
674 |
+
return self.add(other)
|
675 |
+
|
676 |
+
def __sub__(self, other):
|
677 |
+
return self.add(other*-1)
|
678 |
+
|
679 |
+
def __mul__(self, other):
|
680 |
+
return self._generic_mul(self, _sympify(other))
|
681 |
+
|
682 |
+
def __rmul__(self, other):
|
683 |
+
return self._generic_mul(_sympify(other), self)
|
684 |
+
|
685 |
+
def __pow__(self, p):
|
686 |
+
return self.pow(p)
|
687 |
+
|
688 |
+
def __neg__(self):
|
689 |
+
return Quaternion(-self._a, -self._b, -self._c, -self.d)
|
690 |
+
|
691 |
+
def __truediv__(self, other):
|
692 |
+
return self * sympify(other)**-1
|
693 |
+
|
694 |
+
def __rtruediv__(self, other):
|
695 |
+
return sympify(other) * self**-1
|
696 |
+
|
697 |
+
def _eval_Integral(self, *args):
|
698 |
+
return self.integrate(*args)
|
699 |
+
|
700 |
+
def diff(self, *symbols, **kwargs):
|
701 |
+
kwargs.setdefault('evaluate', True)
|
702 |
+
return self.func(*[a.diff(*symbols, **kwargs) for a in self.args])
|
703 |
+
|
704 |
+
def add(self, other):
|
705 |
+
"""Adds quaternions.
|
706 |
+
|
707 |
+
Parameters
|
708 |
+
==========
|
709 |
+
|
710 |
+
other : Quaternion
|
711 |
+
The quaternion to add to current (self) quaternion.
|
712 |
+
|
713 |
+
Returns
|
714 |
+
=======
|
715 |
+
|
716 |
+
Quaternion
|
717 |
+
The resultant quaternion after adding self to other
|
718 |
+
|
719 |
+
Examples
|
720 |
+
========
|
721 |
+
|
722 |
+
>>> from sympy import Quaternion
|
723 |
+
>>> from sympy import symbols
|
724 |
+
>>> q1 = Quaternion(1, 2, 3, 4)
|
725 |
+
>>> q2 = Quaternion(5, 6, 7, 8)
|
726 |
+
>>> q1.add(q2)
|
727 |
+
6 + 8*i + 10*j + 12*k
|
728 |
+
>>> q1 + 5
|
729 |
+
6 + 2*i + 3*j + 4*k
|
730 |
+
>>> x = symbols('x', real = True)
|
731 |
+
>>> q1.add(x)
|
732 |
+
(x + 1) + 2*i + 3*j + 4*k
|
733 |
+
|
734 |
+
Quaternions over complex fields :
|
735 |
+
|
736 |
+
>>> from sympy import Quaternion
|
737 |
+
>>> from sympy import I
|
738 |
+
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
739 |
+
>>> q3.add(2 + 3*I)
|
740 |
+
(5 + 7*I) + (2 + 5*I)*i + 0*j + (7 + 8*I)*k
|
741 |
+
|
742 |
+
"""
|
743 |
+
q1 = self
|
744 |
+
q2 = sympify(other)
|
745 |
+
|
746 |
+
# If q2 is a number or a SymPy expression instead of a quaternion
|
747 |
+
if not isinstance(q2, Quaternion):
|
748 |
+
if q1.real_field and q2.is_complex:
|
749 |
+
return Quaternion(re(q2) + q1.a, im(q2) + q1.b, q1.c, q1.d)
|
750 |
+
elif q2.is_commutative:
|
751 |
+
return Quaternion(q1.a + q2, q1.b, q1.c, q1.d)
|
752 |
+
else:
|
753 |
+
raise ValueError("Only commutative expressions can be added with a Quaternion.")
|
754 |
+
|
755 |
+
return Quaternion(q1.a + q2.a, q1.b + q2.b, q1.c + q2.c, q1.d
|
756 |
+
+ q2.d)
|
757 |
+
|
758 |
+
def mul(self, other):
|
759 |
+
"""Multiplies quaternions.
|
760 |
+
|
761 |
+
Parameters
|
762 |
+
==========
|
763 |
+
|
764 |
+
other : Quaternion or symbol
|
765 |
+
The quaternion to multiply to current (self) quaternion.
|
766 |
+
|
767 |
+
Returns
|
768 |
+
=======
|
769 |
+
|
770 |
+
Quaternion
|
771 |
+
The resultant quaternion after multiplying self with other
|
772 |
+
|
773 |
+
Examples
|
774 |
+
========
|
775 |
+
|
776 |
+
>>> from sympy import Quaternion
|
777 |
+
>>> from sympy import symbols
|
778 |
+
>>> q1 = Quaternion(1, 2, 3, 4)
|
779 |
+
>>> q2 = Quaternion(5, 6, 7, 8)
|
780 |
+
>>> q1.mul(q2)
|
781 |
+
(-60) + 12*i + 30*j + 24*k
|
782 |
+
>>> q1.mul(2)
|
783 |
+
2 + 4*i + 6*j + 8*k
|
784 |
+
>>> x = symbols('x', real = True)
|
785 |
+
>>> q1.mul(x)
|
786 |
+
x + 2*x*i + 3*x*j + 4*x*k
|
787 |
+
|
788 |
+
Quaternions over complex fields :
|
789 |
+
|
790 |
+
>>> from sympy import Quaternion
|
791 |
+
>>> from sympy import I
|
792 |
+
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
793 |
+
>>> q3.mul(2 + 3*I)
|
794 |
+
(2 + 3*I)*(3 + 4*I) + (2 + 3*I)*(2 + 5*I)*i + 0*j + (2 + 3*I)*(7 + 8*I)*k
|
795 |
+
|
796 |
+
"""
|
797 |
+
return self._generic_mul(self, _sympify(other))
|
798 |
+
|
799 |
+
@staticmethod
|
800 |
+
def _generic_mul(q1, q2):
|
801 |
+
"""Generic multiplication.
|
802 |
+
|
803 |
+
Parameters
|
804 |
+
==========
|
805 |
+
|
806 |
+
q1 : Quaternion or symbol
|
807 |
+
q2 : Quaternion or symbol
|
808 |
+
|
809 |
+
It is important to note that if neither q1 nor q2 is a Quaternion,
|
810 |
+
this function simply returns q1 * q2.
|
811 |
+
|
812 |
+
Returns
|
813 |
+
=======
|
814 |
+
|
815 |
+
Quaternion
|
816 |
+
The resultant quaternion after multiplying q1 and q2
|
817 |
+
|
818 |
+
Examples
|
819 |
+
========
|
820 |
+
|
821 |
+
>>> from sympy import Quaternion
|
822 |
+
>>> from sympy import Symbol, S
|
823 |
+
>>> q1 = Quaternion(1, 2, 3, 4)
|
824 |
+
>>> q2 = Quaternion(5, 6, 7, 8)
|
825 |
+
>>> Quaternion._generic_mul(q1, q2)
|
826 |
+
(-60) + 12*i + 30*j + 24*k
|
827 |
+
>>> Quaternion._generic_mul(q1, S(2))
|
828 |
+
2 + 4*i + 6*j + 8*k
|
829 |
+
>>> x = Symbol('x', real = True)
|
830 |
+
>>> Quaternion._generic_mul(q1, x)
|
831 |
+
x + 2*x*i + 3*x*j + 4*x*k
|
832 |
+
|
833 |
+
Quaternions over complex fields :
|
834 |
+
|
835 |
+
>>> from sympy import I
|
836 |
+
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
837 |
+
>>> Quaternion._generic_mul(q3, 2 + 3*I)
|
838 |
+
(2 + 3*I)*(3 + 4*I) + (2 + 3*I)*(2 + 5*I)*i + 0*j + (2 + 3*I)*(7 + 8*I)*k
|
839 |
+
|
840 |
+
"""
|
841 |
+
# None is a Quaternion:
|
842 |
+
if not isinstance(q1, Quaternion) and not isinstance(q2, Quaternion):
|
843 |
+
return q1 * q2
|
844 |
+
|
845 |
+
# If q1 is a number or a SymPy expression instead of a quaternion
|
846 |
+
if not isinstance(q1, Quaternion):
|
847 |
+
if q2.real_field and q1.is_complex:
|
848 |
+
return Quaternion(re(q1), im(q1), 0, 0) * q2
|
849 |
+
elif q1.is_commutative:
|
850 |
+
return Quaternion(q1 * q2.a, q1 * q2.b, q1 * q2.c, q1 * q2.d)
|
851 |
+
else:
|
852 |
+
raise ValueError("Only commutative expressions can be multiplied with a Quaternion.")
|
853 |
+
|
854 |
+
# If q2 is a number or a SymPy expression instead of a quaternion
|
855 |
+
if not isinstance(q2, Quaternion):
|
856 |
+
if q1.real_field and q2.is_complex:
|
857 |
+
return q1 * Quaternion(re(q2), im(q2), 0, 0)
|
858 |
+
elif q2.is_commutative:
|
859 |
+
return Quaternion(q2 * q1.a, q2 * q1.b, q2 * q1.c, q2 * q1.d)
|
860 |
+
else:
|
861 |
+
raise ValueError("Only commutative expressions can be multiplied with a Quaternion.")
|
862 |
+
|
863 |
+
# If any of the quaternions has a fixed norm, pre-compute norm
|
864 |
+
if q1._norm is None and q2._norm is None:
|
865 |
+
norm = None
|
866 |
+
else:
|
867 |
+
norm = q1.norm() * q2.norm()
|
868 |
+
|
869 |
+
return Quaternion(-q1.b*q2.b - q1.c*q2.c - q1.d*q2.d + q1.a*q2.a,
|
870 |
+
q1.b*q2.a + q1.c*q2.d - q1.d*q2.c + q1.a*q2.b,
|
871 |
+
-q1.b*q2.d + q1.c*q2.a + q1.d*q2.b + q1.a*q2.c,
|
872 |
+
q1.b*q2.c - q1.c*q2.b + q1.d*q2.a + q1.a * q2.d,
|
873 |
+
norm=norm)
|
874 |
+
|
875 |
+
def _eval_conjugate(self):
|
876 |
+
"""Returns the conjugate of the quaternion."""
|
877 |
+
q = self
|
878 |
+
return Quaternion(q.a, -q.b, -q.c, -q.d, norm=q._norm)
|
879 |
+
|
880 |
+
def norm(self):
|
881 |
+
"""Returns the norm of the quaternion."""
|
882 |
+
if self._norm is None: # check if norm is pre-defined
|
883 |
+
q = self
|
884 |
+
# trigsimp is used to simplify sin(x)^2 + cos(x)^2 (these terms
|
885 |
+
# arise when from_axis_angle is used).
|
886 |
+
self._norm = sqrt(trigsimp(q.a**2 + q.b**2 + q.c**2 + q.d**2))
|
887 |
+
|
888 |
+
return self._norm
|
889 |
+
|
890 |
+
def normalize(self):
|
891 |
+
"""Returns the normalized form of the quaternion."""
|
892 |
+
q = self
|
893 |
+
return q * (1/q.norm())
|
894 |
+
|
895 |
+
def inverse(self):
|
896 |
+
"""Returns the inverse of the quaternion."""
|
897 |
+
q = self
|
898 |
+
if not q.norm():
|
899 |
+
raise ValueError("Cannot compute inverse for a quaternion with zero norm")
|
900 |
+
return conjugate(q) * (1/q.norm()**2)
|
901 |
+
|
902 |
+
def pow(self, p):
|
903 |
+
"""Finds the pth power of the quaternion.
|
904 |
+
|
905 |
+
Parameters
|
906 |
+
==========
|
907 |
+
|
908 |
+
p : int
|
909 |
+
Power to be applied on quaternion.
|
910 |
+
|
911 |
+
Returns
|
912 |
+
=======
|
913 |
+
|
914 |
+
Quaternion
|
915 |
+
Returns the p-th power of the current quaternion.
|
916 |
+
Returns the inverse if p = -1.
|
917 |
+
|
918 |
+
Examples
|
919 |
+
========
|
920 |
+
|
921 |
+
>>> from sympy import Quaternion
|
922 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
923 |
+
>>> q.pow(4)
|
924 |
+
668 + (-224)*i + (-336)*j + (-448)*k
|
925 |
+
|
926 |
+
"""
|
927 |
+
p = sympify(p)
|
928 |
+
q = self
|
929 |
+
if p == -1:
|
930 |
+
return q.inverse()
|
931 |
+
res = 1
|
932 |
+
|
933 |
+
if not p.is_Integer:
|
934 |
+
return NotImplemented
|
935 |
+
|
936 |
+
if p < 0:
|
937 |
+
q, p = q.inverse(), -p
|
938 |
+
|
939 |
+
while p > 0:
|
940 |
+
if p % 2 == 1:
|
941 |
+
res = q * res
|
942 |
+
|
943 |
+
p = p//2
|
944 |
+
q = q * q
|
945 |
+
|
946 |
+
return res
|
947 |
+
|
948 |
+
def exp(self):
|
949 |
+
"""Returns the exponential of q (e^q).
|
950 |
+
|
951 |
+
Returns
|
952 |
+
=======
|
953 |
+
|
954 |
+
Quaternion
|
955 |
+
Exponential of q (e^q).
|
956 |
+
|
957 |
+
Examples
|
958 |
+
========
|
959 |
+
|
960 |
+
>>> from sympy import Quaternion
|
961 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
962 |
+
>>> q.exp()
|
963 |
+
E*cos(sqrt(29))
|
964 |
+
+ 2*sqrt(29)*E*sin(sqrt(29))/29*i
|
965 |
+
+ 3*sqrt(29)*E*sin(sqrt(29))/29*j
|
966 |
+
+ 4*sqrt(29)*E*sin(sqrt(29))/29*k
|
967 |
+
|
968 |
+
"""
|
969 |
+
# exp(q) = e^a(cos||v|| + v/||v||*sin||v||)
|
970 |
+
q = self
|
971 |
+
vector_norm = sqrt(q.b**2 + q.c**2 + q.d**2)
|
972 |
+
a = exp(q.a) * cos(vector_norm)
|
973 |
+
b = exp(q.a) * sin(vector_norm) * q.b / vector_norm
|
974 |
+
c = exp(q.a) * sin(vector_norm) * q.c / vector_norm
|
975 |
+
d = exp(q.a) * sin(vector_norm) * q.d / vector_norm
|
976 |
+
|
977 |
+
return Quaternion(a, b, c, d)
|
978 |
+
|
979 |
+
def _ln(self):
|
980 |
+
"""Returns the natural logarithm of the quaternion (_ln(q)).
|
981 |
+
|
982 |
+
Examples
|
983 |
+
========
|
984 |
+
|
985 |
+
>>> from sympy import Quaternion
|
986 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
987 |
+
>>> q._ln()
|
988 |
+
log(sqrt(30))
|
989 |
+
+ 2*sqrt(29)*acos(sqrt(30)/30)/29*i
|
990 |
+
+ 3*sqrt(29)*acos(sqrt(30)/30)/29*j
|
991 |
+
+ 4*sqrt(29)*acos(sqrt(30)/30)/29*k
|
992 |
+
|
993 |
+
"""
|
994 |
+
# _ln(q) = _ln||q|| + v/||v||*arccos(a/||q||)
|
995 |
+
q = self
|
996 |
+
vector_norm = sqrt(q.b**2 + q.c**2 + q.d**2)
|
997 |
+
q_norm = q.norm()
|
998 |
+
a = ln(q_norm)
|
999 |
+
b = q.b * acos(q.a / q_norm) / vector_norm
|
1000 |
+
c = q.c * acos(q.a / q_norm) / vector_norm
|
1001 |
+
d = q.d * acos(q.a / q_norm) / vector_norm
|
1002 |
+
|
1003 |
+
return Quaternion(a, b, c, d)
|
1004 |
+
|
1005 |
+
def _eval_subs(self, *args):
|
1006 |
+
elements = [i.subs(*args) for i in self.args]
|
1007 |
+
norm = self._norm
|
1008 |
+
try:
|
1009 |
+
norm = norm.subs(*args)
|
1010 |
+
except AttributeError:
|
1011 |
+
pass
|
1012 |
+
_check_norm(elements, norm)
|
1013 |
+
return Quaternion(*elements, norm=norm)
|
1014 |
+
|
1015 |
+
def _eval_evalf(self, prec):
|
1016 |
+
"""Returns the floating point approximations (decimal numbers) of the quaternion.
|
1017 |
+
|
1018 |
+
Returns
|
1019 |
+
=======
|
1020 |
+
|
1021 |
+
Quaternion
|
1022 |
+
Floating point approximations of quaternion(self)
|
1023 |
+
|
1024 |
+
Examples
|
1025 |
+
========
|
1026 |
+
|
1027 |
+
>>> from sympy import Quaternion
|
1028 |
+
>>> from sympy import sqrt
|
1029 |
+
>>> q = Quaternion(1/sqrt(1), 1/sqrt(2), 1/sqrt(3), 1/sqrt(4))
|
1030 |
+
>>> q.evalf()
|
1031 |
+
1.00000000000000
|
1032 |
+
+ 0.707106781186547*i
|
1033 |
+
+ 0.577350269189626*j
|
1034 |
+
+ 0.500000000000000*k
|
1035 |
+
|
1036 |
+
"""
|
1037 |
+
nprec = prec_to_dps(prec)
|
1038 |
+
return Quaternion(*[arg.evalf(n=nprec) for arg in self.args])
|
1039 |
+
|
1040 |
+
def pow_cos_sin(self, p):
|
1041 |
+
"""Computes the pth power in the cos-sin form.
|
1042 |
+
|
1043 |
+
Parameters
|
1044 |
+
==========
|
1045 |
+
|
1046 |
+
p : int
|
1047 |
+
Power to be applied on quaternion.
|
1048 |
+
|
1049 |
+
Returns
|
1050 |
+
=======
|
1051 |
+
|
1052 |
+
Quaternion
|
1053 |
+
The p-th power in the cos-sin form.
|
1054 |
+
|
1055 |
+
Examples
|
1056 |
+
========
|
1057 |
+
|
1058 |
+
>>> from sympy import Quaternion
|
1059 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
1060 |
+
>>> q.pow_cos_sin(4)
|
1061 |
+
900*cos(4*acos(sqrt(30)/30))
|
1062 |
+
+ 1800*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*i
|
1063 |
+
+ 2700*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*j
|
1064 |
+
+ 3600*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*k
|
1065 |
+
|
1066 |
+
"""
|
1067 |
+
# q = ||q||*(cos(a) + u*sin(a))
|
1068 |
+
# q^p = ||q||^p * (cos(p*a) + u*sin(p*a))
|
1069 |
+
|
1070 |
+
q = self
|
1071 |
+
(v, angle) = q.to_axis_angle()
|
1072 |
+
q2 = Quaternion.from_axis_angle(v, p * angle)
|
1073 |
+
return q2 * (q.norm()**p)
|
1074 |
+
|
1075 |
+
def integrate(self, *args):
|
1076 |
+
"""Computes integration of quaternion.
|
1077 |
+
|
1078 |
+
Returns
|
1079 |
+
=======
|
1080 |
+
|
1081 |
+
Quaternion
|
1082 |
+
Integration of the quaternion(self) with the given variable.
|
1083 |
+
|
1084 |
+
Examples
|
1085 |
+
========
|
1086 |
+
|
1087 |
+
Indefinite Integral of quaternion :
|
1088 |
+
|
1089 |
+
>>> from sympy import Quaternion
|
1090 |
+
>>> from sympy.abc import x
|
1091 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
1092 |
+
>>> q.integrate(x)
|
1093 |
+
x + 2*x*i + 3*x*j + 4*x*k
|
1094 |
+
|
1095 |
+
Definite integral of quaternion :
|
1096 |
+
|
1097 |
+
>>> from sympy import Quaternion
|
1098 |
+
>>> from sympy.abc import x
|
1099 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
1100 |
+
>>> q.integrate((x, 1, 5))
|
1101 |
+
4 + 8*i + 12*j + 16*k
|
1102 |
+
|
1103 |
+
"""
|
1104 |
+
# TODO: is this expression correct?
|
1105 |
+
return Quaternion(integrate(self.a, *args), integrate(self.b, *args),
|
1106 |
+
integrate(self.c, *args), integrate(self.d, *args))
|
1107 |
+
|
1108 |
+
@staticmethod
|
1109 |
+
def rotate_point(pin, r):
|
1110 |
+
"""Returns the coordinates of the point pin(a 3 tuple) after rotation.
|
1111 |
+
|
1112 |
+
Parameters
|
1113 |
+
==========
|
1114 |
+
|
1115 |
+
pin : tuple
|
1116 |
+
A 3-element tuple of coordinates of a point which needs to be
|
1117 |
+
rotated.
|
1118 |
+
r : Quaternion or tuple
|
1119 |
+
Axis and angle of rotation.
|
1120 |
+
|
1121 |
+
It's important to note that when r is a tuple, it must be of the form
|
1122 |
+
(axis, angle)
|
1123 |
+
|
1124 |
+
Returns
|
1125 |
+
=======
|
1126 |
+
|
1127 |
+
tuple
|
1128 |
+
The coordinates of the point after rotation.
|
1129 |
+
|
1130 |
+
Examples
|
1131 |
+
========
|
1132 |
+
|
1133 |
+
>>> from sympy import Quaternion
|
1134 |
+
>>> from sympy import symbols, trigsimp, cos, sin
|
1135 |
+
>>> x = symbols('x')
|
1136 |
+
>>> q = Quaternion(cos(x/2), 0, 0, sin(x/2))
|
1137 |
+
>>> trigsimp(Quaternion.rotate_point((1, 1, 1), q))
|
1138 |
+
(sqrt(2)*cos(x + pi/4), sqrt(2)*sin(x + pi/4), 1)
|
1139 |
+
>>> (axis, angle) = q.to_axis_angle()
|
1140 |
+
>>> trigsimp(Quaternion.rotate_point((1, 1, 1), (axis, angle)))
|
1141 |
+
(sqrt(2)*cos(x + pi/4), sqrt(2)*sin(x + pi/4), 1)
|
1142 |
+
|
1143 |
+
"""
|
1144 |
+
if isinstance(r, tuple):
|
1145 |
+
# if r is of the form (vector, angle)
|
1146 |
+
q = Quaternion.from_axis_angle(r[0], r[1])
|
1147 |
+
else:
|
1148 |
+
# if r is a quaternion
|
1149 |
+
q = r.normalize()
|
1150 |
+
pout = q * Quaternion(0, pin[0], pin[1], pin[2]) * conjugate(q)
|
1151 |
+
return (pout.b, pout.c, pout.d)
|
1152 |
+
|
1153 |
+
def to_axis_angle(self):
|
1154 |
+
"""Returns the axis and angle of rotation of a quaternion.
|
1155 |
+
|
1156 |
+
Returns
|
1157 |
+
=======
|
1158 |
+
|
1159 |
+
tuple
|
1160 |
+
Tuple of (axis, angle)
|
1161 |
+
|
1162 |
+
Examples
|
1163 |
+
========
|
1164 |
+
|
1165 |
+
>>> from sympy import Quaternion
|
1166 |
+
>>> q = Quaternion(1, 1, 1, 1)
|
1167 |
+
>>> (axis, angle) = q.to_axis_angle()
|
1168 |
+
>>> axis
|
1169 |
+
(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3)
|
1170 |
+
>>> angle
|
1171 |
+
2*pi/3
|
1172 |
+
|
1173 |
+
"""
|
1174 |
+
q = self
|
1175 |
+
if q.a.is_negative:
|
1176 |
+
q = q * -1
|
1177 |
+
|
1178 |
+
q = q.normalize()
|
1179 |
+
angle = trigsimp(2 * acos(q.a))
|
1180 |
+
|
1181 |
+
# Since quaternion is normalised, q.a is less than 1.
|
1182 |
+
s = sqrt(1 - q.a*q.a)
|
1183 |
+
|
1184 |
+
x = trigsimp(q.b / s)
|
1185 |
+
y = trigsimp(q.c / s)
|
1186 |
+
z = trigsimp(q.d / s)
|
1187 |
+
|
1188 |
+
v = (x, y, z)
|
1189 |
+
t = (v, angle)
|
1190 |
+
|
1191 |
+
return t
|
1192 |
+
|
1193 |
+
def to_rotation_matrix(self, v=None, homogeneous=True):
|
1194 |
+
"""Returns the equivalent rotation transformation matrix of the quaternion
|
1195 |
+
which represents rotation about the origin if v is not passed.
|
1196 |
+
|
1197 |
+
Parameters
|
1198 |
+
==========
|
1199 |
+
|
1200 |
+
v : tuple or None
|
1201 |
+
Default value: None
|
1202 |
+
homogeneous : bool
|
1203 |
+
When True, gives an expression that may be more efficient for
|
1204 |
+
symbolic calculations but less so for direct evaluation. Both
|
1205 |
+
formulas are mathematically equivalent.
|
1206 |
+
Default value: True
|
1207 |
+
|
1208 |
+
Returns
|
1209 |
+
=======
|
1210 |
+
|
1211 |
+
tuple
|
1212 |
+
Returns the equivalent rotation transformation matrix of the quaternion
|
1213 |
+
which represents rotation about the origin if v is not passed.
|
1214 |
+
|
1215 |
+
Examples
|
1216 |
+
========
|
1217 |
+
|
1218 |
+
>>> from sympy import Quaternion
|
1219 |
+
>>> from sympy import symbols, trigsimp, cos, sin
|
1220 |
+
>>> x = symbols('x')
|
1221 |
+
>>> q = Quaternion(cos(x/2), 0, 0, sin(x/2))
|
1222 |
+
>>> trigsimp(q.to_rotation_matrix())
|
1223 |
+
Matrix([
|
1224 |
+
[cos(x), -sin(x), 0],
|
1225 |
+
[sin(x), cos(x), 0],
|
1226 |
+
[ 0, 0, 1]])
|
1227 |
+
|
1228 |
+
Generates a 4x4 transformation matrix (used for rotation about a point
|
1229 |
+
other than the origin) if the point(v) is passed as an argument.
|
1230 |
+
"""
|
1231 |
+
|
1232 |
+
q = self
|
1233 |
+
s = q.norm()**-2
|
1234 |
+
|
1235 |
+
# diagonal elements are different according to parameter normal
|
1236 |
+
if homogeneous:
|
1237 |
+
m00 = s*(q.a**2 + q.b**2 - q.c**2 - q.d**2)
|
1238 |
+
m11 = s*(q.a**2 - q.b**2 + q.c**2 - q.d**2)
|
1239 |
+
m22 = s*(q.a**2 - q.b**2 - q.c**2 + q.d**2)
|
1240 |
+
else:
|
1241 |
+
m00 = 1 - 2*s*(q.c**2 + q.d**2)
|
1242 |
+
m11 = 1 - 2*s*(q.b**2 + q.d**2)
|
1243 |
+
m22 = 1 - 2*s*(q.b**2 + q.c**2)
|
1244 |
+
|
1245 |
+
m01 = 2*s*(q.b*q.c - q.d*q.a)
|
1246 |
+
m02 = 2*s*(q.b*q.d + q.c*q.a)
|
1247 |
+
|
1248 |
+
m10 = 2*s*(q.b*q.c + q.d*q.a)
|
1249 |
+
m12 = 2*s*(q.c*q.d - q.b*q.a)
|
1250 |
+
|
1251 |
+
m20 = 2*s*(q.b*q.d - q.c*q.a)
|
1252 |
+
m21 = 2*s*(q.c*q.d + q.b*q.a)
|
1253 |
+
|
1254 |
+
if not v:
|
1255 |
+
return Matrix([[m00, m01, m02], [m10, m11, m12], [m20, m21, m22]])
|
1256 |
+
|
1257 |
+
else:
|
1258 |
+
(x, y, z) = v
|
1259 |
+
|
1260 |
+
m03 = x - x*m00 - y*m01 - z*m02
|
1261 |
+
m13 = y - x*m10 - y*m11 - z*m12
|
1262 |
+
m23 = z - x*m20 - y*m21 - z*m22
|
1263 |
+
m30 = m31 = m32 = 0
|
1264 |
+
m33 = 1
|
1265 |
+
|
1266 |
+
return Matrix([[m00, m01, m02, m03], [m10, m11, m12, m13],
|
1267 |
+
[m20, m21, m22, m23], [m30, m31, m32, m33]])
|
1268 |
+
|
1269 |
+
def scalar_part(self):
|
1270 |
+
r"""Returns scalar part($\mathbf{S}(q)$) of the quaternion q.
|
1271 |
+
|
1272 |
+
Explanation
|
1273 |
+
===========
|
1274 |
+
|
1275 |
+
Given a quaternion $q = a + bi + cj + dk$, returns $\mathbf{S}(q) = a$.
|
1276 |
+
|
1277 |
+
Examples
|
1278 |
+
========
|
1279 |
+
|
1280 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1281 |
+
>>> q = Quaternion(4, 8, 13, 12)
|
1282 |
+
>>> q.scalar_part()
|
1283 |
+
4
|
1284 |
+
|
1285 |
+
"""
|
1286 |
+
|
1287 |
+
return self.a
|
1288 |
+
|
1289 |
+
def vector_part(self):
|
1290 |
+
r"""
|
1291 |
+
Returns vector part($\mathbf{V}(q)$) of the quaternion q.
|
1292 |
+
|
1293 |
+
Explanation
|
1294 |
+
===========
|
1295 |
+
|
1296 |
+
Given a quaternion $q = a + bi + cj + dk$, returns $\mathbf{V}(q) = bi + cj + dk$.
|
1297 |
+
|
1298 |
+
Examples
|
1299 |
+
========
|
1300 |
+
|
1301 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1302 |
+
>>> q = Quaternion(1, 1, 1, 1)
|
1303 |
+
>>> q.vector_part()
|
1304 |
+
0 + 1*i + 1*j + 1*k
|
1305 |
+
|
1306 |
+
>>> q = Quaternion(4, 8, 13, 12)
|
1307 |
+
>>> q.vector_part()
|
1308 |
+
0 + 8*i + 13*j + 12*k
|
1309 |
+
|
1310 |
+
"""
|
1311 |
+
|
1312 |
+
return Quaternion(0, self.b, self.c, self.d)
|
1313 |
+
|
1314 |
+
def axis(self):
|
1315 |
+
r"""
|
1316 |
+
Returns the axis($\mathbf{Ax}(q)$) of the quaternion.
|
1317 |
+
|
1318 |
+
Explanation
|
1319 |
+
===========
|
1320 |
+
|
1321 |
+
Given a quaternion $q = a + bi + cj + dk$, returns $\mathbf{Ax}(q)$ i.e., the versor of the vector part of that quaternion
|
1322 |
+
equal to $\mathbf{U}[\mathbf{V}(q)]$.
|
1323 |
+
The axis is always an imaginary unit with square equal to $-1 + 0i + 0j + 0k$.
|
1324 |
+
|
1325 |
+
Examples
|
1326 |
+
========
|
1327 |
+
|
1328 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1329 |
+
>>> q = Quaternion(1, 1, 1, 1)
|
1330 |
+
>>> q.axis()
|
1331 |
+
0 + sqrt(3)/3*i + sqrt(3)/3*j + sqrt(3)/3*k
|
1332 |
+
|
1333 |
+
See Also
|
1334 |
+
========
|
1335 |
+
|
1336 |
+
vector_part
|
1337 |
+
|
1338 |
+
"""
|
1339 |
+
axis = self.vector_part().normalize()
|
1340 |
+
|
1341 |
+
return Quaternion(0, axis.b, axis.c, axis.d)
|
1342 |
+
|
1343 |
+
def is_pure(self):
|
1344 |
+
"""
|
1345 |
+
Returns true if the quaternion is pure, false if the quaternion is not pure
|
1346 |
+
or returns none if it is unknown.
|
1347 |
+
|
1348 |
+
Explanation
|
1349 |
+
===========
|
1350 |
+
|
1351 |
+
A pure quaternion (also a vector quaternion) is a quaternion with scalar
|
1352 |
+
part equal to 0.
|
1353 |
+
|
1354 |
+
Examples
|
1355 |
+
========
|
1356 |
+
|
1357 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1358 |
+
>>> q = Quaternion(0, 8, 13, 12)
|
1359 |
+
>>> q.is_pure()
|
1360 |
+
True
|
1361 |
+
|
1362 |
+
See Also
|
1363 |
+
========
|
1364 |
+
scalar_part
|
1365 |
+
|
1366 |
+
"""
|
1367 |
+
|
1368 |
+
return self.a.is_zero
|
1369 |
+
|
1370 |
+
def is_zero_quaternion(self):
|
1371 |
+
"""
|
1372 |
+
Returns true if the quaternion is a zero quaternion or false if it is not a zero quaternion
|
1373 |
+
and None if the value is unknown.
|
1374 |
+
|
1375 |
+
Explanation
|
1376 |
+
===========
|
1377 |
+
|
1378 |
+
A zero quaternion is a quaternion with both scalar part and
|
1379 |
+
vector part equal to 0.
|
1380 |
+
|
1381 |
+
Examples
|
1382 |
+
========
|
1383 |
+
|
1384 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1385 |
+
>>> q = Quaternion(1, 0, 0, 0)
|
1386 |
+
>>> q.is_zero_quaternion()
|
1387 |
+
False
|
1388 |
+
|
1389 |
+
>>> q = Quaternion(0, 0, 0, 0)
|
1390 |
+
>>> q.is_zero_quaternion()
|
1391 |
+
True
|
1392 |
+
|
1393 |
+
See Also
|
1394 |
+
========
|
1395 |
+
scalar_part
|
1396 |
+
vector_part
|
1397 |
+
|
1398 |
+
"""
|
1399 |
+
|
1400 |
+
return self.norm().is_zero
|
1401 |
+
|
1402 |
+
def angle(self):
|
1403 |
+
r"""
|
1404 |
+
Returns the angle of the quaternion measured in the real-axis plane.
|
1405 |
+
|
1406 |
+
Explanation
|
1407 |
+
===========
|
1408 |
+
|
1409 |
+
Given a quaternion $q = a + bi + cj + dk$ where a, b, c and d
|
1410 |
+
are real numbers, returns the angle of the quaternion given by
|
1411 |
+
|
1412 |
+
.. math::
|
1413 |
+
angle := atan2(\sqrt{b^2 + c^2 + d^2}, {a})
|
1414 |
+
|
1415 |
+
Examples
|
1416 |
+
========
|
1417 |
+
|
1418 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1419 |
+
>>> q = Quaternion(1, 4, 4, 4)
|
1420 |
+
>>> q.angle()
|
1421 |
+
atan(4*sqrt(3))
|
1422 |
+
|
1423 |
+
"""
|
1424 |
+
|
1425 |
+
return atan2(self.vector_part().norm(), self.scalar_part())
|
1426 |
+
|
1427 |
+
|
1428 |
+
def arc_coplanar(self, other):
|
1429 |
+
"""
|
1430 |
+
Returns True if the transformation arcs represented by the input quaternions happen in the same plane.
|
1431 |
+
|
1432 |
+
Explanation
|
1433 |
+
===========
|
1434 |
+
|
1435 |
+
Two quaternions are said to be coplanar (in this arc sense) when their axes are parallel.
|
1436 |
+
The plane of a quaternion is the one normal to its axis.
|
1437 |
+
|
1438 |
+
Parameters
|
1439 |
+
==========
|
1440 |
+
|
1441 |
+
other : a Quaternion
|
1442 |
+
|
1443 |
+
Returns
|
1444 |
+
=======
|
1445 |
+
|
1446 |
+
True : if the planes of the two quaternions are the same, apart from its orientation/sign.
|
1447 |
+
False : if the planes of the two quaternions are not the same, apart from its orientation/sign.
|
1448 |
+
None : if plane of either of the quaternion is unknown.
|
1449 |
+
|
1450 |
+
Examples
|
1451 |
+
========
|
1452 |
+
|
1453 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1454 |
+
>>> q1 = Quaternion(1, 4, 4, 4)
|
1455 |
+
>>> q2 = Quaternion(3, 8, 8, 8)
|
1456 |
+
>>> Quaternion.arc_coplanar(q1, q2)
|
1457 |
+
True
|
1458 |
+
|
1459 |
+
>>> q1 = Quaternion(2, 8, 13, 12)
|
1460 |
+
>>> Quaternion.arc_coplanar(q1, q2)
|
1461 |
+
False
|
1462 |
+
|
1463 |
+
See Also
|
1464 |
+
========
|
1465 |
+
|
1466 |
+
vector_coplanar
|
1467 |
+
is_pure
|
1468 |
+
|
1469 |
+
"""
|
1470 |
+
if (self.is_zero_quaternion()) or (other.is_zero_quaternion()):
|
1471 |
+
raise ValueError('Neither of the given quaternions can be 0')
|
1472 |
+
|
1473 |
+
return fuzzy_or([(self.axis() - other.axis()).is_zero_quaternion(), (self.axis() + other.axis()).is_zero_quaternion()])
|
1474 |
+
|
1475 |
+
@classmethod
|
1476 |
+
def vector_coplanar(cls, q1, q2, q3):
|
1477 |
+
r"""
|
1478 |
+
Returns True if the axis of the pure quaternions seen as 3D vectors
|
1479 |
+
q1, q2, and q3 are coplanar.
|
1480 |
+
|
1481 |
+
Explanation
|
1482 |
+
===========
|
1483 |
+
|
1484 |
+
Three pure quaternions are vector coplanar if the quaternions seen as 3D vectors are coplanar.
|
1485 |
+
|
1486 |
+
Parameters
|
1487 |
+
==========
|
1488 |
+
|
1489 |
+
q1
|
1490 |
+
A pure Quaternion.
|
1491 |
+
q2
|
1492 |
+
A pure Quaternion.
|
1493 |
+
q3
|
1494 |
+
A pure Quaternion.
|
1495 |
+
|
1496 |
+
Returns
|
1497 |
+
=======
|
1498 |
+
|
1499 |
+
True : if the axis of the pure quaternions seen as 3D vectors
|
1500 |
+
q1, q2, and q3 are coplanar.
|
1501 |
+
False : if the axis of the pure quaternions seen as 3D vectors
|
1502 |
+
q1, q2, and q3 are not coplanar.
|
1503 |
+
None : if the axis of the pure quaternions seen as 3D vectors
|
1504 |
+
q1, q2, and q3 are coplanar is unknown.
|
1505 |
+
|
1506 |
+
Examples
|
1507 |
+
========
|
1508 |
+
|
1509 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1510 |
+
>>> q1 = Quaternion(0, 4, 4, 4)
|
1511 |
+
>>> q2 = Quaternion(0, 8, 8, 8)
|
1512 |
+
>>> q3 = Quaternion(0, 24, 24, 24)
|
1513 |
+
>>> Quaternion.vector_coplanar(q1, q2, q3)
|
1514 |
+
True
|
1515 |
+
|
1516 |
+
>>> q1 = Quaternion(0, 8, 16, 8)
|
1517 |
+
>>> q2 = Quaternion(0, 8, 3, 12)
|
1518 |
+
>>> Quaternion.vector_coplanar(q1, q2, q3)
|
1519 |
+
False
|
1520 |
+
|
1521 |
+
See Also
|
1522 |
+
========
|
1523 |
+
|
1524 |
+
axis
|
1525 |
+
is_pure
|
1526 |
+
|
1527 |
+
"""
|
1528 |
+
|
1529 |
+
if fuzzy_not(q1.is_pure()) or fuzzy_not(q2.is_pure()) or fuzzy_not(q3.is_pure()):
|
1530 |
+
raise ValueError('The given quaternions must be pure')
|
1531 |
+
|
1532 |
+
M = Matrix([[q1.b, q1.c, q1.d], [q2.b, q2.c, q2.d], [q3.b, q3.c, q3.d]]).det()
|
1533 |
+
return M.is_zero
|
1534 |
+
|
1535 |
+
def parallel(self, other):
|
1536 |
+
"""
|
1537 |
+
Returns True if the two pure quaternions seen as 3D vectors are parallel.
|
1538 |
+
|
1539 |
+
Explanation
|
1540 |
+
===========
|
1541 |
+
|
1542 |
+
Two pure quaternions are called parallel when their vector product is commutative which
|
1543 |
+
implies that the quaternions seen as 3D vectors have same direction.
|
1544 |
+
|
1545 |
+
Parameters
|
1546 |
+
==========
|
1547 |
+
|
1548 |
+
other : a Quaternion
|
1549 |
+
|
1550 |
+
Returns
|
1551 |
+
=======
|
1552 |
+
|
1553 |
+
True : if the two pure quaternions seen as 3D vectors are parallel.
|
1554 |
+
False : if the two pure quaternions seen as 3D vectors are not parallel.
|
1555 |
+
None : if the two pure quaternions seen as 3D vectors are parallel is unknown.
|
1556 |
+
|
1557 |
+
Examples
|
1558 |
+
========
|
1559 |
+
|
1560 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1561 |
+
>>> q = Quaternion(0, 4, 4, 4)
|
1562 |
+
>>> q1 = Quaternion(0, 8, 8, 8)
|
1563 |
+
>>> q.parallel(q1)
|
1564 |
+
True
|
1565 |
+
|
1566 |
+
>>> q1 = Quaternion(0, 8, 13, 12)
|
1567 |
+
>>> q.parallel(q1)
|
1568 |
+
False
|
1569 |
+
|
1570 |
+
"""
|
1571 |
+
|
1572 |
+
if fuzzy_not(self.is_pure()) or fuzzy_not(other.is_pure()):
|
1573 |
+
raise ValueError('The provided quaternions must be pure')
|
1574 |
+
|
1575 |
+
return (self*other - other*self).is_zero_quaternion()
|
1576 |
+
|
1577 |
+
def orthogonal(self, other):
|
1578 |
+
"""
|
1579 |
+
Returns the orthogonality of two quaternions.
|
1580 |
+
|
1581 |
+
Explanation
|
1582 |
+
===========
|
1583 |
+
|
1584 |
+
Two pure quaternions are called orthogonal when their product is anti-commutative.
|
1585 |
+
|
1586 |
+
Parameters
|
1587 |
+
==========
|
1588 |
+
|
1589 |
+
other : a Quaternion
|
1590 |
+
|
1591 |
+
Returns
|
1592 |
+
=======
|
1593 |
+
|
1594 |
+
True : if the two pure quaternions seen as 3D vectors are orthogonal.
|
1595 |
+
False : if the two pure quaternions seen as 3D vectors are not orthogonal.
|
1596 |
+
None : if the two pure quaternions seen as 3D vectors are orthogonal is unknown.
|
1597 |
+
|
1598 |
+
Examples
|
1599 |
+
========
|
1600 |
+
|
1601 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1602 |
+
>>> q = Quaternion(0, 4, 4, 4)
|
1603 |
+
>>> q1 = Quaternion(0, 8, 8, 8)
|
1604 |
+
>>> q.orthogonal(q1)
|
1605 |
+
False
|
1606 |
+
|
1607 |
+
>>> q1 = Quaternion(0, 2, 2, 0)
|
1608 |
+
>>> q = Quaternion(0, 2, -2, 0)
|
1609 |
+
>>> q.orthogonal(q1)
|
1610 |
+
True
|
1611 |
+
|
1612 |
+
"""
|
1613 |
+
|
1614 |
+
if fuzzy_not(self.is_pure()) or fuzzy_not(other.is_pure()):
|
1615 |
+
raise ValueError('The given quaternions must be pure')
|
1616 |
+
|
1617 |
+
return (self*other + other*self).is_zero_quaternion()
|
1618 |
+
|
1619 |
+
def index_vector(self):
|
1620 |
+
r"""
|
1621 |
+
Returns the index vector of the quaternion.
|
1622 |
+
|
1623 |
+
Explanation
|
1624 |
+
===========
|
1625 |
+
|
1626 |
+
Index vector is given by $\mathbf{T}(q)$ multiplied by $\mathbf{Ax}(q)$ where $\mathbf{Ax}(q)$ is the axis of the quaternion q,
|
1627 |
+
and mod(q) is the $\mathbf{T}(q)$ (magnitude) of the quaternion.
|
1628 |
+
|
1629 |
+
Returns
|
1630 |
+
=======
|
1631 |
+
|
1632 |
+
Quaternion: representing index vector of the provided quaternion.
|
1633 |
+
|
1634 |
+
Examples
|
1635 |
+
========
|
1636 |
+
|
1637 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1638 |
+
>>> q = Quaternion(2, 4, 2, 4)
|
1639 |
+
>>> q.index_vector()
|
1640 |
+
0 + 4*sqrt(10)/3*i + 2*sqrt(10)/3*j + 4*sqrt(10)/3*k
|
1641 |
+
|
1642 |
+
See Also
|
1643 |
+
========
|
1644 |
+
|
1645 |
+
axis
|
1646 |
+
norm
|
1647 |
+
|
1648 |
+
"""
|
1649 |
+
|
1650 |
+
return self.norm() * self.axis()
|
1651 |
+
|
1652 |
+
def mensor(self):
|
1653 |
+
"""
|
1654 |
+
Returns the natural logarithm of the norm(magnitude) of the quaternion.
|
1655 |
+
|
1656 |
+
Examples
|
1657 |
+
========
|
1658 |
+
|
1659 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
1660 |
+
>>> q = Quaternion(2, 4, 2, 4)
|
1661 |
+
>>> q.mensor()
|
1662 |
+
log(2*sqrt(10))
|
1663 |
+
>>> q.norm()
|
1664 |
+
2*sqrt(10)
|
1665 |
+
|
1666 |
+
See Also
|
1667 |
+
========
|
1668 |
+
|
1669 |
+
norm
|
1670 |
+
|
1671 |
+
"""
|
1672 |
+
|
1673 |
+
return ln(self.norm())
|
env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (185 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/__pycache__/test_quaternion.cpython-310.pyc
ADDED
Binary file (15.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/algebras/tests/test_quaternion.py
ADDED
@@ -0,0 +1,409 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.function import diff
|
2 |
+
from sympy.core.function import expand
|
3 |
+
from sympy.core.numbers import (E, I, Rational, pi)
|
4 |
+
from sympy.core.singleton import S
|
5 |
+
from sympy.core.symbol import (Symbol, symbols)
|
6 |
+
from sympy.functions.elementary.complexes import (Abs, conjugate, im, re, sign)
|
7 |
+
from sympy.functions.elementary.exponential import log
|
8 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
9 |
+
from sympy.functions.elementary.trigonometric import (acos, asin, cos, sin, atan2, atan)
|
10 |
+
from sympy.integrals.integrals import integrate
|
11 |
+
from sympy.matrices.dense import Matrix
|
12 |
+
from sympy.simplify import simplify
|
13 |
+
from sympy.simplify.trigsimp import trigsimp
|
14 |
+
from sympy.algebras.quaternion import Quaternion
|
15 |
+
from sympy.testing.pytest import raises
|
16 |
+
from itertools import permutations, product
|
17 |
+
|
18 |
+
w, x, y, z = symbols('w:z')
|
19 |
+
phi = symbols('phi')
|
20 |
+
|
21 |
+
def test_quaternion_construction():
|
22 |
+
q = Quaternion(w, x, y, z)
|
23 |
+
assert q + q == Quaternion(2*w, 2*x, 2*y, 2*z)
|
24 |
+
|
25 |
+
q2 = Quaternion.from_axis_angle((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3),
|
26 |
+
pi*Rational(2, 3))
|
27 |
+
assert q2 == Quaternion(S.Half, S.Half,
|
28 |
+
S.Half, S.Half)
|
29 |
+
|
30 |
+
M = Matrix([[cos(phi), -sin(phi), 0], [sin(phi), cos(phi), 0], [0, 0, 1]])
|
31 |
+
q3 = trigsimp(Quaternion.from_rotation_matrix(M))
|
32 |
+
assert q3 == Quaternion(
|
33 |
+
sqrt(2)*sqrt(cos(phi) + 1)/2, 0, 0, sqrt(2 - 2*cos(phi))*sign(sin(phi))/2)
|
34 |
+
|
35 |
+
nc = Symbol('nc', commutative=False)
|
36 |
+
raises(ValueError, lambda: Quaternion(w, x, nc, z))
|
37 |
+
|
38 |
+
|
39 |
+
def test_quaternion_construction_norm():
|
40 |
+
q1 = Quaternion(*symbols('a:d'))
|
41 |
+
|
42 |
+
q2 = Quaternion(w, x, y, z)
|
43 |
+
assert expand((q1*q2).norm()**2 - (q1.norm()**2 * q2.norm()**2)) == 0
|
44 |
+
|
45 |
+
q3 = Quaternion(w, x, y, z, norm=1)
|
46 |
+
assert (q1 * q3).norm() == q1.norm()
|
47 |
+
|
48 |
+
|
49 |
+
def test_to_and_from_Matrix():
|
50 |
+
q = Quaternion(w, x, y, z)
|
51 |
+
q_full = Quaternion.from_Matrix(q.to_Matrix())
|
52 |
+
q_vect = Quaternion.from_Matrix(q.to_Matrix(True))
|
53 |
+
assert (q - q_full).is_zero_quaternion()
|
54 |
+
assert (q.vector_part() - q_vect).is_zero_quaternion()
|
55 |
+
|
56 |
+
|
57 |
+
def test_product_matrices():
|
58 |
+
q1 = Quaternion(w, x, y, z)
|
59 |
+
q2 = Quaternion(*(symbols("a:d")))
|
60 |
+
assert (q1 * q2).to_Matrix() == q1.product_matrix_left * q2.to_Matrix()
|
61 |
+
assert (q1 * q2).to_Matrix() == q2.product_matrix_right * q1.to_Matrix()
|
62 |
+
|
63 |
+
R1 = (q1.product_matrix_left * q1.product_matrix_right.T)[1:, 1:]
|
64 |
+
R2 = simplify(q1.to_rotation_matrix()*q1.norm()**2)
|
65 |
+
assert R1 == R2
|
66 |
+
|
67 |
+
|
68 |
+
def test_quaternion_axis_angle():
|
69 |
+
|
70 |
+
test_data = [ # axis, angle, expected_quaternion
|
71 |
+
((1, 0, 0), 0, (1, 0, 0, 0)),
|
72 |
+
((1, 0, 0), pi/2, (sqrt(2)/2, sqrt(2)/2, 0, 0)),
|
73 |
+
((0, 1, 0), pi/2, (sqrt(2)/2, 0, sqrt(2)/2, 0)),
|
74 |
+
((0, 0, 1), pi/2, (sqrt(2)/2, 0, 0, sqrt(2)/2)),
|
75 |
+
((1, 0, 0), pi, (0, 1, 0, 0)),
|
76 |
+
((0, 1, 0), pi, (0, 0, 1, 0)),
|
77 |
+
((0, 0, 1), pi, (0, 0, 0, 1)),
|
78 |
+
((1, 1, 1), pi, (0, 1/sqrt(3),1/sqrt(3),1/sqrt(3))),
|
79 |
+
((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3), pi*2/3, (S.Half, S.Half, S.Half, S.Half))
|
80 |
+
]
|
81 |
+
|
82 |
+
for axis, angle, expected in test_data:
|
83 |
+
assert Quaternion.from_axis_angle(axis, angle) == Quaternion(*expected)
|
84 |
+
|
85 |
+
|
86 |
+
def test_quaternion_axis_angle_simplification():
|
87 |
+
result = Quaternion.from_axis_angle((1, 2, 3), asin(4))
|
88 |
+
assert result.a == cos(asin(4)/2)
|
89 |
+
assert result.b == sqrt(14)*sin(asin(4)/2)/14
|
90 |
+
assert result.c == sqrt(14)*sin(asin(4)/2)/7
|
91 |
+
assert result.d == 3*sqrt(14)*sin(asin(4)/2)/14
|
92 |
+
|
93 |
+
def test_quaternion_complex_real_addition():
|
94 |
+
a = symbols("a", complex=True)
|
95 |
+
b = symbols("b", real=True)
|
96 |
+
# This symbol is not complex:
|
97 |
+
c = symbols("c", commutative=False)
|
98 |
+
|
99 |
+
q = Quaternion(w, x, y, z)
|
100 |
+
assert a + q == Quaternion(w + re(a), x + im(a), y, z)
|
101 |
+
assert 1 + q == Quaternion(1 + w, x, y, z)
|
102 |
+
assert I + q == Quaternion(w, 1 + x, y, z)
|
103 |
+
assert b + q == Quaternion(w + b, x, y, z)
|
104 |
+
raises(ValueError, lambda: c + q)
|
105 |
+
raises(ValueError, lambda: q * c)
|
106 |
+
raises(ValueError, lambda: c * q)
|
107 |
+
|
108 |
+
assert -q == Quaternion(-w, -x, -y, -z)
|
109 |
+
|
110 |
+
q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
111 |
+
q2 = Quaternion(1, 4, 7, 8)
|
112 |
+
|
113 |
+
assert q1 + (2 + 3*I) == Quaternion(5 + 7*I, 2 + 5*I, 0, 7 + 8*I)
|
114 |
+
assert q2 + (2 + 3*I) == Quaternion(3, 7, 7, 8)
|
115 |
+
assert q1 * (2 + 3*I) == \
|
116 |
+
Quaternion((2 + 3*I)*(3 + 4*I), (2 + 3*I)*(2 + 5*I), 0, (2 + 3*I)*(7 + 8*I))
|
117 |
+
assert q2 * (2 + 3*I) == Quaternion(-10, 11, 38, -5)
|
118 |
+
|
119 |
+
q1 = Quaternion(1, 2, 3, 4)
|
120 |
+
q0 = Quaternion(0, 0, 0, 0)
|
121 |
+
assert q1 + q0 == q1
|
122 |
+
assert q1 - q0 == q1
|
123 |
+
assert q1 - q1 == q0
|
124 |
+
|
125 |
+
|
126 |
+
def test_quaternion_evalf():
|
127 |
+
assert (Quaternion(sqrt(2), 0, 0, sqrt(3)).evalf() ==
|
128 |
+
Quaternion(sqrt(2).evalf(), 0, 0, sqrt(3).evalf()))
|
129 |
+
assert (Quaternion(1/sqrt(2), 0, 0, 1/sqrt(2)).evalf() ==
|
130 |
+
Quaternion((1/sqrt(2)).evalf(), 0, 0, (1/sqrt(2)).evalf()))
|
131 |
+
|
132 |
+
|
133 |
+
def test_quaternion_functions():
|
134 |
+
q = Quaternion(w, x, y, z)
|
135 |
+
q1 = Quaternion(1, 2, 3, 4)
|
136 |
+
q0 = Quaternion(0, 0, 0, 0)
|
137 |
+
|
138 |
+
assert conjugate(q) == Quaternion(w, -x, -y, -z)
|
139 |
+
assert q.norm() == sqrt(w**2 + x**2 + y**2 + z**2)
|
140 |
+
assert q.normalize() == Quaternion(w, x, y, z) / sqrt(w**2 + x**2 + y**2 + z**2)
|
141 |
+
assert q.inverse() == Quaternion(w, -x, -y, -z) / (w**2 + x**2 + y**2 + z**2)
|
142 |
+
assert q.inverse() == q.pow(-1)
|
143 |
+
raises(ValueError, lambda: q0.inverse())
|
144 |
+
assert q.pow(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
|
145 |
+
assert q**(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
|
146 |
+
assert q1.pow(-2) == Quaternion(
|
147 |
+
Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
|
148 |
+
assert q1**(-2) == Quaternion(
|
149 |
+
Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
|
150 |
+
assert q1.pow(-0.5) == NotImplemented
|
151 |
+
raises(TypeError, lambda: q1**(-0.5))
|
152 |
+
|
153 |
+
assert q1.exp() == \
|
154 |
+
Quaternion(E * cos(sqrt(29)),
|
155 |
+
2 * sqrt(29) * E * sin(sqrt(29)) / 29,
|
156 |
+
3 * sqrt(29) * E * sin(sqrt(29)) / 29,
|
157 |
+
4 * sqrt(29) * E * sin(sqrt(29)) / 29)
|
158 |
+
assert q1._ln() == \
|
159 |
+
Quaternion(log(sqrt(30)),
|
160 |
+
2 * sqrt(29) * acos(sqrt(30)/30) / 29,
|
161 |
+
3 * sqrt(29) * acos(sqrt(30)/30) / 29,
|
162 |
+
4 * sqrt(29) * acos(sqrt(30)/30) / 29)
|
163 |
+
|
164 |
+
assert q1.pow_cos_sin(2) == \
|
165 |
+
Quaternion(30 * cos(2 * acos(sqrt(30)/30)),
|
166 |
+
60 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
|
167 |
+
90 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
|
168 |
+
120 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29)
|
169 |
+
|
170 |
+
assert diff(Quaternion(x, x, x, x), x) == Quaternion(1, 1, 1, 1)
|
171 |
+
|
172 |
+
assert integrate(Quaternion(x, x, x, x), x) == \
|
173 |
+
Quaternion(x**2 / 2, x**2 / 2, x**2 / 2, x**2 / 2)
|
174 |
+
|
175 |
+
assert Quaternion.rotate_point((1, 1, 1), q1) == (S.One / 5, 1, S(7) / 5)
|
176 |
+
n = Symbol('n')
|
177 |
+
raises(TypeError, lambda: q1**n)
|
178 |
+
n = Symbol('n', integer=True)
|
179 |
+
raises(TypeError, lambda: q1**n)
|
180 |
+
|
181 |
+
assert Quaternion(22, 23, 55, 8).scalar_part() == 22
|
182 |
+
assert Quaternion(w, x, y, z).scalar_part() == w
|
183 |
+
|
184 |
+
assert Quaternion(22, 23, 55, 8).vector_part() == Quaternion(0, 23, 55, 8)
|
185 |
+
assert Quaternion(w, x, y, z).vector_part() == Quaternion(0, x, y, z)
|
186 |
+
|
187 |
+
assert q1.axis() == Quaternion(0, 2*sqrt(29)/29, 3*sqrt(29)/29, 4*sqrt(29)/29)
|
188 |
+
assert q1.axis().pow(2) == Quaternion(-1, 0, 0, 0)
|
189 |
+
assert q0.axis().scalar_part() == 0
|
190 |
+
assert (q.axis() == Quaternion(0,
|
191 |
+
x/sqrt(x**2 + y**2 + z**2),
|
192 |
+
y/sqrt(x**2 + y**2 + z**2),
|
193 |
+
z/sqrt(x**2 + y**2 + z**2)))
|
194 |
+
|
195 |
+
assert q0.is_pure() is True
|
196 |
+
assert q1.is_pure() is False
|
197 |
+
assert Quaternion(0, 0, 0, 3).is_pure() is True
|
198 |
+
assert Quaternion(0, 2, 10, 3).is_pure() is True
|
199 |
+
assert Quaternion(w, 2, 10, 3).is_pure() is None
|
200 |
+
|
201 |
+
assert q1.angle() == atan(sqrt(29))
|
202 |
+
assert q.angle() == atan2(sqrt(x**2 + y**2 + z**2), w)
|
203 |
+
|
204 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(2, 4, 6, 8)) is True
|
205 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(1, -2, -3, -4)) is True
|
206 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(1, 8, 12, 16)) is True
|
207 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(1, 2, 3, 4)) is True
|
208 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(w, 4, 6, 8)) is True
|
209 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(2, 7, 4, 1)) is False
|
210 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(w, x, y, z)) is None
|
211 |
+
raises(ValueError, lambda: Quaternion.arc_coplanar(q1, q0))
|
212 |
+
|
213 |
+
assert Quaternion.vector_coplanar(
|
214 |
+
Quaternion(0, 8, 12, 16),
|
215 |
+
Quaternion(0, 4, 6, 8),
|
216 |
+
Quaternion(0, 2, 3, 4)) is True
|
217 |
+
assert Quaternion.vector_coplanar(
|
218 |
+
Quaternion(0, 0, 0, 0), Quaternion(0, 4, 6, 8), Quaternion(0, 2, 3, 4)) is True
|
219 |
+
assert Quaternion.vector_coplanar(
|
220 |
+
Quaternion(0, 8, 2, 6), Quaternion(0, 1, 6, 6), Quaternion(0, 0, 3, 4)) is False
|
221 |
+
assert Quaternion.vector_coplanar(
|
222 |
+
Quaternion(0, 1, 3, 4),
|
223 |
+
Quaternion(0, 4, w, 6),
|
224 |
+
Quaternion(0, 6, 8, 1)) is None
|
225 |
+
raises(ValueError, lambda:
|
226 |
+
Quaternion.vector_coplanar(q0, Quaternion(0, 4, 6, 8), q1))
|
227 |
+
|
228 |
+
assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 4, 6)) is True
|
229 |
+
assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 2, 6)) is False
|
230 |
+
assert Quaternion(0, 1, 2, 3).parallel(Quaternion(w, x, y, 6)) is None
|
231 |
+
raises(ValueError, lambda: q0.parallel(q1))
|
232 |
+
|
233 |
+
assert Quaternion(0, 1, 2, 3).orthogonal(Quaternion(0, -2, 1, 0)) is True
|
234 |
+
assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(0, 2, 2, 6)) is False
|
235 |
+
assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(w, x, y, 6)) is None
|
236 |
+
raises(ValueError, lambda: q0.orthogonal(q1))
|
237 |
+
|
238 |
+
assert q1.index_vector() == Quaternion(
|
239 |
+
0, 2*sqrt(870)/29,
|
240 |
+
3*sqrt(870)/29,
|
241 |
+
4*sqrt(870)/29)
|
242 |
+
assert Quaternion(0, 3, 9, 4).index_vector() == Quaternion(0, 3, 9, 4)
|
243 |
+
|
244 |
+
assert Quaternion(4, 3, 9, 4).mensor() == log(sqrt(122))
|
245 |
+
assert Quaternion(3, 3, 0, 2).mensor() == log(sqrt(22))
|
246 |
+
|
247 |
+
assert q0.is_zero_quaternion() is True
|
248 |
+
assert q1.is_zero_quaternion() is False
|
249 |
+
assert Quaternion(w, 0, 0, 0).is_zero_quaternion() is None
|
250 |
+
|
251 |
+
def test_quaternion_conversions():
|
252 |
+
q1 = Quaternion(1, 2, 3, 4)
|
253 |
+
|
254 |
+
assert q1.to_axis_angle() == ((2 * sqrt(29)/29,
|
255 |
+
3 * sqrt(29)/29,
|
256 |
+
4 * sqrt(29)/29),
|
257 |
+
2 * acos(sqrt(30)/30))
|
258 |
+
|
259 |
+
assert (q1.to_rotation_matrix() ==
|
260 |
+
Matrix([[Rational(-2, 3), Rational(2, 15), Rational(11, 15)],
|
261 |
+
[Rational(2, 3), Rational(-1, 3), Rational(2, 3)],
|
262 |
+
[Rational(1, 3), Rational(14, 15), Rational(2, 15)]]))
|
263 |
+
|
264 |
+
assert (q1.to_rotation_matrix((1, 1, 1)) ==
|
265 |
+
Matrix([
|
266 |
+
[Rational(-2, 3), Rational(2, 15), Rational(11, 15), Rational(4, 5)],
|
267 |
+
[Rational(2, 3), Rational(-1, 3), Rational(2, 3), S.Zero],
|
268 |
+
[Rational(1, 3), Rational(14, 15), Rational(2, 15), Rational(-2, 5)],
|
269 |
+
[S.Zero, S.Zero, S.Zero, S.One]]))
|
270 |
+
|
271 |
+
theta = symbols("theta", real=True)
|
272 |
+
q2 = Quaternion(cos(theta/2), 0, 0, sin(theta/2))
|
273 |
+
|
274 |
+
assert trigsimp(q2.to_rotation_matrix()) == Matrix([
|
275 |
+
[cos(theta), -sin(theta), 0],
|
276 |
+
[sin(theta), cos(theta), 0],
|
277 |
+
[0, 0, 1]])
|
278 |
+
|
279 |
+
assert q2.to_axis_angle() == ((0, 0, sin(theta/2)/Abs(sin(theta/2))),
|
280 |
+
2*acos(cos(theta/2)))
|
281 |
+
|
282 |
+
assert trigsimp(q2.to_rotation_matrix((1, 1, 1))) == Matrix([
|
283 |
+
[cos(theta), -sin(theta), 0, sin(theta) - cos(theta) + 1],
|
284 |
+
[sin(theta), cos(theta), 0, -sin(theta) - cos(theta) + 1],
|
285 |
+
[0, 0, 1, 0],
|
286 |
+
[0, 0, 0, 1]])
|
287 |
+
|
288 |
+
|
289 |
+
def test_rotation_matrix_homogeneous():
|
290 |
+
q = Quaternion(w, x, y, z)
|
291 |
+
R1 = q.to_rotation_matrix(homogeneous=True) * q.norm()**2
|
292 |
+
R2 = simplify(q.to_rotation_matrix(homogeneous=False) * q.norm()**2)
|
293 |
+
assert R1 == R2
|
294 |
+
|
295 |
+
|
296 |
+
def test_quaternion_rotation_iss1593():
|
297 |
+
"""
|
298 |
+
There was a sign mistake in the definition,
|
299 |
+
of the rotation matrix. This tests that particular sign mistake.
|
300 |
+
See issue 1593 for reference.
|
301 |
+
See wikipedia
|
302 |
+
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix
|
303 |
+
for the correct definition
|
304 |
+
"""
|
305 |
+
q = Quaternion(cos(phi/2), sin(phi/2), 0, 0)
|
306 |
+
assert(trigsimp(q.to_rotation_matrix()) == Matrix([
|
307 |
+
[1, 0, 0],
|
308 |
+
[0, cos(phi), -sin(phi)],
|
309 |
+
[0, sin(phi), cos(phi)]]))
|
310 |
+
|
311 |
+
|
312 |
+
def test_quaternion_multiplication():
|
313 |
+
q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
314 |
+
q2 = Quaternion(1, 2, 3, 5)
|
315 |
+
q3 = Quaternion(1, 1, 1, y)
|
316 |
+
|
317 |
+
assert Quaternion._generic_mul(S(4), S.One) == 4
|
318 |
+
assert (Quaternion._generic_mul(S(4), q1) ==
|
319 |
+
Quaternion(12 + 16*I, 8 + 20*I, 0, 28 + 32*I))
|
320 |
+
assert q2.mul(2) == Quaternion(2, 4, 6, 10)
|
321 |
+
assert q2.mul(q3) == Quaternion(-5*y - 4, 3*y - 2, 9 - 2*y, y + 4)
|
322 |
+
assert q2.mul(q3) == q2*q3
|
323 |
+
|
324 |
+
z = symbols('z', complex=True)
|
325 |
+
z_quat = Quaternion(re(z), im(z), 0, 0)
|
326 |
+
q = Quaternion(*symbols('q:4', real=True))
|
327 |
+
|
328 |
+
assert z * q == z_quat * q
|
329 |
+
assert q * z == q * z_quat
|
330 |
+
|
331 |
+
|
332 |
+
def test_issue_16318():
|
333 |
+
#for rtruediv
|
334 |
+
q0 = Quaternion(0, 0, 0, 0)
|
335 |
+
raises(ValueError, lambda: 1/q0)
|
336 |
+
#for rotate_point
|
337 |
+
q = Quaternion(1, 2, 3, 4)
|
338 |
+
(axis, angle) = q.to_axis_angle()
|
339 |
+
assert Quaternion.rotate_point((1, 1, 1), (axis, angle)) == (S.One / 5, 1, S(7) / 5)
|
340 |
+
#test for to_axis_angle
|
341 |
+
q = Quaternion(-1, 1, 1, 1)
|
342 |
+
axis = (-sqrt(3)/3, -sqrt(3)/3, -sqrt(3)/3)
|
343 |
+
angle = 2*pi/3
|
344 |
+
assert (axis, angle) == q.to_axis_angle()
|
345 |
+
|
346 |
+
|
347 |
+
def test_to_euler():
|
348 |
+
q = Quaternion(w, x, y, z)
|
349 |
+
q_normalized = q.normalize()
|
350 |
+
|
351 |
+
seqs = ['zxy', 'zyx', 'zyz', 'zxz']
|
352 |
+
seqs += [seq.upper() for seq in seqs]
|
353 |
+
|
354 |
+
for seq in seqs:
|
355 |
+
euler_from_q = q.to_euler(seq)
|
356 |
+
q_back = simplify(Quaternion.from_euler(euler_from_q, seq))
|
357 |
+
assert q_back == q_normalized
|
358 |
+
|
359 |
+
|
360 |
+
def test_to_euler_iss24504():
|
361 |
+
"""
|
362 |
+
There was a mistake in the degenerate case testing
|
363 |
+
See issue 24504 for reference.
|
364 |
+
"""
|
365 |
+
q = Quaternion.from_euler((phi, 0, 0), 'zyz')
|
366 |
+
assert trigsimp(q.to_euler('zyz'), inverse=True) == (phi, 0, 0)
|
367 |
+
|
368 |
+
|
369 |
+
def test_to_euler_numerical_singilarities():
|
370 |
+
|
371 |
+
def test_one_case(angles, seq):
|
372 |
+
q = Quaternion.from_euler(angles, seq)
|
373 |
+
assert q.to_euler(seq) == angles
|
374 |
+
|
375 |
+
# symmetric
|
376 |
+
test_one_case((pi/2, 0, 0), 'zyz')
|
377 |
+
test_one_case((pi/2, 0, 0), 'ZYZ')
|
378 |
+
test_one_case((pi/2, pi, 0), 'zyz')
|
379 |
+
test_one_case((pi/2, pi, 0), 'ZYZ')
|
380 |
+
|
381 |
+
# asymmetric
|
382 |
+
test_one_case((pi/2, pi/2, 0), 'zyx')
|
383 |
+
test_one_case((pi/2, -pi/2, 0), 'zyx')
|
384 |
+
test_one_case((pi/2, pi/2, 0), 'ZYX')
|
385 |
+
test_one_case((pi/2, -pi/2, 0), 'ZYX')
|
386 |
+
|
387 |
+
|
388 |
+
def test_to_euler_options():
|
389 |
+
def test_one_case(q):
|
390 |
+
angles1 = Matrix(q.to_euler(seq, True, True))
|
391 |
+
angles2 = Matrix(q.to_euler(seq, False, False))
|
392 |
+
angle_errors = simplify(angles1-angles2).evalf()
|
393 |
+
for angle_error in angle_errors:
|
394 |
+
# forcing angles to set {-pi, pi}
|
395 |
+
angle_error = (angle_error + pi) % (2 * pi) - pi
|
396 |
+
assert angle_error < 10e-7
|
397 |
+
|
398 |
+
for xyz in ('xyz', 'XYZ'):
|
399 |
+
for seq_tuple in permutations(xyz):
|
400 |
+
for symmetric in (True, False):
|
401 |
+
if symmetric:
|
402 |
+
seq = ''.join([seq_tuple[0], seq_tuple[1], seq_tuple[0]])
|
403 |
+
else:
|
404 |
+
seq = ''.join(seq_tuple)
|
405 |
+
|
406 |
+
for elements in product([-1, 0, 1], repeat=4):
|
407 |
+
q = Quaternion(*elements)
|
408 |
+
if not q.is_zero_quaternion():
|
409 |
+
test_one_case(q)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__init__.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of mathematical domains. """
|
2 |
+
|
3 |
+
__all__ = [
|
4 |
+
'Domain', 'FiniteField', 'IntegerRing', 'RationalField', 'RealField',
|
5 |
+
'ComplexField', 'AlgebraicField', 'PolynomialRing', 'FractionField',
|
6 |
+
'ExpressionDomain', 'PythonRational',
|
7 |
+
|
8 |
+
'GF', 'FF', 'ZZ', 'QQ', 'ZZ_I', 'QQ_I', 'RR', 'CC', 'EX', 'EXRAW',
|
9 |
+
]
|
10 |
+
|
11 |
+
from .domain import Domain
|
12 |
+
from .finitefield import FiniteField, FF, GF
|
13 |
+
from .integerring import IntegerRing, ZZ
|
14 |
+
from .rationalfield import RationalField, QQ
|
15 |
+
from .algebraicfield import AlgebraicField
|
16 |
+
from .gaussiandomains import ZZ_I, QQ_I
|
17 |
+
from .realfield import RealField, RR
|
18 |
+
from .complexfield import ComplexField, CC
|
19 |
+
from .polynomialring import PolynomialRing
|
20 |
+
from .fractionfield import FractionField
|
21 |
+
from .expressiondomain import ExpressionDomain, EX
|
22 |
+
from .expressionrawdomain import EXRAW
|
23 |
+
from .pythonrational import PythonRational
|
24 |
+
|
25 |
+
|
26 |
+
# This is imported purely for backwards compatibility because some parts of
|
27 |
+
# the codebase used to import this from here and it's possible that downstream
|
28 |
+
# does as well:
|
29 |
+
from sympy.external.gmpy import GROUND_TYPES # noqa: F401
|
30 |
+
|
31 |
+
#
|
32 |
+
# The rest of these are obsolete and provided only for backwards
|
33 |
+
# compatibility:
|
34 |
+
#
|
35 |
+
|
36 |
+
from .pythonfinitefield import PythonFiniteField
|
37 |
+
from .gmpyfinitefield import GMPYFiniteField
|
38 |
+
from .pythonintegerring import PythonIntegerRing
|
39 |
+
from .gmpyintegerring import GMPYIntegerRing
|
40 |
+
from .pythonrationalfield import PythonRationalField
|
41 |
+
from .gmpyrationalfield import GMPYRationalField
|
42 |
+
|
43 |
+
FF_python = PythonFiniteField
|
44 |
+
FF_gmpy = GMPYFiniteField
|
45 |
+
|
46 |
+
ZZ_python = PythonIntegerRing
|
47 |
+
ZZ_gmpy = GMPYIntegerRing
|
48 |
+
|
49 |
+
QQ_python = PythonRationalField
|
50 |
+
QQ_gmpy = GMPYRationalField
|
51 |
+
|
52 |
+
__all__.extend((
|
53 |
+
'PythonFiniteField', 'GMPYFiniteField', 'PythonIntegerRing',
|
54 |
+
'GMPYIntegerRing', 'PythonRational', 'GMPYRationalField',
|
55 |
+
|
56 |
+
'FF_python', 'FF_gmpy', 'ZZ_python', 'ZZ_gmpy', 'QQ_python', 'QQ_gmpy',
|
57 |
+
))
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.7 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/algebraicfield.cpython-310.pyc
ADDED
Binary file (23 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/compositedomain.cpython-310.pyc
ADDED
Binary file (1.47 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/domain.cpython-310.pyc
ADDED
Binary file (35.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/expressiondomain.cpython-310.pyc
ADDED
Binary file (9.94 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/field.cpython-310.pyc
ADDED
Binary file (3.28 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/finitefield.cpython-310.pyc
ADDED
Binary file (6.94 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/gaussiandomains.cpython-310.pyc
ADDED
Binary file (20.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/groundtypes.cpython-310.pyc
ADDED
Binary file (1.71 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/integerring.cpython-310.pyc
ADDED
Binary file (7.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/modularinteger.cpython-310.pyc
ADDED
Binary file (6.63 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_fractionfield.cpython-310.pyc
ADDED
Binary file (7.51 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/old_polynomialring.cpython-310.pyc
ADDED
Binary file (17.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonfinitefield.cpython-310.pyc
ADDED
Binary file (913 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/pythonrationalfield.cpython-310.pyc
ADDED
Binary file (3.31 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/quotientring.cpython-310.pyc
ADDED
Binary file (7.28 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/rationalfield.cpython-310.pyc
ADDED
Binary file (6.28 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/__pycache__/ring.cpython-310.pyc
ADDED
Binary file (4.24 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/field.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`Field` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.ring import Ring
|
5 |
+
from sympy.polys.polyerrors import NotReversible, DomainError
|
6 |
+
from sympy.utilities import public
|
7 |
+
|
8 |
+
@public
|
9 |
+
class Field(Ring):
|
10 |
+
"""Represents a field domain. """
|
11 |
+
|
12 |
+
is_Field = True
|
13 |
+
is_PID = True
|
14 |
+
|
15 |
+
def get_ring(self):
|
16 |
+
"""Returns a ring associated with ``self``. """
|
17 |
+
raise DomainError('there is no ring associated with %s' % self)
|
18 |
+
|
19 |
+
def get_field(self):
|
20 |
+
"""Returns a field associated with ``self``. """
|
21 |
+
return self
|
22 |
+
|
23 |
+
def exquo(self, a, b):
|
24 |
+
"""Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
25 |
+
return a / b
|
26 |
+
|
27 |
+
def quo(self, a, b):
|
28 |
+
"""Quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
29 |
+
return a / b
|
30 |
+
|
31 |
+
def rem(self, a, b):
|
32 |
+
"""Remainder of ``a`` and ``b``, implies nothing. """
|
33 |
+
return self.zero
|
34 |
+
|
35 |
+
def div(self, a, b):
|
36 |
+
"""Division of ``a`` and ``b``, implies ``__truediv__``. """
|
37 |
+
return a / b, self.zero
|
38 |
+
|
39 |
+
def gcd(self, a, b):
|
40 |
+
"""
|
41 |
+
Returns GCD of ``a`` and ``b``.
|
42 |
+
|
43 |
+
This definition of GCD over fields allows to clear denominators
|
44 |
+
in `primitive()`.
|
45 |
+
|
46 |
+
Examples
|
47 |
+
========
|
48 |
+
|
49 |
+
>>> from sympy.polys.domains import QQ
|
50 |
+
>>> from sympy import S, gcd, primitive
|
51 |
+
>>> from sympy.abc import x
|
52 |
+
|
53 |
+
>>> QQ.gcd(QQ(2, 3), QQ(4, 9))
|
54 |
+
2/9
|
55 |
+
>>> gcd(S(2)/3, S(4)/9)
|
56 |
+
2/9
|
57 |
+
>>> primitive(2*x/3 + S(4)/9)
|
58 |
+
(2/9, 3*x + 2)
|
59 |
+
|
60 |
+
"""
|
61 |
+
try:
|
62 |
+
ring = self.get_ring()
|
63 |
+
except DomainError:
|
64 |
+
return self.one
|
65 |
+
|
66 |
+
p = ring.gcd(self.numer(a), self.numer(b))
|
67 |
+
q = ring.lcm(self.denom(a), self.denom(b))
|
68 |
+
|
69 |
+
return self.convert(p, ring)/q
|
70 |
+
|
71 |
+
def lcm(self, a, b):
|
72 |
+
"""
|
73 |
+
Returns LCM of ``a`` and ``b``.
|
74 |
+
|
75 |
+
>>> from sympy.polys.domains import QQ
|
76 |
+
>>> from sympy import S, lcm
|
77 |
+
|
78 |
+
>>> QQ.lcm(QQ(2, 3), QQ(4, 9))
|
79 |
+
4/3
|
80 |
+
>>> lcm(S(2)/3, S(4)/9)
|
81 |
+
4/3
|
82 |
+
|
83 |
+
"""
|
84 |
+
|
85 |
+
try:
|
86 |
+
ring = self.get_ring()
|
87 |
+
except DomainError:
|
88 |
+
return a*b
|
89 |
+
|
90 |
+
p = ring.lcm(self.numer(a), self.numer(b))
|
91 |
+
q = ring.gcd(self.denom(a), self.denom(b))
|
92 |
+
|
93 |
+
return self.convert(p, ring)/q
|
94 |
+
|
95 |
+
def revert(self, a):
|
96 |
+
"""Returns ``a**(-1)`` if possible. """
|
97 |
+
if a:
|
98 |
+
return 1/a
|
99 |
+
else:
|
100 |
+
raise NotReversible('zero is not reversible')
|
101 |
+
|
102 |
+
def is_unit(self, a):
|
103 |
+
"""Return true if ``a`` is a invertible"""
|
104 |
+
return bool(a)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/groundtypes.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Ground types for various mathematical domains in SymPy. """
|
2 |
+
|
3 |
+
import builtins
|
4 |
+
from sympy.external.gmpy import HAS_GMPY, factorial, sqrt
|
5 |
+
|
6 |
+
PythonInteger = builtins.int
|
7 |
+
PythonReal = builtins.float
|
8 |
+
PythonComplex = builtins.complex
|
9 |
+
|
10 |
+
from .pythonrational import PythonRational
|
11 |
+
|
12 |
+
from sympy.core.numbers import (
|
13 |
+
igcdex as python_gcdex,
|
14 |
+
igcd2 as python_gcd,
|
15 |
+
ilcm as python_lcm,
|
16 |
+
)
|
17 |
+
|
18 |
+
from sympy.core.numbers import (Float as SymPyReal, Integer as SymPyInteger, Rational as SymPyRational)
|
19 |
+
|
20 |
+
|
21 |
+
if HAS_GMPY == 2:
|
22 |
+
from gmpy2 import (
|
23 |
+
mpz as GMPYInteger,
|
24 |
+
mpq as GMPYRational,
|
25 |
+
numer as gmpy_numer,
|
26 |
+
denom as gmpy_denom,
|
27 |
+
gcdext as gmpy_gcdex,
|
28 |
+
gcd as gmpy_gcd,
|
29 |
+
lcm as gmpy_lcm,
|
30 |
+
qdiv as gmpy_qdiv,
|
31 |
+
)
|
32 |
+
gcdex = gmpy_gcdex
|
33 |
+
gcd = gmpy_gcd
|
34 |
+
lcm = gmpy_lcm
|
35 |
+
else:
|
36 |
+
class _GMPYInteger:
|
37 |
+
def __init__(self, obj):
|
38 |
+
pass
|
39 |
+
|
40 |
+
class _GMPYRational:
|
41 |
+
def __init__(self, obj):
|
42 |
+
pass
|
43 |
+
|
44 |
+
GMPYInteger = _GMPYInteger
|
45 |
+
GMPYRational = _GMPYRational
|
46 |
+
gmpy_numer = None
|
47 |
+
gmpy_denom = None
|
48 |
+
gmpy_gcdex = None
|
49 |
+
gmpy_gcd = None
|
50 |
+
gmpy_lcm = None
|
51 |
+
gmpy_qdiv = None
|
52 |
+
gcdex = python_gcdex
|
53 |
+
gcd = python_gcd
|
54 |
+
lcm = python_lcm
|
55 |
+
|
56 |
+
|
57 |
+
__all__ = [
|
58 |
+
'PythonInteger', 'PythonReal', 'PythonComplex',
|
59 |
+
|
60 |
+
'PythonRational',
|
61 |
+
|
62 |
+
'python_gcdex', 'python_gcd', 'python_lcm',
|
63 |
+
|
64 |
+
'SymPyReal', 'SymPyInteger', 'SymPyRational',
|
65 |
+
|
66 |
+
'GMPYInteger', 'GMPYRational', 'gmpy_numer',
|
67 |
+
'gmpy_denom', 'gmpy_gcdex', 'gmpy_gcd', 'gmpy_lcm',
|
68 |
+
'gmpy_qdiv',
|
69 |
+
|
70 |
+
'factorial', 'sqrt',
|
71 |
+
|
72 |
+
'GMPYInteger', 'GMPYRational',
|
73 |
+
]
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/pythonfinitefield.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`PythonFiniteField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.finitefield import FiniteField
|
5 |
+
from sympy.polys.domains.pythonintegerring import PythonIntegerRing
|
6 |
+
|
7 |
+
from sympy.utilities import public
|
8 |
+
|
9 |
+
@public
|
10 |
+
class PythonFiniteField(FiniteField):
|
11 |
+
"""Finite field based on Python's integers. """
|
12 |
+
|
13 |
+
alias = 'FF_python'
|
14 |
+
|
15 |
+
def __init__(self, mod, symmetric=True):
|
16 |
+
return super().__init__(mod, PythonIntegerRing(), symmetric)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/pythonrational.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Rational number type based on Python integers.
|
3 |
+
|
4 |
+
The PythonRational class from here has been moved to
|
5 |
+
sympy.external.pythonmpq
|
6 |
+
|
7 |
+
This module is just left here for backwards compatibility.
|
8 |
+
"""
|
9 |
+
|
10 |
+
|
11 |
+
from sympy.core.numbers import Rational
|
12 |
+
from sympy.core.sympify import _sympy_converter
|
13 |
+
from sympy.utilities import public
|
14 |
+
from sympy.external.pythonmpq import PythonMPQ
|
15 |
+
|
16 |
+
|
17 |
+
PythonRational = public(PythonMPQ)
|
18 |
+
|
19 |
+
|
20 |
+
def sympify_pythonrational(arg):
|
21 |
+
return Rational(arg.numerator, arg.denominator)
|
22 |
+
_sympy_converter[PythonRational] = sympify_pythonrational
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/rationalfield.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`RationalField` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.external.gmpy import MPQ
|
5 |
+
|
6 |
+
from sympy.polys.domains.groundtypes import SymPyRational
|
7 |
+
|
8 |
+
from sympy.polys.domains.characteristiczero import CharacteristicZero
|
9 |
+
from sympy.polys.domains.field import Field
|
10 |
+
from sympy.polys.domains.simpledomain import SimpleDomain
|
11 |
+
from sympy.polys.polyerrors import CoercionFailed
|
12 |
+
from sympy.utilities import public
|
13 |
+
|
14 |
+
@public
|
15 |
+
class RationalField(Field, CharacteristicZero, SimpleDomain):
|
16 |
+
r"""Abstract base class for the domain :ref:`QQ`.
|
17 |
+
|
18 |
+
The :py:class:`RationalField` class represents the field of rational
|
19 |
+
numbers $\mathbb{Q}$ as a :py:class:`~.Domain` in the domain system.
|
20 |
+
:py:class:`RationalField` is a superclass of
|
21 |
+
:py:class:`PythonRationalField` and :py:class:`GMPYRationalField` one of
|
22 |
+
which will be the implementation for :ref:`QQ` depending on whether either
|
23 |
+
of ``gmpy`` or ``gmpy2`` is installed or not.
|
24 |
+
|
25 |
+
See also
|
26 |
+
========
|
27 |
+
|
28 |
+
Domain
|
29 |
+
"""
|
30 |
+
|
31 |
+
rep = 'QQ'
|
32 |
+
alias = 'QQ'
|
33 |
+
|
34 |
+
is_RationalField = is_QQ = True
|
35 |
+
is_Numerical = True
|
36 |
+
|
37 |
+
has_assoc_Ring = True
|
38 |
+
has_assoc_Field = True
|
39 |
+
|
40 |
+
dtype = MPQ
|
41 |
+
zero = dtype(0)
|
42 |
+
one = dtype(1)
|
43 |
+
tp = type(one)
|
44 |
+
|
45 |
+
def __init__(self):
|
46 |
+
pass
|
47 |
+
|
48 |
+
def get_ring(self):
|
49 |
+
"""Returns ring associated with ``self``. """
|
50 |
+
from sympy.polys.domains import ZZ
|
51 |
+
return ZZ
|
52 |
+
|
53 |
+
def to_sympy(self, a):
|
54 |
+
"""Convert ``a`` to a SymPy object. """
|
55 |
+
return SymPyRational(int(a.numerator), int(a.denominator))
|
56 |
+
|
57 |
+
def from_sympy(self, a):
|
58 |
+
"""Convert SymPy's Integer to ``dtype``. """
|
59 |
+
if a.is_Rational:
|
60 |
+
return MPQ(a.p, a.q)
|
61 |
+
elif a.is_Float:
|
62 |
+
from sympy.polys.domains import RR
|
63 |
+
return MPQ(*map(int, RR.to_rational(a)))
|
64 |
+
else:
|
65 |
+
raise CoercionFailed("expected `Rational` object, got %s" % a)
|
66 |
+
|
67 |
+
def algebraic_field(self, *extension, alias=None):
|
68 |
+
r"""Returns an algebraic field, i.e. `\mathbb{Q}(\alpha, \ldots)`.
|
69 |
+
|
70 |
+
Parameters
|
71 |
+
==========
|
72 |
+
|
73 |
+
*extension : One or more :py:class:`~.Expr`
|
74 |
+
Generators of the extension. These should be expressions that are
|
75 |
+
algebraic over `\mathbb{Q}`.
|
76 |
+
|
77 |
+
alias : str, :py:class:`~.Symbol`, None, optional (default=None)
|
78 |
+
If provided, this will be used as the alias symbol for the
|
79 |
+
primitive element of the returned :py:class:`~.AlgebraicField`.
|
80 |
+
|
81 |
+
Returns
|
82 |
+
=======
|
83 |
+
|
84 |
+
:py:class:`~.AlgebraicField`
|
85 |
+
A :py:class:`~.Domain` representing the algebraic field extension.
|
86 |
+
|
87 |
+
Examples
|
88 |
+
========
|
89 |
+
|
90 |
+
>>> from sympy import QQ, sqrt
|
91 |
+
>>> QQ.algebraic_field(sqrt(2))
|
92 |
+
QQ<sqrt(2)>
|
93 |
+
"""
|
94 |
+
from sympy.polys.domains import AlgebraicField
|
95 |
+
return AlgebraicField(self, *extension, alias=alias)
|
96 |
+
|
97 |
+
def from_AlgebraicField(K1, a, K0):
|
98 |
+
"""Convert a :py:class:`~.ANP` object to :ref:`QQ`.
|
99 |
+
|
100 |
+
See :py:meth:`~.Domain.convert`
|
101 |
+
"""
|
102 |
+
if a.is_ground:
|
103 |
+
return K1.convert(a.LC(), K0.dom)
|
104 |
+
|
105 |
+
def from_ZZ(K1, a, K0):
|
106 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
107 |
+
return MPQ(a)
|
108 |
+
|
109 |
+
def from_ZZ_python(K1, a, K0):
|
110 |
+
"""Convert a Python ``int`` object to ``dtype``. """
|
111 |
+
return MPQ(a)
|
112 |
+
|
113 |
+
def from_QQ(K1, a, K0):
|
114 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
115 |
+
return MPQ(a.numerator, a.denominator)
|
116 |
+
|
117 |
+
def from_QQ_python(K1, a, K0):
|
118 |
+
"""Convert a Python ``Fraction`` object to ``dtype``. """
|
119 |
+
return MPQ(a.numerator, a.denominator)
|
120 |
+
|
121 |
+
def from_ZZ_gmpy(K1, a, K0):
|
122 |
+
"""Convert a GMPY ``mpz`` object to ``dtype``. """
|
123 |
+
return MPQ(a)
|
124 |
+
|
125 |
+
def from_QQ_gmpy(K1, a, K0):
|
126 |
+
"""Convert a GMPY ``mpq`` object to ``dtype``. """
|
127 |
+
return a
|
128 |
+
|
129 |
+
def from_GaussianRationalField(K1, a, K0):
|
130 |
+
"""Convert a ``GaussianElement`` object to ``dtype``. """
|
131 |
+
if a.y == 0:
|
132 |
+
return MPQ(a.x)
|
133 |
+
|
134 |
+
def from_RealField(K1, a, K0):
|
135 |
+
"""Convert a mpmath ``mpf`` object to ``dtype``. """
|
136 |
+
return MPQ(*map(int, K0.to_rational(a)))
|
137 |
+
|
138 |
+
def exquo(self, a, b):
|
139 |
+
"""Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
140 |
+
return MPQ(a) / MPQ(b)
|
141 |
+
|
142 |
+
def quo(self, a, b):
|
143 |
+
"""Quotient of ``a`` and ``b``, implies ``__truediv__``. """
|
144 |
+
return MPQ(a) / MPQ(b)
|
145 |
+
|
146 |
+
def rem(self, a, b):
|
147 |
+
"""Remainder of ``a`` and ``b``, implies nothing. """
|
148 |
+
return self.zero
|
149 |
+
|
150 |
+
def div(self, a, b):
|
151 |
+
"""Division of ``a`` and ``b``, implies ``__truediv__``. """
|
152 |
+
return MPQ(a) / MPQ(b), self.zero
|
153 |
+
|
154 |
+
def numer(self, a):
|
155 |
+
"""Returns numerator of ``a``. """
|
156 |
+
return a.numerator
|
157 |
+
|
158 |
+
def denom(self, a):
|
159 |
+
"""Returns denominator of ``a``. """
|
160 |
+
return a.denominator
|
161 |
+
|
162 |
+
|
163 |
+
QQ = RationalField()
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/simpledomain.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Implementation of :class:`SimpleDomain` class. """
|
2 |
+
|
3 |
+
|
4 |
+
from sympy.polys.domains.domain import Domain
|
5 |
+
from sympy.utilities import public
|
6 |
+
|
7 |
+
@public
|
8 |
+
class SimpleDomain(Domain):
|
9 |
+
"""Base class for simple domains, e.g. ZZ, QQ. """
|
10 |
+
|
11 |
+
is_Simple = True
|
12 |
+
|
13 |
+
def inject(self, *gens):
|
14 |
+
"""Inject generators into this domain. """
|
15 |
+
return self.poly_ring(*gens)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/tests/test_domains.py
ADDED
@@ -0,0 +1,1270 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Tests for classes defining properties of ground domains, e.g. ZZ, QQ, ZZ[x] ... """
|
2 |
+
|
3 |
+
from sympy.core.numbers import (AlgebraicNumber, E, Float, I, Integer,
|
4 |
+
Rational, oo, pi, _illegal)
|
5 |
+
from sympy.core.singleton import S
|
6 |
+
from sympy.functions.elementary.exponential import exp
|
7 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
8 |
+
from sympy.functions.elementary.trigonometric import sin
|
9 |
+
from sympy.polys.polytools import Poly
|
10 |
+
from sympy.abc import x, y, z
|
11 |
+
|
12 |
+
from sympy.external.gmpy import HAS_GMPY
|
13 |
+
|
14 |
+
from sympy.polys.domains import (ZZ, QQ, RR, CC, FF, GF, EX, EXRAW, ZZ_gmpy,
|
15 |
+
ZZ_python, QQ_gmpy, QQ_python)
|
16 |
+
from sympy.polys.domains.algebraicfield import AlgebraicField
|
17 |
+
from sympy.polys.domains.gaussiandomains import ZZ_I, QQ_I
|
18 |
+
from sympy.polys.domains.polynomialring import PolynomialRing
|
19 |
+
from sympy.polys.domains.realfield import RealField
|
20 |
+
|
21 |
+
from sympy.polys.numberfields.subfield import field_isomorphism
|
22 |
+
from sympy.polys.rings import ring
|
23 |
+
from sympy.polys.specialpolys import cyclotomic_poly
|
24 |
+
from sympy.polys.fields import field
|
25 |
+
|
26 |
+
from sympy.polys.agca.extensions import FiniteExtension
|
27 |
+
|
28 |
+
from sympy.polys.polyerrors import (
|
29 |
+
UnificationFailed,
|
30 |
+
GeneratorsError,
|
31 |
+
CoercionFailed,
|
32 |
+
NotInvertible,
|
33 |
+
DomainError)
|
34 |
+
|
35 |
+
from sympy.testing.pytest import raises
|
36 |
+
|
37 |
+
from itertools import product
|
38 |
+
|
39 |
+
ALG = QQ.algebraic_field(sqrt(2), sqrt(3))
|
40 |
+
|
41 |
+
def unify(K0, K1):
|
42 |
+
return K0.unify(K1)
|
43 |
+
|
44 |
+
def test_Domain_unify():
|
45 |
+
F3 = GF(3)
|
46 |
+
|
47 |
+
assert unify(F3, F3) == F3
|
48 |
+
assert unify(F3, ZZ) == ZZ
|
49 |
+
assert unify(F3, QQ) == QQ
|
50 |
+
assert unify(F3, ALG) == ALG
|
51 |
+
assert unify(F3, RR) == RR
|
52 |
+
assert unify(F3, CC) == CC
|
53 |
+
assert unify(F3, ZZ[x]) == ZZ[x]
|
54 |
+
assert unify(F3, ZZ.frac_field(x)) == ZZ.frac_field(x)
|
55 |
+
assert unify(F3, EX) == EX
|
56 |
+
|
57 |
+
assert unify(ZZ, F3) == ZZ
|
58 |
+
assert unify(ZZ, ZZ) == ZZ
|
59 |
+
assert unify(ZZ, QQ) == QQ
|
60 |
+
assert unify(ZZ, ALG) == ALG
|
61 |
+
assert unify(ZZ, RR) == RR
|
62 |
+
assert unify(ZZ, CC) == CC
|
63 |
+
assert unify(ZZ, ZZ[x]) == ZZ[x]
|
64 |
+
assert unify(ZZ, ZZ.frac_field(x)) == ZZ.frac_field(x)
|
65 |
+
assert unify(ZZ, EX) == EX
|
66 |
+
|
67 |
+
assert unify(QQ, F3) == QQ
|
68 |
+
assert unify(QQ, ZZ) == QQ
|
69 |
+
assert unify(QQ, QQ) == QQ
|
70 |
+
assert unify(QQ, ALG) == ALG
|
71 |
+
assert unify(QQ, RR) == RR
|
72 |
+
assert unify(QQ, CC) == CC
|
73 |
+
assert unify(QQ, ZZ[x]) == QQ[x]
|
74 |
+
assert unify(QQ, ZZ.frac_field(x)) == QQ.frac_field(x)
|
75 |
+
assert unify(QQ, EX) == EX
|
76 |
+
|
77 |
+
assert unify(ZZ_I, F3) == ZZ_I
|
78 |
+
assert unify(ZZ_I, ZZ) == ZZ_I
|
79 |
+
assert unify(ZZ_I, ZZ_I) == ZZ_I
|
80 |
+
assert unify(ZZ_I, QQ) == QQ_I
|
81 |
+
assert unify(ZZ_I, ALG) == QQ.algebraic_field(I, sqrt(2), sqrt(3))
|
82 |
+
assert unify(ZZ_I, RR) == CC
|
83 |
+
assert unify(ZZ_I, CC) == CC
|
84 |
+
assert unify(ZZ_I, ZZ[x]) == ZZ_I[x]
|
85 |
+
assert unify(ZZ_I, ZZ_I[x]) == ZZ_I[x]
|
86 |
+
assert unify(ZZ_I, ZZ.frac_field(x)) == ZZ_I.frac_field(x)
|
87 |
+
assert unify(ZZ_I, ZZ_I.frac_field(x)) == ZZ_I.frac_field(x)
|
88 |
+
assert unify(ZZ_I, EX) == EX
|
89 |
+
|
90 |
+
assert unify(QQ_I, F3) == QQ_I
|
91 |
+
assert unify(QQ_I, ZZ) == QQ_I
|
92 |
+
assert unify(QQ_I, ZZ_I) == QQ_I
|
93 |
+
assert unify(QQ_I, QQ) == QQ_I
|
94 |
+
assert unify(QQ_I, ALG) == QQ.algebraic_field(I, sqrt(2), sqrt(3))
|
95 |
+
assert unify(QQ_I, RR) == CC
|
96 |
+
assert unify(QQ_I, CC) == CC
|
97 |
+
assert unify(QQ_I, ZZ[x]) == QQ_I[x]
|
98 |
+
assert unify(QQ_I, ZZ_I[x]) == QQ_I[x]
|
99 |
+
assert unify(QQ_I, QQ[x]) == QQ_I[x]
|
100 |
+
assert unify(QQ_I, QQ_I[x]) == QQ_I[x]
|
101 |
+
assert unify(QQ_I, ZZ.frac_field(x)) == QQ_I.frac_field(x)
|
102 |
+
assert unify(QQ_I, ZZ_I.frac_field(x)) == QQ_I.frac_field(x)
|
103 |
+
assert unify(QQ_I, QQ.frac_field(x)) == QQ_I.frac_field(x)
|
104 |
+
assert unify(QQ_I, QQ_I.frac_field(x)) == QQ_I.frac_field(x)
|
105 |
+
assert unify(QQ_I, EX) == EX
|
106 |
+
|
107 |
+
assert unify(RR, F3) == RR
|
108 |
+
assert unify(RR, ZZ) == RR
|
109 |
+
assert unify(RR, QQ) == RR
|
110 |
+
assert unify(RR, ALG) == RR
|
111 |
+
assert unify(RR, RR) == RR
|
112 |
+
assert unify(RR, CC) == CC
|
113 |
+
assert unify(RR, ZZ[x]) == RR[x]
|
114 |
+
assert unify(RR, ZZ.frac_field(x)) == RR.frac_field(x)
|
115 |
+
assert unify(RR, EX) == EX
|
116 |
+
assert RR[x].unify(ZZ.frac_field(y)) == RR.frac_field(x, y)
|
117 |
+
|
118 |
+
assert unify(CC, F3) == CC
|
119 |
+
assert unify(CC, ZZ) == CC
|
120 |
+
assert unify(CC, QQ) == CC
|
121 |
+
assert unify(CC, ALG) == CC
|
122 |
+
assert unify(CC, RR) == CC
|
123 |
+
assert unify(CC, CC) == CC
|
124 |
+
assert unify(CC, ZZ[x]) == CC[x]
|
125 |
+
assert unify(CC, ZZ.frac_field(x)) == CC.frac_field(x)
|
126 |
+
assert unify(CC, EX) == EX
|
127 |
+
|
128 |
+
assert unify(ZZ[x], F3) == ZZ[x]
|
129 |
+
assert unify(ZZ[x], ZZ) == ZZ[x]
|
130 |
+
assert unify(ZZ[x], QQ) == QQ[x]
|
131 |
+
assert unify(ZZ[x], ALG) == ALG[x]
|
132 |
+
assert unify(ZZ[x], RR) == RR[x]
|
133 |
+
assert unify(ZZ[x], CC) == CC[x]
|
134 |
+
assert unify(ZZ[x], ZZ[x]) == ZZ[x]
|
135 |
+
assert unify(ZZ[x], ZZ.frac_field(x)) == ZZ.frac_field(x)
|
136 |
+
assert unify(ZZ[x], EX) == EX
|
137 |
+
|
138 |
+
assert unify(ZZ.frac_field(x), F3) == ZZ.frac_field(x)
|
139 |
+
assert unify(ZZ.frac_field(x), ZZ) == ZZ.frac_field(x)
|
140 |
+
assert unify(ZZ.frac_field(x), QQ) == QQ.frac_field(x)
|
141 |
+
assert unify(ZZ.frac_field(x), ALG) == ALG.frac_field(x)
|
142 |
+
assert unify(ZZ.frac_field(x), RR) == RR.frac_field(x)
|
143 |
+
assert unify(ZZ.frac_field(x), CC) == CC.frac_field(x)
|
144 |
+
assert unify(ZZ.frac_field(x), ZZ[x]) == ZZ.frac_field(x)
|
145 |
+
assert unify(ZZ.frac_field(x), ZZ.frac_field(x)) == ZZ.frac_field(x)
|
146 |
+
assert unify(ZZ.frac_field(x), EX) == EX
|
147 |
+
|
148 |
+
assert unify(EX, F3) == EX
|
149 |
+
assert unify(EX, ZZ) == EX
|
150 |
+
assert unify(EX, QQ) == EX
|
151 |
+
assert unify(EX, ALG) == EX
|
152 |
+
assert unify(EX, RR) == EX
|
153 |
+
assert unify(EX, CC) == EX
|
154 |
+
assert unify(EX, ZZ[x]) == EX
|
155 |
+
assert unify(EX, ZZ.frac_field(x)) == EX
|
156 |
+
assert unify(EX, EX) == EX
|
157 |
+
|
158 |
+
def test_Domain_unify_composite():
|
159 |
+
assert unify(ZZ.poly_ring(x), ZZ) == ZZ.poly_ring(x)
|
160 |
+
assert unify(ZZ.poly_ring(x), QQ) == QQ.poly_ring(x)
|
161 |
+
assert unify(QQ.poly_ring(x), ZZ) == QQ.poly_ring(x)
|
162 |
+
assert unify(QQ.poly_ring(x), QQ) == QQ.poly_ring(x)
|
163 |
+
|
164 |
+
assert unify(ZZ, ZZ.poly_ring(x)) == ZZ.poly_ring(x)
|
165 |
+
assert unify(QQ, ZZ.poly_ring(x)) == QQ.poly_ring(x)
|
166 |
+
assert unify(ZZ, QQ.poly_ring(x)) == QQ.poly_ring(x)
|
167 |
+
assert unify(QQ, QQ.poly_ring(x)) == QQ.poly_ring(x)
|
168 |
+
|
169 |
+
assert unify(ZZ.poly_ring(x, y), ZZ) == ZZ.poly_ring(x, y)
|
170 |
+
assert unify(ZZ.poly_ring(x, y), QQ) == QQ.poly_ring(x, y)
|
171 |
+
assert unify(QQ.poly_ring(x, y), ZZ) == QQ.poly_ring(x, y)
|
172 |
+
assert unify(QQ.poly_ring(x, y), QQ) == QQ.poly_ring(x, y)
|
173 |
+
|
174 |
+
assert unify(ZZ, ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y)
|
175 |
+
assert unify(QQ, ZZ.poly_ring(x, y)) == QQ.poly_ring(x, y)
|
176 |
+
assert unify(ZZ, QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)
|
177 |
+
assert unify(QQ, QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)
|
178 |
+
|
179 |
+
assert unify(ZZ.frac_field(x), ZZ) == ZZ.frac_field(x)
|
180 |
+
assert unify(ZZ.frac_field(x), QQ) == QQ.frac_field(x)
|
181 |
+
assert unify(QQ.frac_field(x), ZZ) == QQ.frac_field(x)
|
182 |
+
assert unify(QQ.frac_field(x), QQ) == QQ.frac_field(x)
|
183 |
+
|
184 |
+
assert unify(ZZ, ZZ.frac_field(x)) == ZZ.frac_field(x)
|
185 |
+
assert unify(QQ, ZZ.frac_field(x)) == QQ.frac_field(x)
|
186 |
+
assert unify(ZZ, QQ.frac_field(x)) == QQ.frac_field(x)
|
187 |
+
assert unify(QQ, QQ.frac_field(x)) == QQ.frac_field(x)
|
188 |
+
|
189 |
+
assert unify(ZZ.frac_field(x, y), ZZ) == ZZ.frac_field(x, y)
|
190 |
+
assert unify(ZZ.frac_field(x, y), QQ) == QQ.frac_field(x, y)
|
191 |
+
assert unify(QQ.frac_field(x, y), ZZ) == QQ.frac_field(x, y)
|
192 |
+
assert unify(QQ.frac_field(x, y), QQ) == QQ.frac_field(x, y)
|
193 |
+
|
194 |
+
assert unify(ZZ, ZZ.frac_field(x, y)) == ZZ.frac_field(x, y)
|
195 |
+
assert unify(QQ, ZZ.frac_field(x, y)) == QQ.frac_field(x, y)
|
196 |
+
assert unify(ZZ, QQ.frac_field(x, y)) == QQ.frac_field(x, y)
|
197 |
+
assert unify(QQ, QQ.frac_field(x, y)) == QQ.frac_field(x, y)
|
198 |
+
|
199 |
+
assert unify(ZZ.poly_ring(x), ZZ.poly_ring(x)) == ZZ.poly_ring(x)
|
200 |
+
assert unify(ZZ.poly_ring(x), QQ.poly_ring(x)) == QQ.poly_ring(x)
|
201 |
+
assert unify(QQ.poly_ring(x), ZZ.poly_ring(x)) == QQ.poly_ring(x)
|
202 |
+
assert unify(QQ.poly_ring(x), QQ.poly_ring(x)) == QQ.poly_ring(x)
|
203 |
+
|
204 |
+
assert unify(ZZ.poly_ring(x, y), ZZ.poly_ring(x)) == ZZ.poly_ring(x, y)
|
205 |
+
assert unify(ZZ.poly_ring(x, y), QQ.poly_ring(x)) == QQ.poly_ring(x, y)
|
206 |
+
assert unify(QQ.poly_ring(x, y), ZZ.poly_ring(x)) == QQ.poly_ring(x, y)
|
207 |
+
assert unify(QQ.poly_ring(x, y), QQ.poly_ring(x)) == QQ.poly_ring(x, y)
|
208 |
+
|
209 |
+
assert unify(ZZ.poly_ring(x), ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y)
|
210 |
+
assert unify(ZZ.poly_ring(x), QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)
|
211 |
+
assert unify(QQ.poly_ring(x), ZZ.poly_ring(x, y)) == QQ.poly_ring(x, y)
|
212 |
+
assert unify(QQ.poly_ring(x), QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)
|
213 |
+
|
214 |
+
assert unify(ZZ.poly_ring(x, y), ZZ.poly_ring(x, z)) == ZZ.poly_ring(x, y, z)
|
215 |
+
assert unify(ZZ.poly_ring(x, y), QQ.poly_ring(x, z)) == QQ.poly_ring(x, y, z)
|
216 |
+
assert unify(QQ.poly_ring(x, y), ZZ.poly_ring(x, z)) == QQ.poly_ring(x, y, z)
|
217 |
+
assert unify(QQ.poly_ring(x, y), QQ.poly_ring(x, z)) == QQ.poly_ring(x, y, z)
|
218 |
+
|
219 |
+
assert unify(ZZ.frac_field(x), ZZ.frac_field(x)) == ZZ.frac_field(x)
|
220 |
+
assert unify(ZZ.frac_field(x), QQ.frac_field(x)) == QQ.frac_field(x)
|
221 |
+
assert unify(QQ.frac_field(x), ZZ.frac_field(x)) == QQ.frac_field(x)
|
222 |
+
assert unify(QQ.frac_field(x), QQ.frac_field(x)) == QQ.frac_field(x)
|
223 |
+
|
224 |
+
assert unify(ZZ.frac_field(x, y), ZZ.frac_field(x)) == ZZ.frac_field(x, y)
|
225 |
+
assert unify(ZZ.frac_field(x, y), QQ.frac_field(x)) == QQ.frac_field(x, y)
|
226 |
+
assert unify(QQ.frac_field(x, y), ZZ.frac_field(x)) == QQ.frac_field(x, y)
|
227 |
+
assert unify(QQ.frac_field(x, y), QQ.frac_field(x)) == QQ.frac_field(x, y)
|
228 |
+
|
229 |
+
assert unify(ZZ.frac_field(x), ZZ.frac_field(x, y)) == ZZ.frac_field(x, y)
|
230 |
+
assert unify(ZZ.frac_field(x), QQ.frac_field(x, y)) == QQ.frac_field(x, y)
|
231 |
+
assert unify(QQ.frac_field(x), ZZ.frac_field(x, y)) == QQ.frac_field(x, y)
|
232 |
+
assert unify(QQ.frac_field(x), QQ.frac_field(x, y)) == QQ.frac_field(x, y)
|
233 |
+
|
234 |
+
assert unify(ZZ.frac_field(x, y), ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z)
|
235 |
+
assert unify(ZZ.frac_field(x, y), QQ.frac_field(x, z)) == QQ.frac_field(x, y, z)
|
236 |
+
assert unify(QQ.frac_field(x, y), ZZ.frac_field(x, z)) == QQ.frac_field(x, y, z)
|
237 |
+
assert unify(QQ.frac_field(x, y), QQ.frac_field(x, z)) == QQ.frac_field(x, y, z)
|
238 |
+
|
239 |
+
assert unify(ZZ.poly_ring(x), ZZ.frac_field(x)) == ZZ.frac_field(x)
|
240 |
+
assert unify(ZZ.poly_ring(x), QQ.frac_field(x)) == ZZ.frac_field(x)
|
241 |
+
assert unify(QQ.poly_ring(x), ZZ.frac_field(x)) == ZZ.frac_field(x)
|
242 |
+
assert unify(QQ.poly_ring(x), QQ.frac_field(x)) == QQ.frac_field(x)
|
243 |
+
|
244 |
+
assert unify(ZZ.poly_ring(x, y), ZZ.frac_field(x)) == ZZ.frac_field(x, y)
|
245 |
+
assert unify(ZZ.poly_ring(x, y), QQ.frac_field(x)) == ZZ.frac_field(x, y)
|
246 |
+
assert unify(QQ.poly_ring(x, y), ZZ.frac_field(x)) == ZZ.frac_field(x, y)
|
247 |
+
assert unify(QQ.poly_ring(x, y), QQ.frac_field(x)) == QQ.frac_field(x, y)
|
248 |
+
|
249 |
+
assert unify(ZZ.poly_ring(x), ZZ.frac_field(x, y)) == ZZ.frac_field(x, y)
|
250 |
+
assert unify(ZZ.poly_ring(x), QQ.frac_field(x, y)) == ZZ.frac_field(x, y)
|
251 |
+
assert unify(QQ.poly_ring(x), ZZ.frac_field(x, y)) == ZZ.frac_field(x, y)
|
252 |
+
assert unify(QQ.poly_ring(x), QQ.frac_field(x, y)) == QQ.frac_field(x, y)
|
253 |
+
|
254 |
+
assert unify(ZZ.poly_ring(x, y), ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z)
|
255 |
+
assert unify(ZZ.poly_ring(x, y), QQ.frac_field(x, z)) == ZZ.frac_field(x, y, z)
|
256 |
+
assert unify(QQ.poly_ring(x, y), ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z)
|
257 |
+
assert unify(QQ.poly_ring(x, y), QQ.frac_field(x, z)) == QQ.frac_field(x, y, z)
|
258 |
+
|
259 |
+
assert unify(ZZ.frac_field(x), ZZ.poly_ring(x)) == ZZ.frac_field(x)
|
260 |
+
assert unify(ZZ.frac_field(x), QQ.poly_ring(x)) == ZZ.frac_field(x)
|
261 |
+
assert unify(QQ.frac_field(x), ZZ.poly_ring(x)) == ZZ.frac_field(x)
|
262 |
+
assert unify(QQ.frac_field(x), QQ.poly_ring(x)) == QQ.frac_field(x)
|
263 |
+
|
264 |
+
assert unify(ZZ.frac_field(x, y), ZZ.poly_ring(x)) == ZZ.frac_field(x, y)
|
265 |
+
assert unify(ZZ.frac_field(x, y), QQ.poly_ring(x)) == ZZ.frac_field(x, y)
|
266 |
+
assert unify(QQ.frac_field(x, y), ZZ.poly_ring(x)) == ZZ.frac_field(x, y)
|
267 |
+
assert unify(QQ.frac_field(x, y), QQ.poly_ring(x)) == QQ.frac_field(x, y)
|
268 |
+
|
269 |
+
assert unify(ZZ.frac_field(x), ZZ.poly_ring(x, y)) == ZZ.frac_field(x, y)
|
270 |
+
assert unify(ZZ.frac_field(x), QQ.poly_ring(x, y)) == ZZ.frac_field(x, y)
|
271 |
+
assert unify(QQ.frac_field(x), ZZ.poly_ring(x, y)) == ZZ.frac_field(x, y)
|
272 |
+
assert unify(QQ.frac_field(x), QQ.poly_ring(x, y)) == QQ.frac_field(x, y)
|
273 |
+
|
274 |
+
assert unify(ZZ.frac_field(x, y), ZZ.poly_ring(x, z)) == ZZ.frac_field(x, y, z)
|
275 |
+
assert unify(ZZ.frac_field(x, y), QQ.poly_ring(x, z)) == ZZ.frac_field(x, y, z)
|
276 |
+
assert unify(QQ.frac_field(x, y), ZZ.poly_ring(x, z)) == ZZ.frac_field(x, y, z)
|
277 |
+
assert unify(QQ.frac_field(x, y), QQ.poly_ring(x, z)) == QQ.frac_field(x, y, z)
|
278 |
+
|
279 |
+
def test_Domain_unify_algebraic():
|
280 |
+
sqrt5 = QQ.algebraic_field(sqrt(5))
|
281 |
+
sqrt7 = QQ.algebraic_field(sqrt(7))
|
282 |
+
sqrt57 = QQ.algebraic_field(sqrt(5), sqrt(7))
|
283 |
+
|
284 |
+
assert sqrt5.unify(sqrt7) == sqrt57
|
285 |
+
|
286 |
+
assert sqrt5.unify(sqrt5[x, y]) == sqrt5[x, y]
|
287 |
+
assert sqrt5[x, y].unify(sqrt5) == sqrt5[x, y]
|
288 |
+
|
289 |
+
assert sqrt5.unify(sqrt5.frac_field(x, y)) == sqrt5.frac_field(x, y)
|
290 |
+
assert sqrt5.frac_field(x, y).unify(sqrt5) == sqrt5.frac_field(x, y)
|
291 |
+
|
292 |
+
assert sqrt5.unify(sqrt7[x, y]) == sqrt57[x, y]
|
293 |
+
assert sqrt5[x, y].unify(sqrt7) == sqrt57[x, y]
|
294 |
+
|
295 |
+
assert sqrt5.unify(sqrt7.frac_field(x, y)) == sqrt57.frac_field(x, y)
|
296 |
+
assert sqrt5.frac_field(x, y).unify(sqrt7) == sqrt57.frac_field(x, y)
|
297 |
+
|
298 |
+
def test_Domain_unify_FiniteExtension():
|
299 |
+
KxZZ = FiniteExtension(Poly(x**2 - 2, x, domain=ZZ))
|
300 |
+
KxQQ = FiniteExtension(Poly(x**2 - 2, x, domain=QQ))
|
301 |
+
KxZZy = FiniteExtension(Poly(x**2 - 2, x, domain=ZZ[y]))
|
302 |
+
KxQQy = FiniteExtension(Poly(x**2 - 2, x, domain=QQ[y]))
|
303 |
+
|
304 |
+
assert KxZZ.unify(KxZZ) == KxZZ
|
305 |
+
assert KxQQ.unify(KxQQ) == KxQQ
|
306 |
+
assert KxZZy.unify(KxZZy) == KxZZy
|
307 |
+
assert KxQQy.unify(KxQQy) == KxQQy
|
308 |
+
|
309 |
+
assert KxZZ.unify(ZZ) == KxZZ
|
310 |
+
assert KxZZ.unify(QQ) == KxQQ
|
311 |
+
assert KxQQ.unify(ZZ) == KxQQ
|
312 |
+
assert KxQQ.unify(QQ) == KxQQ
|
313 |
+
|
314 |
+
assert KxZZ.unify(ZZ[y]) == KxZZy
|
315 |
+
assert KxZZ.unify(QQ[y]) == KxQQy
|
316 |
+
assert KxQQ.unify(ZZ[y]) == KxQQy
|
317 |
+
assert KxQQ.unify(QQ[y]) == KxQQy
|
318 |
+
|
319 |
+
assert KxZZy.unify(ZZ) == KxZZy
|
320 |
+
assert KxZZy.unify(QQ) == KxQQy
|
321 |
+
assert KxQQy.unify(ZZ) == KxQQy
|
322 |
+
assert KxQQy.unify(QQ) == KxQQy
|
323 |
+
|
324 |
+
assert KxZZy.unify(ZZ[y]) == KxZZy
|
325 |
+
assert KxZZy.unify(QQ[y]) == KxQQy
|
326 |
+
assert KxQQy.unify(ZZ[y]) == KxQQy
|
327 |
+
assert KxQQy.unify(QQ[y]) == KxQQy
|
328 |
+
|
329 |
+
K = FiniteExtension(Poly(x**2 - 2, x, domain=ZZ[y]))
|
330 |
+
assert K.unify(ZZ) == K
|
331 |
+
assert K.unify(ZZ[x]) == K
|
332 |
+
assert K.unify(ZZ[y]) == K
|
333 |
+
assert K.unify(ZZ[x, y]) == K
|
334 |
+
|
335 |
+
Kz = FiniteExtension(Poly(x**2 - 2, x, domain=ZZ[y, z]))
|
336 |
+
assert K.unify(ZZ[z]) == Kz
|
337 |
+
assert K.unify(ZZ[x, z]) == Kz
|
338 |
+
assert K.unify(ZZ[y, z]) == Kz
|
339 |
+
assert K.unify(ZZ[x, y, z]) == Kz
|
340 |
+
|
341 |
+
Kx = FiniteExtension(Poly(x**2 - 2, x, domain=ZZ))
|
342 |
+
Ky = FiniteExtension(Poly(y**2 - 2, y, domain=ZZ))
|
343 |
+
Kxy = FiniteExtension(Poly(y**2 - 2, y, domain=Kx))
|
344 |
+
assert Kx.unify(Kx) == Kx
|
345 |
+
assert Ky.unify(Ky) == Ky
|
346 |
+
assert Kx.unify(Ky) == Kxy
|
347 |
+
assert Ky.unify(Kx) == Kxy
|
348 |
+
|
349 |
+
def test_Domain_unify_with_symbols():
|
350 |
+
raises(UnificationFailed, lambda: ZZ[x, y].unify_with_symbols(ZZ, (y, z)))
|
351 |
+
raises(UnificationFailed, lambda: ZZ.unify_with_symbols(ZZ[x, y], (y, z)))
|
352 |
+
|
353 |
+
def test_Domain__contains__():
|
354 |
+
assert (0 in EX) is True
|
355 |
+
assert (0 in ZZ) is True
|
356 |
+
assert (0 in QQ) is True
|
357 |
+
assert (0 in RR) is True
|
358 |
+
assert (0 in CC) is True
|
359 |
+
assert (0 in ALG) is True
|
360 |
+
assert (0 in ZZ[x, y]) is True
|
361 |
+
assert (0 in QQ[x, y]) is True
|
362 |
+
assert (0 in RR[x, y]) is True
|
363 |
+
|
364 |
+
assert (-7 in EX) is True
|
365 |
+
assert (-7 in ZZ) is True
|
366 |
+
assert (-7 in QQ) is True
|
367 |
+
assert (-7 in RR) is True
|
368 |
+
assert (-7 in CC) is True
|
369 |
+
assert (-7 in ALG) is True
|
370 |
+
assert (-7 in ZZ[x, y]) is True
|
371 |
+
assert (-7 in QQ[x, y]) is True
|
372 |
+
assert (-7 in RR[x, y]) is True
|
373 |
+
|
374 |
+
assert (17 in EX) is True
|
375 |
+
assert (17 in ZZ) is True
|
376 |
+
assert (17 in QQ) is True
|
377 |
+
assert (17 in RR) is True
|
378 |
+
assert (17 in CC) is True
|
379 |
+
assert (17 in ALG) is True
|
380 |
+
assert (17 in ZZ[x, y]) is True
|
381 |
+
assert (17 in QQ[x, y]) is True
|
382 |
+
assert (17 in RR[x, y]) is True
|
383 |
+
|
384 |
+
assert (Rational(-1, 7) in EX) is True
|
385 |
+
assert (Rational(-1, 7) in ZZ) is False
|
386 |
+
assert (Rational(-1, 7) in QQ) is True
|
387 |
+
assert (Rational(-1, 7) in RR) is True
|
388 |
+
assert (Rational(-1, 7) in CC) is True
|
389 |
+
assert (Rational(-1, 7) in ALG) is True
|
390 |
+
assert (Rational(-1, 7) in ZZ[x, y]) is False
|
391 |
+
assert (Rational(-1, 7) in QQ[x, y]) is True
|
392 |
+
assert (Rational(-1, 7) in RR[x, y]) is True
|
393 |
+
|
394 |
+
assert (Rational(3, 5) in EX) is True
|
395 |
+
assert (Rational(3, 5) in ZZ) is False
|
396 |
+
assert (Rational(3, 5) in QQ) is True
|
397 |
+
assert (Rational(3, 5) in RR) is True
|
398 |
+
assert (Rational(3, 5) in CC) is True
|
399 |
+
assert (Rational(3, 5) in ALG) is True
|
400 |
+
assert (Rational(3, 5) in ZZ[x, y]) is False
|
401 |
+
assert (Rational(3, 5) in QQ[x, y]) is True
|
402 |
+
assert (Rational(3, 5) in RR[x, y]) is True
|
403 |
+
|
404 |
+
assert (3.0 in EX) is True
|
405 |
+
assert (3.0 in ZZ) is True
|
406 |
+
assert (3.0 in QQ) is True
|
407 |
+
assert (3.0 in RR) is True
|
408 |
+
assert (3.0 in CC) is True
|
409 |
+
assert (3.0 in ALG) is True
|
410 |
+
assert (3.0 in ZZ[x, y]) is True
|
411 |
+
assert (3.0 in QQ[x, y]) is True
|
412 |
+
assert (3.0 in RR[x, y]) is True
|
413 |
+
|
414 |
+
assert (3.14 in EX) is True
|
415 |
+
assert (3.14 in ZZ) is False
|
416 |
+
assert (3.14 in QQ) is True
|
417 |
+
assert (3.14 in RR) is True
|
418 |
+
assert (3.14 in CC) is True
|
419 |
+
assert (3.14 in ALG) is True
|
420 |
+
assert (3.14 in ZZ[x, y]) is False
|
421 |
+
assert (3.14 in QQ[x, y]) is True
|
422 |
+
assert (3.14 in RR[x, y]) is True
|
423 |
+
|
424 |
+
assert (oo in ALG) is False
|
425 |
+
assert (oo in ZZ[x, y]) is False
|
426 |
+
assert (oo in QQ[x, y]) is False
|
427 |
+
|
428 |
+
assert (-oo in ZZ) is False
|
429 |
+
assert (-oo in QQ) is False
|
430 |
+
assert (-oo in ALG) is False
|
431 |
+
assert (-oo in ZZ[x, y]) is False
|
432 |
+
assert (-oo in QQ[x, y]) is False
|
433 |
+
|
434 |
+
assert (sqrt(7) in EX) is True
|
435 |
+
assert (sqrt(7) in ZZ) is False
|
436 |
+
assert (sqrt(7) in QQ) is False
|
437 |
+
assert (sqrt(7) in RR) is True
|
438 |
+
assert (sqrt(7) in CC) is True
|
439 |
+
assert (sqrt(7) in ALG) is False
|
440 |
+
assert (sqrt(7) in ZZ[x, y]) is False
|
441 |
+
assert (sqrt(7) in QQ[x, y]) is False
|
442 |
+
assert (sqrt(7) in RR[x, y]) is True
|
443 |
+
|
444 |
+
assert (2*sqrt(3) + 1 in EX) is True
|
445 |
+
assert (2*sqrt(3) + 1 in ZZ) is False
|
446 |
+
assert (2*sqrt(3) + 1 in QQ) is False
|
447 |
+
assert (2*sqrt(3) + 1 in RR) is True
|
448 |
+
assert (2*sqrt(3) + 1 in CC) is True
|
449 |
+
assert (2*sqrt(3) + 1 in ALG) is True
|
450 |
+
assert (2*sqrt(3) + 1 in ZZ[x, y]) is False
|
451 |
+
assert (2*sqrt(3) + 1 in QQ[x, y]) is False
|
452 |
+
assert (2*sqrt(3) + 1 in RR[x, y]) is True
|
453 |
+
|
454 |
+
assert (sin(1) in EX) is True
|
455 |
+
assert (sin(1) in ZZ) is False
|
456 |
+
assert (sin(1) in QQ) is False
|
457 |
+
assert (sin(1) in RR) is True
|
458 |
+
assert (sin(1) in CC) is True
|
459 |
+
assert (sin(1) in ALG) is False
|
460 |
+
assert (sin(1) in ZZ[x, y]) is False
|
461 |
+
assert (sin(1) in QQ[x, y]) is False
|
462 |
+
assert (sin(1) in RR[x, y]) is True
|
463 |
+
|
464 |
+
assert (x**2 + 1 in EX) is True
|
465 |
+
assert (x**2 + 1 in ZZ) is False
|
466 |
+
assert (x**2 + 1 in QQ) is False
|
467 |
+
assert (x**2 + 1 in RR) is False
|
468 |
+
assert (x**2 + 1 in CC) is False
|
469 |
+
assert (x**2 + 1 in ALG) is False
|
470 |
+
assert (x**2 + 1 in ZZ[x]) is True
|
471 |
+
assert (x**2 + 1 in QQ[x]) is True
|
472 |
+
assert (x**2 + 1 in RR[x]) is True
|
473 |
+
assert (x**2 + 1 in ZZ[x, y]) is True
|
474 |
+
assert (x**2 + 1 in QQ[x, y]) is True
|
475 |
+
assert (x**2 + 1 in RR[x, y]) is True
|
476 |
+
|
477 |
+
assert (x**2 + y**2 in EX) is True
|
478 |
+
assert (x**2 + y**2 in ZZ) is False
|
479 |
+
assert (x**2 + y**2 in QQ) is False
|
480 |
+
assert (x**2 + y**2 in RR) is False
|
481 |
+
assert (x**2 + y**2 in CC) is False
|
482 |
+
assert (x**2 + y**2 in ALG) is False
|
483 |
+
assert (x**2 + y**2 in ZZ[x]) is False
|
484 |
+
assert (x**2 + y**2 in QQ[x]) is False
|
485 |
+
assert (x**2 + y**2 in RR[x]) is False
|
486 |
+
assert (x**2 + y**2 in ZZ[x, y]) is True
|
487 |
+
assert (x**2 + y**2 in QQ[x, y]) is True
|
488 |
+
assert (x**2 + y**2 in RR[x, y]) is True
|
489 |
+
|
490 |
+
assert (Rational(3, 2)*x/(y + 1) - z in QQ[x, y, z]) is False
|
491 |
+
|
492 |
+
|
493 |
+
def test_issue_14433():
|
494 |
+
assert (Rational(2, 3)*x in QQ.frac_field(1/x)) is True
|
495 |
+
assert (1/x in QQ.frac_field(x)) is True
|
496 |
+
assert ((x**2 + y**2) in QQ.frac_field(1/x, 1/y)) is True
|
497 |
+
assert ((x + y) in QQ.frac_field(1/x, y)) is True
|
498 |
+
assert ((x - y) in QQ.frac_field(x, 1/y)) is True
|
499 |
+
|
500 |
+
|
501 |
+
def test_Domain_get_ring():
|
502 |
+
assert ZZ.has_assoc_Ring is True
|
503 |
+
assert QQ.has_assoc_Ring is True
|
504 |
+
assert ZZ[x].has_assoc_Ring is True
|
505 |
+
assert QQ[x].has_assoc_Ring is True
|
506 |
+
assert ZZ[x, y].has_assoc_Ring is True
|
507 |
+
assert QQ[x, y].has_assoc_Ring is True
|
508 |
+
assert ZZ.frac_field(x).has_assoc_Ring is True
|
509 |
+
assert QQ.frac_field(x).has_assoc_Ring is True
|
510 |
+
assert ZZ.frac_field(x, y).has_assoc_Ring is True
|
511 |
+
assert QQ.frac_field(x, y).has_assoc_Ring is True
|
512 |
+
|
513 |
+
assert EX.has_assoc_Ring is False
|
514 |
+
assert RR.has_assoc_Ring is False
|
515 |
+
assert ALG.has_assoc_Ring is False
|
516 |
+
|
517 |
+
assert ZZ.get_ring() == ZZ
|
518 |
+
assert QQ.get_ring() == ZZ
|
519 |
+
assert ZZ[x].get_ring() == ZZ[x]
|
520 |
+
assert QQ[x].get_ring() == QQ[x]
|
521 |
+
assert ZZ[x, y].get_ring() == ZZ[x, y]
|
522 |
+
assert QQ[x, y].get_ring() == QQ[x, y]
|
523 |
+
assert ZZ.frac_field(x).get_ring() == ZZ[x]
|
524 |
+
assert QQ.frac_field(x).get_ring() == QQ[x]
|
525 |
+
assert ZZ.frac_field(x, y).get_ring() == ZZ[x, y]
|
526 |
+
assert QQ.frac_field(x, y).get_ring() == QQ[x, y]
|
527 |
+
|
528 |
+
assert EX.get_ring() == EX
|
529 |
+
|
530 |
+
assert RR.get_ring() == RR
|
531 |
+
# XXX: This should also be like RR
|
532 |
+
raises(DomainError, lambda: ALG.get_ring())
|
533 |
+
|
534 |
+
|
535 |
+
def test_Domain_get_field():
|
536 |
+
assert EX.has_assoc_Field is True
|
537 |
+
assert ZZ.has_assoc_Field is True
|
538 |
+
assert QQ.has_assoc_Field is True
|
539 |
+
assert RR.has_assoc_Field is True
|
540 |
+
assert ALG.has_assoc_Field is True
|
541 |
+
assert ZZ[x].has_assoc_Field is True
|
542 |
+
assert QQ[x].has_assoc_Field is True
|
543 |
+
assert ZZ[x, y].has_assoc_Field is True
|
544 |
+
assert QQ[x, y].has_assoc_Field is True
|
545 |
+
|
546 |
+
assert EX.get_field() == EX
|
547 |
+
assert ZZ.get_field() == QQ
|
548 |
+
assert QQ.get_field() == QQ
|
549 |
+
assert RR.get_field() == RR
|
550 |
+
assert ALG.get_field() == ALG
|
551 |
+
assert ZZ[x].get_field() == ZZ.frac_field(x)
|
552 |
+
assert QQ[x].get_field() == QQ.frac_field(x)
|
553 |
+
assert ZZ[x, y].get_field() == ZZ.frac_field(x, y)
|
554 |
+
assert QQ[x, y].get_field() == QQ.frac_field(x, y)
|
555 |
+
|
556 |
+
|
557 |
+
def test_Domain_get_exact():
|
558 |
+
assert EX.get_exact() == EX
|
559 |
+
assert ZZ.get_exact() == ZZ
|
560 |
+
assert QQ.get_exact() == QQ
|
561 |
+
assert RR.get_exact() == QQ
|
562 |
+
assert ALG.get_exact() == ALG
|
563 |
+
assert ZZ[x].get_exact() == ZZ[x]
|
564 |
+
assert QQ[x].get_exact() == QQ[x]
|
565 |
+
assert ZZ[x, y].get_exact() == ZZ[x, y]
|
566 |
+
assert QQ[x, y].get_exact() == QQ[x, y]
|
567 |
+
assert ZZ.frac_field(x).get_exact() == ZZ.frac_field(x)
|
568 |
+
assert QQ.frac_field(x).get_exact() == QQ.frac_field(x)
|
569 |
+
assert ZZ.frac_field(x, y).get_exact() == ZZ.frac_field(x, y)
|
570 |
+
assert QQ.frac_field(x, y).get_exact() == QQ.frac_field(x, y)
|
571 |
+
|
572 |
+
|
573 |
+
def test_Domain_is_unit():
|
574 |
+
nums = [-2, -1, 0, 1, 2]
|
575 |
+
invring = [False, True, False, True, False]
|
576 |
+
invfield = [True, True, False, True, True]
|
577 |
+
ZZx, QQx, QQxf = ZZ[x], QQ[x], QQ.frac_field(x)
|
578 |
+
assert [ZZ.is_unit(ZZ(n)) for n in nums] == invring
|
579 |
+
assert [QQ.is_unit(QQ(n)) for n in nums] == invfield
|
580 |
+
assert [ZZx.is_unit(ZZx(n)) for n in nums] == invring
|
581 |
+
assert [QQx.is_unit(QQx(n)) for n in nums] == invfield
|
582 |
+
assert [QQxf.is_unit(QQxf(n)) for n in nums] == invfield
|
583 |
+
assert ZZx.is_unit(ZZx(x)) is False
|
584 |
+
assert QQx.is_unit(QQx(x)) is False
|
585 |
+
assert QQxf.is_unit(QQxf(x)) is True
|
586 |
+
|
587 |
+
|
588 |
+
def test_Domain_convert():
|
589 |
+
|
590 |
+
def check_element(e1, e2, K1, K2, K3):
|
591 |
+
assert type(e1) is type(e2), '%s, %s: %s %s -> %s' % (e1, e2, K1, K2, K3)
|
592 |
+
assert e1 == e2, '%s, %s: %s %s -> %s' % (e1, e2, K1, K2, K3)
|
593 |
+
|
594 |
+
def check_domains(K1, K2):
|
595 |
+
K3 = K1.unify(K2)
|
596 |
+
check_element(K3.convert_from( K1.one, K1), K3.one, K1, K2, K3)
|
597 |
+
check_element(K3.convert_from( K2.one, K2), K3.one, K1, K2, K3)
|
598 |
+
check_element(K3.convert_from(K1.zero, K1), K3.zero, K1, K2, K3)
|
599 |
+
check_element(K3.convert_from(K2.zero, K2), K3.zero, K1, K2, K3)
|
600 |
+
|
601 |
+
def composite_domains(K):
|
602 |
+
domains = [
|
603 |
+
K,
|
604 |
+
K[y], K[z], K[y, z],
|
605 |
+
K.frac_field(y), K.frac_field(z), K.frac_field(y, z),
|
606 |
+
# XXX: These should be tested and made to work...
|
607 |
+
# K.old_poly_ring(y), K.old_frac_field(y),
|
608 |
+
]
|
609 |
+
return domains
|
610 |
+
|
611 |
+
QQ2 = QQ.algebraic_field(sqrt(2))
|
612 |
+
QQ3 = QQ.algebraic_field(sqrt(3))
|
613 |
+
doms = [ZZ, QQ, QQ2, QQ3, QQ_I, ZZ_I, RR, CC]
|
614 |
+
|
615 |
+
for i, K1 in enumerate(doms):
|
616 |
+
for K2 in doms[i:]:
|
617 |
+
for K3 in composite_domains(K1):
|
618 |
+
for K4 in composite_domains(K2):
|
619 |
+
check_domains(K3, K4)
|
620 |
+
|
621 |
+
assert QQ.convert(10e-52) == QQ(1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576)
|
622 |
+
|
623 |
+
R, xr = ring("x", ZZ)
|
624 |
+
assert ZZ.convert(xr - xr) == 0
|
625 |
+
assert ZZ.convert(xr - xr, R.to_domain()) == 0
|
626 |
+
|
627 |
+
assert CC.convert(ZZ_I(1, 2)) == CC(1, 2)
|
628 |
+
assert CC.convert(QQ_I(1, 2)) == CC(1, 2)
|
629 |
+
|
630 |
+
K1 = QQ.frac_field(x)
|
631 |
+
K2 = ZZ.frac_field(x)
|
632 |
+
K3 = QQ[x]
|
633 |
+
K4 = ZZ[x]
|
634 |
+
Ks = [K1, K2, K3, K4]
|
635 |
+
for Ka, Kb in product(Ks, Ks):
|
636 |
+
assert Ka.convert_from(Kb.from_sympy(x), Kb) == Ka.from_sympy(x)
|
637 |
+
|
638 |
+
assert K2.convert_from(QQ(1, 2), QQ) == K2(QQ(1, 2))
|
639 |
+
|
640 |
+
|
641 |
+
def test_GlobalPolynomialRing_convert():
|
642 |
+
K1 = QQ.old_poly_ring(x)
|
643 |
+
K2 = QQ[x]
|
644 |
+
assert K1.convert(x) == K1.convert(K2.convert(x), K2)
|
645 |
+
assert K2.convert(x) == K2.convert(K1.convert(x), K1)
|
646 |
+
|
647 |
+
K1 = QQ.old_poly_ring(x, y)
|
648 |
+
K2 = QQ[x]
|
649 |
+
assert K1.convert(x) == K1.convert(K2.convert(x), K2)
|
650 |
+
#assert K2.convert(x) == K2.convert(K1.convert(x), K1)
|
651 |
+
|
652 |
+
K1 = ZZ.old_poly_ring(x, y)
|
653 |
+
K2 = QQ[x]
|
654 |
+
assert K1.convert(x) == K1.convert(K2.convert(x), K2)
|
655 |
+
#assert K2.convert(x) == K2.convert(K1.convert(x), K1)
|
656 |
+
|
657 |
+
|
658 |
+
def test_PolynomialRing__init():
|
659 |
+
R, = ring("", ZZ)
|
660 |
+
assert ZZ.poly_ring() == R.to_domain()
|
661 |
+
|
662 |
+
|
663 |
+
def test_FractionField__init():
|
664 |
+
F, = field("", ZZ)
|
665 |
+
assert ZZ.frac_field() == F.to_domain()
|
666 |
+
|
667 |
+
|
668 |
+
def test_FractionField_convert():
|
669 |
+
K = QQ.frac_field(x)
|
670 |
+
assert K.convert(QQ(2, 3), QQ) == K.from_sympy(Rational(2, 3))
|
671 |
+
K = QQ.frac_field(x)
|
672 |
+
assert K.convert(ZZ(2), ZZ) == K.from_sympy(Integer(2))
|
673 |
+
|
674 |
+
|
675 |
+
def test_inject():
|
676 |
+
assert ZZ.inject(x, y, z) == ZZ[x, y, z]
|
677 |
+
assert ZZ[x].inject(y, z) == ZZ[x, y, z]
|
678 |
+
assert ZZ.frac_field(x).inject(y, z) == ZZ.frac_field(x, y, z)
|
679 |
+
raises(GeneratorsError, lambda: ZZ[x].inject(x))
|
680 |
+
|
681 |
+
|
682 |
+
def test_drop():
|
683 |
+
assert ZZ.drop(x) == ZZ
|
684 |
+
assert ZZ[x].drop(x) == ZZ
|
685 |
+
assert ZZ[x, y].drop(x) == ZZ[y]
|
686 |
+
assert ZZ.frac_field(x).drop(x) == ZZ
|
687 |
+
assert ZZ.frac_field(x, y).drop(x) == ZZ.frac_field(y)
|
688 |
+
assert ZZ[x][y].drop(y) == ZZ[x]
|
689 |
+
assert ZZ[x][y].drop(x) == ZZ[y]
|
690 |
+
assert ZZ.frac_field(x)[y].drop(x) == ZZ[y]
|
691 |
+
assert ZZ.frac_field(x)[y].drop(y) == ZZ.frac_field(x)
|
692 |
+
Ky = FiniteExtension(Poly(x**2-1, x, domain=ZZ[y]))
|
693 |
+
K = FiniteExtension(Poly(x**2-1, x, domain=ZZ))
|
694 |
+
assert Ky.drop(y) == K
|
695 |
+
raises(GeneratorsError, lambda: Ky.drop(x))
|
696 |
+
|
697 |
+
|
698 |
+
def test_Domain_map():
|
699 |
+
seq = ZZ.map([1, 2, 3, 4])
|
700 |
+
|
701 |
+
assert all(ZZ.of_type(elt) for elt in seq)
|
702 |
+
|
703 |
+
seq = ZZ.map([[1, 2, 3, 4]])
|
704 |
+
|
705 |
+
assert all(ZZ.of_type(elt) for elt in seq[0]) and len(seq) == 1
|
706 |
+
|
707 |
+
|
708 |
+
def test_Domain___eq__():
|
709 |
+
assert (ZZ[x, y] == ZZ[x, y]) is True
|
710 |
+
assert (QQ[x, y] == QQ[x, y]) is True
|
711 |
+
|
712 |
+
assert (ZZ[x, y] == QQ[x, y]) is False
|
713 |
+
assert (QQ[x, y] == ZZ[x, y]) is False
|
714 |
+
|
715 |
+
assert (ZZ.frac_field(x, y) == ZZ.frac_field(x, y)) is True
|
716 |
+
assert (QQ.frac_field(x, y) == QQ.frac_field(x, y)) is True
|
717 |
+
|
718 |
+
assert (ZZ.frac_field(x, y) == QQ.frac_field(x, y)) is False
|
719 |
+
assert (QQ.frac_field(x, y) == ZZ.frac_field(x, y)) is False
|
720 |
+
|
721 |
+
assert RealField()[x] == RR[x]
|
722 |
+
|
723 |
+
|
724 |
+
def test_Domain__algebraic_field():
|
725 |
+
alg = ZZ.algebraic_field(sqrt(2))
|
726 |
+
assert alg.ext.minpoly == Poly(x**2 - 2)
|
727 |
+
assert alg.dom == QQ
|
728 |
+
|
729 |
+
alg = QQ.algebraic_field(sqrt(2))
|
730 |
+
assert alg.ext.minpoly == Poly(x**2 - 2)
|
731 |
+
assert alg.dom == QQ
|
732 |
+
|
733 |
+
alg = alg.algebraic_field(sqrt(3))
|
734 |
+
assert alg.ext.minpoly == Poly(x**4 - 10*x**2 + 1)
|
735 |
+
assert alg.dom == QQ
|
736 |
+
|
737 |
+
|
738 |
+
def test_Domain_alg_field_from_poly():
|
739 |
+
f = Poly(x**2 - 2)
|
740 |
+
g = Poly(x**2 - 3)
|
741 |
+
h = Poly(x**4 - 10*x**2 + 1)
|
742 |
+
|
743 |
+
alg = ZZ.alg_field_from_poly(f)
|
744 |
+
assert alg.ext.minpoly == f
|
745 |
+
assert alg.dom == QQ
|
746 |
+
|
747 |
+
alg = QQ.alg_field_from_poly(f)
|
748 |
+
assert alg.ext.minpoly == f
|
749 |
+
assert alg.dom == QQ
|
750 |
+
|
751 |
+
alg = alg.alg_field_from_poly(g)
|
752 |
+
assert alg.ext.minpoly == h
|
753 |
+
assert alg.dom == QQ
|
754 |
+
|
755 |
+
|
756 |
+
def test_Domain_cyclotomic_field():
|
757 |
+
K = ZZ.cyclotomic_field(12)
|
758 |
+
assert K.ext.minpoly == Poly(cyclotomic_poly(12))
|
759 |
+
assert K.dom == QQ
|
760 |
+
|
761 |
+
F = QQ.cyclotomic_field(3)
|
762 |
+
assert F.ext.minpoly == Poly(cyclotomic_poly(3))
|
763 |
+
assert F.dom == QQ
|
764 |
+
|
765 |
+
E = F.cyclotomic_field(4)
|
766 |
+
assert field_isomorphism(E.ext, K.ext) is not None
|
767 |
+
assert E.dom == QQ
|
768 |
+
|
769 |
+
|
770 |
+
def test_PolynomialRing_from_FractionField():
|
771 |
+
F, x,y = field("x,y", ZZ)
|
772 |
+
R, X,Y = ring("x,y", ZZ)
|
773 |
+
|
774 |
+
f = (x**2 + y**2)/(x + 1)
|
775 |
+
g = (x**2 + y**2)/4
|
776 |
+
h = x**2 + y**2
|
777 |
+
|
778 |
+
assert R.to_domain().from_FractionField(f, F.to_domain()) is None
|
779 |
+
assert R.to_domain().from_FractionField(g, F.to_domain()) == X**2/4 + Y**2/4
|
780 |
+
assert R.to_domain().from_FractionField(h, F.to_domain()) == X**2 + Y**2
|
781 |
+
|
782 |
+
F, x,y = field("x,y", QQ)
|
783 |
+
R, X,Y = ring("x,y", QQ)
|
784 |
+
|
785 |
+
f = (x**2 + y**2)/(x + 1)
|
786 |
+
g = (x**2 + y**2)/4
|
787 |
+
h = x**2 + y**2
|
788 |
+
|
789 |
+
assert R.to_domain().from_FractionField(f, F.to_domain()) is None
|
790 |
+
assert R.to_domain().from_FractionField(g, F.to_domain()) == X**2/4 + Y**2/4
|
791 |
+
assert R.to_domain().from_FractionField(h, F.to_domain()) == X**2 + Y**2
|
792 |
+
|
793 |
+
def test_FractionField_from_PolynomialRing():
|
794 |
+
R, x,y = ring("x,y", QQ)
|
795 |
+
F, X,Y = field("x,y", ZZ)
|
796 |
+
|
797 |
+
f = 3*x**2 + 5*y**2
|
798 |
+
g = x**2/3 + y**2/5
|
799 |
+
|
800 |
+
assert F.to_domain().from_PolynomialRing(f, R.to_domain()) == 3*X**2 + 5*Y**2
|
801 |
+
assert F.to_domain().from_PolynomialRing(g, R.to_domain()) == (5*X**2 + 3*Y**2)/15
|
802 |
+
|
803 |
+
def test_FF_of_type():
|
804 |
+
assert FF(3).of_type(FF(3)(1)) is True
|
805 |
+
assert FF(5).of_type(FF(5)(3)) is True
|
806 |
+
assert FF(5).of_type(FF(7)(3)) is False
|
807 |
+
|
808 |
+
|
809 |
+
def test___eq__():
|
810 |
+
assert not QQ[x] == ZZ[x]
|
811 |
+
assert not QQ.frac_field(x) == ZZ.frac_field(x)
|
812 |
+
|
813 |
+
|
814 |
+
def test_RealField_from_sympy():
|
815 |
+
assert RR.convert(S.Zero) == RR.dtype(0)
|
816 |
+
assert RR.convert(S(0.0)) == RR.dtype(0.0)
|
817 |
+
assert RR.convert(S.One) == RR.dtype(1)
|
818 |
+
assert RR.convert(S(1.0)) == RR.dtype(1.0)
|
819 |
+
assert RR.convert(sin(1)) == RR.dtype(sin(1).evalf())
|
820 |
+
|
821 |
+
|
822 |
+
def test_not_in_any_domain():
|
823 |
+
check = list(_illegal) + [x] + [
|
824 |
+
float(i) for i in _illegal[:3]]
|
825 |
+
for dom in (ZZ, QQ, RR, CC, EX):
|
826 |
+
for i in check:
|
827 |
+
if i == x and dom == EX:
|
828 |
+
continue
|
829 |
+
assert i not in dom, (i, dom)
|
830 |
+
raises(CoercionFailed, lambda: dom.convert(i))
|
831 |
+
|
832 |
+
|
833 |
+
def test_ModularInteger():
|
834 |
+
F3 = FF(3)
|
835 |
+
|
836 |
+
a = F3(0)
|
837 |
+
assert isinstance(a, F3.dtype) and a == 0
|
838 |
+
a = F3(1)
|
839 |
+
assert isinstance(a, F3.dtype) and a == 1
|
840 |
+
a = F3(2)
|
841 |
+
assert isinstance(a, F3.dtype) and a == 2
|
842 |
+
a = F3(3)
|
843 |
+
assert isinstance(a, F3.dtype) and a == 0
|
844 |
+
a = F3(4)
|
845 |
+
assert isinstance(a, F3.dtype) and a == 1
|
846 |
+
|
847 |
+
a = F3(F3(0))
|
848 |
+
assert isinstance(a, F3.dtype) and a == 0
|
849 |
+
a = F3(F3(1))
|
850 |
+
assert isinstance(a, F3.dtype) and a == 1
|
851 |
+
a = F3(F3(2))
|
852 |
+
assert isinstance(a, F3.dtype) and a == 2
|
853 |
+
a = F3(F3(3))
|
854 |
+
assert isinstance(a, F3.dtype) and a == 0
|
855 |
+
a = F3(F3(4))
|
856 |
+
assert isinstance(a, F3.dtype) and a == 1
|
857 |
+
|
858 |
+
a = -F3(1)
|
859 |
+
assert isinstance(a, F3.dtype) and a == 2
|
860 |
+
a = -F3(2)
|
861 |
+
assert isinstance(a, F3.dtype) and a == 1
|
862 |
+
|
863 |
+
a = 2 + F3(2)
|
864 |
+
assert isinstance(a, F3.dtype) and a == 1
|
865 |
+
a = F3(2) + 2
|
866 |
+
assert isinstance(a, F3.dtype) and a == 1
|
867 |
+
a = F3(2) + F3(2)
|
868 |
+
assert isinstance(a, F3.dtype) and a == 1
|
869 |
+
a = F3(2) + F3(2)
|
870 |
+
assert isinstance(a, F3.dtype) and a == 1
|
871 |
+
|
872 |
+
a = 3 - F3(2)
|
873 |
+
assert isinstance(a, F3.dtype) and a == 1
|
874 |
+
a = F3(3) - 2
|
875 |
+
assert isinstance(a, F3.dtype) and a == 1
|
876 |
+
a = F3(3) - F3(2)
|
877 |
+
assert isinstance(a, F3.dtype) and a == 1
|
878 |
+
a = F3(3) - F3(2)
|
879 |
+
assert isinstance(a, F3.dtype) and a == 1
|
880 |
+
|
881 |
+
a = 2*F3(2)
|
882 |
+
assert isinstance(a, F3.dtype) and a == 1
|
883 |
+
a = F3(2)*2
|
884 |
+
assert isinstance(a, F3.dtype) and a == 1
|
885 |
+
a = F3(2)*F3(2)
|
886 |
+
assert isinstance(a, F3.dtype) and a == 1
|
887 |
+
a = F3(2)*F3(2)
|
888 |
+
assert isinstance(a, F3.dtype) and a == 1
|
889 |
+
|
890 |
+
a = 2/F3(2)
|
891 |
+
assert isinstance(a, F3.dtype) and a == 1
|
892 |
+
a = F3(2)/2
|
893 |
+
assert isinstance(a, F3.dtype) and a == 1
|
894 |
+
a = F3(2)/F3(2)
|
895 |
+
assert isinstance(a, F3.dtype) and a == 1
|
896 |
+
a = F3(2)/F3(2)
|
897 |
+
assert isinstance(a, F3.dtype) and a == 1
|
898 |
+
|
899 |
+
a = 1 % F3(2)
|
900 |
+
assert isinstance(a, F3.dtype) and a == 1
|
901 |
+
a = F3(1) % 2
|
902 |
+
assert isinstance(a, F3.dtype) and a == 1
|
903 |
+
a = F3(1) % F3(2)
|
904 |
+
assert isinstance(a, F3.dtype) and a == 1
|
905 |
+
a = F3(1) % F3(2)
|
906 |
+
assert isinstance(a, F3.dtype) and a == 1
|
907 |
+
|
908 |
+
a = F3(2)**0
|
909 |
+
assert isinstance(a, F3.dtype) and a == 1
|
910 |
+
a = F3(2)**1
|
911 |
+
assert isinstance(a, F3.dtype) and a == 2
|
912 |
+
a = F3(2)**2
|
913 |
+
assert isinstance(a, F3.dtype) and a == 1
|
914 |
+
|
915 |
+
F7 = FF(7)
|
916 |
+
|
917 |
+
a = F7(3)**100000000000
|
918 |
+
assert isinstance(a, F7.dtype) and a == 4
|
919 |
+
a = F7(3)**-100000000000
|
920 |
+
assert isinstance(a, F7.dtype) and a == 2
|
921 |
+
a = F7(3)**S(2)
|
922 |
+
assert isinstance(a, F7.dtype) and a == 2
|
923 |
+
|
924 |
+
assert bool(F3(3)) is False
|
925 |
+
assert bool(F3(4)) is True
|
926 |
+
|
927 |
+
F5 = FF(5)
|
928 |
+
|
929 |
+
a = F5(1)**(-1)
|
930 |
+
assert isinstance(a, F5.dtype) and a == 1
|
931 |
+
a = F5(2)**(-1)
|
932 |
+
assert isinstance(a, F5.dtype) and a == 3
|
933 |
+
a = F5(3)**(-1)
|
934 |
+
assert isinstance(a, F5.dtype) and a == 2
|
935 |
+
a = F5(4)**(-1)
|
936 |
+
assert isinstance(a, F5.dtype) and a == 4
|
937 |
+
|
938 |
+
assert (F5(1) < F5(2)) is True
|
939 |
+
assert (F5(1) <= F5(2)) is True
|
940 |
+
assert (F5(1) > F5(2)) is False
|
941 |
+
assert (F5(1) >= F5(2)) is False
|
942 |
+
|
943 |
+
assert (F5(3) < F5(2)) is False
|
944 |
+
assert (F5(3) <= F5(2)) is False
|
945 |
+
assert (F5(3) > F5(2)) is True
|
946 |
+
assert (F5(3) >= F5(2)) is True
|
947 |
+
|
948 |
+
assert (F5(1) < F5(7)) is True
|
949 |
+
assert (F5(1) <= F5(7)) is True
|
950 |
+
assert (F5(1) > F5(7)) is False
|
951 |
+
assert (F5(1) >= F5(7)) is False
|
952 |
+
|
953 |
+
assert (F5(3) < F5(7)) is False
|
954 |
+
assert (F5(3) <= F5(7)) is False
|
955 |
+
assert (F5(3) > F5(7)) is True
|
956 |
+
assert (F5(3) >= F5(7)) is True
|
957 |
+
|
958 |
+
assert (F5(1) < 2) is True
|
959 |
+
assert (F5(1) <= 2) is True
|
960 |
+
assert (F5(1) > 2) is False
|
961 |
+
assert (F5(1) >= 2) is False
|
962 |
+
|
963 |
+
assert (F5(3) < 2) is False
|
964 |
+
assert (F5(3) <= 2) is False
|
965 |
+
assert (F5(3) > 2) is True
|
966 |
+
assert (F5(3) >= 2) is True
|
967 |
+
|
968 |
+
assert (F5(1) < 7) is True
|
969 |
+
assert (F5(1) <= 7) is True
|
970 |
+
assert (F5(1) > 7) is False
|
971 |
+
assert (F5(1) >= 7) is False
|
972 |
+
|
973 |
+
assert (F5(3) < 7) is False
|
974 |
+
assert (F5(3) <= 7) is False
|
975 |
+
assert (F5(3) > 7) is True
|
976 |
+
assert (F5(3) >= 7) is True
|
977 |
+
|
978 |
+
raises(NotInvertible, lambda: F5(0)**(-1))
|
979 |
+
raises(NotInvertible, lambda: F5(5)**(-1))
|
980 |
+
|
981 |
+
raises(ValueError, lambda: FF(0))
|
982 |
+
raises(ValueError, lambda: FF(2.1))
|
983 |
+
|
984 |
+
def test_QQ_int():
|
985 |
+
assert int(QQ(2**2000, 3**1250)) == 455431
|
986 |
+
assert int(QQ(2**100, 3)) == 422550200076076467165567735125
|
987 |
+
|
988 |
+
def test_RR_double():
|
989 |
+
assert RR(3.14) > 1e-50
|
990 |
+
assert RR(1e-13) > 1e-50
|
991 |
+
assert RR(1e-14) > 1e-50
|
992 |
+
assert RR(1e-15) > 1e-50
|
993 |
+
assert RR(1e-20) > 1e-50
|
994 |
+
assert RR(1e-40) > 1e-50
|
995 |
+
|
996 |
+
def test_RR_Float():
|
997 |
+
f1 = Float("1.01")
|
998 |
+
f2 = Float("1.0000000000000000000001")
|
999 |
+
assert f1._prec == 53
|
1000 |
+
assert f2._prec == 80
|
1001 |
+
assert RR(f1)-1 > 1e-50
|
1002 |
+
assert RR(f2)-1 < 1e-50 # RR's precision is lower than f2's
|
1003 |
+
|
1004 |
+
RR2 = RealField(prec=f2._prec)
|
1005 |
+
assert RR2(f1)-1 > 1e-50
|
1006 |
+
assert RR2(f2)-1 > 1e-50 # RR's precision is equal to f2's
|
1007 |
+
|
1008 |
+
|
1009 |
+
def test_CC_double():
|
1010 |
+
assert CC(3.14).real > 1e-50
|
1011 |
+
assert CC(1e-13).real > 1e-50
|
1012 |
+
assert CC(1e-14).real > 1e-50
|
1013 |
+
assert CC(1e-15).real > 1e-50
|
1014 |
+
assert CC(1e-20).real > 1e-50
|
1015 |
+
assert CC(1e-40).real > 1e-50
|
1016 |
+
|
1017 |
+
assert CC(3.14j).imag > 1e-50
|
1018 |
+
assert CC(1e-13j).imag > 1e-50
|
1019 |
+
assert CC(1e-14j).imag > 1e-50
|
1020 |
+
assert CC(1e-15j).imag > 1e-50
|
1021 |
+
assert CC(1e-20j).imag > 1e-50
|
1022 |
+
assert CC(1e-40j).imag > 1e-50
|
1023 |
+
|
1024 |
+
|
1025 |
+
def test_gaussian_domains():
|
1026 |
+
I = S.ImaginaryUnit
|
1027 |
+
a, b, c, d = [ZZ_I.convert(x) for x in (5, 2 + I, 3 - I, 5 - 5*I)]
|
1028 |
+
assert ZZ_I.gcd(a, b) == b
|
1029 |
+
assert ZZ_I.gcd(a, c) == b
|
1030 |
+
assert ZZ_I.lcm(a, b) == a
|
1031 |
+
assert ZZ_I.lcm(a, c) == d
|
1032 |
+
assert ZZ_I(3, 4) != QQ_I(3, 4) # XXX is this right or should QQ->ZZ if possible?
|
1033 |
+
assert ZZ_I(3, 0) != 3 # and should this go to Integer?
|
1034 |
+
assert QQ_I(S(3)/4, 0) != S(3)/4 # and this to Rational?
|
1035 |
+
assert ZZ_I(0, 0).quadrant() == 0
|
1036 |
+
assert ZZ_I(-1, 0).quadrant() == 2
|
1037 |
+
|
1038 |
+
assert QQ_I.convert(QQ(3, 2)) == QQ_I(QQ(3, 2), QQ(0))
|
1039 |
+
assert QQ_I.convert(QQ(3, 2), QQ) == QQ_I(QQ(3, 2), QQ(0))
|
1040 |
+
|
1041 |
+
for G in (QQ_I, ZZ_I):
|
1042 |
+
|
1043 |
+
q = G(3, 4)
|
1044 |
+
assert str(q) == '3 + 4*I'
|
1045 |
+
assert q.parent() == G
|
1046 |
+
assert q._get_xy(pi) == (None, None)
|
1047 |
+
assert q._get_xy(2) == (2, 0)
|
1048 |
+
assert q._get_xy(2*I) == (0, 2)
|
1049 |
+
|
1050 |
+
assert hash(q) == hash((3, 4))
|
1051 |
+
assert G(1, 2) == G(1, 2)
|
1052 |
+
assert G(1, 2) != G(1, 3)
|
1053 |
+
assert G(3, 0) == G(3)
|
1054 |
+
|
1055 |
+
assert q + q == G(6, 8)
|
1056 |
+
assert q - q == G(0, 0)
|
1057 |
+
assert 3 - q == -q + 3 == G(0, -4)
|
1058 |
+
assert 3 + q == q + 3 == G(6, 4)
|
1059 |
+
assert q * q == G(-7, 24)
|
1060 |
+
assert 3 * q == q * 3 == G(9, 12)
|
1061 |
+
assert q ** 0 == G(1, 0)
|
1062 |
+
assert q ** 1 == q
|
1063 |
+
assert q ** 2 == q * q == G(-7, 24)
|
1064 |
+
assert q ** 3 == q * q * q == G(-117, 44)
|
1065 |
+
assert 1 / q == q ** -1 == QQ_I(S(3)/25, - S(4)/25)
|
1066 |
+
assert q / 1 == QQ_I(3, 4)
|
1067 |
+
assert q / 2 == QQ_I(S(3)/2, 2)
|
1068 |
+
assert q/3 == QQ_I(1, S(4)/3)
|
1069 |
+
assert 3/q == QQ_I(S(9)/25, -S(12)/25)
|
1070 |
+
i, r = divmod(q, 2)
|
1071 |
+
assert 2*i + r == q
|
1072 |
+
i, r = divmod(2, q)
|
1073 |
+
assert q*i + r == G(2, 0)
|
1074 |
+
|
1075 |
+
raises(ZeroDivisionError, lambda: q % 0)
|
1076 |
+
raises(ZeroDivisionError, lambda: q / 0)
|
1077 |
+
raises(ZeroDivisionError, lambda: q // 0)
|
1078 |
+
raises(ZeroDivisionError, lambda: divmod(q, 0))
|
1079 |
+
raises(ZeroDivisionError, lambda: divmod(q, 0))
|
1080 |
+
raises(TypeError, lambda: q + x)
|
1081 |
+
raises(TypeError, lambda: q - x)
|
1082 |
+
raises(TypeError, lambda: x + q)
|
1083 |
+
raises(TypeError, lambda: x - q)
|
1084 |
+
raises(TypeError, lambda: q * x)
|
1085 |
+
raises(TypeError, lambda: x * q)
|
1086 |
+
raises(TypeError, lambda: q / x)
|
1087 |
+
raises(TypeError, lambda: x / q)
|
1088 |
+
raises(TypeError, lambda: q // x)
|
1089 |
+
raises(TypeError, lambda: x // q)
|
1090 |
+
|
1091 |
+
assert G.from_sympy(S(2)) == G(2, 0)
|
1092 |
+
assert G.to_sympy(G(2, 0)) == S(2)
|
1093 |
+
raises(CoercionFailed, lambda: G.from_sympy(pi))
|
1094 |
+
|
1095 |
+
PR = G.inject(x)
|
1096 |
+
assert isinstance(PR, PolynomialRing)
|
1097 |
+
assert PR.domain == G
|
1098 |
+
assert len(PR.gens) == 1 and PR.gens[0].as_expr() == x
|
1099 |
+
|
1100 |
+
if G is QQ_I:
|
1101 |
+
AF = G.as_AlgebraicField()
|
1102 |
+
assert isinstance(AF, AlgebraicField)
|
1103 |
+
assert AF.domain == QQ
|
1104 |
+
assert AF.ext.args[0] == I
|
1105 |
+
|
1106 |
+
for qi in [G(-1, 0), G(1, 0), G(0, -1), G(0, 1)]:
|
1107 |
+
assert G.is_negative(qi) is False
|
1108 |
+
assert G.is_positive(qi) is False
|
1109 |
+
assert G.is_nonnegative(qi) is False
|
1110 |
+
assert G.is_nonpositive(qi) is False
|
1111 |
+
|
1112 |
+
domains = [ZZ_python(), QQ_python(), AlgebraicField(QQ, I)]
|
1113 |
+
if HAS_GMPY:
|
1114 |
+
domains += [ZZ_gmpy(), QQ_gmpy()]
|
1115 |
+
|
1116 |
+
for K in domains:
|
1117 |
+
assert G.convert(K(2)) == G(2, 0)
|
1118 |
+
assert G.convert(K(2), K) == G(2, 0)
|
1119 |
+
|
1120 |
+
for K in ZZ_I, QQ_I:
|
1121 |
+
assert G.convert(K(1, 1)) == G(1, 1)
|
1122 |
+
assert G.convert(K(1, 1), K) == G(1, 1)
|
1123 |
+
|
1124 |
+
if G == ZZ_I:
|
1125 |
+
assert repr(q) == 'ZZ_I(3, 4)'
|
1126 |
+
assert q//3 == G(1, 1)
|
1127 |
+
assert 12//q == G(1, -2)
|
1128 |
+
assert 12 % q == G(1, 2)
|
1129 |
+
assert q % 2 == G(-1, 0)
|
1130 |
+
assert i == G(0, 0)
|
1131 |
+
assert r == G(2, 0)
|
1132 |
+
assert G.get_ring() == G
|
1133 |
+
assert G.get_field() == QQ_I
|
1134 |
+
else:
|
1135 |
+
assert repr(q) == 'QQ_I(3, 4)'
|
1136 |
+
assert G.get_ring() == ZZ_I
|
1137 |
+
assert G.get_field() == G
|
1138 |
+
assert q//3 == G(1, S(4)/3)
|
1139 |
+
assert 12//q == G(S(36)/25, -S(48)/25)
|
1140 |
+
assert 12 % q == G(0, 0)
|
1141 |
+
assert q % 2 == G(0, 0)
|
1142 |
+
assert i == G(S(6)/25, -S(8)/25), (G,i)
|
1143 |
+
assert r == G(0, 0)
|
1144 |
+
q2 = G(S(3)/2, S(5)/3)
|
1145 |
+
assert G.numer(q2) == ZZ_I(9, 10)
|
1146 |
+
assert G.denom(q2) == ZZ_I(6)
|
1147 |
+
|
1148 |
+
|
1149 |
+
def test_EX_EXRAW():
|
1150 |
+
assert EXRAW.zero is S.Zero
|
1151 |
+
assert EXRAW.one is S.One
|
1152 |
+
|
1153 |
+
assert EX(1) == EX.Expression(1)
|
1154 |
+
assert EX(1).ex is S.One
|
1155 |
+
assert EXRAW(1) is S.One
|
1156 |
+
|
1157 |
+
# EX has cancelling but EXRAW does not
|
1158 |
+
assert 2*EX((x + y*x)/x) == EX(2 + 2*y) != 2*((x + y*x)/x)
|
1159 |
+
assert 2*EXRAW((x + y*x)/x) == 2*((x + y*x)/x) != (1 + y)
|
1160 |
+
|
1161 |
+
assert EXRAW.convert_from(EX(1), EX) is EXRAW.one
|
1162 |
+
assert EX.convert_from(EXRAW(1), EXRAW) == EX.one
|
1163 |
+
|
1164 |
+
assert EXRAW.from_sympy(S.One) is S.One
|
1165 |
+
assert EXRAW.to_sympy(EXRAW.one) is S.One
|
1166 |
+
raises(CoercionFailed, lambda: EXRAW.from_sympy([]))
|
1167 |
+
|
1168 |
+
assert EXRAW.get_field() == EXRAW
|
1169 |
+
|
1170 |
+
assert EXRAW.unify(EX) == EXRAW
|
1171 |
+
assert EX.unify(EXRAW) == EXRAW
|
1172 |
+
|
1173 |
+
|
1174 |
+
def test_canonical_unit():
|
1175 |
+
|
1176 |
+
for K in [ZZ, QQ, RR]: # CC?
|
1177 |
+
assert K.canonical_unit(K(2)) == K(1)
|
1178 |
+
assert K.canonical_unit(K(-2)) == K(-1)
|
1179 |
+
|
1180 |
+
for K in [ZZ_I, QQ_I]:
|
1181 |
+
i = K.from_sympy(I)
|
1182 |
+
assert K.canonical_unit(K(2)) == K(1)
|
1183 |
+
assert K.canonical_unit(K(2)*i) == -i
|
1184 |
+
assert K.canonical_unit(-K(2)) == K(-1)
|
1185 |
+
assert K.canonical_unit(-K(2)*i) == i
|
1186 |
+
|
1187 |
+
K = ZZ[x]
|
1188 |
+
assert K.canonical_unit(K(x + 1)) == K(1)
|
1189 |
+
assert K.canonical_unit(K(-x + 1)) == K(-1)
|
1190 |
+
|
1191 |
+
K = ZZ_I[x]
|
1192 |
+
assert K.canonical_unit(K.from_sympy(I*x)) == ZZ_I(0, -1)
|
1193 |
+
|
1194 |
+
K = ZZ_I.frac_field(x, y)
|
1195 |
+
i = K.from_sympy(I)
|
1196 |
+
assert i / i == K.one
|
1197 |
+
assert (K.one + i)/(i - K.one) == -i
|
1198 |
+
|
1199 |
+
|
1200 |
+
def test_issue_18278():
|
1201 |
+
assert str(RR(2).parent()) == 'RR'
|
1202 |
+
assert str(CC(2).parent()) == 'CC'
|
1203 |
+
|
1204 |
+
|
1205 |
+
def test_Domain_is_negative():
|
1206 |
+
I = S.ImaginaryUnit
|
1207 |
+
a, b = [CC.convert(x) for x in (2 + I, 5)]
|
1208 |
+
assert CC.is_negative(a) == False
|
1209 |
+
assert CC.is_negative(b) == False
|
1210 |
+
|
1211 |
+
|
1212 |
+
def test_Domain_is_positive():
|
1213 |
+
I = S.ImaginaryUnit
|
1214 |
+
a, b = [CC.convert(x) for x in (2 + I, 5)]
|
1215 |
+
assert CC.is_positive(a) == False
|
1216 |
+
assert CC.is_positive(b) == False
|
1217 |
+
|
1218 |
+
|
1219 |
+
def test_Domain_is_nonnegative():
|
1220 |
+
I = S.ImaginaryUnit
|
1221 |
+
a, b = [CC.convert(x) for x in (2 + I, 5)]
|
1222 |
+
assert CC.is_nonnegative(a) == False
|
1223 |
+
assert CC.is_nonnegative(b) == False
|
1224 |
+
|
1225 |
+
|
1226 |
+
def test_Domain_is_nonpositive():
|
1227 |
+
I = S.ImaginaryUnit
|
1228 |
+
a, b = [CC.convert(x) for x in (2 + I, 5)]
|
1229 |
+
assert CC.is_nonpositive(a) == False
|
1230 |
+
assert CC.is_nonpositive(b) == False
|
1231 |
+
|
1232 |
+
|
1233 |
+
def test_exponential_domain():
|
1234 |
+
K = ZZ[E]
|
1235 |
+
eK = K.from_sympy(E)
|
1236 |
+
assert K.from_sympy(exp(3)) == eK ** 3
|
1237 |
+
assert K.convert(exp(3)) == eK ** 3
|
1238 |
+
|
1239 |
+
|
1240 |
+
def test_AlgebraicField_alias():
|
1241 |
+
# No default alias:
|
1242 |
+
k = QQ.algebraic_field(sqrt(2))
|
1243 |
+
assert k.ext.alias is None
|
1244 |
+
|
1245 |
+
# For a single extension, its alias is used:
|
1246 |
+
alpha = AlgebraicNumber(sqrt(2), alias='alpha')
|
1247 |
+
k = QQ.algebraic_field(alpha)
|
1248 |
+
assert k.ext.alias.name == 'alpha'
|
1249 |
+
|
1250 |
+
# Can override the alias of a single extension:
|
1251 |
+
k = QQ.algebraic_field(alpha, alias='theta')
|
1252 |
+
assert k.ext.alias.name == 'theta'
|
1253 |
+
|
1254 |
+
# With multiple extensions, no default alias:
|
1255 |
+
k = QQ.algebraic_field(sqrt(2), sqrt(3))
|
1256 |
+
assert k.ext.alias is None
|
1257 |
+
|
1258 |
+
# With multiple extensions, no default alias, even if one of
|
1259 |
+
# the extensions has one:
|
1260 |
+
k = QQ.algebraic_field(alpha, sqrt(3))
|
1261 |
+
assert k.ext.alias is None
|
1262 |
+
|
1263 |
+
# With multiple extensions, may set an alias:
|
1264 |
+
k = QQ.algebraic_field(sqrt(2), sqrt(3), alias='theta')
|
1265 |
+
assert k.ext.alias.name == 'theta'
|
1266 |
+
|
1267 |
+
# Alias is passed to constructed field elements:
|
1268 |
+
k = QQ.algebraic_field(alpha)
|
1269 |
+
beta = k.to_alg_num(k([1, 2, 3]))
|
1270 |
+
assert beta.alias is alpha.alias
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/tests/test_polynomialring.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Tests for the PolynomialRing classes. """
|
2 |
+
|
3 |
+
from sympy.polys.domains import QQ, ZZ
|
4 |
+
from sympy.polys.polyerrors import ExactQuotientFailed, CoercionFailed, NotReversible
|
5 |
+
|
6 |
+
from sympy.abc import x, y
|
7 |
+
|
8 |
+
from sympy.testing.pytest import raises
|
9 |
+
|
10 |
+
|
11 |
+
def test_build_order():
|
12 |
+
R = QQ.old_poly_ring(x, y, order=(("lex", x), ("ilex", y)))
|
13 |
+
assert R.order((1, 5)) == ((1,), (-5,))
|
14 |
+
|
15 |
+
|
16 |
+
def test_globalring():
|
17 |
+
Qxy = QQ.old_frac_field(x, y)
|
18 |
+
R = QQ.old_poly_ring(x, y)
|
19 |
+
X = R.convert(x)
|
20 |
+
Y = R.convert(y)
|
21 |
+
|
22 |
+
assert x in R
|
23 |
+
assert 1/x not in R
|
24 |
+
assert 1/(1 + x) not in R
|
25 |
+
assert Y in R
|
26 |
+
assert X.ring == R
|
27 |
+
assert X * (Y**2 + 1) == R.convert(x * (y**2 + 1))
|
28 |
+
assert X * y == X * Y == R.convert(x * y) == x * Y
|
29 |
+
assert X + y == X + Y == R.convert(x + y) == x + Y
|
30 |
+
assert X - y == X - Y == R.convert(x - y) == x - Y
|
31 |
+
assert X + 1 == R.convert(x + 1)
|
32 |
+
raises(ExactQuotientFailed, lambda: X/Y)
|
33 |
+
raises(ExactQuotientFailed, lambda: x/Y)
|
34 |
+
raises(ExactQuotientFailed, lambda: X/y)
|
35 |
+
assert X**2 / X == X
|
36 |
+
|
37 |
+
assert R.from_GlobalPolynomialRing(ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X
|
38 |
+
assert R.from_FractionField(Qxy.convert(x), Qxy) == X
|
39 |
+
assert R.from_FractionField(Qxy.convert(x)/y, Qxy) is None
|
40 |
+
|
41 |
+
assert R._sdm_to_vector(R._vector_to_sdm([X, Y], R.order), 2) == [X, Y]
|
42 |
+
|
43 |
+
|
44 |
+
def test_localring():
|
45 |
+
Qxy = QQ.old_frac_field(x, y)
|
46 |
+
R = QQ.old_poly_ring(x, y, order="ilex")
|
47 |
+
X = R.convert(x)
|
48 |
+
Y = R.convert(y)
|
49 |
+
|
50 |
+
assert x in R
|
51 |
+
assert 1/x not in R
|
52 |
+
assert 1/(1 + x) in R
|
53 |
+
assert Y in R
|
54 |
+
assert X.ring == R
|
55 |
+
assert X*(Y**2 + 1)/(1 + X) == R.convert(x*(y**2 + 1)/(1 + x))
|
56 |
+
assert X*y == X*Y
|
57 |
+
raises(ExactQuotientFailed, lambda: X/Y)
|
58 |
+
raises(ExactQuotientFailed, lambda: x/Y)
|
59 |
+
raises(ExactQuotientFailed, lambda: X/y)
|
60 |
+
assert X + y == X + Y == R.convert(x + y) == x + Y
|
61 |
+
assert X - y == X - Y == R.convert(x - y) == x - Y
|
62 |
+
assert X + 1 == R.convert(x + 1)
|
63 |
+
assert X**2 / X == X
|
64 |
+
|
65 |
+
assert R.from_GlobalPolynomialRing(ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X
|
66 |
+
assert R.from_FractionField(Qxy.convert(x), Qxy) == X
|
67 |
+
raises(CoercionFailed, lambda: R.from_FractionField(Qxy.convert(x)/y, Qxy))
|
68 |
+
raises(ExactQuotientFailed, lambda: X/Y)
|
69 |
+
raises(NotReversible, lambda: X.invert())
|
70 |
+
|
71 |
+
assert R._sdm_to_vector(
|
72 |
+
R._vector_to_sdm([X/(X + 1), Y/(1 + X*Y)], R.order), 2) == \
|
73 |
+
[X*(1 + X*Y), Y*(1 + X)]
|
74 |
+
|
75 |
+
|
76 |
+
def test_conversion():
|
77 |
+
L = QQ.old_poly_ring(x, y, order="ilex")
|
78 |
+
G = QQ.old_poly_ring(x, y)
|
79 |
+
|
80 |
+
assert L.convert(x) == L.convert(G.convert(x), G)
|
81 |
+
assert G.convert(x) == G.convert(L.convert(x), L)
|
82 |
+
raises(CoercionFailed, lambda: G.convert(L.convert(1/(1 + x)), L))
|
83 |
+
|
84 |
+
|
85 |
+
def test_units():
|
86 |
+
R = QQ.old_poly_ring(x)
|
87 |
+
assert R.is_unit(R.convert(1))
|
88 |
+
assert R.is_unit(R.convert(2))
|
89 |
+
assert not R.is_unit(R.convert(x))
|
90 |
+
assert not R.is_unit(R.convert(1 + x))
|
91 |
+
|
92 |
+
R = QQ.old_poly_ring(x, order='ilex')
|
93 |
+
assert R.is_unit(R.convert(1))
|
94 |
+
assert R.is_unit(R.convert(2))
|
95 |
+
assert not R.is_unit(R.convert(x))
|
96 |
+
assert R.is_unit(R.convert(1 + x))
|
97 |
+
|
98 |
+
R = ZZ.old_poly_ring(x)
|
99 |
+
assert R.is_unit(R.convert(1))
|
100 |
+
assert not R.is_unit(R.convert(2))
|
101 |
+
assert not R.is_unit(R.convert(x))
|
102 |
+
assert not R.is_unit(R.convert(1 + x))
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/domains/tests/test_quotientring.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Tests for quotient rings."""
|
2 |
+
|
3 |
+
from sympy.polys.domains.integerring import ZZ
|
4 |
+
from sympy.polys.domains.rationalfield import QQ
|
5 |
+
from sympy.abc import x, y
|
6 |
+
|
7 |
+
from sympy.polys.polyerrors import NotReversible
|
8 |
+
|
9 |
+
from sympy.testing.pytest import raises
|
10 |
+
|
11 |
+
|
12 |
+
def test_QuotientRingElement():
|
13 |
+
R = QQ.old_poly_ring(x)/[x**10]
|
14 |
+
X = R.convert(x)
|
15 |
+
|
16 |
+
assert X*(X + 1) == R.convert(x**2 + x)
|
17 |
+
assert X*x == R.convert(x**2)
|
18 |
+
assert x*X == R.convert(x**2)
|
19 |
+
assert X + x == R.convert(2*x)
|
20 |
+
assert x + X == 2*X
|
21 |
+
assert X**2 == R.convert(x**2)
|
22 |
+
assert 1/(1 - X) == R.convert(sum(x**i for i in range(10)))
|
23 |
+
assert X**10 == R.zero
|
24 |
+
assert X != x
|
25 |
+
|
26 |
+
raises(NotReversible, lambda: 1/X)
|
27 |
+
|
28 |
+
|
29 |
+
def test_QuotientRing():
|
30 |
+
I = QQ.old_poly_ring(x).ideal(x**2 + 1)
|
31 |
+
R = QQ.old_poly_ring(x)/I
|
32 |
+
|
33 |
+
assert R == QQ.old_poly_ring(x)/[x**2 + 1]
|
34 |
+
assert R == QQ.old_poly_ring(x)/QQ.old_poly_ring(x).ideal(x**2 + 1)
|
35 |
+
assert R != QQ.old_poly_ring(x)
|
36 |
+
|
37 |
+
assert R.convert(1)/x == -x + I
|
38 |
+
assert -1 + I == x**2 + I
|
39 |
+
assert R.convert(ZZ(1), ZZ) == 1 + I
|
40 |
+
assert R.convert(R.convert(x), R) == R.convert(x)
|
41 |
+
|
42 |
+
X = R.convert(x)
|
43 |
+
Y = QQ.old_poly_ring(x).convert(x)
|
44 |
+
assert -1 + I == X**2 + I
|
45 |
+
assert -1 + I == Y**2 + I
|
46 |
+
assert R.to_sympy(X) == x
|
47 |
+
|
48 |
+
raises(ValueError, lambda: QQ.old_poly_ring(x)/QQ.old_poly_ring(x, y).ideal(x))
|
49 |
+
|
50 |
+
R = QQ.old_poly_ring(x, order="ilex")
|
51 |
+
I = R.ideal(x)
|
52 |
+
assert R.convert(1) + I == (R/I).convert(1)
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__init__.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Computational algebraic field theory. """
|
2 |
+
|
3 |
+
__all__ = [
|
4 |
+
'minpoly', 'minimal_polynomial',
|
5 |
+
|
6 |
+
'field_isomorphism', 'primitive_element', 'to_number_field',
|
7 |
+
|
8 |
+
'isolate',
|
9 |
+
|
10 |
+
'round_two',
|
11 |
+
|
12 |
+
'prime_decomp', 'prime_valuation',
|
13 |
+
|
14 |
+
'galois_group',
|
15 |
+
]
|
16 |
+
|
17 |
+
from .minpoly import minpoly, minimal_polynomial
|
18 |
+
|
19 |
+
from .subfield import field_isomorphism, primitive_element, to_number_field
|
20 |
+
|
21 |
+
from .utilities import isolate
|
22 |
+
|
23 |
+
from .basis import round_two
|
24 |
+
|
25 |
+
from .primes import prime_decomp, prime_valuation
|
26 |
+
|
27 |
+
from .galoisgroups import galois_group
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (676 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/basis.cpython-310.pyc
ADDED
Binary file (7.26 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/exceptions.cpython-310.pyc
ADDED
Binary file (2.09 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/galois_resolvents.cpython-310.pyc
ADDED
Binary file (24.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/galoisgroups.cpython-310.pyc
ADDED
Binary file (14.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/minpoly.cpython-310.pyc
ADDED
Binary file (24.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/modules.cpython-310.pyc
ADDED
Binary file (72 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/primes.cpython-310.pyc
ADDED
Binary file (23.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/resolvent_lookup.cpython-310.pyc
ADDED
Binary file (59.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/subfield.cpython-310.pyc
ADDED
Binary file (14.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/__pycache__/ut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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Computing integral bases for number fields. """
|
2 |
+
|
3 |
+
from sympy.polys.polytools import Poly
|
4 |
+
from sympy.polys.domains.algebraicfield import AlgebraicField
|
5 |
+
from sympy.polys.domains.integerring import ZZ
|
6 |
+
from sympy.polys.domains.rationalfield import QQ
|
7 |
+
from sympy.utilities.decorator import public
|
8 |
+
from .modules import ModuleEndomorphism, ModuleHomomorphism, PowerBasis
|
9 |
+
from .utilities import extract_fundamental_discriminant
|
10 |
+
|
11 |
+
|
12 |
+
def _apply_Dedekind_criterion(T, p):
|
13 |
+
r"""
|
14 |
+
Apply the "Dedekind criterion" to test whether the order needs to be
|
15 |
+
enlarged relative to a given prime *p*.
|
16 |
+
"""
|
17 |
+
x = T.gen
|
18 |
+
T_bar = Poly(T, modulus=p)
|
19 |
+
lc, fl = T_bar.factor_list()
|
20 |
+
assert lc == 1
|
21 |
+
g_bar = Poly(1, x, modulus=p)
|
22 |
+
for ti_bar, _ in fl:
|
23 |
+
g_bar *= ti_bar
|
24 |
+
h_bar = T_bar // g_bar
|
25 |
+
g = Poly(g_bar, domain=ZZ)
|
26 |
+
h = Poly(h_bar, domain=ZZ)
|
27 |
+
f = (g * h - T) // p
|
28 |
+
f_bar = Poly(f, modulus=p)
|
29 |
+
Z_bar = f_bar
|
30 |
+
for b in [g_bar, h_bar]:
|
31 |
+
Z_bar = Z_bar.gcd(b)
|
32 |
+
U_bar = T_bar // Z_bar
|
33 |
+
m = Z_bar.degree()
|
34 |
+
return U_bar, m
|
35 |
+
|
36 |
+
|
37 |
+
def nilradical_mod_p(H, p, q=None):
|
38 |
+
r"""
|
39 |
+
Compute the nilradical mod *p* for a given order *H*, and prime *p*.
|
40 |
+
|
41 |
+
Explanation
|
42 |
+
===========
|
43 |
+
|
44 |
+
This is the ideal $I$ in $H/pH$ consisting of all elements some positive
|
45 |
+
power of which is zero in this quotient ring, i.e. is a multiple of *p*.
|
46 |
+
|
47 |
+
Parameters
|
48 |
+
==========
|
49 |
+
|
50 |
+
H : :py:class:`~.Submodule`
|
51 |
+
The given order.
|
52 |
+
p : int
|
53 |
+
The rational prime.
|
54 |
+
q : int, optional
|
55 |
+
If known, the smallest power of *p* that is $>=$ the dimension of *H*.
|
56 |
+
If not provided, we compute it here.
|
57 |
+
|
58 |
+
Returns
|
59 |
+
=======
|
60 |
+
|
61 |
+
:py:class:`~.Module` representing the nilradical mod *p* in *H*.
|
62 |
+
|
63 |
+
References
|
64 |
+
==========
|
65 |
+
|
66 |
+
.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory*.
|
67 |
+
(See Lemma 6.1.6.)
|
68 |
+
|
69 |
+
"""
|
70 |
+
n = H.n
|
71 |
+
if q is None:
|
72 |
+
q = p
|
73 |
+
while q < n:
|
74 |
+
q *= p
|
75 |
+
phi = ModuleEndomorphism(H, lambda x: x**q)
|
76 |
+
return phi.kernel(modulus=p)
|
77 |
+
|
78 |
+
|
79 |
+
def _second_enlargement(H, p, q):
|
80 |
+
r"""
|
81 |
+
Perform the second enlargement in the Round Two algorithm.
|
82 |
+
"""
|
83 |
+
Ip = nilradical_mod_p(H, p, q=q)
|
84 |
+
B = H.parent.submodule_from_matrix(H.matrix * Ip.matrix, denom=H.denom)
|
85 |
+
C = B + p*H
|
86 |
+
E = C.endomorphism_ring()
|
87 |
+
phi = ModuleHomomorphism(H, E, lambda x: E.inner_endomorphism(x))
|
88 |
+
gamma = phi.kernel(modulus=p)
|
89 |
+
G = H.parent.submodule_from_matrix(H.matrix * gamma.matrix, denom=H.denom * p)
|
90 |
+
H1 = G + H
|
91 |
+
return H1, Ip
|
92 |
+
|
93 |
+
|
94 |
+
@public
|
95 |
+
def round_two(T, radicals=None):
|
96 |
+
r"""
|
97 |
+
Zassenhaus's "Round 2" algorithm.
|
98 |
+
|
99 |
+
Explanation
|
100 |
+
===========
|
101 |
+
|
102 |
+
Carry out Zassenhaus's "Round 2" algorithm on an irreducible polynomial
|
103 |
+
*T* over :ref:`ZZ` or :ref:`QQ`. This computes an integral basis and the
|
104 |
+
discriminant for the field $K = \mathbb{Q}[x]/(T(x))$.
|
105 |
+
|
106 |
+
Alternatively, you may pass an :py:class:`~.AlgebraicField` instance, in
|
107 |
+
place of the polynomial *T*, in which case the algorithm is applied to the
|
108 |
+
minimal polynomial for the field's primitive element.
|
109 |
+
|
110 |
+
Ordinarily this function need not be called directly, as one can instead
|
111 |
+
access the :py:meth:`~.AlgebraicField.maximal_order`,
|
112 |
+
:py:meth:`~.AlgebraicField.integral_basis`, and
|
113 |
+
:py:meth:`~.AlgebraicField.discriminant` methods of an
|
114 |
+
:py:class:`~.AlgebraicField`.
|
115 |
+
|
116 |
+
Examples
|
117 |
+
========
|
118 |
+
|
119 |
+
Working through an AlgebraicField:
|
120 |
+
|
121 |
+
>>> from sympy import Poly, QQ
|
122 |
+
>>> from sympy.abc import x
|
123 |
+
>>> T = Poly(x ** 3 + x ** 2 - 2 * x + 8)
|
124 |
+
>>> K = QQ.alg_field_from_poly(T, "theta")
|
125 |
+
>>> print(K.maximal_order())
|
126 |
+
Submodule[[2, 0, 0], [0, 2, 0], [0, 1, 1]]/2
|
127 |
+
>>> print(K.discriminant())
|
128 |
+
-503
|
129 |
+
>>> print(K.integral_basis(fmt='sympy'))
|
130 |
+
[1, theta, theta/2 + theta**2/2]
|
131 |
+
|
132 |
+
Calling directly:
|
133 |
+
|
134 |
+
>>> from sympy import Poly
|
135 |
+
>>> from sympy.abc import x
|
136 |
+
>>> from sympy.polys.numberfields.basis import round_two
|
137 |
+
>>> T = Poly(x ** 3 + x ** 2 - 2 * x + 8)
|
138 |
+
>>> print(round_two(T))
|
139 |
+
(Submodule[[2, 0, 0], [0, 2, 0], [0, 1, 1]]/2, -503)
|
140 |
+
|
141 |
+
The nilradicals mod $p$ that are sometimes computed during the Round Two
|
142 |
+
algorithm may be useful in further calculations. Pass a dictionary under
|
143 |
+
`radicals` to receive these:
|
144 |
+
|
145 |
+
>>> T = Poly(x**3 + 3*x**2 + 5)
|
146 |
+
>>> rad = {}
|
147 |
+
>>> ZK, dK = round_two(T, radicals=rad)
|
148 |
+
>>> print(rad)
|
149 |
+
{3: Submodule[[-1, 1, 0], [-1, 0, 1]]}
|
150 |
+
|
151 |
+
Parameters
|
152 |
+
==========
|
153 |
+
|
154 |
+
T : :py:class:`~.Poly`, :py:class:`~.AlgebraicField`
|
155 |
+
Either (1) the irreducible polynomial over :ref:`ZZ` or :ref:`QQ`
|
156 |
+
defining the number field, or (2) an :py:class:`~.AlgebraicField`
|
157 |
+
representing the number field itself.
|
158 |
+
|
159 |
+
radicals : dict, optional
|
160 |
+
This is a way for any $p$-radicals (if computed) to be returned by
|
161 |
+
reference. If desired, pass an empty dictionary. If the algorithm
|
162 |
+
reaches the point where it computes the nilradical mod $p$ of the ring
|
163 |
+
of integers $Z_K$, then an $\mathbb{F}_p$-basis for this ideal will be
|
164 |
+
stored in this dictionary under the key ``p``. This can be useful for
|
165 |
+
other algorithms, such as prime decomposition.
|
166 |
+
|
167 |
+
Returns
|
168 |
+
=======
|
169 |
+
|
170 |
+
Pair ``(ZK, dK)``, where:
|
171 |
+
|
172 |
+
``ZK`` is a :py:class:`~sympy.polys.numberfields.modules.Submodule`
|
173 |
+
representing the maximal order.
|
174 |
+
|
175 |
+
``dK`` is the discriminant of the field $K = \mathbb{Q}[x]/(T(x))$.
|
176 |
+
|
177 |
+
See Also
|
178 |
+
========
|
179 |
+
|
180 |
+
.AlgebraicField.maximal_order
|
181 |
+
.AlgebraicField.integral_basis
|
182 |
+
.AlgebraicField.discriminant
|
183 |
+
|
184 |
+
References
|
185 |
+
==========
|
186 |
+
|
187 |
+
.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
|
188 |
+
|
189 |
+
"""
|
190 |
+
K = None
|
191 |
+
if isinstance(T, AlgebraicField):
|
192 |
+
K, T = T, T.ext.minpoly_of_element()
|
193 |
+
if ( not T.is_univariate
|
194 |
+
or not T.is_irreducible
|
195 |
+
or T.domain not in [ZZ, QQ]):
|
196 |
+
raise ValueError('Round 2 requires an irreducible univariate polynomial over ZZ or QQ.')
|
197 |
+
T, _ = T.make_monic_over_integers_by_scaling_roots()
|
198 |
+
n = T.degree()
|
199 |
+
D = T.discriminant()
|
200 |
+
D_modulus = ZZ.from_sympy(abs(D))
|
201 |
+
# D must be 0 or 1 mod 4 (see Cohen Sec 4.4), which ensures we can write
|
202 |
+
# it in the form D = D_0 * F**2, where D_0 is 1 or a fundamental discriminant.
|
203 |
+
_, F = extract_fundamental_discriminant(D)
|
204 |
+
Ztheta = PowerBasis(K or T)
|
205 |
+
H = Ztheta.whole_submodule()
|
206 |
+
nilrad = None
|
207 |
+
while F:
|
208 |
+
# Next prime:
|
209 |
+
p, e = F.popitem()
|
210 |
+
U_bar, m = _apply_Dedekind_criterion(T, p)
|
211 |
+
if m == 0:
|
212 |
+
continue
|
213 |
+
# For a given prime p, the first enlargement of the order spanned by
|
214 |
+
# the current basis can be done in a simple way:
|
215 |
+
U = Ztheta.element_from_poly(Poly(U_bar, domain=ZZ))
|
216 |
+
# TODO:
|
217 |
+
# Theory says only first m columns of the U//p*H term below are needed.
|
218 |
+
# Could be slightly more efficient to use only those. Maybe `Submodule`
|
219 |
+
# class should support a slice operator?
|
220 |
+
H = H.add(U // p * H, hnf_modulus=D_modulus)
|
221 |
+
if e <= m:
|
222 |
+
continue
|
223 |
+
# A second, and possibly more, enlargements for p will be needed.
|
224 |
+
# These enlargements require a more involved procedure.
|
225 |
+
q = p
|
226 |
+
while q < n:
|
227 |
+
q *= p
|
228 |
+
H1, nilrad = _second_enlargement(H, p, q)
|
229 |
+
while H1 != H:
|
230 |
+
H = H1
|
231 |
+
H1, nilrad = _second_enlargement(H, p, q)
|
232 |
+
# Note: We do not store all nilradicals mod p, only the very last. This is
|
233 |
+
# because, unless computed against the entire integral basis, it might not
|
234 |
+
# be accurate. (In other words, if H was not already equal to ZK when we
|
235 |
+
# passed it to `_second_enlargement`, then we can't trust the nilradical
|
236 |
+
# so computed.) Example: if T(x) = x ** 3 + 15 * x ** 2 - 9 * x + 13, then
|
237 |
+
# F is divisible by 2, 3, and 7, and the nilradical mod 2 as computed above
|
238 |
+
# will not be accurate for the full, maximal order ZK.
|
239 |
+
if nilrad is not None and isinstance(radicals, dict):
|
240 |
+
radicals[p] = nilrad
|
241 |
+
ZK = H
|
242 |
+
# Pre-set expensive boolean properties which we already know to be true:
|
243 |
+
ZK._starts_with_unity = True
|
244 |
+
ZK._is_sq_maxrank_HNF = True
|
245 |
+
dK = (D * ZK.matrix.det() ** 2) // ZK.denom ** (2 * n)
|
246 |
+
return ZK, dK
|
env-llmeval/lib/python3.10/site-packages/sympy/polys/numberfields/exceptions.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Special exception classes for numberfields. """
|
2 |
+
|
3 |
+
|
4 |
+
class ClosureFailure(Exception):
|
5 |
+
r"""
|
6 |
+
Signals that a :py:class:`ModuleElement` which we tried to represent in a
|
7 |
+
certain :py:class:`Module` cannot in fact be represented there.
|
8 |
+
|
9 |
+
Examples
|
10 |
+
========
|
11 |
+
|
12 |
+
>>> from sympy.polys import Poly, cyclotomic_poly, ZZ
|
13 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
14 |
+
>>> from sympy.polys.numberfields.modules import PowerBasis, to_col, ClosureFailure
|
15 |
+
>>> from sympy.testing.pytest import raises
|
16 |
+
>>> T = Poly(cyclotomic_poly(5))
|
17 |
+
>>> A = PowerBasis(T)
|
18 |
+
>>> B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
|
19 |
+
|
20 |
+
Because we are in a cyclotomic field, the power basis ``A`` is an integral
|
21 |
+
basis, and the submodule ``B`` is just the ideal $(2)$. Therefore ``B`` can
|
22 |
+
represent an element having all even coefficients over the power basis:
|
23 |
+
|
24 |
+
>>> a1 = A(to_col([2, 4, 6, 8]))
|
25 |
+
>>> print(B.represent(a1))
|
26 |
+
DomainMatrix([[1], [2], [3], [4]], (4, 1), ZZ)
|
27 |
+
|
28 |
+
but ``B`` cannot represent an element with an odd coefficient:
|
29 |
+
|
30 |
+
>>> a2 = A(to_col([1, 2, 2, 2]))
|
31 |
+
>>> print(raises(ClosureFailure, lambda: B.represent(a2)))
|
32 |
+
<ExceptionInfo ClosureFailure('Element in QQ-span but not ZZ-span of this basis.')>
|
33 |
+
|
34 |
+
"""
|
35 |
+
pass
|
36 |
+
|
37 |
+
|
38 |
+
class StructureError(Exception):
|
39 |
+
r"""
|
40 |
+
Represents cases in which an algebraic structure was expected to have a
|
41 |
+
certain property, or be of a certain type, but was not.
|
42 |
+
"""
|
43 |
+
pass
|
44 |
+
|
45 |
+
|
46 |
+
class MissingUnityError(StructureError):
|
47 |
+
r"""Structure should contain a unity element but does not."""
|
48 |
+
pass
|
49 |
+
|
50 |
+
|
51 |
+
__all__ = [
|
52 |
+
'ClosureFailure', 'StructureError', 'MissingUnityError',
|
53 |
+
]
|