|
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 |
NOTES:
|
|
9 |
Reported errors in Eclipse come from the calls to
|
|
10 |
the Sollya library
|
|
11 |
|
|
12 |
ToDo (among other things): memory management.
|
|
13 |
"""
|
|
14 |
from ctypes import *
|
|
15 |
|
|
16 |
(SOLLYA_BASE_FUNC_ABS,
|
|
17 |
SOLLYA_BASE_FUNC_ACOS,
|
|
18 |
SOLLYA_BASE_FUNC_ACOSH,
|
|
19 |
SOLLYA_BASE_FUNC_ADD,
|
|
20 |
SOLLYA_BASE_FUNC_ASIN,
|
|
21 |
SOLLYA_BASE_FUNC_ASINH,
|
|
22 |
SOLLYA_BASE_FUNC_ATAN,
|
|
23 |
SOLLYA_BASE_FUNC_ATANH,
|
|
24 |
SOLLYA_BASE_FUNC_CEIL,
|
|
25 |
SOLLYA_BASE_FUNC_CONSTANT,
|
|
26 |
SOLLYA_BASE_FUNC_COS,
|
|
27 |
SOLLYA_BASE_FUNC_COSH,
|
|
28 |
SOLLYA_BASE_FUNC_DIV,
|
|
29 |
SOLLYA_BASE_FUNC_DOUBLE,
|
|
30 |
SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
|
|
31 |
SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
|
|
32 |
SOLLYA_BASE_FUNC_ERF,
|
|
33 |
SOLLYA_BASE_FUNC_ERFC,
|
|
34 |
SOLLYA_BASE_FUNC_EXP,
|
|
35 |
SOLLYA_BASE_FUNC_EXP_M1,
|
|
36 |
SOLLYA_BASE_FUNC_FLOOR,
|
|
37 |
SOLLYA_BASE_FUNC_FREE_VARIABLE,
|
|
38 |
SOLLYA_BASE_FUNC_HALFPRECISION,
|
|
39 |
SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
|
|
40 |
SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
|
|
41 |
SOLLYA_BASE_FUNC_LOG,
|
|
42 |
SOLLYA_BASE_FUNC_LOG_10,
|
|
43 |
SOLLYA_BASE_FUNC_LOG_1P,
|
|
44 |
SOLLYA_BASE_FUNC_LOG_2,
|
|
45 |
SOLLYA_BASE_FUNC_MUL,
|
|
46 |
SOLLYA_BASE_FUNC_NEARESTINT,
|
|
47 |
SOLLYA_BASE_FUNC_NEG,
|
|
48 |
SOLLYA_BASE_FUNC_PI,
|
|
49 |
SOLLYA_BASE_FUNC_POW,
|
|
50 |
SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
|
|
51 |
SOLLYA_BASE_FUNC_QUAD,
|
|
52 |
SOLLYA_BASE_FUNC_SIN,
|
|
53 |
SOLLYA_BASE_FUNC_SINGLE,
|
|
54 |
SOLLYA_BASE_FUNC_SINH,
|
|
55 |
SOLLYA_BASE_FUNC_SQRT,
|
|
56 |
SOLLYA_BASE_FUNC_SUB,
|
|
57 |
SOLLYA_BASE_FUNC_TAN,
|
|
58 |
SOLLYA_BASE_FUNC_TANH,
|
|
59 |
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
|
|
60 |
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
|
|
61 |
print "Last constant - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
|
|
62 |
|
|
63 |
pobyso_max_arity = 9
|
|
64 |
|
|
65 |
def pobyso_autoprint(arg):
|
|
66 |
sollya_lib_autoprint(arg,None)
|
|
67 |
|
|
68 |
def pobyso_cmp(rnArg, soCte):
|
|
69 |
precisionOfCte = c_int(0)
|
|
70 |
# From the Sollya constant, create a local Sage RealNumber.
|
|
71 |
sollya_lib_get_prec_of_constant(precisionOfCte, soCte)
|
|
72 |
#print "Precision of constant: ", precisionOfCte
|
|
73 |
RRRR = RealField(precisionOfCte.value)
|
|
74 |
rnLocal = RRRR(0)
|
|
75 |
sollya_lib_get_constant(get_rn_value(rnLocal), soCte)
|
|
76 |
#print "rnDummy: ", rnDummy
|
|
77 |
# Compare the local Sage RealNumber with rnArg.
|
|
78 |
return(cmp_rn_value(rnArg, rnLocal))
|
|
79 |
|
|
80 |
def pobyso_constant(rnArg):
|
|
81 |
return (sollya_lib_constant(get_rn_value(rnArg)))
|
|
82 |
|
|
83 |
def pobyso_constant_1():
|
|
84 |
return(pobyso_constant_from_int(1))
|
|
85 |
|
|
86 |
def pobyso_constant_from_int(anInt):
|
|
87 |
return(sollya_lib_constant_from_int(int(anInt)))
|
|
88 |
|
|
89 |
# Numeric Sollya function codes -> Sage mathematical function names
|
|
90 |
def pobyso_function_type_as_string(funcType):
|
|
91 |
if funcType == SOLLYA_BASE_FUNC_ABS:
|
|
92 |
return "abs"
|
|
93 |
elif funcType == SOLLYA_BASE_FUNC_ACOS:
|
|
94 |
return "arccos"
|
|
95 |
elif funcType == SOLLYA_BASE_FUNC_ACOSH:
|
|
96 |
return "arccosh"
|
|
97 |
elif funcType == SOLLYA_BASE_FUNC_ADD:
|
|
98 |
return "+"
|
|
99 |
elif funcType == SOLLYA_BASE_FUNC_ASIN:
|
|
100 |
return "arcsin"
|
|
101 |
elif funcType == SOLLYA_BASE_FUNC_ASINH:
|
|
102 |
return "arcsinh"
|
|
103 |
elif funcType == SOLLYA_BASE_FUNC_ATAN:
|
|
104 |
return "arctan"
|
|
105 |
elif funcType == SOLLYA_BASE_FUNC_ATANH:
|
|
106 |
return "arctanh"
|
|
107 |
elif funcType == SOLLYA_BASE_FUNC_CEIL:
|
|
108 |
return "ceil"
|
|
109 |
elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
|
|
110 |
return "cte"
|
|
111 |
elif funcType == SOLLYA_BASE_FUNC_COS:
|
|
112 |
return "cos"
|
|
113 |
elif funcType == SOLLYA_BASE_FUNC_COSH:
|
|
114 |
return "cosh"
|
|
115 |
elif funcType == SOLLYA_BASE_FUNC_DIV:
|
|
116 |
return "/"
|
|
117 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
|
|
118 |
return "double"
|
|
119 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
|
|
120 |
return "doubleDouble"
|
|
121 |
elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
|
|
122 |
return "doubleDxtended"
|
|
123 |
elif funcType == SOLLYA_BASE_FUNC_ERF:
|
|
124 |
return "erf"
|
|
125 |
elif funcType == SOLLYA_BASE_FUNC_ERFC:
|
|
126 |
return "erfc"
|
|
127 |
elif funcType == SOLLYA_BASE_FUNC_EXP:
|
|
128 |
return "exp"
|
|
129 |
elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
|
|
130 |
return "expm1"
|
|
131 |
elif funcType == SOLLYA_BASE_FUNC_FLOOR:
|
|
132 |
return "floor"
|
|
133 |
elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
|
134 |
return "freeVariable"
|
|
135 |
elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
|
|
136 |
return "halfPrecision"
|
|
137 |
elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
|
|
138 |
return "libraryConstant"
|
|
139 |
elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
|
|
140 |
return "libraryFunction"
|
|
141 |
elif funcType == SOLLYA_BASE_FUNC_LOG:
|
|
142 |
return "log"
|
|
143 |
elif funcType == SOLLYA_BASE_FUNC_LOG_10:
|
|
144 |
return "log10"
|
|
145 |
elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
|
|
146 |
return "log1p"
|
|
147 |
elif funcType == SOLLYA_BASE_FUNC_LOG_2:
|
|
148 |
return "log2"
|
|
149 |
elif funcType == SOLLYA_BASE_FUNC_MUL:
|
|
150 |
return "*"
|
|
151 |
elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
|
|
152 |
return "round"
|
|
153 |
elif funcType == SOLLYA_BASE_FUNC_NEG:
|
|
154 |
return "__neg__"
|
|
155 |
elif funcType == SOLLYA_BASE_FUNC_PI:
|
|
156 |
return "pi"
|
|
157 |
elif funcType == SOLLYA_BASE_FUNC_POW:
|
|
158 |
return "^"
|
|
159 |
elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
|
|
160 |
return "procedureFunction"
|
|
161 |
elif funcType == SOLLYA_BASE_FUNC_QUAD:
|
|
162 |
return "quad"
|
|
163 |
elif funcType == SOLLYA_BASE_FUNC_SIN:
|
|
164 |
return "sin"
|
|
165 |
elif funcType == SOLLYA_BASE_FUNC_SINGLE:
|
|
166 |
return "single"
|
|
167 |
elif funcType == SOLLYA_BASE_FUNC_SINH:
|
|
168 |
return "sinh"
|
|
169 |
elif funcType == SOLLYA_BASE_FUNC_SQRT:
|
|
170 |
return "sqrt"
|
|
171 |
elif funcType == SOLLYA_BASE_FUNC_SUB:
|
|
172 |
return "-"
|
|
173 |
elif funcType == SOLLYA_BASE_FUNC_TAN:
|
|
174 |
return "tan"
|
|
175 |
elif funcType == SOLLYA_BASE_FUNC_TANH:
|
|
176 |
return "tanh"
|
|
177 |
elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
|
|
178 |
return "tripleDouble"
|
|
179 |
else:
|
|
180 |
return None
|
|
181 |
|
|
182 |
def pobyso_get_constant(rnArg, soConst):
|
|
183 |
set_rn_value(rnArg, soConst)
|
|
184 |
|
|
185 |
def pobyso_get_constant_as_rn(ctExp):
|
|
186 |
precision = pobyso_get_prec_of_constant(ctExp)
|
|
187 |
RRRR = RealField(precision)
|
|
188 |
rn = RRRR(0)
|
|
189 |
sollya_lib_get_constant(get_rn_value(rn), ctExp)
|
|
190 |
return(rn)
|
|
191 |
|
|
192 |
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
|
|
193 |
rn = realField(0)
|
|
194 |
sollya_lib_get_constant(get_rn_value(rn), ctExp)
|
|
195 |
return(rn)
|
|
196 |
def pobyso_get_free_variable_name():
|
|
197 |
return(sollya_lib_get_free_variable_name())
|
|
198 |
|
|
199 |
def pobyso_get_function_arity(expression):
|
|
200 |
arity = c_int(0)
|
|
201 |
sollya_lib_get_function_arity(byref(arity),expression)
|
|
202 |
return(int(arity.value))
|
|
203 |
|
|
204 |
def pobyso_get_head_function(expression):
|
|
205 |
functionType = c_int(0)
|
|
206 |
sollya_lib_get_head_function(byref(functionType), expression, None)
|
|
207 |
return(int(functionType.value))
|
|
208 |
|
|
209 |
def pobyso_get_list_elements(soObj):
|
|
210 |
# Type for array of pointers to sollya_obj_t
|
|
211 |
listAddress = POINTER(c_longlong)()
|
|
212 |
numElements = c_int(0)
|
|
213 |
isEndElliptic = c_int(0)
|
|
214 |
listAsList = []
|
|
215 |
result = sollya_lib_get_list_elements(byref(listAddress),\
|
|
216 |
byref(numElements),\
|
|
217 |
byref(isEndElliptic),\
|
|
218 |
soObj)
|
|
219 |
if result == 0 :
|
|
220 |
return None
|
|
221 |
for i in xrange(0, numElements.value, 1):
|
|
222 |
print "address ", i, " ->", listAddress[i]
|
|
223 |
listAsList.append(listAddress[i])
|
|
224 |
return(listAsList, numElements.value, isEndElliptic.value)
|
|
225 |
|
|
226 |
|
|
227 |
# Get the maximum precision used for the numbers in a
|
|
228 |
# Sollya expression.
|
|
229 |
# ToDo:
|
|
230 |
# - error management;
|
|
231 |
# - correctly deal with numerical type such as DOUBLEEXTENDED.
|
|
232 |
def pobyso_get_max_prec_of_exp(soExp):
|
|
233 |
maxPrecision = 0
|
|
234 |
operator = pobyso_get_head_function(soExp)
|
|
235 |
if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
|
|
236 |
(operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
|
|
237 |
(arity, subexpressions) = pobyso_get_subfunctions(soExp)
|
|
238 |
for i in xrange(arity):
|
|
239 |
maxPrecisionCandidate = \
|
|
240 |
pobyso_get_max_prec_of_exp(subexpressions[i])
|
|
241 |
if maxPrecisionCandidate > maxPrecision:
|
|
242 |
maxPrecision = maxPrecisionCandidate
|
|
243 |
return(maxPrecision)
|
|
244 |
elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
|
245 |
#print pobyso_get_prec_of_constant(soExp)
|
|
246 |
return(pobyso_get_prec_of_constant(soExp))
|
|
247 |
elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
|
248 |
return(0)
|
|
249 |
else:
|
|
250 |
print "pobyso_get_max_prec_of_exp: unexepected operator."
|
|
251 |
return(0)
|
|
252 |
|
|
253 |
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
|
|
254 |
"""
|
|
255 |
Get a Sage expression from a Sollya expression, currently only tested
|
|
256 |
with polynomials with floating-point coefficients.
|
|
257 |
Notice that, in the returned polynomial, the exponents are RealNumbers.
|
|
258 |
"""
|
|
259 |
#pobyso_autoprint(sollyaExp)
|
|
260 |
operator = pobyso_get_head_function(sollyaExp)
|
|
261 |
# Constants and the free variable are special cases.
|
|
262 |
# All other operator are dealt with in the same way.
|
|
263 |
if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
|
|
264 |
(operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
|
|
265 |
(arity, subexpressions) = pobyso_get_subfunctions(sollyaExp)
|
|
266 |
if arity == 1:
|
|
267 |
sageExp = eval(pobyso_function_type_as_string(operator) + \
|
|
268 |
"(" + pobyso_get_sage_exp_from_sollya_exp(subexpressions[0], realField)\
|
|
269 |
+ ")")
|
|
270 |
elif arity == 2:
|
|
271 |
if operator == SOLLYA_BASE_FUNC_POW:
|
|
272 |
operatorAsString = "**"
|
|
273 |
else:
|
|
274 |
operatorAsString = pobyso_function_type_as_string(operator)
|
|
275 |
sageExp = \
|
|
276 |
eval("pobyso_get_sage_exp_from_sollya_exp(subexpressions[0], realField)"\
|
|
277 |
+ " " + operatorAsString + " " + \
|
|
278 |
"pobyso_get_sage_exp_from_sollya_exp(subexpressions[1], realField)")
|
|
279 |
# We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
|
|
280 |
else:
|
|
281 |
sageExp = eval('None')
|
|
282 |
return(sageExp)
|
|
283 |
elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
|
284 |
#print "This is a constant"
|
|
285 |
return pobyso_get_constant_as_rn_with_rf(sollyaExp, realField)
|
|
286 |
elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
|
287 |
#print "This is free variable"
|
|
288 |
return(eval(sollya_lib_get_free_variable_name()))
|
|
289 |
else:
|
|
290 |
print "Unexpected"
|
|
291 |
return eval('None')
|
|
292 |
# End pobyso_get_sage_poly_from_sollya_poly
|
|
293 |
|
|
294 |
def pobyso_get_subfunctions(expression):
|
|
295 |
subf0 = c_int(0)
|
|
296 |
subf1 = c_int(0)
|
|
297 |
subf2 = c_int(0)
|
|
298 |
subf3 = c_int(0)
|
|
299 |
subf4 = c_int(0)
|
|
300 |
subf5 = c_int(0)
|
|
301 |
subf6 = c_int(0)
|
|
302 |
subf7 = c_int(0)
|
|
303 |
subf8 = c_int(0)
|
|
304 |
arity = c_int(0)
|
|
305 |
nullPtr = POINTER(c_int)()
|
|
306 |
sollya_lib_get_subfunctions(expression, byref(arity), \
|
|
307 |
byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
|
|
308 |
byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
|
|
309 |
# byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
|
|
310 |
# byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
|
|
311 |
# byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
|
|
312 |
# byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
|
|
313 |
# byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
|
|
314 |
subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
|
|
315 |
subs = []
|
|
316 |
if arity.value > pobyso_max_arity:
|
|
317 |
return(None,None)
|
|
318 |
for i in xrange(arity.value):
|
|
319 |
subs.append(int(subfunctions[i].value))
|
|
320 |
#print subs[i]
|
|
321 |
return(int(arity.value), subs)
|
|
322 |
|
|
323 |
def pobyso_get_prec():
|
|
324 |
retc = sollya_lib_get_prec(None)
|
|
325 |
a = c_int(0)
|
|
326 |
sollya_lib_get_constant_as_int(byref(a), retc)
|
|
327 |
return(int(a.value))
|
|
328 |
|
|
329 |
def pobyso_get_prec_of_constant(ctExp):
|
|
330 |
prec = c_int(0)
|
|
331 |
retc = sollya_lib_get_prec_of_constant(byref(prec), ctExp, None)
|
|
332 |
return(int(prec.value))
|
|
333 |
|
|
334 |
def pobyso_parse_string(string):
|
|
335 |
return(sollya_lib_parse_string(string))
|
|
336 |
|
|
337 |
def pobyso_univar_polynomial_print_reverse(polySa):
|
|
338 |
"""
|
|
339 |
Return the string representation of a univariate polynomial with
|
|
340 |
monomial ordered in the x^0..x^n order of the monomials.
|
|
341 |
Remember: Sage
|
|
342 |
"""
|
|
343 |
polynomialRing = polySa.base_ring()
|
|
344 |
# A very expensive solution:
|
|
345 |
# -create a fake multivariate polynomial field with only one variable,
|
|
346 |
# specifying a negative lexicographical order;
|
|
347 |
mpolynomialRing = PolynomialRing(polynomialRing.base(), \
|
|
348 |
polynomialRing.variable_name(), \
|
|
349 |
1, order='neglex')
|
|
350 |
# - convert the univariate argument polynomial into a multivariate
|
|
351 |
# version;
|
|
352 |
p = mpolynomialRing(polySa)
|
|
353 |
# - return the string representation of the converted form.
|
|
354 |
# There is no simple str() method defined for p's class.
|
|
355 |
return(p.__str__())
|
|
356 |
|
|
357 |
def pobyso_range(rnLowerBound, rnUpperBound):
|
|
358 |
lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound))
|
|
359 |
upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound))
|
|
360 |
rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
|
|
361 |
return(rangeSo)
|
|
362 |
|
|
363 |
def pobyso_remez_canonical(function, \
|
|
364 |
degree, \
|
|
365 |
lowerBound, \
|
|
366 |
upperBound, \
|
|
367 |
weightSo = pobyso_constant_1(),
|
|
368 |
quality = None):
|
|
369 |
if parent(function) == parent("string"):
|
|
370 |
functionSo = sollya_lib_parse_string(function)
|
|
371 |
# print "Is string!"
|
|
372 |
elif sollya_lib_obj_is_function(function):
|
|
373 |
functionSo = function
|
|
374 |
# print "Is Function!"
|
|
375 |
degreeSo = pobyso_constant_from_int(degree)
|
|
376 |
rangeSo = pobyso_range(lowerBound, upperBound)
|
|
377 |
return(sollya_lib_remez(functionSo, degreeSo, rangeSo, quality, None))
|
|
378 |
|
|
379 |
def pobyso_set_canonical_off():
|
|
380 |
sollya_lib_set_canonical(sollya_lib_off())
|
|
381 |
|
|
382 |
def pobyso_set_canonical_on():
|
|
383 |
sollya_lib_set_canonical(sollya_lib_on())
|
|
384 |
|
|
385 |
def pobyso_set_prec(p):
|
|
386 |
a = c_int(p)
|
|
387 |
precSo = c_void_p(sollya_lib_constant_from_int(a))
|
|
388 |
sollya_lib_set_prec(precSo)
|
|
389 |
|
|
390 |
def pobyso_taylor(function, degree, point):
|
|
391 |
return(sollya_lib_taylor(function, degree, point))
|
|
392 |
|
|
393 |
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
|
|
394 |
if errorType is None:
|
|
395 |
errorType = sollya_lib_absolute()
|
|
396 |
return(sollya_lib_taylorform(function, degree, point, errorType, None))
|
|
397 |
#
|
|
398 |
print "Superficial test of pobyso:"
|
|
399 |
print pobyso_get_prec()
|
|
400 |
pobyso_set_prec(165)
|
|
401 |
print pobyso_get_prec()
|
|
402 |
a=100
|
|
403 |
print type(a)
|
|
404 |
id(a)
|
|
405 |
print "Max arity: ", pobyso_max_arity
|
|
406 |
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
|
|
407 |
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
|