root / pobysoPythonSage / src / pobyso.py @ 84
Historique | Voir | Annoter | Télécharger (41,52 ko)
1 |
"""
|
---|---|
2 |
Actual functions to use in Sage
|
3 |
ST 2012-11-13
|
4 |
|
5 |
Command line syntax:
|
6 |
use from Sage (via the "load" or the "attach" commands)
|
7 |
|
8 |
pobyso functions come in five flavors:
|
9 |
- the _so_so (arguments and returned objects are pointers to Sollya objects,
|
10 |
includes the void function and the no arguments function that return a
|
11 |
pointer to a Sollya object);
|
12 |
- the _so_sa (argument are pointers to Sollya objects, returned objects are
|
13 |
Sage/Python objects or, more generally, information is transfered from the
|
14 |
Sollya world to Sage/Python world; e.g. functions without arguments that
|
15 |
return a Sage/Python object);
|
16 |
- the _sa_so (arguments are Sage/Python objects, returned objects are
|
17 |
pointers to Sollya objects);
|
18 |
- the sa_sa (arguments and returned objects are all Sage/Python objects);
|
19 |
- a catch all flavor, without any suffix, (e. g. functions that have no argument
|
20 |
nor return value).
|
21 |
This classification is not always very strict. Conversion functions from Sollya
|
22 |
to Sage/Python are sometimes decorated with Sage/Python arguments to set
|
23 |
the precision. These functions remain in the so_sa category.
|
24 |
NOTES:
|
25 |
Reported errors in Eclipse come from the calls to
|
26 |
the Sollya library
|
27 |
|
28 |
ToDo (among other things):
|
29 |
-memory management.
|
30 |
"""
|
31 |
from ctypes import * |
32 |
import re |
33 |
from sage.symbolic.expression_conversions import polynomial |
34 |
from sage.symbolic.expression_conversions import PolynomialConverter |
35 |
"""
|
36 |
Create the equivalent to an enum for the Sollya function types.
|
37 |
"""
|
38 |
(SOLLYA_BASE_FUNC_ABS, |
39 |
SOLLYA_BASE_FUNC_ACOS, |
40 |
SOLLYA_BASE_FUNC_ACOSH, |
41 |
SOLLYA_BASE_FUNC_ADD, |
42 |
SOLLYA_BASE_FUNC_ASIN, |
43 |
SOLLYA_BASE_FUNC_ASINH, |
44 |
SOLLYA_BASE_FUNC_ATAN, |
45 |
SOLLYA_BASE_FUNC_ATANH, |
46 |
SOLLYA_BASE_FUNC_CEIL, |
47 |
SOLLYA_BASE_FUNC_CONSTANT, |
48 |
SOLLYA_BASE_FUNC_COS, |
49 |
SOLLYA_BASE_FUNC_COSH, |
50 |
SOLLYA_BASE_FUNC_DIV, |
51 |
SOLLYA_BASE_FUNC_DOUBLE, |
52 |
SOLLYA_BASE_FUNC_DOUBLEDOUBLE, |
53 |
SOLLYA_BASE_FUNC_DOUBLEEXTENDED, |
54 |
SOLLYA_BASE_FUNC_ERF, |
55 |
SOLLYA_BASE_FUNC_ERFC, |
56 |
SOLLYA_BASE_FUNC_EXP, |
57 |
SOLLYA_BASE_FUNC_EXP_M1, |
58 |
SOLLYA_BASE_FUNC_FLOOR, |
59 |
SOLLYA_BASE_FUNC_FREE_VARIABLE, |
60 |
SOLLYA_BASE_FUNC_HALFPRECISION, |
61 |
SOLLYA_BASE_FUNC_LIBRARYCONSTANT, |
62 |
SOLLYA_BASE_FUNC_LIBRARYFUNCTION, |
63 |
SOLLYA_BASE_FUNC_LOG, |
64 |
SOLLYA_BASE_FUNC_LOG_10, |
65 |
SOLLYA_BASE_FUNC_LOG_1P, |
66 |
SOLLYA_BASE_FUNC_LOG_2, |
67 |
SOLLYA_BASE_FUNC_MUL, |
68 |
SOLLYA_BASE_FUNC_NEARESTINT, |
69 |
SOLLYA_BASE_FUNC_NEG, |
70 |
SOLLYA_BASE_FUNC_PI, |
71 |
SOLLYA_BASE_FUNC_POW, |
72 |
SOLLYA_BASE_FUNC_PROCEDUREFUNCTION, |
73 |
SOLLYA_BASE_FUNC_QUAD, |
74 |
SOLLYA_BASE_FUNC_SIN, |
75 |
SOLLYA_BASE_FUNC_SINGLE, |
76 |
SOLLYA_BASE_FUNC_SINH, |
77 |
SOLLYA_BASE_FUNC_SQRT, |
78 |
SOLLYA_BASE_FUNC_SUB, |
79 |
SOLLYA_BASE_FUNC_TAN, |
80 |
SOLLYA_BASE_FUNC_TANH, |
81 |
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44)) |
82 |
print "\nSuperficial pobyso check..." |
83 |
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS |
84 |
print "Last constant - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE |
85 |
|
86 |
pobyso_max_arity = 9
|
87 |
|
88 |
def pobyso_autoprint(arg): |
89 |
sollya_lib_autoprint(arg,None)
|
90 |
|
91 |
def pobyso_autoprint_so_so(arg): |
92 |
sollya_lib_autoprint(arg,None)
|
93 |
|
94 |
def pobyso_absolute_so_so(): |
95 |
return(sollya_lib_absolute(None)) |
96 |
|
97 |
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa, \ |
98 |
precisionSa=None):
|
99 |
"""
|
100 |
Return a Sollya range from to 2 RealField Sage elements.
|
101 |
The Sollya range element has a sufficient precision to hold all
|
102 |
the digits of the Sage bounds.
|
103 |
"""
|
104 |
# Sanity check.
|
105 |
if rnLowerBoundSa > rnUpperBoundSa:
|
106 |
return None |
107 |
if precision is None: |
108 |
# Check for the largest precision.
|
109 |
lbPrecSa = rnLowerBoundSa.parent().precision() |
110 |
ubPrecSa = rnLowerBoundSa.parent().precision() |
111 |
maxPrecSa = max(lbPrecSa, ubPrecSa)
|
112 |
else:
|
113 |
maxPrecSa = precisionSa |
114 |
sollyaCurrentPrecSo = pobyso_get_prec_so() |
115 |
sollyaCurrentPrecSa = pobyso_constant_from_int_so_sa(sollyaCurrentPrecSo) |
116 |
# Change the current Sollya precision only if necessary.
|
117 |
if maxPrecSa > sollyaCurrentPrecSa:
|
118 |
pobyso_set_prec_sa_so(maxPrecSa) |
119 |
lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa)) |
120 |
upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa)) |
121 |
rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo) |
122 |
# Back to original precision.
|
123 |
if maxPrecSa > sollyaCurrentPrecSa:
|
124 |
sollya_lib_set_prec(sollyaCurrentPrecSo) |
125 |
# Clean up
|
126 |
sollya_lib_clear_obj(sollyaCurrentPrecSo) |
127 |
sollya_lib_clear_obj(lowerBoundSo) |
128 |
sollya_lib_clear_obj(upperBoundSo) |
129 |
return(rangeSo)
|
130 |
# End pobyso_bounds_to_range_sa_so
|
131 |
|
132 |
def pobyso_build_function_sub_so_so(exp1So, exp2So): |
133 |
return(sollya_lib_build_function_sub(exp1So, exp2So))
|
134 |
|
135 |
def pobyso_cmp(rnArg, soCte): |
136 |
"""
|
137 |
Compare the MPFR value a RealNumber with that of a Sollya constant.
|
138 |
|
139 |
Get the value of the Sollya constant into a RealNumber and compare
|
140 |
using MPFR. Could be optimized by working directly with a mpfr_t
|
141 |
for the intermediate number.
|
142 |
"""
|
143 |
precisionOfCte = c_int(0)
|
144 |
# From the Sollya constant, create a local Sage RealNumber.
|
145 |
sollya_lib_get_prec_of_constant(precisionOfCte, soCte) |
146 |
#print "Precision of constant: ", precisionOfCte
|
147 |
RRRR = RealField(precisionOfCte.value) |
148 |
rnLocal = RRRR(0)
|
149 |
sollya_lib_get_constant(get_rn_value(rnLocal), soCte) |
150 |
#print "rnDummy: ", rnDummy
|
151 |
# Compare the local Sage RealNumber with rnArg.
|
152 |
return(cmp_rn_value(rnArg, rnLocal))
|
153 |
# End pobyso_smp
|
154 |
|
155 |
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo): |
156 |
"""
|
157 |
Variable change in a function.
|
158 |
"""
|
159 |
return(sollya_lib_evaluate(funcSo,chvarExpSo))
|
160 |
# End pobyso_change_var_in_function_so_so
|
161 |
|
162 |
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo): |
163 |
resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo) |
164 |
return(resultSo)
|
165 |
# End pobyso_chebyshevform_so_so.
|
166 |
|
167 |
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \ |
168 |
upperBoundSa): |
169 |
"""
|
170 |
TODO: set the variable name in Sollya.
|
171 |
"""
|
172 |
funcSo = pobyso_parse_string(funcSa._assume_str()) |
173 |
rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa) |
174 |
infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo) |
175 |
# Sollya return the infnorm as an interval.
|
176 |
fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo) |
177 |
# Get the top bound and compute the binade top limit.
|
178 |
fMaxUpperBoundSa = fMaxSa.upper() |
179 |
binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
|
180 |
# Put up together the function to use to compute the lower bound.
|
181 |
funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) + \
|
182 |
'-(' + f._assume_str() + ')') |
183 |
pobyso_autoprint(funcAuxSo) |
184 |
# Clear the Sollya range before a new call to infnorm and issue the call.
|
185 |
sollya_lib_clear_obj(infnormSo) |
186 |
infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo) |
187 |
fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo) |
188 |
sollya_lib_clear_obj(infnormSo) |
189 |
fMinLowerBoundSa = topBinadeLimit - fMinSa.lower() |
190 |
# Compute the maximum of the precisions of the different bounds.
|
191 |
maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
|
192 |
fMaxUpperBoundSa.parent().precision()]) |
193 |
# Create a RealIntervalField and create an interval with the "good" bounds.
|
194 |
RRRI = RealIntervalField(maxPrecSa) |
195 |
imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa) |
196 |
# Free the unneeded Sollya objects
|
197 |
sollya_lib_clear_obj(funcSo) |
198 |
sollya_lib_clear_obj(funcAuxSo) |
199 |
sollya_lib_clear_obj(rangeSo) |
200 |
return(imageIntervalSa)
|
201 |
# End pobyso_compute_pos_function_abs_val_bounds_sa_sa
|
202 |
|
203 |
def pobyso_constant(rnArg): |
204 |
""" Legacy function. See pobyso_constant_sa_so. """
|
205 |
return(pobyso_constant_sa_so(rnArg))
|
206 |
|
207 |
def pobyso_constant_sa_so(rnArgSa, precisionSa=None): |
208 |
"""
|
209 |
Create a Sollya constant from a RealNumber.
|
210 |
"""
|
211 |
# Precision stuff
|
212 |
if precisionSa is None: |
213 |
precisionSa = rnArgSa.parent().precision() |
214 |
currentSollyaPrecisionSo = pobyso_get_prec_so() |
215 |
currentSollyaPrecisionSa = pobyso_get |
216 |
if precisionSa > currentSollyaPrecisionSa:
|
217 |
pobyso_set_prec_sa_so(precisionSa) |
218 |
constantSo = sollya_lib_constant(get_rn_value(rnArgSa)) |
219 |
pobyso_set_prec_sa_so(currentSollyaPrecision) |
220 |
else:
|
221 |
constantSo = sollya_lib_constant(get_rn_value(rnArgSa)) |
222 |
return(constantSo)
|
223 |
|
224 |
def pobyso_constant_0_sa_so(): |
225 |
return(pobyso_constant_from_int_sa_so(0)) |
226 |
|
227 |
def pobyso_constant_1(): |
228 |
""" Legacy function. See pobyso_constant_so_so. """
|
229 |
return(pobyso_constant_1_sa_so())
|
230 |
|
231 |
def pobyso_constant_1_sa_so(): |
232 |
return(pobyso_constant_from_int_sa_so(1)) |
233 |
|
234 |
def pobyso_constant_from_int(anInt): |
235 |
""" Legacy function. See pobyso_constant_from_int_sa_so. """
|
236 |
return(pobyso_constant_from_int_sa_so(anInt))
|
237 |
|
238 |
def pobyso_constant_from_int_sa_so(anInt): |
239 |
return(sollya_lib_constant_from_int(int(anInt))) |
240 |
|
241 |
def pobyso_constant_from_int_so_sa(constSo): |
242 |
"""
|
243 |
Get a Sollya constant as an int.
|
244 |
Use full for precision or powers in polynomials.
|
245 |
"""
|
246 |
constSa = c_int(0)
|
247 |
sollya_lib_get_constant_as_int(byref(constSa), constSo) |
248 |
return(constSa.value)
|
249 |
# End pobyso_constant_from_int_so_sa
|
250 |
|
251 |
def pobyso_function_type_as_string(funcType): |
252 |
""" Legacy function. See pobyso_function_type_as_string_so_sa. """
|
253 |
return(pobyso_function_type_as_string_so_sa(funcType))
|
254 |
|
255 |
def pobyso_function_type_as_string_so_sa(funcType): |
256 |
"""
|
257 |
Numeric Sollya function codes -> Sage mathematical function names.
|
258 |
Notice that pow -> ^ (a la Sage, not a la Python).
|
259 |
"""
|
260 |
if funcType == SOLLYA_BASE_FUNC_ABS:
|
261 |
return "abs" |
262 |
elif funcType == SOLLYA_BASE_FUNC_ACOS:
|
263 |
return "arccos" |
264 |
elif funcType == SOLLYA_BASE_FUNC_ACOSH:
|
265 |
return "arccosh" |
266 |
elif funcType == SOLLYA_BASE_FUNC_ADD:
|
267 |
return "+" |
268 |
elif funcType == SOLLYA_BASE_FUNC_ASIN:
|
269 |
return "arcsin" |
270 |
elif funcType == SOLLYA_BASE_FUNC_ASINH:
|
271 |
return "arcsinh" |
272 |
elif funcType == SOLLYA_BASE_FUNC_ATAN:
|
273 |
return "arctan" |
274 |
elif funcType == SOLLYA_BASE_FUNC_ATANH:
|
275 |
return "arctanh" |
276 |
elif funcType == SOLLYA_BASE_FUNC_CEIL:
|
277 |
return "ceil" |
278 |
elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
|
279 |
return "cte" |
280 |
elif funcType == SOLLYA_BASE_FUNC_COS:
|
281 |
return "cos" |
282 |
elif funcType == SOLLYA_BASE_FUNC_COSH:
|
283 |
return "cosh" |
284 |
elif funcType == SOLLYA_BASE_FUNC_DIV:
|
285 |
return "/" |
286 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
|
287 |
return "double" |
288 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
|
289 |
return "doubleDouble" |
290 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
|
291 |
return "doubleDxtended" |
292 |
elif funcType == SOLLYA_BASE_FUNC_ERF:
|
293 |
return "erf" |
294 |
elif funcType == SOLLYA_BASE_FUNC_ERFC:
|
295 |
return "erfc" |
296 |
elif funcType == SOLLYA_BASE_FUNC_EXP:
|
297 |
return "exp" |
298 |
elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
|
299 |
return "expm1" |
300 |
elif funcType == SOLLYA_BASE_FUNC_FLOOR:
|
301 |
return "floor" |
302 |
elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
303 |
return "freeVariable" |
304 |
elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
|
305 |
return "halfPrecision" |
306 |
elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
|
307 |
return "libraryConstant" |
308 |
elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
|
309 |
return "libraryFunction" |
310 |
elif funcType == SOLLYA_BASE_FUNC_LOG:
|
311 |
return "log" |
312 |
elif funcType == SOLLYA_BASE_FUNC_LOG_10:
|
313 |
return "log10" |
314 |
elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
|
315 |
return "log1p" |
316 |
elif funcType == SOLLYA_BASE_FUNC_LOG_2:
|
317 |
return "log2" |
318 |
elif funcType == SOLLYA_BASE_FUNC_MUL:
|
319 |
return "*" |
320 |
elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
|
321 |
return "round" |
322 |
elif funcType == SOLLYA_BASE_FUNC_NEG:
|
323 |
return "__neg__" |
324 |
elif funcType == SOLLYA_BASE_FUNC_PI:
|
325 |
return "pi" |
326 |
elif funcType == SOLLYA_BASE_FUNC_POW:
|
327 |
return "^" |
328 |
elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
|
329 |
return "procedureFunction" |
330 |
elif funcType == SOLLYA_BASE_FUNC_QUAD:
|
331 |
return "quad" |
332 |
elif funcType == SOLLYA_BASE_FUNC_SIN:
|
333 |
return "sin" |
334 |
elif funcType == SOLLYA_BASE_FUNC_SINGLE:
|
335 |
return "single" |
336 |
elif funcType == SOLLYA_BASE_FUNC_SINH:
|
337 |
return "sinh" |
338 |
elif funcType == SOLLYA_BASE_FUNC_SQRT:
|
339 |
return "sqrt" |
340 |
elif funcType == SOLLYA_BASE_FUNC_SUB:
|
341 |
return "-" |
342 |
elif funcType == SOLLYA_BASE_FUNC_TAN:
|
343 |
return "tan" |
344 |
elif funcType == SOLLYA_BASE_FUNC_TANH:
|
345 |
return "tanh" |
346 |
elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
|
347 |
return "tripleDouble" |
348 |
else:
|
349 |
return None |
350 |
|
351 |
def pobyso_get_constant(rnArg, soConst): |
352 |
""" Legacy function. See pobyso_get_constant_so_sa. """
|
353 |
return(pobyso_get_constant_so_sa(rnArg, soConst))
|
354 |
|
355 |
def pobyso_get_constant_so_sa(rnArgSa, constSo): |
356 |
"""
|
357 |
Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
|
358 |
rnArg must already exist and belong to some RealField.
|
359 |
We assume that soConst points to a Sollya constant.
|
360 |
"""
|
361 |
return(sollya_lib_get_constant(get_rn_value(rnArgSa), constSo))
|
362 |
|
363 |
def pobyso_get_constant_as_rn(ctExpSo): |
364 |
"""
|
365 |
Legacy function. See pobyso_get_constant_as_rn_so_sa.
|
366 |
"""
|
367 |
return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
|
368 |
|
369 |
def pobyso_get_constant_as_rn_so_sa(constExpSo): |
370 |
"""
|
371 |
Get a Sollya constant as a Sage "real number".
|
372 |
The precision of the floating-point number returned is that of the Sollya
|
373 |
constant.
|
374 |
"""
|
375 |
precisionSa = pobyso_get_prec_of_constant_so_sa(constExpSo) |
376 |
RRRR = RealField(precisionSa) |
377 |
rnSa = RRRR(0)
|
378 |
sollya_lib_get_constant(get_rn_value(rnSa), constExpSo) |
379 |
return(rnSa)
|
380 |
# End pobyso_get_constant_as_rn_so_sa
|
381 |
|
382 |
def pobyso_get_constant_as_rn_with_rf(ctExp, realField): |
383 |
"""
|
384 |
Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
|
385 |
"""
|
386 |
return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
|
387 |
|
388 |
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None): |
389 |
"""
|
390 |
Get a Sollya constant as a Sage "real number".
|
391 |
If no real field is specified, the precision of the floating-point number
|
392 |
returned is that of the Solly constant.
|
393 |
Otherwise is is that of the real field. Hence rounding may happen.
|
394 |
"""
|
395 |
if realFieldSa is None: |
396 |
sollyaPrecSa = pobyso_get_prec_so_sa() |
397 |
realFieldSa = RealField(sollyaPrecSa) |
398 |
rnSa = realFieldSa(0)
|
399 |
sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo) |
400 |
return(rnSa)
|
401 |
# End pobyso_get_constant_as_rn_with_rf_so_sa
|
402 |
|
403 |
def pobyso_get_free_variable_name(): |
404 |
"""
|
405 |
Legacy function. See pobyso_get_free_variable_name_so_sa.
|
406 |
"""
|
407 |
return(pobyso_get_free_variable_name_so_sa())
|
408 |
|
409 |
def pobyso_get_free_variable_name_so_sa(): |
410 |
return(sollya_lib_get_free_variable_name())
|
411 |
|
412 |
def pobyso_get_function_arity(expressionSo): |
413 |
"""
|
414 |
Legacy function. See pobyso_get_function_arity_so_sa.
|
415 |
"""
|
416 |
return(pobyso_get_function_arity_so_sa(expressionSo))
|
417 |
|
418 |
def pobyso_get_function_arity_so_sa(expressionSo): |
419 |
arity = c_int(0)
|
420 |
sollya_lib_get_function_arity(byref(arity),expressionSo) |
421 |
return(int(arity.value)) |
422 |
|
423 |
def pobyso_get_head_function(expressionSo): |
424 |
"""
|
425 |
Legacy function. See pobyso_get_head_function_so_sa.
|
426 |
"""
|
427 |
return(pobyso_get_head_function_so_sa(expressionSo))
|
428 |
|
429 |
def pobyso_get_head_function_so_sa(expressionSo): |
430 |
functionType = c_int(0)
|
431 |
sollya_lib_get_head_function(byref(functionType), expressionSo, None)
|
432 |
return(int(functionType.value)) |
433 |
|
434 |
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ): |
435 |
"""
|
436 |
Return the Sage interval corresponding to the Sollya range argument.
|
437 |
If no reaIntervalField is passed as an argument, the interval bounds are not
|
438 |
rounded: they are elements of RealIntervalField of the "right" precision
|
439 |
to hold all the digits.
|
440 |
"""
|
441 |
prec = c_int(0)
|
442 |
if realIntervalFieldSa is None: |
443 |
retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
|
444 |
if retval == 0: |
445 |
return(None) |
446 |
realIntervalFieldSa = RealIntervalField(prec.value) |
447 |
intervalSa = realIntervalFieldSa(0,0) |
448 |
retval = \ |
449 |
sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\ |
450 |
soRange) |
451 |
if retval == 0: |
452 |
return(None) |
453 |
return(intervalSa)
|
454 |
# End pobyso_get_interval_from_range_so_sa
|
455 |
|
456 |
def pobyso_get_list_elements(soObj): |
457 |
""" Legacy function. See pobyso_get_list_elements_so_so. """
|
458 |
return(pobyso_get_list_elements_so_so(soObj))
|
459 |
|
460 |
def pobyso_get_list_elements_so_so(soObj): |
461 |
"""
|
462 |
Get the list elements as a Sage/Python array of Sollya objects.
|
463 |
The other data returned are also Sage/Python objects.
|
464 |
"""
|
465 |
listAddress = POINTER(c_longlong)() |
466 |
numElements = c_int(0)
|
467 |
isEndElliptic = c_int(0)
|
468 |
listAsList = [] |
469 |
result = sollya_lib_get_list_elements(byref(listAddress),\ |
470 |
byref(numElements),\ |
471 |
byref(isEndElliptic),\ |
472 |
soObj) |
473 |
if result == 0 : |
474 |
return None |
475 |
for i in xrange(0, numElements.value, 1): |
476 |
listAsList.append(listAddress[i]) |
477 |
return(listAsList, numElements.value, isEndElliptic.value)
|
478 |
|
479 |
def pobyso_get_max_prec_of_exp(soExp): |
480 |
""" Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
|
481 |
return(pobyso_get_max_prec_of_exp_so_sa(soExp))
|
482 |
|
483 |
def pobyso_get_max_prec_of_exp_so_sa(soExp): |
484 |
"""
|
485 |
Get the maximum precision used for the numbers in a Sollya expression.
|
486 |
|
487 |
Arguments:
|
488 |
soExp -- a Sollya expression pointer
|
489 |
Return value:
|
490 |
A Python integer
|
491 |
TODO:
|
492 |
- error management;
|
493 |
- correctly deal with numerical type such as DOUBLEEXTENDED.
|
494 |
"""
|
495 |
maxPrecision = 0
|
496 |
minConstPrec = 0
|
497 |
currentConstPrec = 0
|
498 |
operator = pobyso_get_head_function_so_sa(soExp) |
499 |
if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \ |
500 |
(operator != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
501 |
(arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp) |
502 |
for i in xrange(arity): |
503 |
maxPrecisionCandidate = \ |
504 |
pobyso_get_max_prec_of_exp_so_sa(subexpressions[i]) |
505 |
if maxPrecisionCandidate > maxPrecision:
|
506 |
maxPrecision = maxPrecisionCandidate |
507 |
return(maxPrecision)
|
508 |
elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
509 |
minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp) |
510 |
#currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
|
511 |
#print minConstPrec, " - ", currentConstPrec
|
512 |
return(pobyso_get_min_prec_of_constant_so_sa(soExp))
|
513 |
|
514 |
elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
515 |
return(0) |
516 |
else:
|
517 |
print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator." |
518 |
return(0) |
519 |
|
520 |
def pobyso_get_min_prec_of_constant_so_sa(soConstExp): |
521 |
"""
|
522 |
Get the minimum precision necessary to represent the value of a Sollya
|
523 |
constant.
|
524 |
MPFR_MIN_PREC and powers of 2 are taken into account.
|
525 |
We assume that soCteExp is a point
|
526 |
"""
|
527 |
constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp) |
528 |
return(min_mpfr_size(get_rn_value(constExpAsRn)))
|
529 |
|
530 |
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR): |
531 |
""" Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
|
532 |
return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
|
533 |
|
534 |
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR): |
535 |
"""
|
536 |
Get a Sage expression from a Sollya expression.
|
537 |
Currently only tested with polynomials with floating-point coefficients.
|
538 |
Notice that, in the returned polynomial, the exponents are RealNumbers.
|
539 |
"""
|
540 |
#pobyso_autoprint(sollyaExp)
|
541 |
operator = pobyso_get_head_function_so_sa(sollyaExp) |
542 |
sollyaLibFreeVariableName = sollya_lib_get_free_variable_name() |
543 |
# Constants and the free variable are special cases.
|
544 |
# All other operator are dealt with in the same way.
|
545 |
if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \ |
546 |
(operator != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
547 |
(arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp) |
548 |
if arity == 1: |
549 |
sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
|
550 |
"(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \ |
551 |
realField) + ")")
|
552 |
elif arity == 2: |
553 |
# We do not get through the preprocessor.
|
554 |
# The "^" operator is then a special case.
|
555 |
if operator == SOLLYA_BASE_FUNC_POW:
|
556 |
operatorAsString = "**"
|
557 |
else:
|
558 |
operatorAsString = \ |
559 |
pobyso_function_type_as_string_so_sa(operator) |
560 |
sageExp = \ |
561 |
eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\ |
562 |
+ " " + operatorAsString + " " + \ |
563 |
"pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
|
564 |
# We do not know yet how to deal with arity >= 3
|
565 |
# (is there any in Sollya anyway?).
|
566 |
else:
|
567 |
sageExp = eval('None') |
568 |
return(sageExp)
|
569 |
elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
570 |
#print "This is a constant"
|
571 |
return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
|
572 |
elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
573 |
#print "This is free variable"
|
574 |
return(eval(sollyaLibFreeVariableName)) |
575 |
else:
|
576 |
print "Unexpected" |
577 |
return eval('None') |
578 |
# End pobyso_get_sage_poly_from_sollya_poly
|
579 |
|
580 |
def pobyso_get_poly_sa_so(polySo, realFieldSa=None): |
581 |
"""
|
582 |
Create a Sollya polynomial from a Sage polynomial.
|
583 |
"""
|
584 |
pass
|
585 |
# pobyso_get_poly_sa_so
|
586 |
|
587 |
def pobyso_get_poly_so_sa(polySo, realFieldSa=None): |
588 |
"""
|
589 |
Convert a Sollya polynomial into a Sage polynomial.
|
590 |
We assume that the polynomial is in canonical form.
|
591 |
If no realField is given, a RealField corresponding to the maximum precision
|
592 |
of the coefficients is internally computed.
|
593 |
It is not returned but can be easily retrieved from the polynomial itself.
|
594 |
Main steps:
|
595 |
- (optional) compute the RealField of the coefficients;
|
596 |
- convert the Sollya expression into a Sage expression;
|
597 |
- convert the Sage expression into a Sage polynomial
|
598 |
TODO: the canonical thing for the polynomial.
|
599 |
"""
|
600 |
if realFieldSa is None: |
601 |
expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo) |
602 |
realFieldSa = RealField(expressionPrecSa) |
603 |
#print "Sollya expression before...",
|
604 |
#pobyso_autoprint(polySo)
|
605 |
|
606 |
expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \ |
607 |
realFieldSa) |
608 |
#print "...Sollya expression after.",
|
609 |
#pobyso_autoprint(polySo)
|
610 |
polyVariableSa = expressionSa.variables()[0]
|
611 |
polyRingSa = realFieldSa[str(polyVariableSa)]
|
612 |
#print polyRingSa
|
613 |
# Do not use the polynomial(expressionSa, ring=polyRingSa) form!
|
614 |
polynomialSa = polyRingSa(expressionSa) |
615 |
return(polynomialSa)
|
616 |
# End pobyso_get_sage_poly_from_sollya_poly
|
617 |
|
618 |
def pobyso_get_subfunctions(expressionSo): |
619 |
""" Legacy function. See pobyso_get_subfunctions_so_sa. """
|
620 |
return(pobyso_get_subfunctions_so_sa(expressionSo))
|
621 |
|
622 |
def pobyso_get_subfunctions_so_sa(expressionSo): |
623 |
"""
|
624 |
Get the subfunctions of an expression.
|
625 |
Return the number of subfunctions and the list of subfunctions addresses.
|
626 |
S.T.: Could not figure out another way than that ugly list of declarations
|
627 |
to recover the addresses of the subfunctions.
|
628 |
We limit ourselves to arity 8 functions.
|
629 |
"""
|
630 |
subf0 = c_int(0)
|
631 |
subf1 = c_int(0)
|
632 |
subf2 = c_int(0)
|
633 |
subf3 = c_int(0)
|
634 |
subf4 = c_int(0)
|
635 |
subf5 = c_int(0)
|
636 |
subf6 = c_int(0)
|
637 |
subf7 = c_int(0)
|
638 |
subf8 = c_int(0)
|
639 |
arity = c_int(0)
|
640 |
nullPtr = POINTER(c_int)() |
641 |
sollya_lib_get_subfunctions(expressionSo, byref(arity), \ |
642 |
byref(subf0), byref(subf1), byref(subf2), byref(subf3), \ |
643 |
byref(subf4), byref(subf5),\ |
644 |
byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
|
645 |
# byref(cast(subfunctions[0], POINTER(c_int))), \
|
646 |
# byref(cast(subfunctions[0], POINTER(c_int))), \
|
647 |
# byref(cast(subfunctions[2], POINTER(c_int))), \
|
648 |
# byref(cast(subfunctions[3], POINTER(c_int))), \
|
649 |
# byref(cast(subfunctions[4], POINTER(c_int))), \
|
650 |
# byref(cast(subfunctions[5], POINTER(c_int))), \
|
651 |
# byref(cast(subfunctions[6], POINTER(c_int))), \
|
652 |
# byref(cast(subfunctions[7], POINTER(c_int))), \
|
653 |
# byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
|
654 |
subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \ |
655 |
subf8] |
656 |
subs = [] |
657 |
if arity.value > pobyso_max_arity:
|
658 |
return(0,[]) |
659 |
for i in xrange(arity.value): |
660 |
subs.append(int(subfunctions[i].value))
|
661 |
#print subs[i]
|
662 |
return(int(arity.value), subs) |
663 |
|
664 |
def pobyso_get_prec(): |
665 |
""" Legacy function. See pobyso_get_prec_so_sa(). """
|
666 |
return(pobyso_get_prec_so_sa())
|
667 |
|
668 |
def pobyso_get_prec_so(): |
669 |
"""
|
670 |
Get the current default precision in Sollya.
|
671 |
The return value is a Sollya object.
|
672 |
Usefull when modifying the precision back and forth by avoiding
|
673 |
extra conversions.
|
674 |
"""
|
675 |
return(sollya_lib_get_prec(None)) |
676 |
|
677 |
def pobyso_get_prec_so_sa(): |
678 |
"""
|
679 |
Get the current default precision in Sollya.
|
680 |
The return value is Sage/Python int.
|
681 |
"""
|
682 |
precSo = sollya_lib_get_prec(None)
|
683 |
precSa = c_int(0)
|
684 |
sollya_lib_get_constant_as_int(byref(precSa), precSo) |
685 |
sollya_lib_clear_obj(precSo) |
686 |
return(int(precSa.value)) |
687 |
# End pobyso_get_prec_so_sa.
|
688 |
|
689 |
def pobyso_get_prec_of_constant(ctExpSo): |
690 |
""" Legacy function. See pobyso_get_prec_of_constant_so_sa. """
|
691 |
return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
|
692 |
|
693 |
def pobyso_get_prec_of_constant_so_sa(ctExpSo): |
694 |
prec = c_int(0)
|
695 |
retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
|
696 |
if retc == 0: |
697 |
return(None) |
698 |
return(int(prec.value)) |
699 |
|
700 |
def pobyso_get_prec_of_range_so_sa(rangeSo): |
701 |
prec = c_int(0)
|
702 |
retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
|
703 |
if retc == 0: |
704 |
return(None) |
705 |
return(int(prec.value)) |
706 |
|
707 |
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None): |
708 |
print "Do not use this function. User pobyso_supnorm_so_so instead." |
709 |
return(None) |
710 |
|
711 |
def pobyso_interval_to_range_sa_so(intervalSa, precisionSa=None): |
712 |
if precisionSa is None: |
713 |
precisionSa = intervalSa.parent().precision() |
714 |
intervalSo = pobyso_bounds_to_range_sa_so(intervalSa.lower(),\ |
715 |
intervalSa.upper(),\ |
716 |
precisionSa) |
717 |
return(intervalSo)
|
718 |
# End pobyso_interval_to_range_sa_so
|
719 |
|
720 |
def pobyso_lib_init(): |
721 |
sollya_lib_init(None)
|
722 |
|
723 |
def pobyso_name_free_variable(freeVariableName): |
724 |
""" Legacy function. See pobyso_name_free_variable_sa_so. """
|
725 |
pobyso_name_free_variable_sa_so(freeVariableName) |
726 |
|
727 |
def pobyso_name_free_variable_sa_so(freeVariableName): |
728 |
"""
|
729 |
Set the free variable name in Sollya from a Sage string.
|
730 |
"""
|
731 |
sollya_lib_name_free_variable(freeVariableName) |
732 |
|
733 |
def pobyso_parse_string(string): |
734 |
""" Legacy function. See pobyso_parse_string_sa_so. """
|
735 |
return(pobyso_parse_string_sa_so(string))
|
736 |
|
737 |
def pobyso_parse_string_sa_so(string): |
738 |
"""
|
739 |
Get the Sollya expression computed from a Sage string.
|
740 |
"""
|
741 |
return(sollya_lib_parse_string(string))
|
742 |
|
743 |
def pobyso_range(rnLowerBound, rnUpperBound): |
744 |
""" Legacy function. See pobyso_range_sa_so. """
|
745 |
return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
|
746 |
|
747 |
|
748 |
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalField = None): |
749 |
"""
|
750 |
Get a Sage interval from a Sollya range.
|
751 |
If no realIntervalField is given as a parameter, the Sage interval
|
752 |
precision is that of the Sollya range.
|
753 |
Otherwise, the precision is that of the realIntervalField. Rounding
|
754 |
may happen.
|
755 |
"""
|
756 |
if realIntervalField is None: |
757 |
precSa = pobyso_get_prec_of_range_so_sa(rangeSo) |
758 |
realIntervalField = RealIntervalField(precSa) |
759 |
intervalSa = \ |
760 |
pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalField) |
761 |
return(intervalSa)
|
762 |
|
763 |
def pobyso_remez_canonical_sa_sa(func, \ |
764 |
degree, \ |
765 |
lowerBound, \ |
766 |
upperBound, \ |
767 |
weight = None, \
|
768 |
quality = None):
|
769 |
"""
|
770 |
All arguments are Sage/Python.
|
771 |
The functions (func and weight) must be passed as expressions or strings.
|
772 |
Otherwise the function fails.
|
773 |
The return value is a Sage polynomial.
|
774 |
"""
|
775 |
var('zorglub') # Dummy variable name for type check only. Type of |
776 |
# zorglub is "symbolic expression".
|
777 |
polySo = pobyso_remez_canonical_sa_so(func, \ |
778 |
degree, \ |
779 |
lowerBound, \ |
780 |
upperBound, \ |
781 |
weight = None, \
|
782 |
quality = None)
|
783 |
# String test
|
784 |
if parent(func) == parent("string"): |
785 |
functionSa = eval(func)
|
786 |
# Expression test.
|
787 |
elif type(func) == type(zorglub): |
788 |
functionSa = func |
789 |
else:
|
790 |
return None |
791 |
#
|
792 |
maxPrecision = 0
|
793 |
if polySo is None: |
794 |
return(None) |
795 |
maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
796 |
RRRR = RealField(maxPrecision) |
797 |
polynomialRing = RRRR[functionSa.variables()[0]]
|
798 |
expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR) |
799 |
polySa = polynomial(expSa, polynomialRing) |
800 |
sollya_lib_clear_obj(polySo) |
801 |
return(polySa)
|
802 |
|
803 |
def pobyso_remez_canonical(func, \ |
804 |
degree, \ |
805 |
lowerBound, \ |
806 |
upperBound, \ |
807 |
weight = "1", \
|
808 |
quality = None):
|
809 |
""" Legacy function. See pobyso_remez_canonical_sa_so. """
|
810 |
return(pobyso_remez_canonical_sa_so(func, \
|
811 |
degree, \ |
812 |
lowerBound, \ |
813 |
upperBound, \ |
814 |
weight, \ |
815 |
quality)) |
816 |
def pobyso_remez_canonical_sa_so(func, \ |
817 |
degree, \ |
818 |
lowerBound, \ |
819 |
upperBound, \ |
820 |
weight = None, \
|
821 |
quality = None):
|
822 |
"""
|
823 |
All arguments are Sage/Python.
|
824 |
The functions (func and weight) must be passed as expressions or strings.
|
825 |
Otherwise the function fails.
|
826 |
The return value is a pointer to a Sollya function.
|
827 |
"""
|
828 |
var('zorglub') # Dummy variable name for type check only. Type of |
829 |
# zorglub is "symbolic expression".
|
830 |
currentVariableName = None
|
831 |
# The func argument can be of different types (string,
|
832 |
# symbolic expression...)
|
833 |
if parent(func) == parent("string"): |
834 |
functionSo = sollya_lib_parse_string(func) |
835 |
# Expression test.
|
836 |
elif type(func) == type(zorglub): |
837 |
# Until we are able to translate Sage expressions into Sollya
|
838 |
# expressions : parse the string version.
|
839 |
currentVariableName = func.variables()[0]
|
840 |
sollya_lib_name_free_variable(str(currentVariableName))
|
841 |
functionSo = sollya_lib_parse_string(func._assume_str()) |
842 |
else:
|
843 |
return(None) |
844 |
if weight is None: |
845 |
weightSo = pobyso_constant_1_sa_so() |
846 |
elif parent(weight) == parent("string"): |
847 |
weightSo = sollya_lib_parse_string(func) |
848 |
elif type(weight) == type(zorglub): |
849 |
functionSo = sollya_lib_parse_string_sa_so(weight._assume_str()) |
850 |
else:
|
851 |
return(None) |
852 |
degreeSo = pobyso_constant_from_int(degree) |
853 |
rangeSo = pobyso_range_sa_so(lowerBound, upperBound) |
854 |
if not quality is None: |
855 |
qualitySo= pobyso_constant_sa_so(quality) |
856 |
else:
|
857 |
qualitySo = None
|
858 |
|
859 |
remezPolySo = sollya_lib_remez(functionSo, \ |
860 |
degreeSo, \ |
861 |
rangeSo, \ |
862 |
weightSo, \ |
863 |
qualitySo, \ |
864 |
None)
|
865 |
sollya_lib_clear_obj(functionSo) |
866 |
sollya_lib_clear_obj(degreeSo) |
867 |
sollya_lib_clear_obj(rangeSo) |
868 |
sollya_lib_clear_obj(weightSo) |
869 |
if not qualitySo is None: |
870 |
sollya_lib_clear_obj(qualtiySo) |
871 |
return(remezPolySo)
|
872 |
# End pobyso_remez_canonical_sa_so
|
873 |
|
874 |
def pobyso_remez_canonical_so_so(funcSo, \ |
875 |
degreeSo, \ |
876 |
rangeSo, \ |
877 |
weightSo = pobyso_constant_1_sa_so(),\ |
878 |
qualitySo = None):
|
879 |
"""
|
880 |
All arguments are pointers to Sollya objects.
|
881 |
The return value is a pointer to a Sollya function.
|
882 |
"""
|
883 |
if not sollya_lib_obj_is_function(funcSo): |
884 |
return(None) |
885 |
return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None)) |
886 |
|
887 |
def pobyso_set_canonical_off(): |
888 |
sollya_lib_set_canonical(sollya_lib_off()) |
889 |
|
890 |
def pobyso_set_canonical_on(): |
891 |
sollya_lib_set_canonical(sollya_lib_on()) |
892 |
|
893 |
def pobyso_set_prec(p): |
894 |
""" Legacy function. See pobyso_set_prec_sa_so. """
|
895 |
return( pobyso_set_prec_sa_so(p))
|
896 |
|
897 |
def pobyso_set_prec_sa_so(p): |
898 |
a = c_int(p) |
899 |
precSo = c_void_p(sollya_lib_constant_from_int(a)) |
900 |
sollya_lib_set_prec(precSo) |
901 |
|
902 |
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo, accuracySo): |
903 |
return(sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
|
904 |
accuracySo)) |
905 |
|
906 |
def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, rangeSo, \ |
907 |
errorTypeSo, \ |
908 |
sollyaPrecSo=None):
|
909 |
"""
|
910 |
Compute the Taylor expansion with the variable change
|
911 |
x -> (x-intervalCenter) included.
|
912 |
"""
|
913 |
# No global change of the working precision.
|
914 |
if not sollyaPrecSo is None: |
915 |
initialPrecSo = sollya_lib_get_prec(None)
|
916 |
sollya_lib_set_prec(sollyaPrecSo) |
917 |
#
|
918 |
intervalCenterSo = sollya_lib_mid(rangeSo) |
919 |
taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \ |
920 |
intervalCenterSo, \ |
921 |
rangeSo, errorTypeSo, None)
|
922 |
(taylorFormListSo, numElements, isEndElliptic) = \ |
923 |
pobyso_get_list_elements_so_so(taylorFormSo) |
924 |
polySo = taylorFormListSo[0]
|
925 |
errorRangeSo = taylorFormListSo[2]
|
926 |
maxErrorSo = sollya_lib_sup(errorRangeSo) |
927 |
changeVarExpSo = sollya_lib_build_function_sub(\ |
928 |
sollya_lib_build_function_free_variable(),\ |
929 |
sollya_lib_copy_obj(intervalCenterSo)) |
930 |
polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo) |
931 |
# If changed, reset the Sollya working precision.
|
932 |
if not sollyaPrecSo is None: |
933 |
sollya_lib_set_prec(initialPrecSo) |
934 |
sollya_lib_clear_obj(initialPrecSo) |
935 |
return((polyVarChangedSo, intervalCenterSo, maxErrorSo))
|
936 |
# end pobyso_taylor_expansion_with_change_var_so_so
|
937 |
|
938 |
def pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, rangeSo, \ |
939 |
errorTypeSo, \ |
940 |
sollyaPrecSo=None):
|
941 |
"""
|
942 |
Compute the Taylor expansion without the variable change
|
943 |
x -> x-intervalCenter.
|
944 |
"""
|
945 |
# No global change of the working precision.
|
946 |
if not sollyaPrecSo is None: |
947 |
initialPrecSo = sollya_lib_get_prec(None)
|
948 |
sollya_lib_set_prec(sollyaPrecSo) |
949 |
#
|
950 |
intervalCenterSo = sollya_lib_mid(rangeSo) |
951 |
taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \ |
952 |
intervalCenterSo, \ |
953 |
rangeSo, errorTypeSo, None)
|
954 |
(taylorFormListSo, numElements, isEndElliptic) = \ |
955 |
pobyso_get_list_elements_so_so(taylorFormSo) |
956 |
polySo = taylorFormListSo[0]
|
957 |
errorRangeSo = taylorFormListSo[2]
|
958 |
maxErrorSo = sollya_lib_sup(errorRangeSo) |
959 |
# If changed, reset the Sollya working precision.
|
960 |
if not sollyaPrecSo is None: |
961 |
sollya_lib_set_prec(initialPrecSo) |
962 |
sollya_lib_clear_obj(initialPrecSo) |
963 |
return((polySo, intervalCenterSo, maxErrorSo))
|
964 |
# end pobyso_taylor_expansion_no_change_var_so_so
|
965 |
|
966 |
def pobyso_taylor(function, degree, point): |
967 |
""" Legacy function. See pobysoTaylor_so_so. """
|
968 |
return(pobyso_taylor_so_so(function, degree, point))
|
969 |
|
970 |
def pobyso_taylor_so_so(functionSo, degreeSo, pointSo): |
971 |
return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
|
972 |
|
973 |
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None): |
974 |
""" Legacy function. See ;"""
|
975 |
|
976 |
def pobyso_taylorform_sa_sa(functionSa, \ |
977 |
degreeSa, \ |
978 |
pointSa, \ |
979 |
intervalSa=None, \
|
980 |
errorTypeSa=None, \
|
981 |
precisionSa=None):
|
982 |
"""
|
983 |
Compute the Taylor form of 'degree' for 'functionSa' at 'point'
|
984 |
for 'interval' with 'errorType' (a string).
|
985 |
point: must be a Real or a Real interval.
|
986 |
return the Taylor form as an array
|
987 |
TODO: take care of the interval and of the point when it is an interval;
|
988 |
when errorType is not None;
|
989 |
take care of the other elements of the Taylor form (coefficients
|
990 |
errors and delta.
|
991 |
"""
|
992 |
# Absolute as the default error.
|
993 |
if errorTypeSa is None: |
994 |
errorTypeSo = sollya_lib_absolute() |
995 |
elif errorTypeSa == "relative": |
996 |
errorTypeSo = sollya_lib_relative() |
997 |
elif errortypeSa == "absolute": |
998 |
errorTypeSo = sollya_lib_absolute() |
999 |
else:
|
1000 |
# No clean up needed.
|
1001 |
return None |
1002 |
# Global precision stuff
|
1003 |
precisionChangedSa = False
|
1004 |
currentSollyaPrecSo = pobyso_get_prec_so() |
1005 |
currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo) |
1006 |
if not precisionSa is None: |
1007 |
if precisionSa > currentSollyaPrecSa:
|
1008 |
pobyso_set_prec_sa_so(precisionSa) |
1009 |
precisionChangedSa = True
|
1010 |
|
1011 |
varSa = functionSa.variables()[0]
|
1012 |
pobyso_name_free_variable_sa_so(str(varSa))
|
1013 |
# In any case (point or interval) the parent of pointSa has a precision
|
1014 |
# method.
|
1015 |
pointPrecSa = pointSa.parent().precision() |
1016 |
if precisionSa > pointPrecSa:
|
1017 |
pointPrecSa = precisionSa |
1018 |
# In any case (point or interval) pointSa has a base_ring() method.
|
1019 |
pointBaseRingString = str(pointSa.base_ring())
|
1020 |
if re.search('Interval', pointBaseRingString) is None: # Point |
1021 |
pointSo = pobyso_constant_sa_so(pointSa, pointPrecSa) |
1022 |
else: # Interval. |
1023 |
pointSo = pobyso_interval_to_range_sa_so(pointSa, pointPrecSa) |
1024 |
# Sollyafy the function.
|
1025 |
functionSo = pobyso_parse_string_sa_so(functionSa._assume_str()) |
1026 |
if sollya_lib_obj_is_error(functionSo):
|
1027 |
print "pobyso_tailorform: function string can't be parsed!" |
1028 |
return None |
1029 |
# Sollyafy the degree
|
1030 |
degreeSo = sollya_lib_constant_from_int(int(degreeSa))
|
1031 |
# Sollyafy the point
|
1032 |
# Call Sollya
|
1033 |
taylorFormSo = \ |
1034 |
sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\ |
1035 |
None)
|
1036 |
(tfsAsList, numElements, isEndElliptic) = \ |
1037 |
pobyso_get_list_elements_so_so(taylorFormSo) |
1038 |
polySo = tfsAsList[0]
|
1039 |
maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
1040 |
polyRealField = RealField(maxPrecision) |
1041 |
expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField) |
1042 |
if precisionChangedSa:
|
1043 |
sollya_lib_set_prec(currentSollyaPrecSo) |
1044 |
sollya_lib_clear_obj(currentSollyaPrecSo) |
1045 |
# Clean up.
|
1046 |
sollya_lib_clear_obj(pointSo) # Works, whatever the type of pointSo is.
|
1047 |
sollya_lib_clear_obj(errorTypeSo) |
1048 |
sollya_lib_clear_obj(degreeSo) |
1049 |
polynomialRing = polyRealField[str(varSa)]
|
1050 |
polySa = polynomial(expSa, polynomialRing) |
1051 |
taylorFormSa = [polySa] |
1052 |
return(taylorFormSa)
|
1053 |
# End pobyso_taylor_form_sa_sa
|
1054 |
|
1055 |
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \ |
1056 |
errorTypeSo=None):
|
1057 |
createdErrorType = False
|
1058 |
if errorTypeSo is None: |
1059 |
errorTypeSo = sollya_lib_absolute() |
1060 |
createdErrorType = True
|
1061 |
else:
|
1062 |
#TODO: deal with the other case.
|
1063 |
pass
|
1064 |
if intervalSo is None: |
1065 |
resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \ |
1066 |
errorTypeSo, None)
|
1067 |
else:
|
1068 |
resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \ |
1069 |
intervalSo, errorTypeSo, None)
|
1070 |
if createdErrorType:
|
1071 |
sollya_lib_clear_obj(errorTypeSo) |
1072 |
return(resultSo)
|
1073 |
|
1074 |
|
1075 |
def pobyso_univar_polynomial_print_reverse(polySa): |
1076 |
""" Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
|
1077 |
return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
|
1078 |
|
1079 |
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa): |
1080 |
"""
|
1081 |
Return the string representation of a univariate polynomial with
|
1082 |
monomials ordered in the x^0..x^n order of the monomials.
|
1083 |
Remember: Sage
|
1084 |
"""
|
1085 |
polynomialRing = polySa.base_ring() |
1086 |
# A very expensive solution:
|
1087 |
# -create a fake multivariate polynomial field with only one variable,
|
1088 |
# specifying a negative lexicographical order;
|
1089 |
mpolynomialRing = PolynomialRing(polynomialRing.base(), \ |
1090 |
polynomialRing.variable_name(), \ |
1091 |
1, order='neglex') |
1092 |
# - convert the univariate argument polynomial into a multivariate
|
1093 |
# version;
|
1094 |
p = mpolynomialRing(polySa) |
1095 |
# - return the string representation of the converted form.
|
1096 |
# There is no simple str() method defined for p's class.
|
1097 |
return(p.__str__())
|
1098 |
#
|
1099 |
print pobyso_get_prec()
|
1100 |
pobyso_set_prec(165)
|
1101 |
print pobyso_get_prec()
|
1102 |
a=100
|
1103 |
print type(a) |
1104 |
id(a)
|
1105 |
print "Max arity: ", pobyso_max_arity |
1106 |
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43) |
1107 |
print "Function None (44) as a string: ", pobyso_function_type_as_string(44) |
1108 |
print "...Pobyso check done" |