passagemath-symbolics 10.6.40__cp314-cp314t-macosx_13_0_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of passagemath-symbolics might be problematic. Click here for more details.
- passagemath_symbolics/.dylibs/libgmp.10.dylib +0 -0
- passagemath_symbolics/__init__.py +3 -0
- passagemath_symbolics-10.6.40.dist-info/METADATA +187 -0
- passagemath_symbolics-10.6.40.dist-info/RECORD +172 -0
- passagemath_symbolics-10.6.40.dist-info/WHEEL +6 -0
- passagemath_symbolics-10.6.40.dist-info/top_level.txt +3 -0
- sage/all__sagemath_symbolics.py +17 -0
- sage/calculus/all.py +14 -0
- sage/calculus/calculus.py +2826 -0
- sage/calculus/desolvers.py +1866 -0
- sage/calculus/predefined.py +51 -0
- sage/calculus/tests.py +225 -0
- sage/calculus/var.cpython-314t-darwin.so +0 -0
- sage/calculus/var.pyx +401 -0
- sage/dynamics/all__sagemath_symbolics.py +6 -0
- sage/dynamics/complex_dynamics/all.py +5 -0
- sage/dynamics/complex_dynamics/mandel_julia.py +765 -0
- sage/dynamics/complex_dynamics/mandel_julia_helper.cpython-314t-darwin.so +0 -0
- sage/dynamics/complex_dynamics/mandel_julia_helper.pyx +1035 -0
- sage/ext/all__sagemath_symbolics.py +1 -0
- sage/ext_data/kenzo/CP2.txt +45 -0
- sage/ext_data/kenzo/CP3.txt +349 -0
- sage/ext_data/kenzo/CP4.txt +4774 -0
- sage/ext_data/kenzo/README.txt +49 -0
- sage/ext_data/kenzo/S4.txt +20 -0
- sage/ext_data/magma/latex/latex.m +1021 -0
- sage/ext_data/magma/latex/latex.spec +1 -0
- sage/ext_data/magma/sage/basic.m +356 -0
- sage/ext_data/magma/sage/sage.spec +1 -0
- sage/ext_data/magma/spec +9 -0
- sage/geometry/all__sagemath_symbolics.py +8 -0
- sage/geometry/hyperbolic_space/all.py +5 -0
- sage/geometry/hyperbolic_space/hyperbolic_coercion.py +743 -0
- sage/geometry/hyperbolic_space/hyperbolic_constants.py +5 -0
- sage/geometry/hyperbolic_space/hyperbolic_geodesic.py +2409 -0
- sage/geometry/hyperbolic_space/hyperbolic_interface.py +206 -0
- sage/geometry/hyperbolic_space/hyperbolic_isometry.py +1082 -0
- sage/geometry/hyperbolic_space/hyperbolic_model.py +1502 -0
- sage/geometry/hyperbolic_space/hyperbolic_point.py +621 -0
- sage/geometry/riemannian_manifolds/all.py +7 -0
- sage/geometry/riemannian_manifolds/parametrized_surface3d.py +1632 -0
- sage/geometry/riemannian_manifolds/surface3d_generators.py +461 -0
- sage/interfaces/all__sagemath_symbolics.py +1 -0
- sage/interfaces/magma.py +3017 -0
- sage/interfaces/magma_free.py +92 -0
- sage/interfaces/maple.py +1397 -0
- sage/interfaces/mathematica.py +1345 -0
- sage/interfaces/mathics.py +1312 -0
- sage/interfaces/sympy.py +1398 -0
- sage/interfaces/sympy_wrapper.py +197 -0
- sage/interfaces/tides.py +938 -0
- sage/libs/all__sagemath_symbolics.py +6 -0
- sage/manifolds/all.py +7 -0
- sage/manifolds/calculus_method.py +555 -0
- sage/manifolds/catalog.py +437 -0
- sage/manifolds/chart.py +4019 -0
- sage/manifolds/chart_func.py +3419 -0
- sage/manifolds/continuous_map.py +2183 -0
- sage/manifolds/continuous_map_image.py +155 -0
- sage/manifolds/differentiable/affine_connection.py +2475 -0
- sage/manifolds/differentiable/all.py +1 -0
- sage/manifolds/differentiable/automorphismfield.py +1383 -0
- sage/manifolds/differentiable/automorphismfield_group.py +604 -0
- sage/manifolds/differentiable/bundle_connection.py +1445 -0
- sage/manifolds/differentiable/characteristic_cohomology_class.py +1840 -0
- sage/manifolds/differentiable/chart.py +1241 -0
- sage/manifolds/differentiable/curve.py +1028 -0
- sage/manifolds/differentiable/de_rham_cohomology.py +541 -0
- sage/manifolds/differentiable/degenerate.py +559 -0
- sage/manifolds/differentiable/degenerate_submanifold.py +1671 -0
- sage/manifolds/differentiable/diff_form.py +1658 -0
- sage/manifolds/differentiable/diff_form_module.py +1062 -0
- sage/manifolds/differentiable/diff_map.py +1315 -0
- sage/manifolds/differentiable/differentiable_submanifold.py +291 -0
- sage/manifolds/differentiable/examples/all.py +1 -0
- sage/manifolds/differentiable/examples/euclidean.py +2517 -0
- sage/manifolds/differentiable/examples/real_line.py +897 -0
- sage/manifolds/differentiable/examples/sphere.py +1186 -0
- sage/manifolds/differentiable/examples/symplectic_space.py +187 -0
- sage/manifolds/differentiable/examples/symplectic_space_test.py +40 -0
- sage/manifolds/differentiable/integrated_curve.py +4035 -0
- sage/manifolds/differentiable/levi_civita_connection.py +841 -0
- sage/manifolds/differentiable/manifold.py +4254 -0
- sage/manifolds/differentiable/manifold_homset.py +1826 -0
- sage/manifolds/differentiable/metric.py +3032 -0
- sage/manifolds/differentiable/mixed_form.py +1507 -0
- sage/manifolds/differentiable/mixed_form_algebra.py +559 -0
- sage/manifolds/differentiable/multivector_module.py +800 -0
- sage/manifolds/differentiable/multivectorfield.py +1520 -0
- sage/manifolds/differentiable/poisson_tensor.py +268 -0
- sage/manifolds/differentiable/pseudo_riemannian.py +755 -0
- sage/manifolds/differentiable/pseudo_riemannian_submanifold.py +1839 -0
- sage/manifolds/differentiable/scalarfield.py +1343 -0
- sage/manifolds/differentiable/scalarfield_algebra.py +472 -0
- sage/manifolds/differentiable/symplectic_form.py +910 -0
- sage/manifolds/differentiable/symplectic_form_test.py +220 -0
- sage/manifolds/differentiable/tangent_space.py +412 -0
- sage/manifolds/differentiable/tangent_vector.py +616 -0
- sage/manifolds/differentiable/tensorfield.py +4665 -0
- sage/manifolds/differentiable/tensorfield_module.py +963 -0
- sage/manifolds/differentiable/tensorfield_paral.py +2450 -0
- sage/manifolds/differentiable/tensorfield_paral_test.py +16 -0
- sage/manifolds/differentiable/vector_bundle.py +1728 -0
- sage/manifolds/differentiable/vectorfield.py +1717 -0
- sage/manifolds/differentiable/vectorfield_module.py +2445 -0
- sage/manifolds/differentiable/vectorframe.py +1832 -0
- sage/manifolds/family.py +270 -0
- sage/manifolds/local_frame.py +1490 -0
- sage/manifolds/manifold.py +3090 -0
- sage/manifolds/manifold_homset.py +452 -0
- sage/manifolds/operators.py +359 -0
- sage/manifolds/point.py +994 -0
- sage/manifolds/scalarfield.py +3718 -0
- sage/manifolds/scalarfield_algebra.py +629 -0
- sage/manifolds/section.py +3111 -0
- sage/manifolds/section_module.py +831 -0
- sage/manifolds/structure.py +229 -0
- sage/manifolds/subset.py +2764 -0
- sage/manifolds/subsets/all.py +1 -0
- sage/manifolds/subsets/closure.py +131 -0
- sage/manifolds/subsets/pullback.py +885 -0
- sage/manifolds/topological_submanifold.py +891 -0
- sage/manifolds/trivialization.py +733 -0
- sage/manifolds/utilities.py +1348 -0
- sage/manifolds/vector_bundle.py +1342 -0
- sage/manifolds/vector_bundle_fiber.py +332 -0
- sage/manifolds/vector_bundle_fiber_element.py +111 -0
- sage/matrix/all__sagemath_symbolics.py +1 -0
- sage/matrix/matrix_symbolic_dense.cpython-314t-darwin.so +0 -0
- sage/matrix/matrix_symbolic_dense.pxd +6 -0
- sage/matrix/matrix_symbolic_dense.pyx +1022 -0
- sage/matrix/matrix_symbolic_sparse.cpython-314t-darwin.so +0 -0
- sage/matrix/matrix_symbolic_sparse.pxd +6 -0
- sage/matrix/matrix_symbolic_sparse.pyx +1029 -0
- sage/modules/all__sagemath_symbolics.py +1 -0
- sage/modules/vector_callable_symbolic_dense.py +105 -0
- sage/modules/vector_symbolic_dense.py +116 -0
- sage/modules/vector_symbolic_sparse.py +118 -0
- sage/rings/all__sagemath_symbolics.py +4 -0
- sage/rings/asymptotic/all.py +6 -0
- sage/rings/asymptotic/asymptotic_expansion_generators.py +1485 -0
- sage/rings/asymptotic/asymptotic_ring.py +4858 -0
- sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py +4153 -0
- sage/rings/asymptotic/growth_group.py +5373 -0
- sage/rings/asymptotic/growth_group_cartesian.py +1400 -0
- sage/rings/asymptotic/term_monoid.py +5237 -0
- sage/rings/function_field/all__sagemath_symbolics.py +2 -0
- sage/rings/polynomial/all__sagemath_symbolics.py +1 -0
- sage/symbolic/all.py +15 -0
- sage/symbolic/assumptions.py +985 -0
- sage/symbolic/benchmark.py +93 -0
- sage/symbolic/callable.py +459 -0
- sage/symbolic/complexity_measures.py +35 -0
- sage/symbolic/constants.py +1287 -0
- sage/symbolic/expression_conversion_algebraic.py +310 -0
- sage/symbolic/expression_conversion_sympy.py +317 -0
- sage/symbolic/expression_conversions.py +1713 -0
- sage/symbolic/function_factory.py +355 -0
- sage/symbolic/integration/all.py +1 -0
- sage/symbolic/integration/external.py +270 -0
- sage/symbolic/integration/integral.py +1115 -0
- sage/symbolic/maxima_wrapper.py +162 -0
- sage/symbolic/operators.py +267 -0
- sage/symbolic/random_tests.py +462 -0
- sage/symbolic/relation.py +1907 -0
- sage/symbolic/ring.cpython-314t-darwin.so +0 -0
- sage/symbolic/ring.pxd +5 -0
- sage/symbolic/ring.pyx +1396 -0
- sage/symbolic/subring.py +1025 -0
- sage/symbolic/symengine.py +19 -0
- sage/symbolic/tests.py +40 -0
- sage/symbolic/units.py +1470 -0
sage/interfaces/sympy.py
ADDED
|
@@ -0,0 +1,1398 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-symbolics
|
|
2
|
+
# sage.doctest: optional - sympy
|
|
3
|
+
"""
|
|
4
|
+
SymPy --> Sage conversion
|
|
5
|
+
|
|
6
|
+
The file consists of ``_sage_()`` methods that are added lazily to
|
|
7
|
+
the respective SymPy objects. Any call of the ``_sympy_()`` method
|
|
8
|
+
of a symbolic expression will trigger the addition. See
|
|
9
|
+
:class:`sage.symbolic.expression_conversion.SymPyConverter` for the
|
|
10
|
+
conversion to SymPy.
|
|
11
|
+
|
|
12
|
+
Only ``Function`` objects where the names differ need their own ``_sage()_``
|
|
13
|
+
method. There are several functions with differing name that have an alias
|
|
14
|
+
in Sage that is the same as the name in SymPy, so no explicit translation
|
|
15
|
+
is needed for them::
|
|
16
|
+
|
|
17
|
+
sage: from sympy import Symbol, Si, Ci, Shi, Chi, sign
|
|
18
|
+
sage: sx = Symbol('x')
|
|
19
|
+
sage: assert sin_integral(x)._sympy_() == Si(sx)
|
|
20
|
+
sage: assert sin_integral(x) == Si(sx)._sage_()
|
|
21
|
+
sage: assert sinh_integral(x)._sympy_() == Shi(sx)
|
|
22
|
+
sage: assert sinh_integral(x) == Shi(sx)._sage_()
|
|
23
|
+
sage: assert cos_integral(x)._sympy_() == Ci(sx)
|
|
24
|
+
sage: assert cos_integral(x) == Ci(sx)._sage_()
|
|
25
|
+
sage: assert cosh_integral(x)._sympy_() == Chi(sx)
|
|
26
|
+
sage: assert cosh_integral(x) == Chi(sx)._sage_()
|
|
27
|
+
sage: assert sgn(x)._sympy_() == sign(sx)
|
|
28
|
+
sage: assert sgn(x) == sign(sx)._sage_()
|
|
29
|
+
|
|
30
|
+
TESTS:
|
|
31
|
+
|
|
32
|
+
Check that :issue:`24212` is fixed::
|
|
33
|
+
|
|
34
|
+
sage: integrate(sin(x^2), x, algorithm='sympy')
|
|
35
|
+
3/8*sqrt(2)*sqrt(pi)*fresnel_sin(sqrt(2)*x/sqrt(pi))*gamma(3/4)/gamma(7/4)
|
|
36
|
+
|
|
37
|
+
Test that conversion of symbolic functions with latex names works (:issue:`31047`)::
|
|
38
|
+
|
|
39
|
+
sage: var('phi')
|
|
40
|
+
phi
|
|
41
|
+
sage: function('Cp', latex_name='C_+')
|
|
42
|
+
Cp
|
|
43
|
+
sage: test = Cp(phi)._sympy_()._sage_()
|
|
44
|
+
sage: test.operator() == Cp
|
|
45
|
+
True
|
|
46
|
+
sage: test.operator()._latex_() == 'C_+'
|
|
47
|
+
True
|
|
48
|
+
|
|
49
|
+
AUTHORS:
|
|
50
|
+
|
|
51
|
+
- Ralf Stephan (2017-10)
|
|
52
|
+
"""
|
|
53
|
+
################################################################
|
|
54
|
+
# Distributed under GNU GPL3, see www.gnu.org
|
|
55
|
+
################################################################
|
|
56
|
+
from sage.misc.misc import run_once
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# ################ numbers and constants ##############
|
|
60
|
+
|
|
61
|
+
def _sympysage_float(self):
|
|
62
|
+
"""
|
|
63
|
+
EXAMPLES::
|
|
64
|
+
|
|
65
|
+
sage: from sympy.core.numbers import RealNumber as RN
|
|
66
|
+
sage: assert SR(-1.34)._sympy_() == RN('-1.34')
|
|
67
|
+
sage: assert SR(-1.34) == RN('-1.34')._sage_()
|
|
68
|
+
"""
|
|
69
|
+
from sage.rings.real_mpfr import create_RealNumber
|
|
70
|
+
return create_RealNumber(str(self))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _sympysage_integer_ring(self):
|
|
74
|
+
r"""
|
|
75
|
+
EXAMPLES::
|
|
76
|
+
|
|
77
|
+
sage: import sympy
|
|
78
|
+
sage: sympy.ZZ._sage_()
|
|
79
|
+
Integer Ring
|
|
80
|
+
"""
|
|
81
|
+
from sage.rings.integer_ring import ZZ
|
|
82
|
+
return ZZ
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _sympysage_integer(self):
|
|
86
|
+
"""
|
|
87
|
+
EXAMPLES::
|
|
88
|
+
|
|
89
|
+
sage: from sympy.core.numbers import Integer as SympyInt
|
|
90
|
+
sage: assert SR(2)._sympy_() == SympyInt(int(2))
|
|
91
|
+
sage: assert SR(2) == SympyInt(int(2))._sage_()
|
|
92
|
+
sage: type(SympyInt(int(2))._sage_())
|
|
93
|
+
<class 'sage.rings.integer.Integer'>
|
|
94
|
+
"""
|
|
95
|
+
from sage.rings.integer import Integer
|
|
96
|
+
return Integer(self.p)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _sympysage_rational(self):
|
|
100
|
+
"""
|
|
101
|
+
EXAMPLES::
|
|
102
|
+
|
|
103
|
+
sage: from sympy.core.numbers import Rational
|
|
104
|
+
sage: assert SR(-5/7)._sympy_() == Rational(int(-5),int(7))
|
|
105
|
+
sage: assert SR(-5/7) == Rational(int(-5),int(7))._sage_()
|
|
106
|
+
"""
|
|
107
|
+
from sage.rings.integer import Integer
|
|
108
|
+
from sage.rings.rational import Rational
|
|
109
|
+
return Rational((Integer(self.p), Integer(self.q)))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _sympysage_rational_field(self):
|
|
113
|
+
r"""
|
|
114
|
+
EXAMPLES::
|
|
115
|
+
|
|
116
|
+
sage: import sympy
|
|
117
|
+
sage: sympy.QQ._sage_()
|
|
118
|
+
Rational Field
|
|
119
|
+
"""
|
|
120
|
+
from sage.rings.rational_field import QQ
|
|
121
|
+
return QQ
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _sympysage_real_interval(self):
|
|
125
|
+
r"""
|
|
126
|
+
EXAMPLES::
|
|
127
|
+
|
|
128
|
+
sage: from sage.interfaces.sympy import sympy_init
|
|
129
|
+
sage: sympy_init()
|
|
130
|
+
|
|
131
|
+
sage: from sympy import CRootOf
|
|
132
|
+
sage: from sympy.abc import x
|
|
133
|
+
sage: root = CRootOf(x**3 - x^2 - x - 1, 0)
|
|
134
|
+
sage: interval = root._get_interval()
|
|
135
|
+
sage: interval._sage_()
|
|
136
|
+
2.?
|
|
137
|
+
sage: interval._sage_().parent()
|
|
138
|
+
Real Interval Field with 1024 bits of precision
|
|
139
|
+
"""
|
|
140
|
+
# NOTE: this is a very approximate conversion as we do not consider any
|
|
141
|
+
# potential issue with precision
|
|
142
|
+
# Just to be (a little bit) safe, we set it to 1024
|
|
143
|
+
from sage.rings.real_mpfi import RealIntervalField
|
|
144
|
+
RIF = RealIntervalField(1024)
|
|
145
|
+
# NOTE: we call fraction_field since sympy stores mpq even
|
|
146
|
+
# for integral entries
|
|
147
|
+
domain = self.dom._sage_().fraction_field()
|
|
148
|
+
return RIF(domain(self.a)).union(RIF(domain(self.b)))
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _sympysage_complex_interval(self):
|
|
152
|
+
r"""
|
|
153
|
+
EXAMPLES::
|
|
154
|
+
|
|
155
|
+
sage: from sage.interfaces.sympy import sympy_init
|
|
156
|
+
sage: sympy_init()
|
|
157
|
+
|
|
158
|
+
sage: from sympy import CRootOf
|
|
159
|
+
sage: from sympy.abc import x
|
|
160
|
+
sage: root = CRootOf(x**10 - 2*x + 3, 9)
|
|
161
|
+
sage: interval = root._get_interval()
|
|
162
|
+
sage: interval._sage_()
|
|
163
|
+
0.1? + 1.2?*I
|
|
164
|
+
sage: interval._sage_().parent()
|
|
165
|
+
Complex Interval Field with 1024 bits of precision
|
|
166
|
+
"""
|
|
167
|
+
# NOTE: this is a very approximate conversion as we do not consider any
|
|
168
|
+
# potential issue with precision
|
|
169
|
+
# Just to be (a little bit) safe, we set it to 1024
|
|
170
|
+
from sage.rings.complex_interval_field import ComplexIntervalField
|
|
171
|
+
CIF = ComplexIntervalField(1024)
|
|
172
|
+
# NOTE: we call fraction_field since sympy stores mpq even
|
|
173
|
+
# for integral entries
|
|
174
|
+
domain = self.dom._sage_().fraction_field()
|
|
175
|
+
return CIF(domain(self.ax), domain(self.ay)).union(CIF(domain(self.bx), domain(self.by)))
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _sympysage_polynomial_ring(self):
|
|
179
|
+
r"""
|
|
180
|
+
EXAMPLES::
|
|
181
|
+
|
|
182
|
+
sage: from sage.interfaces.sympy import sympy_init
|
|
183
|
+
sage: sympy_init()
|
|
184
|
+
|
|
185
|
+
sage: import sympy
|
|
186
|
+
sage: ZZx = sympy.PolynomialRing(sympy.ZZ, 'x')
|
|
187
|
+
sage: ZZx._sage_()
|
|
188
|
+
Univariate Polynomial Ring in x over Integer Ring
|
|
189
|
+
|
|
190
|
+
sage: ZZxy = sympy.PolynomialRing(ZZx, 'y')
|
|
191
|
+
sage: ZZxy._sage_()
|
|
192
|
+
Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Integer Ring
|
|
193
|
+
"""
|
|
194
|
+
base_ring = self.domain._sage_()
|
|
195
|
+
variables = ','.join(map(str, self.gens))
|
|
196
|
+
return base_ring[variables]
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _sympysage_polynomial(self):
|
|
200
|
+
r"""
|
|
201
|
+
EXAMPLES::
|
|
202
|
+
|
|
203
|
+
sage: from sage.interfaces.sympy import sympy_init
|
|
204
|
+
sage: sympy_init()
|
|
205
|
+
|
|
206
|
+
sage: import sympy
|
|
207
|
+
sage: from sympy.abc import x, y
|
|
208
|
+
sage: p = sympy.Poly(x*(x**2 + x - 1)**2)
|
|
209
|
+
sage: p._sage_()
|
|
210
|
+
x^5 + 2*x^4 - x^3 - 2*x^2 + x
|
|
211
|
+
sage: p._sage_().parent()
|
|
212
|
+
Univariate Polynomial Ring in x over Integer Ring
|
|
213
|
+
|
|
214
|
+
sage: p = sympy.Poly(y*x**2 + x*y + 1)
|
|
215
|
+
sage: p._sage_()
|
|
216
|
+
x^2*y + x*y + 1
|
|
217
|
+
sage: p._sage_().parent()
|
|
218
|
+
Multivariate Polynomial Ring in x, y over Integer Ring
|
|
219
|
+
|
|
220
|
+
sage: p = sympy.Poly(y*x**2 + x*y + 1, x)
|
|
221
|
+
sage: p._sage_()
|
|
222
|
+
y*x^2 + y*x + 1
|
|
223
|
+
sage: p._sage_().parent()
|
|
224
|
+
Univariate Polynomial Ring in x over Univariate Polynomial Ring in y over Integer Ring
|
|
225
|
+
"""
|
|
226
|
+
base_ring = self.domain._sage_()
|
|
227
|
+
variables = ','.join(map(str, self.gens))
|
|
228
|
+
R = base_ring[variables]
|
|
229
|
+
return R.sum(base_ring(coeff) * R.monomial(*exp) for exp, coeff in self.rep.terms(order=None))
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _sympysage_pinfty(self):
|
|
233
|
+
"""
|
|
234
|
+
EXAMPLES::
|
|
235
|
+
|
|
236
|
+
sage: from sympy.core.numbers import oo as sinf
|
|
237
|
+
sage: assert SR(oo)._sympy_() == sinf
|
|
238
|
+
sage: assert SR(oo) == sinf._sage_()
|
|
239
|
+
"""
|
|
240
|
+
from sage.rings.infinity import PlusInfinity
|
|
241
|
+
return PlusInfinity()
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _sympysage_ninfty(self):
|
|
245
|
+
"""
|
|
246
|
+
EXAMPLES::
|
|
247
|
+
|
|
248
|
+
sage: from sympy.core.numbers import oo as sinf
|
|
249
|
+
sage: assert SR(-oo)._sympy_() == -sinf
|
|
250
|
+
sage: assert SR(-oo) == (-sinf)._sage_()
|
|
251
|
+
"""
|
|
252
|
+
from sage.rings.infinity import MinusInfinity
|
|
253
|
+
return MinusInfinity()
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _sympysage_uinfty(self):
|
|
257
|
+
"""
|
|
258
|
+
EXAMPLES::
|
|
259
|
+
|
|
260
|
+
sage: from sympy.core.numbers import zoo
|
|
261
|
+
sage: assert unsigned_infinity._sympy_() == zoo
|
|
262
|
+
sage: assert unsigned_infinity == zoo._sage_()
|
|
263
|
+
"""
|
|
264
|
+
from sage.rings.infinity import unsigned_infinity
|
|
265
|
+
return unsigned_infinity
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _sympysage_nan(self):
|
|
269
|
+
"""
|
|
270
|
+
EXAMPLES::
|
|
271
|
+
|
|
272
|
+
sage: from sympy.core.numbers import nan as snan
|
|
273
|
+
sage: assert NaN._sympy_() == snan
|
|
274
|
+
sage: assert NaN == snan._sage_()
|
|
275
|
+
"""
|
|
276
|
+
from sage.symbolic.constants import NaN
|
|
277
|
+
return NaN
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _sympysage_e(self):
|
|
281
|
+
"""
|
|
282
|
+
EXAMPLES::
|
|
283
|
+
|
|
284
|
+
sage: from sympy.core.numbers import E
|
|
285
|
+
sage: assert e._sympy_() == E
|
|
286
|
+
sage: assert e == E._sage_()
|
|
287
|
+
"""
|
|
288
|
+
from sage.symbolic.constants import e
|
|
289
|
+
return e
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _sympysage_pi(self):
|
|
293
|
+
"""
|
|
294
|
+
EXAMPLES::
|
|
295
|
+
|
|
296
|
+
sage: from sympy.core.numbers import pi as spi
|
|
297
|
+
sage: assert pi._sympy_() == spi
|
|
298
|
+
sage: assert pi == spi._sage_()
|
|
299
|
+
"""
|
|
300
|
+
from sage.symbolic.constants import pi
|
|
301
|
+
return pi
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _sympysage_golden_ratio(self):
|
|
305
|
+
"""
|
|
306
|
+
EXAMPLES::
|
|
307
|
+
|
|
308
|
+
sage: from sympy.core.singleton import S
|
|
309
|
+
sage: assert golden_ratio._sympy_() == S.GoldenRatio
|
|
310
|
+
sage: assert golden_ratio == S.GoldenRatio._sage_()
|
|
311
|
+
"""
|
|
312
|
+
from sage.symbolic.constants import golden_ratio
|
|
313
|
+
return golden_ratio
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _sympysage_eulerg(self):
|
|
317
|
+
"""
|
|
318
|
+
EXAMPLES::
|
|
319
|
+
|
|
320
|
+
sage: from sympy.core.singleton import S
|
|
321
|
+
sage: assert euler_gamma._sympy_() == S.EulerGamma
|
|
322
|
+
sage: assert euler_gamma == S.EulerGamma._sage_()
|
|
323
|
+
"""
|
|
324
|
+
from sage.symbolic.constants import euler_gamma
|
|
325
|
+
return euler_gamma
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _sympysage_catalan(self):
|
|
329
|
+
"""
|
|
330
|
+
EXAMPLES::
|
|
331
|
+
|
|
332
|
+
sage: from sympy.core.singleton import S
|
|
333
|
+
sage: assert catalan._sympy_() == S.Catalan
|
|
334
|
+
sage: assert catalan == S.Catalan._sage_()
|
|
335
|
+
"""
|
|
336
|
+
from sage.symbolic.constants import catalan
|
|
337
|
+
return catalan
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def _sympysage_i(self):
|
|
341
|
+
"""
|
|
342
|
+
EXAMPLES::
|
|
343
|
+
|
|
344
|
+
sage: from sympy.core.singleton import S
|
|
345
|
+
sage: assert I._sympy_() == S.ImaginaryUnit
|
|
346
|
+
sage: assert I == S.ImaginaryUnit._sage_()
|
|
347
|
+
"""
|
|
348
|
+
from sage.symbolic.constants import I
|
|
349
|
+
return I
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
# ################# basic operators ##############
|
|
353
|
+
|
|
354
|
+
def _sympysage_add(self):
|
|
355
|
+
"""
|
|
356
|
+
EXAMPLES::
|
|
357
|
+
|
|
358
|
+
sage: from sympy import Symbol
|
|
359
|
+
sage: from sympy.core.singleton import S
|
|
360
|
+
sage: assert (x-pi+1)._sympy_() == Symbol('x')-S.Pi+1
|
|
361
|
+
sage: assert x-pi+1 == (Symbol('x')-S.Pi+1)._sage_()
|
|
362
|
+
"""
|
|
363
|
+
s = 0
|
|
364
|
+
for x in self.args:
|
|
365
|
+
s += x._sage_()
|
|
366
|
+
return s
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _sympysage_mul(self):
|
|
370
|
+
"""
|
|
371
|
+
EXAMPLES::
|
|
372
|
+
|
|
373
|
+
sage: from sympy import Symbol
|
|
374
|
+
sage: from sympy.core.singleton import S
|
|
375
|
+
sage: assert (-x*pi*5)._sympy_() == -Symbol('x')*S.Pi*5
|
|
376
|
+
sage: assert -x*pi*5 == (-Symbol('x')*S.Pi*5)._sage_()
|
|
377
|
+
"""
|
|
378
|
+
s = 1
|
|
379
|
+
for x in self.args:
|
|
380
|
+
s *= x._sage_()
|
|
381
|
+
return s
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _sympysage_pow(self):
|
|
385
|
+
"""
|
|
386
|
+
EXAMPLES::
|
|
387
|
+
|
|
388
|
+
sage: from sympy import Symbol
|
|
389
|
+
sage: from sympy.core.singleton import S
|
|
390
|
+
sage: assert (x^pi^5)._sympy_() == Symbol('x')**S.Pi**5
|
|
391
|
+
sage: assert x^pi^5 == (Symbol('x')**S.Pi**5)._sage_()
|
|
392
|
+
"""
|
|
393
|
+
return self.args[0]._sage_()**self.args[1]._sage_()
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def _sympysage_symbol(self):
|
|
397
|
+
"""
|
|
398
|
+
EXAMPLES::
|
|
399
|
+
|
|
400
|
+
sage: from sympy import Symbol
|
|
401
|
+
sage: assert x._sympy_() == Symbol('x')
|
|
402
|
+
sage: assert x == Symbol('x')._sage_()
|
|
403
|
+
"""
|
|
404
|
+
from sage.symbolic.ring import SR
|
|
405
|
+
try:
|
|
406
|
+
return SR.var(self.name)
|
|
407
|
+
except ValueError:
|
|
408
|
+
# sympy sometimes returns dummy variables
|
|
409
|
+
# with name = 'None', str rep = '_None'
|
|
410
|
+
# in particular in inverse Laplace and inverse Mellin transforms
|
|
411
|
+
return SR.var(str(self))
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def _sympysage_Subs(self):
|
|
415
|
+
"""
|
|
416
|
+
EXAMPLES::
|
|
417
|
+
|
|
418
|
+
sage: from sympy import Symbol
|
|
419
|
+
sage: from sympy.core.singleton import S
|
|
420
|
+
"""
|
|
421
|
+
args = self.args
|
|
422
|
+
substi = {args[1][i]._sage_(): args[2][i]._sage_()
|
|
423
|
+
for i in range(len(args[1]))}
|
|
424
|
+
return args[0]._sage_().subs(substi)
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
# ############# functions ###############
|
|
428
|
+
|
|
429
|
+
def _sympysage_function_by_name(fname):
|
|
430
|
+
"""
|
|
431
|
+
Given a sympy function with name ``fname`` find the corresponding
|
|
432
|
+
sage function or create a new one with the given name.
|
|
433
|
+
|
|
434
|
+
EXAMPLES::
|
|
435
|
+
|
|
436
|
+
sage: from sympy import Function
|
|
437
|
+
sage: f = function('f')
|
|
438
|
+
sage: F = Function('f')
|
|
439
|
+
sage: assert f._sympy_() == F
|
|
440
|
+
sage: assert f == F._sage_()
|
|
441
|
+
"""
|
|
442
|
+
from sage.functions import all as sagefuncs
|
|
443
|
+
func = getattr(sagefuncs, fname, None)
|
|
444
|
+
# In the case the function is not known in sage:
|
|
445
|
+
if func is None:
|
|
446
|
+
import sympy
|
|
447
|
+
if getattr(sympy, fname, None) is None:
|
|
448
|
+
# symbolic function
|
|
449
|
+
from sage.symbolic.expression import symbol_table
|
|
450
|
+
func = symbol_table['functions'].get(fname)
|
|
451
|
+
if func is None:
|
|
452
|
+
from sage.calculus.var import function
|
|
453
|
+
return function(fname)
|
|
454
|
+
|
|
455
|
+
else:
|
|
456
|
+
# the function defined in sympy is not known in sage
|
|
457
|
+
raise AttributeError
|
|
458
|
+
return func
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
# the convoluted class structure with metaclasses and stuff sympy uses
|
|
462
|
+
# to implement undefined functions makes things a bit harder for us
|
|
463
|
+
# here
|
|
464
|
+
class UndefSageHelper:
|
|
465
|
+
"""
|
|
466
|
+
Helper class to convert sympy function objects to sage functions
|
|
467
|
+
|
|
468
|
+
EXAMPLES::
|
|
469
|
+
|
|
470
|
+
sage: from sympy import Function
|
|
471
|
+
sage: f = function('f')
|
|
472
|
+
sage: F = Function('f')
|
|
473
|
+
sage: assert f._sympy_() == F
|
|
474
|
+
sage: assert f == F._sage_()
|
|
475
|
+
"""
|
|
476
|
+
def __get__(self, ins, typ):
|
|
477
|
+
if ins is None:
|
|
478
|
+
return lambda: _sympysage_function_by_name(typ.__name__)
|
|
479
|
+
else:
|
|
480
|
+
args = [arg._sage_() for arg in ins.args]
|
|
481
|
+
return lambda: _sympysage_function_by_name(ins.__class__.__name__)(*args)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
def _sympysage_function(self):
|
|
485
|
+
"""
|
|
486
|
+
EXAMPLES::
|
|
487
|
+
|
|
488
|
+
sage: from sympy import Symbol, Function, sin as Sin
|
|
489
|
+
sage: assert sin(x)._sympy_() == Sin(Symbol('x'))
|
|
490
|
+
sage: assert sin(x) == Sin(Symbol('x'))._sage_()
|
|
491
|
+
|
|
492
|
+
sage: f = function('f')
|
|
493
|
+
sage: F = Function('f')
|
|
494
|
+
sage: assert f(x)._sympy_() == F(x)
|
|
495
|
+
sage: assert f(x) == F(x)._sage_()
|
|
496
|
+
sage: assert f(x+3)._sympy_() == F(x+3)
|
|
497
|
+
sage: assert f(x+3) == F(x+3)._sage_()
|
|
498
|
+
sage: assert (3*f(x))._sympy_() == 3*F(x)
|
|
499
|
+
sage: assert 3*f(x) == (3*F(x))._sage_()
|
|
500
|
+
|
|
501
|
+
Test that functions unknown to Sage raise an exception::
|
|
502
|
+
|
|
503
|
+
sage: from sympy.functions.combinatorial.numbers import lucas
|
|
504
|
+
sage: lucas(Symbol('x'))._sage_()
|
|
505
|
+
Traceback (most recent call last):
|
|
506
|
+
...
|
|
507
|
+
AttributeError...
|
|
508
|
+
"""
|
|
509
|
+
fname = self.func.__name__
|
|
510
|
+
func = _sympysage_function_by_name(fname)
|
|
511
|
+
args = [arg._sage_() for arg in self.args]
|
|
512
|
+
|
|
513
|
+
return func(*args)
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def _sympysage_integral(self):
|
|
517
|
+
"""
|
|
518
|
+
EXAMPLES::
|
|
519
|
+
|
|
520
|
+
sage: from sympy import Symbol, Integral
|
|
521
|
+
sage: sx = Symbol('x')
|
|
522
|
+
sage: assert integral(x, x, hold=True)._sympy_() == Integral(sx, sx)
|
|
523
|
+
sage: assert integral(x, x, hold=True) == Integral(sx, sx)._sage_()
|
|
524
|
+
sage: assert integral(x, x, 0, 1, hold=True)._sympy_() == Integral(sx, (sx,0,1))
|
|
525
|
+
sage: assert integral(x, x, 0, 1, hold=True) == Integral(sx, (sx,0,1))._sage_()
|
|
526
|
+
"""
|
|
527
|
+
from sage.misc.functional import integral
|
|
528
|
+
f, limits = self.function._sage_(), list(self.limits)
|
|
529
|
+
for limit in limits:
|
|
530
|
+
if len(limit) == 1:
|
|
531
|
+
x = limit[0]
|
|
532
|
+
f = integral(f, x._sage_(), hold=True)
|
|
533
|
+
elif len(limit) == 2:
|
|
534
|
+
x, b = limit
|
|
535
|
+
f = integral(f, x._sage_(), b._sage_(), hold=True)
|
|
536
|
+
else:
|
|
537
|
+
x, a, b = limit
|
|
538
|
+
f = integral(f, (x._sage_(), a._sage_(), b._sage_()), hold=True)
|
|
539
|
+
return f
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
def _sympysage_derivative(self):
|
|
543
|
+
"""
|
|
544
|
+
EXAMPLES::
|
|
545
|
+
|
|
546
|
+
sage: from sympy import Derivative
|
|
547
|
+
sage: f = function('f')
|
|
548
|
+
sage: sympy_diff = Derivative(f(x)._sympy_(), x._sympy_())
|
|
549
|
+
sage: assert diff(f(x),x)._sympy_() == sympy_diff
|
|
550
|
+
sage: assert diff(f(x),x) == sympy_diff._sage_()
|
|
551
|
+
|
|
552
|
+
TESTS:
|
|
553
|
+
|
|
554
|
+
Check that :issue:`28964` is fixed::
|
|
555
|
+
|
|
556
|
+
sage: f = function('f')
|
|
557
|
+
sage: _ = var('x,t')
|
|
558
|
+
sage: assert diff(f(x, t), t)._sympy_()._sage_() == diff(f(x, t), t)
|
|
559
|
+
sage: assert diff(f(x, t), x, 2, t)._sympy_()._sage_() == diff(f(x, t), x, 2, t)
|
|
560
|
+
|
|
561
|
+
sage: diff(f(x, t), x).integrate(x)
|
|
562
|
+
f(x, t)
|
|
563
|
+
sage: diff(f(x, t), x).integrate(t, algorithm='maxima')
|
|
564
|
+
integrate(diff(f(x, t), x), t)
|
|
565
|
+
sage: diff(f(x, t), x).integrate(t, algorithm='sympy')
|
|
566
|
+
integrate(diff(f(x, t), x), t)
|
|
567
|
+
sage: result = integrate(f(x, t), x).diff(t)
|
|
568
|
+
...
|
|
569
|
+
sage: result
|
|
570
|
+
integrate(diff(f(x, t), t), x)
|
|
571
|
+
"""
|
|
572
|
+
from sage.calculus.functional import derivative
|
|
573
|
+
from sympy.core.containers import Tuple
|
|
574
|
+
f = self.args[0]._sage_()
|
|
575
|
+
args = [a._sage_() for arg in self.args[1:]
|
|
576
|
+
for a in (arg if isinstance(arg, (tuple, Tuple)) else [arg])]
|
|
577
|
+
return derivative(f, *args)
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
def _sympysage_order(self):
|
|
581
|
+
"""
|
|
582
|
+
EXAMPLES::
|
|
583
|
+
|
|
584
|
+
sage: from sage.functions.other import Order
|
|
585
|
+
sage: from sympy.series import Order as SOrder
|
|
586
|
+
sage: assert Order(1)._sympy_() == SOrder(1)
|
|
587
|
+
sage: assert Order(1) == SOrder(1)._sage_()
|
|
588
|
+
"""
|
|
589
|
+
from sage.functions.other import Order
|
|
590
|
+
return Order(self.args[0])._sage_()
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def _sympysage_lambertw(self):
|
|
594
|
+
"""
|
|
595
|
+
EXAMPLES::
|
|
596
|
+
|
|
597
|
+
sage: from sympy import Symbol, LambertW
|
|
598
|
+
sage: assert lambert_w(x)._sympy_() == LambertW(0, Symbol('x'))
|
|
599
|
+
sage: assert lambert_w(x) == LambertW(Symbol('x'))._sage_()
|
|
600
|
+
"""
|
|
601
|
+
from sage.functions.log import lambert_w
|
|
602
|
+
return lambert_w(self.args[0]._sage_())
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
def _sympysage_rf(self):
|
|
606
|
+
"""
|
|
607
|
+
EXAMPLES::
|
|
608
|
+
|
|
609
|
+
sage: from sympy import Symbol, rf
|
|
610
|
+
sage: _ = var('x, y')
|
|
611
|
+
sage: rfxy = rf(Symbol('x'), Symbol('y'))
|
|
612
|
+
sage: assert rising_factorial(x,y)._sympy_() == rfxy.rewrite('gamma', piecewise=False)
|
|
613
|
+
sage: assert rising_factorial(x,y) == rfxy._sage_()
|
|
614
|
+
"""
|
|
615
|
+
from sage.arith.misc import rising_factorial
|
|
616
|
+
return rising_factorial(self.args[0]._sage_(), self.args[1]._sage_())
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
def _sympysage_ff(self):
|
|
620
|
+
"""
|
|
621
|
+
EXAMPLES::
|
|
622
|
+
|
|
623
|
+
sage: from sympy import Symbol, ff
|
|
624
|
+
sage: _ = var('x, y')
|
|
625
|
+
sage: ffxy = ff(Symbol('x'), Symbol('y'))
|
|
626
|
+
sage: assert falling_factorial(x,y)._sympy_() == ffxy.rewrite('gamma') # known bug
|
|
627
|
+
sage: assert falling_factorial(x,y) == ffxy._sage_()
|
|
628
|
+
"""
|
|
629
|
+
from sage.arith.misc import falling_factorial
|
|
630
|
+
return falling_factorial(self.args[0]._sage_(), self.args[1]._sage_())
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
def _sympysage_lgamma(self):
|
|
634
|
+
"""
|
|
635
|
+
EXAMPLES::
|
|
636
|
+
|
|
637
|
+
sage: from sympy import Symbol, loggamma
|
|
638
|
+
sage: assert log_gamma(x)._sympy_() == loggamma(Symbol('x'))
|
|
639
|
+
sage: assert log_gamma(x) == loggamma(Symbol('x'))._sage_()
|
|
640
|
+
"""
|
|
641
|
+
from sage.functions.gamma import log_gamma
|
|
642
|
+
return log_gamma(self.args[0]._sage_())
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
def _sympysage_polygamma(self):
|
|
646
|
+
"""
|
|
647
|
+
EXAMPLES::
|
|
648
|
+
|
|
649
|
+
sage: from sympy import Symbol, polygamma as pg
|
|
650
|
+
sage: _ = var('x, y')
|
|
651
|
+
sage: pgxy = pg(Symbol('x'), Symbol('y'))
|
|
652
|
+
sage: assert psi(x)._sympy_() == pg(0, Symbol('x'))
|
|
653
|
+
sage: assert psi(x) == pg(0, Symbol('x'))._sage_()
|
|
654
|
+
sage: assert psi(x,y)._sympy_() == pgxy
|
|
655
|
+
sage: assert psi(x,y) == pgxy._sage_()
|
|
656
|
+
sage: integrate(psi(x), x, algorithm='sympy')
|
|
657
|
+
integrate(psi(x), x)
|
|
658
|
+
"""
|
|
659
|
+
from sage.functions.gamma import psi
|
|
660
|
+
return psi(self.args[0]._sage_(), self.args[1]._sage_())
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
def _sympysage_dirac_delta(self):
|
|
664
|
+
"""
|
|
665
|
+
EXAMPLES::
|
|
666
|
+
|
|
667
|
+
sage: from sympy import Symbol, DiracDelta
|
|
668
|
+
sage: assert dirac_delta(x)._sympy_() == DiracDelta(Symbol('x'))
|
|
669
|
+
sage: assert dirac_delta(x) == DiracDelta(Symbol('x'))._sage_()
|
|
670
|
+
"""
|
|
671
|
+
from sage.functions.generalized import dirac_delta
|
|
672
|
+
return dirac_delta(self.args[0]._sage_())
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
def _sympysage_heaviside(self):
|
|
676
|
+
"""
|
|
677
|
+
EXAMPLES::
|
|
678
|
+
|
|
679
|
+
sage: from sympy import Symbol, Heaviside
|
|
680
|
+
sage: assert heaviside(x)._sympy_() == Heaviside(Symbol('x'))
|
|
681
|
+
sage: assert heaviside(x) == Heaviside(Symbol('x'))._sage_()
|
|
682
|
+
"""
|
|
683
|
+
from sage.functions.generalized import heaviside
|
|
684
|
+
return heaviside(self.args[0]._sage_())
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
def _sympysage_expint(self):
|
|
688
|
+
"""
|
|
689
|
+
EXAMPLES::
|
|
690
|
+
|
|
691
|
+
sage: from sympy import Symbol, expint
|
|
692
|
+
sage: _ = var('x, y')
|
|
693
|
+
sage: sy = expint(Symbol('x'), Symbol('y'))
|
|
694
|
+
sage: assert exp_integral_e(x,y)._sympy_() == sy
|
|
695
|
+
sage: assert exp_integral_e(x,y) == sy._sage_()
|
|
696
|
+
"""
|
|
697
|
+
from sage.functions.exp_integral import exp_integral_e
|
|
698
|
+
return exp_integral_e(self.args[0]._sage_(), self.args[1]._sage_())
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
def _sympysage_hyp(self):
|
|
702
|
+
"""
|
|
703
|
+
EXAMPLES::
|
|
704
|
+
|
|
705
|
+
sage: from sympy import Symbol, hyper
|
|
706
|
+
sage: _ = var('a,b,p,q,x')
|
|
707
|
+
sage: sy = hyper((Symbol('a'), Symbol('b')), (Symbol('p'), Symbol('q')), Symbol('x'))
|
|
708
|
+
sage: assert hypergeometric((a,b),(p,q),x)._sympy_() == sy
|
|
709
|
+
sage: assert hypergeometric((a,b),(p,q),x) == sy._sage_()
|
|
710
|
+
"""
|
|
711
|
+
from sage.functions.hypergeometric import hypergeometric
|
|
712
|
+
ap = [arg._sage_() for arg in self.args[0]]
|
|
713
|
+
bq = [arg._sage_() for arg in self.args[1]]
|
|
714
|
+
return hypergeometric(ap, bq, self.argument._sage_())
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
def _sympysage_elliptic_k(self):
|
|
718
|
+
"""
|
|
719
|
+
EXAMPLES::
|
|
720
|
+
|
|
721
|
+
sage: from sympy import Symbol, elliptic_k
|
|
722
|
+
sage: assert elliptic_kc(x)._sympy_() == elliptic_k(Symbol('x'))
|
|
723
|
+
sage: assert elliptic_kc(x) == elliptic_k(Symbol('x'))._sage_()
|
|
724
|
+
"""
|
|
725
|
+
from sage.functions.special import elliptic_kc
|
|
726
|
+
return elliptic_kc(self.args[0]._sage_())
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
def _sympysage_kronecker_delta(self):
|
|
730
|
+
"""
|
|
731
|
+
EXAMPLES::
|
|
732
|
+
|
|
733
|
+
sage: from sympy import Symbol, KroneckerDelta
|
|
734
|
+
sage: _ = var('x, y')
|
|
735
|
+
sage: sy = KroneckerDelta(Symbol('x'), Symbol('y'))
|
|
736
|
+
sage: assert kronecker_delta(x,y)._sympy_() == sy
|
|
737
|
+
sage: assert kronecker_delta(x,y) == sy._sage_()
|
|
738
|
+
"""
|
|
739
|
+
from sage.functions.generalized import kronecker_delta
|
|
740
|
+
return kronecker_delta(self.args[0]._sage_(), self.args[1]._sage_())
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
def _sympysage_ceiling(self):
|
|
744
|
+
"""
|
|
745
|
+
EXAMPLES::
|
|
746
|
+
|
|
747
|
+
sage: from sympy import Symbol, ceiling
|
|
748
|
+
sage: assert ceil(x)._sympy_() == ceiling(Symbol('x'))
|
|
749
|
+
sage: assert ceil(x) == ceiling(Symbol('x'))._sage_()
|
|
750
|
+
sage: integrate(ceil(x), x, 0, infinity, algorithm='sympy')
|
|
751
|
+
integrate(ceil(x), x, 0, +Infinity)
|
|
752
|
+
"""
|
|
753
|
+
from sage.functions.other import ceil
|
|
754
|
+
return ceil(self.args[0]._sage_())
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
def _sympysage_piecewise(self):
|
|
758
|
+
"""
|
|
759
|
+
EXAMPLES::
|
|
760
|
+
|
|
761
|
+
sage: from sympy import Symbol, pi as spi, Eq, Lt, Piecewise
|
|
762
|
+
sage: sx = Symbol('x')
|
|
763
|
+
sage: sp = Piecewise((spi, Lt(sx,0)), (1, Eq(sx,1)), (0, True))
|
|
764
|
+
sage: ex = cases(((x<0, pi), (x==1, 1), (True, 0)))
|
|
765
|
+
sage: assert ex._sympy_() == sp
|
|
766
|
+
sage: assert ex == sp._sage_()
|
|
767
|
+
|
|
768
|
+
sage: _ = var('y, z')
|
|
769
|
+
sage: (x^y - z).integrate(y, algorithm='sympy')
|
|
770
|
+
-y*z + cases(((log(x) != 0, x^y/log(x)), (1, y)))
|
|
771
|
+
"""
|
|
772
|
+
from sage.functions.other import cases
|
|
773
|
+
return cases([(p.cond._sage_(), p.expr._sage_()) for p in self.args])
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
def _sympysage_fresnels(self):
|
|
777
|
+
"""
|
|
778
|
+
EXAMPLES::
|
|
779
|
+
|
|
780
|
+
sage: from sympy import Symbol, pi as spi, fresnels
|
|
781
|
+
sage: sx = Symbol('x')
|
|
782
|
+
sage: sp = fresnels(sx)
|
|
783
|
+
sage: ex = fresnel_sin(x)
|
|
784
|
+
sage: assert ex._sympy_() == sp
|
|
785
|
+
sage: assert ex == sp._sage_()
|
|
786
|
+
"""
|
|
787
|
+
from sage.functions.error import fresnel_sin
|
|
788
|
+
return fresnel_sin(self.args[0]._sage_())
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
def _sympysage_fresnelc(self):
|
|
792
|
+
"""
|
|
793
|
+
EXAMPLES::
|
|
794
|
+
|
|
795
|
+
sage: from sympy import Symbol, pi as spi, fresnelc
|
|
796
|
+
sage: sx = Symbol('x')
|
|
797
|
+
sage: sp = fresnelc(sx)
|
|
798
|
+
sage: ex = fresnel_cos(x)
|
|
799
|
+
sage: assert ex._sympy_() == sp
|
|
800
|
+
sage: assert ex == sp._sage_()
|
|
801
|
+
"""
|
|
802
|
+
from sage.functions.error import fresnel_cos
|
|
803
|
+
return fresnel_cos(self.args[0]._sage_())
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
def _sympysage_besselj(self):
|
|
807
|
+
"""
|
|
808
|
+
EXAMPLES::
|
|
809
|
+
|
|
810
|
+
sage: from sympy import Symbol, besselj
|
|
811
|
+
sage: _ = var('x, y')
|
|
812
|
+
sage: sy = besselj(Symbol('x'), Symbol('y'))
|
|
813
|
+
sage: assert bessel_J(x,y)._sympy_() == sy
|
|
814
|
+
sage: assert bessel_J(x,y) == sy._sage_()
|
|
815
|
+
"""
|
|
816
|
+
from sage.functions.bessel import bessel_J
|
|
817
|
+
return bessel_J(self.args[0]._sage_(), self.args[1]._sage_())
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
def _sympysage_bessely(self):
|
|
821
|
+
"""
|
|
822
|
+
EXAMPLES::
|
|
823
|
+
|
|
824
|
+
sage: from sympy import Symbol, bessely
|
|
825
|
+
sage: _ = var('x, y')
|
|
826
|
+
sage: sy = bessely(Symbol('x'), Symbol('y'))
|
|
827
|
+
sage: assert bessel_Y(x,y)._sympy_() == sy
|
|
828
|
+
sage: assert bessel_Y(x,y) == sy._sage_()
|
|
829
|
+
"""
|
|
830
|
+
from sage.functions.bessel import bessel_Y
|
|
831
|
+
return bessel_Y(self.args[0]._sage_(), self.args[1]._sage_())
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
def _sympysage_besseli(self):
|
|
835
|
+
"""
|
|
836
|
+
EXAMPLES::
|
|
837
|
+
|
|
838
|
+
sage: from sympy import Symbol, besseli
|
|
839
|
+
sage: _ = var('x, y')
|
|
840
|
+
sage: sy = besseli(Symbol('x'), Symbol('y'))
|
|
841
|
+
sage: assert bessel_I(x,y)._sympy_() == sy
|
|
842
|
+
sage: assert bessel_I(x,y) == sy._sage_()
|
|
843
|
+
"""
|
|
844
|
+
from sage.functions.bessel import bessel_I
|
|
845
|
+
return bessel_I(self.args[0]._sage_(), self.args[1]._sage_())
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
def _sympysage_besselk(self):
|
|
849
|
+
"""
|
|
850
|
+
EXAMPLES::
|
|
851
|
+
|
|
852
|
+
sage: from sympy import Symbol, besselk
|
|
853
|
+
sage: _ = var('x, y')
|
|
854
|
+
sage: sy = besselk(Symbol('x'), Symbol('y'))
|
|
855
|
+
sage: assert bessel_K(x,y)._sympy_() == sy
|
|
856
|
+
sage: assert bessel_K(x,y) == sy._sage_()
|
|
857
|
+
"""
|
|
858
|
+
from sage.functions.bessel import bessel_K
|
|
859
|
+
return bessel_K(self.args[0]._sage_(), self.args[1]._sage_())
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
def _sympysage_ynm(self):
|
|
863
|
+
"""
|
|
864
|
+
EXAMPLES::
|
|
865
|
+
|
|
866
|
+
sage: from sympy import Symbol, Ynm
|
|
867
|
+
sage: _ = var('n,m,t,p')
|
|
868
|
+
sage: sy = Ynm(Symbol('n'), Symbol('m'), Symbol('t'), Symbol('p'))
|
|
869
|
+
sage: assert spherical_harmonic(n,m,t,p)._sympy_() == sy
|
|
870
|
+
sage: assert spherical_harmonic(n,m,t,p) == sy._sage_()
|
|
871
|
+
"""
|
|
872
|
+
from sage.functions.special import spherical_harmonic
|
|
873
|
+
return spherical_harmonic(self.args[0]._sage_(),
|
|
874
|
+
self.args[1]._sage_(),
|
|
875
|
+
self.args[2]._sage_(),
|
|
876
|
+
self.args[3]._sage_())
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
def _sympysage_re(self):
|
|
880
|
+
"""
|
|
881
|
+
EXAMPLES::
|
|
882
|
+
|
|
883
|
+
sage: from sympy import Symbol, re
|
|
884
|
+
sage: assert real_part(x)._sympy_() == re(Symbol('x'))
|
|
885
|
+
sage: assert real_part(x) == re(Symbol('x'))._sage_()
|
|
886
|
+
"""
|
|
887
|
+
from sage.functions.other import real_part
|
|
888
|
+
return real_part(self.args[0]._sage_())
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
def _sympysage_im(self):
|
|
892
|
+
"""
|
|
893
|
+
EXAMPLES::
|
|
894
|
+
|
|
895
|
+
sage: from sympy import Symbol, im
|
|
896
|
+
sage: assert imag_part(x)._sympy_() == im(Symbol('x'))
|
|
897
|
+
sage: assert imag_part(x) == im(Symbol('x'))._sage_()
|
|
898
|
+
"""
|
|
899
|
+
from sage.functions.other import imag_part
|
|
900
|
+
return imag_part(self.args[0]._sage_())
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
def _sympysage_abs(self):
|
|
904
|
+
"""
|
|
905
|
+
EXAMPLES::
|
|
906
|
+
|
|
907
|
+
sage: from sympy import Symbol, Abs
|
|
908
|
+
sage: assert abs(x)._sympy_() == Abs(Symbol('x'))
|
|
909
|
+
sage: assert abs(x) == Abs(Symbol('x'))._sage_()
|
|
910
|
+
"""
|
|
911
|
+
from sage.functions.other import abs_symbolic
|
|
912
|
+
return abs_symbolic(self.args[0]._sage_())
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
def _sympysage_crootof(self):
|
|
916
|
+
"""
|
|
917
|
+
EXAMPLES::
|
|
918
|
+
|
|
919
|
+
sage: from sympy import Symbol, CRootOf
|
|
920
|
+
sage: sobj = CRootOf(Symbol('x')**2 - 2, 1)
|
|
921
|
+
sage: assert complex_root_of(x^2-2, 1)._sympy_() == sobj
|
|
922
|
+
sage: assert complex_root_of(x^2-2, 1) == sobj._sage_()
|
|
923
|
+
|
|
924
|
+
sage: from sympy import solve as ssolve
|
|
925
|
+
sage: sols = ssolve(x^6+x+1, x)
|
|
926
|
+
sage: (sols[0]+1)._sage_().n()
|
|
927
|
+
0.209332811185582 - 0.300506920309552*I
|
|
928
|
+
"""
|
|
929
|
+
from sage.functions.other import complex_root_of
|
|
930
|
+
from sage.symbolic.ring import SR
|
|
931
|
+
return complex_root_of(self.args[0]._sage_(), SR(self.args[1]))
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
def _sympysage_matrix(self):
|
|
935
|
+
"""
|
|
936
|
+
Convert SymPy matrix ``self`` to Sage.
|
|
937
|
+
|
|
938
|
+
EXAMPLES::
|
|
939
|
+
|
|
940
|
+
sage: from sympy.matrices import Matrix, SparseMatrix, ImmutableMatrix
|
|
941
|
+
sage: from sage.interfaces.sympy import sympy_init
|
|
942
|
+
sage: from sympy.abc import x
|
|
943
|
+
sage: sympy_init()
|
|
944
|
+
sage: sM = Matrix([[1, x + 1], [x - 1, 1]]); sM
|
|
945
|
+
Matrix([
|
|
946
|
+
[ 1, x + 1],
|
|
947
|
+
[x - 1, 1]])
|
|
948
|
+
sage: M = sM._sage_(); M
|
|
949
|
+
[ 1 x + 1]
|
|
950
|
+
[x - 1 1]
|
|
951
|
+
sage: M.parent()
|
|
952
|
+
Full MatrixSpace of 2 by 2 dense matrices over Symbolic Ring
|
|
953
|
+
|
|
954
|
+
sage: sN = SparseMatrix.eye(3); sN
|
|
955
|
+
Matrix([
|
|
956
|
+
[1, 0, 0],
|
|
957
|
+
[0, 1, 0],
|
|
958
|
+
[0, 0, 1]])
|
|
959
|
+
sage: N = sN._sage_(); N
|
|
960
|
+
[1 0 0]
|
|
961
|
+
[0 1 0]
|
|
962
|
+
[0 0 1]
|
|
963
|
+
sage: N.parent()
|
|
964
|
+
Full MatrixSpace of 3 by 3 sparse matrices over Integer Ring
|
|
965
|
+
|
|
966
|
+
sage: sO = SparseMatrix.zeros(3); sO
|
|
967
|
+
Matrix([
|
|
968
|
+
[0, 0, 0],
|
|
969
|
+
[0, 0, 0],
|
|
970
|
+
[0, 0, 0]])
|
|
971
|
+
sage: O = sO._sage_(); O
|
|
972
|
+
[0 0 0]
|
|
973
|
+
[0 0 0]
|
|
974
|
+
[0 0 0]
|
|
975
|
+
sage: O.parent()
|
|
976
|
+
Full MatrixSpace of 3 by 3 sparse matrices over Integer Ring
|
|
977
|
+
|
|
978
|
+
If ``self`` is immutable, the result is cached::
|
|
979
|
+
|
|
980
|
+
sage: sImmM = ImmutableMatrix([[1, x + 1], [x - 1, 1]]); sImmM
|
|
981
|
+
Matrix([
|
|
982
|
+
[ 1, x + 1],
|
|
983
|
+
[x - 1, 1]])
|
|
984
|
+
sage: ImmM = sImmM._sage_(); ImmM
|
|
985
|
+
[ 1 x + 1]
|
|
986
|
+
[x - 1 1]
|
|
987
|
+
sage: ImmM is sImmM._sage_()
|
|
988
|
+
True
|
|
989
|
+
|
|
990
|
+
If ``self`` is mutable, the conversion is redone every time::
|
|
991
|
+
|
|
992
|
+
sage: sM[0, 0] = 1000
|
|
993
|
+
sage: MutatedM = sM._sage_(); MutatedM
|
|
994
|
+
[ 1000 x + 1]
|
|
995
|
+
[x - 1 1]
|
|
996
|
+
sage: M == MutatedM
|
|
997
|
+
False
|
|
998
|
+
"""
|
|
999
|
+
try:
|
|
1000
|
+
return self._sage_object
|
|
1001
|
+
except AttributeError:
|
|
1002
|
+
from sympy.matrices import SparseMatrix, ImmutableMatrix
|
|
1003
|
+
from sage.matrix.constructor import matrix
|
|
1004
|
+
|
|
1005
|
+
rows, cols = self.shape
|
|
1006
|
+
d = {row_col: value._sage_()
|
|
1007
|
+
for row_col, value in self.todok().items()}
|
|
1008
|
+
if not d:
|
|
1009
|
+
from sage.rings.integer_ring import ZZ
|
|
1010
|
+
base_ring = ZZ
|
|
1011
|
+
else:
|
|
1012
|
+
from sage.structure.element import get_coercion_model
|
|
1013
|
+
from sage.symbolic.ring import SR
|
|
1014
|
+
coercion_model = get_coercion_model()
|
|
1015
|
+
try:
|
|
1016
|
+
base_ring = coercion_model.common_parent(*d.values())
|
|
1017
|
+
except TypeError: # no common canonical parent
|
|
1018
|
+
base_ring = SR
|
|
1019
|
+
result = matrix(base_ring, rows, cols, d,
|
|
1020
|
+
sparse=isinstance(self, SparseMatrix),
|
|
1021
|
+
immutable=True)
|
|
1022
|
+
if isinstance(self, ImmutableMatrix):
|
|
1023
|
+
self._sage_object = result
|
|
1024
|
+
return result
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
def _sympysage_relational(self):
|
|
1028
|
+
"""
|
|
1029
|
+
EXAMPLES::
|
|
1030
|
+
|
|
1031
|
+
sage: from sympy import Eq, Ne, Gt, Ge, Lt, Le, Symbol
|
|
1032
|
+
sage: sx = Symbol('x')
|
|
1033
|
+
sage: assert (x == 0)._sympy_() == Eq(sx, 0)
|
|
1034
|
+
sage: assert (x == 0) == Eq(x, 0)._sage_()
|
|
1035
|
+
sage: assert (x != 0)._sympy_() == Ne(sx, 0)
|
|
1036
|
+
sage: assert (x != 0) == Ne(x, 0)._sage_()
|
|
1037
|
+
sage: assert (x > 0)._sympy_() == Gt(sx, 0)
|
|
1038
|
+
sage: assert (x > 0) == Gt(x, 0)._sage_()
|
|
1039
|
+
sage: assert (x >= 0)._sympy_() == Ge(sx, 0)
|
|
1040
|
+
sage: assert (x >= 0) == Ge(x, 0)._sage_()
|
|
1041
|
+
sage: assert (x < 0)._sympy_() == Lt(sx, 0)
|
|
1042
|
+
sage: assert (x < 0) == Lt(x, 0)._sage_()
|
|
1043
|
+
sage: assert (x <= 0)._sympy_() == Le(sx, 0)
|
|
1044
|
+
sage: assert (x <= 0) == Le(x, 0)._sage_()
|
|
1045
|
+
"""
|
|
1046
|
+
from operator import eq, ne, gt, lt, ge, le
|
|
1047
|
+
from sympy import Eq, Ne, Gt, Ge, Lt, Le
|
|
1048
|
+
ops = {Eq: eq, Ne: ne, Gt: gt, Lt: lt, Ge: ge, Le: le}
|
|
1049
|
+
return ops.get(self.func)(self.lhs._sage_(), self.rhs._sage_())
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
def _sympysage_false(self):
|
|
1053
|
+
"""
|
|
1054
|
+
EXAMPLES::
|
|
1055
|
+
|
|
1056
|
+
sage: from sympy.logic.boolalg import BooleanFalse
|
|
1057
|
+
sage: assert SR(False)._sympy_() == BooleanFalse() # known bug
|
|
1058
|
+
sage: assert SR(False) == BooleanFalse()._sage_()
|
|
1059
|
+
"""
|
|
1060
|
+
from sage.symbolic.ring import SR
|
|
1061
|
+
return SR(False)
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
def _sympysage_true(self):
|
|
1065
|
+
"""
|
|
1066
|
+
EXAMPLES::
|
|
1067
|
+
|
|
1068
|
+
sage: from sympy.logic.boolalg import BooleanTrue
|
|
1069
|
+
sage: assert SR(True)._sympy_() == BooleanTrue() # known bug
|
|
1070
|
+
sage: assert SR(True) == BooleanTrue()._sage_()
|
|
1071
|
+
"""
|
|
1072
|
+
from sage.symbolic.ring import SR
|
|
1073
|
+
return SR(True)
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
# ------------------------------------------------------------------
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
@run_once
|
|
1080
|
+
def sympy_init():
|
|
1081
|
+
"""
|
|
1082
|
+
Add ``_sage_()`` methods to SymPy objects where needed.
|
|
1083
|
+
|
|
1084
|
+
This gets called with every call to ``Expression._sympy_()``
|
|
1085
|
+
so there is only need to call it if you bypass ``_sympy_()`` to
|
|
1086
|
+
create SymPy objects. Note that SymPy objects have ``_sage_()``
|
|
1087
|
+
methods hard installed but having them inside Sage as
|
|
1088
|
+
one file makes them easier to maintain for Sage developers.
|
|
1089
|
+
|
|
1090
|
+
EXAMPLES::
|
|
1091
|
+
|
|
1092
|
+
sage: from sage.interfaces.sympy import sympy_init
|
|
1093
|
+
sage: from sympy import Symbol, Abs
|
|
1094
|
+
sage: sympy_init()
|
|
1095
|
+
sage: assert abs(x) == Abs(Symbol('x'))._sage_()
|
|
1096
|
+
"""
|
|
1097
|
+
from sympy import Add
|
|
1098
|
+
if Add._sage_ == _sympysage_add:
|
|
1099
|
+
return
|
|
1100
|
+
|
|
1101
|
+
from sympy import Mul, Pow, Symbol, Subs
|
|
1102
|
+
from sympy.core.function import (Function, AppliedUndef, Derivative)
|
|
1103
|
+
from sympy.core.numbers import (Float, Integer, Rational, Infinity,
|
|
1104
|
+
NegativeInfinity, ComplexInfinity,
|
|
1105
|
+
Exp1, Pi, GoldenRatio,
|
|
1106
|
+
EulerGamma, Catalan, ImaginaryUnit)
|
|
1107
|
+
from sympy.core.numbers import NaN as sympy_nan
|
|
1108
|
+
from sympy.core.relational import Relational
|
|
1109
|
+
from sympy.functions.combinatorial.factorials import (RisingFactorial,
|
|
1110
|
+
FallingFactorial)
|
|
1111
|
+
from sympy.functions.elementary.complexes import (re, im, Abs)
|
|
1112
|
+
from sympy.functions.elementary.exponential import LambertW
|
|
1113
|
+
from sympy.functions.elementary.integers import ceiling
|
|
1114
|
+
from sympy.functions.elementary.piecewise import Piecewise
|
|
1115
|
+
from sympy.functions.special.error_functions import fresnels, fresnelc
|
|
1116
|
+
from sympy.functions.special.bessel import (besselj, bessely, besseli, besselk)
|
|
1117
|
+
from sympy.functions.special.delta_functions import (DiracDelta, Heaviside)
|
|
1118
|
+
from sympy.functions.special.error_functions import expint
|
|
1119
|
+
from sympy.functions.special.elliptic_integrals import elliptic_k
|
|
1120
|
+
from sympy.functions.special.gamma_functions import loggamma, polygamma
|
|
1121
|
+
from sympy.functions.special.hyper import hyper
|
|
1122
|
+
from sympy.functions.special.spherical_harmonics import Ynm
|
|
1123
|
+
from sympy.functions.special.tensor_functions import KroneckerDelta
|
|
1124
|
+
from sympy.logic.boolalg import BooleanTrue, BooleanFalse
|
|
1125
|
+
from sympy.integrals.integrals import Integral
|
|
1126
|
+
from sympy.polys import Poly
|
|
1127
|
+
from sympy.polys.domains.integerring import IntegerRing
|
|
1128
|
+
from sympy.polys.domains.rationalfield import RationalField
|
|
1129
|
+
from sympy.polys.domains.polynomialring import PolynomialRing
|
|
1130
|
+
from sympy.polys.rootoftools import CRootOf
|
|
1131
|
+
from sympy.polys.rootisolation import RealInterval, ComplexInterval
|
|
1132
|
+
from sympy.series.order import Order
|
|
1133
|
+
from sympy.matrices import ImmutableMatrix, ImmutableSparseMatrix, Matrix, SparseMatrix
|
|
1134
|
+
|
|
1135
|
+
Float._sage_ = _sympysage_float
|
|
1136
|
+
Integer._sage_ = _sympysage_integer
|
|
1137
|
+
Rational._sage_ = _sympysage_rational
|
|
1138
|
+
RealInterval._sage_ = _sympysage_real_interval
|
|
1139
|
+
ComplexInterval._sage_ = _sympysage_complex_interval
|
|
1140
|
+
IntegerRing._sage_ = _sympysage_integer_ring
|
|
1141
|
+
RationalField._sage_ = _sympysage_rational_field
|
|
1142
|
+
PolynomialRing._sage_ = _sympysage_polynomial_ring
|
|
1143
|
+
Poly._sage_ = _sympysage_polynomial
|
|
1144
|
+
Infinity._sage_ = _sympysage_pinfty
|
|
1145
|
+
NegativeInfinity._sage_ = _sympysage_ninfty
|
|
1146
|
+
ComplexInfinity._sage_ = _sympysage_uinfty
|
|
1147
|
+
sympy_nan._sage_ = _sympysage_nan
|
|
1148
|
+
ImmutableMatrix._sage_ = _sympysage_matrix
|
|
1149
|
+
ImmutableSparseMatrix._sage_ = _sympysage_matrix
|
|
1150
|
+
Matrix._sage_ = _sympysage_matrix
|
|
1151
|
+
SparseMatrix._sage_ = _sympysage_matrix
|
|
1152
|
+
Relational._sage_ = _sympysage_relational
|
|
1153
|
+
Exp1._sage_ = _sympysage_e
|
|
1154
|
+
Pi._sage_ = _sympysage_pi
|
|
1155
|
+
GoldenRatio._sage_ = _sympysage_golden_ratio
|
|
1156
|
+
EulerGamma._sage_ = _sympysage_eulerg
|
|
1157
|
+
Catalan._sage_ = _sympysage_catalan
|
|
1158
|
+
ImaginaryUnit._sage_ = _sympysage_i
|
|
1159
|
+
Add._sage_ = _sympysage_add
|
|
1160
|
+
Mul._sage_ = _sympysage_mul
|
|
1161
|
+
Pow._sage_ = _sympysage_pow
|
|
1162
|
+
Symbol._sage_ = _sympysage_symbol
|
|
1163
|
+
Subs._sage_ = _sympysage_Subs
|
|
1164
|
+
Function._sage_ = _sympysage_function
|
|
1165
|
+
AppliedUndef._sage_ = _sympysage_function
|
|
1166
|
+
import sympy.core.function
|
|
1167
|
+
sympy.core.function._undef_sage_helper = UndefSageHelper()
|
|
1168
|
+
Integral._sage_ = _sympysage_integral
|
|
1169
|
+
Derivative._sage_ = _sympysage_derivative
|
|
1170
|
+
Order._sage_ = _sympysage_order
|
|
1171
|
+
LambertW._sage_ = _sympysage_lambertw
|
|
1172
|
+
RisingFactorial._sage_ = _sympysage_rf
|
|
1173
|
+
FallingFactorial._sage_ = _sympysage_ff
|
|
1174
|
+
loggamma._sage_ = _sympysage_lgamma
|
|
1175
|
+
polygamma._sage_ = _sympysage_polygamma
|
|
1176
|
+
DiracDelta._sage_ = _sympysage_dirac_delta
|
|
1177
|
+
Heaviside._sage_ = _sympysage_heaviside
|
|
1178
|
+
expint._sage_ = _sympysage_expint
|
|
1179
|
+
hyper._sage_ = _sympysage_hyp
|
|
1180
|
+
elliptic_k._sage_ = _sympysage_elliptic_k
|
|
1181
|
+
KroneckerDelta._sage_ = _sympysage_kronecker_delta
|
|
1182
|
+
Piecewise._sage_ = _sympysage_piecewise
|
|
1183
|
+
fresnels._sage_ = _sympysage_fresnels
|
|
1184
|
+
fresnelc._sage_ = _sympysage_fresnelc
|
|
1185
|
+
besselj._sage_ = _sympysage_besselj
|
|
1186
|
+
bessely._sage_ = _sympysage_bessely
|
|
1187
|
+
besseli._sage_ = _sympysage_besseli
|
|
1188
|
+
besselk._sage_ = _sympysage_besselk
|
|
1189
|
+
Ynm._sage_ = _sympysage_ynm
|
|
1190
|
+
re._sage_ = _sympysage_re
|
|
1191
|
+
im._sage_ = _sympysage_im
|
|
1192
|
+
Abs._sage_ = _sympysage_abs
|
|
1193
|
+
CRootOf._sage_ = _sympysage_crootof
|
|
1194
|
+
BooleanFalse._sage_ = _sympysage_false
|
|
1195
|
+
BooleanTrue._sage_ = _sympysage_true
|
|
1196
|
+
ceiling._sage_ = _sympysage_ceiling
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
def check_expression(expr, var_symbols, only_from_sympy=False):
|
|
1200
|
+
"""
|
|
1201
|
+
Do ``eval(expr)`` both in Sage and SymPy and other checks.
|
|
1202
|
+
|
|
1203
|
+
EXAMPLES::
|
|
1204
|
+
|
|
1205
|
+
sage: from sage.interfaces.sympy import check_expression
|
|
1206
|
+
sage: check_expression("1.123*x", "x")
|
|
1207
|
+
"""
|
|
1208
|
+
from sage.symbolic.ring import SR
|
|
1209
|
+
from sympy import (__dict__ as sympydict, Basic, S, var as svar)
|
|
1210
|
+
# evaluate the expression in the context of Sage:
|
|
1211
|
+
if var_symbols:
|
|
1212
|
+
SR.var(var_symbols)
|
|
1213
|
+
is_different = False
|
|
1214
|
+
try:
|
|
1215
|
+
e_sage = SR(expr)
|
|
1216
|
+
assert not isinstance(e_sage, Basic)
|
|
1217
|
+
except (NameError, TypeError):
|
|
1218
|
+
is_different = True
|
|
1219
|
+
|
|
1220
|
+
# evaluate the expression in the context of SymPy:
|
|
1221
|
+
if var_symbols:
|
|
1222
|
+
svar(var_symbols)
|
|
1223
|
+
b = globals().copy()
|
|
1224
|
+
b.update(sympydict)
|
|
1225
|
+
assert "sin" in b
|
|
1226
|
+
b.update(sympydict)
|
|
1227
|
+
e_sympy = eval(expr, b)
|
|
1228
|
+
assert isinstance(e_sympy, Basic)
|
|
1229
|
+
|
|
1230
|
+
# Sympy func may have specific _sage_ method
|
|
1231
|
+
if is_different:
|
|
1232
|
+
_sage_method = getattr(e_sympy.func, "_sage_")
|
|
1233
|
+
e_sage = _sage_method(S(e_sympy))
|
|
1234
|
+
|
|
1235
|
+
# Do the actual checks:
|
|
1236
|
+
if not only_from_sympy:
|
|
1237
|
+
assert S(e_sage) == e_sympy
|
|
1238
|
+
assert e_sage == SR(e_sympy)
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
def test_all():
|
|
1242
|
+
"""
|
|
1243
|
+
Call some tests that were originally in SymPy.
|
|
1244
|
+
|
|
1245
|
+
EXAMPLES::
|
|
1246
|
+
|
|
1247
|
+
sage: from sage.interfaces.sympy import test_all
|
|
1248
|
+
sage: test_all()
|
|
1249
|
+
"""
|
|
1250
|
+
def test_basics():
|
|
1251
|
+
check_expression("x", "x")
|
|
1252
|
+
check_expression("x**2", "x")
|
|
1253
|
+
check_expression("x**2+y**3", "x y")
|
|
1254
|
+
check_expression("1/(x+y)**2-x**3/4", "x y")
|
|
1255
|
+
|
|
1256
|
+
def test_complex():
|
|
1257
|
+
check_expression("I", "")
|
|
1258
|
+
check_expression("23+I*4", "x")
|
|
1259
|
+
|
|
1260
|
+
def test_complex_fail():
|
|
1261
|
+
# Sage doesn't properly implement _sympy_ on I
|
|
1262
|
+
check_expression("I*y", "y")
|
|
1263
|
+
check_expression("x+I*y", "x y")
|
|
1264
|
+
|
|
1265
|
+
def test_integer():
|
|
1266
|
+
check_expression("4*x", "x")
|
|
1267
|
+
check_expression("-4*x", "x")
|
|
1268
|
+
|
|
1269
|
+
def test_real():
|
|
1270
|
+
check_expression("1.123*x", "x")
|
|
1271
|
+
check_expression("-18.22*x", "x")
|
|
1272
|
+
|
|
1273
|
+
def test_functions():
|
|
1274
|
+
# Test at least one Function without own _sage_ method
|
|
1275
|
+
from sympy import factorial
|
|
1276
|
+
assert "_sage_" not in factorial.__dict__
|
|
1277
|
+
check_expression("factorial(x)", "x")
|
|
1278
|
+
check_expression("sin(x)", "x")
|
|
1279
|
+
check_expression("cos(x)", "x")
|
|
1280
|
+
check_expression("tan(x)", "x")
|
|
1281
|
+
check_expression("cot(x)", "x")
|
|
1282
|
+
check_expression("asin(x)", "x")
|
|
1283
|
+
check_expression("acos(x)", "x")
|
|
1284
|
+
check_expression("atan(x)", "x")
|
|
1285
|
+
check_expression("atan2(y, x)", "x, y")
|
|
1286
|
+
check_expression("acot(x)", "x")
|
|
1287
|
+
check_expression("sinh(x)", "x")
|
|
1288
|
+
check_expression("cosh(x)", "x")
|
|
1289
|
+
check_expression("tanh(x)", "x")
|
|
1290
|
+
check_expression("coth(x)", "x")
|
|
1291
|
+
check_expression("asinh(x)", "x")
|
|
1292
|
+
check_expression("acosh(x)", "x")
|
|
1293
|
+
check_expression("atanh(x)", "x")
|
|
1294
|
+
check_expression("acoth(x)", "x")
|
|
1295
|
+
check_expression("exp(x)", "x")
|
|
1296
|
+
check_expression("log(x)", "x")
|
|
1297
|
+
check_expression("abs(x)", "x")
|
|
1298
|
+
check_expression("arg(x)", "x")
|
|
1299
|
+
check_expression("conjugate(x)", "x")
|
|
1300
|
+
|
|
1301
|
+
def test_issue_4023():
|
|
1302
|
+
from sage.symbolic.ring import SR
|
|
1303
|
+
from sage.functions.all import log
|
|
1304
|
+
from sympy import integrate, simplify
|
|
1305
|
+
a, x = SR.var("a x")
|
|
1306
|
+
i = integrate(log(x) / a, (x, a, a + 1))
|
|
1307
|
+
i2 = simplify(i)
|
|
1308
|
+
s = SR(i2)
|
|
1309
|
+
assert s == (a * log(1 + a) - a * log(a) + log(1 + a) - 1) / a
|
|
1310
|
+
|
|
1311
|
+
def test_integral():
|
|
1312
|
+
# test Sympy-->Sage
|
|
1313
|
+
check_expression("Integral(x, (x,))", "x", only_from_sympy=True)
|
|
1314
|
+
check_expression("Integral(x, (x, 0, 1))", "x", only_from_sympy=True)
|
|
1315
|
+
check_expression("Integral(x*y, (x,), (y, ))", "x,y", only_from_sympy=True)
|
|
1316
|
+
check_expression("Integral(x*y, (x,), (y, 0, 1))", "x,y", only_from_sympy=True)
|
|
1317
|
+
check_expression("Integral(x*y, (x, 0, 1), (y,))", "x,y", only_from_sympy=True)
|
|
1318
|
+
check_expression("Integral(x*y, (x, 0, 1), (y, 0, 1))", "x,y", only_from_sympy=True)
|
|
1319
|
+
check_expression("Integral(x*y*z, (x, 0, 1), (y, 0, 1), (z, 0, 1))", "x,y,z", only_from_sympy=True)
|
|
1320
|
+
|
|
1321
|
+
def test_integral_failing():
|
|
1322
|
+
# Note: sage may attempt to turn this into Integral(x, (x, x, 0))
|
|
1323
|
+
check_expression("Integral(x, (x, 0))", "x", only_from_sympy=True)
|
|
1324
|
+
check_expression("Integral(x*y, (x,), (y, 0))", "x,y", only_from_sympy=True)
|
|
1325
|
+
check_expression("Integral(x*y, (x, 0, 1), (y, 0))", "x,y", only_from_sympy=True)
|
|
1326
|
+
|
|
1327
|
+
def test_undefined_function():
|
|
1328
|
+
from sage.symbolic.ring import SR
|
|
1329
|
+
from sage.calculus.var import function
|
|
1330
|
+
from sympy import Symbol, Function
|
|
1331
|
+
f = function('f')
|
|
1332
|
+
sf = Function('f')
|
|
1333
|
+
x, y = SR.var('x y')
|
|
1334
|
+
sx = Symbol('x')
|
|
1335
|
+
sy = Symbol('y')
|
|
1336
|
+
assert f(x)._sympy_() == sf(sx)
|
|
1337
|
+
assert f(x) == sf(sx)._sage_()
|
|
1338
|
+
assert f(x, y)._sympy_() == sf(sx, sy)
|
|
1339
|
+
assert f(x, y) == sf(sx, sy)._sage_()
|
|
1340
|
+
assert f._sympy_() == sf
|
|
1341
|
+
assert f == sf._sage_()
|
|
1342
|
+
|
|
1343
|
+
test_basics()
|
|
1344
|
+
test_complex()
|
|
1345
|
+
test_complex_fail()
|
|
1346
|
+
test_integer()
|
|
1347
|
+
test_real()
|
|
1348
|
+
test_functions()
|
|
1349
|
+
test_issue_4023()
|
|
1350
|
+
test_integral()
|
|
1351
|
+
# test_integral_failing()
|
|
1352
|
+
test_undefined_function()
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
def sympy_set_to_list(set, vars):
|
|
1356
|
+
"""
|
|
1357
|
+
Convert all set objects that can be returned by SymPy's solvers.
|
|
1358
|
+
"""
|
|
1359
|
+
from sage.rings.infinity import UnsignedInfinity
|
|
1360
|
+
from sympy import (FiniteSet, And, Or, Union, Interval, oo, S)
|
|
1361
|
+
from sympy.core.relational import Relational
|
|
1362
|
+
if set == S.Reals:
|
|
1363
|
+
return [x._sage_() < oo for x in vars]
|
|
1364
|
+
elif set == S.Complexes:
|
|
1365
|
+
return [x._sage_() != UnsignedInfinity for x in vars]
|
|
1366
|
+
elif set is None or set == S.EmptySet:
|
|
1367
|
+
return []
|
|
1368
|
+
if isinstance(set, (And, Or, Relational)):
|
|
1369
|
+
if isinstance(set, And):
|
|
1370
|
+
return [[item for rel in set._args[0]
|
|
1371
|
+
for item in sympy_set_to_list(rel, vars)]]
|
|
1372
|
+
elif isinstance(set, Or):
|
|
1373
|
+
return [sympy_set_to_list(iv, vars) for iv in set._args[0]]
|
|
1374
|
+
elif isinstance(set, Relational):
|
|
1375
|
+
return [set._sage_()]
|
|
1376
|
+
elif isinstance(set, FiniteSet):
|
|
1377
|
+
x = vars[0]
|
|
1378
|
+
return [x._sage_() == arg._sage_() for arg in set.args]
|
|
1379
|
+
elif isinstance(set, (Union, Interval)):
|
|
1380
|
+
x = vars[0]
|
|
1381
|
+
if isinstance(set, Interval):
|
|
1382
|
+
left, right, lclosed, rclosed = set._args
|
|
1383
|
+
if lclosed:
|
|
1384
|
+
rel1 = [x._sage_() > left._sage_()]
|
|
1385
|
+
else:
|
|
1386
|
+
rel1 = [x._sage_() >= left._sage_()]
|
|
1387
|
+
if rclosed:
|
|
1388
|
+
rel2 = [x._sage_() < right._sage_()]
|
|
1389
|
+
else:
|
|
1390
|
+
return [x._sage_() <= right._sage_()]
|
|
1391
|
+
if right == oo:
|
|
1392
|
+
return rel1
|
|
1393
|
+
if left == -oo:
|
|
1394
|
+
return rel2
|
|
1395
|
+
return [rel1, rel2]
|
|
1396
|
+
if isinstance(set, Union):
|
|
1397
|
+
return [sympy_set_to_list(iv, vars) for iv in set._args]
|
|
1398
|
+
return set
|