passagemath-symbolics 10.6.37__cp314-cp314t-macosx_13_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- passagemath_symbolics/.dylibs/libgmp.10.dylib +0 -0
- passagemath_symbolics/__init__.py +3 -0
- passagemath_symbolics-10.6.37.dist-info/METADATA +187 -0
- passagemath_symbolics-10.6.37.dist-info/RECORD +172 -0
- passagemath_symbolics-10.6.37.dist-info/WHEEL +6 -0
- passagemath_symbolics-10.6.37.dist-info/top_level.txt +3 -0
- sage/all__sagemath_symbolics.py +17 -0
- sage/calculus/all.py +14 -0
- sage/calculus/calculus.py +2826 -0
- sage/calculus/desolvers.py +1866 -0
- sage/calculus/predefined.py +51 -0
- sage/calculus/tests.py +225 -0
- sage/calculus/var.cpython-314t-darwin.so +0 -0
- sage/calculus/var.pyx +401 -0
- sage/dynamics/all__sagemath_symbolics.py +6 -0
- sage/dynamics/complex_dynamics/all.py +5 -0
- sage/dynamics/complex_dynamics/mandel_julia.py +765 -0
- sage/dynamics/complex_dynamics/mandel_julia_helper.cpython-314t-darwin.so +0 -0
- sage/dynamics/complex_dynamics/mandel_julia_helper.pyx +1035 -0
- sage/ext/all__sagemath_symbolics.py +1 -0
- sage/ext_data/kenzo/CP2.txt +45 -0
- sage/ext_data/kenzo/CP3.txt +349 -0
- sage/ext_data/kenzo/CP4.txt +4774 -0
- sage/ext_data/kenzo/README.txt +49 -0
- sage/ext_data/kenzo/S4.txt +20 -0
- sage/ext_data/magma/latex/latex.m +1021 -0
- sage/ext_data/magma/latex/latex.spec +1 -0
- sage/ext_data/magma/sage/basic.m +356 -0
- sage/ext_data/magma/sage/sage.spec +1 -0
- sage/ext_data/magma/spec +9 -0
- sage/geometry/all__sagemath_symbolics.py +8 -0
- sage/geometry/hyperbolic_space/all.py +5 -0
- sage/geometry/hyperbolic_space/hyperbolic_coercion.py +743 -0
- sage/geometry/hyperbolic_space/hyperbolic_constants.py +5 -0
- sage/geometry/hyperbolic_space/hyperbolic_geodesic.py +2409 -0
- sage/geometry/hyperbolic_space/hyperbolic_interface.py +206 -0
- sage/geometry/hyperbolic_space/hyperbolic_isometry.py +1082 -0
- sage/geometry/hyperbolic_space/hyperbolic_model.py +1502 -0
- sage/geometry/hyperbolic_space/hyperbolic_point.py +621 -0
- sage/geometry/riemannian_manifolds/all.py +7 -0
- sage/geometry/riemannian_manifolds/parametrized_surface3d.py +1632 -0
- sage/geometry/riemannian_manifolds/surface3d_generators.py +461 -0
- sage/interfaces/all__sagemath_symbolics.py +1 -0
- sage/interfaces/magma.py +3017 -0
- sage/interfaces/magma_free.py +92 -0
- sage/interfaces/maple.py +1397 -0
- sage/interfaces/mathematica.py +1345 -0
- sage/interfaces/mathics.py +1312 -0
- sage/interfaces/sympy.py +1398 -0
- sage/interfaces/sympy_wrapper.py +197 -0
- sage/interfaces/tides.py +938 -0
- sage/libs/all__sagemath_symbolics.py +6 -0
- sage/manifolds/all.py +7 -0
- sage/manifolds/calculus_method.py +555 -0
- sage/manifolds/catalog.py +437 -0
- sage/manifolds/chart.py +4019 -0
- sage/manifolds/chart_func.py +3419 -0
- sage/manifolds/continuous_map.py +2183 -0
- sage/manifolds/continuous_map_image.py +155 -0
- sage/manifolds/differentiable/affine_connection.py +2475 -0
- sage/manifolds/differentiable/all.py +1 -0
- sage/manifolds/differentiable/automorphismfield.py +1383 -0
- sage/manifolds/differentiable/automorphismfield_group.py +604 -0
- sage/manifolds/differentiable/bundle_connection.py +1445 -0
- sage/manifolds/differentiable/characteristic_cohomology_class.py +1840 -0
- sage/manifolds/differentiable/chart.py +1241 -0
- sage/manifolds/differentiable/curve.py +1028 -0
- sage/manifolds/differentiable/de_rham_cohomology.py +541 -0
- sage/manifolds/differentiable/degenerate.py +559 -0
- sage/manifolds/differentiable/degenerate_submanifold.py +1671 -0
- sage/manifolds/differentiable/diff_form.py +1658 -0
- sage/manifolds/differentiable/diff_form_module.py +1062 -0
- sage/manifolds/differentiable/diff_map.py +1315 -0
- sage/manifolds/differentiable/differentiable_submanifold.py +291 -0
- sage/manifolds/differentiable/examples/all.py +1 -0
- sage/manifolds/differentiable/examples/euclidean.py +2517 -0
- sage/manifolds/differentiable/examples/real_line.py +897 -0
- sage/manifolds/differentiable/examples/sphere.py +1186 -0
- sage/manifolds/differentiable/examples/symplectic_space.py +187 -0
- sage/manifolds/differentiable/examples/symplectic_space_test.py +40 -0
- sage/manifolds/differentiable/integrated_curve.py +4035 -0
- sage/manifolds/differentiable/levi_civita_connection.py +841 -0
- sage/manifolds/differentiable/manifold.py +4254 -0
- sage/manifolds/differentiable/manifold_homset.py +1826 -0
- sage/manifolds/differentiable/metric.py +3032 -0
- sage/manifolds/differentiable/mixed_form.py +1507 -0
- sage/manifolds/differentiable/mixed_form_algebra.py +559 -0
- sage/manifolds/differentiable/multivector_module.py +800 -0
- sage/manifolds/differentiable/multivectorfield.py +1520 -0
- sage/manifolds/differentiable/poisson_tensor.py +268 -0
- sage/manifolds/differentiable/pseudo_riemannian.py +755 -0
- sage/manifolds/differentiable/pseudo_riemannian_submanifold.py +1839 -0
- sage/manifolds/differentiable/scalarfield.py +1343 -0
- sage/manifolds/differentiable/scalarfield_algebra.py +472 -0
- sage/manifolds/differentiable/symplectic_form.py +910 -0
- sage/manifolds/differentiable/symplectic_form_test.py +220 -0
- sage/manifolds/differentiable/tangent_space.py +412 -0
- sage/manifolds/differentiable/tangent_vector.py +616 -0
- sage/manifolds/differentiable/tensorfield.py +4665 -0
- sage/manifolds/differentiable/tensorfield_module.py +963 -0
- sage/manifolds/differentiable/tensorfield_paral.py +2450 -0
- sage/manifolds/differentiable/tensorfield_paral_test.py +16 -0
- sage/manifolds/differentiable/vector_bundle.py +1728 -0
- sage/manifolds/differentiable/vectorfield.py +1717 -0
- sage/manifolds/differentiable/vectorfield_module.py +2445 -0
- sage/manifolds/differentiable/vectorframe.py +1832 -0
- sage/manifolds/family.py +270 -0
- sage/manifolds/local_frame.py +1490 -0
- sage/manifolds/manifold.py +3090 -0
- sage/manifolds/manifold_homset.py +452 -0
- sage/manifolds/operators.py +359 -0
- sage/manifolds/point.py +994 -0
- sage/manifolds/scalarfield.py +3718 -0
- sage/manifolds/scalarfield_algebra.py +629 -0
- sage/manifolds/section.py +3111 -0
- sage/manifolds/section_module.py +831 -0
- sage/manifolds/structure.py +229 -0
- sage/manifolds/subset.py +2764 -0
- sage/manifolds/subsets/all.py +1 -0
- sage/manifolds/subsets/closure.py +131 -0
- sage/manifolds/subsets/pullback.py +885 -0
- sage/manifolds/topological_submanifold.py +891 -0
- sage/manifolds/trivialization.py +733 -0
- sage/manifolds/utilities.py +1348 -0
- sage/manifolds/vector_bundle.py +1342 -0
- sage/manifolds/vector_bundle_fiber.py +332 -0
- sage/manifolds/vector_bundle_fiber_element.py +111 -0
- sage/matrix/all__sagemath_symbolics.py +1 -0
- sage/matrix/matrix_symbolic_dense.cpython-314t-darwin.so +0 -0
- sage/matrix/matrix_symbolic_dense.pxd +6 -0
- sage/matrix/matrix_symbolic_dense.pyx +1022 -0
- sage/matrix/matrix_symbolic_sparse.cpython-314t-darwin.so +0 -0
- sage/matrix/matrix_symbolic_sparse.pxd +6 -0
- sage/matrix/matrix_symbolic_sparse.pyx +1029 -0
- sage/modules/all__sagemath_symbolics.py +1 -0
- sage/modules/vector_callable_symbolic_dense.py +105 -0
- sage/modules/vector_symbolic_dense.py +116 -0
- sage/modules/vector_symbolic_sparse.py +118 -0
- sage/rings/all__sagemath_symbolics.py +4 -0
- sage/rings/asymptotic/all.py +6 -0
- sage/rings/asymptotic/asymptotic_expansion_generators.py +1485 -0
- sage/rings/asymptotic/asymptotic_ring.py +4858 -0
- sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py +4153 -0
- sage/rings/asymptotic/growth_group.py +5373 -0
- sage/rings/asymptotic/growth_group_cartesian.py +1400 -0
- sage/rings/asymptotic/term_monoid.py +5237 -0
- sage/rings/function_field/all__sagemath_symbolics.py +2 -0
- sage/rings/polynomial/all__sagemath_symbolics.py +1 -0
- sage/symbolic/all.py +15 -0
- sage/symbolic/assumptions.py +985 -0
- sage/symbolic/benchmark.py +93 -0
- sage/symbolic/callable.py +459 -0
- sage/symbolic/complexity_measures.py +35 -0
- sage/symbolic/constants.py +1287 -0
- sage/symbolic/expression_conversion_algebraic.py +310 -0
- sage/symbolic/expression_conversion_sympy.py +317 -0
- sage/symbolic/expression_conversions.py +1713 -0
- sage/symbolic/function_factory.py +355 -0
- sage/symbolic/integration/all.py +1 -0
- sage/symbolic/integration/external.py +270 -0
- sage/symbolic/integration/integral.py +1115 -0
- sage/symbolic/maxima_wrapper.py +162 -0
- sage/symbolic/operators.py +267 -0
- sage/symbolic/random_tests.py +462 -0
- sage/symbolic/relation.py +1907 -0
- sage/symbolic/ring.cpython-314t-darwin.so +0 -0
- sage/symbolic/ring.pxd +5 -0
- sage/symbolic/ring.pyx +1396 -0
- sage/symbolic/subring.py +1025 -0
- sage/symbolic/symengine.py +19 -0
- sage/symbolic/tests.py +40 -0
- sage/symbolic/units.py +1470 -0
sage/interfaces/tides.py
ADDED
|
@@ -0,0 +1,938 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-symbolics
|
|
2
|
+
r"""
|
|
3
|
+
Interface to TIDES
|
|
4
|
+
|
|
5
|
+
This module contains tools to write the .c files needed for TIDES [TIDES]_ .
|
|
6
|
+
|
|
7
|
+
Tides is an integration engine based on the Taylor method. It is implemented
|
|
8
|
+
as a c library. The user must translate its initial value problem (IVP) into a
|
|
9
|
+
pair of .c files that will then be compiled and linked against the TIDES
|
|
10
|
+
library. The resulting binary will produce the desired output. The tools in this
|
|
11
|
+
module can be used to automate the generation of these files from the symbolic
|
|
12
|
+
expression of the differential equation.
|
|
13
|
+
|
|
14
|
+
::
|
|
15
|
+
|
|
16
|
+
# ************************************************************************
|
|
17
|
+
# Copyright (C) 2014 Miguel Marco <mmarco@unizar.es>, Marcos Rodriguez
|
|
18
|
+
# <marcos@uunizar.es>
|
|
19
|
+
#
|
|
20
|
+
# Distributed under the terms of the GNU General Public License (GPL):
|
|
21
|
+
#
|
|
22
|
+
# https://www.gnu.org/licenses/
|
|
23
|
+
# ************************************************************************
|
|
24
|
+
|
|
25
|
+
AUTHORS:
|
|
26
|
+
|
|
27
|
+
- Miguel Marco (06-2014) - Implementation of tides solver
|
|
28
|
+
|
|
29
|
+
- Marcos Rodriguez (06-2014) - Implementation of tides solver
|
|
30
|
+
|
|
31
|
+
- Alberto Abad (06-2014) - tides solver
|
|
32
|
+
|
|
33
|
+
- Roberto Barrio (06-2014) - tides solver
|
|
34
|
+
|
|
35
|
+
REFERENCES:
|
|
36
|
+
|
|
37
|
+
- [ABBR2012]_
|
|
38
|
+
|
|
39
|
+
- [TIDES]_
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
from sage.rings.real_mpfr import RealField
|
|
43
|
+
from sage.misc.lazy_import import lazy_import
|
|
44
|
+
lazy_import("sage.calculus.all", "symbolic_expression")
|
|
45
|
+
from sage.misc.flatten import flatten
|
|
46
|
+
from sage.ext.fast_callable import fast_callable
|
|
47
|
+
from sage.rings.semirings.non_negative_integer_semiring import NN
|
|
48
|
+
from sage.functions.log import log, exp
|
|
49
|
+
from sage.functions.other import floor, ceil
|
|
50
|
+
from sage.misc.functional import sqrt
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def subexpressions_list(f, pars=None):
|
|
54
|
+
"""
|
|
55
|
+
Construct the lists with the intermediate steps on the evaluation of the
|
|
56
|
+
function.
|
|
57
|
+
|
|
58
|
+
INPUT:
|
|
59
|
+
|
|
60
|
+
- ``f`` -- a symbolic function of several components
|
|
61
|
+
|
|
62
|
+
- ``pars`` -- list of the parameters that appear in the function
|
|
63
|
+
this should be the symbolic constants that appear in f but are not
|
|
64
|
+
arguments
|
|
65
|
+
|
|
66
|
+
OUTPUT:
|
|
67
|
+
|
|
68
|
+
- a list of the intermediate subexpressions that appear in the evaluation
|
|
69
|
+
of f.
|
|
70
|
+
|
|
71
|
+
- a list with the operations used to construct each of the subexpressions.
|
|
72
|
+
each element of this list is a tuple, formed by a string describing the
|
|
73
|
+
operation made, and the operands.
|
|
74
|
+
|
|
75
|
+
For the trigonometric functions, some extra expressions will be added.
|
|
76
|
+
These extra expressions will be used later to compute their derivatives.
|
|
77
|
+
|
|
78
|
+
EXAMPLES::
|
|
79
|
+
|
|
80
|
+
sage: from sage.interfaces.tides import subexpressions_list
|
|
81
|
+
sage: var('x,y')
|
|
82
|
+
(x, y)
|
|
83
|
+
sage: f(x,y) = [x^2+y, cos(x)/log(y)]
|
|
84
|
+
sage: subexpressions_list(f)
|
|
85
|
+
([x^2, x^2 + y, sin(x), cos(x), log(y), cos(x)/log(y)],
|
|
86
|
+
[('mul', x, x),
|
|
87
|
+
('add', y, x^2),
|
|
88
|
+
('sin', x),
|
|
89
|
+
('cos', x),
|
|
90
|
+
('log', y),
|
|
91
|
+
('div', log(y), cos(x))])
|
|
92
|
+
|
|
93
|
+
::
|
|
94
|
+
|
|
95
|
+
sage: f(a)=[cos(a), arctan(a)]
|
|
96
|
+
sage: from sage.interfaces.tides import subexpressions_list
|
|
97
|
+
sage: subexpressions_list(f)
|
|
98
|
+
([sin(a), cos(a), a^2, a^2 + 1, arctan(a)],
|
|
99
|
+
[('sin', a), ('cos', a), ('mul', a, a), ('add', 1, a^2), ('atan', a)])
|
|
100
|
+
|
|
101
|
+
::
|
|
102
|
+
|
|
103
|
+
sage: from sage.interfaces.tides import subexpressions_list
|
|
104
|
+
sage: var('s,b,r')
|
|
105
|
+
(s, b, r)
|
|
106
|
+
sage: f(t,x,y,z)= [s*(y-x),x*(r-z)-y,x*y-b*z]
|
|
107
|
+
sage: subexpressions_list(f,[s,b,r])
|
|
108
|
+
([-y,
|
|
109
|
+
x - y,
|
|
110
|
+
s*(x - y),
|
|
111
|
+
-s*(x - y),
|
|
112
|
+
-z,
|
|
113
|
+
r - z,
|
|
114
|
+
(r - z)*x,
|
|
115
|
+
-y,
|
|
116
|
+
(r - z)*x - y,
|
|
117
|
+
x*y,
|
|
118
|
+
b*z,
|
|
119
|
+
-b*z,
|
|
120
|
+
x*y - b*z],
|
|
121
|
+
[('mul', -1, y),
|
|
122
|
+
('add', -y, x),
|
|
123
|
+
('mul', x - y, s),
|
|
124
|
+
('mul', -1, s*(x - y)),
|
|
125
|
+
('mul', -1, z),
|
|
126
|
+
('add', -z, r),
|
|
127
|
+
('mul', x, r - z),
|
|
128
|
+
('mul', -1, y),
|
|
129
|
+
('add', -y, (r - z)*x),
|
|
130
|
+
('mul', y, x),
|
|
131
|
+
('mul', z, b),
|
|
132
|
+
('mul', -1, b*z),
|
|
133
|
+
('add', -b*z, x*y)])
|
|
134
|
+
|
|
135
|
+
::
|
|
136
|
+
|
|
137
|
+
sage: var('x, y')
|
|
138
|
+
(x, y)
|
|
139
|
+
sage: f(x,y)=[exp(x^2+sin(y))]
|
|
140
|
+
sage: from sage.interfaces.tides import *
|
|
141
|
+
sage: subexpressions_list(f)
|
|
142
|
+
([x^2, sin(y), cos(y), x^2 + sin(y), e^(x^2 + sin(y))],
|
|
143
|
+
[('mul', x, x),
|
|
144
|
+
('sin', y),
|
|
145
|
+
('cos', y),
|
|
146
|
+
('add', sin(y), x^2),
|
|
147
|
+
('exp', x^2 + sin(y))])
|
|
148
|
+
"""
|
|
149
|
+
from sage.functions.trig import sin, cos, arcsin, arctan, arccos
|
|
150
|
+
variables = f[0].arguments()
|
|
151
|
+
if not pars:
|
|
152
|
+
parameters = []
|
|
153
|
+
else:
|
|
154
|
+
parameters = pars
|
|
155
|
+
varpar = list(parameters) + list(variables)
|
|
156
|
+
F = symbolic_expression([i(*variables) for i in f]).function(*varpar)
|
|
157
|
+
lis = flatten([fast_callable(i,vars=varpar).op_list() for i in F], max_level=1)
|
|
158
|
+
stack = []
|
|
159
|
+
const = []
|
|
160
|
+
stackcomp = []
|
|
161
|
+
detail = []
|
|
162
|
+
for i in lis:
|
|
163
|
+
if i[0] == 'load_arg':
|
|
164
|
+
stack.append(varpar[i[1]])
|
|
165
|
+
elif i[0] == 'ipow':
|
|
166
|
+
if i[1] in NN:
|
|
167
|
+
basis = stack[-1]
|
|
168
|
+
for j in range(i[1]-1):
|
|
169
|
+
a = stack.pop(-1)
|
|
170
|
+
detail.append(('mul', a, basis))
|
|
171
|
+
stack.append(a*basis)
|
|
172
|
+
stackcomp.append(stack[-1])
|
|
173
|
+
else:
|
|
174
|
+
detail.append(('pow',stack[-1],i[1]))
|
|
175
|
+
stack[-1] = stack[-1]**i[1]
|
|
176
|
+
stackcomp.append(stack[-1])
|
|
177
|
+
|
|
178
|
+
elif i[0] == 'load_const':
|
|
179
|
+
const.append(i[1])
|
|
180
|
+
stack.append(i[1])
|
|
181
|
+
elif i == 'mul':
|
|
182
|
+
a = stack.pop(-1)
|
|
183
|
+
b = stack.pop(-1)
|
|
184
|
+
detail.append(('mul', a, b))
|
|
185
|
+
stack.append(a*b)
|
|
186
|
+
stackcomp.append(stack[-1])
|
|
187
|
+
|
|
188
|
+
elif i == 'div':
|
|
189
|
+
a = stack.pop(-1)
|
|
190
|
+
b = stack.pop(-1)
|
|
191
|
+
detail.append(('div', a, b))
|
|
192
|
+
stack.append(b/a)
|
|
193
|
+
stackcomp.append(stack[-1])
|
|
194
|
+
|
|
195
|
+
elif i == 'add':
|
|
196
|
+
a = stack.pop(-1)
|
|
197
|
+
b = stack.pop(-1)
|
|
198
|
+
detail.append(('add',a,b))
|
|
199
|
+
stack.append(a+b)
|
|
200
|
+
stackcomp.append(stack[-1])
|
|
201
|
+
|
|
202
|
+
elif i == 'pow':
|
|
203
|
+
a = stack.pop(-1)
|
|
204
|
+
b = stack.pop(-1)
|
|
205
|
+
detail.append(('pow', b, a))
|
|
206
|
+
stack.append(b**a)
|
|
207
|
+
stackcomp.append(stack[-1])
|
|
208
|
+
|
|
209
|
+
elif i[0] == 'py_call' and str(i[1]) == 'log':
|
|
210
|
+
a = stack.pop(-1)
|
|
211
|
+
detail.append(('log', a))
|
|
212
|
+
stack.append(log(a))
|
|
213
|
+
stackcomp.append(stack[-1])
|
|
214
|
+
|
|
215
|
+
elif i[0] == 'py_call' and str(i[1]) == 'exp':
|
|
216
|
+
a = stack.pop(-1)
|
|
217
|
+
detail.append(('exp', a))
|
|
218
|
+
stack.append(exp(a))
|
|
219
|
+
stackcomp.append(stack[-1])
|
|
220
|
+
|
|
221
|
+
elif i[0] == 'py_call' and str(i[1]) == 'sin':
|
|
222
|
+
a = stack.pop(-1)
|
|
223
|
+
detail.append(('sin', a))
|
|
224
|
+
detail.append(('cos', a))
|
|
225
|
+
stackcomp.append(sin(a))
|
|
226
|
+
stackcomp.append(cos(a))
|
|
227
|
+
stack.append(sin(a))
|
|
228
|
+
|
|
229
|
+
elif i[0] == 'py_call' and str(i[1]) == 'cos':
|
|
230
|
+
a = stack.pop(-1)
|
|
231
|
+
detail.append(('sin', a))
|
|
232
|
+
detail.append(('cos', a))
|
|
233
|
+
stackcomp.append(sin(a))
|
|
234
|
+
stackcomp.append(cos(a))
|
|
235
|
+
stack.append(cos(a))
|
|
236
|
+
|
|
237
|
+
elif i[0] == 'py_call' and str(i[1]) == 'tan':
|
|
238
|
+
a = stack.pop(-1)
|
|
239
|
+
b = sin(a)
|
|
240
|
+
c = cos(a)
|
|
241
|
+
detail.append(('sin', a))
|
|
242
|
+
detail.append(('cos', a))
|
|
243
|
+
detail.append(('div', b, c))
|
|
244
|
+
stackcomp.append(b)
|
|
245
|
+
stackcomp.append(c)
|
|
246
|
+
stackcomp.append(b/c)
|
|
247
|
+
stack.append(b/c)
|
|
248
|
+
|
|
249
|
+
elif i[0] == 'py_call' and str(i[1]) == 'arctan':
|
|
250
|
+
a = stack.pop(-1)
|
|
251
|
+
detail.append(('mul', a, a))
|
|
252
|
+
detail.append(('add', 1, a*a))
|
|
253
|
+
detail.append(('atan', a))
|
|
254
|
+
stackcomp.append(a*a)
|
|
255
|
+
stackcomp.append(1+a*a)
|
|
256
|
+
stackcomp.append(arctan(a))
|
|
257
|
+
stack.append(arctan(a))
|
|
258
|
+
|
|
259
|
+
elif i[0] == 'py_call' and str(i[1]) == 'arcsin':
|
|
260
|
+
a = stack.pop(-1)
|
|
261
|
+
detail.append(('mul', a, a))
|
|
262
|
+
detail.append(('mul', -1, a*a))
|
|
263
|
+
detail.append(('add', 1, -a*a))
|
|
264
|
+
detail.append(('pow', 1 - a*a, 0.5))
|
|
265
|
+
detail.append(('asin', a))
|
|
266
|
+
stackcomp.append(a*a)
|
|
267
|
+
stackcomp.append(-a*a)
|
|
268
|
+
stackcomp.append(1-a*a)
|
|
269
|
+
stackcomp.append(sqrt(1-a*a))
|
|
270
|
+
stackcomp.append(arcsin(a))
|
|
271
|
+
stack.append(arcsin(a))
|
|
272
|
+
|
|
273
|
+
elif i[0] == 'py_call' and str(i[1]) == 'arccos':
|
|
274
|
+
a = stack.pop(-1)
|
|
275
|
+
detail.append(('mul', a, a))
|
|
276
|
+
detail.append(('mul', -1, a*a))
|
|
277
|
+
detail.append(('add', 1, -a*a))
|
|
278
|
+
detail.append(('pow', 1 - a*a, 0.5))
|
|
279
|
+
detail.append(('mul', -1, sqrt(1-a*a)))
|
|
280
|
+
detail.append(('acos', a))
|
|
281
|
+
stackcomp.append(a*a)
|
|
282
|
+
stackcomp.append(-a*a)
|
|
283
|
+
stackcomp.append(1-a*a)
|
|
284
|
+
stackcomp.append(sqrt(1-a*a))
|
|
285
|
+
stackcomp.append(-sqrt(1-a*a))
|
|
286
|
+
stackcomp.append(arccos(a))
|
|
287
|
+
stack.append(arccos(a))
|
|
288
|
+
|
|
289
|
+
elif i[0] == 'py_call' and 'sqrt' in str(i[1]):
|
|
290
|
+
a = stack.pop(-1)
|
|
291
|
+
detail.append(('pow', a, 0.5))
|
|
292
|
+
stackcomp.append(sqrt(a))
|
|
293
|
+
stack.append(sqrt(a))
|
|
294
|
+
|
|
295
|
+
elif i == 'neg':
|
|
296
|
+
a = stack.pop(-1)
|
|
297
|
+
detail.append(('mul', -1, a))
|
|
298
|
+
stack.append(-a)
|
|
299
|
+
stackcomp.append(-a)
|
|
300
|
+
|
|
301
|
+
return stackcomp,detail
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def remove_repeated(l1, l2):
|
|
305
|
+
"""
|
|
306
|
+
Given two lists, remove the repeated elements in l1, and the elements
|
|
307
|
+
in l2 that are on the same position.
|
|
308
|
+
positions.
|
|
309
|
+
|
|
310
|
+
EXAMPLES::
|
|
311
|
+
|
|
312
|
+
sage: from sage.interfaces.tides import (subexpressions_list, remove_repeated)
|
|
313
|
+
sage: f(a)=[1 + a^2, arcsin(a)]
|
|
314
|
+
sage: l1, l2 = subexpressions_list(f)
|
|
315
|
+
sage: l1, l2
|
|
316
|
+
([a^2, a^2 + 1, a^2, -a^2, -a^2 + 1, sqrt(-a^2 + 1), arcsin(a)],
|
|
317
|
+
[('mul', a, a),
|
|
318
|
+
('add', 1, a^2),
|
|
319
|
+
('mul', a, a),
|
|
320
|
+
('mul', -1, a^2),
|
|
321
|
+
('add', 1, -a^2),
|
|
322
|
+
('pow', -a^2 + 1, 0.5),
|
|
323
|
+
('asin', a)])
|
|
324
|
+
sage: remove_repeated(l1, l2)
|
|
325
|
+
sage: l1, l2
|
|
326
|
+
([a^2, a^2 + 1, -a^2, -a^2 + 1, sqrt(-a^2 + 1), arcsin(a)],
|
|
327
|
+
[('mul', a, a),
|
|
328
|
+
('add', 1, a^2),
|
|
329
|
+
('mul', -1, a^2),
|
|
330
|
+
('add', 1, -a^2),
|
|
331
|
+
('pow', -a^2 + 1, 0.5),
|
|
332
|
+
('asin', a)])
|
|
333
|
+
"""
|
|
334
|
+
for i in range(len(l1)-1):
|
|
335
|
+
j = i+1
|
|
336
|
+
while j < len(l1):
|
|
337
|
+
if str(l1[j]) == str(l1[i]):
|
|
338
|
+
l1.pop(j)
|
|
339
|
+
l2.pop(j)
|
|
340
|
+
else:
|
|
341
|
+
j += 1
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def remove_constants(l1, l2):
|
|
345
|
+
"""
|
|
346
|
+
Given two lists, remove the entries in the first that are real constants,
|
|
347
|
+
and also the corresponding elements in the second one.
|
|
348
|
+
|
|
349
|
+
EXAMPLES::
|
|
350
|
+
|
|
351
|
+
sage: from sage.interfaces.tides import subexpressions_list, remove_constants
|
|
352
|
+
sage: f(a)=[1+cos(7)*a]
|
|
353
|
+
sage: l1, l2 = subexpressions_list(f)
|
|
354
|
+
sage: l1, l2
|
|
355
|
+
([sin(7), cos(7), a*cos(7), a*cos(7) + 1],
|
|
356
|
+
[('sin', 7), ('cos', 7), ('mul', cos(7), a), ('add', 1, a*cos(7))])
|
|
357
|
+
sage: remove_constants(l1,l2)
|
|
358
|
+
sage: l1, l2
|
|
359
|
+
([a*cos(7), a*cos(7) + 1], [('mul', cos(7), a), ('add', 1, a*cos(7))])
|
|
360
|
+
"""
|
|
361
|
+
i = 0
|
|
362
|
+
while i < len(l1):
|
|
363
|
+
if l1[i] in RealField():
|
|
364
|
+
l1.pop(i)
|
|
365
|
+
l2.pop(i)
|
|
366
|
+
else:
|
|
367
|
+
i += 1
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def genfiles_mintides(integrator, driver, f, ics, initial, final, delta,
|
|
371
|
+
tolrel=1e-16, tolabs=1e-16, output=''):
|
|
372
|
+
r"""
|
|
373
|
+
Generate the needed files for the min_tides library.
|
|
374
|
+
|
|
375
|
+
INPUT:
|
|
376
|
+
|
|
377
|
+
- ``integrator`` -- the name of the integrator file
|
|
378
|
+
|
|
379
|
+
- ``driver`` -- the name of the driver file
|
|
380
|
+
|
|
381
|
+
- ``f`` -- the function that determines the differential equation
|
|
382
|
+
|
|
383
|
+
- ``ics`` -- list or tuple with the initial conditions
|
|
384
|
+
|
|
385
|
+
- ``initial`` -- the initial time for the integration
|
|
386
|
+
|
|
387
|
+
- ``final`` -- the final time for the integration
|
|
388
|
+
|
|
389
|
+
- ``delta`` -- the step of the output
|
|
390
|
+
|
|
391
|
+
- ``tolrel`` -- the relative tolerance
|
|
392
|
+
|
|
393
|
+
- ``tolabs`` -- the absolute tolerance
|
|
394
|
+
|
|
395
|
+
- ``output`` -- the name of the file that the compiled integrator will write to
|
|
396
|
+
|
|
397
|
+
This function creates two files, integrator and driver, that can be used
|
|
398
|
+
later with the min_tides library [TIDES]_.
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
TESTS::
|
|
402
|
+
|
|
403
|
+
sage: from sage.interfaces.tides import genfiles_mintides
|
|
404
|
+
sage: import os
|
|
405
|
+
sage: import shutil
|
|
406
|
+
sage: from sage.misc.temporary_file import tmp_dir
|
|
407
|
+
sage: tempdir = tmp_dir()
|
|
408
|
+
sage: intfile = os.path.join(tempdir, 'integrator.c')
|
|
409
|
+
sage: drfile = os.path.join(tempdir ,'driver.c')
|
|
410
|
+
sage: var('t,x,y,X,Y')
|
|
411
|
+
(t, x, y, X, Y)
|
|
412
|
+
sage: f(t,x,y,X,Y)=[X, Y, -x/(x^2+y^2)^(3/2), -y/(x^2+y^2)^(3/2)]
|
|
413
|
+
sage: genfiles_mintides(intfile, drfile, f, [1,0, 0, 0.2], 0, 10, 0.1, output = 'out')
|
|
414
|
+
sage: fileint = open(intfile)
|
|
415
|
+
sage: l = fileint.readlines()
|
|
416
|
+
sage: fileint.close()
|
|
417
|
+
sage: l[5]
|
|
418
|
+
' #include "minc_tides.h"\n'
|
|
419
|
+
sage: l[15]
|
|
420
|
+
' double XX[TT+1][MO+1];\n'
|
|
421
|
+
sage: l[25]
|
|
422
|
+
'\n'
|
|
423
|
+
sage: l[35]
|
|
424
|
+
'\t\tXX[1][i+1] = XX[3][i] / (i+1.0);\n'
|
|
425
|
+
sage: filedr = open(drfile)
|
|
426
|
+
sage: l = filedr.readlines()
|
|
427
|
+
sage: filedr.close()
|
|
428
|
+
sage: l[6]
|
|
429
|
+
' #include "minc_tides.h"\n'
|
|
430
|
+
sage: l[15]
|
|
431
|
+
' double tolrel, tolabs, tini, tend, dt;\n'
|
|
432
|
+
sage: l[25]
|
|
433
|
+
'\ttolrel = 9.9999999999999998e-17 ;\n'
|
|
434
|
+
sage: shutil.rmtree(tempdir)
|
|
435
|
+
|
|
436
|
+
Check that issue :issue:`17179` is fixed (handle expressions like `\\pi`)::
|
|
437
|
+
|
|
438
|
+
sage: from sage.interfaces.tides import genfiles_mintides
|
|
439
|
+
sage: import os
|
|
440
|
+
sage: import shutil
|
|
441
|
+
sage: from sage.misc.temporary_file import tmp_dir
|
|
442
|
+
sage: tempdir = tmp_dir()
|
|
443
|
+
sage: intfile = os.path.join(tempdir, 'integrator.c')
|
|
444
|
+
sage: drfile = os.path.join(tempdir ,'driver.c')
|
|
445
|
+
sage: var('t,x,y,X,Y')
|
|
446
|
+
(t, x, y, X, Y)
|
|
447
|
+
sage: f(t,x,y,X,Y)=[X, Y, -x/(x^2+y^2)^(3/2), -y/(x^2+y^2)^(3/2)]
|
|
448
|
+
sage: genfiles_mintides(intfile, drfile, f, [pi, 0, 0, 0.2], 0, 10, 0.1, output = 'out')
|
|
449
|
+
sage: fileint = open(intfile)
|
|
450
|
+
sage: l = fileint.readlines()
|
|
451
|
+
sage: fileint.close()
|
|
452
|
+
sage: l[30]
|
|
453
|
+
'\t\tXX[8][i] = pow_mc_c(XX[7],-1.5000000000000000,XX[8], i);\n'
|
|
454
|
+
sage: filedr = open(drfile)
|
|
455
|
+
sage: l = filedr.readlines()
|
|
456
|
+
sage: filedr.close()
|
|
457
|
+
sage: l[18]
|
|
458
|
+
' \tv[0] = 3.1415926535897931 ; \n'
|
|
459
|
+
sage: shutil.rmtree(tempdir)
|
|
460
|
+
"""
|
|
461
|
+
RR = RealField()
|
|
462
|
+
|
|
463
|
+
l1, l2 = subexpressions_list(f)
|
|
464
|
+
|
|
465
|
+
remove_repeated(l1, l2)
|
|
466
|
+
remove_constants(l1, l2)
|
|
467
|
+
l0 = [str(l) for l in l1]
|
|
468
|
+
# generate the corresponding c lines
|
|
469
|
+
|
|
470
|
+
l3 = []
|
|
471
|
+
var = f[0].arguments()
|
|
472
|
+
lv = [str(v) for v in var]
|
|
473
|
+
for i in l2:
|
|
474
|
+
oper = i[0]
|
|
475
|
+
if oper in ["log", "exp", "sin", "cos"]:
|
|
476
|
+
a = i[1]
|
|
477
|
+
if a in var:
|
|
478
|
+
l3.append((oper, 'XX[{}]'.format(lv.index(str(a)))))
|
|
479
|
+
elif a in l1:
|
|
480
|
+
l3.append((oper, 'XX[{}]'.format(l0.index(str(a))+len(var))))
|
|
481
|
+
|
|
482
|
+
else:
|
|
483
|
+
a = i[1]
|
|
484
|
+
b = i[2]
|
|
485
|
+
consta = False
|
|
486
|
+
constb = False
|
|
487
|
+
|
|
488
|
+
if str(a) in lv:
|
|
489
|
+
aa = 'XX[{}]'.format(lv.index(str(a)))
|
|
490
|
+
elif str(a) in l0:
|
|
491
|
+
aa = 'XX[{}]'.format(l0.index(str(a))+len(var))
|
|
492
|
+
else:
|
|
493
|
+
consta = True
|
|
494
|
+
aa = RR(a).str()
|
|
495
|
+
if str(b) in lv:
|
|
496
|
+
bb = 'XX[{}]'.format(lv.index(str(b)))
|
|
497
|
+
elif str(b) in l0:
|
|
498
|
+
bb = 'XX[{}]'.format(l0.index(str(b))+len(var))
|
|
499
|
+
else:
|
|
500
|
+
constb = True
|
|
501
|
+
bb = RR(b).str()
|
|
502
|
+
if consta:
|
|
503
|
+
oper += '_c'
|
|
504
|
+
if not oper == 'div':
|
|
505
|
+
bb, aa = aa, bb
|
|
506
|
+
elif constb:
|
|
507
|
+
oper += '_c'
|
|
508
|
+
l3.append((oper, aa, bb))
|
|
509
|
+
|
|
510
|
+
n = len(var)
|
|
511
|
+
res = []
|
|
512
|
+
for i in range(len(l3)):
|
|
513
|
+
el = l3[i]
|
|
514
|
+
string = "XX[{}][i] = ".format(i + n)
|
|
515
|
+
if el[0] == 'add':
|
|
516
|
+
string += el[1] + "[i] + " + el[2] + "[i];"
|
|
517
|
+
elif el[0] == 'add_c':
|
|
518
|
+
string += "(i==0)? {}+".format(el[2]) + el[1] + "[0] : " + el[1] + "[i];"
|
|
519
|
+
elif el[0] == 'mul':
|
|
520
|
+
string += "mul_mc("+el[1]+","+el[2]+",i);"
|
|
521
|
+
elif el[0] == 'mul_c':
|
|
522
|
+
string += el[2] + "*" + el[1] + "[i];"
|
|
523
|
+
elif el[0] == 'pow_c':
|
|
524
|
+
string += "pow_mc_c("+el[1]+","+el[2]+",XX[{}], i);".format(i+n)
|
|
525
|
+
elif el[0] == 'div':
|
|
526
|
+
string += "div_mc("+el[2]+","+el[1]+",XX[{}], i);".format(i+n)
|
|
527
|
+
elif el[0] == 'div_c':
|
|
528
|
+
string += "inv_mc("+el[2]+","+el[1]+",XX[{}], i);".format(i+n)
|
|
529
|
+
elif el[0] == 'log':
|
|
530
|
+
string += "log_mc(" + el[1] + ",XX[{}], i);".format(i+n)
|
|
531
|
+
elif el[0] == 'exp':
|
|
532
|
+
string += "exp_mc(" + el[1] + ",XX[{}], i);".format(i+n)
|
|
533
|
+
elif el[0] == 'sin':
|
|
534
|
+
string += "sin_mc(" + el[1] + ",XX[{}], i);".format(i+n+1)
|
|
535
|
+
elif el[0] == 'cos':
|
|
536
|
+
string += "cos_mc(" + el[1] + ",XX[{}], i);".format(i+n-1)
|
|
537
|
+
|
|
538
|
+
res.append(string)
|
|
539
|
+
|
|
540
|
+
l0 = lv + l0
|
|
541
|
+
indices = [l0.index(str(i(*var))) + n for i in f]
|
|
542
|
+
for i in range(1, n):
|
|
543
|
+
res.append("XX[{}][i+1] = XX[{}][i] / (i+1.0);".format(i,indices[i-1]-n))
|
|
544
|
+
|
|
545
|
+
code = res
|
|
546
|
+
|
|
547
|
+
outfile = open(integrator, 'a')
|
|
548
|
+
auxstring = """
|
|
549
|
+
/****************************************************************************
|
|
550
|
+
This file has been created by Sage for its use with TIDES
|
|
551
|
+
*****************************************************************************/
|
|
552
|
+
|
|
553
|
+
#include "minc_tides.h"
|
|
554
|
+
|
|
555
|
+
void mincseries(double t,double *v, double *p, double **XVAR,int ORDER, int MO)
|
|
556
|
+
{
|
|
557
|
+
int VAR,PAR,TT,i,j, inext;
|
|
558
|
+
"""
|
|
559
|
+
outfile.write(auxstring)
|
|
560
|
+
|
|
561
|
+
outfile.write("\tVAR = {};\n".format(n))
|
|
562
|
+
outfile.write("\tPAR = {};\n".format(0))
|
|
563
|
+
outfile.write("\tTT = {};\n".format(len(res)))
|
|
564
|
+
|
|
565
|
+
auxstring = """
|
|
566
|
+
|
|
567
|
+
double XX[TT+1][MO+1];
|
|
568
|
+
|
|
569
|
+
for(j=0; j<=TT; j++)
|
|
570
|
+
for(i=0; i<=ORDER; i++)
|
|
571
|
+
XX[j][i] = 0.e0;
|
|
572
|
+
XX[0][0] = t;
|
|
573
|
+
XX[0][1] = 1.e0;
|
|
574
|
+
for(i=1;i<=VAR;i++) {
|
|
575
|
+
XX[i][0] = v[i-1];
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
for(i=0;i<ORDER;i++) {
|
|
579
|
+
"""
|
|
580
|
+
outfile.write(auxstring)
|
|
581
|
+
outfile.writelines(["\t\t" + i + "\n" for i in code])
|
|
582
|
+
|
|
583
|
+
outfile.write('\t}\n')
|
|
584
|
+
outfile.write('\n')
|
|
585
|
+
outfile.write('\tfor(j=0; j<=VAR; j++)\n')
|
|
586
|
+
outfile.write('\t\tfor(i=0; i<=ORDER; i++)\n')
|
|
587
|
+
outfile.write('\t\t\tXVAR[i][j] = XX[j][i];\n')
|
|
588
|
+
outfile.write('}\n')
|
|
589
|
+
outfile.write('\n')
|
|
590
|
+
|
|
591
|
+
outfile = open(driver, 'a')
|
|
592
|
+
|
|
593
|
+
auxstring = """
|
|
594
|
+
/****************************************************************************
|
|
595
|
+
Driver file of the minc_tides program
|
|
596
|
+
This file has been automatically created by Sage
|
|
597
|
+
*****************************************************************************/
|
|
598
|
+
|
|
599
|
+
#include "minc_tides.h"
|
|
600
|
+
|
|
601
|
+
int main() {
|
|
602
|
+
|
|
603
|
+
int i, VARS, PARS;
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
VARS = %s ;
|
|
607
|
+
PARS = 1;
|
|
608
|
+
double tolrel, tolabs, tini, tend, dt;
|
|
609
|
+
double v[VARS], p[PARS];
|
|
610
|
+
|
|
611
|
+
""" % (n-1)
|
|
612
|
+
outfile.write(auxstring)
|
|
613
|
+
for i in range(len(ics)):
|
|
614
|
+
outfile.write('\tv[{}] = {} ; \n'.format(i, RR(ics[i]).str()))
|
|
615
|
+
outfile.write('\ttini = {} ;\n'.format(RR(initial).str()))
|
|
616
|
+
outfile.write('\ttend = {} ;\n'.format(RR(final).str()))
|
|
617
|
+
outfile.write('\tdt = {} ;\n'.format(RR(delta).str()))
|
|
618
|
+
outfile.write('\ttolrel = {} ;\n'.format(RR(tolrel).str()))
|
|
619
|
+
outfile.write('\ttolabs = {} ;\n'.format(RR(tolabs).str()))
|
|
620
|
+
outfile.write('\textern char ofname[500];')
|
|
621
|
+
outfile.write('\tstrcpy(ofname, "' + output + '");\n')
|
|
622
|
+
outfile.write('\tminc_tides(v,VARS,p,PARS,tini,tend,dt,tolrel,tolabs);\n')
|
|
623
|
+
outfile.write('\treturn 0; \n }')
|
|
624
|
+
outfile.close()
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
def genfiles_mpfr(integrator, driver, f, ics, initial, final, delta,
|
|
628
|
+
parameters=None, parameter_values=None, dig=20, tolrel=1e-16,
|
|
629
|
+
tolabs=1e-16, output=''):
|
|
630
|
+
r"""
|
|
631
|
+
Generate the needed files for the mpfr module of the tides library.
|
|
632
|
+
|
|
633
|
+
INPUT:
|
|
634
|
+
|
|
635
|
+
- ``integrator`` -- the name of the integrator file
|
|
636
|
+
|
|
637
|
+
- ``driver`` -- the name of the driver file
|
|
638
|
+
|
|
639
|
+
- ``f`` -- the function that determines the differential equation
|
|
640
|
+
|
|
641
|
+
- ``ics`` -- list or tuple with the initial conditions
|
|
642
|
+
|
|
643
|
+
- ``initial`` -- the initial time for the integration
|
|
644
|
+
|
|
645
|
+
- ``final`` -- the final time for the integration
|
|
646
|
+
|
|
647
|
+
- ``delta`` -- the step of the output
|
|
648
|
+
|
|
649
|
+
- ``parameters`` -- the variables inside the function that should be treated
|
|
650
|
+
as parameters
|
|
651
|
+
|
|
652
|
+
- ``parameter_values`` -- the values of the parameters for the particular
|
|
653
|
+
initial value problem
|
|
654
|
+
|
|
655
|
+
- ``dig`` -- the number of digits of precision that will be used in the integration
|
|
656
|
+
|
|
657
|
+
- ``tolrel`` -- the relative tolerance
|
|
658
|
+
|
|
659
|
+
- ``tolabs`` -- the absolute tolerance
|
|
660
|
+
|
|
661
|
+
- ``output`` -- the name of the file that the compiled integrator will write to
|
|
662
|
+
|
|
663
|
+
This function creates two files, integrator and driver, that can be used
|
|
664
|
+
later with the tides library ([TIDES]_).
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
TESTS::
|
|
668
|
+
|
|
669
|
+
sage: from tempfile import mkdtemp
|
|
670
|
+
sage: from sage.interfaces.tides import genfiles_mpfr
|
|
671
|
+
sage: import os
|
|
672
|
+
sage: import shutil
|
|
673
|
+
sage: from sage.misc.temporary_file import tmp_dir
|
|
674
|
+
sage: tempdir = tmp_dir()
|
|
675
|
+
sage: intfile = os.path.join(tempdir, 'integrator.c')
|
|
676
|
+
sage: drfile = os.path.join(tempdir ,'driver.c')
|
|
677
|
+
sage: var('t,x,y,X,Y')
|
|
678
|
+
(t, x, y, X, Y)
|
|
679
|
+
sage: f(t,x,y,X,Y)=[X, Y, -x/(x^2+y^2)^(3/2), -y/(x^2+y^2)^(3/2)]
|
|
680
|
+
sage: genfiles_mpfr(intfile, drfile, f, [1,0, 0, 0.2], 0, 10, 0.1, output = 'out', dig = 50)
|
|
681
|
+
sage: fileint = open(intfile)
|
|
682
|
+
sage: l = fileint.readlines()
|
|
683
|
+
sage: fileint.close()
|
|
684
|
+
sage: l[5]
|
|
685
|
+
' #include "mp_tides.h"\n'
|
|
686
|
+
sage: l[15]
|
|
687
|
+
'\tstatic int PARAMETERS = 0;\n'
|
|
688
|
+
sage: l[25]
|
|
689
|
+
'\t\tmpfrts_var_t(itd, link[5], var[3], i);\n'
|
|
690
|
+
sage: l[30]
|
|
691
|
+
'\t\tmpfrts_pow_t_c(itd, link[2], "-1.500000000000000000000000000000000000000000000000000", link[3], i);\n'
|
|
692
|
+
sage: l[35]
|
|
693
|
+
'\n'
|
|
694
|
+
sage: l[36]
|
|
695
|
+
' }\n'
|
|
696
|
+
sage: l[37]
|
|
697
|
+
' write_mp_solution();\n'
|
|
698
|
+
sage: filedr = open(drfile)
|
|
699
|
+
sage: l = filedr.readlines()
|
|
700
|
+
sage: filedr.close()
|
|
701
|
+
sage: l[6]
|
|
702
|
+
' #include "mpfr.h"\n'
|
|
703
|
+
sage: l[16]
|
|
704
|
+
' int nfun = 0;\n'
|
|
705
|
+
sage: l[26]
|
|
706
|
+
'\tmpfr_set_str(v[2], "0.000000000000000000000000000000000000000000000000000", 10, TIDES_RND);\n'
|
|
707
|
+
sage: l[30]
|
|
708
|
+
'\tmpfr_init2(tolabs, TIDES_PREC); \n'
|
|
709
|
+
sage: l[34]
|
|
710
|
+
'\tmpfr_init2(tini, TIDES_PREC); \n'
|
|
711
|
+
sage: l[40]
|
|
712
|
+
'\tmp_tides_delta(function_iteration, NULL, nvar, npar, nfun, v, p, tini, dt, nipt, tolrel, tolabs, NULL, fd);\n'
|
|
713
|
+
sage: shutil.rmtree(tempdir)
|
|
714
|
+
|
|
715
|
+
Check that issue :issue:`17179` is fixed (handle expressions like `\\pi`)::
|
|
716
|
+
|
|
717
|
+
sage: from sage.interfaces.tides import genfiles_mpfr
|
|
718
|
+
sage: import os
|
|
719
|
+
sage: import shutil
|
|
720
|
+
sage: from sage.misc.temporary_file import tmp_dir
|
|
721
|
+
sage: tempdir = tmp_dir()
|
|
722
|
+
sage: intfile = os.path.join(tempdir, 'integrator.c')
|
|
723
|
+
sage: drfile = os.path.join(tempdir ,'driver.c')
|
|
724
|
+
sage: var('t,x,y,X,Y')
|
|
725
|
+
(t, x, y, X, Y)
|
|
726
|
+
sage: f(t,x,y,X,Y)=[X, Y, -x/(x^2+y^2)^(3/2), -y/(x^2+y^2)^(3/2)]
|
|
727
|
+
sage: genfiles_mpfr(intfile, drfile, f, [pi, 0, 0, 0.2], 0, 10, 0.1, output = 'out', dig = 50)
|
|
728
|
+
sage: fileint = open(intfile)
|
|
729
|
+
sage: l = fileint.readlines()
|
|
730
|
+
sage: fileint.close()
|
|
731
|
+
sage: l[30]
|
|
732
|
+
'\t\tmpfrts_pow_t_c(itd, link[2], "-1.500000000000000000000000000000000000000000000000000", link[3], i);\n'
|
|
733
|
+
sage: filedr = open(drfile)
|
|
734
|
+
sage: l = filedr.readlines()
|
|
735
|
+
sage: filedr.close()
|
|
736
|
+
sage: l[24]
|
|
737
|
+
'\tmpfr_set_str(v[0], "3.141592653589793238462643383279502884197169399375101", 10, TIDES_RND);\n'
|
|
738
|
+
sage: shutil.rmtree(tempdir)
|
|
739
|
+
"""
|
|
740
|
+
if parameters is None:
|
|
741
|
+
parameters = []
|
|
742
|
+
if parameter_values is None:
|
|
743
|
+
parameter_values = []
|
|
744
|
+
RR = RealField(ceil(dig * 3.3219))
|
|
745
|
+
l1, l2 = subexpressions_list(f, parameters)
|
|
746
|
+
remove_repeated(l1, l2)
|
|
747
|
+
remove_constants(l1, l2)
|
|
748
|
+
l3 = []
|
|
749
|
+
var = f[0].arguments()
|
|
750
|
+
l0 = [str(l) for l in l1]
|
|
751
|
+
lv = [str(v) for v in var]
|
|
752
|
+
lp = [str(p) for p in parameters]
|
|
753
|
+
for i in l2:
|
|
754
|
+
oper = i[0]
|
|
755
|
+
if oper in ["log", "exp", "sin", "cos", "atan", "asin", "acos"]:
|
|
756
|
+
a = i[1]
|
|
757
|
+
if str(a) in lv:
|
|
758
|
+
l3.append((oper, 'var[{}]'.format(lv.index(str(a)))))
|
|
759
|
+
elif str(a) in lp:
|
|
760
|
+
l3.append((oper, 'par[{}]'.format(lp.index(str(a)))))
|
|
761
|
+
else:
|
|
762
|
+
l3.append((oper, 'link[{}]'.format(l0.index(str(a)))))
|
|
763
|
+
|
|
764
|
+
else:
|
|
765
|
+
a = i[1]
|
|
766
|
+
b = i[2]
|
|
767
|
+
sa = str(a)
|
|
768
|
+
sb = str(b)
|
|
769
|
+
consta = False
|
|
770
|
+
constb = False
|
|
771
|
+
|
|
772
|
+
if sa in lv:
|
|
773
|
+
aa = 'var[{}]'.format(lv.index(sa))
|
|
774
|
+
elif sa in l0:
|
|
775
|
+
aa = 'link[{}]'.format(l0.index(sa))
|
|
776
|
+
elif sa in lp:
|
|
777
|
+
aa = 'par[{}]'.format(lp.index(sa))
|
|
778
|
+
else:
|
|
779
|
+
consta = True
|
|
780
|
+
aa = RR(a).str()
|
|
781
|
+
if sb in lv:
|
|
782
|
+
bb = 'var[{}]'.format(lv.index(sb))
|
|
783
|
+
elif sb in l0:
|
|
784
|
+
bb = 'link[{}]'.format(l0.index(sb))
|
|
785
|
+
elif sb in lp:
|
|
786
|
+
bb = 'par[{}]'.format(lp.index(sb))
|
|
787
|
+
else:
|
|
788
|
+
constb = True
|
|
789
|
+
bb = RR(b).str()
|
|
790
|
+
if consta:
|
|
791
|
+
oper += '_c'
|
|
792
|
+
if not oper == 'div':
|
|
793
|
+
bb, aa = aa,bb
|
|
794
|
+
elif constb:
|
|
795
|
+
oper += '_c'
|
|
796
|
+
l3.append((oper, aa, bb))
|
|
797
|
+
|
|
798
|
+
n = len(var)
|
|
799
|
+
code = []
|
|
800
|
+
|
|
801
|
+
l0 = lv + l0
|
|
802
|
+
indices = [l0.index(str(i(*var))) + n for i in f]
|
|
803
|
+
for i in range(1, n):
|
|
804
|
+
aux = indices[i - 1] - n
|
|
805
|
+
if aux < n:
|
|
806
|
+
code.append(f'mpfrts_var_t(itd, var[{aux}], var[{i}], i);')
|
|
807
|
+
else:
|
|
808
|
+
code.append(f'mpfrts_var_t(itd, link[{aux-n}], var[{i}], i);')
|
|
809
|
+
|
|
810
|
+
for i in range(len(l3)):
|
|
811
|
+
el = l3[i]
|
|
812
|
+
string = "mpfrts_"
|
|
813
|
+
if el[0] == 'add':
|
|
814
|
+
string += 'add_t(itd, ' + el[1] + ', ' + el[2] + f', link[{i}], i);'
|
|
815
|
+
elif el[0] == 'add_c':
|
|
816
|
+
string += 'add_t_c(itd, "' + el[2] + '", ' + el[1] + f', link[{i}], i);'
|
|
817
|
+
elif el[0] == 'mul':
|
|
818
|
+
string += 'mul_t(itd, ' + el[1] + ', ' + el[2] + f', link[{i}], i);'
|
|
819
|
+
elif el[0] == 'mul_c':
|
|
820
|
+
string += 'mul_t_c(itd, "' + el[2] + '", ' + el[1] + f', link[{i}], i);'
|
|
821
|
+
elif el[0] == 'pow_c':
|
|
822
|
+
string += 'pow_t_c(itd, ' + el[1] + ', "' + el[2] + f'", link[{i}], i);'
|
|
823
|
+
elif el[0] == 'div':
|
|
824
|
+
string += 'div_t(itd, ' + el[2] + ', ' + el[1] + f', link[{i}], i);'
|
|
825
|
+
elif el[0] == 'div_c':
|
|
826
|
+
string += 'div_t_cv(itd, "' + el[2] + '", ' + el[1] + f', link[{i}], i);'
|
|
827
|
+
elif el[0] == 'log':
|
|
828
|
+
string += 'log_t(itd, ' + el[1] + f', link[{i}], i);'
|
|
829
|
+
elif el[0] == 'exp':
|
|
830
|
+
string += 'exp_t(itd, ' + el[1] + f', link[{i}], i);'
|
|
831
|
+
elif el[0] == 'sin':
|
|
832
|
+
string += 'sin_t(itd, ' + el[1] + f', link[{i+1}], link[{i}], i);'
|
|
833
|
+
elif el[0] == 'cos':
|
|
834
|
+
string += 'cos_t(itd, ' + el[1] + f', link[{i-1}], link[{i}], i);'
|
|
835
|
+
elif el[0] == 'atan':
|
|
836
|
+
indarg = l0.index(str(1+l2[i][1]**2))-n
|
|
837
|
+
string += 'atan_t(itd, ' + el[1] + f', link[{indarg}], link[{i}], i);'
|
|
838
|
+
elif el[0] == 'asin':
|
|
839
|
+
indarg = l0.index(str(sqrt(1-l2[i][1]**2)))-n
|
|
840
|
+
string += 'asin_t(itd, ' + el[1] + f', link[{indarg}], link[{i}], i);'
|
|
841
|
+
elif el[0] == 'acos':
|
|
842
|
+
indarg = l0.index(str(-sqrt(1-l2[i][1]**2)))-n
|
|
843
|
+
string += 'acos_t(itd, ' + el[1] + f', link[{indarg}], link[{i}], i);'
|
|
844
|
+
code.append(string)
|
|
845
|
+
|
|
846
|
+
VAR = n - 1
|
|
847
|
+
PAR = len(parameters)
|
|
848
|
+
TT = len(code) + 1 - VAR
|
|
849
|
+
|
|
850
|
+
auxstring1 = """
|
|
851
|
+
/****************************************************************************
|
|
852
|
+
This file has been created by Sage for its use with TIDES
|
|
853
|
+
*****************************************************************************/
|
|
854
|
+
|
|
855
|
+
#include "mp_tides.h"
|
|
856
|
+
|
|
857
|
+
long function_iteration(iteration_data *itd, mpfr_t t, mpfr_t v[], mpfr_t p[], int ORDER, mpfr_t *cvfd)
|
|
858
|
+
{
|
|
859
|
+
|
|
860
|
+
int i;
|
|
861
|
+
int NCONST = 0;
|
|
862
|
+
mpfr_t ct[0];
|
|
863
|
+
"""
|
|
864
|
+
|
|
865
|
+
auxstring2 = """
|
|
866
|
+
}
|
|
867
|
+
write_mp_solution();
|
|
868
|
+
clear_vpl();
|
|
869
|
+
clear_cts();
|
|
870
|
+
return NUM_COLUMNS;
|
|
871
|
+
}
|
|
872
|
+
"""
|
|
873
|
+
|
|
874
|
+
with open(integrator, 'a') as outfile:
|
|
875
|
+
outfile.write(auxstring1)
|
|
876
|
+
|
|
877
|
+
outfile.write(f"\n\tstatic int VARIABLES = {VAR};\n")
|
|
878
|
+
outfile.write(f"\tstatic int PARAMETERS = {PAR};\n")
|
|
879
|
+
outfile.write(f"\tstatic int LINKS = {TT};\n")
|
|
880
|
+
outfile.write('\tstatic int FUNCTIONS = 0;\n')
|
|
881
|
+
outfile.write('\tstatic int POS_FUNCTIONS[1] = {0};\n')
|
|
882
|
+
outfile.write('\n\tinitialize_mp_case();\n')
|
|
883
|
+
outfile.write('\n\tfor(i=0; i<=ORDER; i++) {\n')
|
|
884
|
+
for i in code:
|
|
885
|
+
outfile.write('\t\t' + i + '\n')
|
|
886
|
+
|
|
887
|
+
outfile.write(auxstring2)
|
|
888
|
+
|
|
889
|
+
npar = len(parameter_values)
|
|
890
|
+
outfile = open(driver, 'a')
|
|
891
|
+
|
|
892
|
+
auxstring3 = """
|
|
893
|
+
/****************************************************************************
|
|
894
|
+
Driver file of the mp_tides program
|
|
895
|
+
This file has been created automatically by Sage
|
|
896
|
+
*****************************************************************************/
|
|
897
|
+
|
|
898
|
+
#include "mpfr.h"
|
|
899
|
+
#include "mp_tides.h"
|
|
900
|
+
long function_iteration(iteration_data *itd, mpfr_t t, mpfr_t v[], mpfr_t p[], int ORDER, mpfr_t *cvfd);
|
|
901
|
+
|
|
902
|
+
int main() {
|
|
903
|
+
|
|
904
|
+
int i;
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
int nfun = 0;
|
|
909
|
+
"""
|
|
910
|
+
outfile.write(auxstring3)
|
|
911
|
+
outfile.write('\tset_precision_digits({});'.format(dig))
|
|
912
|
+
outfile.write('\n\tint npar = {};\n'.format(npar))
|
|
913
|
+
outfile.write('\tmpfr_t p[npar];\n')
|
|
914
|
+
outfile.write('\tfor(i=0; i<npar; i++) mpfr_init2(p[i], TIDES_PREC);\n')
|
|
915
|
+
|
|
916
|
+
for i in range(npar):
|
|
917
|
+
outfile.write('\tmpfr_set_str(p[{}], "{}", 10, TIDES_RND);\n'.format(i,RR(parameter_values[i]).str()))
|
|
918
|
+
outfile.write('\tint nvar = {};\n\tmpfr_t v[nvar];\n'.format(VAR))
|
|
919
|
+
outfile.write('\tfor(i=0; i<nvar; i++) mpfr_init2(v[i], TIDES_PREC);\n')
|
|
920
|
+
for i in range(len(ics)):
|
|
921
|
+
outfile.write('\tmpfr_set_str(v[{}], "{}", 10, TIDES_RND);\n'.format(i,RR(ics[i]).str()))
|
|
922
|
+
outfile.write('\tmpfr_t tolrel, tolabs;\n')
|
|
923
|
+
outfile.write('\tmpfr_init2(tolrel, TIDES_PREC); \n')
|
|
924
|
+
outfile.write('\tmpfr_init2(tolabs, TIDES_PREC); \n')
|
|
925
|
+
outfile.write('\tmpfr_set_str(tolrel, "{}", 10, TIDES_RND);\n'.format(RR(tolrel).str()))
|
|
926
|
+
outfile.write('\tmpfr_set_str(tolabs, "{}", 10, TIDES_RND);\n'.format(RR(tolabs).str()))
|
|
927
|
+
|
|
928
|
+
outfile.write('\tmpfr_t tini, dt; \n')
|
|
929
|
+
outfile.write('\tmpfr_init2(tini, TIDES_PREC); \n')
|
|
930
|
+
outfile.write('\tmpfr_init2(dt, TIDES_PREC); \n')
|
|
931
|
+
|
|
932
|
+
outfile.write('\tmpfr_set_str(tini, "{}", 10, TIDES_RND);;\n'.format(RR(initial).str()))
|
|
933
|
+
outfile.write('\tmpfr_set_str(dt, "{}", 10, TIDES_RND);\n'.format(RR(delta).str()))
|
|
934
|
+
outfile.write('\tint nipt = {};\n'.format(floor((final-initial)/delta)))
|
|
935
|
+
outfile.write('\tFILE* fd = fopen("' + output + '", "w");\n')
|
|
936
|
+
outfile.write('\tmp_tides_delta(function_iteration, NULL, nvar, npar, nfun, v, p, tini, dt, nipt, tolrel, tolabs, NULL, fd);\n')
|
|
937
|
+
outfile.write('\tfclose(fd);\n\treturn 0;\n}')
|
|
938
|
+
outfile.close()
|