root / pobysoPythonSage / src / pobyso.py @ 52
Historique | Voir | Annoter | Télécharger (26,81 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, includes
|
10 |
the void function and the no arguments function that return a pointer to a Sollya
|
11 |
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 Sollya
|
14 |
world to Sage/Python world);
|
15 |
- the _sa_so (arguments are Sage/Python objects, returned objects are
|
16 |
pointers to Sollya objects);
|
17 |
- the sa_sa (arguments and returned objects are all Sage/Python objects);
|
18 |
- a catch all flavor, without any suffix, (e. g. functions that have no argument
|
19 |
nor return value).
|
20 |
NOTES:
|
21 |
Reported errors in Eclipse come from the calls to
|
22 |
the Sollya library
|
23 |
|
24 |
ToDo (among other things):
|
25 |
-memory management.
|
26 |
"""
|
27 |
from ctypes import * |
28 |
import re |
29 |
from sage.symbolic.expression_conversions import polynomial |
30 |
"""
|
31 |
Create the equivalent to an enum for the Sollya function types.
|
32 |
"""
|
33 |
(SOLLYA_BASE_FUNC_ABS, |
34 |
SOLLYA_BASE_FUNC_ACOS, |
35 |
SOLLYA_BASE_FUNC_ACOSH, |
36 |
SOLLYA_BASE_FUNC_ADD, |
37 |
SOLLYA_BASE_FUNC_ASIN, |
38 |
SOLLYA_BASE_FUNC_ASINH, |
39 |
SOLLYA_BASE_FUNC_ATAN, |
40 |
SOLLYA_BASE_FUNC_ATANH, |
41 |
SOLLYA_BASE_FUNC_CEIL, |
42 |
SOLLYA_BASE_FUNC_CONSTANT, |
43 |
SOLLYA_BASE_FUNC_COS, |
44 |
SOLLYA_BASE_FUNC_COSH, |
45 |
SOLLYA_BASE_FUNC_DIV, |
46 |
SOLLYA_BASE_FUNC_DOUBLE, |
47 |
SOLLYA_BASE_FUNC_DOUBLEDOUBLE, |
48 |
SOLLYA_BASE_FUNC_DOUBLEEXTENDED, |
49 |
SOLLYA_BASE_FUNC_ERF, |
50 |
SOLLYA_BASE_FUNC_ERFC, |
51 |
SOLLYA_BASE_FUNC_EXP, |
52 |
SOLLYA_BASE_FUNC_EXP_M1, |
53 |
SOLLYA_BASE_FUNC_FLOOR, |
54 |
SOLLYA_BASE_FUNC_FREE_VARIABLE, |
55 |
SOLLYA_BASE_FUNC_HALFPRECISION, |
56 |
SOLLYA_BASE_FUNC_LIBRARYCONSTANT, |
57 |
SOLLYA_BASE_FUNC_LIBRARYFUNCTION, |
58 |
SOLLYA_BASE_FUNC_LOG, |
59 |
SOLLYA_BASE_FUNC_LOG_10, |
60 |
SOLLYA_BASE_FUNC_LOG_1P, |
61 |
SOLLYA_BASE_FUNC_LOG_2, |
62 |
SOLLYA_BASE_FUNC_MUL, |
63 |
SOLLYA_BASE_FUNC_NEARESTINT, |
64 |
SOLLYA_BASE_FUNC_NEG, |
65 |
SOLLYA_BASE_FUNC_PI, |
66 |
SOLLYA_BASE_FUNC_POW, |
67 |
SOLLYA_BASE_FUNC_PROCEDUREFUNCTION, |
68 |
SOLLYA_BASE_FUNC_QUAD, |
69 |
SOLLYA_BASE_FUNC_SIN, |
70 |
SOLLYA_BASE_FUNC_SINGLE, |
71 |
SOLLYA_BASE_FUNC_SINH, |
72 |
SOLLYA_BASE_FUNC_SQRT, |
73 |
SOLLYA_BASE_FUNC_SUB, |
74 |
SOLLYA_BASE_FUNC_TAN, |
75 |
SOLLYA_BASE_FUNC_TANH, |
76 |
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44)) |
77 |
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS |
78 |
print "Last constant - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE |
79 |
|
80 |
pobyso_max_arity = 9
|
81 |
|
82 |
def pobyso_autoprint(arg): |
83 |
sollya_lib_autoprint(arg,None)
|
84 |
|
85 |
def pobyso_autoprint_so_so(arg): |
86 |
sollya_lib_autoprint(arg,None)
|
87 |
|
88 |
def pobyso_cmp(rnArg, soCte): |
89 |
precisionOfCte = c_int(0)
|
90 |
# From the Sollya constant, create a local Sage RealNumber.
|
91 |
sollya_lib_get_prec_of_constant(precisionOfCte, soCte) |
92 |
#print "Precision of constant: ", precisionOfCte
|
93 |
RRRR = RealField(precisionOfCte.value) |
94 |
rnLocal = RRRR(0)
|
95 |
sollya_lib_get_constant(get_rn_value(rnLocal), soCte) |
96 |
#print "rnDummy: ", rnDummy
|
97 |
# Compare the local Sage RealNumber with rnArg.
|
98 |
return(cmp_rn_value(rnArg, rnLocal))
|
99 |
|
100 |
def pobyso_constant(rnArg): |
101 |
""" Legacy function. See pobyso_constant_sa_so. """
|
102 |
return(pobyso_constant_sa_so(rnArg))
|
103 |
|
104 |
def pobyso_constant_sa_so(rnArg): |
105 |
"""
|
106 |
Create a Sollya constant from a RealNumber.
|
107 |
"""
|
108 |
return(sollya_lib_constant(get_rn_value(rnArg)))
|
109 |
|
110 |
def pobyso_constant_1(): |
111 |
""" Legacy function. See pobyso_constant_so_so. """
|
112 |
return(pobyso_constant_1_sa_so())
|
113 |
|
114 |
def pobyso_constant_1_sa_so(): |
115 |
return(pobyso_constant_from_int_sa_so(1)) |
116 |
|
117 |
def pobyso_constant_from_int(anInt): |
118 |
""" Legacy function. See pobyso_constant_from_int_sa_so. """
|
119 |
return(pobyso_constant_from_int_sa_so(anInt))
|
120 |
|
121 |
def pobyso_constant_from_int_sa_so(anInt): |
122 |
return(sollya_lib_constant_from_int(int(anInt))) |
123 |
|
124 |
def pobyso_function_type_as_string(funcType): |
125 |
""" Legacy function. See pobyso_function_type_as_string_so_sa. """
|
126 |
return(pobyso_function_type_as_string_so_sa(funcType))
|
127 |
|
128 |
def pobyso_function_type_as_string_so_sa(funcType): |
129 |
"""
|
130 |
Numeric Sollya function codes -> Sage mathematical function names.
|
131 |
Notice that pow -> ^ (a la Sage, not a la Python).
|
132 |
"""
|
133 |
if funcType == SOLLYA_BASE_FUNC_ABS:
|
134 |
return "abs" |
135 |
elif funcType == SOLLYA_BASE_FUNC_ACOS:
|
136 |
return "arccos" |
137 |
elif funcType == SOLLYA_BASE_FUNC_ACOSH:
|
138 |
return "arccosh" |
139 |
elif funcType == SOLLYA_BASE_FUNC_ADD:
|
140 |
return "+" |
141 |
elif funcType == SOLLYA_BASE_FUNC_ASIN:
|
142 |
return "arcsin" |
143 |
elif funcType == SOLLYA_BASE_FUNC_ASINH:
|
144 |
return "arcsinh" |
145 |
elif funcType == SOLLYA_BASE_FUNC_ATAN:
|
146 |
return "arctan" |
147 |
elif funcType == SOLLYA_BASE_FUNC_ATANH:
|
148 |
return "arctanh" |
149 |
elif funcType == SOLLYA_BASE_FUNC_CEIL:
|
150 |
return "ceil" |
151 |
elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
|
152 |
return "cte" |
153 |
elif funcType == SOLLYA_BASE_FUNC_COS:
|
154 |
return "cos" |
155 |
elif funcType == SOLLYA_BASE_FUNC_COSH:
|
156 |
return "cosh" |
157 |
elif funcType == SOLLYA_BASE_FUNC_DIV:
|
158 |
return "/" |
159 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
|
160 |
return "double" |
161 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
|
162 |
return "doubleDouble" |
163 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
|
164 |
return "doubleDxtended" |
165 |
elif funcType == SOLLYA_BASE_FUNC_ERF:
|
166 |
return "erf" |
167 |
elif funcType == SOLLYA_BASE_FUNC_ERFC:
|
168 |
return "erfc" |
169 |
elif funcType == SOLLYA_BASE_FUNC_EXP:
|
170 |
return "exp" |
171 |
elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
|
172 |
return "expm1" |
173 |
elif funcType == SOLLYA_BASE_FUNC_FLOOR:
|
174 |
return "floor" |
175 |
elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
176 |
return "freeVariable" |
177 |
elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
|
178 |
return "halfPrecision" |
179 |
elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
|
180 |
return "libraryConstant" |
181 |
elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
|
182 |
return "libraryFunction" |
183 |
elif funcType == SOLLYA_BASE_FUNC_LOG:
|
184 |
return "log" |
185 |
elif funcType == SOLLYA_BASE_FUNC_LOG_10:
|
186 |
return "log10" |
187 |
elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
|
188 |
return "log1p" |
189 |
elif funcType == SOLLYA_BASE_FUNC_LOG_2:
|
190 |
return "log2" |
191 |
elif funcType == SOLLYA_BASE_FUNC_MUL:
|
192 |
return "*" |
193 |
elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
|
194 |
return "round" |
195 |
elif funcType == SOLLYA_BASE_FUNC_NEG:
|
196 |
return "__neg__" |
197 |
elif funcType == SOLLYA_BASE_FUNC_PI:
|
198 |
return "pi" |
199 |
elif funcType == SOLLYA_BASE_FUNC_POW:
|
200 |
return "^" |
201 |
elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
|
202 |
return "procedureFunction" |
203 |
elif funcType == SOLLYA_BASE_FUNC_QUAD:
|
204 |
return "quad" |
205 |
elif funcType == SOLLYA_BASE_FUNC_SIN:
|
206 |
return "sin" |
207 |
elif funcType == SOLLYA_BASE_FUNC_SINGLE:
|
208 |
return "single" |
209 |
elif funcType == SOLLYA_BASE_FUNC_SINH:
|
210 |
return "sinh" |
211 |
elif funcType == SOLLYA_BASE_FUNC_SQRT:
|
212 |
return "sqrt" |
213 |
elif funcType == SOLLYA_BASE_FUNC_SUB:
|
214 |
return "-" |
215 |
elif funcType == SOLLYA_BASE_FUNC_TAN:
|
216 |
return "tan" |
217 |
elif funcType == SOLLYA_BASE_FUNC_TANH:
|
218 |
return "tanh" |
219 |
elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
|
220 |
return "tripleDouble" |
221 |
else:
|
222 |
return None |
223 |
|
224 |
def pobyso_get_constant(rnArg, soConst): |
225 |
""" Legacy function. See pobyso_get_constant_so_sa. """
|
226 |
pobyso_get_constant_so_sa(rnArg, soConst) |
227 |
|
228 |
def pobyso_get_constant_so_sa(rnArg, soConst): |
229 |
"""
|
230 |
Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
|
231 |
rnArg must already exist and belong to some RealField.
|
232 |
We assume that soConst points to a Sollya constant.
|
233 |
"""
|
234 |
sollya_lib_get_constant(get_rn_value(rnArg), soConst) |
235 |
|
236 |
def pobyso_get_constant_as_rn(ctExp): |
237 |
""" Legacy function. See pobyso_get_constant_as_rn_so_sa. """
|
238 |
return(pobyso_get_constant_as_rn_so_sa(ctExp))
|
239 |
|
240 |
def pobyso_get_constant_as_rn_so_sa(constExp): |
241 |
precision = pobyso_get_prec_of_constant(constExp) |
242 |
RRRR = RealField(precision) |
243 |
rn = RRRR(0)
|
244 |
sollya_lib_get_constant(get_rn_value(rn), constExp) |
245 |
return(rn)
|
246 |
|
247 |
def pobyso_get_constant_as_rn_with_rf(ctExp, realField): |
248 |
""" Legacy function. See ."""
|
249 |
return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
|
250 |
|
251 |
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField): |
252 |
rn = realField(0)
|
253 |
sollya_lib_get_constant(get_rn_value(rn), ctExp) |
254 |
return(rn)
|
255 |
|
256 |
def pobyso_get_free_variable_name(): |
257 |
""" Legacy function. See pobyso_get_free_variable_name_so_sa."""
|
258 |
return(pobyso_get_free_variable_name_so_sa())
|
259 |
|
260 |
def pobyso_get_free_variable_name_so_sa(): |
261 |
return(sollya_lib_get_free_variable_name())
|
262 |
|
263 |
def pobyso_get_function_arity(expressionSo): |
264 |
""" Legacy function. See pobyso_get_function_arity_so_sa."""
|
265 |
return(pobyso_get_function_arity_so_sa(expressionSo))
|
266 |
|
267 |
def pobyso_get_function_arity_so_sa(expressionSo): |
268 |
arity = c_int(0)
|
269 |
sollya_lib_get_function_arity(byref(arity),expressionSo) |
270 |
return(int(arity.value)) |
271 |
|
272 |
def pobyso_get_head_function(expressionSo): |
273 |
""" Legacy function. See pobyso_get_head_function_so_sa. """
|
274 |
return(pobyso_get_head_function_so_sa(expressionSo))
|
275 |
|
276 |
def pobyso_get_head_function_so_sa(expressionSo): |
277 |
functionType = c_int(0)
|
278 |
sollya_lib_get_head_function(byref(functionType), expressionSo, None)
|
279 |
return(int(functionType.value)) |
280 |
|
281 |
def pobyso_get_list_elements(soObj): |
282 |
""" Legacy function. See pobyso_get_list_elements_so_so. """
|
283 |
return(pobyso_get_list_elements_so_so(soObj))
|
284 |
|
285 |
def pobyso_get_list_elements_so_so(soObj): |
286 |
"""
|
287 |
Get the list elements as a Sage/Python array of Sollya objects.
|
288 |
The other data returned are also Sage/Python objects.
|
289 |
"""
|
290 |
listAddress = POINTER(c_longlong)() |
291 |
numElements = c_int(0)
|
292 |
isEndElliptic = c_int(0)
|
293 |
listAsList = [] |
294 |
result = sollya_lib_get_list_elements(byref(listAddress),\ |
295 |
byref(numElements),\ |
296 |
byref(isEndElliptic),\ |
297 |
soObj) |
298 |
if result == 0 : |
299 |
return None |
300 |
for i in xrange(0, numElements.value, 1): |
301 |
listAsList.append(listAddress[i]) |
302 |
return(listAsList, numElements.value, isEndElliptic.value)
|
303 |
|
304 |
def pobyso_get_max_prec_of_exp(soExp): |
305 |
""" Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
|
306 |
return(pobyso_get_max_prec_of_exp_so_sa(soExp))
|
307 |
|
308 |
def pobyso_get_max_prec_of_exp_so_sa(soExp): |
309 |
"""
|
310 |
Get the maximum precision used for the numbers in a Sollya expression.
|
311 |
|
312 |
Arguments:
|
313 |
soExp -- a Sollya expression pointer
|
314 |
Return value:
|
315 |
A Python integer
|
316 |
TODO:
|
317 |
- error management;
|
318 |
- correctly deal with numerical type such as DOUBLEEXTENDED.
|
319 |
"""
|
320 |
maxPrecision = 0
|
321 |
minConstPrec = 0
|
322 |
currentConstPrec = 0
|
323 |
operator = pobyso_get_head_function_so_sa(soExp) |
324 |
if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \ |
325 |
(operator != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
326 |
(arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp) |
327 |
for i in xrange(arity): |
328 |
maxPrecisionCandidate = \ |
329 |
pobyso_get_max_prec_of_exp_so_sa(subexpressions[i]) |
330 |
if maxPrecisionCandidate > maxPrecision:
|
331 |
maxPrecision = maxPrecisionCandidate |
332 |
return(maxPrecision)
|
333 |
elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
334 |
minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp) |
335 |
#currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
|
336 |
#print minConstPrec, " - ", currentConstPrec
|
337 |
return(pobyso_get_min_prec_of_constant_so_sa(soExp))
|
338 |
|
339 |
elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
340 |
return(0) |
341 |
else:
|
342 |
print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator." |
343 |
return(0) |
344 |
|
345 |
def pobyso_get_min_prec_of_constant_so_sa(soConstExp): |
346 |
"""
|
347 |
Get the minimum precision necessary to represent the value of a Sollya
|
348 |
constant.
|
349 |
MPFR_MIN_PREC and powers of 2 are taken into account.
|
350 |
We assume that soCteExp is a point
|
351 |
"""
|
352 |
constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp) |
353 |
return(min_mpfr_size(get_rn_value(constExpAsRn)))
|
354 |
|
355 |
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR): |
356 |
""" Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
|
357 |
return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
|
358 |
|
359 |
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR): |
360 |
"""
|
361 |
Get a Sage expression from a Sollya expression.
|
362 |
Currently only tested with polynomials with floating-point coefficients.
|
363 |
Notice that, in the returned polynomial, the exponents are RealNumbers.
|
364 |
"""
|
365 |
#pobyso_autoprint(sollyaExp)
|
366 |
operator = pobyso_get_head_function(sollyaExp) |
367 |
# Constants and the free variable are special cases.
|
368 |
# All other operator are dealt with in the same way.
|
369 |
if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \ |
370 |
(operator != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
371 |
(arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp) |
372 |
if arity == 1: |
373 |
sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
|
374 |
"(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \ |
375 |
realField) + ")")
|
376 |
elif arity == 2: |
377 |
if operator == SOLLYA_BASE_FUNC_POW:
|
378 |
operatorAsString = "**"
|
379 |
else:
|
380 |
operatorAsString = \ |
381 |
pobyso_function_type_as_string_so_sa(operator) |
382 |
sageExp = \ |
383 |
eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\ |
384 |
+ " " + operatorAsString + " " + \ |
385 |
"pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
|
386 |
# We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
|
387 |
else:
|
388 |
sageExp = eval('None') |
389 |
return(sageExp)
|
390 |
elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
391 |
#print "This is a constant"
|
392 |
return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
|
393 |
elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
394 |
#print "This is free variable"
|
395 |
return(eval(sollya_lib_get_free_variable_name())) |
396 |
else:
|
397 |
print "Unexpected" |
398 |
return eval('None') |
399 |
# End pobyso_get_sage_poly_from_sollya_poly
|
400 |
|
401 |
def pobyso_get_subfunctions(expressionSo): |
402 |
""" Legacy function. See pobyso_get_subfunctions_so_sa. """
|
403 |
return(pobyso_get_subfunctions_so_sa(expressionSo))
|
404 |
|
405 |
def pobyso_get_subfunctions_so_sa(expressionSo): |
406 |
"""
|
407 |
Get the subfunctions of an expression.
|
408 |
Return the number of subfunctions and the list of subfunctions addresses.
|
409 |
Could not figure out another way than that ugly list of declarations
|
410 |
to recover the addresses of the subfunctions.
|
411 |
Arity is limited to 9.
|
412 |
"""
|
413 |
subf0 = c_int(0)
|
414 |
subf1 = c_int(0)
|
415 |
subf2 = c_int(0)
|
416 |
subf3 = c_int(0)
|
417 |
subf4 = c_int(0)
|
418 |
subf5 = c_int(0)
|
419 |
subf6 = c_int(0)
|
420 |
subf7 = c_int(0)
|
421 |
subf8 = c_int(0)
|
422 |
arity = c_int(0)
|
423 |
nullPtr = POINTER(c_int)() |
424 |
sollya_lib_get_subfunctions(expressionSo, byref(arity), \ |
425 |
byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\ |
426 |
byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
|
427 |
# byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
|
428 |
# byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
|
429 |
# byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
|
430 |
# byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
|
431 |
# byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
|
432 |
subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8] |
433 |
subs = [] |
434 |
if arity.value > pobyso_max_arity:
|
435 |
return(0,[]) |
436 |
for i in xrange(arity.value): |
437 |
subs.append(int(subfunctions[i].value))
|
438 |
#print subs[i]
|
439 |
return(int(arity.value), subs) |
440 |
|
441 |
def pobyso_get_prec(): |
442 |
""" Legacy function. See pobyso_get_prec_so_sa(). """
|
443 |
return(pobyso_get_prec_so_sa())
|
444 |
|
445 |
def pobyso_get_prec_so_sa(): |
446 |
"""
|
447 |
Get the current default precision in Sollya.
|
448 |
The return value is Sage/Python int.
|
449 |
"""
|
450 |
retc = sollya_lib_get_prec(None)
|
451 |
a = c_int(0)
|
452 |
sollya_lib_get_constant_as_int(byref(a), retc) |
453 |
return(int(a.value)) |
454 |
|
455 |
def pobyso_get_prec_of_constant(ctExpSo): |
456 |
""" Legacy function. See pobyso_get_prec_of_constant_so_sa. """
|
457 |
return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
|
458 |
|
459 |
def pobyso_get_prec_of_constant_so_sa(ctExpSo): |
460 |
prec = c_int(0)
|
461 |
retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
|
462 |
return(int(prec.value)) |
463 |
|
464 |
def pobyso_lib_init(): |
465 |
sollya_lib_init(None)
|
466 |
|
467 |
def pobyso_name_free_variable(freeVariableName): |
468 |
""" Legacy function. See pobyso_name_free_variable_sa_so. """
|
469 |
pobyso_name_free_variable_sa_so(freeVariableName) |
470 |
|
471 |
def pobyso_name_free_variable_sa_so(freeVariableName): |
472 |
sollya_lib_name_free_variable(freeVariableName) |
473 |
|
474 |
def pobyso_parse_string(string): |
475 |
""" Legacy function. See pobyso_parse_string_sa_so. """
|
476 |
return(pobyso_parse_string_sa_so(string))
|
477 |
|
478 |
def pobyso_parse_string_sa_so(string): |
479 |
return(sollya_lib_parse_string(string))
|
480 |
|
481 |
def pobyso_range(rnLowerBound, rnUpperBound): |
482 |
""" Legacy function. See pobyso_range_sa_so. """
|
483 |
return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
|
484 |
|
485 |
def pobyso_range_sa_so(rnLowerBound, rnUpperBound): |
486 |
lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound)) |
487 |
upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound)) |
488 |
rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo) |
489 |
return(rangeSo)
|
490 |
|
491 |
def pobyso_remez_canonical_sa_sa(func, \ |
492 |
degree, \ |
493 |
lowerBound, \ |
494 |
upperBound, \ |
495 |
weight = None, \
|
496 |
quality = None):
|
497 |
"""
|
498 |
All arguments are Sage/Python.
|
499 |
The functions (func and weight) must be passed as expressions or strings.
|
500 |
Otherwise the function fails.
|
501 |
The return value is a pointer is a Sage polynomial.
|
502 |
"""
|
503 |
var('zorglub') # Dummy variable name for type check only. |
504 |
polySo = pobyso_remez_canonical_sa_so(func, \ |
505 |
degree, \ |
506 |
lowerBound, \ |
507 |
upperBound, \ |
508 |
weight = None, \
|
509 |
quality = None)
|
510 |
if parent(func) == parent("string"): |
511 |
functionSa = eval(func)
|
512 |
# Expression test.
|
513 |
elif type(func) == type(zorglub): |
514 |
functionSa = func |
515 |
maxPrecision = 0
|
516 |
if polySo is None: |
517 |
return(None) |
518 |
maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
519 |
RRRR = RealField(maxPrecision) |
520 |
polynomialRing = RRRR[functionSa.variables()[0]]
|
521 |
expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR) |
522 |
polySa = polynomial(expSa, polynomialRing) |
523 |
return(polySa)
|
524 |
|
525 |
def pobyso_remez_canonical(func, \ |
526 |
degree, \ |
527 |
lowerBound, \ |
528 |
upperBound, \ |
529 |
weight = "1", \
|
530 |
quality = None):
|
531 |
""" Legacy function. See pobyso_remez_canonical_sa_so. """
|
532 |
return(pobyso_remez_canonical_sa_so(func, \
|
533 |
degree, \ |
534 |
lowerBound, \ |
535 |
upperBound, \ |
536 |
weight, \ |
537 |
quality)) |
538 |
def pobyso_remez_canonical_sa_so(func, \ |
539 |
degree, \ |
540 |
lowerBound, \ |
541 |
upperBound, \ |
542 |
weight = None, \
|
543 |
quality = None):
|
544 |
"""
|
545 |
All arguments are Sage/Python.
|
546 |
The functions (func and weight) must be passed as expressions or strings.
|
547 |
Otherwise the function fails.
|
548 |
The return value is a pointer to a Sollya function.
|
549 |
"""
|
550 |
var('zorglub') # Dummy variable name for type check only. |
551 |
currentVariableName = None
|
552 |
# The func argument can be of different types (string,
|
553 |
# symbolic expression...)
|
554 |
if parent(func) == parent("string"): |
555 |
functionSo = sollya_lib_parse_string(func) |
556 |
# Expression test.
|
557 |
elif type(func) == type(zorglub): |
558 |
# Until we are able to translate Sage expressions into Sollya
|
559 |
# expressions : parse the string version.
|
560 |
currentVariableName = func.variables()[0]
|
561 |
sollya_lib_name_free_variable(str(currentVariableName))
|
562 |
functionSo = sollya_lib_parse_string(func._assume_str()) |
563 |
else:
|
564 |
return(None) |
565 |
if weight is None: |
566 |
weightSo = pobyso_constant_1_sa_so() |
567 |
elif parent(weight) == parent("string"): |
568 |
weightSo = sollya_lib_parse_string(func) |
569 |
elif type(weight) == type(zorglub): |
570 |
functionSo = sollya_lib_parse_string_sa_so(weight._assume_str()) |
571 |
else:
|
572 |
return(None) |
573 |
degreeSo = pobyso_constant_from_int(degree) |
574 |
rangeSo = pobyso_range_sa_so(lowerBound, upperBound) |
575 |
if not quality is None: |
576 |
qualitySo= pobyso_constant_sa_so(quality) |
577 |
else:
|
578 |
qualitySo = None
|
579 |
return(sollya_lib_remez(functionSo, \
|
580 |
degreeSo, \ |
581 |
rangeSo, \ |
582 |
weightSo, \ |
583 |
qualitySo, \ |
584 |
None))
|
585 |
|
586 |
def pobyso_remez_canonical_so_so(funcSo, \ |
587 |
degreeSo, \ |
588 |
rangeSo, \ |
589 |
weightSo = pobyso_constant_1_sa_so(),\ |
590 |
qualitySo = None):
|
591 |
"""
|
592 |
All arguments are pointers to Sollya objects.
|
593 |
The return value is a pointer to a Sollya function.
|
594 |
"""
|
595 |
if not sollya_lib_obj_is_function(funcSo): |
596 |
return(None) |
597 |
return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None)) |
598 |
|
599 |
def pobyso_set_canonical_off(): |
600 |
sollya_lib_set_canonical(sollya_lib_off()) |
601 |
|
602 |
def pobyso_set_canonical_on(): |
603 |
sollya_lib_set_canonical(sollya_lib_on()) |
604 |
|
605 |
def pobyso_set_prec(p): |
606 |
""" Legacy function. See pobyso_set_prec_sa_so. """
|
607 |
return( pobyso_set_prec_sa_so(p))
|
608 |
|
609 |
def pobyso_set_prec_sa_so(p): |
610 |
a = c_int(p) |
611 |
precSo = c_void_p(sollya_lib_constant_from_int(a)) |
612 |
sollya_lib_set_prec(precSo) |
613 |
|
614 |
def pobyso_taylor(function, degree, point): |
615 |
""" Legacy function. See pobysoTaylor_so_so. """
|
616 |
return(pobyso_taylor_so_so(function, degree, point))
|
617 |
|
618 |
def pobyso_taylor_so_so(function, degree, point): |
619 |
return(sollya_lib_taylor(function, degree, point))
|
620 |
|
621 |
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None): |
622 |
""" Legacy function. See ;"""
|
623 |
|
624 |
def pobyso_taylorform_sa_sa(functionSa, \ |
625 |
degree, \ |
626 |
point, \ |
627 |
precision, \ |
628 |
interval = None, \
|
629 |
errorType=None):
|
630 |
"""
|
631 |
Compute the Taylor form of 'degree' for 'functionSa' at 'point'
|
632 |
for 'interval' with 'errorType'.
|
633 |
point: must be a Real or a Real interval.
|
634 |
return the Taylor form as an array
|
635 |
TODO: take care of the interval and of point when it is an interval;
|
636 |
when errorType is not None;
|
637 |
take care of the other elements of the Taylor form (coefficients errors and
|
638 |
delta.
|
639 |
"""
|
640 |
# Absolute as the default error.
|
641 |
if errorType is None: |
642 |
errorTypeSo = sollya_lib_absolute() |
643 |
else:
|
644 |
#TODO: deal with the other case.
|
645 |
pass
|
646 |
varSa = functionSa.variables()[0]
|
647 |
pointBaseRingString = str(point.base_ring())
|
648 |
if not re.search('Real', pointBaseRingString): |
649 |
return None |
650 |
# Call Sollya but first "sollyafy" the arguments.
|
651 |
sollya_lib_init(None)
|
652 |
pobyso_name_free_variable_sa_so(str(varSa))
|
653 |
#pobyso_set_prec_sa_so(300)
|
654 |
# Sollyafy the function.
|
655 |
functionSo = pobyso_parse_string_sa_so(functionSa._assume_str()) |
656 |
if sollya_lib_obj_is_error(functionSo):
|
657 |
print "pobyso_tailorform: function string can't be parsed!" |
658 |
return None |
659 |
# Sollyafy the degree
|
660 |
degreeSo = sollya_lib_constant_from_int(int(degree))
|
661 |
# Sollyafy the point
|
662 |
if not re.search('Interval', pointBaseRingString): |
663 |
pointSo = pobyso_constant_sa_so(point) |
664 |
else:
|
665 |
# TODO: deal with the interval case.
|
666 |
pass
|
667 |
# Call Sollya
|
668 |
taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\ |
669 |
None)
|
670 |
(tfsAsList, numElements, isEndElliptic) = \ |
671 |
pobyso_get_list_elements_so_so(taylorFormSo) |
672 |
polySo = tfsAsList[0]
|
673 |
maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
674 |
polyRealField = RealField(maxPrecision) |
675 |
expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField) |
676 |
sollya_lib_close() |
677 |
polynomialRing = polyRealField[str(varSa)]
|
678 |
polySa = polynomial(expSa, polynomialRing) |
679 |
taylorFormSa = [polySa] |
680 |
return(taylorFormSa)
|
681 |
# End pobyso_taylor_form_sa_sa
|
682 |
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, errorTypeSo=None): |
683 |
if errorTypeSo is None: |
684 |
errorTypeSo = sollya_lib_absolute() |
685 |
else:
|
686 |
#TODO: deal with the other case.
|
687 |
pass
|
688 |
if intervalSo is None: |
689 |
resultSo = sollya_lib_taylorform(functionSo, degree, pointSo, errorTypeSo, None)
|
690 |
else:
|
691 |
resultSo = sollya_lib_taylorform(functionSo, degree, pointSo, intervalSo, errorTypeSo, None)
|
692 |
sollya_lib_clear_obj(errorTypeSo) |
693 |
return(resultSo)
|
694 |
|
695 |
|
696 |
def pobyso_univar_polynomial_print_reverse(polySa): |
697 |
""" Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
|
698 |
return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
|
699 |
|
700 |
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa): |
701 |
"""
|
702 |
Return the string representation of a univariate polynomial with
|
703 |
monomials ordered in the x^0..x^n order of the monomials.
|
704 |
Remember: Sage
|
705 |
"""
|
706 |
polynomialRing = polySa.base_ring() |
707 |
# A very expensive solution:
|
708 |
# -create a fake multivariate polynomial field with only one variable,
|
709 |
# specifying a negative lexicographical order;
|
710 |
mpolynomialRing = PolynomialRing(polynomialRing.base(), \ |
711 |
polynomialRing.variable_name(), \ |
712 |
1, order='neglex') |
713 |
# - convert the univariate argument polynomial into a multivariate
|
714 |
# version;
|
715 |
p = mpolynomialRing(polySa) |
716 |
# - return the string representation of the converted form.
|
717 |
# There is no simple str() method defined for p's class.
|
718 |
return(p.__str__())
|
719 |
#
|
720 |
print "Superficial test of pobyso:" |
721 |
print pobyso_get_prec()
|
722 |
pobyso_set_prec(165)
|
723 |
print pobyso_get_prec()
|
724 |
a=100
|
725 |
print type(a) |
726 |
id(a)
|
727 |
print "Max arity: ", pobyso_max_arity |
728 |
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43) |
729 |
print "Function None (44) as a string: ", pobyso_function_type_as_string(44) |