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