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/holonomic/__init__.py +18 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/holonomic.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/holonomicerrors.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/numerical.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/recurrence.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/holonomic.py +2899 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/holonomicerrors.py +49 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/numerical.py +109 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/recurrence.py +365 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__pycache__/test_holonomic.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__pycache__/test_recurrence.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/test_holonomic.py +830 -0
- env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/test_recurrence.py +29 -0
- env-llmeval/lib/python3.10/site-packages/sympy/ntheory/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/experimental_lambdify.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/plot.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/plot_implicit.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/textplot.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_arithmetic.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_membership.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/lib_interval.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/interval_arithmetic.py +412 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/interval_membership.py +78 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/lib_interval.py +452 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_interval_functions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_interval_membership.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_intervalmath.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_interval_functions.py +415 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_interval_membership.py +150 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_intervalmath.py +213 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/color_scheme.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/managed_window.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_axes.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_camera.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_controller.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_curve.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_interval.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_mode.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_mode_base.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_modes.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_object.cpython-310.pyc +0 -0
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__init__.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
r"""
|
2 |
+
The :py:mod:`~sympy.holonomic` module is intended to deal with holonomic functions along
|
3 |
+
with various operations on them like addition, multiplication, composition,
|
4 |
+
integration and differentiation. The module also implements various kinds of
|
5 |
+
conversions such as converting holonomic functions to a different form and the
|
6 |
+
other way around.
|
7 |
+
"""
|
8 |
+
|
9 |
+
from .holonomic import (DifferentialOperator, HolonomicFunction, DifferentialOperators,
|
10 |
+
from_hyper, from_meijerg, expr_to_holonomic)
|
11 |
+
from .recurrence import RecurrenceOperators, RecurrenceOperator, HolonomicSequence
|
12 |
+
|
13 |
+
__all__ = [
|
14 |
+
'DifferentialOperator', 'HolonomicFunction', 'DifferentialOperators',
|
15 |
+
'from_hyper', 'from_meijerg', 'expr_to_holonomic',
|
16 |
+
|
17 |
+
'RecurrenceOperators', 'RecurrenceOperator', 'HolonomicSequence',
|
18 |
+
]
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (903 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/holonomic.cpython-310.pyc
ADDED
Binary file (68.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/holonomicerrors.cpython-310.pyc
ADDED
Binary file (2.25 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/numerical.cpython-310.pyc
ADDED
Binary file (3.41 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/__pycache__/recurrence.cpython-310.pyc
ADDED
Binary file (9.39 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/holonomic.py
ADDED
@@ -0,0 +1,2899 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This module implements Holonomic Functions and
|
3 |
+
various operations on them.
|
4 |
+
"""
|
5 |
+
|
6 |
+
from sympy.core import Add, Mul, Pow
|
7 |
+
from sympy.core.numbers import (NaN, Infinity, NegativeInfinity, Float, I, pi,
|
8 |
+
equal_valued)
|
9 |
+
from sympy.core.singleton import S
|
10 |
+
from sympy.core.sorting import ordered
|
11 |
+
from sympy.core.symbol import Dummy, Symbol
|
12 |
+
from sympy.core.sympify import sympify
|
13 |
+
from sympy.functions.combinatorial.factorials import binomial, factorial, rf
|
14 |
+
from sympy.functions.elementary.exponential import exp_polar, exp, log
|
15 |
+
from sympy.functions.elementary.hyperbolic import (cosh, sinh)
|
16 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
17 |
+
from sympy.functions.elementary.trigonometric import (cos, sin, sinc)
|
18 |
+
from sympy.functions.special.error_functions import (Ci, Shi, Si, erf, erfc, erfi)
|
19 |
+
from sympy.functions.special.gamma_functions import gamma
|
20 |
+
from sympy.functions.special.hyper import hyper, meijerg
|
21 |
+
from sympy.integrals import meijerint
|
22 |
+
from sympy.matrices import Matrix
|
23 |
+
from sympy.polys.rings import PolyElement
|
24 |
+
from sympy.polys.fields import FracElement
|
25 |
+
from sympy.polys.domains import QQ, RR
|
26 |
+
from sympy.polys.polyclasses import DMF
|
27 |
+
from sympy.polys.polyroots import roots
|
28 |
+
from sympy.polys.polytools import Poly
|
29 |
+
from sympy.polys.matrices import DomainMatrix
|
30 |
+
from sympy.printing import sstr
|
31 |
+
from sympy.series.limits import limit
|
32 |
+
from sympy.series.order import Order
|
33 |
+
from sympy.simplify.hyperexpand import hyperexpand
|
34 |
+
from sympy.simplify.simplify import nsimplify
|
35 |
+
from sympy.solvers.solvers import solve
|
36 |
+
|
37 |
+
from .recurrence import HolonomicSequence, RecurrenceOperator, RecurrenceOperators
|
38 |
+
from .holonomicerrors import (NotPowerSeriesError, NotHyperSeriesError,
|
39 |
+
SingularityError, NotHolonomicError)
|
40 |
+
|
41 |
+
|
42 |
+
def _find_nonzero_solution(r, homosys):
|
43 |
+
ones = lambda shape: DomainMatrix.ones(shape, r.domain)
|
44 |
+
particular, nullspace = r._solve(homosys)
|
45 |
+
nullity = nullspace.shape[0]
|
46 |
+
nullpart = ones((1, nullity)) * nullspace
|
47 |
+
sol = (particular + nullpart).transpose()
|
48 |
+
return sol
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
def DifferentialOperators(base, generator):
|
53 |
+
r"""
|
54 |
+
This function is used to create annihilators using ``Dx``.
|
55 |
+
|
56 |
+
Explanation
|
57 |
+
===========
|
58 |
+
|
59 |
+
Returns an Algebra of Differential Operators also called Weyl Algebra
|
60 |
+
and the operator for differentiation i.e. the ``Dx`` operator.
|
61 |
+
|
62 |
+
Parameters
|
63 |
+
==========
|
64 |
+
|
65 |
+
base:
|
66 |
+
Base polynomial ring for the algebra.
|
67 |
+
The base polynomial ring is the ring of polynomials in :math:`x` that
|
68 |
+
will appear as coefficients in the operators.
|
69 |
+
generator:
|
70 |
+
Generator of the algebra which can
|
71 |
+
be either a noncommutative ``Symbol`` or a string. e.g. "Dx" or "D".
|
72 |
+
|
73 |
+
Examples
|
74 |
+
========
|
75 |
+
|
76 |
+
>>> from sympy import ZZ
|
77 |
+
>>> from sympy.abc import x
|
78 |
+
>>> from sympy.holonomic.holonomic import DifferentialOperators
|
79 |
+
>>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
80 |
+
>>> R
|
81 |
+
Univariate Differential Operator Algebra in intermediate Dx over the base ring ZZ[x]
|
82 |
+
>>> Dx*x
|
83 |
+
(1) + (x)*Dx
|
84 |
+
"""
|
85 |
+
|
86 |
+
ring = DifferentialOperatorAlgebra(base, generator)
|
87 |
+
return (ring, ring.derivative_operator)
|
88 |
+
|
89 |
+
|
90 |
+
class DifferentialOperatorAlgebra:
|
91 |
+
r"""
|
92 |
+
An Ore Algebra is a set of noncommutative polynomials in the
|
93 |
+
intermediate ``Dx`` and coefficients in a base polynomial ring :math:`A`.
|
94 |
+
It follows the commutation rule:
|
95 |
+
|
96 |
+
.. math ::
|
97 |
+
Dxa = \sigma(a)Dx + \delta(a)
|
98 |
+
|
99 |
+
for :math:`a \subset A`.
|
100 |
+
|
101 |
+
Where :math:`\sigma: A \Rightarrow A` is an endomorphism and :math:`\delta: A \rightarrow A`
|
102 |
+
is a skew-derivation i.e. :math:`\delta(ab) = \delta(a) b + \sigma(a) \delta(b)`.
|
103 |
+
|
104 |
+
If one takes the sigma as identity map and delta as the standard derivation
|
105 |
+
then it becomes the algebra of Differential Operators also called
|
106 |
+
a Weyl Algebra i.e. an algebra whose elements are Differential Operators.
|
107 |
+
|
108 |
+
This class represents a Weyl Algebra and serves as the parent ring for
|
109 |
+
Differential Operators.
|
110 |
+
|
111 |
+
Examples
|
112 |
+
========
|
113 |
+
|
114 |
+
>>> from sympy import ZZ
|
115 |
+
>>> from sympy import symbols
|
116 |
+
>>> from sympy.holonomic.holonomic import DifferentialOperators
|
117 |
+
>>> x = symbols('x')
|
118 |
+
>>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
119 |
+
>>> R
|
120 |
+
Univariate Differential Operator Algebra in intermediate Dx over the base ring
|
121 |
+
ZZ[x]
|
122 |
+
|
123 |
+
See Also
|
124 |
+
========
|
125 |
+
|
126 |
+
DifferentialOperator
|
127 |
+
"""
|
128 |
+
|
129 |
+
def __init__(self, base, generator):
|
130 |
+
# the base polynomial ring for the algebra
|
131 |
+
self.base = base
|
132 |
+
# the operator representing differentiation i.e. `Dx`
|
133 |
+
self.derivative_operator = DifferentialOperator(
|
134 |
+
[base.zero, base.one], self)
|
135 |
+
|
136 |
+
if generator is None:
|
137 |
+
self.gen_symbol = Symbol('Dx', commutative=False)
|
138 |
+
else:
|
139 |
+
if isinstance(generator, str):
|
140 |
+
self.gen_symbol = Symbol(generator, commutative=False)
|
141 |
+
elif isinstance(generator, Symbol):
|
142 |
+
self.gen_symbol = generator
|
143 |
+
|
144 |
+
def __str__(self):
|
145 |
+
string = 'Univariate Differential Operator Algebra in intermediate '\
|
146 |
+
+ sstr(self.gen_symbol) + ' over the base ring ' + \
|
147 |
+
(self.base).__str__()
|
148 |
+
|
149 |
+
return string
|
150 |
+
|
151 |
+
__repr__ = __str__
|
152 |
+
|
153 |
+
def __eq__(self, other):
|
154 |
+
if self.base == other.base and self.gen_symbol == other.gen_symbol:
|
155 |
+
return True
|
156 |
+
else:
|
157 |
+
return False
|
158 |
+
|
159 |
+
|
160 |
+
class DifferentialOperator:
|
161 |
+
"""
|
162 |
+
Differential Operators are elements of Weyl Algebra. The Operators
|
163 |
+
are defined by a list of polynomials in the base ring and the
|
164 |
+
parent ring of the Operator i.e. the algebra it belongs to.
|
165 |
+
|
166 |
+
Explanation
|
167 |
+
===========
|
168 |
+
|
169 |
+
Takes a list of polynomials for each power of ``Dx`` and the
|
170 |
+
parent ring which must be an instance of DifferentialOperatorAlgebra.
|
171 |
+
|
172 |
+
A Differential Operator can be created easily using
|
173 |
+
the operator ``Dx``. See examples below.
|
174 |
+
|
175 |
+
Examples
|
176 |
+
========
|
177 |
+
|
178 |
+
>>> from sympy.holonomic.holonomic import DifferentialOperator, DifferentialOperators
|
179 |
+
>>> from sympy import ZZ
|
180 |
+
>>> from sympy import symbols
|
181 |
+
>>> x = symbols('x')
|
182 |
+
>>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')
|
183 |
+
|
184 |
+
>>> DifferentialOperator([0, 1, x**2], R)
|
185 |
+
(1)*Dx + (x**2)*Dx**2
|
186 |
+
|
187 |
+
>>> (x*Dx*x + 1 - Dx**2)**2
|
188 |
+
(2*x**2 + 2*x + 1) + (4*x**3 + 2*x**2 - 4)*Dx + (x**4 - 6*x - 2)*Dx**2 + (-2*x**2)*Dx**3 + (1)*Dx**4
|
189 |
+
|
190 |
+
See Also
|
191 |
+
========
|
192 |
+
|
193 |
+
DifferentialOperatorAlgebra
|
194 |
+
"""
|
195 |
+
|
196 |
+
_op_priority = 20
|
197 |
+
|
198 |
+
def __init__(self, list_of_poly, parent):
|
199 |
+
"""
|
200 |
+
Parameters
|
201 |
+
==========
|
202 |
+
|
203 |
+
list_of_poly:
|
204 |
+
List of polynomials belonging to the base ring of the algebra.
|
205 |
+
parent:
|
206 |
+
Parent algebra of the operator.
|
207 |
+
"""
|
208 |
+
|
209 |
+
# the parent ring for this operator
|
210 |
+
# must be an DifferentialOperatorAlgebra object
|
211 |
+
self.parent = parent
|
212 |
+
base = self.parent.base
|
213 |
+
self.x = base.gens[0] if isinstance(base.gens[0], Symbol) else base.gens[0][0]
|
214 |
+
# sequence of polynomials in x for each power of Dx
|
215 |
+
# the list should not have trailing zeroes
|
216 |
+
# represents the operator
|
217 |
+
# convert the expressions into ring elements using from_sympy
|
218 |
+
for i, j in enumerate(list_of_poly):
|
219 |
+
if not isinstance(j, base.dtype):
|
220 |
+
list_of_poly[i] = base.from_sympy(sympify(j))
|
221 |
+
else:
|
222 |
+
list_of_poly[i] = base.from_sympy(base.to_sympy(j))
|
223 |
+
|
224 |
+
self.listofpoly = list_of_poly
|
225 |
+
# highest power of `Dx`
|
226 |
+
self.order = len(self.listofpoly) - 1
|
227 |
+
|
228 |
+
def __mul__(self, other):
|
229 |
+
"""
|
230 |
+
Multiplies two DifferentialOperator and returns another
|
231 |
+
DifferentialOperator instance using the commutation rule
|
232 |
+
Dx*a = a*Dx + a'
|
233 |
+
"""
|
234 |
+
|
235 |
+
listofself = self.listofpoly
|
236 |
+
|
237 |
+
if not isinstance(other, DifferentialOperator):
|
238 |
+
if not isinstance(other, self.parent.base.dtype):
|
239 |
+
listofother = [self.parent.base.from_sympy(sympify(other))]
|
240 |
+
|
241 |
+
else:
|
242 |
+
listofother = [other]
|
243 |
+
else:
|
244 |
+
listofother = other.listofpoly
|
245 |
+
|
246 |
+
# multiplies a polynomial `b` with a list of polynomials
|
247 |
+
def _mul_dmp_diffop(b, listofother):
|
248 |
+
if isinstance(listofother, list):
|
249 |
+
sol = []
|
250 |
+
for i in listofother:
|
251 |
+
sol.append(i * b)
|
252 |
+
return sol
|
253 |
+
else:
|
254 |
+
return [b * listofother]
|
255 |
+
|
256 |
+
sol = _mul_dmp_diffop(listofself[0], listofother)
|
257 |
+
|
258 |
+
# compute Dx^i * b
|
259 |
+
def _mul_Dxi_b(b):
|
260 |
+
sol1 = [self.parent.base.zero]
|
261 |
+
sol2 = []
|
262 |
+
|
263 |
+
if isinstance(b, list):
|
264 |
+
for i in b:
|
265 |
+
sol1.append(i)
|
266 |
+
sol2.append(i.diff())
|
267 |
+
else:
|
268 |
+
sol1.append(self.parent.base.from_sympy(b))
|
269 |
+
sol2.append(self.parent.base.from_sympy(b).diff())
|
270 |
+
|
271 |
+
return _add_lists(sol1, sol2)
|
272 |
+
|
273 |
+
for i in range(1, len(listofself)):
|
274 |
+
# find Dx^i * b in ith iteration
|
275 |
+
listofother = _mul_Dxi_b(listofother)
|
276 |
+
# solution = solution + listofself[i] * (Dx^i * b)
|
277 |
+
sol = _add_lists(sol, _mul_dmp_diffop(listofself[i], listofother))
|
278 |
+
|
279 |
+
return DifferentialOperator(sol, self.parent)
|
280 |
+
|
281 |
+
def __rmul__(self, other):
|
282 |
+
if not isinstance(other, DifferentialOperator):
|
283 |
+
|
284 |
+
if not isinstance(other, self.parent.base.dtype):
|
285 |
+
other = (self.parent.base).from_sympy(sympify(other))
|
286 |
+
|
287 |
+
sol = []
|
288 |
+
for j in self.listofpoly:
|
289 |
+
sol.append(other * j)
|
290 |
+
|
291 |
+
return DifferentialOperator(sol, self.parent)
|
292 |
+
|
293 |
+
def __add__(self, other):
|
294 |
+
if isinstance(other, DifferentialOperator):
|
295 |
+
|
296 |
+
sol = _add_lists(self.listofpoly, other.listofpoly)
|
297 |
+
return DifferentialOperator(sol, self.parent)
|
298 |
+
|
299 |
+
else:
|
300 |
+
list_self = self.listofpoly
|
301 |
+
if not isinstance(other, self.parent.base.dtype):
|
302 |
+
list_other = [((self.parent).base).from_sympy(sympify(other))]
|
303 |
+
else:
|
304 |
+
list_other = [other]
|
305 |
+
sol = []
|
306 |
+
sol.append(list_self[0] + list_other[0])
|
307 |
+
sol += list_self[1:]
|
308 |
+
|
309 |
+
return DifferentialOperator(sol, self.parent)
|
310 |
+
|
311 |
+
__radd__ = __add__
|
312 |
+
|
313 |
+
def __sub__(self, other):
|
314 |
+
return self + (-1) * other
|
315 |
+
|
316 |
+
def __rsub__(self, other):
|
317 |
+
return (-1) * self + other
|
318 |
+
|
319 |
+
def __neg__(self):
|
320 |
+
return -1 * self
|
321 |
+
|
322 |
+
def __truediv__(self, other):
|
323 |
+
return self * (S.One / other)
|
324 |
+
|
325 |
+
def __pow__(self, n):
|
326 |
+
if n == 1:
|
327 |
+
return self
|
328 |
+
if n == 0:
|
329 |
+
return DifferentialOperator([self.parent.base.one], self.parent)
|
330 |
+
|
331 |
+
# if self is `Dx`
|
332 |
+
if self.listofpoly == self.parent.derivative_operator.listofpoly:
|
333 |
+
sol = [self.parent.base.zero]*n
|
334 |
+
sol.append(self.parent.base.one)
|
335 |
+
return DifferentialOperator(sol, self.parent)
|
336 |
+
|
337 |
+
# the general case
|
338 |
+
else:
|
339 |
+
if n % 2 == 1:
|
340 |
+
powreduce = self**(n - 1)
|
341 |
+
return powreduce * self
|
342 |
+
elif n % 2 == 0:
|
343 |
+
powreduce = self**(n / 2)
|
344 |
+
return powreduce * powreduce
|
345 |
+
|
346 |
+
def __str__(self):
|
347 |
+
listofpoly = self.listofpoly
|
348 |
+
print_str = ''
|
349 |
+
|
350 |
+
for i, j in enumerate(listofpoly):
|
351 |
+
if j == self.parent.base.zero:
|
352 |
+
continue
|
353 |
+
|
354 |
+
if i == 0:
|
355 |
+
print_str += '(' + sstr(j) + ')'
|
356 |
+
continue
|
357 |
+
|
358 |
+
if print_str:
|
359 |
+
print_str += ' + '
|
360 |
+
|
361 |
+
if i == 1:
|
362 |
+
print_str += '(' + sstr(j) + ')*%s' %(self.parent.gen_symbol)
|
363 |
+
continue
|
364 |
+
|
365 |
+
print_str += '(' + sstr(j) + ')' + '*%s**' %(self.parent.gen_symbol) + sstr(i)
|
366 |
+
|
367 |
+
return print_str
|
368 |
+
|
369 |
+
__repr__ = __str__
|
370 |
+
|
371 |
+
def __eq__(self, other):
|
372 |
+
if isinstance(other, DifferentialOperator):
|
373 |
+
if self.listofpoly == other.listofpoly and self.parent == other.parent:
|
374 |
+
return True
|
375 |
+
else:
|
376 |
+
return False
|
377 |
+
else:
|
378 |
+
if self.listofpoly[0] == other:
|
379 |
+
for i in self.listofpoly[1:]:
|
380 |
+
if i is not self.parent.base.zero:
|
381 |
+
return False
|
382 |
+
return True
|
383 |
+
else:
|
384 |
+
return False
|
385 |
+
|
386 |
+
def is_singular(self, x0):
|
387 |
+
"""
|
388 |
+
Checks if the differential equation is singular at x0.
|
389 |
+
"""
|
390 |
+
|
391 |
+
base = self.parent.base
|
392 |
+
return x0 in roots(base.to_sympy(self.listofpoly[-1]), self.x)
|
393 |
+
|
394 |
+
|
395 |
+
class HolonomicFunction:
|
396 |
+
r"""
|
397 |
+
A Holonomic Function is a solution to a linear homogeneous ordinary
|
398 |
+
differential equation with polynomial coefficients. This differential
|
399 |
+
equation can also be represented by an annihilator i.e. a Differential
|
400 |
+
Operator ``L`` such that :math:`L.f = 0`. For uniqueness of these functions,
|
401 |
+
initial conditions can also be provided along with the annihilator.
|
402 |
+
|
403 |
+
Explanation
|
404 |
+
===========
|
405 |
+
|
406 |
+
Holonomic functions have closure properties and thus forms a ring.
|
407 |
+
Given two Holonomic Functions f and g, their sum, product,
|
408 |
+
integral and derivative is also a Holonomic Function.
|
409 |
+
|
410 |
+
For ordinary points initial condition should be a vector of values of
|
411 |
+
the derivatives i.e. :math:`[y(x_0), y'(x_0), y''(x_0) ... ]`.
|
412 |
+
|
413 |
+
For regular singular points initial conditions can also be provided in this
|
414 |
+
format:
|
415 |
+
:math:`{s0: [C_0, C_1, ...], s1: [C^1_0, C^1_1, ...], ...}`
|
416 |
+
where s0, s1, ... are the roots of indicial equation and vectors
|
417 |
+
:math:`[C_0, C_1, ...], [C^0_0, C^0_1, ...], ...` are the corresponding initial
|
418 |
+
terms of the associated power series. See Examples below.
|
419 |
+
|
420 |
+
Examples
|
421 |
+
========
|
422 |
+
|
423 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
424 |
+
>>> from sympy import QQ
|
425 |
+
>>> from sympy import symbols, S
|
426 |
+
>>> x = symbols('x')
|
427 |
+
>>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
|
428 |
+
|
429 |
+
>>> p = HolonomicFunction(Dx - 1, x, 0, [1]) # e^x
|
430 |
+
>>> q = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]) # sin(x)
|
431 |
+
|
432 |
+
>>> p + q # annihilator of e^x + sin(x)
|
433 |
+
HolonomicFunction((-1) + (1)*Dx + (-1)*Dx**2 + (1)*Dx**3, x, 0, [1, 2, 1])
|
434 |
+
|
435 |
+
>>> p * q # annihilator of e^x * sin(x)
|
436 |
+
HolonomicFunction((2) + (-2)*Dx + (1)*Dx**2, x, 0, [0, 1])
|
437 |
+
|
438 |
+
An example of initial conditions for regular singular points,
|
439 |
+
the indicial equation has only one root `1/2`.
|
440 |
+
|
441 |
+
>>> HolonomicFunction(-S(1)/2 + x*Dx, x, 0, {S(1)/2: [1]})
|
442 |
+
HolonomicFunction((-1/2) + (x)*Dx, x, 0, {1/2: [1]})
|
443 |
+
|
444 |
+
>>> HolonomicFunction(-S(1)/2 + x*Dx, x, 0, {S(1)/2: [1]}).to_expr()
|
445 |
+
sqrt(x)
|
446 |
+
|
447 |
+
To plot a Holonomic Function, one can use `.evalf()` for numerical
|
448 |
+
computation. Here's an example on `sin(x)**2/x` using numpy and matplotlib.
|
449 |
+
|
450 |
+
>>> import sympy.holonomic # doctest: +SKIP
|
451 |
+
>>> from sympy import var, sin # doctest: +SKIP
|
452 |
+
>>> import matplotlib.pyplot as plt # doctest: +SKIP
|
453 |
+
>>> import numpy as np # doctest: +SKIP
|
454 |
+
>>> var("x") # doctest: +SKIP
|
455 |
+
>>> r = np.linspace(1, 5, 100) # doctest: +SKIP
|
456 |
+
>>> y = sympy.holonomic.expr_to_holonomic(sin(x)**2/x, x0=1).evalf(r) # doctest: +SKIP
|
457 |
+
>>> plt.plot(r, y, label="holonomic function") # doctest: +SKIP
|
458 |
+
>>> plt.show() # doctest: +SKIP
|
459 |
+
|
460 |
+
"""
|
461 |
+
|
462 |
+
_op_priority = 20
|
463 |
+
|
464 |
+
def __init__(self, annihilator, x, x0=0, y0=None):
|
465 |
+
"""
|
466 |
+
|
467 |
+
Parameters
|
468 |
+
==========
|
469 |
+
|
470 |
+
annihilator:
|
471 |
+
Annihilator of the Holonomic Function, represented by a
|
472 |
+
`DifferentialOperator` object.
|
473 |
+
x:
|
474 |
+
Variable of the function.
|
475 |
+
x0:
|
476 |
+
The point at which initial conditions are stored.
|
477 |
+
Generally an integer.
|
478 |
+
y0:
|
479 |
+
The initial condition. The proper format for the initial condition
|
480 |
+
is described in class docstring. To make the function unique,
|
481 |
+
length of the vector `y0` should be equal to or greater than the
|
482 |
+
order of differential equation.
|
483 |
+
"""
|
484 |
+
|
485 |
+
# initial condition
|
486 |
+
self.y0 = y0
|
487 |
+
# the point for initial conditions, default is zero.
|
488 |
+
self.x0 = x0
|
489 |
+
# differential operator L such that L.f = 0
|
490 |
+
self.annihilator = annihilator
|
491 |
+
self.x = x
|
492 |
+
|
493 |
+
def __str__(self):
|
494 |
+
if self._have_init_cond():
|
495 |
+
str_sol = 'HolonomicFunction(%s, %s, %s, %s)' % (str(self.annihilator),\
|
496 |
+
sstr(self.x), sstr(self.x0), sstr(self.y0))
|
497 |
+
else:
|
498 |
+
str_sol = 'HolonomicFunction(%s, %s)' % (str(self.annihilator),\
|
499 |
+
sstr(self.x))
|
500 |
+
|
501 |
+
return str_sol
|
502 |
+
|
503 |
+
__repr__ = __str__
|
504 |
+
|
505 |
+
def unify(self, other):
|
506 |
+
"""
|
507 |
+
Unifies the base polynomial ring of a given two Holonomic
|
508 |
+
Functions.
|
509 |
+
"""
|
510 |
+
|
511 |
+
R1 = self.annihilator.parent.base
|
512 |
+
R2 = other.annihilator.parent.base
|
513 |
+
|
514 |
+
dom1 = R1.dom
|
515 |
+
dom2 = R2.dom
|
516 |
+
|
517 |
+
if R1 == R2:
|
518 |
+
return (self, other)
|
519 |
+
|
520 |
+
R = (dom1.unify(dom2)).old_poly_ring(self.x)
|
521 |
+
|
522 |
+
newparent, _ = DifferentialOperators(R, str(self.annihilator.parent.gen_symbol))
|
523 |
+
|
524 |
+
sol1 = [R1.to_sympy(i) for i in self.annihilator.listofpoly]
|
525 |
+
sol2 = [R2.to_sympy(i) for i in other.annihilator.listofpoly]
|
526 |
+
|
527 |
+
sol1 = DifferentialOperator(sol1, newparent)
|
528 |
+
sol2 = DifferentialOperator(sol2, newparent)
|
529 |
+
|
530 |
+
sol1 = HolonomicFunction(sol1, self.x, self.x0, self.y0)
|
531 |
+
sol2 = HolonomicFunction(sol2, other.x, other.x0, other.y0)
|
532 |
+
|
533 |
+
return (sol1, sol2)
|
534 |
+
|
535 |
+
def is_singularics(self):
|
536 |
+
"""
|
537 |
+
Returns True if the function have singular initial condition
|
538 |
+
in the dictionary format.
|
539 |
+
|
540 |
+
Returns False if the function have ordinary initial condition
|
541 |
+
in the list format.
|
542 |
+
|
543 |
+
Returns None for all other cases.
|
544 |
+
"""
|
545 |
+
|
546 |
+
if isinstance(self.y0, dict):
|
547 |
+
return True
|
548 |
+
elif isinstance(self.y0, list):
|
549 |
+
return False
|
550 |
+
|
551 |
+
def _have_init_cond(self):
|
552 |
+
"""
|
553 |
+
Checks if the function have initial condition.
|
554 |
+
"""
|
555 |
+
return bool(self.y0)
|
556 |
+
|
557 |
+
def _singularics_to_ord(self):
|
558 |
+
"""
|
559 |
+
Converts a singular initial condition to ordinary if possible.
|
560 |
+
"""
|
561 |
+
a = list(self.y0)[0]
|
562 |
+
b = self.y0[a]
|
563 |
+
|
564 |
+
if len(self.y0) == 1 and a == int(a) and a > 0:
|
565 |
+
y0 = []
|
566 |
+
a = int(a)
|
567 |
+
for i in range(a):
|
568 |
+
y0.append(S.Zero)
|
569 |
+
y0 += [j * factorial(a + i) for i, j in enumerate(b)]
|
570 |
+
|
571 |
+
return HolonomicFunction(self.annihilator, self.x, self.x0, y0)
|
572 |
+
|
573 |
+
def __add__(self, other):
|
574 |
+
# if the ground domains are different
|
575 |
+
if self.annihilator.parent.base != other.annihilator.parent.base:
|
576 |
+
a, b = self.unify(other)
|
577 |
+
return a + b
|
578 |
+
|
579 |
+
deg1 = self.annihilator.order
|
580 |
+
deg2 = other.annihilator.order
|
581 |
+
dim = max(deg1, deg2)
|
582 |
+
R = self.annihilator.parent.base
|
583 |
+
K = R.get_field()
|
584 |
+
|
585 |
+
rowsself = [self.annihilator]
|
586 |
+
rowsother = [other.annihilator]
|
587 |
+
gen = self.annihilator.parent.derivative_operator
|
588 |
+
|
589 |
+
# constructing annihilators up to order dim
|
590 |
+
for i in range(dim - deg1):
|
591 |
+
diff1 = (gen * rowsself[-1])
|
592 |
+
rowsself.append(diff1)
|
593 |
+
|
594 |
+
for i in range(dim - deg2):
|
595 |
+
diff2 = (gen * rowsother[-1])
|
596 |
+
rowsother.append(diff2)
|
597 |
+
|
598 |
+
row = rowsself + rowsother
|
599 |
+
|
600 |
+
# constructing the matrix of the ansatz
|
601 |
+
r = []
|
602 |
+
|
603 |
+
for expr in row:
|
604 |
+
p = []
|
605 |
+
for i in range(dim + 1):
|
606 |
+
if i >= len(expr.listofpoly):
|
607 |
+
p.append(K.zero)
|
608 |
+
else:
|
609 |
+
p.append(K.new(expr.listofpoly[i].rep))
|
610 |
+
r.append(p)
|
611 |
+
|
612 |
+
# solving the linear system using gauss jordan solver
|
613 |
+
r = DomainMatrix(r, (len(row), dim+1), K).transpose()
|
614 |
+
homosys = DomainMatrix.zeros((dim+1, 1), K)
|
615 |
+
sol = _find_nonzero_solution(r, homosys)
|
616 |
+
|
617 |
+
# if a solution is not obtained then increasing the order by 1 in each
|
618 |
+
# iteration
|
619 |
+
while sol.is_zero_matrix:
|
620 |
+
dim += 1
|
621 |
+
|
622 |
+
diff1 = (gen * rowsself[-1])
|
623 |
+
rowsself.append(diff1)
|
624 |
+
|
625 |
+
diff2 = (gen * rowsother[-1])
|
626 |
+
rowsother.append(diff2)
|
627 |
+
|
628 |
+
row = rowsself + rowsother
|
629 |
+
r = []
|
630 |
+
|
631 |
+
for expr in row:
|
632 |
+
p = []
|
633 |
+
for i in range(dim + 1):
|
634 |
+
if i >= len(expr.listofpoly):
|
635 |
+
p.append(K.zero)
|
636 |
+
else:
|
637 |
+
p.append(K.new(expr.listofpoly[i].rep))
|
638 |
+
r.append(p)
|
639 |
+
|
640 |
+
# solving the linear system using gauss jordan solver
|
641 |
+
r = DomainMatrix(r, (len(row), dim+1), K).transpose()
|
642 |
+
homosys = DomainMatrix.zeros((dim+1, 1), K)
|
643 |
+
sol = _find_nonzero_solution(r, homosys)
|
644 |
+
|
645 |
+
# taking only the coefficients needed to multiply with `self`
|
646 |
+
# can be also be done the other way by taking R.H.S and multiplying with
|
647 |
+
# `other`
|
648 |
+
sol = sol.flat()[:dim + 1 - deg1]
|
649 |
+
sol1 = _normalize(sol, self.annihilator.parent)
|
650 |
+
# annihilator of the solution
|
651 |
+
sol = sol1 * (self.annihilator)
|
652 |
+
sol = _normalize(sol.listofpoly, self.annihilator.parent, negative=False)
|
653 |
+
|
654 |
+
if not (self._have_init_cond() and other._have_init_cond()):
|
655 |
+
return HolonomicFunction(sol, self.x)
|
656 |
+
|
657 |
+
# both the functions have ordinary initial conditions
|
658 |
+
if self.is_singularics() == False and other.is_singularics() == False:
|
659 |
+
|
660 |
+
# directly add the corresponding value
|
661 |
+
if self.x0 == other.x0:
|
662 |
+
# try to extended the initial conditions
|
663 |
+
# using the annihilator
|
664 |
+
y1 = _extend_y0(self, sol.order)
|
665 |
+
y2 = _extend_y0(other, sol.order)
|
666 |
+
y0 = [a + b for a, b in zip(y1, y2)]
|
667 |
+
return HolonomicFunction(sol, self.x, self.x0, y0)
|
668 |
+
|
669 |
+
else:
|
670 |
+
# change the initial conditions to a same point
|
671 |
+
selfat0 = self.annihilator.is_singular(0)
|
672 |
+
otherat0 = other.annihilator.is_singular(0)
|
673 |
+
|
674 |
+
if self.x0 == 0 and not selfat0 and not otherat0:
|
675 |
+
return self + other.change_ics(0)
|
676 |
+
|
677 |
+
elif other.x0 == 0 and not selfat0 and not otherat0:
|
678 |
+
return self.change_ics(0) + other
|
679 |
+
|
680 |
+
else:
|
681 |
+
selfatx0 = self.annihilator.is_singular(self.x0)
|
682 |
+
otheratx0 = other.annihilator.is_singular(self.x0)
|
683 |
+
|
684 |
+
if not selfatx0 and not otheratx0:
|
685 |
+
return self + other.change_ics(self.x0)
|
686 |
+
|
687 |
+
else:
|
688 |
+
return self.change_ics(other.x0) + other
|
689 |
+
|
690 |
+
if self.x0 != other.x0:
|
691 |
+
return HolonomicFunction(sol, self.x)
|
692 |
+
|
693 |
+
# if the functions have singular_ics
|
694 |
+
y1 = None
|
695 |
+
y2 = None
|
696 |
+
|
697 |
+
if self.is_singularics() == False and other.is_singularics() == True:
|
698 |
+
# convert the ordinary initial condition to singular.
|
699 |
+
_y0 = [j / factorial(i) for i, j in enumerate(self.y0)]
|
700 |
+
y1 = {S.Zero: _y0}
|
701 |
+
y2 = other.y0
|
702 |
+
elif self.is_singularics() == True and other.is_singularics() == False:
|
703 |
+
_y0 = [j / factorial(i) for i, j in enumerate(other.y0)]
|
704 |
+
y1 = self.y0
|
705 |
+
y2 = {S.Zero: _y0}
|
706 |
+
elif self.is_singularics() == True and other.is_singularics() == True:
|
707 |
+
y1 = self.y0
|
708 |
+
y2 = other.y0
|
709 |
+
|
710 |
+
# computing singular initial condition for the result
|
711 |
+
# taking union of the series terms of both functions
|
712 |
+
y0 = {}
|
713 |
+
for i in y1:
|
714 |
+
# add corresponding initial terms if the power
|
715 |
+
# on `x` is same
|
716 |
+
if i in y2:
|
717 |
+
y0[i] = [a + b for a, b in zip(y1[i], y2[i])]
|
718 |
+
else:
|
719 |
+
y0[i] = y1[i]
|
720 |
+
for i in y2:
|
721 |
+
if i not in y1:
|
722 |
+
y0[i] = y2[i]
|
723 |
+
return HolonomicFunction(sol, self.x, self.x0, y0)
|
724 |
+
|
725 |
+
def integrate(self, limits, initcond=False):
|
726 |
+
"""
|
727 |
+
Integrates the given holonomic function.
|
728 |
+
|
729 |
+
Examples
|
730 |
+
========
|
731 |
+
|
732 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
733 |
+
>>> from sympy import QQ
|
734 |
+
>>> from sympy import symbols
|
735 |
+
>>> x = symbols('x')
|
736 |
+
>>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
|
737 |
+
>>> HolonomicFunction(Dx - 1, x, 0, [1]).integrate((x, 0, x)) # e^x - 1
|
738 |
+
HolonomicFunction((-1)*Dx + (1)*Dx**2, x, 0, [0, 1])
|
739 |
+
>>> HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).integrate((x, 0, x))
|
740 |
+
HolonomicFunction((1)*Dx + (1)*Dx**3, x, 0, [0, 1, 0])
|
741 |
+
"""
|
742 |
+
|
743 |
+
# to get the annihilator, just multiply by Dx from right
|
744 |
+
D = self.annihilator.parent.derivative_operator
|
745 |
+
|
746 |
+
# if the function have initial conditions of the series format
|
747 |
+
if self.is_singularics() == True:
|
748 |
+
|
749 |
+
r = self._singularics_to_ord()
|
750 |
+
if r:
|
751 |
+
return r.integrate(limits, initcond=initcond)
|
752 |
+
|
753 |
+
# computing singular initial condition for the function
|
754 |
+
# produced after integration.
|
755 |
+
y0 = {}
|
756 |
+
for i in self.y0:
|
757 |
+
c = self.y0[i]
|
758 |
+
c2 = []
|
759 |
+
for j, cj in enumerate(c):
|
760 |
+
if cj == 0:
|
761 |
+
c2.append(S.Zero)
|
762 |
+
|
763 |
+
# if power on `x` is -1, the integration becomes log(x)
|
764 |
+
# TODO: Implement this case
|
765 |
+
elif i + j + 1 == 0:
|
766 |
+
raise NotImplementedError("logarithmic terms in the series are not supported")
|
767 |
+
else:
|
768 |
+
c2.append(cj / S(i + j + 1))
|
769 |
+
y0[i + 1] = c2
|
770 |
+
|
771 |
+
if hasattr(limits, "__iter__"):
|
772 |
+
raise NotImplementedError("Definite integration for singular initial conditions")
|
773 |
+
|
774 |
+
return HolonomicFunction(self.annihilator * D, self.x, self.x0, y0)
|
775 |
+
|
776 |
+
# if no initial conditions are available for the function
|
777 |
+
if not self._have_init_cond():
|
778 |
+
if initcond:
|
779 |
+
return HolonomicFunction(self.annihilator * D, self.x, self.x0, [S.Zero])
|
780 |
+
return HolonomicFunction(self.annihilator * D, self.x)
|
781 |
+
|
782 |
+
# definite integral
|
783 |
+
# initial conditions for the answer will be stored at point `a`,
|
784 |
+
# where `a` is the lower limit of the integrand
|
785 |
+
if hasattr(limits, "__iter__"):
|
786 |
+
|
787 |
+
if len(limits) == 3 and limits[0] == self.x:
|
788 |
+
x0 = self.x0
|
789 |
+
a = limits[1]
|
790 |
+
b = limits[2]
|
791 |
+
definite = True
|
792 |
+
|
793 |
+
else:
|
794 |
+
definite = False
|
795 |
+
|
796 |
+
y0 = [S.Zero]
|
797 |
+
y0 += self.y0
|
798 |
+
|
799 |
+
indefinite_integral = HolonomicFunction(self.annihilator * D, self.x, self.x0, y0)
|
800 |
+
|
801 |
+
if not definite:
|
802 |
+
return indefinite_integral
|
803 |
+
|
804 |
+
# use evalf to get the values at `a`
|
805 |
+
if x0 != a:
|
806 |
+
try:
|
807 |
+
indefinite_expr = indefinite_integral.to_expr()
|
808 |
+
except (NotHyperSeriesError, NotPowerSeriesError):
|
809 |
+
indefinite_expr = None
|
810 |
+
|
811 |
+
if indefinite_expr:
|
812 |
+
lower = indefinite_expr.subs(self.x, a)
|
813 |
+
if isinstance(lower, NaN):
|
814 |
+
lower = indefinite_expr.limit(self.x, a)
|
815 |
+
else:
|
816 |
+
lower = indefinite_integral.evalf(a)
|
817 |
+
|
818 |
+
if b == self.x:
|
819 |
+
y0[0] = y0[0] - lower
|
820 |
+
return HolonomicFunction(self.annihilator * D, self.x, x0, y0)
|
821 |
+
|
822 |
+
elif S(b).is_Number:
|
823 |
+
if indefinite_expr:
|
824 |
+
upper = indefinite_expr.subs(self.x, b)
|
825 |
+
if isinstance(upper, NaN):
|
826 |
+
upper = indefinite_expr.limit(self.x, b)
|
827 |
+
else:
|
828 |
+
upper = indefinite_integral.evalf(b)
|
829 |
+
|
830 |
+
return upper - lower
|
831 |
+
|
832 |
+
|
833 |
+
# if the upper limit is `x`, the answer will be a function
|
834 |
+
if b == self.x:
|
835 |
+
return HolonomicFunction(self.annihilator * D, self.x, a, y0)
|
836 |
+
|
837 |
+
# if the upper limits is a Number, a numerical value will be returned
|
838 |
+
elif S(b).is_Number:
|
839 |
+
try:
|
840 |
+
s = HolonomicFunction(self.annihilator * D, self.x, a,\
|
841 |
+
y0).to_expr()
|
842 |
+
indefinite = s.subs(self.x, b)
|
843 |
+
if not isinstance(indefinite, NaN):
|
844 |
+
return indefinite
|
845 |
+
else:
|
846 |
+
return s.limit(self.x, b)
|
847 |
+
except (NotHyperSeriesError, NotPowerSeriesError):
|
848 |
+
return HolonomicFunction(self.annihilator * D, self.x, a, y0).evalf(b)
|
849 |
+
|
850 |
+
return HolonomicFunction(self.annihilator * D, self.x)
|
851 |
+
|
852 |
+
def diff(self, *args, **kwargs):
|
853 |
+
r"""
|
854 |
+
Differentiation of the given Holonomic function.
|
855 |
+
|
856 |
+
Examples
|
857 |
+
========
|
858 |
+
|
859 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
860 |
+
>>> from sympy import ZZ
|
861 |
+
>>> from sympy import symbols
|
862 |
+
>>> x = symbols('x')
|
863 |
+
>>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')
|
864 |
+
>>> HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]).diff().to_expr()
|
865 |
+
cos(x)
|
866 |
+
>>> HolonomicFunction(Dx - 2, x, 0, [1]).diff().to_expr()
|
867 |
+
2*exp(2*x)
|
868 |
+
|
869 |
+
See Also
|
870 |
+
========
|
871 |
+
|
872 |
+
integrate
|
873 |
+
"""
|
874 |
+
kwargs.setdefault('evaluate', True)
|
875 |
+
if args:
|
876 |
+
if args[0] != self.x:
|
877 |
+
return S.Zero
|
878 |
+
elif len(args) == 2:
|
879 |
+
sol = self
|
880 |
+
for i in range(args[1]):
|
881 |
+
sol = sol.diff(args[0])
|
882 |
+
return sol
|
883 |
+
|
884 |
+
ann = self.annihilator
|
885 |
+
|
886 |
+
# if the function is constant.
|
887 |
+
if ann.listofpoly[0] == ann.parent.base.zero and ann.order == 1:
|
888 |
+
return S.Zero
|
889 |
+
|
890 |
+
# if the coefficient of y in the differential equation is zero.
|
891 |
+
# a shifting is done to compute the answer in this case.
|
892 |
+
elif ann.listofpoly[0] == ann.parent.base.zero:
|
893 |
+
|
894 |
+
sol = DifferentialOperator(ann.listofpoly[1:], ann.parent)
|
895 |
+
|
896 |
+
if self._have_init_cond():
|
897 |
+
# if ordinary initial condition
|
898 |
+
if self.is_singularics() == False:
|
899 |
+
return HolonomicFunction(sol, self.x, self.x0, self.y0[1:])
|
900 |
+
# TODO: support for singular initial condition
|
901 |
+
return HolonomicFunction(sol, self.x)
|
902 |
+
else:
|
903 |
+
return HolonomicFunction(sol, self.x)
|
904 |
+
|
905 |
+
# the general algorithm
|
906 |
+
R = ann.parent.base
|
907 |
+
K = R.get_field()
|
908 |
+
|
909 |
+
seq_dmf = [K.new(i.rep) for i in ann.listofpoly]
|
910 |
+
|
911 |
+
# -y = a1*y'/a0 + a2*y''/a0 ... + an*y^n/a0
|
912 |
+
rhs = [i / seq_dmf[0] for i in seq_dmf[1:]]
|
913 |
+
rhs.insert(0, K.zero)
|
914 |
+
|
915 |
+
# differentiate both lhs and rhs
|
916 |
+
sol = _derivate_diff_eq(rhs)
|
917 |
+
|
918 |
+
# add the term y' in lhs to rhs
|
919 |
+
sol = _add_lists(sol, [K.zero, K.one])
|
920 |
+
|
921 |
+
sol = _normalize(sol[1:], self.annihilator.parent, negative=False)
|
922 |
+
|
923 |
+
if not self._have_init_cond() or self.is_singularics() == True:
|
924 |
+
return HolonomicFunction(sol, self.x)
|
925 |
+
|
926 |
+
y0 = _extend_y0(self, sol.order + 1)[1:]
|
927 |
+
return HolonomicFunction(sol, self.x, self.x0, y0)
|
928 |
+
|
929 |
+
def __eq__(self, other):
|
930 |
+
if self.annihilator == other.annihilator:
|
931 |
+
if self.x == other.x:
|
932 |
+
if self._have_init_cond() and other._have_init_cond():
|
933 |
+
if self.x0 == other.x0 and self.y0 == other.y0:
|
934 |
+
return True
|
935 |
+
else:
|
936 |
+
return False
|
937 |
+
else:
|
938 |
+
return True
|
939 |
+
else:
|
940 |
+
return False
|
941 |
+
else:
|
942 |
+
return False
|
943 |
+
|
944 |
+
def __mul__(self, other):
|
945 |
+
ann_self = self.annihilator
|
946 |
+
|
947 |
+
if not isinstance(other, HolonomicFunction):
|
948 |
+
other = sympify(other)
|
949 |
+
|
950 |
+
if other.has(self.x):
|
951 |
+
raise NotImplementedError(" Can't multiply a HolonomicFunction and expressions/functions.")
|
952 |
+
|
953 |
+
if not self._have_init_cond():
|
954 |
+
return self
|
955 |
+
else:
|
956 |
+
y0 = _extend_y0(self, ann_self.order)
|
957 |
+
y1 = []
|
958 |
+
|
959 |
+
for j in y0:
|
960 |
+
y1.append((Poly.new(j, self.x) * other).rep)
|
961 |
+
|
962 |
+
return HolonomicFunction(ann_self, self.x, self.x0, y1)
|
963 |
+
|
964 |
+
if self.annihilator.parent.base != other.annihilator.parent.base:
|
965 |
+
a, b = self.unify(other)
|
966 |
+
return a * b
|
967 |
+
|
968 |
+
ann_other = other.annihilator
|
969 |
+
|
970 |
+
list_self = []
|
971 |
+
list_other = []
|
972 |
+
|
973 |
+
a = ann_self.order
|
974 |
+
b = ann_other.order
|
975 |
+
|
976 |
+
R = ann_self.parent.base
|
977 |
+
K = R.get_field()
|
978 |
+
|
979 |
+
for j in ann_self.listofpoly:
|
980 |
+
list_self.append(K.new(j.rep))
|
981 |
+
|
982 |
+
for j in ann_other.listofpoly:
|
983 |
+
list_other.append(K.new(j.rep))
|
984 |
+
|
985 |
+
# will be used to reduce the degree
|
986 |
+
self_red = [-list_self[i] / list_self[a] for i in range(a)]
|
987 |
+
|
988 |
+
other_red = [-list_other[i] / list_other[b] for i in range(b)]
|
989 |
+
|
990 |
+
# coeff_mull[i][j] is the coefficient of Dx^i(f).Dx^j(g)
|
991 |
+
coeff_mul = [[K.zero for i in range(b + 1)] for j in range(a + 1)]
|
992 |
+
coeff_mul[0][0] = K.one
|
993 |
+
|
994 |
+
# making the ansatz
|
995 |
+
lin_sys_elements = [[coeff_mul[i][j] for i in range(a) for j in range(b)]]
|
996 |
+
lin_sys = DomainMatrix(lin_sys_elements, (1, a*b), K).transpose()
|
997 |
+
|
998 |
+
homo_sys = DomainMatrix.zeros((a*b, 1), K)
|
999 |
+
|
1000 |
+
sol = _find_nonzero_solution(lin_sys, homo_sys)
|
1001 |
+
|
1002 |
+
# until a non trivial solution is found
|
1003 |
+
while sol.is_zero_matrix:
|
1004 |
+
|
1005 |
+
# updating the coefficients Dx^i(f).Dx^j(g) for next degree
|
1006 |
+
for i in range(a - 1, -1, -1):
|
1007 |
+
for j in range(b - 1, -1, -1):
|
1008 |
+
coeff_mul[i][j + 1] += coeff_mul[i][j]
|
1009 |
+
coeff_mul[i + 1][j] += coeff_mul[i][j]
|
1010 |
+
if isinstance(coeff_mul[i][j], K.dtype):
|
1011 |
+
coeff_mul[i][j] = DMFdiff(coeff_mul[i][j])
|
1012 |
+
else:
|
1013 |
+
coeff_mul[i][j] = coeff_mul[i][j].diff(self.x)
|
1014 |
+
|
1015 |
+
# reduce the terms to lower power using annihilators of f, g
|
1016 |
+
for i in range(a + 1):
|
1017 |
+
if not coeff_mul[i][b].is_zero:
|
1018 |
+
for j in range(b):
|
1019 |
+
coeff_mul[i][j] += other_red[j] * \
|
1020 |
+
coeff_mul[i][b]
|
1021 |
+
coeff_mul[i][b] = K.zero
|
1022 |
+
|
1023 |
+
# not d2 + 1, as that is already covered in previous loop
|
1024 |
+
for j in range(b):
|
1025 |
+
if not coeff_mul[a][j] == 0:
|
1026 |
+
for i in range(a):
|
1027 |
+
coeff_mul[i][j] += self_red[i] * \
|
1028 |
+
coeff_mul[a][j]
|
1029 |
+
coeff_mul[a][j] = K.zero
|
1030 |
+
|
1031 |
+
lin_sys_elements.append([coeff_mul[i][j] for i in range(a) for j in range(b)])
|
1032 |
+
lin_sys = DomainMatrix(lin_sys_elements, (len(lin_sys_elements), a*b), K).transpose()
|
1033 |
+
|
1034 |
+
sol = _find_nonzero_solution(lin_sys, homo_sys)
|
1035 |
+
|
1036 |
+
sol_ann = _normalize(sol.flat(), self.annihilator.parent, negative=False)
|
1037 |
+
|
1038 |
+
if not (self._have_init_cond() and other._have_init_cond()):
|
1039 |
+
return HolonomicFunction(sol_ann, self.x)
|
1040 |
+
|
1041 |
+
if self.is_singularics() == False and other.is_singularics() == False:
|
1042 |
+
|
1043 |
+
# if both the conditions are at same point
|
1044 |
+
if self.x0 == other.x0:
|
1045 |
+
|
1046 |
+
# try to find more initial conditions
|
1047 |
+
y0_self = _extend_y0(self, sol_ann.order)
|
1048 |
+
y0_other = _extend_y0(other, sol_ann.order)
|
1049 |
+
# h(x0) = f(x0) * g(x0)
|
1050 |
+
y0 = [y0_self[0] * y0_other[0]]
|
1051 |
+
|
1052 |
+
# coefficient of Dx^j(f)*Dx^i(g) in Dx^i(fg)
|
1053 |
+
for i in range(1, min(len(y0_self), len(y0_other))):
|
1054 |
+
coeff = [[0 for i in range(i + 1)] for j in range(i + 1)]
|
1055 |
+
for j in range(i + 1):
|
1056 |
+
for k in range(i + 1):
|
1057 |
+
if j + k == i:
|
1058 |
+
coeff[j][k] = binomial(i, j)
|
1059 |
+
|
1060 |
+
sol = 0
|
1061 |
+
for j in range(i + 1):
|
1062 |
+
for k in range(i + 1):
|
1063 |
+
sol += coeff[j][k]* y0_self[j] * y0_other[k]
|
1064 |
+
|
1065 |
+
y0.append(sol)
|
1066 |
+
|
1067 |
+
return HolonomicFunction(sol_ann, self.x, self.x0, y0)
|
1068 |
+
|
1069 |
+
# if the points are different, consider one
|
1070 |
+
else:
|
1071 |
+
|
1072 |
+
selfat0 = self.annihilator.is_singular(0)
|
1073 |
+
otherat0 = other.annihilator.is_singular(0)
|
1074 |
+
|
1075 |
+
if self.x0 == 0 and not selfat0 and not otherat0:
|
1076 |
+
return self * other.change_ics(0)
|
1077 |
+
|
1078 |
+
elif other.x0 == 0 and not selfat0 and not otherat0:
|
1079 |
+
return self.change_ics(0) * other
|
1080 |
+
|
1081 |
+
else:
|
1082 |
+
selfatx0 = self.annihilator.is_singular(self.x0)
|
1083 |
+
otheratx0 = other.annihilator.is_singular(self.x0)
|
1084 |
+
|
1085 |
+
if not selfatx0 and not otheratx0:
|
1086 |
+
return self * other.change_ics(self.x0)
|
1087 |
+
|
1088 |
+
else:
|
1089 |
+
return self.change_ics(other.x0) * other
|
1090 |
+
|
1091 |
+
if self.x0 != other.x0:
|
1092 |
+
return HolonomicFunction(sol_ann, self.x)
|
1093 |
+
|
1094 |
+
# if the functions have singular_ics
|
1095 |
+
y1 = None
|
1096 |
+
y2 = None
|
1097 |
+
|
1098 |
+
if self.is_singularics() == False and other.is_singularics() == True:
|
1099 |
+
_y0 = [j / factorial(i) for i, j in enumerate(self.y0)]
|
1100 |
+
y1 = {S.Zero: _y0}
|
1101 |
+
y2 = other.y0
|
1102 |
+
elif self.is_singularics() == True and other.is_singularics() == False:
|
1103 |
+
_y0 = [j / factorial(i) for i, j in enumerate(other.y0)]
|
1104 |
+
y1 = self.y0
|
1105 |
+
y2 = {S.Zero: _y0}
|
1106 |
+
elif self.is_singularics() == True and other.is_singularics() == True:
|
1107 |
+
y1 = self.y0
|
1108 |
+
y2 = other.y0
|
1109 |
+
|
1110 |
+
y0 = {}
|
1111 |
+
# multiply every possible pair of the series terms
|
1112 |
+
for i in y1:
|
1113 |
+
for j in y2:
|
1114 |
+
k = min(len(y1[i]), len(y2[j]))
|
1115 |
+
c = []
|
1116 |
+
for a in range(k):
|
1117 |
+
s = S.Zero
|
1118 |
+
for b in range(a + 1):
|
1119 |
+
s += y1[i][b] * y2[j][a - b]
|
1120 |
+
c.append(s)
|
1121 |
+
if not i + j in y0:
|
1122 |
+
y0[i + j] = c
|
1123 |
+
else:
|
1124 |
+
y0[i + j] = [a + b for a, b in zip(c, y0[i + j])]
|
1125 |
+
return HolonomicFunction(sol_ann, self.x, self.x0, y0)
|
1126 |
+
|
1127 |
+
__rmul__ = __mul__
|
1128 |
+
|
1129 |
+
def __sub__(self, other):
|
1130 |
+
return self + other * -1
|
1131 |
+
|
1132 |
+
def __rsub__(self, other):
|
1133 |
+
return self * -1 + other
|
1134 |
+
|
1135 |
+
def __neg__(self):
|
1136 |
+
return -1 * self
|
1137 |
+
|
1138 |
+
def __truediv__(self, other):
|
1139 |
+
return self * (S.One / other)
|
1140 |
+
|
1141 |
+
def __pow__(self, n):
|
1142 |
+
if self.annihilator.order <= 1:
|
1143 |
+
ann = self.annihilator
|
1144 |
+
parent = ann.parent
|
1145 |
+
|
1146 |
+
if self.y0 is None:
|
1147 |
+
y0 = None
|
1148 |
+
else:
|
1149 |
+
y0 = [list(self.y0)[0] ** n]
|
1150 |
+
|
1151 |
+
p0 = ann.listofpoly[0]
|
1152 |
+
p1 = ann.listofpoly[1]
|
1153 |
+
|
1154 |
+
p0 = (Poly.new(p0, self.x) * n).rep
|
1155 |
+
|
1156 |
+
sol = [parent.base.to_sympy(i) for i in [p0, p1]]
|
1157 |
+
dd = DifferentialOperator(sol, parent)
|
1158 |
+
return HolonomicFunction(dd, self.x, self.x0, y0)
|
1159 |
+
if n < 0:
|
1160 |
+
raise NotHolonomicError("Negative Power on a Holonomic Function")
|
1161 |
+
if n == 0:
|
1162 |
+
Dx = self.annihilator.parent.derivative_operator
|
1163 |
+
return HolonomicFunction(Dx, self.x, S.Zero, [S.One])
|
1164 |
+
if n == 1:
|
1165 |
+
return self
|
1166 |
+
else:
|
1167 |
+
if n % 2 == 1:
|
1168 |
+
powreduce = self**(n - 1)
|
1169 |
+
return powreduce * self
|
1170 |
+
elif n % 2 == 0:
|
1171 |
+
powreduce = self**(n / 2)
|
1172 |
+
return powreduce * powreduce
|
1173 |
+
|
1174 |
+
def degree(self):
|
1175 |
+
"""
|
1176 |
+
Returns the highest power of `x` in the annihilator.
|
1177 |
+
"""
|
1178 |
+
sol = [i.degree() for i in self.annihilator.listofpoly]
|
1179 |
+
return max(sol)
|
1180 |
+
|
1181 |
+
def composition(self, expr, *args, **kwargs):
|
1182 |
+
"""
|
1183 |
+
Returns function after composition of a holonomic
|
1184 |
+
function with an algebraic function. The method cannot compute
|
1185 |
+
initial conditions for the result by itself, so they can be also be
|
1186 |
+
provided.
|
1187 |
+
|
1188 |
+
Examples
|
1189 |
+
========
|
1190 |
+
|
1191 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
1192 |
+
>>> from sympy import QQ
|
1193 |
+
>>> from sympy import symbols
|
1194 |
+
>>> x = symbols('x')
|
1195 |
+
>>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
|
1196 |
+
>>> HolonomicFunction(Dx - 1, x).composition(x**2, 0, [1]) # e^(x**2)
|
1197 |
+
HolonomicFunction((-2*x) + (1)*Dx, x, 0, [1])
|
1198 |
+
>>> HolonomicFunction(Dx**2 + 1, x).composition(x**2 - 1, 1, [1, 0])
|
1199 |
+
HolonomicFunction((4*x**3) + (-1)*Dx + (x)*Dx**2, x, 1, [1, 0])
|
1200 |
+
|
1201 |
+
See Also
|
1202 |
+
========
|
1203 |
+
|
1204 |
+
from_hyper
|
1205 |
+
"""
|
1206 |
+
|
1207 |
+
R = self.annihilator.parent
|
1208 |
+
a = self.annihilator.order
|
1209 |
+
diff = expr.diff(self.x)
|
1210 |
+
listofpoly = self.annihilator.listofpoly
|
1211 |
+
|
1212 |
+
for i, j in enumerate(listofpoly):
|
1213 |
+
if isinstance(j, self.annihilator.parent.base.dtype):
|
1214 |
+
listofpoly[i] = self.annihilator.parent.base.to_sympy(j)
|
1215 |
+
|
1216 |
+
r = listofpoly[a].subs({self.x:expr})
|
1217 |
+
subs = [-listofpoly[i].subs({self.x:expr}) / r for i in range (a)]
|
1218 |
+
coeffs = [S.Zero for i in range(a)] # coeffs[i] == coeff of (D^i f)(a) in D^k (f(a))
|
1219 |
+
coeffs[0] = S.One
|
1220 |
+
system = [coeffs]
|
1221 |
+
homogeneous = Matrix([[S.Zero for i in range(a)]]).transpose()
|
1222 |
+
while True:
|
1223 |
+
coeffs_next = [p.diff(self.x) for p in coeffs]
|
1224 |
+
for i in range(a - 1):
|
1225 |
+
coeffs_next[i + 1] += (coeffs[i] * diff)
|
1226 |
+
for i in range(a):
|
1227 |
+
coeffs_next[i] += (coeffs[-1] * subs[i] * diff)
|
1228 |
+
coeffs = coeffs_next
|
1229 |
+
# check for linear relations
|
1230 |
+
system.append(coeffs)
|
1231 |
+
sol, taus = (Matrix(system).transpose()
|
1232 |
+
).gauss_jordan_solve(homogeneous)
|
1233 |
+
if sol.is_zero_matrix is not True:
|
1234 |
+
break
|
1235 |
+
|
1236 |
+
tau = list(taus)[0]
|
1237 |
+
sol = sol.subs(tau, 1)
|
1238 |
+
sol = _normalize(sol[0:], R, negative=False)
|
1239 |
+
|
1240 |
+
# if initial conditions are given for the resulting function
|
1241 |
+
if args:
|
1242 |
+
return HolonomicFunction(sol, self.x, args[0], args[1])
|
1243 |
+
return HolonomicFunction(sol, self.x)
|
1244 |
+
|
1245 |
+
def to_sequence(self, lb=True):
|
1246 |
+
r"""
|
1247 |
+
Finds recurrence relation for the coefficients in the series expansion
|
1248 |
+
of the function about :math:`x_0`, where :math:`x_0` is the point at
|
1249 |
+
which the initial condition is stored.
|
1250 |
+
|
1251 |
+
Explanation
|
1252 |
+
===========
|
1253 |
+
|
1254 |
+
If the point :math:`x_0` is ordinary, solution of the form :math:`[(R, n_0)]`
|
1255 |
+
is returned. Where :math:`R` is the recurrence relation and :math:`n_0` is the
|
1256 |
+
smallest ``n`` for which the recurrence holds true.
|
1257 |
+
|
1258 |
+
If the point :math:`x_0` is regular singular, a list of solutions in
|
1259 |
+
the format :math:`(R, p, n_0)` is returned, i.e. `[(R, p, n_0), ... ]`.
|
1260 |
+
Each tuple in this vector represents a recurrence relation :math:`R`
|
1261 |
+
associated with a root of the indicial equation ``p``. Conditions of
|
1262 |
+
a different format can also be provided in this case, see the
|
1263 |
+
docstring of HolonomicFunction class.
|
1264 |
+
|
1265 |
+
If it's not possible to numerically compute a initial condition,
|
1266 |
+
it is returned as a symbol :math:`C_j`, denoting the coefficient of
|
1267 |
+
:math:`(x - x_0)^j` in the power series about :math:`x_0`.
|
1268 |
+
|
1269 |
+
Examples
|
1270 |
+
========
|
1271 |
+
|
1272 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
1273 |
+
>>> from sympy import QQ
|
1274 |
+
>>> from sympy import symbols, S
|
1275 |
+
>>> x = symbols('x')
|
1276 |
+
>>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
|
1277 |
+
>>> HolonomicFunction(Dx - 1, x, 0, [1]).to_sequence()
|
1278 |
+
[(HolonomicSequence((-1) + (n + 1)Sn, n), u(0) = 1, 0)]
|
1279 |
+
>>> HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1]).to_sequence()
|
1280 |
+
[(HolonomicSequence((n**2) + (n**2 + n)Sn, n), u(0) = 0, u(1) = 1, u(2) = -1/2, 2)]
|
1281 |
+
>>> HolonomicFunction(-S(1)/2 + x*Dx, x, 0, {S(1)/2: [1]}).to_sequence()
|
1282 |
+
[(HolonomicSequence((n), n), u(0) = 1, 1/2, 1)]
|
1283 |
+
|
1284 |
+
See Also
|
1285 |
+
========
|
1286 |
+
|
1287 |
+
HolonomicFunction.series
|
1288 |
+
|
1289 |
+
References
|
1290 |
+
==========
|
1291 |
+
|
1292 |
+
.. [1] https://hal.inria.fr/inria-00070025/document
|
1293 |
+
.. [2] https://www3.risc.jku.at/publications/download/risc_2244/DIPLFORM.pdf
|
1294 |
+
|
1295 |
+
"""
|
1296 |
+
|
1297 |
+
if self.x0 != 0:
|
1298 |
+
return self.shift_x(self.x0).to_sequence()
|
1299 |
+
|
1300 |
+
# check whether a power series exists if the point is singular
|
1301 |
+
if self.annihilator.is_singular(self.x0):
|
1302 |
+
return self._frobenius(lb=lb)
|
1303 |
+
|
1304 |
+
dict1 = {}
|
1305 |
+
n = Symbol('n', integer=True)
|
1306 |
+
dom = self.annihilator.parent.base.dom
|
1307 |
+
R, _ = RecurrenceOperators(dom.old_poly_ring(n), 'Sn')
|
1308 |
+
|
1309 |
+
# substituting each term of the form `x^k Dx^j` in the
|
1310 |
+
# annihilator, according to the formula below:
|
1311 |
+
# x^k Dx^j = Sum(rf(n + 1 - k, j) * a(n + j - k) * x^n, (n, k, oo))
|
1312 |
+
# for explanation see [2].
|
1313 |
+
for i, j in enumerate(self.annihilator.listofpoly):
|
1314 |
+
|
1315 |
+
listofdmp = j.all_coeffs()
|
1316 |
+
degree = len(listofdmp) - 1
|
1317 |
+
|
1318 |
+
for k in range(degree + 1):
|
1319 |
+
coeff = listofdmp[degree - k]
|
1320 |
+
|
1321 |
+
if coeff == 0:
|
1322 |
+
continue
|
1323 |
+
|
1324 |
+
if (i - k, k) in dict1:
|
1325 |
+
dict1[(i - k, k)] += (dom.to_sympy(coeff) * rf(n - k + 1, i))
|
1326 |
+
else:
|
1327 |
+
dict1[(i - k, k)] = (dom.to_sympy(coeff) * rf(n - k + 1, i))
|
1328 |
+
|
1329 |
+
|
1330 |
+
sol = []
|
1331 |
+
keylist = [i[0] for i in dict1]
|
1332 |
+
lower = min(keylist)
|
1333 |
+
upper = max(keylist)
|
1334 |
+
degree = self.degree()
|
1335 |
+
|
1336 |
+
# the recurrence relation holds for all values of
|
1337 |
+
# n greater than smallest_n, i.e. n >= smallest_n
|
1338 |
+
smallest_n = lower + degree
|
1339 |
+
dummys = {}
|
1340 |
+
eqs = []
|
1341 |
+
unknowns = []
|
1342 |
+
|
1343 |
+
# an appropriate shift of the recurrence
|
1344 |
+
for j in range(lower, upper + 1):
|
1345 |
+
if j in keylist:
|
1346 |
+
temp = S.Zero
|
1347 |
+
for k in dict1.keys():
|
1348 |
+
if k[0] == j:
|
1349 |
+
temp += dict1[k].subs(n, n - lower)
|
1350 |
+
sol.append(temp)
|
1351 |
+
else:
|
1352 |
+
sol.append(S.Zero)
|
1353 |
+
|
1354 |
+
# the recurrence relation
|
1355 |
+
sol = RecurrenceOperator(sol, R)
|
1356 |
+
|
1357 |
+
# computing the initial conditions for recurrence
|
1358 |
+
order = sol.order
|
1359 |
+
all_roots = roots(R.base.to_sympy(sol.listofpoly[-1]), n, filter='Z')
|
1360 |
+
all_roots = all_roots.keys()
|
1361 |
+
|
1362 |
+
if all_roots:
|
1363 |
+
max_root = max(all_roots) + 1
|
1364 |
+
smallest_n = max(max_root, smallest_n)
|
1365 |
+
order += smallest_n
|
1366 |
+
|
1367 |
+
y0 = _extend_y0(self, order)
|
1368 |
+
u0 = []
|
1369 |
+
|
1370 |
+
# u(n) = y^n(0)/factorial(n)
|
1371 |
+
for i, j in enumerate(y0):
|
1372 |
+
u0.append(j / factorial(i))
|
1373 |
+
|
1374 |
+
# if sufficient conditions can't be computed then
|
1375 |
+
# try to use the series method i.e.
|
1376 |
+
# equate the coefficients of x^k in the equation formed by
|
1377 |
+
# substituting the series in differential equation, to zero.
|
1378 |
+
if len(u0) < order:
|
1379 |
+
|
1380 |
+
for i in range(degree):
|
1381 |
+
eq = S.Zero
|
1382 |
+
|
1383 |
+
for j in dict1:
|
1384 |
+
|
1385 |
+
if i + j[0] < 0:
|
1386 |
+
dummys[i + j[0]] = S.Zero
|
1387 |
+
|
1388 |
+
elif i + j[0] < len(u0):
|
1389 |
+
dummys[i + j[0]] = u0[i + j[0]]
|
1390 |
+
|
1391 |
+
elif not i + j[0] in dummys:
|
1392 |
+
dummys[i + j[0]] = Symbol('C_%s' %(i + j[0]))
|
1393 |
+
unknowns.append(dummys[i + j[0]])
|
1394 |
+
|
1395 |
+
if j[1] <= i:
|
1396 |
+
eq += dict1[j].subs(n, i) * dummys[i + j[0]]
|
1397 |
+
|
1398 |
+
eqs.append(eq)
|
1399 |
+
|
1400 |
+
# solve the system of equations formed
|
1401 |
+
soleqs = solve(eqs, *unknowns)
|
1402 |
+
|
1403 |
+
if isinstance(soleqs, dict):
|
1404 |
+
|
1405 |
+
for i in range(len(u0), order):
|
1406 |
+
|
1407 |
+
if i not in dummys:
|
1408 |
+
dummys[i] = Symbol('C_%s' %i)
|
1409 |
+
|
1410 |
+
if dummys[i] in soleqs:
|
1411 |
+
u0.append(soleqs[dummys[i]])
|
1412 |
+
|
1413 |
+
else:
|
1414 |
+
u0.append(dummys[i])
|
1415 |
+
|
1416 |
+
if lb:
|
1417 |
+
return [(HolonomicSequence(sol, u0), smallest_n)]
|
1418 |
+
return [HolonomicSequence(sol, u0)]
|
1419 |
+
|
1420 |
+
for i in range(len(u0), order):
|
1421 |
+
|
1422 |
+
if i not in dummys:
|
1423 |
+
dummys[i] = Symbol('C_%s' %i)
|
1424 |
+
|
1425 |
+
s = False
|
1426 |
+
for j in soleqs:
|
1427 |
+
if dummys[i] in j:
|
1428 |
+
u0.append(j[dummys[i]])
|
1429 |
+
s = True
|
1430 |
+
if not s:
|
1431 |
+
u0.append(dummys[i])
|
1432 |
+
|
1433 |
+
if lb:
|
1434 |
+
return [(HolonomicSequence(sol, u0), smallest_n)]
|
1435 |
+
|
1436 |
+
return [HolonomicSequence(sol, u0)]
|
1437 |
+
|
1438 |
+
def _frobenius(self, lb=True):
|
1439 |
+
# compute the roots of indicial equation
|
1440 |
+
indicialroots = self._indicial()
|
1441 |
+
|
1442 |
+
reals = []
|
1443 |
+
compl = []
|
1444 |
+
for i in ordered(indicialroots.keys()):
|
1445 |
+
if i.is_real:
|
1446 |
+
reals.extend([i] * indicialroots[i])
|
1447 |
+
else:
|
1448 |
+
a, b = i.as_real_imag()
|
1449 |
+
compl.extend([(i, a, b)] * indicialroots[i])
|
1450 |
+
|
1451 |
+
# sort the roots for a fixed ordering of solution
|
1452 |
+
compl.sort(key=lambda x : x[1])
|
1453 |
+
compl.sort(key=lambda x : x[2])
|
1454 |
+
reals.sort()
|
1455 |
+
|
1456 |
+
# grouping the roots, roots differ by an integer are put in the same group.
|
1457 |
+
grp = []
|
1458 |
+
|
1459 |
+
for i in reals:
|
1460 |
+
intdiff = False
|
1461 |
+
if len(grp) == 0:
|
1462 |
+
grp.append([i])
|
1463 |
+
continue
|
1464 |
+
for j in grp:
|
1465 |
+
if int(j[0] - i) == j[0] - i:
|
1466 |
+
j.append(i)
|
1467 |
+
intdiff = True
|
1468 |
+
break
|
1469 |
+
if not intdiff:
|
1470 |
+
grp.append([i])
|
1471 |
+
|
1472 |
+
# True if none of the roots differ by an integer i.e.
|
1473 |
+
# each element in group have only one member
|
1474 |
+
independent = True if all(len(i) == 1 for i in grp) else False
|
1475 |
+
|
1476 |
+
allpos = all(i >= 0 for i in reals)
|
1477 |
+
allint = all(int(i) == i for i in reals)
|
1478 |
+
|
1479 |
+
# if initial conditions are provided
|
1480 |
+
# then use them.
|
1481 |
+
if self.is_singularics() == True:
|
1482 |
+
rootstoconsider = []
|
1483 |
+
for i in ordered(self.y0.keys()):
|
1484 |
+
for j in ordered(indicialroots.keys()):
|
1485 |
+
if equal_valued(j, i):
|
1486 |
+
rootstoconsider.append(i)
|
1487 |
+
|
1488 |
+
elif allpos and allint:
|
1489 |
+
rootstoconsider = [min(reals)]
|
1490 |
+
|
1491 |
+
elif independent:
|
1492 |
+
rootstoconsider = [i[0] for i in grp] + [j[0] for j in compl]
|
1493 |
+
|
1494 |
+
elif not allint:
|
1495 |
+
rootstoconsider = []
|
1496 |
+
for i in reals:
|
1497 |
+
if not int(i) == i:
|
1498 |
+
rootstoconsider.append(i)
|
1499 |
+
|
1500 |
+
elif not allpos:
|
1501 |
+
|
1502 |
+
if not self._have_init_cond() or S(self.y0[0]).is_finite == False:
|
1503 |
+
rootstoconsider = [min(reals)]
|
1504 |
+
|
1505 |
+
else:
|
1506 |
+
posroots = []
|
1507 |
+
for i in reals:
|
1508 |
+
if i >= 0:
|
1509 |
+
posroots.append(i)
|
1510 |
+
rootstoconsider = [min(posroots)]
|
1511 |
+
|
1512 |
+
n = Symbol('n', integer=True)
|
1513 |
+
dom = self.annihilator.parent.base.dom
|
1514 |
+
R, _ = RecurrenceOperators(dom.old_poly_ring(n), 'Sn')
|
1515 |
+
|
1516 |
+
finalsol = []
|
1517 |
+
char = ord('C')
|
1518 |
+
|
1519 |
+
for p in rootstoconsider:
|
1520 |
+
dict1 = {}
|
1521 |
+
|
1522 |
+
for i, j in enumerate(self.annihilator.listofpoly):
|
1523 |
+
|
1524 |
+
listofdmp = j.all_coeffs()
|
1525 |
+
degree = len(listofdmp) - 1
|
1526 |
+
|
1527 |
+
for k in range(degree + 1):
|
1528 |
+
coeff = listofdmp[degree - k]
|
1529 |
+
|
1530 |
+
if coeff == 0:
|
1531 |
+
continue
|
1532 |
+
|
1533 |
+
if (i - k, k - i) in dict1:
|
1534 |
+
dict1[(i - k, k - i)] += (dom.to_sympy(coeff) * rf(n - k + 1 + p, i))
|
1535 |
+
else:
|
1536 |
+
dict1[(i - k, k - i)] = (dom.to_sympy(coeff) * rf(n - k + 1 + p, i))
|
1537 |
+
|
1538 |
+
sol = []
|
1539 |
+
keylist = [i[0] for i in dict1]
|
1540 |
+
lower = min(keylist)
|
1541 |
+
upper = max(keylist)
|
1542 |
+
degree = max([i[1] for i in dict1])
|
1543 |
+
degree2 = min([i[1] for i in dict1])
|
1544 |
+
|
1545 |
+
smallest_n = lower + degree
|
1546 |
+
dummys = {}
|
1547 |
+
eqs = []
|
1548 |
+
unknowns = []
|
1549 |
+
|
1550 |
+
for j in range(lower, upper + 1):
|
1551 |
+
if j in keylist:
|
1552 |
+
temp = S.Zero
|
1553 |
+
for k in dict1.keys():
|
1554 |
+
if k[0] == j:
|
1555 |
+
temp += dict1[k].subs(n, n - lower)
|
1556 |
+
sol.append(temp)
|
1557 |
+
else:
|
1558 |
+
sol.append(S.Zero)
|
1559 |
+
|
1560 |
+
# the recurrence relation
|
1561 |
+
sol = RecurrenceOperator(sol, R)
|
1562 |
+
|
1563 |
+
# computing the initial conditions for recurrence
|
1564 |
+
order = sol.order
|
1565 |
+
all_roots = roots(R.base.to_sympy(sol.listofpoly[-1]), n, filter='Z')
|
1566 |
+
all_roots = all_roots.keys()
|
1567 |
+
|
1568 |
+
if all_roots:
|
1569 |
+
max_root = max(all_roots) + 1
|
1570 |
+
smallest_n = max(max_root, smallest_n)
|
1571 |
+
order += smallest_n
|
1572 |
+
|
1573 |
+
u0 = []
|
1574 |
+
|
1575 |
+
if self.is_singularics() == True:
|
1576 |
+
u0 = self.y0[p]
|
1577 |
+
|
1578 |
+
elif self.is_singularics() == False and p >= 0 and int(p) == p and len(rootstoconsider) == 1:
|
1579 |
+
y0 = _extend_y0(self, order + int(p))
|
1580 |
+
# u(n) = y^n(0)/factorial(n)
|
1581 |
+
if len(y0) > int(p):
|
1582 |
+
for i in range(int(p), len(y0)):
|
1583 |
+
u0.append(y0[i] / factorial(i))
|
1584 |
+
|
1585 |
+
if len(u0) < order:
|
1586 |
+
|
1587 |
+
for i in range(degree2, degree):
|
1588 |
+
eq = S.Zero
|
1589 |
+
|
1590 |
+
for j in dict1:
|
1591 |
+
if i + j[0] < 0:
|
1592 |
+
dummys[i + j[0]] = S.Zero
|
1593 |
+
|
1594 |
+
elif i + j[0] < len(u0):
|
1595 |
+
dummys[i + j[0]] = u0[i + j[0]]
|
1596 |
+
|
1597 |
+
elif not i + j[0] in dummys:
|
1598 |
+
letter = chr(char) + '_%s' %(i + j[0])
|
1599 |
+
dummys[i + j[0]] = Symbol(letter)
|
1600 |
+
unknowns.append(dummys[i + j[0]])
|
1601 |
+
|
1602 |
+
if j[1] <= i:
|
1603 |
+
eq += dict1[j].subs(n, i) * dummys[i + j[0]]
|
1604 |
+
|
1605 |
+
eqs.append(eq)
|
1606 |
+
|
1607 |
+
# solve the system of equations formed
|
1608 |
+
soleqs = solve(eqs, *unknowns)
|
1609 |
+
|
1610 |
+
if isinstance(soleqs, dict):
|
1611 |
+
|
1612 |
+
for i in range(len(u0), order):
|
1613 |
+
|
1614 |
+
if i not in dummys:
|
1615 |
+
letter = chr(char) + '_%s' %i
|
1616 |
+
dummys[i] = Symbol(letter)
|
1617 |
+
|
1618 |
+
if dummys[i] in soleqs:
|
1619 |
+
u0.append(soleqs[dummys[i]])
|
1620 |
+
|
1621 |
+
else:
|
1622 |
+
u0.append(dummys[i])
|
1623 |
+
|
1624 |
+
if lb:
|
1625 |
+
finalsol.append((HolonomicSequence(sol, u0), p, smallest_n))
|
1626 |
+
continue
|
1627 |
+
else:
|
1628 |
+
finalsol.append((HolonomicSequence(sol, u0), p))
|
1629 |
+
continue
|
1630 |
+
|
1631 |
+
for i in range(len(u0), order):
|
1632 |
+
|
1633 |
+
if i not in dummys:
|
1634 |
+
letter = chr(char) + '_%s' %i
|
1635 |
+
dummys[i] = Symbol(letter)
|
1636 |
+
|
1637 |
+
s = False
|
1638 |
+
for j in soleqs:
|
1639 |
+
if dummys[i] in j:
|
1640 |
+
u0.append(j[dummys[i]])
|
1641 |
+
s = True
|
1642 |
+
if not s:
|
1643 |
+
u0.append(dummys[i])
|
1644 |
+
if lb:
|
1645 |
+
finalsol.append((HolonomicSequence(sol, u0), p, smallest_n))
|
1646 |
+
|
1647 |
+
else:
|
1648 |
+
finalsol.append((HolonomicSequence(sol, u0), p))
|
1649 |
+
char += 1
|
1650 |
+
return finalsol
|
1651 |
+
|
1652 |
+
def series(self, n=6, coefficient=False, order=True, _recur=None):
|
1653 |
+
r"""
|
1654 |
+
Finds the power series expansion of given holonomic function about :math:`x_0`.
|
1655 |
+
|
1656 |
+
Explanation
|
1657 |
+
===========
|
1658 |
+
|
1659 |
+
A list of series might be returned if :math:`x_0` is a regular point with
|
1660 |
+
multiple roots of the indicial equation.
|
1661 |
+
|
1662 |
+
Examples
|
1663 |
+
========
|
1664 |
+
|
1665 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
1666 |
+
>>> from sympy import QQ
|
1667 |
+
>>> from sympy import symbols
|
1668 |
+
>>> x = symbols('x')
|
1669 |
+
>>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
|
1670 |
+
>>> HolonomicFunction(Dx - 1, x, 0, [1]).series() # e^x
|
1671 |
+
1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + O(x**6)
|
1672 |
+
>>> HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]).series(n=8) # sin(x)
|
1673 |
+
x - x**3/6 + x**5/120 - x**7/5040 + O(x**8)
|
1674 |
+
|
1675 |
+
See Also
|
1676 |
+
========
|
1677 |
+
|
1678 |
+
HolonomicFunction.to_sequence
|
1679 |
+
"""
|
1680 |
+
|
1681 |
+
if _recur is None:
|
1682 |
+
recurrence = self.to_sequence()
|
1683 |
+
else:
|
1684 |
+
recurrence = _recur
|
1685 |
+
|
1686 |
+
if isinstance(recurrence, tuple) and len(recurrence) == 2:
|
1687 |
+
recurrence = recurrence[0]
|
1688 |
+
constantpower = 0
|
1689 |
+
elif isinstance(recurrence, tuple) and len(recurrence) == 3:
|
1690 |
+
constantpower = recurrence[1]
|
1691 |
+
recurrence = recurrence[0]
|
1692 |
+
|
1693 |
+
elif len(recurrence) == 1 and len(recurrence[0]) == 2:
|
1694 |
+
recurrence = recurrence[0][0]
|
1695 |
+
constantpower = 0
|
1696 |
+
elif len(recurrence) == 1 and len(recurrence[0]) == 3:
|
1697 |
+
constantpower = recurrence[0][1]
|
1698 |
+
recurrence = recurrence[0][0]
|
1699 |
+
else:
|
1700 |
+
sol = []
|
1701 |
+
for i in recurrence:
|
1702 |
+
sol.append(self.series(_recur=i))
|
1703 |
+
return sol
|
1704 |
+
|
1705 |
+
n = n - int(constantpower)
|
1706 |
+
l = len(recurrence.u0) - 1
|
1707 |
+
k = recurrence.recurrence.order
|
1708 |
+
x = self.x
|
1709 |
+
x0 = self.x0
|
1710 |
+
seq_dmp = recurrence.recurrence.listofpoly
|
1711 |
+
R = recurrence.recurrence.parent.base
|
1712 |
+
K = R.get_field()
|
1713 |
+
seq = []
|
1714 |
+
|
1715 |
+
for i, j in enumerate(seq_dmp):
|
1716 |
+
seq.append(K.new(j.rep))
|
1717 |
+
|
1718 |
+
sub = [-seq[i] / seq[k] for i in range(k)]
|
1719 |
+
sol = list(recurrence.u0)
|
1720 |
+
|
1721 |
+
if l + 1 >= n:
|
1722 |
+
pass
|
1723 |
+
else:
|
1724 |
+
# use the initial conditions to find the next term
|
1725 |
+
for i in range(l + 1 - k, n - k):
|
1726 |
+
coeff = S.Zero
|
1727 |
+
for j in range(k):
|
1728 |
+
if i + j >= 0:
|
1729 |
+
coeff += DMFsubs(sub[j], i) * sol[i + j]
|
1730 |
+
sol.append(coeff)
|
1731 |
+
|
1732 |
+
if coefficient:
|
1733 |
+
return sol
|
1734 |
+
|
1735 |
+
ser = S.Zero
|
1736 |
+
for i, j in enumerate(sol):
|
1737 |
+
ser += x**(i + constantpower) * j
|
1738 |
+
if order:
|
1739 |
+
ser += Order(x**(n + int(constantpower)), x)
|
1740 |
+
if x0 != 0:
|
1741 |
+
return ser.subs(x, x - x0)
|
1742 |
+
return ser
|
1743 |
+
|
1744 |
+
def _indicial(self):
|
1745 |
+
"""
|
1746 |
+
Computes roots of the Indicial equation.
|
1747 |
+
"""
|
1748 |
+
|
1749 |
+
if self.x0 != 0:
|
1750 |
+
return self.shift_x(self.x0)._indicial()
|
1751 |
+
|
1752 |
+
list_coeff = self.annihilator.listofpoly
|
1753 |
+
R = self.annihilator.parent.base
|
1754 |
+
x = self.x
|
1755 |
+
s = R.zero
|
1756 |
+
y = R.one
|
1757 |
+
|
1758 |
+
def _pole_degree(poly):
|
1759 |
+
root_all = roots(R.to_sympy(poly), x, filter='Z')
|
1760 |
+
if 0 in root_all.keys():
|
1761 |
+
return root_all[0]
|
1762 |
+
else:
|
1763 |
+
return 0
|
1764 |
+
|
1765 |
+
degree = [j.degree() for j in list_coeff]
|
1766 |
+
degree = max(degree)
|
1767 |
+
inf = 10 * (max(1, degree) + max(1, self.annihilator.order))
|
1768 |
+
|
1769 |
+
deg = lambda q: inf if q.is_zero else _pole_degree(q)
|
1770 |
+
b = deg(list_coeff[0])
|
1771 |
+
|
1772 |
+
for j in range(1, len(list_coeff)):
|
1773 |
+
b = min(b, deg(list_coeff[j]) - j)
|
1774 |
+
|
1775 |
+
for i, j in enumerate(list_coeff):
|
1776 |
+
listofdmp = j.all_coeffs()
|
1777 |
+
degree = len(listofdmp) - 1
|
1778 |
+
if - i - b <= 0 and degree - i - b >= 0:
|
1779 |
+
s = s + listofdmp[degree - i - b] * y
|
1780 |
+
y *= x - i
|
1781 |
+
|
1782 |
+
return roots(R.to_sympy(s), x)
|
1783 |
+
|
1784 |
+
def evalf(self, points, method='RK4', h=0.05, derivatives=False):
|
1785 |
+
r"""
|
1786 |
+
Finds numerical value of a holonomic function using numerical methods.
|
1787 |
+
(RK4 by default). A set of points (real or complex) must be provided
|
1788 |
+
which will be the path for the numerical integration.
|
1789 |
+
|
1790 |
+
Explanation
|
1791 |
+
===========
|
1792 |
+
|
1793 |
+
The path should be given as a list :math:`[x_1, x_2, \dots x_n]`. The numerical
|
1794 |
+
values will be computed at each point in this order
|
1795 |
+
:math:`x_1 \rightarrow x_2 \rightarrow x_3 \dots \rightarrow x_n`.
|
1796 |
+
|
1797 |
+
Returns values of the function at :math:`x_1, x_2, \dots x_n` in a list.
|
1798 |
+
|
1799 |
+
Examples
|
1800 |
+
========
|
1801 |
+
|
1802 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
1803 |
+
>>> from sympy import QQ
|
1804 |
+
>>> from sympy import symbols
|
1805 |
+
>>> x = symbols('x')
|
1806 |
+
>>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
|
1807 |
+
|
1808 |
+
A straight line on the real axis from (0 to 1)
|
1809 |
+
|
1810 |
+
>>> r = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
|
1811 |
+
|
1812 |
+
Runge-Kutta 4th order on e^x from 0.1 to 1.
|
1813 |
+
Exact solution at 1 is 2.71828182845905
|
1814 |
+
|
1815 |
+
>>> HolonomicFunction(Dx - 1, x, 0, [1]).evalf(r)
|
1816 |
+
[1.10517083333333, 1.22140257085069, 1.34985849706254, 1.49182424008069,
|
1817 |
+
1.64872063859684, 1.82211796209193, 2.01375162659678, 2.22553956329232,
|
1818 |
+
2.45960141378007, 2.71827974413517]
|
1819 |
+
|
1820 |
+
Euler's method for the same
|
1821 |
+
|
1822 |
+
>>> HolonomicFunction(Dx - 1, x, 0, [1]).evalf(r, method='Euler')
|
1823 |
+
[1.1, 1.21, 1.331, 1.4641, 1.61051, 1.771561, 1.9487171, 2.14358881,
|
1824 |
+
2.357947691, 2.5937424601]
|
1825 |
+
|
1826 |
+
One can also observe that the value obtained using Runge-Kutta 4th order
|
1827 |
+
is much more accurate than Euler's method.
|
1828 |
+
"""
|
1829 |
+
|
1830 |
+
from sympy.holonomic.numerical import _evalf
|
1831 |
+
lp = False
|
1832 |
+
|
1833 |
+
# if a point `b` is given instead of a mesh
|
1834 |
+
if not hasattr(points, "__iter__"):
|
1835 |
+
lp = True
|
1836 |
+
b = S(points)
|
1837 |
+
if self.x0 == b:
|
1838 |
+
return _evalf(self, [b], method=method, derivatives=derivatives)[-1]
|
1839 |
+
|
1840 |
+
if not b.is_Number:
|
1841 |
+
raise NotImplementedError
|
1842 |
+
|
1843 |
+
a = self.x0
|
1844 |
+
if a > b:
|
1845 |
+
h = -h
|
1846 |
+
n = int((b - a) / h)
|
1847 |
+
points = [a + h]
|
1848 |
+
for i in range(n - 1):
|
1849 |
+
points.append(points[-1] + h)
|
1850 |
+
|
1851 |
+
for i in roots(self.annihilator.parent.base.to_sympy(self.annihilator.listofpoly[-1]), self.x):
|
1852 |
+
if i == self.x0 or i in points:
|
1853 |
+
raise SingularityError(self, i)
|
1854 |
+
|
1855 |
+
if lp:
|
1856 |
+
return _evalf(self, points, method=method, derivatives=derivatives)[-1]
|
1857 |
+
return _evalf(self, points, method=method, derivatives=derivatives)
|
1858 |
+
|
1859 |
+
def change_x(self, z):
|
1860 |
+
"""
|
1861 |
+
Changes only the variable of Holonomic Function, for internal
|
1862 |
+
purposes. For composition use HolonomicFunction.composition()
|
1863 |
+
"""
|
1864 |
+
|
1865 |
+
dom = self.annihilator.parent.base.dom
|
1866 |
+
R = dom.old_poly_ring(z)
|
1867 |
+
parent, _ = DifferentialOperators(R, 'Dx')
|
1868 |
+
sol = []
|
1869 |
+
for j in self.annihilator.listofpoly:
|
1870 |
+
sol.append(R(j.rep))
|
1871 |
+
sol = DifferentialOperator(sol, parent)
|
1872 |
+
return HolonomicFunction(sol, z, self.x0, self.y0)
|
1873 |
+
|
1874 |
+
def shift_x(self, a):
|
1875 |
+
"""
|
1876 |
+
Substitute `x + a` for `x`.
|
1877 |
+
"""
|
1878 |
+
|
1879 |
+
x = self.x
|
1880 |
+
listaftershift = self.annihilator.listofpoly
|
1881 |
+
base = self.annihilator.parent.base
|
1882 |
+
|
1883 |
+
sol = [base.from_sympy(base.to_sympy(i).subs(x, x + a)) for i in listaftershift]
|
1884 |
+
sol = DifferentialOperator(sol, self.annihilator.parent)
|
1885 |
+
x0 = self.x0 - a
|
1886 |
+
if not self._have_init_cond():
|
1887 |
+
return HolonomicFunction(sol, x)
|
1888 |
+
return HolonomicFunction(sol, x, x0, self.y0)
|
1889 |
+
|
1890 |
+
def to_hyper(self, as_list=False, _recur=None):
|
1891 |
+
r"""
|
1892 |
+
Returns a hypergeometric function (or linear combination of them)
|
1893 |
+
representing the given holonomic function.
|
1894 |
+
|
1895 |
+
Explanation
|
1896 |
+
===========
|
1897 |
+
|
1898 |
+
Returns an answer of the form:
|
1899 |
+
`a_1 \cdot x^{b_1} \cdot{hyper()} + a_2 \cdot x^{b_2} \cdot{hyper()} \dots`
|
1900 |
+
|
1901 |
+
This is very useful as one can now use ``hyperexpand`` to find the
|
1902 |
+
symbolic expressions/functions.
|
1903 |
+
|
1904 |
+
Examples
|
1905 |
+
========
|
1906 |
+
|
1907 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
1908 |
+
>>> from sympy import ZZ
|
1909 |
+
>>> from sympy import symbols
|
1910 |
+
>>> x = symbols('x')
|
1911 |
+
>>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')
|
1912 |
+
>>> # sin(x)
|
1913 |
+
>>> HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]).to_hyper()
|
1914 |
+
x*hyper((), (3/2,), -x**2/4)
|
1915 |
+
>>> # exp(x)
|
1916 |
+
>>> HolonomicFunction(Dx - 1, x, 0, [1]).to_hyper()
|
1917 |
+
hyper((), (), x)
|
1918 |
+
|
1919 |
+
See Also
|
1920 |
+
========
|
1921 |
+
|
1922 |
+
from_hyper, from_meijerg
|
1923 |
+
"""
|
1924 |
+
|
1925 |
+
if _recur is None:
|
1926 |
+
recurrence = self.to_sequence()
|
1927 |
+
else:
|
1928 |
+
recurrence = _recur
|
1929 |
+
|
1930 |
+
if isinstance(recurrence, tuple) and len(recurrence) == 2:
|
1931 |
+
smallest_n = recurrence[1]
|
1932 |
+
recurrence = recurrence[0]
|
1933 |
+
constantpower = 0
|
1934 |
+
elif isinstance(recurrence, tuple) and len(recurrence) == 3:
|
1935 |
+
smallest_n = recurrence[2]
|
1936 |
+
constantpower = recurrence[1]
|
1937 |
+
recurrence = recurrence[0]
|
1938 |
+
elif len(recurrence) == 1 and len(recurrence[0]) == 2:
|
1939 |
+
smallest_n = recurrence[0][1]
|
1940 |
+
recurrence = recurrence[0][0]
|
1941 |
+
constantpower = 0
|
1942 |
+
elif len(recurrence) == 1 and len(recurrence[0]) == 3:
|
1943 |
+
smallest_n = recurrence[0][2]
|
1944 |
+
constantpower = recurrence[0][1]
|
1945 |
+
recurrence = recurrence[0][0]
|
1946 |
+
else:
|
1947 |
+
sol = self.to_hyper(as_list=as_list, _recur=recurrence[0])
|
1948 |
+
for i in recurrence[1:]:
|
1949 |
+
sol += self.to_hyper(as_list=as_list, _recur=i)
|
1950 |
+
return sol
|
1951 |
+
|
1952 |
+
u0 = recurrence.u0
|
1953 |
+
r = recurrence.recurrence
|
1954 |
+
x = self.x
|
1955 |
+
x0 = self.x0
|
1956 |
+
|
1957 |
+
# order of the recurrence relation
|
1958 |
+
m = r.order
|
1959 |
+
|
1960 |
+
# when no recurrence exists, and the power series have finite terms
|
1961 |
+
if m == 0:
|
1962 |
+
nonzeroterms = roots(r.parent.base.to_sympy(r.listofpoly[0]), recurrence.n, filter='R')
|
1963 |
+
|
1964 |
+
sol = S.Zero
|
1965 |
+
for j, i in enumerate(nonzeroterms):
|
1966 |
+
|
1967 |
+
if i < 0 or int(i) != i:
|
1968 |
+
continue
|
1969 |
+
|
1970 |
+
i = int(i)
|
1971 |
+
if i < len(u0):
|
1972 |
+
if isinstance(u0[i], (PolyElement, FracElement)):
|
1973 |
+
u0[i] = u0[i].as_expr()
|
1974 |
+
sol += u0[i] * x**i
|
1975 |
+
|
1976 |
+
else:
|
1977 |
+
sol += Symbol('C_%s' %j) * x**i
|
1978 |
+
|
1979 |
+
if isinstance(sol, (PolyElement, FracElement)):
|
1980 |
+
sol = sol.as_expr() * x**constantpower
|
1981 |
+
else:
|
1982 |
+
sol = sol * x**constantpower
|
1983 |
+
if as_list:
|
1984 |
+
if x0 != 0:
|
1985 |
+
return [(sol.subs(x, x - x0), )]
|
1986 |
+
return [(sol, )]
|
1987 |
+
if x0 != 0:
|
1988 |
+
return sol.subs(x, x - x0)
|
1989 |
+
return sol
|
1990 |
+
|
1991 |
+
if smallest_n + m > len(u0):
|
1992 |
+
raise NotImplementedError("Can't compute sufficient Initial Conditions")
|
1993 |
+
|
1994 |
+
# check if the recurrence represents a hypergeometric series
|
1995 |
+
is_hyper = True
|
1996 |
+
|
1997 |
+
for i in range(1, len(r.listofpoly)-1):
|
1998 |
+
if r.listofpoly[i] != r.parent.base.zero:
|
1999 |
+
is_hyper = False
|
2000 |
+
break
|
2001 |
+
|
2002 |
+
if not is_hyper:
|
2003 |
+
raise NotHyperSeriesError(self, self.x0)
|
2004 |
+
|
2005 |
+
a = r.listofpoly[0]
|
2006 |
+
b = r.listofpoly[-1]
|
2007 |
+
|
2008 |
+
# the constant multiple of argument of hypergeometric function
|
2009 |
+
if isinstance(a.rep[0], (PolyElement, FracElement)):
|
2010 |
+
c = - (S(a.rep[0].as_expr()) * m**(a.degree())) / (S(b.rep[0].as_expr()) * m**(b.degree()))
|
2011 |
+
else:
|
2012 |
+
c = - (S(a.rep[0]) * m**(a.degree())) / (S(b.rep[0]) * m**(b.degree()))
|
2013 |
+
|
2014 |
+
sol = 0
|
2015 |
+
|
2016 |
+
arg1 = roots(r.parent.base.to_sympy(a), recurrence.n)
|
2017 |
+
arg2 = roots(r.parent.base.to_sympy(b), recurrence.n)
|
2018 |
+
|
2019 |
+
# iterate through the initial conditions to find
|
2020 |
+
# the hypergeometric representation of the given
|
2021 |
+
# function.
|
2022 |
+
# The answer will be a linear combination
|
2023 |
+
# of different hypergeometric series which satisfies
|
2024 |
+
# the recurrence.
|
2025 |
+
if as_list:
|
2026 |
+
listofsol = []
|
2027 |
+
for i in range(smallest_n + m):
|
2028 |
+
|
2029 |
+
# if the recurrence relation doesn't hold for `n = i`,
|
2030 |
+
# then a Hypergeometric representation doesn't exist.
|
2031 |
+
# add the algebraic term a * x**i to the solution,
|
2032 |
+
# where a is u0[i]
|
2033 |
+
if i < smallest_n:
|
2034 |
+
if as_list:
|
2035 |
+
listofsol.append(((S(u0[i]) * x**(i+constantpower)).subs(x, x-x0), ))
|
2036 |
+
else:
|
2037 |
+
sol += S(u0[i]) * x**i
|
2038 |
+
continue
|
2039 |
+
|
2040 |
+
# if the coefficient u0[i] is zero, then the
|
2041 |
+
# independent hypergeomtric series starting with
|
2042 |
+
# x**i is not a part of the answer.
|
2043 |
+
if S(u0[i]) == 0:
|
2044 |
+
continue
|
2045 |
+
|
2046 |
+
ap = []
|
2047 |
+
bq = []
|
2048 |
+
|
2049 |
+
# substitute m * n + i for n
|
2050 |
+
for k in ordered(arg1.keys()):
|
2051 |
+
ap.extend([nsimplify((i - k) / m)] * arg1[k])
|
2052 |
+
|
2053 |
+
for k in ordered(arg2.keys()):
|
2054 |
+
bq.extend([nsimplify((i - k) / m)] * arg2[k])
|
2055 |
+
|
2056 |
+
# convention of (k + 1) in the denominator
|
2057 |
+
if 1 in bq:
|
2058 |
+
bq.remove(1)
|
2059 |
+
else:
|
2060 |
+
ap.append(1)
|
2061 |
+
if as_list:
|
2062 |
+
listofsol.append(((S(u0[i])*x**(i+constantpower)).subs(x, x-x0), (hyper(ap, bq, c*x**m)).subs(x, x-x0)))
|
2063 |
+
else:
|
2064 |
+
sol += S(u0[i]) * hyper(ap, bq, c * x**m) * x**i
|
2065 |
+
if as_list:
|
2066 |
+
return listofsol
|
2067 |
+
sol = sol * x**constantpower
|
2068 |
+
if x0 != 0:
|
2069 |
+
return sol.subs(x, x - x0)
|
2070 |
+
|
2071 |
+
return sol
|
2072 |
+
|
2073 |
+
def to_expr(self):
|
2074 |
+
"""
|
2075 |
+
Converts a Holonomic Function back to elementary functions.
|
2076 |
+
|
2077 |
+
Examples
|
2078 |
+
========
|
2079 |
+
|
2080 |
+
>>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
|
2081 |
+
>>> from sympy import ZZ
|
2082 |
+
>>> from sympy import symbols, S
|
2083 |
+
>>> x = symbols('x')
|
2084 |
+
>>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')
|
2085 |
+
>>> HolonomicFunction(x**2*Dx**2 + x*Dx + (x**2 - 1), x, 0, [0, S(1)/2]).to_expr()
|
2086 |
+
besselj(1, x)
|
2087 |
+
>>> HolonomicFunction((1 + x)*Dx**3 + Dx**2, x, 0, [1, 1, 1]).to_expr()
|
2088 |
+
x*log(x + 1) + log(x + 1) + 1
|
2089 |
+
|
2090 |
+
"""
|
2091 |
+
|
2092 |
+
return hyperexpand(self.to_hyper()).simplify()
|
2093 |
+
|
2094 |
+
def change_ics(self, b, lenics=None):
|
2095 |
+
"""
|
2096 |
+
Changes the point `x0` to ``b`` for initial conditions.
|
2097 |
+
|
2098 |
+
Examples
|
2099 |
+
========
|
2100 |
+
|
2101 |
+
>>> from sympy.holonomic import expr_to_holonomic
|
2102 |
+
>>> from sympy import symbols, sin, exp
|
2103 |
+
>>> x = symbols('x')
|
2104 |
+
|
2105 |
+
>>> expr_to_holonomic(sin(x)).change_ics(1)
|
2106 |
+
HolonomicFunction((1) + (1)*Dx**2, x, 1, [sin(1), cos(1)])
|
2107 |
+
|
2108 |
+
>>> expr_to_holonomic(exp(x)).change_ics(2)
|
2109 |
+
HolonomicFunction((-1) + (1)*Dx, x, 2, [exp(2)])
|
2110 |
+
"""
|
2111 |
+
|
2112 |
+
symbolic = True
|
2113 |
+
|
2114 |
+
if lenics is None and len(self.y0) > self.annihilator.order:
|
2115 |
+
lenics = len(self.y0)
|
2116 |
+
dom = self.annihilator.parent.base.domain
|
2117 |
+
|
2118 |
+
try:
|
2119 |
+
sol = expr_to_holonomic(self.to_expr(), x=self.x, x0=b, lenics=lenics, domain=dom)
|
2120 |
+
except (NotPowerSeriesError, NotHyperSeriesError):
|
2121 |
+
symbolic = False
|
2122 |
+
|
2123 |
+
if symbolic and sol.x0 == b:
|
2124 |
+
return sol
|
2125 |
+
|
2126 |
+
y0 = self.evalf(b, derivatives=True)
|
2127 |
+
return HolonomicFunction(self.annihilator, self.x, b, y0)
|
2128 |
+
|
2129 |
+
def to_meijerg(self):
|
2130 |
+
"""
|
2131 |
+
Returns a linear combination of Meijer G-functions.
|
2132 |
+
|
2133 |
+
Examples
|
2134 |
+
========
|
2135 |
+
|
2136 |
+
>>> from sympy.holonomic import expr_to_holonomic
|
2137 |
+
>>> from sympy import sin, cos, hyperexpand, log, symbols
|
2138 |
+
>>> x = symbols('x')
|
2139 |
+
>>> hyperexpand(expr_to_holonomic(cos(x) + sin(x)).to_meijerg())
|
2140 |
+
sin(x) + cos(x)
|
2141 |
+
>>> hyperexpand(expr_to_holonomic(log(x)).to_meijerg()).simplify()
|
2142 |
+
log(x)
|
2143 |
+
|
2144 |
+
See Also
|
2145 |
+
========
|
2146 |
+
|
2147 |
+
to_hyper
|
2148 |
+
"""
|
2149 |
+
|
2150 |
+
# convert to hypergeometric first
|
2151 |
+
rep = self.to_hyper(as_list=True)
|
2152 |
+
sol = S.Zero
|
2153 |
+
|
2154 |
+
for i in rep:
|
2155 |
+
if len(i) == 1:
|
2156 |
+
sol += i[0]
|
2157 |
+
|
2158 |
+
elif len(i) == 2:
|
2159 |
+
sol += i[0] * _hyper_to_meijerg(i[1])
|
2160 |
+
|
2161 |
+
return sol
|
2162 |
+
|
2163 |
+
|
2164 |
+
def from_hyper(func, x0=0, evalf=False):
|
2165 |
+
r"""
|
2166 |
+
Converts a hypergeometric function to holonomic.
|
2167 |
+
``func`` is the Hypergeometric Function and ``x0`` is the point at
|
2168 |
+
which initial conditions are required.
|
2169 |
+
|
2170 |
+
Examples
|
2171 |
+
========
|
2172 |
+
|
2173 |
+
>>> from sympy.holonomic.holonomic import from_hyper
|
2174 |
+
>>> from sympy import symbols, hyper, S
|
2175 |
+
>>> x = symbols('x')
|
2176 |
+
>>> from_hyper(hyper([], [S(3)/2], x**2/4))
|
2177 |
+
HolonomicFunction((-x) + (2)*Dx + (x)*Dx**2, x, 1, [sinh(1), -sinh(1) + cosh(1)])
|
2178 |
+
"""
|
2179 |
+
|
2180 |
+
a = func.ap
|
2181 |
+
b = func.bq
|
2182 |
+
z = func.args[2]
|
2183 |
+
x = z.atoms(Symbol).pop()
|
2184 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
2185 |
+
|
2186 |
+
# generalized hypergeometric differential equation
|
2187 |
+
xDx = x*Dx
|
2188 |
+
r1 = 1
|
2189 |
+
for ai in a: # XXX gives sympify error if Mul is used with list of all factors
|
2190 |
+
r1 *= xDx + ai
|
2191 |
+
xDx_1 = xDx - 1
|
2192 |
+
# r2 = Mul(*([Dx] + [xDx_1 + bi for bi in b])) # XXX gives sympify error
|
2193 |
+
r2 = Dx
|
2194 |
+
for bi in b:
|
2195 |
+
r2 *= xDx_1 + bi
|
2196 |
+
sol = r1 - r2
|
2197 |
+
|
2198 |
+
simp = hyperexpand(func)
|
2199 |
+
|
2200 |
+
if simp in (Infinity, NegativeInfinity):
|
2201 |
+
return HolonomicFunction(sol, x).composition(z)
|
2202 |
+
|
2203 |
+
def _find_conditions(simp, x, x0, order, evalf=False):
|
2204 |
+
y0 = []
|
2205 |
+
for i in range(order):
|
2206 |
+
if evalf:
|
2207 |
+
val = simp.subs(x, x0).evalf()
|
2208 |
+
else:
|
2209 |
+
val = simp.subs(x, x0)
|
2210 |
+
# return None if it is Infinite or NaN
|
2211 |
+
if val.is_finite is False or isinstance(val, NaN):
|
2212 |
+
return None
|
2213 |
+
y0.append(val)
|
2214 |
+
simp = simp.diff(x)
|
2215 |
+
return y0
|
2216 |
+
|
2217 |
+
# if the function is known symbolically
|
2218 |
+
if not isinstance(simp, hyper):
|
2219 |
+
y0 = _find_conditions(simp, x, x0, sol.order)
|
2220 |
+
while not y0:
|
2221 |
+
# if values don't exist at 0, then try to find initial
|
2222 |
+
# conditions at 1. If it doesn't exist at 1 too then
|
2223 |
+
# try 2 and so on.
|
2224 |
+
x0 += 1
|
2225 |
+
y0 = _find_conditions(simp, x, x0, sol.order)
|
2226 |
+
|
2227 |
+
return HolonomicFunction(sol, x).composition(z, x0, y0)
|
2228 |
+
|
2229 |
+
if isinstance(simp, hyper):
|
2230 |
+
x0 = 1
|
2231 |
+
# use evalf if the function can't be simplified
|
2232 |
+
y0 = _find_conditions(simp, x, x0, sol.order, evalf)
|
2233 |
+
while not y0:
|
2234 |
+
x0 += 1
|
2235 |
+
y0 = _find_conditions(simp, x, x0, sol.order, evalf)
|
2236 |
+
return HolonomicFunction(sol, x).composition(z, x0, y0)
|
2237 |
+
|
2238 |
+
return HolonomicFunction(sol, x).composition(z)
|
2239 |
+
|
2240 |
+
|
2241 |
+
def from_meijerg(func, x0=0, evalf=False, initcond=True, domain=QQ):
|
2242 |
+
"""
|
2243 |
+
Converts a Meijer G-function to Holonomic.
|
2244 |
+
``func`` is the G-Function and ``x0`` is the point at
|
2245 |
+
which initial conditions are required.
|
2246 |
+
|
2247 |
+
Examples
|
2248 |
+
========
|
2249 |
+
|
2250 |
+
>>> from sympy.holonomic.holonomic import from_meijerg
|
2251 |
+
>>> from sympy import symbols, meijerg, S
|
2252 |
+
>>> x = symbols('x')
|
2253 |
+
>>> from_meijerg(meijerg(([], []), ([S(1)/2], [0]), x**2/4))
|
2254 |
+
HolonomicFunction((1) + (1)*Dx**2, x, 0, [0, 1/sqrt(pi)])
|
2255 |
+
"""
|
2256 |
+
|
2257 |
+
a = func.ap
|
2258 |
+
b = func.bq
|
2259 |
+
n = len(func.an)
|
2260 |
+
m = len(func.bm)
|
2261 |
+
p = len(a)
|
2262 |
+
z = func.args[2]
|
2263 |
+
x = z.atoms(Symbol).pop()
|
2264 |
+
R, Dx = DifferentialOperators(domain.old_poly_ring(x), 'Dx')
|
2265 |
+
|
2266 |
+
# compute the differential equation satisfied by the
|
2267 |
+
# Meijer G-function.
|
2268 |
+
xDx = x*Dx
|
2269 |
+
xDx1 = xDx + 1
|
2270 |
+
r1 = x*(-1)**(m + n - p)
|
2271 |
+
for ai in a: # XXX gives sympify error if args given in list
|
2272 |
+
r1 *= xDx1 - ai
|
2273 |
+
# r2 = Mul(*[xDx - bi for bi in b]) # gives sympify error
|
2274 |
+
r2 = 1
|
2275 |
+
for bi in b:
|
2276 |
+
r2 *= xDx - bi
|
2277 |
+
sol = r1 - r2
|
2278 |
+
|
2279 |
+
if not initcond:
|
2280 |
+
return HolonomicFunction(sol, x).composition(z)
|
2281 |
+
|
2282 |
+
simp = hyperexpand(func)
|
2283 |
+
|
2284 |
+
if simp in (Infinity, NegativeInfinity):
|
2285 |
+
return HolonomicFunction(sol, x).composition(z)
|
2286 |
+
|
2287 |
+
def _find_conditions(simp, x, x0, order, evalf=False):
|
2288 |
+
y0 = []
|
2289 |
+
for i in range(order):
|
2290 |
+
if evalf:
|
2291 |
+
val = simp.subs(x, x0).evalf()
|
2292 |
+
else:
|
2293 |
+
val = simp.subs(x, x0)
|
2294 |
+
if val.is_finite is False or isinstance(val, NaN):
|
2295 |
+
return None
|
2296 |
+
y0.append(val)
|
2297 |
+
simp = simp.diff(x)
|
2298 |
+
return y0
|
2299 |
+
|
2300 |
+
# computing initial conditions
|
2301 |
+
if not isinstance(simp, meijerg):
|
2302 |
+
y0 = _find_conditions(simp, x, x0, sol.order)
|
2303 |
+
while not y0:
|
2304 |
+
x0 += 1
|
2305 |
+
y0 = _find_conditions(simp, x, x0, sol.order)
|
2306 |
+
|
2307 |
+
return HolonomicFunction(sol, x).composition(z, x0, y0)
|
2308 |
+
|
2309 |
+
if isinstance(simp, meijerg):
|
2310 |
+
x0 = 1
|
2311 |
+
y0 = _find_conditions(simp, x, x0, sol.order, evalf)
|
2312 |
+
while not y0:
|
2313 |
+
x0 += 1
|
2314 |
+
y0 = _find_conditions(simp, x, x0, sol.order, evalf)
|
2315 |
+
|
2316 |
+
return HolonomicFunction(sol, x).composition(z, x0, y0)
|
2317 |
+
|
2318 |
+
return HolonomicFunction(sol, x).composition(z)
|
2319 |
+
|
2320 |
+
|
2321 |
+
x_1 = Dummy('x_1')
|
2322 |
+
_lookup_table = None
|
2323 |
+
domain_for_table = None
|
2324 |
+
from sympy.integrals.meijerint import _mytype
|
2325 |
+
|
2326 |
+
|
2327 |
+
def expr_to_holonomic(func, x=None, x0=0, y0=None, lenics=None, domain=None, initcond=True):
|
2328 |
+
"""
|
2329 |
+
Converts a function or an expression to a holonomic function.
|
2330 |
+
|
2331 |
+
Parameters
|
2332 |
+
==========
|
2333 |
+
|
2334 |
+
func:
|
2335 |
+
The expression to be converted.
|
2336 |
+
x:
|
2337 |
+
variable for the function.
|
2338 |
+
x0:
|
2339 |
+
point at which initial condition must be computed.
|
2340 |
+
y0:
|
2341 |
+
One can optionally provide initial condition if the method
|
2342 |
+
is not able to do it automatically.
|
2343 |
+
lenics:
|
2344 |
+
Number of terms in the initial condition. By default it is
|
2345 |
+
equal to the order of the annihilator.
|
2346 |
+
domain:
|
2347 |
+
Ground domain for the polynomials in ``x`` appearing as coefficients
|
2348 |
+
in the annihilator.
|
2349 |
+
initcond:
|
2350 |
+
Set it false if you do not want the initial conditions to be computed.
|
2351 |
+
|
2352 |
+
Examples
|
2353 |
+
========
|
2354 |
+
|
2355 |
+
>>> from sympy.holonomic.holonomic import expr_to_holonomic
|
2356 |
+
>>> from sympy import sin, exp, symbols
|
2357 |
+
>>> x = symbols('x')
|
2358 |
+
>>> expr_to_holonomic(sin(x))
|
2359 |
+
HolonomicFunction((1) + (1)*Dx**2, x, 0, [0, 1])
|
2360 |
+
>>> expr_to_holonomic(exp(x))
|
2361 |
+
HolonomicFunction((-1) + (1)*Dx, x, 0, [1])
|
2362 |
+
|
2363 |
+
See Also
|
2364 |
+
========
|
2365 |
+
|
2366 |
+
sympy.integrals.meijerint._rewrite1, _convert_poly_rat_alg, _create_table
|
2367 |
+
"""
|
2368 |
+
func = sympify(func)
|
2369 |
+
syms = func.free_symbols
|
2370 |
+
|
2371 |
+
if not x:
|
2372 |
+
if len(syms) == 1:
|
2373 |
+
x= syms.pop()
|
2374 |
+
else:
|
2375 |
+
raise ValueError("Specify the variable for the function")
|
2376 |
+
elif x in syms:
|
2377 |
+
syms.remove(x)
|
2378 |
+
|
2379 |
+
extra_syms = list(syms)
|
2380 |
+
|
2381 |
+
if domain is None:
|
2382 |
+
if func.has(Float):
|
2383 |
+
domain = RR
|
2384 |
+
else:
|
2385 |
+
domain = QQ
|
2386 |
+
if len(extra_syms) != 0:
|
2387 |
+
domain = domain[extra_syms].get_field()
|
2388 |
+
|
2389 |
+
# try to convert if the function is polynomial or rational
|
2390 |
+
solpoly = _convert_poly_rat_alg(func, x, x0=x0, y0=y0, lenics=lenics, domain=domain, initcond=initcond)
|
2391 |
+
if solpoly:
|
2392 |
+
return solpoly
|
2393 |
+
|
2394 |
+
# create the lookup table
|
2395 |
+
global _lookup_table, domain_for_table
|
2396 |
+
if not _lookup_table:
|
2397 |
+
domain_for_table = domain
|
2398 |
+
_lookup_table = {}
|
2399 |
+
_create_table(_lookup_table, domain=domain)
|
2400 |
+
elif domain != domain_for_table:
|
2401 |
+
domain_for_table = domain
|
2402 |
+
_lookup_table = {}
|
2403 |
+
_create_table(_lookup_table, domain=domain)
|
2404 |
+
|
2405 |
+
# use the table directly to convert to Holonomic
|
2406 |
+
if func.is_Function:
|
2407 |
+
f = func.subs(x, x_1)
|
2408 |
+
t = _mytype(f, x_1)
|
2409 |
+
if t in _lookup_table:
|
2410 |
+
l = _lookup_table[t]
|
2411 |
+
sol = l[0][1].change_x(x)
|
2412 |
+
else:
|
2413 |
+
sol = _convert_meijerint(func, x, initcond=False, domain=domain)
|
2414 |
+
if not sol:
|
2415 |
+
raise NotImplementedError
|
2416 |
+
if y0:
|
2417 |
+
sol.y0 = y0
|
2418 |
+
if y0 or not initcond:
|
2419 |
+
sol.x0 = x0
|
2420 |
+
return sol
|
2421 |
+
if not lenics:
|
2422 |
+
lenics = sol.annihilator.order
|
2423 |
+
_y0 = _find_conditions(func, x, x0, lenics)
|
2424 |
+
while not _y0:
|
2425 |
+
x0 += 1
|
2426 |
+
_y0 = _find_conditions(func, x, x0, lenics)
|
2427 |
+
return HolonomicFunction(sol.annihilator, x, x0, _y0)
|
2428 |
+
|
2429 |
+
if y0 or not initcond:
|
2430 |
+
sol = sol.composition(func.args[0])
|
2431 |
+
if y0:
|
2432 |
+
sol.y0 = y0
|
2433 |
+
sol.x0 = x0
|
2434 |
+
return sol
|
2435 |
+
if not lenics:
|
2436 |
+
lenics = sol.annihilator.order
|
2437 |
+
|
2438 |
+
_y0 = _find_conditions(func, x, x0, lenics)
|
2439 |
+
while not _y0:
|
2440 |
+
x0 += 1
|
2441 |
+
_y0 = _find_conditions(func, x, x0, lenics)
|
2442 |
+
return sol.composition(func.args[0], x0, _y0)
|
2443 |
+
|
2444 |
+
# iterate through the expression recursively
|
2445 |
+
args = func.args
|
2446 |
+
f = func.func
|
2447 |
+
sol = expr_to_holonomic(args[0], x=x, initcond=False, domain=domain)
|
2448 |
+
|
2449 |
+
if f is Add:
|
2450 |
+
for i in range(1, len(args)):
|
2451 |
+
sol += expr_to_holonomic(args[i], x=x, initcond=False, domain=domain)
|
2452 |
+
|
2453 |
+
elif f is Mul:
|
2454 |
+
for i in range(1, len(args)):
|
2455 |
+
sol *= expr_to_holonomic(args[i], x=x, initcond=False, domain=domain)
|
2456 |
+
|
2457 |
+
elif f is Pow:
|
2458 |
+
sol = sol**args[1]
|
2459 |
+
sol.x0 = x0
|
2460 |
+
if not sol:
|
2461 |
+
raise NotImplementedError
|
2462 |
+
if y0:
|
2463 |
+
sol.y0 = y0
|
2464 |
+
if y0 or not initcond:
|
2465 |
+
return sol
|
2466 |
+
if sol.y0:
|
2467 |
+
return sol
|
2468 |
+
if not lenics:
|
2469 |
+
lenics = sol.annihilator.order
|
2470 |
+
if sol.annihilator.is_singular(x0):
|
2471 |
+
r = sol._indicial()
|
2472 |
+
l = list(r)
|
2473 |
+
if len(r) == 1 and r[l[0]] == S.One:
|
2474 |
+
r = l[0]
|
2475 |
+
g = func / (x - x0)**r
|
2476 |
+
singular_ics = _find_conditions(g, x, x0, lenics)
|
2477 |
+
singular_ics = [j / factorial(i) for i, j in enumerate(singular_ics)]
|
2478 |
+
y0 = {r:singular_ics}
|
2479 |
+
return HolonomicFunction(sol.annihilator, x, x0, y0)
|
2480 |
+
|
2481 |
+
_y0 = _find_conditions(func, x, x0, lenics)
|
2482 |
+
while not _y0:
|
2483 |
+
x0 += 1
|
2484 |
+
_y0 = _find_conditions(func, x, x0, lenics)
|
2485 |
+
|
2486 |
+
return HolonomicFunction(sol.annihilator, x, x0, _y0)
|
2487 |
+
|
2488 |
+
|
2489 |
+
## Some helper functions ##
|
2490 |
+
|
2491 |
+
def _normalize(list_of, parent, negative=True):
|
2492 |
+
"""
|
2493 |
+
Normalize a given annihilator
|
2494 |
+
"""
|
2495 |
+
|
2496 |
+
num = []
|
2497 |
+
denom = []
|
2498 |
+
base = parent.base
|
2499 |
+
K = base.get_field()
|
2500 |
+
lcm_denom = base.from_sympy(S.One)
|
2501 |
+
list_of_coeff = []
|
2502 |
+
|
2503 |
+
# convert polynomials to the elements of associated
|
2504 |
+
# fraction field
|
2505 |
+
for i, j in enumerate(list_of):
|
2506 |
+
if isinstance(j, base.dtype):
|
2507 |
+
list_of_coeff.append(K.new(j.rep))
|
2508 |
+
elif not isinstance(j, K.dtype):
|
2509 |
+
list_of_coeff.append(K.from_sympy(sympify(j)))
|
2510 |
+
else:
|
2511 |
+
list_of_coeff.append(j)
|
2512 |
+
|
2513 |
+
# corresponding numerators of the sequence of polynomials
|
2514 |
+
num.append(list_of_coeff[i].numer())
|
2515 |
+
|
2516 |
+
# corresponding denominators
|
2517 |
+
denom.append(list_of_coeff[i].denom())
|
2518 |
+
|
2519 |
+
# lcm of denominators in the coefficients
|
2520 |
+
for i in denom:
|
2521 |
+
lcm_denom = i.lcm(lcm_denom)
|
2522 |
+
|
2523 |
+
if negative:
|
2524 |
+
lcm_denom = -lcm_denom
|
2525 |
+
|
2526 |
+
lcm_denom = K.new(lcm_denom.rep)
|
2527 |
+
|
2528 |
+
# multiply the coefficients with lcm
|
2529 |
+
for i, j in enumerate(list_of_coeff):
|
2530 |
+
list_of_coeff[i] = j * lcm_denom
|
2531 |
+
|
2532 |
+
gcd_numer = base((list_of_coeff[-1].numer() / list_of_coeff[-1].denom()).rep)
|
2533 |
+
|
2534 |
+
# gcd of numerators in the coefficients
|
2535 |
+
for i in num:
|
2536 |
+
gcd_numer = i.gcd(gcd_numer)
|
2537 |
+
|
2538 |
+
gcd_numer = K.new(gcd_numer.rep)
|
2539 |
+
|
2540 |
+
# divide all the coefficients by the gcd
|
2541 |
+
for i, j in enumerate(list_of_coeff):
|
2542 |
+
frac_ans = j / gcd_numer
|
2543 |
+
list_of_coeff[i] = base((frac_ans.numer() / frac_ans.denom()).rep)
|
2544 |
+
|
2545 |
+
return DifferentialOperator(list_of_coeff, parent)
|
2546 |
+
|
2547 |
+
|
2548 |
+
def _derivate_diff_eq(listofpoly):
|
2549 |
+
"""
|
2550 |
+
Let a differential equation a0(x)y(x) + a1(x)y'(x) + ... = 0
|
2551 |
+
where a0, a1,... are polynomials or rational functions. The function
|
2552 |
+
returns b0, b1, b2... such that the differential equation
|
2553 |
+
b0(x)y(x) + b1(x)y'(x) +... = 0 is formed after differentiating the
|
2554 |
+
former equation.
|
2555 |
+
"""
|
2556 |
+
|
2557 |
+
sol = []
|
2558 |
+
a = len(listofpoly) - 1
|
2559 |
+
sol.append(DMFdiff(listofpoly[0]))
|
2560 |
+
|
2561 |
+
for i, j in enumerate(listofpoly[1:]):
|
2562 |
+
sol.append(DMFdiff(j) + listofpoly[i])
|
2563 |
+
|
2564 |
+
sol.append(listofpoly[a])
|
2565 |
+
return sol
|
2566 |
+
|
2567 |
+
|
2568 |
+
def _hyper_to_meijerg(func):
|
2569 |
+
"""
|
2570 |
+
Converts a `hyper` to meijerg.
|
2571 |
+
"""
|
2572 |
+
ap = func.ap
|
2573 |
+
bq = func.bq
|
2574 |
+
|
2575 |
+
ispoly = any(i <= 0 and int(i) == i for i in ap)
|
2576 |
+
if ispoly:
|
2577 |
+
return hyperexpand(func)
|
2578 |
+
|
2579 |
+
z = func.args[2]
|
2580 |
+
|
2581 |
+
# parameters of the `meijerg` function.
|
2582 |
+
an = (1 - i for i in ap)
|
2583 |
+
anp = ()
|
2584 |
+
bm = (S.Zero, )
|
2585 |
+
bmq = (1 - i for i in bq)
|
2586 |
+
|
2587 |
+
k = S.One
|
2588 |
+
|
2589 |
+
for i in bq:
|
2590 |
+
k = k * gamma(i)
|
2591 |
+
|
2592 |
+
for i in ap:
|
2593 |
+
k = k / gamma(i)
|
2594 |
+
|
2595 |
+
return k * meijerg(an, anp, bm, bmq, -z)
|
2596 |
+
|
2597 |
+
|
2598 |
+
def _add_lists(list1, list2):
|
2599 |
+
"""Takes polynomial sequences of two annihilators a and b and returns
|
2600 |
+
the list of polynomials of sum of a and b.
|
2601 |
+
"""
|
2602 |
+
if len(list1) <= len(list2):
|
2603 |
+
sol = [a + b for a, b in zip(list1, list2)] + list2[len(list1):]
|
2604 |
+
else:
|
2605 |
+
sol = [a + b for a, b in zip(list1, list2)] + list1[len(list2):]
|
2606 |
+
return sol
|
2607 |
+
|
2608 |
+
|
2609 |
+
def _extend_y0(Holonomic, n):
|
2610 |
+
"""
|
2611 |
+
Tries to find more initial conditions by substituting the initial
|
2612 |
+
value point in the differential equation.
|
2613 |
+
"""
|
2614 |
+
|
2615 |
+
if Holonomic.annihilator.is_singular(Holonomic.x0) or Holonomic.is_singularics() == True:
|
2616 |
+
return Holonomic.y0
|
2617 |
+
|
2618 |
+
annihilator = Holonomic.annihilator
|
2619 |
+
a = annihilator.order
|
2620 |
+
|
2621 |
+
listofpoly = []
|
2622 |
+
|
2623 |
+
y0 = Holonomic.y0
|
2624 |
+
R = annihilator.parent.base
|
2625 |
+
K = R.get_field()
|
2626 |
+
|
2627 |
+
for i, j in enumerate(annihilator.listofpoly):
|
2628 |
+
if isinstance(j, annihilator.parent.base.dtype):
|
2629 |
+
listofpoly.append(K.new(j.rep))
|
2630 |
+
|
2631 |
+
if len(y0) < a or n <= len(y0):
|
2632 |
+
return y0
|
2633 |
+
else:
|
2634 |
+
list_red = [-listofpoly[i] / listofpoly[a]
|
2635 |
+
for i in range(a)]
|
2636 |
+
if len(y0) > a:
|
2637 |
+
y1 = [y0[i] for i in range(a)]
|
2638 |
+
else:
|
2639 |
+
y1 = list(y0)
|
2640 |
+
for i in range(n - a):
|
2641 |
+
sol = 0
|
2642 |
+
for a, b in zip(y1, list_red):
|
2643 |
+
r = DMFsubs(b, Holonomic.x0)
|
2644 |
+
if not getattr(r, 'is_finite', True):
|
2645 |
+
return y0
|
2646 |
+
if isinstance(r, (PolyElement, FracElement)):
|
2647 |
+
r = r.as_expr()
|
2648 |
+
sol += a * r
|
2649 |
+
y1.append(sol)
|
2650 |
+
list_red = _derivate_diff_eq(list_red)
|
2651 |
+
|
2652 |
+
return y0 + y1[len(y0):]
|
2653 |
+
|
2654 |
+
|
2655 |
+
def DMFdiff(frac):
|
2656 |
+
# differentiate a DMF object represented as p/q
|
2657 |
+
if not isinstance(frac, DMF):
|
2658 |
+
return frac.diff()
|
2659 |
+
|
2660 |
+
K = frac.ring
|
2661 |
+
p = K.numer(frac)
|
2662 |
+
q = K.denom(frac)
|
2663 |
+
sol_num = - p * q.diff() + q * p.diff()
|
2664 |
+
sol_denom = q**2
|
2665 |
+
return K((sol_num.rep, sol_denom.rep))
|
2666 |
+
|
2667 |
+
|
2668 |
+
def DMFsubs(frac, x0, mpm=False):
|
2669 |
+
# substitute the point x0 in DMF object of the form p/q
|
2670 |
+
if not isinstance(frac, DMF):
|
2671 |
+
return frac
|
2672 |
+
|
2673 |
+
p = frac.num
|
2674 |
+
q = frac.den
|
2675 |
+
sol_p = S.Zero
|
2676 |
+
sol_q = S.Zero
|
2677 |
+
|
2678 |
+
if mpm:
|
2679 |
+
from mpmath import mp
|
2680 |
+
|
2681 |
+
for i, j in enumerate(reversed(p)):
|
2682 |
+
if mpm:
|
2683 |
+
j = sympify(j)._to_mpmath(mp.prec)
|
2684 |
+
sol_p += j * x0**i
|
2685 |
+
|
2686 |
+
for i, j in enumerate(reversed(q)):
|
2687 |
+
if mpm:
|
2688 |
+
j = sympify(j)._to_mpmath(mp.prec)
|
2689 |
+
sol_q += j * x0**i
|
2690 |
+
|
2691 |
+
if isinstance(sol_p, (PolyElement, FracElement)):
|
2692 |
+
sol_p = sol_p.as_expr()
|
2693 |
+
if isinstance(sol_q, (PolyElement, FracElement)):
|
2694 |
+
sol_q = sol_q.as_expr()
|
2695 |
+
|
2696 |
+
return sol_p / sol_q
|
2697 |
+
|
2698 |
+
|
2699 |
+
def _convert_poly_rat_alg(func, x, x0=0, y0=None, lenics=None, domain=QQ, initcond=True):
|
2700 |
+
"""
|
2701 |
+
Converts polynomials, rationals and algebraic functions to holonomic.
|
2702 |
+
"""
|
2703 |
+
|
2704 |
+
ispoly = func.is_polynomial()
|
2705 |
+
if not ispoly:
|
2706 |
+
israt = func.is_rational_function()
|
2707 |
+
else:
|
2708 |
+
israt = True
|
2709 |
+
|
2710 |
+
if not (ispoly or israt):
|
2711 |
+
basepoly, ratexp = func.as_base_exp()
|
2712 |
+
if basepoly.is_polynomial() and ratexp.is_Number:
|
2713 |
+
if isinstance(ratexp, Float):
|
2714 |
+
ratexp = nsimplify(ratexp)
|
2715 |
+
m, n = ratexp.p, ratexp.q
|
2716 |
+
is_alg = True
|
2717 |
+
else:
|
2718 |
+
is_alg = False
|
2719 |
+
else:
|
2720 |
+
is_alg = True
|
2721 |
+
|
2722 |
+
if not (ispoly or israt or is_alg):
|
2723 |
+
return None
|
2724 |
+
|
2725 |
+
R = domain.old_poly_ring(x)
|
2726 |
+
_, Dx = DifferentialOperators(R, 'Dx')
|
2727 |
+
|
2728 |
+
# if the function is constant
|
2729 |
+
if not func.has(x):
|
2730 |
+
return HolonomicFunction(Dx, x, 0, [func])
|
2731 |
+
|
2732 |
+
if ispoly:
|
2733 |
+
# differential equation satisfied by polynomial
|
2734 |
+
sol = func * Dx - func.diff(x)
|
2735 |
+
sol = _normalize(sol.listofpoly, sol.parent, negative=False)
|
2736 |
+
is_singular = sol.is_singular(x0)
|
2737 |
+
|
2738 |
+
# try to compute the conditions for singular points
|
2739 |
+
if y0 is None and x0 == 0 and is_singular:
|
2740 |
+
rep = R.from_sympy(func).rep
|
2741 |
+
for i, j in enumerate(reversed(rep)):
|
2742 |
+
if j == 0:
|
2743 |
+
continue
|
2744 |
+
else:
|
2745 |
+
coeff = list(reversed(rep))[i:]
|
2746 |
+
indicial = i
|
2747 |
+
break
|
2748 |
+
for i, j in enumerate(coeff):
|
2749 |
+
if isinstance(j, (PolyElement, FracElement)):
|
2750 |
+
coeff[i] = j.as_expr()
|
2751 |
+
y0 = {indicial: S(coeff)}
|
2752 |
+
|
2753 |
+
elif israt:
|
2754 |
+
p, q = func.as_numer_denom()
|
2755 |
+
# differential equation satisfied by rational
|
2756 |
+
sol = p * q * Dx + p * q.diff(x) - q * p.diff(x)
|
2757 |
+
sol = _normalize(sol.listofpoly, sol.parent, negative=False)
|
2758 |
+
|
2759 |
+
elif is_alg:
|
2760 |
+
sol = n * (x / m) * Dx - 1
|
2761 |
+
sol = HolonomicFunction(sol, x).composition(basepoly).annihilator
|
2762 |
+
is_singular = sol.is_singular(x0)
|
2763 |
+
|
2764 |
+
# try to compute the conditions for singular points
|
2765 |
+
if y0 is None and x0 == 0 and is_singular and \
|
2766 |
+
(lenics is None or lenics <= 1):
|
2767 |
+
rep = R.from_sympy(basepoly).rep
|
2768 |
+
for i, j in enumerate(reversed(rep)):
|
2769 |
+
if j == 0:
|
2770 |
+
continue
|
2771 |
+
if isinstance(j, (PolyElement, FracElement)):
|
2772 |
+
j = j.as_expr()
|
2773 |
+
|
2774 |
+
coeff = S(j)**ratexp
|
2775 |
+
indicial = S(i) * ratexp
|
2776 |
+
break
|
2777 |
+
if isinstance(coeff, (PolyElement, FracElement)):
|
2778 |
+
coeff = coeff.as_expr()
|
2779 |
+
y0 = {indicial: S([coeff])}
|
2780 |
+
|
2781 |
+
if y0 or not initcond:
|
2782 |
+
return HolonomicFunction(sol, x, x0, y0)
|
2783 |
+
|
2784 |
+
if not lenics:
|
2785 |
+
lenics = sol.order
|
2786 |
+
|
2787 |
+
if sol.is_singular(x0):
|
2788 |
+
r = HolonomicFunction(sol, x, x0)._indicial()
|
2789 |
+
l = list(r)
|
2790 |
+
if len(r) == 1 and r[l[0]] == S.One:
|
2791 |
+
r = l[0]
|
2792 |
+
g = func / (x - x0)**r
|
2793 |
+
singular_ics = _find_conditions(g, x, x0, lenics)
|
2794 |
+
singular_ics = [j / factorial(i) for i, j in enumerate(singular_ics)]
|
2795 |
+
y0 = {r:singular_ics}
|
2796 |
+
return HolonomicFunction(sol, x, x0, y0)
|
2797 |
+
|
2798 |
+
y0 = _find_conditions(func, x, x0, lenics)
|
2799 |
+
while not y0:
|
2800 |
+
x0 += 1
|
2801 |
+
y0 = _find_conditions(func, x, x0, lenics)
|
2802 |
+
|
2803 |
+
return HolonomicFunction(sol, x, x0, y0)
|
2804 |
+
|
2805 |
+
|
2806 |
+
def _convert_meijerint(func, x, initcond=True, domain=QQ):
|
2807 |
+
args = meijerint._rewrite1(func, x)
|
2808 |
+
|
2809 |
+
if args:
|
2810 |
+
fac, po, g, _ = args
|
2811 |
+
else:
|
2812 |
+
return None
|
2813 |
+
|
2814 |
+
# lists for sum of meijerg functions
|
2815 |
+
fac_list = [fac * i[0] for i in g]
|
2816 |
+
t = po.as_base_exp()
|
2817 |
+
s = t[1] if t[0] == x else S.Zero
|
2818 |
+
po_list = [s + i[1] for i in g]
|
2819 |
+
G_list = [i[2] for i in g]
|
2820 |
+
|
2821 |
+
# finds meijerg representation of x**s * meijerg(a1 ... ap, b1 ... bq, z)
|
2822 |
+
def _shift(func, s):
|
2823 |
+
z = func.args[-1]
|
2824 |
+
if z.has(I):
|
2825 |
+
z = z.subs(exp_polar, exp)
|
2826 |
+
|
2827 |
+
d = z.collect(x, evaluate=False)
|
2828 |
+
b = list(d)[0]
|
2829 |
+
a = d[b]
|
2830 |
+
|
2831 |
+
t = b.as_base_exp()
|
2832 |
+
b = t[1] if t[0] == x else S.Zero
|
2833 |
+
r = s / b
|
2834 |
+
an = (i + r for i in func.args[0][0])
|
2835 |
+
ap = (i + r for i in func.args[0][1])
|
2836 |
+
bm = (i + r for i in func.args[1][0])
|
2837 |
+
bq = (i + r for i in func.args[1][1])
|
2838 |
+
|
2839 |
+
return a**-r, meijerg((an, ap), (bm, bq), z)
|
2840 |
+
|
2841 |
+
coeff, m = _shift(G_list[0], po_list[0])
|
2842 |
+
sol = fac_list[0] * coeff * from_meijerg(m, initcond=initcond, domain=domain)
|
2843 |
+
|
2844 |
+
# add all the meijerg functions after converting to holonomic
|
2845 |
+
for i in range(1, len(G_list)):
|
2846 |
+
coeff, m = _shift(G_list[i], po_list[i])
|
2847 |
+
sol += fac_list[i] * coeff * from_meijerg(m, initcond=initcond, domain=domain)
|
2848 |
+
|
2849 |
+
return sol
|
2850 |
+
|
2851 |
+
|
2852 |
+
def _create_table(table, domain=QQ):
|
2853 |
+
"""
|
2854 |
+
Creates the look-up table. For a similar implementation
|
2855 |
+
see meijerint._create_lookup_table.
|
2856 |
+
"""
|
2857 |
+
|
2858 |
+
def add(formula, annihilator, arg, x0=0, y0=()):
|
2859 |
+
"""
|
2860 |
+
Adds a formula in the dictionary
|
2861 |
+
"""
|
2862 |
+
table.setdefault(_mytype(formula, x_1), []).append((formula,
|
2863 |
+
HolonomicFunction(annihilator, arg, x0, y0)))
|
2864 |
+
|
2865 |
+
R = domain.old_poly_ring(x_1)
|
2866 |
+
_, Dx = DifferentialOperators(R, 'Dx')
|
2867 |
+
|
2868 |
+
# add some basic functions
|
2869 |
+
add(sin(x_1), Dx**2 + 1, x_1, 0, [0, 1])
|
2870 |
+
add(cos(x_1), Dx**2 + 1, x_1, 0, [1, 0])
|
2871 |
+
add(exp(x_1), Dx - 1, x_1, 0, 1)
|
2872 |
+
add(log(x_1), Dx + x_1*Dx**2, x_1, 1, [0, 1])
|
2873 |
+
|
2874 |
+
add(erf(x_1), 2*x_1*Dx + Dx**2, x_1, 0, [0, 2/sqrt(pi)])
|
2875 |
+
add(erfc(x_1), 2*x_1*Dx + Dx**2, x_1, 0, [1, -2/sqrt(pi)])
|
2876 |
+
add(erfi(x_1), -2*x_1*Dx + Dx**2, x_1, 0, [0, 2/sqrt(pi)])
|
2877 |
+
|
2878 |
+
add(sinh(x_1), Dx**2 - 1, x_1, 0, [0, 1])
|
2879 |
+
add(cosh(x_1), Dx**2 - 1, x_1, 0, [1, 0])
|
2880 |
+
|
2881 |
+
add(sinc(x_1), x_1 + 2*Dx + x_1*Dx**2, x_1)
|
2882 |
+
|
2883 |
+
add(Si(x_1), x_1*Dx + 2*Dx**2 + x_1*Dx**3, x_1)
|
2884 |
+
add(Ci(x_1), x_1*Dx + 2*Dx**2 + x_1*Dx**3, x_1)
|
2885 |
+
|
2886 |
+
add(Shi(x_1), -x_1*Dx + 2*Dx**2 + x_1*Dx**3, x_1)
|
2887 |
+
|
2888 |
+
|
2889 |
+
def _find_conditions(func, x, x0, order):
|
2890 |
+
y0 = []
|
2891 |
+
for i in range(order):
|
2892 |
+
val = func.subs(x, x0)
|
2893 |
+
if isinstance(val, NaN):
|
2894 |
+
val = limit(func, x, x0)
|
2895 |
+
if val.is_finite is False or isinstance(val, NaN):
|
2896 |
+
return None
|
2897 |
+
y0.append(val)
|
2898 |
+
func = func.diff(x)
|
2899 |
+
return y0
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/holonomicerrors.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Common Exceptions for `holonomic` module. """
|
2 |
+
|
3 |
+
class BaseHolonomicError(Exception):
|
4 |
+
|
5 |
+
def new(self, *args):
|
6 |
+
raise NotImplementedError("abstract base class")
|
7 |
+
|
8 |
+
class NotPowerSeriesError(BaseHolonomicError):
|
9 |
+
|
10 |
+
def __init__(self, holonomic, x0):
|
11 |
+
self.holonomic = holonomic
|
12 |
+
self.x0 = x0
|
13 |
+
|
14 |
+
def __str__(self):
|
15 |
+
s = 'A Power Series does not exists for '
|
16 |
+
s += str(self.holonomic)
|
17 |
+
s += ' about %s.' %self.x0
|
18 |
+
return s
|
19 |
+
|
20 |
+
class NotHolonomicError(BaseHolonomicError):
|
21 |
+
|
22 |
+
def __init__(self, m):
|
23 |
+
self.m = m
|
24 |
+
|
25 |
+
def __str__(self):
|
26 |
+
return self.m
|
27 |
+
|
28 |
+
class SingularityError(BaseHolonomicError):
|
29 |
+
|
30 |
+
def __init__(self, holonomic, x0):
|
31 |
+
self.holonomic = holonomic
|
32 |
+
self.x0 = x0
|
33 |
+
|
34 |
+
def __str__(self):
|
35 |
+
s = str(self.holonomic)
|
36 |
+
s += ' has a singularity at %s.' %self.x0
|
37 |
+
return s
|
38 |
+
|
39 |
+
class NotHyperSeriesError(BaseHolonomicError):
|
40 |
+
|
41 |
+
def __init__(self, holonomic, x0):
|
42 |
+
self.holonomic = holonomic
|
43 |
+
self.x0 = x0
|
44 |
+
|
45 |
+
def __str__(self):
|
46 |
+
s = 'Power series expansion of '
|
47 |
+
s += str(self.holonomic)
|
48 |
+
s += ' about %s is not hypergeometric' %self.x0
|
49 |
+
return s
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/numerical.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Numerical Methods for Holonomic Functions"""
|
2 |
+
|
3 |
+
from sympy.core.sympify import sympify
|
4 |
+
from sympy.holonomic.holonomic import DMFsubs
|
5 |
+
|
6 |
+
from mpmath import mp
|
7 |
+
|
8 |
+
|
9 |
+
def _evalf(func, points, derivatives=False, method='RK4'):
|
10 |
+
"""
|
11 |
+
Numerical methods for numerical integration along a given set of
|
12 |
+
points in the complex plane.
|
13 |
+
"""
|
14 |
+
|
15 |
+
ann = func.annihilator
|
16 |
+
a = ann.order
|
17 |
+
R = ann.parent.base
|
18 |
+
K = R.get_field()
|
19 |
+
|
20 |
+
if method == 'Euler':
|
21 |
+
meth = _euler
|
22 |
+
else:
|
23 |
+
meth = _rk4
|
24 |
+
|
25 |
+
dmf = []
|
26 |
+
for j in ann.listofpoly:
|
27 |
+
dmf.append(K.new(j.rep))
|
28 |
+
|
29 |
+
red = [-dmf[i] / dmf[a] for i in range(a)]
|
30 |
+
|
31 |
+
y0 = func.y0
|
32 |
+
if len(y0) < a:
|
33 |
+
raise TypeError("Not Enough Initial Conditions")
|
34 |
+
x0 = func.x0
|
35 |
+
sol = [meth(red, x0, points[0], y0, a)]
|
36 |
+
|
37 |
+
for i, j in enumerate(points[1:]):
|
38 |
+
sol.append(meth(red, points[i], j, sol[-1], a))
|
39 |
+
|
40 |
+
if not derivatives:
|
41 |
+
return [sympify(i[0]) for i in sol]
|
42 |
+
else:
|
43 |
+
return sympify(sol)
|
44 |
+
|
45 |
+
|
46 |
+
def _euler(red, x0, x1, y0, a):
|
47 |
+
"""
|
48 |
+
Euler's method for numerical integration.
|
49 |
+
From x0 to x1 with initial values given at x0 as vector y0.
|
50 |
+
"""
|
51 |
+
|
52 |
+
A = sympify(x0)._to_mpmath(mp.prec)
|
53 |
+
B = sympify(x1)._to_mpmath(mp.prec)
|
54 |
+
y_0 = [sympify(i)._to_mpmath(mp.prec) for i in y0]
|
55 |
+
h = B - A
|
56 |
+
f_0 = y_0[1:]
|
57 |
+
f_0_n = 0
|
58 |
+
|
59 |
+
for i in range(a):
|
60 |
+
f_0_n += sympify(DMFsubs(red[i], A, mpm=True))._to_mpmath(mp.prec) * y_0[i]
|
61 |
+
f_0.append(f_0_n)
|
62 |
+
|
63 |
+
sol = []
|
64 |
+
for i in range(a):
|
65 |
+
sol.append(y_0[i] + h * f_0[i])
|
66 |
+
|
67 |
+
return sol
|
68 |
+
|
69 |
+
|
70 |
+
def _rk4(red, x0, x1, y0, a):
|
71 |
+
"""
|
72 |
+
Runge-Kutta 4th order numerical method.
|
73 |
+
"""
|
74 |
+
|
75 |
+
A = sympify(x0)._to_mpmath(mp.prec)
|
76 |
+
B = sympify(x1)._to_mpmath(mp.prec)
|
77 |
+
y_0 = [sympify(i)._to_mpmath(mp.prec) for i in y0]
|
78 |
+
h = B - A
|
79 |
+
|
80 |
+
f_0_n = 0
|
81 |
+
f_1_n = 0
|
82 |
+
f_2_n = 0
|
83 |
+
f_3_n = 0
|
84 |
+
|
85 |
+
f_0 = y_0[1:]
|
86 |
+
for i in range(a):
|
87 |
+
f_0_n += sympify(DMFsubs(red[i], A, mpm=True))._to_mpmath(mp.prec) * y_0[i]
|
88 |
+
f_0.append(f_0_n)
|
89 |
+
|
90 |
+
f_1 = [y_0[i] + f_0[i]*h/2 for i in range(1, a)]
|
91 |
+
for i in range(a):
|
92 |
+
f_1_n += sympify(DMFsubs(red[i], A + h/2, mpm=True))._to_mpmath(mp.prec) * (y_0[i] + f_0[i]*h/2)
|
93 |
+
f_1.append(f_1_n)
|
94 |
+
|
95 |
+
f_2 = [y_0[i] + f_1[i]*h/2 for i in range(1, a)]
|
96 |
+
for i in range(a):
|
97 |
+
f_2_n += sympify(DMFsubs(red[i], A + h/2, mpm=True))._to_mpmath(mp.prec) * (y_0[i] + f_1[i]*h/2)
|
98 |
+
f_2.append(f_2_n)
|
99 |
+
|
100 |
+
f_3 = [y_0[i] + f_2[i]*h for i in range(1, a)]
|
101 |
+
for i in range(a):
|
102 |
+
f_3_n += sympify(DMFsubs(red[i], A + h, mpm=True))._to_mpmath(mp.prec) * (y_0[i] + f_2[i]*h)
|
103 |
+
f_3.append(f_3_n)
|
104 |
+
|
105 |
+
sol = []
|
106 |
+
for i in range(a):
|
107 |
+
sol.append(y_0[i] + h * (f_0[i]+2*f_1[i]+2*f_2[i]+f_3[i])/6)
|
108 |
+
|
109 |
+
return sol
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/recurrence.py
ADDED
@@ -0,0 +1,365 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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rence Operators"""
|
2 |
+
|
3 |
+
from sympy.core.singleton import S
|
4 |
+
from sympy.core.symbol import (Symbol, symbols)
|
5 |
+
from sympy.printing import sstr
|
6 |
+
from sympy.core.sympify import sympify
|
7 |
+
|
8 |
+
|
9 |
+
def RecurrenceOperators(base, generator):
|
10 |
+
"""
|
11 |
+
Returns an Algebra of Recurrence Operators and the operator for
|
12 |
+
shifting i.e. the `Sn` operator.
|
13 |
+
The first argument needs to be the base polynomial ring for the algebra
|
14 |
+
and the second argument must be a generator which can be either a
|
15 |
+
noncommutative Symbol or a string.
|
16 |
+
|
17 |
+
Examples
|
18 |
+
========
|
19 |
+
|
20 |
+
>>> from sympy import ZZ
|
21 |
+
>>> from sympy import symbols
|
22 |
+
>>> from sympy.holonomic.recurrence import RecurrenceOperators
|
23 |
+
>>> n = symbols('n', integer=True)
|
24 |
+
>>> R, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), 'Sn')
|
25 |
+
"""
|
26 |
+
|
27 |
+
ring = RecurrenceOperatorAlgebra(base, generator)
|
28 |
+
return (ring, ring.shift_operator)
|
29 |
+
|
30 |
+
|
31 |
+
class RecurrenceOperatorAlgebra:
|
32 |
+
"""
|
33 |
+
A Recurrence Operator Algebra is a set of noncommutative polynomials
|
34 |
+
in intermediate `Sn` and coefficients in a base ring A. It follows the
|
35 |
+
commutation rule:
|
36 |
+
Sn * a(n) = a(n + 1) * Sn
|
37 |
+
|
38 |
+
This class represents a Recurrence Operator Algebra and serves as the parent ring
|
39 |
+
for Recurrence Operators.
|
40 |
+
|
41 |
+
Examples
|
42 |
+
========
|
43 |
+
|
44 |
+
>>> from sympy import ZZ
|
45 |
+
>>> from sympy import symbols
|
46 |
+
>>> from sympy.holonomic.recurrence import RecurrenceOperators
|
47 |
+
>>> n = symbols('n', integer=True)
|
48 |
+
>>> R, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), 'Sn')
|
49 |
+
>>> R
|
50 |
+
Univariate Recurrence Operator Algebra in intermediate Sn over the base ring
|
51 |
+
ZZ[n]
|
52 |
+
|
53 |
+
See Also
|
54 |
+
========
|
55 |
+
|
56 |
+
RecurrenceOperator
|
57 |
+
"""
|
58 |
+
|
59 |
+
def __init__(self, base, generator):
|
60 |
+
# the base ring for the algebra
|
61 |
+
self.base = base
|
62 |
+
# the operator representing shift i.e. `Sn`
|
63 |
+
self.shift_operator = RecurrenceOperator(
|
64 |
+
[base.zero, base.one], self)
|
65 |
+
|
66 |
+
if generator is None:
|
67 |
+
self.gen_symbol = symbols('Sn', commutative=False)
|
68 |
+
else:
|
69 |
+
if isinstance(generator, str):
|
70 |
+
self.gen_symbol = symbols(generator, commutative=False)
|
71 |
+
elif isinstance(generator, Symbol):
|
72 |
+
self.gen_symbol = generator
|
73 |
+
|
74 |
+
def __str__(self):
|
75 |
+
string = 'Univariate Recurrence Operator Algebra in intermediate '\
|
76 |
+
+ sstr(self.gen_symbol) + ' over the base ring ' + \
|
77 |
+
(self.base).__str__()
|
78 |
+
|
79 |
+
return string
|
80 |
+
|
81 |
+
__repr__ = __str__
|
82 |
+
|
83 |
+
def __eq__(self, other):
|
84 |
+
if self.base == other.base and self.gen_symbol == other.gen_symbol:
|
85 |
+
return True
|
86 |
+
else:
|
87 |
+
return False
|
88 |
+
|
89 |
+
|
90 |
+
def _add_lists(list1, list2):
|
91 |
+
if len(list1) <= len(list2):
|
92 |
+
sol = [a + b for a, b in zip(list1, list2)] + list2[len(list1):]
|
93 |
+
else:
|
94 |
+
sol = [a + b for a, b in zip(list1, list2)] + list1[len(list2):]
|
95 |
+
return sol
|
96 |
+
|
97 |
+
|
98 |
+
class RecurrenceOperator:
|
99 |
+
"""
|
100 |
+
The Recurrence Operators are defined by a list of polynomials
|
101 |
+
in the base ring and the parent ring of the Operator.
|
102 |
+
|
103 |
+
Explanation
|
104 |
+
===========
|
105 |
+
|
106 |
+
Takes a list of polynomials for each power of Sn and the
|
107 |
+
parent ring which must be an instance of RecurrenceOperatorAlgebra.
|
108 |
+
|
109 |
+
A Recurrence Operator can be created easily using
|
110 |
+
the operator `Sn`. See examples below.
|
111 |
+
|
112 |
+
Examples
|
113 |
+
========
|
114 |
+
|
115 |
+
>>> from sympy.holonomic.recurrence import RecurrenceOperator, RecurrenceOperators
|
116 |
+
>>> from sympy import ZZ
|
117 |
+
>>> from sympy import symbols
|
118 |
+
>>> n = symbols('n', integer=True)
|
119 |
+
>>> R, Sn = RecurrenceOperators(ZZ.old_poly_ring(n),'Sn')
|
120 |
+
|
121 |
+
>>> RecurrenceOperator([0, 1, n**2], R)
|
122 |
+
(1)Sn + (n**2)Sn**2
|
123 |
+
|
124 |
+
>>> Sn*n
|
125 |
+
(n + 1)Sn
|
126 |
+
|
127 |
+
>>> n*Sn*n + 1 - Sn**2*n
|
128 |
+
(1) + (n**2 + n)Sn + (-n - 2)Sn**2
|
129 |
+
|
130 |
+
See Also
|
131 |
+
========
|
132 |
+
|
133 |
+
DifferentialOperatorAlgebra
|
134 |
+
"""
|
135 |
+
|
136 |
+
_op_priority = 20
|
137 |
+
|
138 |
+
def __init__(self, list_of_poly, parent):
|
139 |
+
# the parent ring for this operator
|
140 |
+
# must be an RecurrenceOperatorAlgebra object
|
141 |
+
self.parent = parent
|
142 |
+
# sequence of polynomials in n for each power of Sn
|
143 |
+
# represents the operator
|
144 |
+
# convert the expressions into ring elements using from_sympy
|
145 |
+
if isinstance(list_of_poly, list):
|
146 |
+
for i, j in enumerate(list_of_poly):
|
147 |
+
if isinstance(j, int):
|
148 |
+
list_of_poly[i] = self.parent.base.from_sympy(S(j))
|
149 |
+
elif not isinstance(j, self.parent.base.dtype):
|
150 |
+
list_of_poly[i] = self.parent.base.from_sympy(j)
|
151 |
+
|
152 |
+
self.listofpoly = list_of_poly
|
153 |
+
self.order = len(self.listofpoly) - 1
|
154 |
+
|
155 |
+
def __mul__(self, other):
|
156 |
+
"""
|
157 |
+
Multiplies two Operators and returns another
|
158 |
+
RecurrenceOperator instance using the commutation rule
|
159 |
+
Sn * a(n) = a(n + 1) * Sn
|
160 |
+
"""
|
161 |
+
|
162 |
+
listofself = self.listofpoly
|
163 |
+
base = self.parent.base
|
164 |
+
|
165 |
+
if not isinstance(other, RecurrenceOperator):
|
166 |
+
if not isinstance(other, self.parent.base.dtype):
|
167 |
+
listofother = [self.parent.base.from_sympy(sympify(other))]
|
168 |
+
|
169 |
+
else:
|
170 |
+
listofother = [other]
|
171 |
+
else:
|
172 |
+
listofother = other.listofpoly
|
173 |
+
# multiply a polynomial `b` with a list of polynomials
|
174 |
+
|
175 |
+
def _mul_dmp_diffop(b, listofother):
|
176 |
+
if isinstance(listofother, list):
|
177 |
+
sol = []
|
178 |
+
for i in listofother:
|
179 |
+
sol.append(i * b)
|
180 |
+
return sol
|
181 |
+
else:
|
182 |
+
return [b * listofother]
|
183 |
+
|
184 |
+
sol = _mul_dmp_diffop(listofself[0], listofother)
|
185 |
+
|
186 |
+
# compute Sn^i * b
|
187 |
+
def _mul_Sni_b(b):
|
188 |
+
sol = [base.zero]
|
189 |
+
|
190 |
+
if isinstance(b, list):
|
191 |
+
for i in b:
|
192 |
+
j = base.to_sympy(i).subs(base.gens[0], base.gens[0] + S.One)
|
193 |
+
sol.append(base.from_sympy(j))
|
194 |
+
|
195 |
+
else:
|
196 |
+
j = b.subs(base.gens[0], base.gens[0] + S.One)
|
197 |
+
sol.append(base.from_sympy(j))
|
198 |
+
|
199 |
+
return sol
|
200 |
+
|
201 |
+
for i in range(1, len(listofself)):
|
202 |
+
# find Sn^i * b in ith iteration
|
203 |
+
listofother = _mul_Sni_b(listofother)
|
204 |
+
# solution = solution + listofself[i] * (Sn^i * b)
|
205 |
+
sol = _add_lists(sol, _mul_dmp_diffop(listofself[i], listofother))
|
206 |
+
|
207 |
+
return RecurrenceOperator(sol, self.parent)
|
208 |
+
|
209 |
+
def __rmul__(self, other):
|
210 |
+
if not isinstance(other, RecurrenceOperator):
|
211 |
+
|
212 |
+
if isinstance(other, int):
|
213 |
+
other = S(other)
|
214 |
+
|
215 |
+
if not isinstance(other, self.parent.base.dtype):
|
216 |
+
other = (self.parent.base).from_sympy(other)
|
217 |
+
|
218 |
+
sol = []
|
219 |
+
for j in self.listofpoly:
|
220 |
+
sol.append(other * j)
|
221 |
+
|
222 |
+
return RecurrenceOperator(sol, self.parent)
|
223 |
+
|
224 |
+
def __add__(self, other):
|
225 |
+
if isinstance(other, RecurrenceOperator):
|
226 |
+
|
227 |
+
sol = _add_lists(self.listofpoly, other.listofpoly)
|
228 |
+
return RecurrenceOperator(sol, self.parent)
|
229 |
+
|
230 |
+
else:
|
231 |
+
|
232 |
+
if isinstance(other, int):
|
233 |
+
other = S(other)
|
234 |
+
list_self = self.listofpoly
|
235 |
+
if not isinstance(other, self.parent.base.dtype):
|
236 |
+
list_other = [((self.parent).base).from_sympy(other)]
|
237 |
+
else:
|
238 |
+
list_other = [other]
|
239 |
+
sol = []
|
240 |
+
sol.append(list_self[0] + list_other[0])
|
241 |
+
sol += list_self[1:]
|
242 |
+
|
243 |
+
return RecurrenceOperator(sol, self.parent)
|
244 |
+
|
245 |
+
__radd__ = __add__
|
246 |
+
|
247 |
+
def __sub__(self, other):
|
248 |
+
return self + (-1) * other
|
249 |
+
|
250 |
+
def __rsub__(self, other):
|
251 |
+
return (-1) * self + other
|
252 |
+
|
253 |
+
def __pow__(self, n):
|
254 |
+
if n == 1:
|
255 |
+
return self
|
256 |
+
if n == 0:
|
257 |
+
return RecurrenceOperator([self.parent.base.one], self.parent)
|
258 |
+
# if self is `Sn`
|
259 |
+
if self.listofpoly == self.parent.shift_operator.listofpoly:
|
260 |
+
sol = []
|
261 |
+
for i in range(0, n):
|
262 |
+
sol.append(self.parent.base.zero)
|
263 |
+
sol.append(self.parent.base.one)
|
264 |
+
|
265 |
+
return RecurrenceOperator(sol, self.parent)
|
266 |
+
|
267 |
+
else:
|
268 |
+
if n % 2 == 1:
|
269 |
+
powreduce = self**(n - 1)
|
270 |
+
return powreduce * self
|
271 |
+
elif n % 2 == 0:
|
272 |
+
powreduce = self**(n / 2)
|
273 |
+
return powreduce * powreduce
|
274 |
+
|
275 |
+
def __str__(self):
|
276 |
+
listofpoly = self.listofpoly
|
277 |
+
print_str = ''
|
278 |
+
|
279 |
+
for i, j in enumerate(listofpoly):
|
280 |
+
if j == self.parent.base.zero:
|
281 |
+
continue
|
282 |
+
|
283 |
+
if i == 0:
|
284 |
+
print_str += '(' + sstr(j) + ')'
|
285 |
+
continue
|
286 |
+
|
287 |
+
if print_str:
|
288 |
+
print_str += ' + '
|
289 |
+
|
290 |
+
if i == 1:
|
291 |
+
print_str += '(' + sstr(j) + ')Sn'
|
292 |
+
continue
|
293 |
+
|
294 |
+
print_str += '(' + sstr(j) + ')' + 'Sn**' + sstr(i)
|
295 |
+
|
296 |
+
return print_str
|
297 |
+
|
298 |
+
__repr__ = __str__
|
299 |
+
|
300 |
+
def __eq__(self, other):
|
301 |
+
if isinstance(other, RecurrenceOperator):
|
302 |
+
if self.listofpoly == other.listofpoly and self.parent == other.parent:
|
303 |
+
return True
|
304 |
+
else:
|
305 |
+
return False
|
306 |
+
else:
|
307 |
+
if self.listofpoly[0] == other:
|
308 |
+
for i in self.listofpoly[1:]:
|
309 |
+
if i is not self.parent.base.zero:
|
310 |
+
return False
|
311 |
+
return True
|
312 |
+
else:
|
313 |
+
return False
|
314 |
+
|
315 |
+
|
316 |
+
class HolonomicSequence:
|
317 |
+
"""
|
318 |
+
A Holonomic Sequence is a type of sequence satisfying a linear homogeneous
|
319 |
+
recurrence relation with Polynomial coefficients. Alternatively, A sequence
|
320 |
+
is Holonomic if and only if its generating function is a Holonomic Function.
|
321 |
+
"""
|
322 |
+
|
323 |
+
def __init__(self, recurrence, u0=[]):
|
324 |
+
self.recurrence = recurrence
|
325 |
+
if not isinstance(u0, list):
|
326 |
+
self.u0 = [u0]
|
327 |
+
else:
|
328 |
+
self.u0 = u0
|
329 |
+
|
330 |
+
if len(self.u0) == 0:
|
331 |
+
self._have_init_cond = False
|
332 |
+
else:
|
333 |
+
self._have_init_cond = True
|
334 |
+
self.n = recurrence.parent.base.gens[0]
|
335 |
+
|
336 |
+
def __repr__(self):
|
337 |
+
str_sol = 'HolonomicSequence(%s, %s)' % ((self.recurrence).__repr__(), sstr(self.n))
|
338 |
+
if not self._have_init_cond:
|
339 |
+
return str_sol
|
340 |
+
else:
|
341 |
+
cond_str = ''
|
342 |
+
seq_str = 0
|
343 |
+
for i in self.u0:
|
344 |
+
cond_str += ', u(%s) = %s' % (sstr(seq_str), sstr(i))
|
345 |
+
seq_str += 1
|
346 |
+
|
347 |
+
sol = str_sol + cond_str
|
348 |
+
return sol
|
349 |
+
|
350 |
+
__str__ = __repr__
|
351 |
+
|
352 |
+
def __eq__(self, other):
|
353 |
+
if self.recurrence == other.recurrence:
|
354 |
+
if self.n == other.n:
|
355 |
+
if self._have_init_cond and other._have_init_cond:
|
356 |
+
if self.u0 == other.u0:
|
357 |
+
return True
|
358 |
+
else:
|
359 |
+
return False
|
360 |
+
else:
|
361 |
+
return True
|
362 |
+
else:
|
363 |
+
return False
|
364 |
+
else:
|
365 |
+
return False
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (186 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__pycache__/test_holonomic.cpython-310.pyc
ADDED
Binary file (31.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/__pycache__/test_recurrence.cpython-310.pyc
ADDED
Binary file (1.31 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/test_holonomic.py
ADDED
@@ -0,0 +1,830 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.holonomic import (DifferentialOperator, HolonomicFunction,
|
2 |
+
DifferentialOperators, from_hyper,
|
3 |
+
from_meijerg, expr_to_holonomic)
|
4 |
+
from sympy.holonomic.recurrence import RecurrenceOperators, HolonomicSequence
|
5 |
+
from sympy.core import EulerGamma
|
6 |
+
from sympy.core.numbers import (I, Rational, pi)
|
7 |
+
from sympy.core.singleton import S
|
8 |
+
from sympy.core.symbol import (Symbol, symbols)
|
9 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
10 |
+
from sympy.functions.elementary.hyperbolic import (asinh, cosh)
|
11 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
12 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
13 |
+
from sympy.functions.special.bessel import besselj
|
14 |
+
from sympy.functions.special.beta_functions import beta
|
15 |
+
from sympy.functions.special.error_functions import (Ci, Si, erf, erfc)
|
16 |
+
from sympy.functions.special.gamma_functions import gamma
|
17 |
+
from sympy.functions.special.hyper import (hyper, meijerg)
|
18 |
+
from sympy.printing.str import sstr
|
19 |
+
from sympy.series.order import O
|
20 |
+
from sympy.simplify.hyperexpand import hyperexpand
|
21 |
+
from sympy.polys.domains.integerring import ZZ
|
22 |
+
from sympy.polys.domains.rationalfield import QQ
|
23 |
+
from sympy.polys.domains.realfield import RR
|
24 |
+
|
25 |
+
|
26 |
+
def test_DifferentialOperator():
|
27 |
+
x = symbols('x')
|
28 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
29 |
+
assert Dx == R.derivative_operator
|
30 |
+
assert Dx == DifferentialOperator([R.base.zero, R.base.one], R)
|
31 |
+
assert x * Dx + x**2 * Dx**2 == DifferentialOperator([0, x, x**2], R)
|
32 |
+
assert (x**2 + 1) + Dx + x * \
|
33 |
+
Dx**5 == DifferentialOperator([x**2 + 1, 1, 0, 0, 0, x], R)
|
34 |
+
assert (x * Dx + x**2 + 1 - Dx * (x**3 + x))**3 == (-48 * x**6) + \
|
35 |
+
(-57 * x**7) * Dx + (-15 * x**8) * Dx**2 + (-x**9) * Dx**3
|
36 |
+
p = (x * Dx**2 + (x**2 + 3) * Dx**5) * (Dx + x**2)
|
37 |
+
q = (2 * x) + (4 * x**2) * Dx + (x**3) * Dx**2 + \
|
38 |
+
(20 * x**2 + x + 60) * Dx**3 + (10 * x**3 + 30 * x) * Dx**4 + \
|
39 |
+
(x**4 + 3 * x**2) * Dx**5 + (x**2 + 3) * Dx**6
|
40 |
+
assert p == q
|
41 |
+
|
42 |
+
|
43 |
+
def test_HolonomicFunction_addition():
|
44 |
+
x = symbols('x')
|
45 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
46 |
+
p = HolonomicFunction(Dx**2 * x, x)
|
47 |
+
q = HolonomicFunction((2) * Dx + (x) * Dx**2, x)
|
48 |
+
assert p == q
|
49 |
+
p = HolonomicFunction(x * Dx + 1, x)
|
50 |
+
q = HolonomicFunction(Dx + 1, x)
|
51 |
+
r = HolonomicFunction((x - 2) + (x**2 - 2) * Dx + (x**2 - x) * Dx**2, x)
|
52 |
+
assert p + q == r
|
53 |
+
p = HolonomicFunction(x * Dx + Dx**2 * (x**2 + 2), x)
|
54 |
+
q = HolonomicFunction(Dx - 3, x)
|
55 |
+
r = HolonomicFunction((-54 * x**2 - 126 * x - 150) + (-135 * x**3 - 252 * x**2 - 270 * x + 140) * Dx +\
|
56 |
+
(-27 * x**4 - 24 * x**2 + 14 * x - 150) * Dx**2 + \
|
57 |
+
(9 * x**4 + 15 * x**3 + 38 * x**2 + 30 * x +40) * Dx**3, x)
|
58 |
+
assert p + q == r
|
59 |
+
p = HolonomicFunction(Dx**5 - 1, x)
|
60 |
+
q = HolonomicFunction(x**3 + Dx, x)
|
61 |
+
r = HolonomicFunction((-x**18 + 45*x**14 - 525*x**10 + 1575*x**6 - x**3 - 630*x**2) + \
|
62 |
+
(-x**15 + 30*x**11 - 195*x**7 + 210*x**3 - 1)*Dx + (x**18 - 45*x**14 + 525*x**10 - \
|
63 |
+
1575*x**6 + x**3 + 630*x**2)*Dx**5 + (x**15 - 30*x**11 + 195*x**7 - 210*x**3 + \
|
64 |
+
1)*Dx**6, x)
|
65 |
+
assert p+q == r
|
66 |
+
|
67 |
+
p = x**2 + 3*x + 8
|
68 |
+
q = x**3 - 7*x + 5
|
69 |
+
p = p*Dx - p.diff()
|
70 |
+
q = q*Dx - q.diff()
|
71 |
+
r = HolonomicFunction(p, x) + HolonomicFunction(q, x)
|
72 |
+
s = HolonomicFunction((6*x**2 + 18*x + 14) + (-4*x**3 - 18*x**2 - 62*x + 10)*Dx +\
|
73 |
+
(x**4 + 6*x**3 + 31*x**2 - 10*x - 71)*Dx**2, x)
|
74 |
+
assert r == s
|
75 |
+
|
76 |
+
|
77 |
+
def test_HolonomicFunction_multiplication():
|
78 |
+
x = symbols('x')
|
79 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
80 |
+
p = HolonomicFunction(Dx+x+x*Dx**2, x)
|
81 |
+
q = HolonomicFunction(x*Dx+Dx*x+Dx**2, x)
|
82 |
+
r = HolonomicFunction((8*x**6 + 4*x**4 + 6*x**2 + 3) + (24*x**5 - 4*x**3 + 24*x)*Dx + \
|
83 |
+
(8*x**6 + 20*x**4 + 12*x**2 + 2)*Dx**2 + (8*x**5 + 4*x**3 + 4*x)*Dx**3 + \
|
84 |
+
(2*x**4 + x**2)*Dx**4, x)
|
85 |
+
assert p*q == r
|
86 |
+
p = HolonomicFunction(Dx**2+1, x)
|
87 |
+
q = HolonomicFunction(Dx-1, x)
|
88 |
+
r = HolonomicFunction((2) + (-2)*Dx + (1)*Dx**2, x)
|
89 |
+
assert p*q == r
|
90 |
+
p = HolonomicFunction(Dx**2+1+x+Dx, x)
|
91 |
+
q = HolonomicFunction((Dx*x-1)**2, x)
|
92 |
+
r = HolonomicFunction((4*x**7 + 11*x**6 + 16*x**5 + 4*x**4 - 6*x**3 - 7*x**2 - 8*x - 2) + \
|
93 |
+
(8*x**6 + 26*x**5 + 24*x**4 - 3*x**3 - 11*x**2 - 6*x - 2)*Dx + \
|
94 |
+
(8*x**6 + 18*x**5 + 15*x**4 - 3*x**3 - 6*x**2 - 6*x - 2)*Dx**2 + (8*x**5 + \
|
95 |
+
10*x**4 + 6*x**3 - 2*x**2 - 4*x)*Dx**3 + (4*x**5 + 3*x**4 - x**2)*Dx**4, x)
|
96 |
+
assert p*q == r
|
97 |
+
p = HolonomicFunction(x*Dx**2-1, x)
|
98 |
+
q = HolonomicFunction(Dx*x-x, x)
|
99 |
+
r = HolonomicFunction((x - 3) + (-2*x + 2)*Dx + (x)*Dx**2, x)
|
100 |
+
assert p*q == r
|
101 |
+
|
102 |
+
|
103 |
+
def test_addition_initial_condition():
|
104 |
+
x = symbols('x')
|
105 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
106 |
+
p = HolonomicFunction(Dx-1, x, 0, [3])
|
107 |
+
q = HolonomicFunction(Dx**2+1, x, 0, [1, 0])
|
108 |
+
r = HolonomicFunction(-1 + Dx - Dx**2 + Dx**3, x, 0, [4, 3, 2])
|
109 |
+
assert p + q == r
|
110 |
+
p = HolonomicFunction(Dx - x + Dx**2, x, 0, [1, 2])
|
111 |
+
q = HolonomicFunction(Dx**2 + x, x, 0, [1, 0])
|
112 |
+
r = HolonomicFunction((-x**4 - x**3/4 - x**2 + Rational(1, 4)) + (x**3 + x**2/4 + x*Rational(3, 4) + 1)*Dx + \
|
113 |
+
(x*Rational(-3, 2) + Rational(7, 4))*Dx**2 + (x**2 - x*Rational(7, 4) + Rational(1, 4))*Dx**3 + (x**2 + x/4 + S.Half)*Dx**4, x, 0, [2, 2, -2, 2])
|
114 |
+
assert p + q == r
|
115 |
+
p = HolonomicFunction(Dx**2 + 4*x*Dx + x**2, x, 0, [3, 4])
|
116 |
+
q = HolonomicFunction(Dx**2 + 1, x, 0, [1, 1])
|
117 |
+
r = HolonomicFunction((x**6 + 2*x**4 - 5*x**2 - 6) + (4*x**5 + 36*x**3 - 32*x)*Dx + \
|
118 |
+
(x**6 + 3*x**4 + 5*x**2 - 9)*Dx**2 + (4*x**5 + 36*x**3 - 32*x)*Dx**3 + (x**4 + \
|
119 |
+
10*x**2 - 3)*Dx**4, x, 0, [4, 5, -1, -17])
|
120 |
+
assert p + q == r
|
121 |
+
q = HolonomicFunction(Dx**3 + x, x, 2, [3, 0, 1])
|
122 |
+
p = HolonomicFunction(Dx - 1, x, 2, [1])
|
123 |
+
r = HolonomicFunction((-x**2 - x + 1) + (x**2 + x)*Dx + (-x - 2)*Dx**3 + \
|
124 |
+
(x + 1)*Dx**4, x, 2, [4, 1, 2, -5 ])
|
125 |
+
assert p + q == r
|
126 |
+
p = expr_to_holonomic(sin(x))
|
127 |
+
q = expr_to_holonomic(1/x, x0=1)
|
128 |
+
r = HolonomicFunction((x**2 + 6) + (x**3 + 2*x)*Dx + (x**2 + 6)*Dx**2 + (x**3 + 2*x)*Dx**3, \
|
129 |
+
x, 1, [sin(1) + 1, -1 + cos(1), -sin(1) + 2])
|
130 |
+
assert p + q == r
|
131 |
+
C_1 = symbols('C_1')
|
132 |
+
p = expr_to_holonomic(sqrt(x))
|
133 |
+
q = expr_to_holonomic(sqrt(x**2-x))
|
134 |
+
r = (p + q).to_expr().subs(C_1, -I/2).expand()
|
135 |
+
assert r == I*sqrt(x)*sqrt(-x + 1) + sqrt(x)
|
136 |
+
|
137 |
+
|
138 |
+
def test_multiplication_initial_condition():
|
139 |
+
x = symbols('x')
|
140 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
141 |
+
p = HolonomicFunction(Dx**2 + x*Dx - 1, x, 0, [3, 1])
|
142 |
+
q = HolonomicFunction(Dx**2 + 1, x, 0, [1, 1])
|
143 |
+
r = HolonomicFunction((x**4 + 14*x**2 + 60) + 4*x*Dx + (x**4 + 9*x**2 + 20)*Dx**2 + \
|
144 |
+
(2*x**3 + 18*x)*Dx**3 + (x**2 + 10)*Dx**4, x, 0, [3, 4, 2, 3])
|
145 |
+
assert p * q == r
|
146 |
+
p = HolonomicFunction(Dx**2 + x, x, 0, [1, 0])
|
147 |
+
q = HolonomicFunction(Dx**3 - x**2, x, 0, [3, 3, 3])
|
148 |
+
r = HolonomicFunction((x**8 - 37*x**7/27 - 10*x**6/27 - 164*x**5/9 - 184*x**4/9 + \
|
149 |
+
160*x**3/27 + 404*x**2/9 + 8*x + Rational(40, 3)) + (6*x**7 - 128*x**6/9 - 98*x**5/9 - 28*x**4/9 + \
|
150 |
+
8*x**3/9 + 28*x**2 + x*Rational(40, 9) - 40)*Dx + (3*x**6 - 82*x**5/9 + 76*x**4/9 + 4*x**3/3 + \
|
151 |
+
220*x**2/9 - x*Rational(80, 3))*Dx**2 + (-2*x**6 + 128*x**5/27 - 2*x**4/3 -80*x**2/9 + Rational(200, 9))*Dx**3 + \
|
152 |
+
(3*x**5 - 64*x**4/9 - 28*x**3/9 + 6*x**2 - x*Rational(20, 9) - Rational(20, 3))*Dx**4 + (-4*x**3 + 64*x**2/9 + \
|
153 |
+
x*Rational(8, 3))*Dx**5 + (x**4 - 64*x**3/27 - 4*x**2/3 + Rational(20, 9))*Dx**6, x, 0, [3, 3, 3, -3, -12, -24])
|
154 |
+
assert p * q == r
|
155 |
+
p = HolonomicFunction(Dx - 1, x, 0, [2])
|
156 |
+
q = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1])
|
157 |
+
r = HolonomicFunction(2 -2*Dx + Dx**2, x, 0, [0, 2])
|
158 |
+
assert p * q == r
|
159 |
+
q = HolonomicFunction(x*Dx**2 + 1 + 2*Dx, x, 0,[0, 1])
|
160 |
+
r = HolonomicFunction((x - 1) + (-2*x + 2)*Dx + x*Dx**2, x, 0, [0, 2])
|
161 |
+
assert p * q == r
|
162 |
+
p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 3])
|
163 |
+
q = HolonomicFunction(Dx**3 + 1, x, 0, [1, 2, 1])
|
164 |
+
r = HolonomicFunction(6*Dx + 3*Dx**2 + 2*Dx**3 - 3*Dx**4 + Dx**6, x, 0, [1, 5, 14, 17, 17, 2])
|
165 |
+
assert p * q == r
|
166 |
+
p = expr_to_holonomic(sin(x))
|
167 |
+
q = expr_to_holonomic(1/x, x0=1)
|
168 |
+
r = HolonomicFunction(x + 2*Dx + x*Dx**2, x, 1, [sin(1), -sin(1) + cos(1)])
|
169 |
+
assert p * q == r
|
170 |
+
p = expr_to_holonomic(sqrt(x))
|
171 |
+
q = expr_to_holonomic(sqrt(x**2-x))
|
172 |
+
r = (p * q).to_expr()
|
173 |
+
assert r == I*x*sqrt(-x + 1)
|
174 |
+
|
175 |
+
|
176 |
+
def test_HolonomicFunction_composition():
|
177 |
+
x = symbols('x')
|
178 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
179 |
+
p = HolonomicFunction(Dx-1, x).composition(x**2+x)
|
180 |
+
r = HolonomicFunction((-2*x - 1) + Dx, x)
|
181 |
+
assert p == r
|
182 |
+
p = HolonomicFunction(Dx**2+1, x).composition(x**5+x**2+1)
|
183 |
+
r = HolonomicFunction((125*x**12 + 150*x**9 + 60*x**6 + 8*x**3) + (-20*x**3 - 2)*Dx + \
|
184 |
+
(5*x**4 + 2*x)*Dx**2, x)
|
185 |
+
assert p == r
|
186 |
+
p = HolonomicFunction(Dx**2*x+x, x).composition(2*x**3+x**2+1)
|
187 |
+
r = HolonomicFunction((216*x**9 + 324*x**8 + 180*x**7 + 152*x**6 + 112*x**5 + \
|
188 |
+
36*x**4 + 4*x**3) + (24*x**4 + 16*x**3 + 3*x**2 - 6*x - 1)*Dx + (6*x**5 + 5*x**4 + \
|
189 |
+
x**3 + 3*x**2 + x)*Dx**2, x)
|
190 |
+
assert p == r
|
191 |
+
p = HolonomicFunction(Dx**2+1, x).composition(1-x**2)
|
192 |
+
r = HolonomicFunction((4*x**3) - Dx + x*Dx**2, x)
|
193 |
+
assert p == r
|
194 |
+
p = HolonomicFunction(Dx**2+1, x).composition(x - 2/(x**2 + 1))
|
195 |
+
r = HolonomicFunction((x**12 + 6*x**10 + 12*x**9 + 15*x**8 + 48*x**7 + 68*x**6 + \
|
196 |
+
72*x**5 + 111*x**4 + 112*x**3 + 54*x**2 + 12*x + 1) + (12*x**8 + 32*x**6 + \
|
197 |
+
24*x**4 - 4)*Dx + (x**12 + 6*x**10 + 4*x**9 + 15*x**8 + 16*x**7 + 20*x**6 + 24*x**5+ \
|
198 |
+
15*x**4 + 16*x**3 + 6*x**2 + 4*x + 1)*Dx**2, x)
|
199 |
+
assert p == r
|
200 |
+
|
201 |
+
|
202 |
+
def test_from_hyper():
|
203 |
+
x = symbols('x')
|
204 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
205 |
+
p = hyper([1, 1], [Rational(3, 2)], x**2/4)
|
206 |
+
q = HolonomicFunction((4*x) + (5*x**2 - 8)*Dx + (x**3 - 4*x)*Dx**2, x, 1, [2*sqrt(3)*pi/9, -4*sqrt(3)*pi/27 + Rational(4, 3)])
|
207 |
+
r = from_hyper(p)
|
208 |
+
assert r == q
|
209 |
+
p = from_hyper(hyper([1], [Rational(3, 2)], x**2/4))
|
210 |
+
q = HolonomicFunction(-x + (-x**2/2 + 2)*Dx + x*Dx**2, x)
|
211 |
+
# x0 = 1
|
212 |
+
y0 = '[sqrt(pi)*exp(1/4)*erf(1/2), -sqrt(pi)*exp(1/4)*erf(1/2)/2 + 1]'
|
213 |
+
assert sstr(p.y0) == y0
|
214 |
+
assert q.annihilator == p.annihilator
|
215 |
+
|
216 |
+
|
217 |
+
def test_from_meijerg():
|
218 |
+
x = symbols('x')
|
219 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
220 |
+
p = from_meijerg(meijerg(([], [Rational(3, 2)]), ([S.Half], [S.Half, 1]), x))
|
221 |
+
q = HolonomicFunction(x/2 - Rational(1, 4) + (-x**2 + x/4)*Dx + x**2*Dx**2 + x**3*Dx**3, x, 1, \
|
222 |
+
[1/sqrt(pi), 1/(2*sqrt(pi)), -1/(4*sqrt(pi))])
|
223 |
+
assert p == q
|
224 |
+
p = from_meijerg(meijerg(([], []), ([0], []), x))
|
225 |
+
q = HolonomicFunction(1 + Dx, x, 0, [1])
|
226 |
+
assert p == q
|
227 |
+
p = from_meijerg(meijerg(([1], []), ([S.Half], [0]), x))
|
228 |
+
q = HolonomicFunction((x + S.Half)*Dx + x*Dx**2, x, 1, [sqrt(pi)*erf(1), exp(-1)])
|
229 |
+
assert p == q
|
230 |
+
p = from_meijerg(meijerg(([0], [1]), ([0], []), 2*x**2))
|
231 |
+
q = HolonomicFunction((3*x**2 - 1)*Dx + x**3*Dx**2, x, 1, [-exp(Rational(-1, 2)) + 1, -exp(Rational(-1, 2))])
|
232 |
+
assert p == q
|
233 |
+
|
234 |
+
|
235 |
+
def test_to_Sequence():
|
236 |
+
x = symbols('x')
|
237 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
238 |
+
n = symbols('n', integer=True)
|
239 |
+
_, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), 'Sn')
|
240 |
+
p = HolonomicFunction(x**2*Dx**4 + x + Dx, x).to_sequence()
|
241 |
+
q = [(HolonomicSequence(1 + (n + 2)*Sn**2 + (n**4 + 6*n**3 + 11*n**2 + 6*n)*Sn**3), 0, 1)]
|
242 |
+
assert p == q
|
243 |
+
p = HolonomicFunction(x**2*Dx**4 + x**3 + Dx**2, x).to_sequence()
|
244 |
+
q = [(HolonomicSequence(1 + (n**4 + 14*n**3 + 72*n**2 + 163*n + 140)*Sn**5), 0, 0)]
|
245 |
+
assert p == q
|
246 |
+
p = HolonomicFunction(x**3*Dx**4 + 1 + Dx**2, x).to_sequence()
|
247 |
+
q = [(HolonomicSequence(1 + (n**4 - 2*n**3 - n**2 + 2*n)*Sn + (n**2 + 3*n + 2)*Sn**2), 0, 0)]
|
248 |
+
assert p == q
|
249 |
+
p = HolonomicFunction(3*x**3*Dx**4 + 2*x*Dx + x*Dx**3, x).to_sequence()
|
250 |
+
q = [(HolonomicSequence(2*n + (3*n**4 - 6*n**3 - 3*n**2 + 6*n)*Sn + (n**3 + 3*n**2 + 2*n)*Sn**2), 0, 1)]
|
251 |
+
assert p == q
|
252 |
+
|
253 |
+
|
254 |
+
def test_to_Sequence_Initial_Coniditons():
|
255 |
+
x = symbols('x')
|
256 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
257 |
+
n = symbols('n', integer=True)
|
258 |
+
_, Sn = RecurrenceOperators(QQ.old_poly_ring(n), 'Sn')
|
259 |
+
p = HolonomicFunction(Dx - 1, x, 0, [1]).to_sequence()
|
260 |
+
q = [(HolonomicSequence(-1 + (n + 1)*Sn, 1), 0)]
|
261 |
+
assert p == q
|
262 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]).to_sequence()
|
263 |
+
q = [(HolonomicSequence(1 + (n**2 + 3*n + 2)*Sn**2, [0, 1]), 0)]
|
264 |
+
assert p == q
|
265 |
+
p = HolonomicFunction(Dx**2 + 1 + x**3*Dx, x, 0, [2, 3]).to_sequence()
|
266 |
+
q = [(HolonomicSequence(n + Sn**2 + (n**2 + 7*n + 12)*Sn**4, [2, 3, -1, Rational(-1, 2), Rational(1, 12)]), 1)]
|
267 |
+
assert p == q
|
268 |
+
p = HolonomicFunction(x**3*Dx**5 + 1 + Dx, x).to_sequence()
|
269 |
+
q = [(HolonomicSequence(1 + (n + 1)*Sn + (n**5 - 5*n**3 + 4*n)*Sn**2), 0, 3)]
|
270 |
+
assert p == q
|
271 |
+
C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
|
272 |
+
p = expr_to_holonomic(log(1+x**2))
|
273 |
+
q = [(HolonomicSequence(n**2 + (n**2 + 2*n)*Sn**2, [0, 0, C_2]), 0, 1)]
|
274 |
+
assert p.to_sequence() == q
|
275 |
+
p = p.diff()
|
276 |
+
q = [(HolonomicSequence((n + 2) + (n + 2)*Sn**2, [C_0, 0]), 1, 0)]
|
277 |
+
assert p.to_sequence() == q
|
278 |
+
p = expr_to_holonomic(erf(x) + x).to_sequence()
|
279 |
+
q = [(HolonomicSequence((2*n**2 - 2*n) + (n**3 + 2*n**2 - n - 2)*Sn**2, [0, 1 + 2/sqrt(pi), 0, C_3]), 0, 2)]
|
280 |
+
assert p == q
|
281 |
+
|
282 |
+
def test_series():
|
283 |
+
x = symbols('x')
|
284 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
285 |
+
p = HolonomicFunction(Dx**2 + 2*x*Dx, x, 0, [0, 1]).series(n=10)
|
286 |
+
q = x - x**3/3 + x**5/10 - x**7/42 + x**9/216 + O(x**10)
|
287 |
+
assert p == q
|
288 |
+
p = HolonomicFunction(Dx - 1, x).composition(x**2, 0, [1]) # e^(x**2)
|
289 |
+
q = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]) # cos(x)
|
290 |
+
r = (p * q).series(n=10) # expansion of cos(x) * exp(x**2)
|
291 |
+
s = 1 + x**2/2 + x**4/24 - 31*x**6/720 - 179*x**8/8064 + O(x**10)
|
292 |
+
assert r == s
|
293 |
+
t = HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1]) # log(1 + x)
|
294 |
+
r = (p * t + q).series(n=10)
|
295 |
+
s = 1 + x - x**2 + 4*x**3/3 - 17*x**4/24 + 31*x**5/30 - 481*x**6/720 +\
|
296 |
+
71*x**7/105 - 20159*x**8/40320 + 379*x**9/840 + O(x**10)
|
297 |
+
assert r == s
|
298 |
+
p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
|
299 |
+
(4-6*x**3+2*x**4)*Dx**2, x, 0, [0, 1]).series(n=7)
|
300 |
+
q = x + x**3/6 - 3*x**4/16 + x**5/20 - 23*x**6/960 + O(x**7)
|
301 |
+
assert p == q
|
302 |
+
p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
|
303 |
+
(4-6*x**3+2*x**4)*Dx**2, x, 0, [1, 0]).series(n=7)
|
304 |
+
q = 1 - 3*x**2/4 - x**3/4 - 5*x**4/32 - 3*x**5/40 - 17*x**6/384 + O(x**7)
|
305 |
+
assert p == q
|
306 |
+
p = expr_to_holonomic(erf(x) + x).series(n=10)
|
307 |
+
C_3 = symbols('C_3')
|
308 |
+
q = (erf(x) + x).series(n=10)
|
309 |
+
assert p.subs(C_3, -2/(3*sqrt(pi))) == q
|
310 |
+
assert expr_to_holonomic(sqrt(x**3 + x)).series(n=10) == sqrt(x**3 + x).series(n=10)
|
311 |
+
assert expr_to_holonomic((2*x - 3*x**2)**Rational(1, 3)).series() == ((2*x - 3*x**2)**Rational(1, 3)).series()
|
312 |
+
assert expr_to_holonomic(sqrt(x**2-x)).series() == (sqrt(x**2-x)).series()
|
313 |
+
assert expr_to_holonomic(cos(x)**2/x**2, y0={-2: [1, 0, -1]}).series(n=10) == (cos(x)**2/x**2).series(n=10)
|
314 |
+
assert expr_to_holonomic(cos(x)**2/x**2, x0=1).series(n=10).together() == (cos(x)**2/x**2).series(n=10, x0=1).together()
|
315 |
+
assert expr_to_holonomic(cos(x-1)**2/(x-1)**2, x0=1, y0={-2: [1, 0, -1]}).series(n=10) \
|
316 |
+
== (cos(x-1)**2/(x-1)**2).series(x0=1, n=10)
|
317 |
+
|
318 |
+
def test_evalf_euler():
|
319 |
+
x = symbols('x')
|
320 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
321 |
+
|
322 |
+
# log(1+x)
|
323 |
+
p = HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1])
|
324 |
+
|
325 |
+
# path taken is a straight line from 0 to 1, on the real axis
|
326 |
+
r = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
|
327 |
+
s = '0.699525841805253' # approx. equal to log(2) i.e. 0.693147180559945
|
328 |
+
assert sstr(p.evalf(r, method='Euler')[-1]) == s
|
329 |
+
|
330 |
+
# path taken is a triangle 0-->1+i-->2
|
331 |
+
r = [0.1 + 0.1*I]
|
332 |
+
for i in range(9):
|
333 |
+
r.append(r[-1]+0.1+0.1*I)
|
334 |
+
for i in range(10):
|
335 |
+
r.append(r[-1]+0.1-0.1*I)
|
336 |
+
|
337 |
+
# close to the exact solution 1.09861228866811
|
338 |
+
# imaginary part also close to zero
|
339 |
+
s = '1.07530466271334 - 0.0251200594793912*I'
|
340 |
+
assert sstr(p.evalf(r, method='Euler')[-1]) == s
|
341 |
+
|
342 |
+
# sin(x)
|
343 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1])
|
344 |
+
s = '0.905546532085401 - 6.93889390390723e-18*I'
|
345 |
+
assert sstr(p.evalf(r, method='Euler')[-1]) == s
|
346 |
+
|
347 |
+
# computing sin(pi/2) using this method
|
348 |
+
# using a linear path from 0 to pi/2
|
349 |
+
r = [0.1]
|
350 |
+
for i in range(14):
|
351 |
+
r.append(r[-1] + 0.1)
|
352 |
+
r.append(pi/2)
|
353 |
+
s = '1.08016557252834' # close to 1.0 (exact solution)
|
354 |
+
assert sstr(p.evalf(r, method='Euler')[-1]) == s
|
355 |
+
|
356 |
+
# trying different path, a rectangle (0-->i-->pi/2 + i-->pi/2)
|
357 |
+
# computing the same value sin(pi/2) using different path
|
358 |
+
r = [0.1*I]
|
359 |
+
for i in range(9):
|
360 |
+
r.append(r[-1]+0.1*I)
|
361 |
+
for i in range(15):
|
362 |
+
r.append(r[-1]+0.1)
|
363 |
+
r.append(pi/2+I)
|
364 |
+
for i in range(10):
|
365 |
+
r.append(r[-1]-0.1*I)
|
366 |
+
|
367 |
+
# close to 1.0
|
368 |
+
s = '0.976882381836257 - 1.65557671738537e-16*I'
|
369 |
+
assert sstr(p.evalf(r, method='Euler')[-1]) == s
|
370 |
+
|
371 |
+
# cos(x)
|
372 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0])
|
373 |
+
# compute cos(pi) along 0-->pi
|
374 |
+
r = [0.05]
|
375 |
+
for i in range(61):
|
376 |
+
r.append(r[-1]+0.05)
|
377 |
+
r.append(pi)
|
378 |
+
# close to -1 (exact answer)
|
379 |
+
s = '-1.08140824719196'
|
380 |
+
assert sstr(p.evalf(r, method='Euler')[-1]) == s
|
381 |
+
|
382 |
+
# a rectangular path (0 -> i -> 2+i -> 2)
|
383 |
+
r = [0.1*I]
|
384 |
+
for i in range(9):
|
385 |
+
r.append(r[-1]+0.1*I)
|
386 |
+
for i in range(20):
|
387 |
+
r.append(r[-1]+0.1)
|
388 |
+
for i in range(10):
|
389 |
+
r.append(r[-1]-0.1*I)
|
390 |
+
|
391 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [1,1]).evalf(r, method='Euler')
|
392 |
+
s = '0.501421652861245 - 3.88578058618805e-16*I'
|
393 |
+
assert sstr(p[-1]) == s
|
394 |
+
|
395 |
+
def test_evalf_rk4():
|
396 |
+
x = symbols('x')
|
397 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
398 |
+
|
399 |
+
# log(1+x)
|
400 |
+
p = HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1])
|
401 |
+
|
402 |
+
# path taken is a straight line from 0 to 1, on the real axis
|
403 |
+
r = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
|
404 |
+
s = '0.693146363174626' # approx. equal to log(2) i.e. 0.693147180559945
|
405 |
+
assert sstr(p.evalf(r)[-1]) == s
|
406 |
+
|
407 |
+
# path taken is a triangle 0-->1+i-->2
|
408 |
+
r = [0.1 + 0.1*I]
|
409 |
+
for i in range(9):
|
410 |
+
r.append(r[-1]+0.1+0.1*I)
|
411 |
+
for i in range(10):
|
412 |
+
r.append(r[-1]+0.1-0.1*I)
|
413 |
+
|
414 |
+
# close to the exact solution 1.09861228866811
|
415 |
+
# imaginary part also close to zero
|
416 |
+
s = '1.098616 + 1.36083e-7*I'
|
417 |
+
assert sstr(p.evalf(r)[-1].n(7)) == s
|
418 |
+
|
419 |
+
# sin(x)
|
420 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1])
|
421 |
+
s = '0.90929463522785 + 1.52655665885959e-16*I'
|
422 |
+
assert sstr(p.evalf(r)[-1]) == s
|
423 |
+
|
424 |
+
# computing sin(pi/2) using this method
|
425 |
+
# using a linear path from 0 to pi/2
|
426 |
+
r = [0.1]
|
427 |
+
for i in range(14):
|
428 |
+
r.append(r[-1] + 0.1)
|
429 |
+
r.append(pi/2)
|
430 |
+
s = '0.999999895088917' # close to 1.0 (exact solution)
|
431 |
+
assert sstr(p.evalf(r)[-1]) == s
|
432 |
+
|
433 |
+
# trying different path, a rectangle (0-->i-->pi/2 + i-->pi/2)
|
434 |
+
# computing the same value sin(pi/2) using different path
|
435 |
+
r = [0.1*I]
|
436 |
+
for i in range(9):
|
437 |
+
r.append(r[-1]+0.1*I)
|
438 |
+
for i in range(15):
|
439 |
+
r.append(r[-1]+0.1)
|
440 |
+
r.append(pi/2+I)
|
441 |
+
for i in range(10):
|
442 |
+
r.append(r[-1]-0.1*I)
|
443 |
+
|
444 |
+
# close to 1.0
|
445 |
+
s = '1.00000003415141 + 6.11940487991086e-16*I'
|
446 |
+
assert sstr(p.evalf(r)[-1]) == s
|
447 |
+
|
448 |
+
# cos(x)
|
449 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0])
|
450 |
+
# compute cos(pi) along 0-->pi
|
451 |
+
r = [0.05]
|
452 |
+
for i in range(61):
|
453 |
+
r.append(r[-1]+0.05)
|
454 |
+
r.append(pi)
|
455 |
+
# close to -1 (exact answer)
|
456 |
+
s = '-0.999999993238714'
|
457 |
+
assert sstr(p.evalf(r)[-1]) == s
|
458 |
+
|
459 |
+
# a rectangular path (0 -> i -> 2+i -> 2)
|
460 |
+
r = [0.1*I]
|
461 |
+
for i in range(9):
|
462 |
+
r.append(r[-1]+0.1*I)
|
463 |
+
for i in range(20):
|
464 |
+
r.append(r[-1]+0.1)
|
465 |
+
for i in range(10):
|
466 |
+
r.append(r[-1]-0.1*I)
|
467 |
+
|
468 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [1,1]).evalf(r)
|
469 |
+
s = '0.493152791638442 - 1.41553435639707e-15*I'
|
470 |
+
assert sstr(p[-1]) == s
|
471 |
+
|
472 |
+
|
473 |
+
def test_expr_to_holonomic():
|
474 |
+
x = symbols('x')
|
475 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
476 |
+
p = expr_to_holonomic((sin(x)/x)**2)
|
477 |
+
q = HolonomicFunction(8*x + (4*x**2 + 6)*Dx + 6*x*Dx**2 + x**2*Dx**3, x, 0, \
|
478 |
+
[1, 0, Rational(-2, 3)])
|
479 |
+
assert p == q
|
480 |
+
p = expr_to_holonomic(1/(1+x**2)**2)
|
481 |
+
q = HolonomicFunction(4*x + (x**2 + 1)*Dx, x, 0, [1])
|
482 |
+
assert p == q
|
483 |
+
p = expr_to_holonomic(exp(x)*sin(x)+x*log(1+x))
|
484 |
+
q = HolonomicFunction((2*x**3 + 10*x**2 + 20*x + 18) + (-2*x**4 - 10*x**3 - 20*x**2 \
|
485 |
+
- 18*x)*Dx + (2*x**5 + 6*x**4 + 7*x**3 + 8*x**2 + 10*x - 4)*Dx**2 + \
|
486 |
+
(-2*x**5 - 5*x**4 - 2*x**3 + 2*x**2 - x + 4)*Dx**3 + (x**5 + 2*x**4 - x**3 - \
|
487 |
+
7*x**2/2 + x + Rational(5, 2))*Dx**4, x, 0, [0, 1, 4, -1])
|
488 |
+
assert p == q
|
489 |
+
p = expr_to_holonomic(x*exp(x)+cos(x)+1)
|
490 |
+
q = HolonomicFunction((-x - 3)*Dx + (x + 2)*Dx**2 + (-x - 3)*Dx**3 + (x + 2)*Dx**4, x, \
|
491 |
+
0, [2, 1, 1, 3])
|
492 |
+
assert p == q
|
493 |
+
assert (x*exp(x)+cos(x)+1).series(n=10) == p.series(n=10)
|
494 |
+
p = expr_to_holonomic(log(1 + x)**2 + 1)
|
495 |
+
q = HolonomicFunction(Dx + (3*x + 3)*Dx**2 + (x**2 + 2*x + 1)*Dx**3, x, 0, [1, 0, 2])
|
496 |
+
assert p == q
|
497 |
+
p = expr_to_holonomic(erf(x)**2 + x)
|
498 |
+
q = HolonomicFunction((8*x**4 - 2*x**2 + 2)*Dx**2 + (6*x**3 - x/2)*Dx**3 + \
|
499 |
+
(x**2+ Rational(1, 4))*Dx**4, x, 0, [0, 1, 8/pi, 0])
|
500 |
+
assert p == q
|
501 |
+
p = expr_to_holonomic(cosh(x)*x)
|
502 |
+
q = HolonomicFunction((-x**2 + 2) -2*x*Dx + x**2*Dx**2, x, 0, [0, 1])
|
503 |
+
assert p == q
|
504 |
+
p = expr_to_holonomic(besselj(2, x))
|
505 |
+
q = HolonomicFunction((x**2 - 4) + x*Dx + x**2*Dx**2, x, 0, [0, 0])
|
506 |
+
assert p == q
|
507 |
+
p = expr_to_holonomic(besselj(0, x) + exp(x))
|
508 |
+
q = HolonomicFunction((-x**2 - x/2 + S.Half) + (x**2 - x/2 - Rational(3, 2))*Dx + (-x**2 + x/2 + 1)*Dx**2 +\
|
509 |
+
(x**2 + x/2)*Dx**3, x, 0, [2, 1, S.Half])
|
510 |
+
assert p == q
|
511 |
+
p = expr_to_holonomic(sin(x)**2/x)
|
512 |
+
q = HolonomicFunction(4 + 4*x*Dx + 3*Dx**2 + x*Dx**3, x, 0, [0, 1, 0])
|
513 |
+
assert p == q
|
514 |
+
p = expr_to_holonomic(sin(x)**2/x, x0=2)
|
515 |
+
q = HolonomicFunction((4) + (4*x)*Dx + (3)*Dx**2 + (x)*Dx**3, x, 2, [sin(2)**2/2,
|
516 |
+
sin(2)*cos(2) - sin(2)**2/4, -3*sin(2)**2/4 + cos(2)**2 - sin(2)*cos(2)])
|
517 |
+
assert p == q
|
518 |
+
p = expr_to_holonomic(log(x)/2 - Ci(2*x)/2 + Ci(2)/2)
|
519 |
+
q = HolonomicFunction(4*Dx + 4*x*Dx**2 + 3*Dx**3 + x*Dx**4, x, 0, \
|
520 |
+
[-log(2)/2 - EulerGamma/2 + Ci(2)/2, 0, 1, 0])
|
521 |
+
assert p == q
|
522 |
+
p = p.to_expr()
|
523 |
+
q = log(x)/2 - Ci(2*x)/2 + Ci(2)/2
|
524 |
+
assert p == q
|
525 |
+
p = expr_to_holonomic(x**S.Half, x0=1)
|
526 |
+
q = HolonomicFunction(x*Dx - S.Half, x, 1, [1])
|
527 |
+
assert p == q
|
528 |
+
p = expr_to_holonomic(sqrt(1 + x**2))
|
529 |
+
q = HolonomicFunction((-x) + (x**2 + 1)*Dx, x, 0, [1])
|
530 |
+
assert p == q
|
531 |
+
assert (expr_to_holonomic(sqrt(x) + sqrt(2*x)).to_expr()-\
|
532 |
+
(sqrt(x) + sqrt(2*x))).simplify() == 0
|
533 |
+
assert expr_to_holonomic(3*x+2*sqrt(x)).to_expr() == 3*x+2*sqrt(x)
|
534 |
+
p = expr_to_holonomic((x**4+x**3+5*x**2+3*x+2)/x**2, lenics=3)
|
535 |
+
q = HolonomicFunction((-2*x**4 - x**3 + 3*x + 4) + (x**5 + x**4 + 5*x**3 + 3*x**2 + \
|
536 |
+
2*x)*Dx, x, 0, {-2: [2, 3, 5]})
|
537 |
+
assert p == q
|
538 |
+
p = expr_to_holonomic(1/(x-1)**2, lenics=3, x0=1)
|
539 |
+
q = HolonomicFunction((2) + (x - 1)*Dx, x, 1, {-2: [1, 0, 0]})
|
540 |
+
assert p == q
|
541 |
+
a = symbols("a")
|
542 |
+
p = expr_to_holonomic(sqrt(a*x), x=x)
|
543 |
+
assert p.to_expr() == sqrt(a)*sqrt(x)
|
544 |
+
|
545 |
+
def test_to_hyper():
|
546 |
+
x = symbols('x')
|
547 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
548 |
+
p = HolonomicFunction(Dx - 2, x, 0, [3]).to_hyper()
|
549 |
+
q = 3 * hyper([], [], 2*x)
|
550 |
+
assert p == q
|
551 |
+
p = hyperexpand(HolonomicFunction((1 + x) * Dx - 3, x, 0, [2]).to_hyper()).expand()
|
552 |
+
q = 2*x**3 + 6*x**2 + 6*x + 2
|
553 |
+
assert p == q
|
554 |
+
p = HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1]).to_hyper()
|
555 |
+
q = -x**2*hyper((2, 2, 1), (3, 2), -x)/2 + x
|
556 |
+
assert p == q
|
557 |
+
p = HolonomicFunction(2*x*Dx + Dx**2, x, 0, [0, 2/sqrt(pi)]).to_hyper()
|
558 |
+
q = 2*x*hyper((S.Half,), (Rational(3, 2),), -x**2)/sqrt(pi)
|
559 |
+
assert p == q
|
560 |
+
p = hyperexpand(HolonomicFunction(2*x*Dx + Dx**2, x, 0, [1, -2/sqrt(pi)]).to_hyper())
|
561 |
+
q = erfc(x)
|
562 |
+
assert p.rewrite(erfc) == q
|
563 |
+
p = hyperexpand(HolonomicFunction((x**2 - 1) + x*Dx + x**2*Dx**2,
|
564 |
+
x, 0, [0, S.Half]).to_hyper())
|
565 |
+
q = besselj(1, x)
|
566 |
+
assert p == q
|
567 |
+
p = hyperexpand(HolonomicFunction(x*Dx**2 + Dx + x, x, 0, [1, 0]).to_hyper())
|
568 |
+
q = besselj(0, x)
|
569 |
+
assert p == q
|
570 |
+
|
571 |
+
def test_to_expr():
|
572 |
+
x = symbols('x')
|
573 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
574 |
+
p = HolonomicFunction(Dx - 1, x, 0, [1]).to_expr()
|
575 |
+
q = exp(x)
|
576 |
+
assert p == q
|
577 |
+
p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).to_expr()
|
578 |
+
q = cos(x)
|
579 |
+
assert p == q
|
580 |
+
p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0]).to_expr()
|
581 |
+
q = cosh(x)
|
582 |
+
assert p == q
|
583 |
+
p = HolonomicFunction(2 + (4*x - 1)*Dx + \
|
584 |
+
(x**2 - x)*Dx**2, x, 0, [1, 2]).to_expr().expand()
|
585 |
+
q = 1/(x**2 - 2*x + 1)
|
586 |
+
assert p == q
|
587 |
+
p = expr_to_holonomic(sin(x)**2/x).integrate((x, 0, x)).to_expr()
|
588 |
+
q = (sin(x)**2/x).integrate((x, 0, x))
|
589 |
+
assert p == q
|
590 |
+
C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
|
591 |
+
p = expr_to_holonomic(log(1+x**2)).to_expr()
|
592 |
+
q = C_2*log(x**2 + 1)
|
593 |
+
assert p == q
|
594 |
+
p = expr_to_holonomic(log(1+x**2)).diff().to_expr()
|
595 |
+
q = C_0*x/(x**2 + 1)
|
596 |
+
assert p == q
|
597 |
+
p = expr_to_holonomic(erf(x) + x).to_expr()
|
598 |
+
q = 3*C_3*x - 3*sqrt(pi)*C_3*erf(x)/2 + x + 2*x/sqrt(pi)
|
599 |
+
assert p == q
|
600 |
+
p = expr_to_holonomic(sqrt(x), x0=1).to_expr()
|
601 |
+
assert p == sqrt(x)
|
602 |
+
assert expr_to_holonomic(sqrt(x)).to_expr() == sqrt(x)
|
603 |
+
p = expr_to_holonomic(sqrt(1 + x**2)).to_expr()
|
604 |
+
assert p == sqrt(1+x**2)
|
605 |
+
p = expr_to_holonomic((2*x**2 + 1)**Rational(2, 3)).to_expr()
|
606 |
+
assert p == (2*x**2 + 1)**Rational(2, 3)
|
607 |
+
p = expr_to_holonomic(sqrt(-x**2+2*x)).to_expr()
|
608 |
+
assert p == sqrt(x)*sqrt(-x + 2)
|
609 |
+
p = expr_to_holonomic((-2*x**3+7*x)**Rational(2, 3)).to_expr()
|
610 |
+
q = x**Rational(2, 3)*(-2*x**2 + 7)**Rational(2, 3)
|
611 |
+
assert p == q
|
612 |
+
p = from_hyper(hyper((-2, -3), (S.Half, ), x))
|
613 |
+
s = hyperexpand(hyper((-2, -3), (S.Half, ), x))
|
614 |
+
D_0 = Symbol('D_0')
|
615 |
+
C_0 = Symbol('C_0')
|
616 |
+
assert (p.to_expr().subs({C_0:1, D_0:0}) - s).simplify() == 0
|
617 |
+
p.y0 = {0: [1], S.Half: [0]}
|
618 |
+
assert p.to_expr() == s
|
619 |
+
assert expr_to_holonomic(x**5).to_expr() == x**5
|
620 |
+
assert expr_to_holonomic(2*x**3-3*x**2).to_expr().expand() == \
|
621 |
+
2*x**3-3*x**2
|
622 |
+
a = symbols("a")
|
623 |
+
p = (expr_to_holonomic(1.4*x)*expr_to_holonomic(a*x, x)).to_expr()
|
624 |
+
q = 1.4*a*x**2
|
625 |
+
assert p == q
|
626 |
+
p = (expr_to_holonomic(1.4*x)+expr_to_holonomic(a*x, x)).to_expr()
|
627 |
+
q = x*(a + 1.4)
|
628 |
+
assert p == q
|
629 |
+
p = (expr_to_holonomic(1.4*x)+expr_to_holonomic(x)).to_expr()
|
630 |
+
assert p == 2.4*x
|
631 |
+
|
632 |
+
|
633 |
+
def test_integrate():
|
634 |
+
x = symbols('x')
|
635 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
636 |
+
p = expr_to_holonomic(sin(x)**2/x, x0=1).integrate((x, 2, 3))
|
637 |
+
q = '0.166270406994788'
|
638 |
+
assert sstr(p) == q
|
639 |
+
p = expr_to_holonomic(sin(x)).integrate((x, 0, x)).to_expr()
|
640 |
+
q = 1 - cos(x)
|
641 |
+
assert p == q
|
642 |
+
p = expr_to_holonomic(sin(x)).integrate((x, 0, 3))
|
643 |
+
q = 1 - cos(3)
|
644 |
+
assert p == q
|
645 |
+
p = expr_to_holonomic(sin(x)/x, x0=1).integrate((x, 1, 2))
|
646 |
+
q = '0.659329913368450'
|
647 |
+
assert sstr(p) == q
|
648 |
+
p = expr_to_holonomic(sin(x)**2/x, x0=1).integrate((x, 1, 0))
|
649 |
+
q = '-0.423690480850035'
|
650 |
+
assert sstr(p) == q
|
651 |
+
p = expr_to_holonomic(sin(x)/x)
|
652 |
+
assert p.integrate(x).to_expr() == Si(x)
|
653 |
+
assert p.integrate((x, 0, 2)) == Si(2)
|
654 |
+
p = expr_to_holonomic(sin(x)**2/x)
|
655 |
+
q = p.to_expr()
|
656 |
+
assert p.integrate(x).to_expr() == q.integrate((x, 0, x))
|
657 |
+
assert p.integrate((x, 0, 1)) == q.integrate((x, 0, 1))
|
658 |
+
assert expr_to_holonomic(1/x, x0=1).integrate(x).to_expr() == log(x)
|
659 |
+
p = expr_to_holonomic((x + 1)**3*exp(-x), x0=-1).integrate(x).to_expr()
|
660 |
+
q = (-x**3 - 6*x**2 - 15*x + 6*exp(x + 1) - 16)*exp(-x)
|
661 |
+
assert p == q
|
662 |
+
p = expr_to_holonomic(cos(x)**2/x**2, y0={-2: [1, 0, -1]}).integrate(x).to_expr()
|
663 |
+
q = -Si(2*x) - cos(x)**2/x
|
664 |
+
assert p == q
|
665 |
+
p = expr_to_holonomic(sqrt(x**2+x)).integrate(x).to_expr()
|
666 |
+
q = (x**Rational(3, 2)*(2*x**2 + 3*x + 1) - x*sqrt(x + 1)*asinh(sqrt(x)))/(4*x*sqrt(x + 1))
|
667 |
+
assert p == q
|
668 |
+
p = expr_to_holonomic(sqrt(x**2+1)).integrate(x).to_expr()
|
669 |
+
q = (sqrt(x**2+1)).integrate(x)
|
670 |
+
assert (p-q).simplify() == 0
|
671 |
+
p = expr_to_holonomic(1/x**2, y0={-2:[1, 0, 0]})
|
672 |
+
r = expr_to_holonomic(1/x**2, lenics=3)
|
673 |
+
assert p == r
|
674 |
+
q = expr_to_holonomic(cos(x)**2)
|
675 |
+
assert (r*q).integrate(x).to_expr() == -Si(2*x) - cos(x)**2/x
|
676 |
+
|
677 |
+
|
678 |
+
def test_diff():
|
679 |
+
x, y = symbols('x, y')
|
680 |
+
R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
|
681 |
+
p = HolonomicFunction(x*Dx**2 + 1, x, 0, [0, 1])
|
682 |
+
assert p.diff().to_expr() == p.to_expr().diff().simplify()
|
683 |
+
p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0])
|
684 |
+
assert p.diff(x, 2).to_expr() == p.to_expr()
|
685 |
+
p = expr_to_holonomic(Si(x))
|
686 |
+
assert p.diff().to_expr() == sin(x)/x
|
687 |
+
assert p.diff(y) == 0
|
688 |
+
C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
|
689 |
+
q = Si(x)
|
690 |
+
assert p.diff(x).to_expr() == q.diff()
|
691 |
+
assert p.diff(x, 2).to_expr().subs(C_0, Rational(-1, 3)).cancel() == q.diff(x, 2).cancel()
|
692 |
+
assert p.diff(x, 3).series().subs({C_3: Rational(-1, 3), C_0: 0}) == q.diff(x, 3).series()
|
693 |
+
|
694 |
+
|
695 |
+
def test_extended_domain_in_expr_to_holonomic():
|
696 |
+
x = symbols('x')
|
697 |
+
p = expr_to_holonomic(1.2*cos(3.1*x))
|
698 |
+
assert p.to_expr() == 1.2*cos(3.1*x)
|
699 |
+
assert sstr(p.integrate(x).to_expr()) == '0.387096774193548*sin(3.1*x)'
|
700 |
+
_, Dx = DifferentialOperators(RR.old_poly_ring(x), 'Dx')
|
701 |
+
p = expr_to_holonomic(1.1329138213*x)
|
702 |
+
q = HolonomicFunction((-1.1329138213) + (1.1329138213*x)*Dx, x, 0, {1: [1.1329138213]})
|
703 |
+
assert p == q
|
704 |
+
assert p.to_expr() == 1.1329138213*x
|
705 |
+
assert sstr(p.integrate((x, 1, 2))) == sstr((1.1329138213*x).integrate((x, 1, 2)))
|
706 |
+
y, z = symbols('y, z')
|
707 |
+
p = expr_to_holonomic(sin(x*y*z), x=x)
|
708 |
+
assert p.to_expr() == sin(x*y*z)
|
709 |
+
assert p.integrate(x).to_expr() == (-cos(x*y*z) + 1)/(y*z)
|
710 |
+
p = expr_to_holonomic(sin(x*y + z), x=x).integrate(x).to_expr()
|
711 |
+
q = (cos(z) - cos(x*y + z))/y
|
712 |
+
assert p == q
|
713 |
+
a = symbols('a')
|
714 |
+
p = expr_to_holonomic(a*x, x)
|
715 |
+
assert p.to_expr() == a*x
|
716 |
+
assert p.integrate(x).to_expr() == a*x**2/2
|
717 |
+
D_2, C_1 = symbols("D_2, C_1")
|
718 |
+
p = expr_to_holonomic(x) + expr_to_holonomic(1.2*cos(x))
|
719 |
+
p = p.to_expr().subs(D_2, 0)
|
720 |
+
assert p - x - 1.2*cos(1.0*x) == 0
|
721 |
+
p = expr_to_holonomic(x) * expr_to_holonomic(1.2*cos(x))
|
722 |
+
p = p.to_expr().subs(C_1, 0)
|
723 |
+
assert p - 1.2*x*cos(1.0*x) == 0
|
724 |
+
|
725 |
+
|
726 |
+
def test_to_meijerg():
|
727 |
+
x = symbols('x')
|
728 |
+
assert hyperexpand(expr_to_holonomic(sin(x)).to_meijerg()) == sin(x)
|
729 |
+
assert hyperexpand(expr_to_holonomic(cos(x)).to_meijerg()) == cos(x)
|
730 |
+
assert hyperexpand(expr_to_holonomic(exp(x)).to_meijerg()) == exp(x)
|
731 |
+
assert hyperexpand(expr_to_holonomic(log(x)).to_meijerg()).simplify() == log(x)
|
732 |
+
assert expr_to_holonomic(4*x**2/3 + 7).to_meijerg() == 4*x**2/3 + 7
|
733 |
+
assert hyperexpand(expr_to_holonomic(besselj(2, x), lenics=3).to_meijerg()) == besselj(2, x)
|
734 |
+
p = hyper((Rational(-1, 2), -3), (), x)
|
735 |
+
assert from_hyper(p).to_meijerg() == hyperexpand(p)
|
736 |
+
p = hyper((S.One, S(3)), (S(2), ), x)
|
737 |
+
assert (hyperexpand(from_hyper(p).to_meijerg()) - hyperexpand(p)).expand() == 0
|
738 |
+
p = from_hyper(hyper((-2, -3), (S.Half, ), x))
|
739 |
+
s = hyperexpand(hyper((-2, -3), (S.Half, ), x))
|
740 |
+
C_0 = Symbol('C_0')
|
741 |
+
C_1 = Symbol('C_1')
|
742 |
+
D_0 = Symbol('D_0')
|
743 |
+
assert (hyperexpand(p.to_meijerg()).subs({C_0:1, D_0:0}) - s).simplify() == 0
|
744 |
+
p.y0 = {0: [1], S.Half: [0]}
|
745 |
+
assert (hyperexpand(p.to_meijerg()) - s).simplify() == 0
|
746 |
+
p = expr_to_holonomic(besselj(S.Half, x), initcond=False)
|
747 |
+
assert (p.to_expr() - (D_0*sin(x) + C_0*cos(x) + C_1*sin(x))/sqrt(x)).simplify() == 0
|
748 |
+
p = expr_to_holonomic(besselj(S.Half, x), y0={Rational(-1, 2): [sqrt(2)/sqrt(pi), sqrt(2)/sqrt(pi)]})
|
749 |
+
assert (p.to_expr() - besselj(S.Half, x) - besselj(Rational(-1, 2), x)).simplify() == 0
|
750 |
+
|
751 |
+
|
752 |
+
def test_gaussian():
|
753 |
+
mu, x = symbols("mu x")
|
754 |
+
sd = symbols("sd", positive=True)
|
755 |
+
Q = QQ[mu, sd].get_field()
|
756 |
+
e = sqrt(2)*exp(-(-mu + x)**2/(2*sd**2))/(2*sqrt(pi)*sd)
|
757 |
+
h1 = expr_to_holonomic(e, x, domain=Q)
|
758 |
+
|
759 |
+
_, Dx = DifferentialOperators(Q.old_poly_ring(x), 'Dx')
|
760 |
+
h2 = HolonomicFunction((-mu/sd**2 + x/sd**2) + (1)*Dx, x)
|
761 |
+
|
762 |
+
assert h1 == h2
|
763 |
+
|
764 |
+
|
765 |
+
def test_beta():
|
766 |
+
a, b, x = symbols("a b x", positive=True)
|
767 |
+
e = x**(a - 1)*(-x + 1)**(b - 1)/beta(a, b)
|
768 |
+
Q = QQ[a, b].get_field()
|
769 |
+
h1 = expr_to_holonomic(e, x, domain=Q)
|
770 |
+
|
771 |
+
_, Dx = DifferentialOperators(Q.old_poly_ring(x), 'Dx')
|
772 |
+
h2 = HolonomicFunction((a + x*(-a - b + 2) - 1) + (x**2 - x)*Dx, x)
|
773 |
+
|
774 |
+
assert h1 == h2
|
775 |
+
|
776 |
+
|
777 |
+
def test_gamma():
|
778 |
+
a, b, x = symbols("a b x", positive=True)
|
779 |
+
e = b**(-a)*x**(a - 1)*exp(-x/b)/gamma(a)
|
780 |
+
Q = QQ[a, b].get_field()
|
781 |
+
h1 = expr_to_holonomic(e, x, domain=Q)
|
782 |
+
|
783 |
+
_, Dx = DifferentialOperators(Q.old_poly_ring(x), 'Dx')
|
784 |
+
h2 = HolonomicFunction((-a + 1 + x/b) + (x)*Dx, x)
|
785 |
+
|
786 |
+
assert h1 == h2
|
787 |
+
|
788 |
+
|
789 |
+
def test_symbolic_power():
|
790 |
+
x, n = symbols("x n")
|
791 |
+
Q = QQ[n].get_field()
|
792 |
+
_, Dx = DifferentialOperators(Q.old_poly_ring(x), 'Dx')
|
793 |
+
h1 = HolonomicFunction((-1) + (x)*Dx, x) ** -n
|
794 |
+
h2 = HolonomicFunction((n) + (x)*Dx, x)
|
795 |
+
|
796 |
+
assert h1 == h2
|
797 |
+
|
798 |
+
|
799 |
+
def test_negative_power():
|
800 |
+
x = symbols("x")
|
801 |
+
_, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
802 |
+
h1 = HolonomicFunction((-1) + (x)*Dx, x) ** -2
|
803 |
+
h2 = HolonomicFunction((2) + (x)*Dx, x)
|
804 |
+
|
805 |
+
assert h1 == h2
|
806 |
+
|
807 |
+
|
808 |
+
def test_expr_in_power():
|
809 |
+
x, n = symbols("x n")
|
810 |
+
Q = QQ[n].get_field()
|
811 |
+
_, Dx = DifferentialOperators(Q.old_poly_ring(x), 'Dx')
|
812 |
+
h1 = HolonomicFunction((-1) + (x)*Dx, x) ** (n - 3)
|
813 |
+
h2 = HolonomicFunction((-n + 3) + (x)*Dx, x)
|
814 |
+
|
815 |
+
assert h1 == h2
|
816 |
+
|
817 |
+
def test_DifferentialOperatorEqPoly():
|
818 |
+
x = symbols('x', integer=True)
|
819 |
+
R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx')
|
820 |
+
do = DifferentialOperator([x**2, R.base.zero, R.base.zero], R)
|
821 |
+
do2 = DifferentialOperator([x**2, 1, x], R)
|
822 |
+
assert not do == do2
|
823 |
+
|
824 |
+
# polynomial comparison issue, see https://github.com/sympy/sympy/pull/15799
|
825 |
+
# should work once that is solved
|
826 |
+
# p = do.listofpoly[0]
|
827 |
+
# assert do == p
|
828 |
+
|
829 |
+
p2 = do2.listofpoly[0]
|
830 |
+
assert not do2 == p2
|
env-llmeval/lib/python3.10/site-packages/sympy/holonomic/tests/test_recurrence.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.holonomic.recurrence import RecurrenceOperators, RecurrenceOperator
|
2 |
+
from sympy.core.symbol import symbols
|
3 |
+
from sympy.polys.domains.rationalfield import QQ
|
4 |
+
|
5 |
+
def test_RecurrenceOperator():
|
6 |
+
n = symbols('n', integer=True)
|
7 |
+
R, Sn = RecurrenceOperators(QQ.old_poly_ring(n), 'Sn')
|
8 |
+
assert Sn*n == (n + 1)*Sn
|
9 |
+
assert Sn*n**2 == (n**2+1+2*n)*Sn
|
10 |
+
assert Sn**2*n**2 == (n**2 + 4*n + 4)*Sn**2
|
11 |
+
p = (Sn**3*n**2 + Sn*n)**2
|
12 |
+
q = (n**2 + 3*n + 2)*Sn**2 + (2*n**3 + 19*n**2 + 57*n + 52)*Sn**4 + (n**4 + 18*n**3 + \
|
13 |
+
117*n**2 + 324*n + 324)*Sn**6
|
14 |
+
assert p == q
|
15 |
+
|
16 |
+
def test_RecurrenceOperatorEqPoly():
|
17 |
+
n = symbols('n', integer=True)
|
18 |
+
R, Sn = RecurrenceOperators(QQ.old_poly_ring(n), 'Sn')
|
19 |
+
rr = RecurrenceOperator([n**2, 0, 0], R)
|
20 |
+
rr2 = RecurrenceOperator([n**2, 1, n], R)
|
21 |
+
assert not rr == rr2
|
22 |
+
|
23 |
+
# polynomial comparison issue, see https://github.com/sympy/sympy/pull/15799
|
24 |
+
# should work once that is solved
|
25 |
+
# d = rr.listofpoly[0]
|
26 |
+
# assert rr == d
|
27 |
+
|
28 |
+
d2 = rr2.listofpoly[0]
|
29 |
+
assert not rr2 == d2
|
env-llmeval/lib/python3.10/site-packages/sympy/ntheory/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (2.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (596 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/experimental_lambdify.cpython-310.pyc
ADDED
Binary file (14.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/plot.cpython-310.pyc
ADDED
Binary file (78.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/plot_implicit.cpython-310.pyc
ADDED
Binary file (12.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/__pycache__/textplot.cpython-310.pyc
ADDED
Binary file (4.64 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (752 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_arithmetic.cpython-310.pyc
ADDED
Binary file (9.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_membership.cpython-310.pyc
ADDED
Binary file (3.06 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/lib_interval.cpython-310.pyc
ADDED
Binary file (9.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/interval_arithmetic.py
ADDED
@@ -0,0 +1,412 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Interval Arithmetic for plotting.
|
3 |
+
This module does not implement interval arithmetic accurately and
|
4 |
+
hence cannot be used for purposes other than plotting. If you want
|
5 |
+
to use interval arithmetic, use mpmath's interval arithmetic.
|
6 |
+
|
7 |
+
The module implements interval arithmetic using numpy and
|
8 |
+
python floating points. The rounding up and down is not handled
|
9 |
+
and hence this is not an accurate implementation of interval
|
10 |
+
arithmetic.
|
11 |
+
|
12 |
+
The module uses numpy for speed which cannot be achieved with mpmath.
|
13 |
+
"""
|
14 |
+
|
15 |
+
# Q: Why use numpy? Why not simply use mpmath's interval arithmetic?
|
16 |
+
# A: mpmath's interval arithmetic simulates a floating point unit
|
17 |
+
# and hence is slow, while numpy evaluations are orders of magnitude
|
18 |
+
# faster.
|
19 |
+
|
20 |
+
# Q: Why create a separate class for intervals? Why not use SymPy's
|
21 |
+
# Interval Sets?
|
22 |
+
# A: The functionalities that will be required for plotting is quite
|
23 |
+
# different from what Interval Sets implement.
|
24 |
+
|
25 |
+
# Q: Why is rounding up and down according to IEEE754 not handled?
|
26 |
+
# A: It is not possible to do it in both numpy and python. An external
|
27 |
+
# library has to used, which defeats the whole purpose i.e., speed. Also
|
28 |
+
# rounding is handled for very few functions in those libraries.
|
29 |
+
|
30 |
+
# Q Will my plots be affected?
|
31 |
+
# A It will not affect most of the plots. The interval arithmetic
|
32 |
+
# module based suffers the same problems as that of floating point
|
33 |
+
# arithmetic.
|
34 |
+
|
35 |
+
from sympy.core.logic import fuzzy_and
|
36 |
+
from sympy.simplify.simplify import nsimplify
|
37 |
+
|
38 |
+
from .interval_membership import intervalMembership
|
39 |
+
|
40 |
+
|
41 |
+
class interval:
|
42 |
+
""" Represents an interval containing floating points as start and
|
43 |
+
end of the interval
|
44 |
+
The is_valid variable tracks whether the interval obtained as the
|
45 |
+
result of the function is in the domain and is continuous.
|
46 |
+
- True: Represents the interval result of a function is continuous and
|
47 |
+
in the domain of the function.
|
48 |
+
- False: The interval argument of the function was not in the domain of
|
49 |
+
the function, hence the is_valid of the result interval is False
|
50 |
+
- None: The function was not continuous over the interval or
|
51 |
+
the function's argument interval is partly in the domain of the
|
52 |
+
function
|
53 |
+
|
54 |
+
A comparison between an interval and a real number, or a
|
55 |
+
comparison between two intervals may return ``intervalMembership``
|
56 |
+
of two 3-valued logic values.
|
57 |
+
"""
|
58 |
+
|
59 |
+
def __init__(self, *args, is_valid=True, **kwargs):
|
60 |
+
self.is_valid = is_valid
|
61 |
+
if len(args) == 1:
|
62 |
+
if isinstance(args[0], interval):
|
63 |
+
self.start, self.end = args[0].start, args[0].end
|
64 |
+
else:
|
65 |
+
self.start = float(args[0])
|
66 |
+
self.end = float(args[0])
|
67 |
+
elif len(args) == 2:
|
68 |
+
if args[0] < args[1]:
|
69 |
+
self.start = float(args[0])
|
70 |
+
self.end = float(args[1])
|
71 |
+
else:
|
72 |
+
self.start = float(args[1])
|
73 |
+
self.end = float(args[0])
|
74 |
+
|
75 |
+
else:
|
76 |
+
raise ValueError("interval takes a maximum of two float values "
|
77 |
+
"as arguments")
|
78 |
+
|
79 |
+
@property
|
80 |
+
def mid(self):
|
81 |
+
return (self.start + self.end) / 2.0
|
82 |
+
|
83 |
+
@property
|
84 |
+
def width(self):
|
85 |
+
return self.end - self.start
|
86 |
+
|
87 |
+
def __repr__(self):
|
88 |
+
return "interval(%f, %f)" % (self.start, self.end)
|
89 |
+
|
90 |
+
def __str__(self):
|
91 |
+
return "[%f, %f]" % (self.start, self.end)
|
92 |
+
|
93 |
+
def __lt__(self, other):
|
94 |
+
if isinstance(other, (int, float)):
|
95 |
+
if self.end < other:
|
96 |
+
return intervalMembership(True, self.is_valid)
|
97 |
+
elif self.start > other:
|
98 |
+
return intervalMembership(False, self.is_valid)
|
99 |
+
else:
|
100 |
+
return intervalMembership(None, self.is_valid)
|
101 |
+
|
102 |
+
elif isinstance(other, interval):
|
103 |
+
valid = fuzzy_and([self.is_valid, other.is_valid])
|
104 |
+
if self.end < other. start:
|
105 |
+
return intervalMembership(True, valid)
|
106 |
+
if self.start > other.end:
|
107 |
+
return intervalMembership(False, valid)
|
108 |
+
return intervalMembership(None, valid)
|
109 |
+
else:
|
110 |
+
return NotImplemented
|
111 |
+
|
112 |
+
def __gt__(self, other):
|
113 |
+
if isinstance(other, (int, float)):
|
114 |
+
if self.start > other:
|
115 |
+
return intervalMembership(True, self.is_valid)
|
116 |
+
elif self.end < other:
|
117 |
+
return intervalMembership(False, self.is_valid)
|
118 |
+
else:
|
119 |
+
return intervalMembership(None, self.is_valid)
|
120 |
+
elif isinstance(other, interval):
|
121 |
+
return other.__lt__(self)
|
122 |
+
else:
|
123 |
+
return NotImplemented
|
124 |
+
|
125 |
+
def __eq__(self, other):
|
126 |
+
if isinstance(other, (int, float)):
|
127 |
+
if self.start == other and self.end == other:
|
128 |
+
return intervalMembership(True, self.is_valid)
|
129 |
+
if other in self:
|
130 |
+
return intervalMembership(None, self.is_valid)
|
131 |
+
else:
|
132 |
+
return intervalMembership(False, self.is_valid)
|
133 |
+
|
134 |
+
if isinstance(other, interval):
|
135 |
+
valid = fuzzy_and([self.is_valid, other.is_valid])
|
136 |
+
if self.start == other.start and self.end == other.end:
|
137 |
+
return intervalMembership(True, valid)
|
138 |
+
elif self.__lt__(other)[0] is not None:
|
139 |
+
return intervalMembership(False, valid)
|
140 |
+
else:
|
141 |
+
return intervalMembership(None, valid)
|
142 |
+
else:
|
143 |
+
return NotImplemented
|
144 |
+
|
145 |
+
def __ne__(self, other):
|
146 |
+
if isinstance(other, (int, float)):
|
147 |
+
if self.start == other and self.end == other:
|
148 |
+
return intervalMembership(False, self.is_valid)
|
149 |
+
if other in self:
|
150 |
+
return intervalMembership(None, self.is_valid)
|
151 |
+
else:
|
152 |
+
return intervalMembership(True, self.is_valid)
|
153 |
+
|
154 |
+
if isinstance(other, interval):
|
155 |
+
valid = fuzzy_and([self.is_valid, other.is_valid])
|
156 |
+
if self.start == other.start and self.end == other.end:
|
157 |
+
return intervalMembership(False, valid)
|
158 |
+
if not self.__lt__(other)[0] is None:
|
159 |
+
return intervalMembership(True, valid)
|
160 |
+
return intervalMembership(None, valid)
|
161 |
+
else:
|
162 |
+
return NotImplemented
|
163 |
+
|
164 |
+
def __le__(self, other):
|
165 |
+
if isinstance(other, (int, float)):
|
166 |
+
if self.end <= other:
|
167 |
+
return intervalMembership(True, self.is_valid)
|
168 |
+
if self.start > other:
|
169 |
+
return intervalMembership(False, self.is_valid)
|
170 |
+
else:
|
171 |
+
return intervalMembership(None, self.is_valid)
|
172 |
+
|
173 |
+
if isinstance(other, interval):
|
174 |
+
valid = fuzzy_and([self.is_valid, other.is_valid])
|
175 |
+
if self.end <= other.start:
|
176 |
+
return intervalMembership(True, valid)
|
177 |
+
if self.start > other.end:
|
178 |
+
return intervalMembership(False, valid)
|
179 |
+
return intervalMembership(None, valid)
|
180 |
+
else:
|
181 |
+
return NotImplemented
|
182 |
+
|
183 |
+
def __ge__(self, other):
|
184 |
+
if isinstance(other, (int, float)):
|
185 |
+
if self.start >= other:
|
186 |
+
return intervalMembership(True, self.is_valid)
|
187 |
+
elif self.end < other:
|
188 |
+
return intervalMembership(False, self.is_valid)
|
189 |
+
else:
|
190 |
+
return intervalMembership(None, self.is_valid)
|
191 |
+
elif isinstance(other, interval):
|
192 |
+
return other.__le__(self)
|
193 |
+
|
194 |
+
def __add__(self, other):
|
195 |
+
if isinstance(other, (int, float)):
|
196 |
+
if self.is_valid:
|
197 |
+
return interval(self.start + other, self.end + other)
|
198 |
+
else:
|
199 |
+
start = self.start + other
|
200 |
+
end = self.end + other
|
201 |
+
return interval(start, end, is_valid=self.is_valid)
|
202 |
+
|
203 |
+
elif isinstance(other, interval):
|
204 |
+
start = self.start + other.start
|
205 |
+
end = self.end + other.end
|
206 |
+
valid = fuzzy_and([self.is_valid, other.is_valid])
|
207 |
+
return interval(start, end, is_valid=valid)
|
208 |
+
else:
|
209 |
+
return NotImplemented
|
210 |
+
|
211 |
+
__radd__ = __add__
|
212 |
+
|
213 |
+
def __sub__(self, other):
|
214 |
+
if isinstance(other, (int, float)):
|
215 |
+
start = self.start - other
|
216 |
+
end = self.end - other
|
217 |
+
return interval(start, end, is_valid=self.is_valid)
|
218 |
+
|
219 |
+
elif isinstance(other, interval):
|
220 |
+
start = self.start - other.end
|
221 |
+
end = self.end - other.start
|
222 |
+
valid = fuzzy_and([self.is_valid, other.is_valid])
|
223 |
+
return interval(start, end, is_valid=valid)
|
224 |
+
else:
|
225 |
+
return NotImplemented
|
226 |
+
|
227 |
+
def __rsub__(self, other):
|
228 |
+
if isinstance(other, (int, float)):
|
229 |
+
start = other - self.end
|
230 |
+
end = other - self.start
|
231 |
+
return interval(start, end, is_valid=self.is_valid)
|
232 |
+
elif isinstance(other, interval):
|
233 |
+
return other.__sub__(self)
|
234 |
+
else:
|
235 |
+
return NotImplemented
|
236 |
+
|
237 |
+
def __neg__(self):
|
238 |
+
if self.is_valid:
|
239 |
+
return interval(-self.end, -self.start)
|
240 |
+
else:
|
241 |
+
return interval(-self.end, -self.start, is_valid=self.is_valid)
|
242 |
+
|
243 |
+
def __mul__(self, other):
|
244 |
+
if isinstance(other, interval):
|
245 |
+
if self.is_valid is False or other.is_valid is False:
|
246 |
+
return interval(-float('inf'), float('inf'), is_valid=False)
|
247 |
+
elif self.is_valid is None or other.is_valid is None:
|
248 |
+
return interval(-float('inf'), float('inf'), is_valid=None)
|
249 |
+
else:
|
250 |
+
inters = []
|
251 |
+
inters.append(self.start * other.start)
|
252 |
+
inters.append(self.end * other.start)
|
253 |
+
inters.append(self.start * other.end)
|
254 |
+
inters.append(self.end * other.end)
|
255 |
+
start = min(inters)
|
256 |
+
end = max(inters)
|
257 |
+
return interval(start, end)
|
258 |
+
elif isinstance(other, (int, float)):
|
259 |
+
return interval(self.start*other, self.end*other, is_valid=self.is_valid)
|
260 |
+
else:
|
261 |
+
return NotImplemented
|
262 |
+
|
263 |
+
__rmul__ = __mul__
|
264 |
+
|
265 |
+
def __contains__(self, other):
|
266 |
+
if isinstance(other, (int, float)):
|
267 |
+
return self.start <= other and self.end >= other
|
268 |
+
else:
|
269 |
+
return self.start <= other.start and other.end <= self.end
|
270 |
+
|
271 |
+
def __rtruediv__(self, other):
|
272 |
+
if isinstance(other, (int, float)):
|
273 |
+
other = interval(other)
|
274 |
+
return other.__truediv__(self)
|
275 |
+
elif isinstance(other, interval):
|
276 |
+
return other.__truediv__(self)
|
277 |
+
else:
|
278 |
+
return NotImplemented
|
279 |
+
|
280 |
+
def __truediv__(self, other):
|
281 |
+
# Both None and False are handled
|
282 |
+
if not self.is_valid:
|
283 |
+
# Don't divide as the value is not valid
|
284 |
+
return interval(-float('inf'), float('inf'), is_valid=self.is_valid)
|
285 |
+
if isinstance(other, (int, float)):
|
286 |
+
if other == 0:
|
287 |
+
# Divide by zero encountered. valid nowhere
|
288 |
+
return interval(-float('inf'), float('inf'), is_valid=False)
|
289 |
+
else:
|
290 |
+
return interval(self.start / other, self.end / other)
|
291 |
+
|
292 |
+
elif isinstance(other, interval):
|
293 |
+
if other.is_valid is False or self.is_valid is False:
|
294 |
+
return interval(-float('inf'), float('inf'), is_valid=False)
|
295 |
+
elif other.is_valid is None or self.is_valid is None:
|
296 |
+
return interval(-float('inf'), float('inf'), is_valid=None)
|
297 |
+
else:
|
298 |
+
# denominator contains both signs, i.e. being divided by zero
|
299 |
+
# return the whole real line with is_valid = None
|
300 |
+
if 0 in other:
|
301 |
+
return interval(-float('inf'), float('inf'), is_valid=None)
|
302 |
+
|
303 |
+
# denominator negative
|
304 |
+
this = self
|
305 |
+
if other.end < 0:
|
306 |
+
this = -this
|
307 |
+
other = -other
|
308 |
+
|
309 |
+
# denominator positive
|
310 |
+
inters = []
|
311 |
+
inters.append(this.start / other.start)
|
312 |
+
inters.append(this.end / other.start)
|
313 |
+
inters.append(this.start / other.end)
|
314 |
+
inters.append(this.end / other.end)
|
315 |
+
start = max(inters)
|
316 |
+
end = min(inters)
|
317 |
+
return interval(start, end)
|
318 |
+
else:
|
319 |
+
return NotImplemented
|
320 |
+
|
321 |
+
def __pow__(self, other):
|
322 |
+
# Implements only power to an integer.
|
323 |
+
from .lib_interval import exp, log
|
324 |
+
if not self.is_valid:
|
325 |
+
return self
|
326 |
+
if isinstance(other, interval):
|
327 |
+
return exp(other * log(self))
|
328 |
+
elif isinstance(other, (float, int)):
|
329 |
+
if other < 0:
|
330 |
+
return 1 / self.__pow__(abs(other))
|
331 |
+
else:
|
332 |
+
if int(other) == other:
|
333 |
+
return _pow_int(self, other)
|
334 |
+
else:
|
335 |
+
return _pow_float(self, other)
|
336 |
+
else:
|
337 |
+
return NotImplemented
|
338 |
+
|
339 |
+
def __rpow__(self, other):
|
340 |
+
if isinstance(other, (float, int)):
|
341 |
+
if not self.is_valid:
|
342 |
+
#Don't do anything
|
343 |
+
return self
|
344 |
+
elif other < 0:
|
345 |
+
if self.width > 0:
|
346 |
+
return interval(-float('inf'), float('inf'), is_valid=False)
|
347 |
+
else:
|
348 |
+
power_rational = nsimplify(self.start)
|
349 |
+
num, denom = power_rational.as_numer_denom()
|
350 |
+
if denom % 2 == 0:
|
351 |
+
return interval(-float('inf'), float('inf'),
|
352 |
+
is_valid=False)
|
353 |
+
else:
|
354 |
+
start = -abs(other)**self.start
|
355 |
+
end = start
|
356 |
+
return interval(start, end)
|
357 |
+
else:
|
358 |
+
return interval(other**self.start, other**self.end)
|
359 |
+
elif isinstance(other, interval):
|
360 |
+
return other.__pow__(self)
|
361 |
+
else:
|
362 |
+
return NotImplemented
|
363 |
+
|
364 |
+
def __hash__(self):
|
365 |
+
return hash((self.is_valid, self.start, self.end))
|
366 |
+
|
367 |
+
|
368 |
+
def _pow_float(inter, power):
|
369 |
+
"""Evaluates an interval raised to a floating point."""
|
370 |
+
power_rational = nsimplify(power)
|
371 |
+
num, denom = power_rational.as_numer_denom()
|
372 |
+
if num % 2 == 0:
|
373 |
+
start = abs(inter.start)**power
|
374 |
+
end = abs(inter.end)**power
|
375 |
+
if start < 0:
|
376 |
+
ret = interval(0, max(start, end))
|
377 |
+
else:
|
378 |
+
ret = interval(start, end)
|
379 |
+
return ret
|
380 |
+
elif denom % 2 == 0:
|
381 |
+
if inter.end < 0:
|
382 |
+
return interval(-float('inf'), float('inf'), is_valid=False)
|
383 |
+
elif inter.start < 0:
|
384 |
+
return interval(0, inter.end**power, is_valid=None)
|
385 |
+
else:
|
386 |
+
return interval(inter.start**power, inter.end**power)
|
387 |
+
else:
|
388 |
+
if inter.start < 0:
|
389 |
+
start = -abs(inter.start)**power
|
390 |
+
else:
|
391 |
+
start = inter.start**power
|
392 |
+
|
393 |
+
if inter.end < 0:
|
394 |
+
end = -abs(inter.end)**power
|
395 |
+
else:
|
396 |
+
end = inter.end**power
|
397 |
+
|
398 |
+
return interval(start, end, is_valid=inter.is_valid)
|
399 |
+
|
400 |
+
|
401 |
+
def _pow_int(inter, power):
|
402 |
+
"""Evaluates an interval raised to an integer power"""
|
403 |
+
power = int(power)
|
404 |
+
if power & 1:
|
405 |
+
return interval(inter.start**power, inter.end**power)
|
406 |
+
else:
|
407 |
+
if inter.start < 0 and inter.end > 0:
|
408 |
+
start = 0
|
409 |
+
end = max(inter.start**power, inter.end**power)
|
410 |
+
return interval(start, end)
|
411 |
+
else:
|
412 |
+
return interval(inter.start**power, inter.end**power)
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/interval_membership.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.logic import fuzzy_and, fuzzy_or, fuzzy_not, fuzzy_xor
|
2 |
+
|
3 |
+
|
4 |
+
class intervalMembership:
|
5 |
+
"""Represents a boolean expression returned by the comparison of
|
6 |
+
the interval object.
|
7 |
+
|
8 |
+
Parameters
|
9 |
+
==========
|
10 |
+
|
11 |
+
(a, b) : (bool, bool)
|
12 |
+
The first value determines the comparison as follows:
|
13 |
+
- True: If the comparison is True throughout the intervals.
|
14 |
+
- False: If the comparison is False throughout the intervals.
|
15 |
+
- None: If the comparison is True for some part of the intervals.
|
16 |
+
|
17 |
+
The second value is determined as follows:
|
18 |
+
- True: If both the intervals in comparison are valid.
|
19 |
+
- False: If at least one of the intervals is False, else
|
20 |
+
- None
|
21 |
+
"""
|
22 |
+
def __init__(self, a, b):
|
23 |
+
self._wrapped = (a, b)
|
24 |
+
|
25 |
+
def __getitem__(self, i):
|
26 |
+
try:
|
27 |
+
return self._wrapped[i]
|
28 |
+
except IndexError:
|
29 |
+
raise IndexError(
|
30 |
+
"{} must be a valid indexing for the 2-tuple."
|
31 |
+
.format(i))
|
32 |
+
|
33 |
+
def __len__(self):
|
34 |
+
return 2
|
35 |
+
|
36 |
+
def __iter__(self):
|
37 |
+
return iter(self._wrapped)
|
38 |
+
|
39 |
+
def __str__(self):
|
40 |
+
return "intervalMembership({}, {})".format(*self)
|
41 |
+
__repr__ = __str__
|
42 |
+
|
43 |
+
def __and__(self, other):
|
44 |
+
if not isinstance(other, intervalMembership):
|
45 |
+
raise ValueError(
|
46 |
+
"The comparison is not supported for {}.".format(other))
|
47 |
+
|
48 |
+
a1, b1 = self
|
49 |
+
a2, b2 = other
|
50 |
+
return intervalMembership(fuzzy_and([a1, a2]), fuzzy_and([b1, b2]))
|
51 |
+
|
52 |
+
def __or__(self, other):
|
53 |
+
if not isinstance(other, intervalMembership):
|
54 |
+
raise ValueError(
|
55 |
+
"The comparison is not supported for {}.".format(other))
|
56 |
+
|
57 |
+
a1, b1 = self
|
58 |
+
a2, b2 = other
|
59 |
+
return intervalMembership(fuzzy_or([a1, a2]), fuzzy_and([b1, b2]))
|
60 |
+
|
61 |
+
def __invert__(self):
|
62 |
+
a, b = self
|
63 |
+
return intervalMembership(fuzzy_not(a), b)
|
64 |
+
|
65 |
+
def __xor__(self, other):
|
66 |
+
if not isinstance(other, intervalMembership):
|
67 |
+
raise ValueError(
|
68 |
+
"The comparison is not supported for {}.".format(other))
|
69 |
+
|
70 |
+
a1, b1 = self
|
71 |
+
a2, b2 = other
|
72 |
+
return intervalMembership(fuzzy_xor([a1, a2]), fuzzy_and([b1, b2]))
|
73 |
+
|
74 |
+
def __eq__(self, other):
|
75 |
+
return self._wrapped == other
|
76 |
+
|
77 |
+
def __ne__(self, other):
|
78 |
+
return self._wrapped != other
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" The module contains implemented functions for interval arithmetic."""
|
2 |
+
from functools import reduce
|
3 |
+
|
4 |
+
from sympy.plotting.intervalmath import interval
|
5 |
+
from sympy.external import import_module
|
6 |
+
|
7 |
+
|
8 |
+
def Abs(x):
|
9 |
+
if isinstance(x, (int, float)):
|
10 |
+
return interval(abs(x))
|
11 |
+
elif isinstance(x, interval):
|
12 |
+
if x.start < 0 and x.end > 0:
|
13 |
+
return interval(0, max(abs(x.start), abs(x.end)), is_valid=x.is_valid)
|
14 |
+
else:
|
15 |
+
return interval(abs(x.start), abs(x.end))
|
16 |
+
else:
|
17 |
+
raise NotImplementedError
|
18 |
+
|
19 |
+
#Monotonic
|
20 |
+
|
21 |
+
|
22 |
+
def exp(x):
|
23 |
+
"""evaluates the exponential of an interval"""
|
24 |
+
np = import_module('numpy')
|
25 |
+
if isinstance(x, (int, float)):
|
26 |
+
return interval(np.exp(x), np.exp(x))
|
27 |
+
elif isinstance(x, interval):
|
28 |
+
return interval(np.exp(x.start), np.exp(x.end), is_valid=x.is_valid)
|
29 |
+
else:
|
30 |
+
raise NotImplementedError
|
31 |
+
|
32 |
+
|
33 |
+
#Monotonic
|
34 |
+
def log(x):
|
35 |
+
"""evaluates the natural logarithm of an interval"""
|
36 |
+
np = import_module('numpy')
|
37 |
+
if isinstance(x, (int, float)):
|
38 |
+
if x <= 0:
|
39 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
40 |
+
else:
|
41 |
+
return interval(np.log(x))
|
42 |
+
elif isinstance(x, interval):
|
43 |
+
if not x.is_valid:
|
44 |
+
return interval(-np.inf, np.inf, is_valid=x.is_valid)
|
45 |
+
elif x.end <= 0:
|
46 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
47 |
+
elif x.start <= 0:
|
48 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
49 |
+
|
50 |
+
return interval(np.log(x.start), np.log(x.end))
|
51 |
+
else:
|
52 |
+
raise NotImplementedError
|
53 |
+
|
54 |
+
|
55 |
+
#Monotonic
|
56 |
+
def log10(x):
|
57 |
+
"""evaluates the logarithm to the base 10 of an interval"""
|
58 |
+
np = import_module('numpy')
|
59 |
+
if isinstance(x, (int, float)):
|
60 |
+
if x <= 0:
|
61 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
62 |
+
else:
|
63 |
+
return interval(np.log10(x))
|
64 |
+
elif isinstance(x, interval):
|
65 |
+
if not x.is_valid:
|
66 |
+
return interval(-np.inf, np.inf, is_valid=x.is_valid)
|
67 |
+
elif x.end <= 0:
|
68 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
69 |
+
elif x.start <= 0:
|
70 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
71 |
+
return interval(np.log10(x.start), np.log10(x.end))
|
72 |
+
else:
|
73 |
+
raise NotImplementedError
|
74 |
+
|
75 |
+
|
76 |
+
#Monotonic
|
77 |
+
def atan(x):
|
78 |
+
"""evaluates the tan inverse of an interval"""
|
79 |
+
np = import_module('numpy')
|
80 |
+
if isinstance(x, (int, float)):
|
81 |
+
return interval(np.arctan(x))
|
82 |
+
elif isinstance(x, interval):
|
83 |
+
start = np.arctan(x.start)
|
84 |
+
end = np.arctan(x.end)
|
85 |
+
return interval(start, end, is_valid=x.is_valid)
|
86 |
+
else:
|
87 |
+
raise NotImplementedError
|
88 |
+
|
89 |
+
|
90 |
+
#periodic
|
91 |
+
def sin(x):
|
92 |
+
"""evaluates the sine of an interval"""
|
93 |
+
np = import_module('numpy')
|
94 |
+
if isinstance(x, (int, float)):
|
95 |
+
return interval(np.sin(x))
|
96 |
+
elif isinstance(x, interval):
|
97 |
+
if not x.is_valid:
|
98 |
+
return interval(-1, 1, is_valid=x.is_valid)
|
99 |
+
na, __ = divmod(x.start, np.pi / 2.0)
|
100 |
+
nb, __ = divmod(x.end, np.pi / 2.0)
|
101 |
+
start = min(np.sin(x.start), np.sin(x.end))
|
102 |
+
end = max(np.sin(x.start), np.sin(x.end))
|
103 |
+
if nb - na > 4:
|
104 |
+
return interval(-1, 1, is_valid=x.is_valid)
|
105 |
+
elif na == nb:
|
106 |
+
return interval(start, end, is_valid=x.is_valid)
|
107 |
+
else:
|
108 |
+
if (na - 1) // 4 != (nb - 1) // 4:
|
109 |
+
#sin has max
|
110 |
+
end = 1
|
111 |
+
if (na - 3) // 4 != (nb - 3) // 4:
|
112 |
+
#sin has min
|
113 |
+
start = -1
|
114 |
+
return interval(start, end)
|
115 |
+
else:
|
116 |
+
raise NotImplementedError
|
117 |
+
|
118 |
+
|
119 |
+
#periodic
|
120 |
+
def cos(x):
|
121 |
+
"""Evaluates the cos of an interval"""
|
122 |
+
np = import_module('numpy')
|
123 |
+
if isinstance(x, (int, float)):
|
124 |
+
return interval(np.sin(x))
|
125 |
+
elif isinstance(x, interval):
|
126 |
+
if not (np.isfinite(x.start) and np.isfinite(x.end)):
|
127 |
+
return interval(-1, 1, is_valid=x.is_valid)
|
128 |
+
na, __ = divmod(x.start, np.pi / 2.0)
|
129 |
+
nb, __ = divmod(x.end, np.pi / 2.0)
|
130 |
+
start = min(np.cos(x.start), np.cos(x.end))
|
131 |
+
end = max(np.cos(x.start), np.cos(x.end))
|
132 |
+
if nb - na > 4:
|
133 |
+
#differ more than 2*pi
|
134 |
+
return interval(-1, 1, is_valid=x.is_valid)
|
135 |
+
elif na == nb:
|
136 |
+
#in the same quadarant
|
137 |
+
return interval(start, end, is_valid=x.is_valid)
|
138 |
+
else:
|
139 |
+
if (na) // 4 != (nb) // 4:
|
140 |
+
#cos has max
|
141 |
+
end = 1
|
142 |
+
if (na - 2) // 4 != (nb - 2) // 4:
|
143 |
+
#cos has min
|
144 |
+
start = -1
|
145 |
+
return interval(start, end, is_valid=x.is_valid)
|
146 |
+
else:
|
147 |
+
raise NotImplementedError
|
148 |
+
|
149 |
+
|
150 |
+
def tan(x):
|
151 |
+
"""Evaluates the tan of an interval"""
|
152 |
+
return sin(x) / cos(x)
|
153 |
+
|
154 |
+
|
155 |
+
#Monotonic
|
156 |
+
def sqrt(x):
|
157 |
+
"""Evaluates the square root of an interval"""
|
158 |
+
np = import_module('numpy')
|
159 |
+
if isinstance(x, (int, float)):
|
160 |
+
if x > 0:
|
161 |
+
return interval(np.sqrt(x))
|
162 |
+
else:
|
163 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
164 |
+
elif isinstance(x, interval):
|
165 |
+
#Outside the domain
|
166 |
+
if x.end < 0:
|
167 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
168 |
+
#Partially outside the domain
|
169 |
+
elif x.start < 0:
|
170 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
171 |
+
else:
|
172 |
+
return interval(np.sqrt(x.start), np.sqrt(x.end),
|
173 |
+
is_valid=x.is_valid)
|
174 |
+
else:
|
175 |
+
raise NotImplementedError
|
176 |
+
|
177 |
+
|
178 |
+
def imin(*args):
|
179 |
+
"""Evaluates the minimum of a list of intervals"""
|
180 |
+
np = import_module('numpy')
|
181 |
+
if not all(isinstance(arg, (int, float, interval)) for arg in args):
|
182 |
+
return NotImplementedError
|
183 |
+
else:
|
184 |
+
new_args = [a for a in args if isinstance(a, (int, float))
|
185 |
+
or a.is_valid]
|
186 |
+
if len(new_args) == 0:
|
187 |
+
if all(a.is_valid is False for a in args):
|
188 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
189 |
+
else:
|
190 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
191 |
+
start_array = [a if isinstance(a, (int, float)) else a.start
|
192 |
+
for a in new_args]
|
193 |
+
|
194 |
+
end_array = [a if isinstance(a, (int, float)) else a.end
|
195 |
+
for a in new_args]
|
196 |
+
return interval(min(start_array), min(end_array))
|
197 |
+
|
198 |
+
|
199 |
+
def imax(*args):
|
200 |
+
"""Evaluates the maximum of a list of intervals"""
|
201 |
+
np = import_module('numpy')
|
202 |
+
if not all(isinstance(arg, (int, float, interval)) for arg in args):
|
203 |
+
return NotImplementedError
|
204 |
+
else:
|
205 |
+
new_args = [a for a in args if isinstance(a, (int, float))
|
206 |
+
or a.is_valid]
|
207 |
+
if len(new_args) == 0:
|
208 |
+
if all(a.is_valid is False for a in args):
|
209 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
210 |
+
else:
|
211 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
212 |
+
start_array = [a if isinstance(a, (int, float)) else a.start
|
213 |
+
for a in new_args]
|
214 |
+
|
215 |
+
end_array = [a if isinstance(a, (int, float)) else a.end
|
216 |
+
for a in new_args]
|
217 |
+
|
218 |
+
return interval(max(start_array), max(end_array))
|
219 |
+
|
220 |
+
|
221 |
+
#Monotonic
|
222 |
+
def sinh(x):
|
223 |
+
"""Evaluates the hyperbolic sine of an interval"""
|
224 |
+
np = import_module('numpy')
|
225 |
+
if isinstance(x, (int, float)):
|
226 |
+
return interval(np.sinh(x), np.sinh(x))
|
227 |
+
elif isinstance(x, interval):
|
228 |
+
return interval(np.sinh(x.start), np.sinh(x.end), is_valid=x.is_valid)
|
229 |
+
else:
|
230 |
+
raise NotImplementedError
|
231 |
+
|
232 |
+
|
233 |
+
def cosh(x):
|
234 |
+
"""Evaluates the hyperbolic cos of an interval"""
|
235 |
+
np = import_module('numpy')
|
236 |
+
if isinstance(x, (int, float)):
|
237 |
+
return interval(np.cosh(x), np.cosh(x))
|
238 |
+
elif isinstance(x, interval):
|
239 |
+
#both signs
|
240 |
+
if x.start < 0 and x.end > 0:
|
241 |
+
end = max(np.cosh(x.start), np.cosh(x.end))
|
242 |
+
return interval(1, end, is_valid=x.is_valid)
|
243 |
+
else:
|
244 |
+
#Monotonic
|
245 |
+
start = np.cosh(x.start)
|
246 |
+
end = np.cosh(x.end)
|
247 |
+
return interval(start, end, is_valid=x.is_valid)
|
248 |
+
else:
|
249 |
+
raise NotImplementedError
|
250 |
+
|
251 |
+
|
252 |
+
#Monotonic
|
253 |
+
def tanh(x):
|
254 |
+
"""Evaluates the hyperbolic tan of an interval"""
|
255 |
+
np = import_module('numpy')
|
256 |
+
if isinstance(x, (int, float)):
|
257 |
+
return interval(np.tanh(x), np.tanh(x))
|
258 |
+
elif isinstance(x, interval):
|
259 |
+
return interval(np.tanh(x.start), np.tanh(x.end), is_valid=x.is_valid)
|
260 |
+
else:
|
261 |
+
raise NotImplementedError
|
262 |
+
|
263 |
+
|
264 |
+
def asin(x):
|
265 |
+
"""Evaluates the inverse sine of an interval"""
|
266 |
+
np = import_module('numpy')
|
267 |
+
if isinstance(x, (int, float)):
|
268 |
+
#Outside the domain
|
269 |
+
if abs(x) > 1:
|
270 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
271 |
+
else:
|
272 |
+
return interval(np.arcsin(x), np.arcsin(x))
|
273 |
+
elif isinstance(x, interval):
|
274 |
+
#Outside the domain
|
275 |
+
if x.is_valid is False or x.start > 1 or x.end < -1:
|
276 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
277 |
+
#Partially outside the domain
|
278 |
+
elif x.start < -1 or x.end > 1:
|
279 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
280 |
+
else:
|
281 |
+
start = np.arcsin(x.start)
|
282 |
+
end = np.arcsin(x.end)
|
283 |
+
return interval(start, end, is_valid=x.is_valid)
|
284 |
+
|
285 |
+
|
286 |
+
def acos(x):
|
287 |
+
"""Evaluates the inverse cos of an interval"""
|
288 |
+
np = import_module('numpy')
|
289 |
+
if isinstance(x, (int, float)):
|
290 |
+
if abs(x) > 1:
|
291 |
+
#Outside the domain
|
292 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
293 |
+
else:
|
294 |
+
return interval(np.arccos(x), np.arccos(x))
|
295 |
+
elif isinstance(x, interval):
|
296 |
+
#Outside the domain
|
297 |
+
if x.is_valid is False or x.start > 1 or x.end < -1:
|
298 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
299 |
+
#Partially outside the domain
|
300 |
+
elif x.start < -1 or x.end > 1:
|
301 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
302 |
+
else:
|
303 |
+
start = np.arccos(x.start)
|
304 |
+
end = np.arccos(x.end)
|
305 |
+
return interval(start, end, is_valid=x.is_valid)
|
306 |
+
|
307 |
+
|
308 |
+
def ceil(x):
|
309 |
+
"""Evaluates the ceiling of an interval"""
|
310 |
+
np = import_module('numpy')
|
311 |
+
if isinstance(x, (int, float)):
|
312 |
+
return interval(np.ceil(x))
|
313 |
+
elif isinstance(x, interval):
|
314 |
+
if x.is_valid is False:
|
315 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
316 |
+
else:
|
317 |
+
start = np.ceil(x.start)
|
318 |
+
end = np.ceil(x.end)
|
319 |
+
#Continuous over the interval
|
320 |
+
if start == end:
|
321 |
+
return interval(start, end, is_valid=x.is_valid)
|
322 |
+
else:
|
323 |
+
#Not continuous over the interval
|
324 |
+
return interval(start, end, is_valid=None)
|
325 |
+
else:
|
326 |
+
return NotImplementedError
|
327 |
+
|
328 |
+
|
329 |
+
def floor(x):
|
330 |
+
"""Evaluates the floor of an interval"""
|
331 |
+
np = import_module('numpy')
|
332 |
+
if isinstance(x, (int, float)):
|
333 |
+
return interval(np.floor(x))
|
334 |
+
elif isinstance(x, interval):
|
335 |
+
if x.is_valid is False:
|
336 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
337 |
+
else:
|
338 |
+
start = np.floor(x.start)
|
339 |
+
end = np.floor(x.end)
|
340 |
+
#continuous over the argument
|
341 |
+
if start == end:
|
342 |
+
return interval(start, end, is_valid=x.is_valid)
|
343 |
+
else:
|
344 |
+
#not continuous over the interval
|
345 |
+
return interval(start, end, is_valid=None)
|
346 |
+
else:
|
347 |
+
return NotImplementedError
|
348 |
+
|
349 |
+
|
350 |
+
def acosh(x):
|
351 |
+
"""Evaluates the inverse hyperbolic cosine of an interval"""
|
352 |
+
np = import_module('numpy')
|
353 |
+
if isinstance(x, (int, float)):
|
354 |
+
#Outside the domain
|
355 |
+
if x < 1:
|
356 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
357 |
+
else:
|
358 |
+
return interval(np.arccosh(x))
|
359 |
+
elif isinstance(x, interval):
|
360 |
+
#Outside the domain
|
361 |
+
if x.end < 1:
|
362 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
363 |
+
#Partly outside the domain
|
364 |
+
elif x.start < 1:
|
365 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
366 |
+
else:
|
367 |
+
start = np.arccosh(x.start)
|
368 |
+
end = np.arccosh(x.end)
|
369 |
+
return interval(start, end, is_valid=x.is_valid)
|
370 |
+
else:
|
371 |
+
return NotImplementedError
|
372 |
+
|
373 |
+
|
374 |
+
#Monotonic
|
375 |
+
def asinh(x):
|
376 |
+
"""Evaluates the inverse hyperbolic sine of an interval"""
|
377 |
+
np = import_module('numpy')
|
378 |
+
if isinstance(x, (int, float)):
|
379 |
+
return interval(np.arcsinh(x))
|
380 |
+
elif isinstance(x, interval):
|
381 |
+
start = np.arcsinh(x.start)
|
382 |
+
end = np.arcsinh(x.end)
|
383 |
+
return interval(start, end, is_valid=x.is_valid)
|
384 |
+
else:
|
385 |
+
return NotImplementedError
|
386 |
+
|
387 |
+
|
388 |
+
def atanh(x):
|
389 |
+
"""Evaluates the inverse hyperbolic tangent of an interval"""
|
390 |
+
np = import_module('numpy')
|
391 |
+
if isinstance(x, (int, float)):
|
392 |
+
#Outside the domain
|
393 |
+
if abs(x) >= 1:
|
394 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
395 |
+
else:
|
396 |
+
return interval(np.arctanh(x))
|
397 |
+
elif isinstance(x, interval):
|
398 |
+
#outside the domain
|
399 |
+
if x.is_valid is False or x.start >= 1 or x.end <= -1:
|
400 |
+
return interval(-np.inf, np.inf, is_valid=False)
|
401 |
+
#partly outside the domain
|
402 |
+
elif x.start <= -1 or x.end >= 1:
|
403 |
+
return interval(-np.inf, np.inf, is_valid=None)
|
404 |
+
else:
|
405 |
+
start = np.arctanh(x.start)
|
406 |
+
end = np.arctanh(x.end)
|
407 |
+
return interval(start, end, is_valid=x.is_valid)
|
408 |
+
else:
|
409 |
+
return NotImplementedError
|
410 |
+
|
411 |
+
|
412 |
+
#Three valued logic for interval plotting.
|
413 |
+
|
414 |
+
def And(*args):
|
415 |
+
"""Defines the three valued ``And`` behaviour for a 2-tuple of
|
416 |
+
three valued logic values"""
|
417 |
+
def reduce_and(cmp_intervala, cmp_intervalb):
|
418 |
+
if cmp_intervala[0] is False or cmp_intervalb[0] is False:
|
419 |
+
first = False
|
420 |
+
elif cmp_intervala[0] is None or cmp_intervalb[0] is None:
|
421 |
+
first = None
|
422 |
+
else:
|
423 |
+
first = True
|
424 |
+
if cmp_intervala[1] is False or cmp_intervalb[1] is False:
|
425 |
+
second = False
|
426 |
+
elif cmp_intervala[1] is None or cmp_intervalb[1] is None:
|
427 |
+
second = None
|
428 |
+
else:
|
429 |
+
second = True
|
430 |
+
return (first, second)
|
431 |
+
return reduce(reduce_and, args)
|
432 |
+
|
433 |
+
|
434 |
+
def Or(*args):
|
435 |
+
"""Defines the three valued ``Or`` behaviour for a 2-tuple of
|
436 |
+
three valued logic values"""
|
437 |
+
def reduce_or(cmp_intervala, cmp_intervalb):
|
438 |
+
if cmp_intervala[0] is True or cmp_intervalb[0] is True:
|
439 |
+
first = True
|
440 |
+
elif cmp_intervala[0] is None or cmp_intervalb[0] is None:
|
441 |
+
first = None
|
442 |
+
else:
|
443 |
+
first = False
|
444 |
+
|
445 |
+
if cmp_intervala[1] is True or cmp_intervalb[1] is True:
|
446 |
+
second = True
|
447 |
+
elif cmp_intervala[1] is None or cmp_intervalb[1] is None:
|
448 |
+
second = None
|
449 |
+
else:
|
450 |
+
second = False
|
451 |
+
return (first, second)
|
452 |
+
return reduce(reduce_or, args)
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (198 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_interval_functions.cpython-310.pyc
ADDED
Binary file (9.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_interval_membership.cpython-310.pyc
ADDED
Binary file (3.66 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_intervalmath.cpython-310.pyc
ADDED
Binary file (6.36 kB). View file
|
|
env-llmeval/lib/python3.10/site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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.external import import_module
|
2 |
+
from sympy.plotting.intervalmath import (
|
3 |
+
Abs, acos, acosh, And, asin, asinh, atan, atanh, ceil, cos, cosh,
|
4 |
+
exp, floor, imax, imin, interval, log, log10, Or, sin, sinh, sqrt,
|
5 |
+
tan, tanh,
|
6 |
+
)
|
7 |
+
|
8 |
+
np = import_module('numpy')
|
9 |
+
if not np:
|
10 |
+
disabled = True
|
11 |
+
|
12 |
+
|
13 |
+
#requires Numpy. Hence included in interval_functions
|
14 |
+
|
15 |
+
|
16 |
+
def test_interval_pow():
|
17 |
+
a = 2**interval(1, 2) == interval(2, 4)
|
18 |
+
assert a == (True, True)
|
19 |
+
a = interval(1, 2)**interval(1, 2) == interval(1, 4)
|
20 |
+
assert a == (True, True)
|
21 |
+
a = interval(-1, 1)**interval(0.5, 2)
|
22 |
+
assert a.is_valid is None
|
23 |
+
a = interval(-2, -1) ** interval(1, 2)
|
24 |
+
assert a.is_valid is False
|
25 |
+
a = interval(-2, -1) ** (1.0 / 2)
|
26 |
+
assert a.is_valid is False
|
27 |
+
a = interval(-1, 1)**(1.0 / 2)
|
28 |
+
assert a.is_valid is None
|
29 |
+
a = interval(-1, 1)**(1.0 / 3) == interval(-1, 1)
|
30 |
+
assert a == (True, True)
|
31 |
+
a = interval(-1, 1)**2 == interval(0, 1)
|
32 |
+
assert a == (True, True)
|
33 |
+
a = interval(-1, 1) ** (1.0 / 29) == interval(-1, 1)
|
34 |
+
assert a == (True, True)
|
35 |
+
a = -2**interval(1, 1) == interval(-2, -2)
|
36 |
+
assert a == (True, True)
|
37 |
+
|
38 |
+
a = interval(1, 2, is_valid=False)**2
|
39 |
+
assert a.is_valid is False
|
40 |
+
|
41 |
+
a = (-3)**interval(1, 2)
|
42 |
+
assert a.is_valid is False
|
43 |
+
a = (-4)**interval(0.5, 0.5)
|
44 |
+
assert a.is_valid is False
|
45 |
+
assert ((-3)**interval(1, 1) == interval(-3, -3)) == (True, True)
|
46 |
+
|
47 |
+
a = interval(8, 64)**(2.0 / 3)
|
48 |
+
assert abs(a.start - 4) < 1e-10 # eps
|
49 |
+
assert abs(a.end - 16) < 1e-10
|
50 |
+
a = interval(-8, 64)**(2.0 / 3)
|
51 |
+
assert abs(a.start - 4) < 1e-10 # eps
|
52 |
+
assert abs(a.end - 16) < 1e-10
|
53 |
+
|
54 |
+
|
55 |
+
def test_exp():
|
56 |
+
a = exp(interval(-np.inf, 0))
|
57 |
+
assert a.start == np.exp(-np.inf)
|
58 |
+
assert a.end == np.exp(0)
|
59 |
+
a = exp(interval(1, 2))
|
60 |
+
assert a.start == np.exp(1)
|
61 |
+
assert a.end == np.exp(2)
|
62 |
+
a = exp(1)
|
63 |
+
assert a.start == np.exp(1)
|
64 |
+
assert a.end == np.exp(1)
|
65 |
+
|
66 |
+
|
67 |
+
def test_log():
|
68 |
+
a = log(interval(1, 2))
|
69 |
+
assert a.start == 0
|
70 |
+
assert a.end == np.log(2)
|
71 |
+
a = log(interval(-1, 1))
|
72 |
+
assert a.is_valid is None
|
73 |
+
a = log(interval(-3, -1))
|
74 |
+
assert a.is_valid is False
|
75 |
+
a = log(-3)
|
76 |
+
assert a.is_valid is False
|
77 |
+
a = log(2)
|
78 |
+
assert a.start == np.log(2)
|
79 |
+
assert a.end == np.log(2)
|
80 |
+
|
81 |
+
|
82 |
+
def test_log10():
|
83 |
+
a = log10(interval(1, 2))
|
84 |
+
assert a.start == 0
|
85 |
+
assert a.end == np.log10(2)
|
86 |
+
a = log10(interval(-1, 1))
|
87 |
+
assert a.is_valid is None
|
88 |
+
a = log10(interval(-3, -1))
|
89 |
+
assert a.is_valid is False
|
90 |
+
a = log10(-3)
|
91 |
+
assert a.is_valid is False
|
92 |
+
a = log10(2)
|
93 |
+
assert a.start == np.log10(2)
|
94 |
+
assert a.end == np.log10(2)
|
95 |
+
|
96 |
+
|
97 |
+
def test_atan():
|
98 |
+
a = atan(interval(0, 1))
|
99 |
+
assert a.start == np.arctan(0)
|
100 |
+
assert a.end == np.arctan(1)
|
101 |
+
a = atan(1)
|
102 |
+
assert a.start == np.arctan(1)
|
103 |
+
assert a.end == np.arctan(1)
|
104 |
+
|
105 |
+
|
106 |
+
def test_sin():
|
107 |
+
a = sin(interval(0, np.pi / 4))
|
108 |
+
assert a.start == np.sin(0)
|
109 |
+
assert a.end == np.sin(np.pi / 4)
|
110 |
+
|
111 |
+
a = sin(interval(-np.pi / 4, np.pi / 4))
|
112 |
+
assert a.start == np.sin(-np.pi / 4)
|
113 |
+
assert a.end == np.sin(np.pi / 4)
|
114 |
+
|
115 |
+
a = sin(interval(np.pi / 4, 3 * np.pi / 4))
|
116 |
+
assert a.start == np.sin(np.pi / 4)
|
117 |
+
assert a.end == 1
|
118 |
+
|
119 |
+
a = sin(interval(7 * np.pi / 6, 7 * np.pi / 4))
|
120 |
+
assert a.start == -1
|
121 |
+
assert a.end == np.sin(7 * np.pi / 6)
|
122 |
+
|
123 |
+
a = sin(interval(0, 3 * np.pi))
|
124 |
+
assert a.start == -1
|
125 |
+
assert a.end == 1
|
126 |
+
|
127 |
+
a = sin(interval(np.pi / 3, 7 * np.pi / 4))
|
128 |
+
assert a.start == -1
|
129 |
+
assert a.end == 1
|
130 |
+
|
131 |
+
a = sin(np.pi / 4)
|
132 |
+
assert a.start == np.sin(np.pi / 4)
|
133 |
+
assert a.end == np.sin(np.pi / 4)
|
134 |
+
|
135 |
+
a = sin(interval(1, 2, is_valid=False))
|
136 |
+
assert a.is_valid is False
|
137 |
+
|
138 |
+
|
139 |
+
def test_cos():
|
140 |
+
a = cos(interval(0, np.pi / 4))
|
141 |
+
assert a.start == np.cos(np.pi / 4)
|
142 |
+
assert a.end == 1
|
143 |
+
|
144 |
+
a = cos(interval(-np.pi / 4, np.pi / 4))
|
145 |
+
assert a.start == np.cos(-np.pi / 4)
|
146 |
+
assert a.end == 1
|
147 |
+
|
148 |
+
a = cos(interval(np.pi / 4, 3 * np.pi / 4))
|
149 |
+
assert a.start == np.cos(3 * np.pi / 4)
|
150 |
+
assert a.end == np.cos(np.pi / 4)
|
151 |
+
|
152 |
+
a = cos(interval(3 * np.pi / 4, 5 * np.pi / 4))
|
153 |
+
assert a.start == -1
|
154 |
+
assert a.end == np.cos(3 * np.pi / 4)
|
155 |
+
|
156 |
+
a = cos(interval(0, 3 * np.pi))
|
157 |
+
assert a.start == -1
|
158 |
+
assert a.end == 1
|
159 |
+
|
160 |
+
a = cos(interval(- np.pi / 3, 5 * np.pi / 4))
|
161 |
+
assert a.start == -1
|
162 |
+
assert a.end == 1
|
163 |
+
|
164 |
+
a = cos(interval(1, 2, is_valid=False))
|
165 |
+
assert a.is_valid is False
|
166 |
+
|
167 |
+
|
168 |
+
def test_tan():
|
169 |
+
a = tan(interval(0, np.pi / 4))
|
170 |
+
assert a.start == 0
|
171 |
+
# must match lib_interval definition of tan:
|
172 |
+
assert a.end == np.sin(np.pi / 4)/np.cos(np.pi / 4)
|
173 |
+
|
174 |
+
a = tan(interval(np.pi / 4, 3 * np.pi / 4))
|
175 |
+
#discontinuity
|
176 |
+
assert a.is_valid is None
|
177 |
+
|
178 |
+
|
179 |
+
def test_sqrt():
|
180 |
+
a = sqrt(interval(1, 4))
|
181 |
+
assert a.start == 1
|
182 |
+
assert a.end == 2
|
183 |
+
|
184 |
+
a = sqrt(interval(0.01, 1))
|
185 |
+
assert a.start == np.sqrt(0.01)
|
186 |
+
assert a.end == 1
|
187 |
+
|
188 |
+
a = sqrt(interval(-1, 1))
|
189 |
+
assert a.is_valid is None
|
190 |
+
|
191 |
+
a = sqrt(interval(-3, -1))
|
192 |
+
assert a.is_valid is False
|
193 |
+
|
194 |
+
a = sqrt(4)
|
195 |
+
assert (a == interval(2, 2)) == (True, True)
|
196 |
+
|
197 |
+
a = sqrt(-3)
|
198 |
+
assert a.is_valid is False
|
199 |
+
|
200 |
+
|
201 |
+
def test_imin():
|
202 |
+
a = imin(interval(1, 3), interval(2, 5), interval(-1, 3))
|
203 |
+
assert a.start == -1
|
204 |
+
assert a.end == 3
|
205 |
+
|
206 |
+
a = imin(-2, interval(1, 4))
|
207 |
+
assert a.start == -2
|
208 |
+
assert a.end == -2
|
209 |
+
|
210 |
+
a = imin(5, interval(3, 4), interval(-2, 2, is_valid=False))
|
211 |
+
assert a.start == 3
|
212 |
+
assert a.end == 4
|
213 |
+
|
214 |
+
|
215 |
+
def test_imax():
|
216 |
+
a = imax(interval(-2, 2), interval(2, 7), interval(-3, 9))
|
217 |
+
assert a.start == 2
|
218 |
+
assert a.end == 9
|
219 |
+
|
220 |
+
a = imax(8, interval(1, 4))
|
221 |
+
assert a.start == 8
|
222 |
+
assert a.end == 8
|
223 |
+
|
224 |
+
a = imax(interval(1, 2), interval(3, 4), interval(-2, 2, is_valid=False))
|
225 |
+
assert a.start == 3
|
226 |
+
assert a.end == 4
|
227 |
+
|
228 |
+
|
229 |
+
def test_sinh():
|
230 |
+
a = sinh(interval(-1, 1))
|
231 |
+
assert a.start == np.sinh(-1)
|
232 |
+
assert a.end == np.sinh(1)
|
233 |
+
|
234 |
+
a = sinh(1)
|
235 |
+
assert a.start == np.sinh(1)
|
236 |
+
assert a.end == np.sinh(1)
|
237 |
+
|
238 |
+
|
239 |
+
def test_cosh():
|
240 |
+
a = cosh(interval(1, 2))
|
241 |
+
assert a.start == np.cosh(1)
|
242 |
+
assert a.end == np.cosh(2)
|
243 |
+
a = cosh(interval(-2, -1))
|
244 |
+
assert a.start == np.cosh(-1)
|
245 |
+
assert a.end == np.cosh(-2)
|
246 |
+
|
247 |
+
a = cosh(interval(-2, 1))
|
248 |
+
assert a.start == 1
|
249 |
+
assert a.end == np.cosh(-2)
|
250 |
+
|
251 |
+
a = cosh(1)
|
252 |
+
assert a.start == np.cosh(1)
|
253 |
+
assert a.end == np.cosh(1)
|
254 |
+
|
255 |
+
|
256 |
+
def test_tanh():
|
257 |
+
a = tanh(interval(-3, 3))
|
258 |
+
assert a.start == np.tanh(-3)
|
259 |
+
assert a.end == np.tanh(3)
|
260 |
+
|
261 |
+
a = tanh(3)
|
262 |
+
assert a.start == np.tanh(3)
|
263 |
+
assert a.end == np.tanh(3)
|
264 |
+
|
265 |
+
|
266 |
+
def test_asin():
|
267 |
+
a = asin(interval(-0.5, 0.5))
|
268 |
+
assert a.start == np.arcsin(-0.5)
|
269 |
+
assert a.end == np.arcsin(0.5)
|
270 |
+
|
271 |
+
a = asin(interval(-1.5, 1.5))
|
272 |
+
assert a.is_valid is None
|
273 |
+
a = asin(interval(-2, -1.5))
|
274 |
+
assert a.is_valid is False
|
275 |
+
|
276 |
+
a = asin(interval(0, 2))
|
277 |
+
assert a.is_valid is None
|
278 |
+
|
279 |
+
a = asin(interval(2, 5))
|
280 |
+
assert a.is_valid is False
|
281 |
+
|
282 |
+
a = asin(0.5)
|
283 |
+
assert a.start == np.arcsin(0.5)
|
284 |
+
assert a.end == np.arcsin(0.5)
|
285 |
+
|
286 |
+
a = asin(1.5)
|
287 |
+
assert a.is_valid is False
|
288 |
+
|
289 |
+
|
290 |
+
def test_acos():
|
291 |
+
a = acos(interval(-0.5, 0.5))
|
292 |
+
assert a.start == np.arccos(0.5)
|
293 |
+
assert a.end == np.arccos(-0.5)
|
294 |
+
|
295 |
+
a = acos(interval(-1.5, 1.5))
|
296 |
+
assert a.is_valid is None
|
297 |
+
a = acos(interval(-2, -1.5))
|
298 |
+
assert a.is_valid is False
|
299 |
+
|
300 |
+
a = acos(interval(0, 2))
|
301 |
+
assert a.is_valid is None
|
302 |
+
|
303 |
+
a = acos(interval(2, 5))
|
304 |
+
assert a.is_valid is False
|
305 |
+
|
306 |
+
a = acos(0.5)
|
307 |
+
assert a.start == np.arccos(0.5)
|
308 |
+
assert a.end == np.arccos(0.5)
|
309 |
+
|
310 |
+
a = acos(1.5)
|
311 |
+
assert a.is_valid is False
|
312 |
+
|
313 |
+
|
314 |
+
def test_ceil():
|
315 |
+
a = ceil(interval(0.2, 0.5))
|
316 |
+
assert a.start == 1
|
317 |
+
assert a.end == 1
|
318 |
+
|
319 |
+
a = ceil(interval(0.5, 1.5))
|
320 |
+
assert a.start == 1
|
321 |
+
assert a.end == 2
|
322 |
+
assert a.is_valid is None
|
323 |
+
|
324 |
+
a = ceil(interval(-5, 5))
|
325 |
+
assert a.is_valid is None
|
326 |
+
|
327 |
+
a = ceil(5.4)
|
328 |
+
assert a.start == 6
|
329 |
+
assert a.end == 6
|
330 |
+
|
331 |
+
|
332 |
+
def test_floor():
|
333 |
+
a = floor(interval(0.2, 0.5))
|
334 |
+
assert a.start == 0
|
335 |
+
assert a.end == 0
|
336 |
+
|
337 |
+
a = floor(interval(0.5, 1.5))
|
338 |
+
assert a.start == 0
|
339 |
+
assert a.end == 1
|
340 |
+
assert a.is_valid is None
|
341 |
+
|
342 |
+
a = floor(interval(-5, 5))
|
343 |
+
assert a.is_valid is None
|
344 |
+
|
345 |
+
a = floor(5.4)
|
346 |
+
assert a.start == 5
|
347 |
+
assert a.end == 5
|
348 |
+
|
349 |
+
|
350 |
+
def test_asinh():
|
351 |
+
a = asinh(interval(1, 2))
|
352 |
+
assert a.start == np.arcsinh(1)
|
353 |
+
assert a.end == np.arcsinh(2)
|
354 |
+
|
355 |
+
a = asinh(0.5)
|
356 |
+
assert a.start == np.arcsinh(0.5)
|
357 |
+
assert a.end == np.arcsinh(0.5)
|
358 |
+
|
359 |
+
|
360 |
+
def test_acosh():
|
361 |
+
a = acosh(interval(3, 5))
|
362 |
+
assert a.start == np.arccosh(3)
|
363 |
+
assert a.end == np.arccosh(5)
|
364 |
+
|
365 |
+
a = acosh(interval(0, 3))
|
366 |
+
assert a.is_valid is None
|
367 |
+
a = acosh(interval(-3, 0.5))
|
368 |
+
assert a.is_valid is False
|
369 |
+
|
370 |
+
a = acosh(0.5)
|
371 |
+
assert a.is_valid is False
|
372 |
+
|
373 |
+
a = acosh(2)
|
374 |
+
assert a.start == np.arccosh(2)
|
375 |
+
assert a.end == np.arccosh(2)
|
376 |
+
|
377 |
+
|
378 |
+
def test_atanh():
|
379 |
+
a = atanh(interval(-0.5, 0.5))
|
380 |
+
assert a.start == np.arctanh(-0.5)
|
381 |
+
assert a.end == np.arctanh(0.5)
|
382 |
+
|
383 |
+
a = atanh(interval(0, 3))
|
384 |
+
assert a.is_valid is None
|
385 |
+
|
386 |
+
a = atanh(interval(-3, -2))
|
387 |
+
assert a.is_valid is False
|
388 |
+
|
389 |
+
a = atanh(0.5)
|
390 |
+
assert a.start == np.arctanh(0.5)
|
391 |
+
assert a.end == np.arctanh(0.5)
|
392 |
+
|
393 |
+
a = atanh(1.5)
|
394 |
+
assert a.is_valid is False
|
395 |
+
|
396 |
+
|
397 |
+
def test_Abs():
|
398 |
+
assert (Abs(interval(-0.5, 0.5)) == interval(0, 0.5)) == (True, True)
|
399 |
+
assert (Abs(interval(-3, -2)) == interval(2, 3)) == (True, True)
|
400 |
+
assert (Abs(-3) == interval(3, 3)) == (True, True)
|
401 |
+
|
402 |
+
|
403 |
+
def test_And():
|
404 |
+
args = [(True, True), (True, False), (True, None)]
|
405 |
+
assert And(*args) == (True, False)
|
406 |
+
|
407 |
+
args = [(False, True), (None, None), (True, True)]
|
408 |
+
assert And(*args) == (False, None)
|
409 |
+
|
410 |
+
|
411 |
+
def test_Or():
|
412 |
+
args = [(True, True), (True, False), (False, None)]
|
413 |
+
assert Or(*args) == (True, True)
|
414 |
+
args = [(None, None), (False, None), (False, False)]
|
415 |
+
assert Or(*args) == (None, None)
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_interval_membership.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.symbol import Symbol
|
2 |
+
from sympy.plotting.intervalmath import interval
|
3 |
+
from sympy.plotting.intervalmath.interval_membership import intervalMembership
|
4 |
+
from sympy.plotting.experimental_lambdify import experimental_lambdify
|
5 |
+
from sympy.testing.pytest import raises
|
6 |
+
|
7 |
+
|
8 |
+
def test_creation():
|
9 |
+
assert intervalMembership(True, True)
|
10 |
+
raises(TypeError, lambda: intervalMembership(True))
|
11 |
+
raises(TypeError, lambda: intervalMembership(True, True, True))
|
12 |
+
|
13 |
+
|
14 |
+
def test_getitem():
|
15 |
+
a = intervalMembership(True, False)
|
16 |
+
assert a[0] is True
|
17 |
+
assert a[1] is False
|
18 |
+
raises(IndexError, lambda: a[2])
|
19 |
+
|
20 |
+
|
21 |
+
def test_str():
|
22 |
+
a = intervalMembership(True, False)
|
23 |
+
assert str(a) == 'intervalMembership(True, False)'
|
24 |
+
assert repr(a) == 'intervalMembership(True, False)'
|
25 |
+
|
26 |
+
|
27 |
+
def test_equivalence():
|
28 |
+
a = intervalMembership(True, True)
|
29 |
+
b = intervalMembership(True, False)
|
30 |
+
assert (a == b) is False
|
31 |
+
assert (a != b) is True
|
32 |
+
|
33 |
+
a = intervalMembership(True, False)
|
34 |
+
b = intervalMembership(True, False)
|
35 |
+
assert (a == b) is True
|
36 |
+
assert (a != b) is False
|
37 |
+
|
38 |
+
|
39 |
+
def test_not():
|
40 |
+
x = Symbol('x')
|
41 |
+
|
42 |
+
r1 = x > -1
|
43 |
+
r2 = x <= -1
|
44 |
+
|
45 |
+
i = interval
|
46 |
+
|
47 |
+
f1 = experimental_lambdify((x,), r1)
|
48 |
+
f2 = experimental_lambdify((x,), r2)
|
49 |
+
|
50 |
+
tt = i(-0.1, 0.1, is_valid=True)
|
51 |
+
tn = i(-0.1, 0.1, is_valid=None)
|
52 |
+
tf = i(-0.1, 0.1, is_valid=False)
|
53 |
+
|
54 |
+
assert f1(tt) == ~f2(tt)
|
55 |
+
assert f1(tn) == ~f2(tn)
|
56 |
+
assert f1(tf) == ~f2(tf)
|
57 |
+
|
58 |
+
nt = i(0.9, 1.1, is_valid=True)
|
59 |
+
nn = i(0.9, 1.1, is_valid=None)
|
60 |
+
nf = i(0.9, 1.1, is_valid=False)
|
61 |
+
|
62 |
+
assert f1(nt) == ~f2(nt)
|
63 |
+
assert f1(nn) == ~f2(nn)
|
64 |
+
assert f1(nf) == ~f2(nf)
|
65 |
+
|
66 |
+
ft = i(1.9, 2.1, is_valid=True)
|
67 |
+
fn = i(1.9, 2.1, is_valid=None)
|
68 |
+
ff = i(1.9, 2.1, is_valid=False)
|
69 |
+
|
70 |
+
assert f1(ft) == ~f2(ft)
|
71 |
+
assert f1(fn) == ~f2(fn)
|
72 |
+
assert f1(ff) == ~f2(ff)
|
73 |
+
|
74 |
+
|
75 |
+
def test_boolean():
|
76 |
+
# There can be 9*9 test cases in full mapping of the cartesian product.
|
77 |
+
# But we only consider 3*3 cases for simplicity.
|
78 |
+
s = [
|
79 |
+
intervalMembership(False, False),
|
80 |
+
intervalMembership(None, None),
|
81 |
+
intervalMembership(True, True)
|
82 |
+
]
|
83 |
+
|
84 |
+
# Reduced tests for 'And'
|
85 |
+
a1 = [
|
86 |
+
intervalMembership(False, False),
|
87 |
+
intervalMembership(False, False),
|
88 |
+
intervalMembership(False, False),
|
89 |
+
intervalMembership(False, False),
|
90 |
+
intervalMembership(None, None),
|
91 |
+
intervalMembership(None, None),
|
92 |
+
intervalMembership(False, False),
|
93 |
+
intervalMembership(None, None),
|
94 |
+
intervalMembership(True, True)
|
95 |
+
]
|
96 |
+
a1_iter = iter(a1)
|
97 |
+
for i in range(len(s)):
|
98 |
+
for j in range(len(s)):
|
99 |
+
assert s[i] & s[j] == next(a1_iter)
|
100 |
+
|
101 |
+
# Reduced tests for 'Or'
|
102 |
+
a1 = [
|
103 |
+
intervalMembership(False, False),
|
104 |
+
intervalMembership(None, False),
|
105 |
+
intervalMembership(True, False),
|
106 |
+
intervalMembership(None, False),
|
107 |
+
intervalMembership(None, None),
|
108 |
+
intervalMembership(True, None),
|
109 |
+
intervalMembership(True, False),
|
110 |
+
intervalMembership(True, None),
|
111 |
+
intervalMembership(True, True)
|
112 |
+
]
|
113 |
+
a1_iter = iter(a1)
|
114 |
+
for i in range(len(s)):
|
115 |
+
for j in range(len(s)):
|
116 |
+
assert s[i] | s[j] == next(a1_iter)
|
117 |
+
|
118 |
+
# Reduced tests for 'Xor'
|
119 |
+
a1 = [
|
120 |
+
intervalMembership(False, False),
|
121 |
+
intervalMembership(None, False),
|
122 |
+
intervalMembership(True, False),
|
123 |
+
intervalMembership(None, False),
|
124 |
+
intervalMembership(None, None),
|
125 |
+
intervalMembership(None, None),
|
126 |
+
intervalMembership(True, False),
|
127 |
+
intervalMembership(None, None),
|
128 |
+
intervalMembership(False, True)
|
129 |
+
]
|
130 |
+
a1_iter = iter(a1)
|
131 |
+
for i in range(len(s)):
|
132 |
+
for j in range(len(s)):
|
133 |
+
assert s[i] ^ s[j] == next(a1_iter)
|
134 |
+
|
135 |
+
# Reduced tests for 'Not'
|
136 |
+
a1 = [
|
137 |
+
intervalMembership(True, False),
|
138 |
+
intervalMembership(None, None),
|
139 |
+
intervalMembership(False, True)
|
140 |
+
]
|
141 |
+
a1_iter = iter(a1)
|
142 |
+
for i in range(len(s)):
|
143 |
+
assert ~s[i] == next(a1_iter)
|
144 |
+
|
145 |
+
|
146 |
+
def test_boolean_errors():
|
147 |
+
a = intervalMembership(True, True)
|
148 |
+
raises(ValueError, lambda: a & 1)
|
149 |
+
raises(ValueError, lambda: a | 1)
|
150 |
+
raises(ValueError, lambda: a ^ 1)
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_intervalmath.py
ADDED
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.plotting.intervalmath import interval
|
2 |
+
from sympy.testing.pytest import raises
|
3 |
+
|
4 |
+
|
5 |
+
def test_interval():
|
6 |
+
assert (interval(1, 1) == interval(1, 1, is_valid=True)) == (True, True)
|
7 |
+
assert (interval(1, 1) == interval(1, 1, is_valid=False)) == (True, False)
|
8 |
+
assert (interval(1, 1) == interval(1, 1, is_valid=None)) == (True, None)
|
9 |
+
assert (interval(1, 1.5) == interval(1, 2)) == (None, True)
|
10 |
+
assert (interval(0, 1) == interval(2, 3)) == (False, True)
|
11 |
+
assert (interval(0, 1) == interval(1, 2)) == (None, True)
|
12 |
+
assert (interval(1, 2) != interval(1, 2)) == (False, True)
|
13 |
+
assert (interval(1, 3) != interval(2, 3)) == (None, True)
|
14 |
+
assert (interval(1, 3) != interval(-5, -3)) == (True, True)
|
15 |
+
assert (
|
16 |
+
interval(1, 3, is_valid=False) != interval(-5, -3)) == (True, False)
|
17 |
+
assert (interval(1, 3, is_valid=None) != interval(-5, 3)) == (None, None)
|
18 |
+
assert (interval(4, 4) != 4) == (False, True)
|
19 |
+
assert (interval(1, 1) == 1) == (True, True)
|
20 |
+
assert (interval(1, 3, is_valid=False) == interval(1, 3)) == (True, False)
|
21 |
+
assert (interval(1, 3, is_valid=None) == interval(1, 3)) == (True, None)
|
22 |
+
inter = interval(-5, 5)
|
23 |
+
assert (interval(inter) == interval(-5, 5)) == (True, True)
|
24 |
+
assert inter.width == 10
|
25 |
+
assert 0 in inter
|
26 |
+
assert -5 in inter
|
27 |
+
assert 5 in inter
|
28 |
+
assert interval(0, 3) in inter
|
29 |
+
assert interval(-6, 2) not in inter
|
30 |
+
assert -5.05 not in inter
|
31 |
+
assert 5.3 not in inter
|
32 |
+
interb = interval(-float('inf'), float('inf'))
|
33 |
+
assert 0 in inter
|
34 |
+
assert inter in interb
|
35 |
+
assert interval(0, float('inf')) in interb
|
36 |
+
assert interval(-float('inf'), 5) in interb
|
37 |
+
assert interval(-1e50, 1e50) in interb
|
38 |
+
assert (
|
39 |
+
-interval(-1, -2, is_valid=False) == interval(1, 2)) == (True, False)
|
40 |
+
raises(ValueError, lambda: interval(1, 2, 3))
|
41 |
+
|
42 |
+
|
43 |
+
def test_interval_add():
|
44 |
+
assert (interval(1, 2) + interval(2, 3) == interval(3, 5)) == (True, True)
|
45 |
+
assert (1 + interval(1, 2) == interval(2, 3)) == (True, True)
|
46 |
+
assert (interval(1, 2) + 1 == interval(2, 3)) == (True, True)
|
47 |
+
compare = (1 + interval(0, float('inf')) == interval(1, float('inf')))
|
48 |
+
assert compare == (True, True)
|
49 |
+
a = 1 + interval(2, 5, is_valid=False)
|
50 |
+
assert a.is_valid is False
|
51 |
+
a = 1 + interval(2, 5, is_valid=None)
|
52 |
+
assert a.is_valid is None
|
53 |
+
a = interval(2, 5, is_valid=False) + interval(3, 5, is_valid=None)
|
54 |
+
assert a.is_valid is False
|
55 |
+
a = interval(3, 5) + interval(-1, 1, is_valid=None)
|
56 |
+
assert a.is_valid is None
|
57 |
+
a = interval(2, 5, is_valid=False) + 1
|
58 |
+
assert a.is_valid is False
|
59 |
+
|
60 |
+
|
61 |
+
def test_interval_sub():
|
62 |
+
assert (interval(1, 2) - interval(1, 5) == interval(-4, 1)) == (True, True)
|
63 |
+
assert (interval(1, 2) - 1 == interval(0, 1)) == (True, True)
|
64 |
+
assert (1 - interval(1, 2) == interval(-1, 0)) == (True, True)
|
65 |
+
a = 1 - interval(1, 2, is_valid=False)
|
66 |
+
assert a.is_valid is False
|
67 |
+
a = interval(1, 4, is_valid=None) - 1
|
68 |
+
assert a.is_valid is None
|
69 |
+
a = interval(1, 3, is_valid=False) - interval(1, 3)
|
70 |
+
assert a.is_valid is False
|
71 |
+
a = interval(1, 3, is_valid=None) - interval(1, 3)
|
72 |
+
assert a.is_valid is None
|
73 |
+
|
74 |
+
|
75 |
+
def test_interval_inequality():
|
76 |
+
assert (interval(1, 2) < interval(3, 4)) == (True, True)
|
77 |
+
assert (interval(1, 2) < interval(2, 4)) == (None, True)
|
78 |
+
assert (interval(1, 2) < interval(-2, 0)) == (False, True)
|
79 |
+
assert (interval(1, 2) <= interval(2, 4)) == (True, True)
|
80 |
+
assert (interval(1, 2) <= interval(1.5, 6)) == (None, True)
|
81 |
+
assert (interval(2, 3) <= interval(1, 2)) == (None, True)
|
82 |
+
assert (interval(2, 3) <= interval(1, 1.5)) == (False, True)
|
83 |
+
assert (
|
84 |
+
interval(1, 2, is_valid=False) <= interval(-2, 0)) == (False, False)
|
85 |
+
assert (interval(1, 2, is_valid=None) <= interval(-2, 0)) == (False, None)
|
86 |
+
assert (interval(1, 2) <= 1.5) == (None, True)
|
87 |
+
assert (interval(1, 2) <= 3) == (True, True)
|
88 |
+
assert (interval(1, 2) <= 0) == (False, True)
|
89 |
+
assert (interval(5, 8) > interval(2, 3)) == (True, True)
|
90 |
+
assert (interval(2, 5) > interval(1, 3)) == (None, True)
|
91 |
+
assert (interval(2, 3) > interval(3.1, 5)) == (False, True)
|
92 |
+
|
93 |
+
assert (interval(-1, 1) == 0) == (None, True)
|
94 |
+
assert (interval(-1, 1) == 2) == (False, True)
|
95 |
+
assert (interval(-1, 1) != 0) == (None, True)
|
96 |
+
assert (interval(-1, 1) != 2) == (True, True)
|
97 |
+
|
98 |
+
assert (interval(3, 5) > 2) == (True, True)
|
99 |
+
assert (interval(3, 5) < 2) == (False, True)
|
100 |
+
assert (interval(1, 5) < 2) == (None, True)
|
101 |
+
assert (interval(1, 5) > 2) == (None, True)
|
102 |
+
assert (interval(0, 1) > 2) == (False, True)
|
103 |
+
assert (interval(1, 2) >= interval(0, 1)) == (True, True)
|
104 |
+
assert (interval(1, 2) >= interval(0, 1.5)) == (None, True)
|
105 |
+
assert (interval(1, 2) >= interval(3, 4)) == (False, True)
|
106 |
+
assert (interval(1, 2) >= 0) == (True, True)
|
107 |
+
assert (interval(1, 2) >= 1.2) == (None, True)
|
108 |
+
assert (interval(1, 2) >= 3) == (False, True)
|
109 |
+
assert (2 > interval(0, 1)) == (True, True)
|
110 |
+
a = interval(-1, 1, is_valid=False) < interval(2, 5, is_valid=None)
|
111 |
+
assert a == (True, False)
|
112 |
+
a = interval(-1, 1, is_valid=None) < interval(2, 5, is_valid=False)
|
113 |
+
assert a == (True, False)
|
114 |
+
a = interval(-1, 1, is_valid=None) < interval(2, 5, is_valid=None)
|
115 |
+
assert a == (True, None)
|
116 |
+
a = interval(-1, 1, is_valid=False) > interval(-5, -2, is_valid=None)
|
117 |
+
assert a == (True, False)
|
118 |
+
a = interval(-1, 1, is_valid=None) > interval(-5, -2, is_valid=False)
|
119 |
+
assert a == (True, False)
|
120 |
+
a = interval(-1, 1, is_valid=None) > interval(-5, -2, is_valid=None)
|
121 |
+
assert a == (True, None)
|
122 |
+
|
123 |
+
|
124 |
+
def test_interval_mul():
|
125 |
+
assert (
|
126 |
+
interval(1, 5) * interval(2, 10) == interval(2, 50)) == (True, True)
|
127 |
+
a = interval(-1, 1) * interval(2, 10) == interval(-10, 10)
|
128 |
+
assert a == (True, True)
|
129 |
+
|
130 |
+
a = interval(-1, 1) * interval(-5, 3) == interval(-5, 5)
|
131 |
+
assert a == (True, True)
|
132 |
+
|
133 |
+
assert (interval(1, 3) * 2 == interval(2, 6)) == (True, True)
|
134 |
+
assert (3 * interval(-1, 2) == interval(-3, 6)) == (True, True)
|
135 |
+
|
136 |
+
a = 3 * interval(1, 2, is_valid=False)
|
137 |
+
assert a.is_valid is False
|
138 |
+
|
139 |
+
a = 3 * interval(1, 2, is_valid=None)
|
140 |
+
assert a.is_valid is None
|
141 |
+
|
142 |
+
a = interval(1, 5, is_valid=False) * interval(1, 2, is_valid=None)
|
143 |
+
assert a.is_valid is False
|
144 |
+
|
145 |
+
|
146 |
+
def test_interval_div():
|
147 |
+
div = interval(1, 2, is_valid=False) / 3
|
148 |
+
assert div == interval(-float('inf'), float('inf'), is_valid=False)
|
149 |
+
|
150 |
+
div = interval(1, 2, is_valid=None) / 3
|
151 |
+
assert div == interval(-float('inf'), float('inf'), is_valid=None)
|
152 |
+
|
153 |
+
div = 3 / interval(1, 2, is_valid=None)
|
154 |
+
assert div == interval(-float('inf'), float('inf'), is_valid=None)
|
155 |
+
a = interval(1, 2) / 0
|
156 |
+
assert a.is_valid is False
|
157 |
+
a = interval(0.5, 1) / interval(-1, 0)
|
158 |
+
assert a.is_valid is None
|
159 |
+
a = interval(0, 1) / interval(0, 1)
|
160 |
+
assert a.is_valid is None
|
161 |
+
|
162 |
+
a = interval(-1, 1) / interval(-1, 1)
|
163 |
+
assert a.is_valid is None
|
164 |
+
|
165 |
+
a = interval(-1, 2) / interval(0.5, 1) == interval(-2.0, 4.0)
|
166 |
+
assert a == (True, True)
|
167 |
+
a = interval(0, 1) / interval(0.5, 1) == interval(0.0, 2.0)
|
168 |
+
assert a == (True, True)
|
169 |
+
a = interval(-1, 0) / interval(0.5, 1) == interval(-2.0, 0.0)
|
170 |
+
assert a == (True, True)
|
171 |
+
a = interval(-0.5, -0.25) / interval(0.5, 1) == interval(-1.0, -0.25)
|
172 |
+
assert a == (True, True)
|
173 |
+
a = interval(0.5, 1) / interval(0.5, 1) == interval(0.5, 2.0)
|
174 |
+
assert a == (True, True)
|
175 |
+
a = interval(0.5, 4) / interval(0.5, 1) == interval(0.5, 8.0)
|
176 |
+
assert a == (True, True)
|
177 |
+
a = interval(-1, -0.5) / interval(0.5, 1) == interval(-2.0, -0.5)
|
178 |
+
assert a == (True, True)
|
179 |
+
a = interval(-4, -0.5) / interval(0.5, 1) == interval(-8.0, -0.5)
|
180 |
+
assert a == (True, True)
|
181 |
+
a = interval(-1, 2) / interval(-2, -0.5) == interval(-4.0, 2.0)
|
182 |
+
assert a == (True, True)
|
183 |
+
a = interval(0, 1) / interval(-2, -0.5) == interval(-2.0, 0.0)
|
184 |
+
assert a == (True, True)
|
185 |
+
a = interval(-1, 0) / interval(-2, -0.5) == interval(0.0, 2.0)
|
186 |
+
assert a == (True, True)
|
187 |
+
a = interval(-0.5, -0.25) / interval(-2, -0.5) == interval(0.125, 1.0)
|
188 |
+
assert a == (True, True)
|
189 |
+
a = interval(0.5, 1) / interval(-2, -0.5) == interval(-2.0, -0.25)
|
190 |
+
assert a == (True, True)
|
191 |
+
a = interval(0.5, 4) / interval(-2, -0.5) == interval(-8.0, -0.25)
|
192 |
+
assert a == (True, True)
|
193 |
+
a = interval(-1, -0.5) / interval(-2, -0.5) == interval(0.25, 2.0)
|
194 |
+
assert a == (True, True)
|
195 |
+
a = interval(-4, -0.5) / interval(-2, -0.5) == interval(0.25, 8.0)
|
196 |
+
assert a == (True, True)
|
197 |
+
a = interval(-5, 5, is_valid=False) / 2
|
198 |
+
assert a.is_valid is False
|
199 |
+
|
200 |
+
def test_hashable():
|
201 |
+
'''
|
202 |
+
test that interval objects are hashable.
|
203 |
+
this is required in order to be able to put them into the cache, which
|
204 |
+
appears to be necessary for plotting in py3k. For details, see:
|
205 |
+
|
206 |
+
https://github.com/sympy/sympy/pull/2101
|
207 |
+
https://github.com/sympy/sympy/issues/6533
|
208 |
+
'''
|
209 |
+
hash(interval(1, 1))
|
210 |
+
hash(interval(1, 1, is_valid=True))
|
211 |
+
hash(interval(-4, -0.5))
|
212 |
+
hash(interval(-2, -0.5))
|
213 |
+
hash(interval(0.25, 8.0))
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (3.99 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/color_scheme.cpython-310.pyc
ADDED
Binary file (9.98 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/managed_window.cpython-310.pyc
ADDED
Binary file (3.31 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot.cpython-310.pyc
ADDED
Binary file (13.7 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_axes.cpython-310.pyc
ADDED
Binary file (8.52 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_camera.cpython-310.pyc
ADDED
Binary file (4.31 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_controller.cpython-310.pyc
ADDED
Binary file (5.02 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_curve.cpython-310.pyc
ADDED
Binary file (2.92 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_interval.cpython-310.pyc
ADDED
Binary file (5.85 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_mode.cpython-310.pyc
ADDED
Binary file (9.36 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_mode_base.cpython-310.pyc
ADDED
Binary file (10.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_modes.cpython-310.pyc
ADDED
Binary file (7.33 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/plotting/pygletplot/__pycache__/plot_object.cpython-310.pyc
ADDED
Binary file (798 Bytes). View file
|
|