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