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