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