Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 59

Historique | Voir | Annoter | Télécharger (36,77 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 59 storres
from sage.symbolic.expression_conversions import PolynomialConverter
35 38 storres
"""
36 38 storres
Create the equivalent to an enum for the Sollya function types.
37 38 storres
"""
38 5 storres
(SOLLYA_BASE_FUNC_ABS,
39 5 storres
SOLLYA_BASE_FUNC_ACOS,
40 5 storres
    SOLLYA_BASE_FUNC_ACOSH,
41 5 storres
    SOLLYA_BASE_FUNC_ADD,
42 5 storres
    SOLLYA_BASE_FUNC_ASIN,
43 5 storres
    SOLLYA_BASE_FUNC_ASINH,
44 5 storres
    SOLLYA_BASE_FUNC_ATAN,
45 5 storres
    SOLLYA_BASE_FUNC_ATANH,
46 5 storres
    SOLLYA_BASE_FUNC_CEIL,
47 5 storres
    SOLLYA_BASE_FUNC_CONSTANT,
48 5 storres
    SOLLYA_BASE_FUNC_COS,
49 5 storres
    SOLLYA_BASE_FUNC_COSH,
50 5 storres
    SOLLYA_BASE_FUNC_DIV,
51 5 storres
    SOLLYA_BASE_FUNC_DOUBLE,
52 5 storres
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
53 5 storres
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
54 5 storres
    SOLLYA_BASE_FUNC_ERF,
55 5 storres
    SOLLYA_BASE_FUNC_ERFC,
56 5 storres
    SOLLYA_BASE_FUNC_EXP,
57 5 storres
    SOLLYA_BASE_FUNC_EXP_M1,
58 5 storres
    SOLLYA_BASE_FUNC_FLOOR,
59 5 storres
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
60 5 storres
    SOLLYA_BASE_FUNC_HALFPRECISION,
61 5 storres
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
62 5 storres
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
63 5 storres
    SOLLYA_BASE_FUNC_LOG,
64 5 storres
    SOLLYA_BASE_FUNC_LOG_10,
65 5 storres
    SOLLYA_BASE_FUNC_LOG_1P,
66 5 storres
    SOLLYA_BASE_FUNC_LOG_2,
67 5 storres
    SOLLYA_BASE_FUNC_MUL,
68 5 storres
    SOLLYA_BASE_FUNC_NEARESTINT,
69 5 storres
    SOLLYA_BASE_FUNC_NEG,
70 5 storres
    SOLLYA_BASE_FUNC_PI,
71 5 storres
    SOLLYA_BASE_FUNC_POW,
72 5 storres
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
73 5 storres
    SOLLYA_BASE_FUNC_QUAD,
74 5 storres
    SOLLYA_BASE_FUNC_SIN,
75 5 storres
    SOLLYA_BASE_FUNC_SINGLE,
76 5 storres
    SOLLYA_BASE_FUNC_SINH,
77 5 storres
    SOLLYA_BASE_FUNC_SQRT,
78 5 storres
    SOLLYA_BASE_FUNC_SUB,
79 5 storres
    SOLLYA_BASE_FUNC_TAN,
80 5 storres
    SOLLYA_BASE_FUNC_TANH,
81 5 storres
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
82 56 storres
print "\nSuperficial pobyso check..."
83 5 storres
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
84 5 storres
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
85 5 storres
86 5 storres
pobyso_max_arity = 9
87 5 storres
88 5 storres
def pobyso_autoprint(arg):
89 5 storres
    sollya_lib_autoprint(arg,None)
90 5 storres
91 38 storres
def pobyso_autoprint_so_so(arg):
92 38 storres
    sollya_lib_autoprint(arg,None)
93 54 storres
94 54 storres
def pobyso_absolute_so_so():
95 54 storres
    return(sollya_lib_absolute(None))
96 38 storres
97 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
98 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
99 54 storres
100 5 storres
def pobyso_cmp(rnArg, soCte):
101 54 storres
    """
102 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
103 54 storres

104 54 storres
    Get the value of the Sollya constant into a RealNumber and compare
105 54 storres
    using MPFR. Could be optimized by working directly with a mpfr_t
106 54 storres
    for the intermediate number.
107 54 storres
    """
108 5 storres
    precisionOfCte = c_int(0)
109 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
110 5 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, soCte)
111 5 storres
    #print "Precision of constant: ", precisionOfCte
112 5 storres
    RRRR = RealField(precisionOfCte.value)
113 5 storres
    rnLocal = RRRR(0)
114 5 storres
    sollya_lib_get_constant(get_rn_value(rnLocal), soCte)
115 5 storres
    #print "rnDummy: ", rnDummy
116 5 storres
    # Compare the local Sage RealNumber with rnArg.
117 5 storres
    return(cmp_rn_value(rnArg, rnLocal))
118 5 storres
119 55 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
120 55 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
121 55 storres
122 55 storres
123 55 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
124 55 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
125 55 storres
    return(resultSo)
126 55 storres
127 55 storres
128 55 storres
129 54 storres
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
130 54 storres
                                                     upperBoundSa):
131 54 storres
    """
132 54 storres
    TODO: set the variable name in Sollya.
133 54 storres
    """
134 54 storres
    funcSo = pobyso_parse_string(funcSa._assume_str())
135 54 storres
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
136 54 storres
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
137 54 storres
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
138 54 storres
    # Get the top bound and compute the binade top limit.
139 54 storres
    fMaxUpperBoundSa = fMaxSa.upper()
140 54 storres
    binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
141 54 storres
    # Put up together the function to use to compute the lower bound.
142 54 storres
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
143 54 storres
                                    '-(' + f._assume_str() + ')')
144 54 storres
    pobyso_autoprint(funcAuxSo)
145 54 storres
    # Clear the Sollay range before a new call to infnorm and issue the call.
146 54 storres
    sollya_lib_clear_obj(infnormSo)
147 54 storres
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
148 54 storres
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
149 54 storres
    sollya_lib_clear_obj(infnormSo)
150 54 storres
    fMinLowerBoundSa = topBinadeLimit - fMinSa.lower()
151 54 storres
    # Compute the maximum of the precisions of the different bounds.
152 54 storres
    maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
153 54 storres
                     fMaxUpperBoundSa.parent().precision()])
154 54 storres
    # Create a RealIntervalField and create an interval with the "good" bounds.
155 54 storres
    RRRI = RealIntervalField(maxPrecSa)
156 54 storres
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
157 54 storres
    # Free the uneeded Sollya objects
158 54 storres
    sollya_lib_clear_obj(funcSo)
159 54 storres
    sollya_lib_clear_obj(funcAuxSo)
160 54 storres
    sollya_lib_clear_obj(rangeSo)
161 54 storres
    return(imageIntervalSa)
162 54 storres
    # End pobyso_compute_function_abs_val_bounds_sa_sa
163 54 storres
164 5 storres
def pobyso_constant(rnArg):
165 38 storres
    """ Legacy function. See pobyso_constant_sa_so. """
166 38 storres
    return(pobyso_constant_sa_so(rnArg))
167 5 storres
168 38 storres
def pobyso_constant_sa_so(rnArg):
169 52 storres
    """
170 52 storres
    Create a Sollya constant from a RealNumber.
171 52 storres
    """
172 38 storres
    return(sollya_lib_constant(get_rn_value(rnArg)))
173 38 storres
174 55 storres
def pobyso_constant_0_sa_so():
175 55 storres
    return(pobyso_constant_from_int_sa_so(0))
176 55 storres
177 5 storres
def pobyso_constant_1():
178 38 storres
    """ Legacy function. See pobyso_constant_so_so. """
179 52 storres
    return(pobyso_constant_1_sa_so())
180 5 storres
181 52 storres
def pobyso_constant_1_sa_so():
182 38 storres
    return(pobyso_constant_from_int_sa_so(1))
183 38 storres
184 5 storres
def pobyso_constant_from_int(anInt):
185 38 storres
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
186 38 storres
    return(pobyso_constant_from_int_sa_so(anInt))
187 38 storres
188 38 storres
def pobyso_constant_from_int_sa_so(anInt):
189 5 storres
    return(sollya_lib_constant_from_int(int(anInt)))
190 5 storres
191 5 storres
def pobyso_function_type_as_string(funcType):
192 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
193 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
194 38 storres
195 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
196 38 storres
    """
197 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
198 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
199 38 storres
    """
200 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
201 5 storres
        return "abs"
202 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
203 5 storres
        return "arccos"
204 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
205 5 storres
        return "arccosh"
206 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
207 5 storres
        return "+"
208 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
209 5 storres
        return "arcsin"
210 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
211 5 storres
        return "arcsinh"
212 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
213 5 storres
        return "arctan"
214 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
215 5 storres
        return "arctanh"
216 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
217 5 storres
        return "ceil"
218 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
219 5 storres
        return "cte"
220 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
221 5 storres
        return "cos"
222 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
223 5 storres
        return "cosh"
224 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
225 5 storres
        return "/"
226 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
227 5 storres
        return "double"
228 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
229 5 storres
        return "doubleDouble"
230 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
231 5 storres
        return "doubleDxtended"
232 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
233 5 storres
        return "erf"
234 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
235 5 storres
        return "erfc"
236 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
237 5 storres
        return "exp"
238 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
239 5 storres
        return "expm1"
240 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
241 5 storres
        return "floor"
242 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
243 5 storres
        return "freeVariable"
244 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
245 5 storres
        return "halfPrecision"
246 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
247 5 storres
        return "libraryConstant"
248 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
249 5 storres
        return "libraryFunction"
250 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
251 5 storres
        return "log"
252 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
253 5 storres
        return "log10"
254 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
255 5 storres
        return "log1p"
256 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
257 5 storres
        return "log2"
258 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
259 5 storres
        return "*"
260 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
261 5 storres
        return "round"
262 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
263 5 storres
        return "__neg__"
264 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
265 5 storres
        return "pi"
266 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
267 5 storres
        return "^"
268 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
269 5 storres
        return "procedureFunction"
270 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
271 5 storres
        return "quad"
272 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
273 5 storres
        return "sin"
274 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
275 5 storres
        return "single"
276 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
277 5 storres
        return "sinh"
278 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
279 5 storres
        return "sqrt"
280 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
281 5 storres
        return "-"
282 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
283 5 storres
        return "tan"
284 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
285 5 storres
        return "tanh"
286 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
287 5 storres
        return "tripleDouble"
288 5 storres
    else:
289 5 storres
        return None
290 5 storres
291 5 storres
def pobyso_get_constant(rnArg, soConst):
292 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
293 53 storres
    return(pobyso_get_constant_so_sa(rnArg, soConst))
294 38 storres
295 38 storres
def pobyso_get_constant_so_sa(rnArg, soConst):
296 52 storres
    """
297 52 storres
    Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
298 52 storres
    rnArg must already exist and belong to some RealField.
299 52 storres
    We assume that soConst points to a Sollya constant.
300 52 storres
    """
301 53 storres
    return(sollya_lib_get_constant(get_rn_value(rnArg), soConst))
302 5 storres
303 57 storres
def pobyso_get_constant_as_rn(ctExpSo):
304 38 storres
    """ Legacy function. See pobyso_get_constant_as_rn_so_sa. """
305 57 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
306 38 storres
307 56 storres
def pobyso_get_constant_as_rn_so_sa(constExpSo):
308 57 storres
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
309 56 storres
    RRRR = RealField(precisionSa)
310 56 storres
    rnSa = RRRR(0)
311 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), constExpSo)
312 56 storres
    return(rnSa)
313 38 storres
314 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
315 53 storres
    """ Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa."""
316 38 storres
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
317 5 storres
318 56 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None):
319 56 storres
    if realFieldSa is None:
320 56 storres
        sollyaPrecSa = pobyso_get_prec_so_sa()
321 56 storres
        realFieldSa = RealField(sollyaPrecSa)
322 56 storres
    rnSa = realFieldSa(0)
323 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo)
324 56 storres
    return(rnSa)
325 38 storres
326 5 storres
def pobyso_get_free_variable_name():
327 38 storres
    """ Legacy function. See pobyso_get_free_variable_name_so_sa."""
328 38 storres
    return(pobyso_get_free_variable_name_so_sa())
329 38 storres
330 38 storres
def pobyso_get_free_variable_name_so_sa():
331 5 storres
    return(sollya_lib_get_free_variable_name())
332 5 storres
333 38 storres
def pobyso_get_function_arity(expressionSo):
334 38 storres
    """ Legacy function. See pobyso_get_function_arity_so_sa."""
335 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
336 38 storres
337 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
338 5 storres
    arity = c_int(0)
339 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
340 5 storres
    return(int(arity.value))
341 5 storres
342 38 storres
def pobyso_get_head_function(expressionSo):
343 38 storres
    """ Legacy function. See pobyso_get_head_function_so_sa. """
344 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
345 38 storres
346 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
347 5 storres
    functionType = c_int(0)
348 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
349 5 storres
    return(int(functionType.value))
350 5 storres
351 56 storres
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
352 53 storres
    """
353 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
354 56 storres
    If no reaInterval lField is passed as argument, the interval bounds are not
355 56 storres
    rounded: they are elements of RealIntervalField of the "right" precision
356 56 storres
    to hold all the digits.
357 53 storres
    """
358 53 storres
    prec = c_int(0)
359 56 storres
    if realIntervalFieldSa is None:
360 56 storres
        retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
361 56 storres
        if retval == 0:
362 56 storres
            return(None)
363 56 storres
        realIntervalFieldSa = RealIntervalField(prec.value)
364 56 storres
    intervalSa = realIntervalFieldSa(0,0)
365 53 storres
    retval = \
366 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
367 53 storres
                                           soRange)
368 53 storres
    if retval == 0:
369 53 storres
        return(None)
370 53 storres
    return(intervalSa)
371 56 storres
# End pobyso_get_interval_from_range_so_sa
372 56 storres
373 5 storres
def pobyso_get_list_elements(soObj):
374 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
375 38 storres
    return(pobyso_get_list_elements_so_so(soObj))
376 38 storres
377 38 storres
def pobyso_get_list_elements_so_so(soObj):
378 51 storres
    """
379 51 storres
    Get the list elements as a Sage/Python array of Sollya objects.
380 51 storres
    The other data returned are also Sage/Python objects.
381 51 storres
    """
382 5 storres
    listAddress = POINTER(c_longlong)()
383 5 storres
    numElements = c_int(0)
384 5 storres
    isEndElliptic = c_int(0)
385 5 storres
    listAsList = []
386 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
387 54 storres
                                          byref(numElements),\
388 54 storres
                                          byref(isEndElliptic),\
389 54 storres
                                          soObj)
390 5 storres
    if result == 0 :
391 5 storres
        return None
392 5 storres
    for i in xrange(0, numElements.value, 1):
393 5 storres
        listAsList.append(listAddress[i])
394 5 storres
    return(listAsList, numElements.value, isEndElliptic.value)
395 5 storres
396 38 storres
def pobyso_get_max_prec_of_exp(soExp):
397 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
398 38 storres
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
399 5 storres
400 38 storres
def pobyso_get_max_prec_of_exp_so_sa(soExp):
401 38 storres
    """
402 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
403 52 storres

404 52 storres
    Arguments:
405 52 storres
    soExp -- a Sollya expression pointer
406 52 storres
    Return value:
407 52 storres
    A Python integer
408 38 storres
    TODO:
409 38 storres
    - error management;
410 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
411 38 storres
    """
412 5 storres
    maxPrecision = 0
413 52 storres
    minConstPrec = 0
414 52 storres
    currentConstPrec = 0
415 38 storres
    operator = pobyso_get_head_function_so_sa(soExp)
416 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
417 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
418 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp)
419 5 storres
        for i in xrange(arity):
420 5 storres
            maxPrecisionCandidate = \
421 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
422 5 storres
            if maxPrecisionCandidate > maxPrecision:
423 5 storres
                maxPrecision = maxPrecisionCandidate
424 5 storres
        return(maxPrecision)
425 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
426 52 storres
        minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
427 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
428 52 storres
        #print minConstPrec, " - ", currentConstPrec
429 52 storres
        return(pobyso_get_min_prec_of_constant_so_sa(soExp))
430 52 storres
431 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
432 5 storres
        return(0)
433 5 storres
    else:
434 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
435 5 storres
        return(0)
436 5 storres
437 52 storres
def pobyso_get_min_prec_of_constant_so_sa(soConstExp):
438 52 storres
    """
439 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
440 52 storres
    constant.
441 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
442 52 storres
    We assume that soCteExp is a point
443 52 storres
    """
444 52 storres
    constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp)
445 52 storres
    return(min_mpfr_size(get_rn_value(constExpAsRn)))
446 52 storres
447 5 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
448 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
449 38 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
450 38 storres
451 38 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR):
452 5 storres
    """
453 38 storres
    Get a Sage expression from a Sollya expression.
454 38 storres
    Currently only tested with polynomials with floating-point coefficients.
455 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
456 5 storres
    """
457 5 storres
    #pobyso_autoprint(sollyaExp)
458 55 storres
    operator = pobyso_get_head_function_so_sa(sollyaExp)
459 5 storres
    # Constants and the free variable are special cases.
460 5 storres
    # All other operator are dealt with in the same way.
461 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
462 5 storres
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
463 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp)
464 5 storres
        if arity == 1:
465 38 storres
            sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
466 52 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \
467 52 storres
            realField) + ")")
468 5 storres
        elif arity == 2:
469 5 storres
            if operator == SOLLYA_BASE_FUNC_POW:
470 5 storres
                operatorAsString = "**"
471 5 storres
            else:
472 52 storres
                operatorAsString = \
473 52 storres
                    pobyso_function_type_as_string_so_sa(operator)
474 5 storres
            sageExp = \
475 38 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\
476 5 storres
              + " " + operatorAsString + " " + \
477 38 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
478 5 storres
        # We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
479 5 storres
        else:
480 5 storres
            sageExp = eval('None')
481 5 storres
        return(sageExp)
482 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
483 5 storres
        #print "This is a constant"
484 38 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
485 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
486 5 storres
        #print "This is free variable"
487 5 storres
        return(eval(sollya_lib_get_free_variable_name()))
488 5 storres
    else:
489 5 storres
        print "Unexpected"
490 5 storres
        return eval('None')
491 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
492 57 storres
def pobyso_get_sage_poly_from_sollya_poly(polySo, realFieldSa=None):
493 57 storres
    """
494 57 storres
    Convert a Sollya polynomial into a Sage polynomial.
495 57 storres
    If no realField is given, a RealField corresponding to the maximum precision
496 57 storres
    of the coefficients is internally computed.
497 57 storres
    It is not returned but can be easily retrieved from the polynomial itself.
498 57 storres
    Main steps:
499 57 storres
    - (optional) compute the RealField of the coefficients;
500 57 storres
    - convert the Sollya expression into a Sage expression;
501 57 storres
    - convert the Sage expression into a Sage polynomial
502 59 storres
    TODO: the canonical thing for the polynomial.
503 57 storres
    """
504 57 storres
    if realFieldSa is None:
505 57 storres
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
506 57 storres
        realFieldSa = RealField(expressionPrecSa)
507 5 storres
508 57 storres
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \
509 57 storres
                                                             realFieldSa)
510 59 storres
    print "ssssss: ", expressionSa
511 57 storres
    polyVariableSa = expressionSa.variables()[0]
512 59 storres
    print type(str(polyVariableSa))
513 59 storres
    polyRingSa = realFieldSa[str(polyVariableSa)]
514 59 storres
    print polyRingSa
515 59 storres
    polynomialSa = polynomial(polyVariableSa, ring=polyRingSa)
516 59 storres
    print polyRingSa.base()
517 59 storres
    print "tttttt: ", polynomialSa
518 57 storres
    return(polynomialSa)
519 57 storres
# End pobyso_get_sage_poly_from_sollya_poly
520 57 storres
521 38 storres
def pobyso_get_subfunctions(expressionSo):
522 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
523 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
524 38 storres
525 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
526 38 storres
    """
527 38 storres
    Get the subfunctions of an expression.
528 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
529 55 storres
    S.T.: Could not figure out another way than that ugly list of declarations
530 55 storres
    to recover the addresses of the subfunctions.
531 38 storres
    """
532 5 storres
    subf0 = c_int(0)
533 5 storres
    subf1 = c_int(0)
534 5 storres
    subf2 = c_int(0)
535 5 storres
    subf3 = c_int(0)
536 5 storres
    subf4 = c_int(0)
537 5 storres
    subf5 = c_int(0)
538 5 storres
    subf6 = c_int(0)
539 5 storres
    subf7 = c_int(0)
540 5 storres
    subf8 = c_int(0)
541 5 storres
    arity = c_int(0)
542 5 storres
    nullPtr = POINTER(c_int)()
543 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
544 5 storres
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
545 5 storres
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
546 5 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
547 5 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
548 5 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
549 5 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
550 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
551 5 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
552 5 storres
    subs = []
553 5 storres
    if arity.value > pobyso_max_arity:
554 38 storres
        return(0,[])
555 5 storres
    for i in xrange(arity.value):
556 5 storres
        subs.append(int(subfunctions[i].value))
557 5 storres
        #print subs[i]
558 5 storres
    return(int(arity.value), subs)
559 5 storres
560 5 storres
def pobyso_get_prec():
561 38 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
562 38 storres
    return(pobyso_get_prec_so_sa())
563 38 storres
564 38 storres
def pobyso_get_prec_so_sa():
565 38 storres
    """
566 38 storres
    Get the current default precision in Sollya.
567 38 storres
    The return value is Sage/Python int.
568 38 storres
    """
569 56 storres
    precSo = sollya_lib_get_prec(None)
570 56 storres
    precSa = c_int(0)
571 56 storres
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
572 56 storres
    sollya_lib_clear_obj(precSo)
573 56 storres
    return(int(precSa.value))
574 5 storres
575 38 storres
def pobyso_get_prec_of_constant(ctExpSo):
576 38 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
577 38 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
578 38 storres
579 56 storres
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
580 56 storres
    prec = c_int(0)
581 56 storres
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
582 56 storres
    if retc == 0:
583 56 storres
        return(None)
584 56 storres
    return(int(prec.value))
585 56 storres
586 53 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
587 5 storres
    prec = c_int(0)
588 53 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
589 56 storres
    if retc == 0:
590 56 storres
        return(None)
591 5 storres
    return(int(prec.value))
592 5 storres
593 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
594 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
595 54 storres
    return(None)
596 53 storres
597 37 storres
def pobyso_lib_init():
598 37 storres
    sollya_lib_init(None)
599 37 storres
600 37 storres
def pobyso_name_free_variable(freeVariableName):
601 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
602 38 storres
    pobyso_name_free_variable_sa_so(freeVariableName)
603 38 storres
604 38 storres
def pobyso_name_free_variable_sa_so(freeVariableName):
605 37 storres
    sollya_lib_name_free_variable(freeVariableName)
606 37 storres
607 5 storres
def pobyso_parse_string(string):
608 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
609 38 storres
    return(pobyso_parse_string_sa_so(string))
610 38 storres
611 38 storres
def pobyso_parse_string_sa_so(string):
612 5 storres
    return(sollya_lib_parse_string(string))
613 5 storres
614 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
615 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
616 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
617 38 storres
618 56 storres
def pobyso_range_sa_so(rnLowerBoundSa, rnUpperBoundSa):
619 56 storres
    """
620 56 storres
    Return a Sollya range from to 2 RealField elements.
621 56 storres
    The Sollya range element has a sufficient precision to hold all
622 56 storres
    the digits of the bounds.
623 56 storres
    """
624 56 storres
    # TODO: check the bounds.
625 56 storres
    lbPrec = rnLowerBoundSa.parent().precision()
626 56 storres
    ubPrec = rnLowerBoundSa.parent().precision()
627 56 storres
    currentSollyaPrecSa = pobyso_get_prec_so_sa()
628 56 storres
    maxPrecSa = max(lbPrec, ubPrec, currentSollyaPrecSa)
629 56 storres
    # Change the current Sollya precision only if necessary.
630 56 storres
    if maxPrecSa > currentSollyaPrecSa:
631 56 storres
        currentPrecSo = sollya_lib_get_prec(None)
632 56 storres
        newPrecSo = solly_lib_constant_from_uint64(maxPrecSa)
633 56 storres
        sollya_lib_set_prec(newPrecSo)
634 56 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
635 56 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
636 5 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
637 56 storres
    currentPrecSo = sollya_lib_get_prec(None)
638 56 storres
    if maxPrecSa > currentSollyaPrecSa:
639 56 storres
        sollya_lib_set_prec(currentPrecSo)
640 56 storres
        sollya_lib_clear_obj(currentPrecSo)
641 56 storres
        sollya_lib_clear_obj(newPrecSo)
642 56 storres
    sollya_lib_clear_obj(lowerBoundSo)
643 56 storres
    sollya_lib_clear_obj(upperBoundSo)
644 5 storres
    return(rangeSo)
645 5 storres
646 56 storres
def pobyso_range_so_sa(rangeSo, realIntervalField = None):
647 56 storres
    if realIntervalField is None:
648 56 storres
        precSa = pobyso_get_prec_of_range_so_sa(rangeSo)
649 56 storres
        realIntervalField = RealIntervalField(precSa)
650 56 storres
    intervalSa = \
651 56 storres
        pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalField)
652 56 storres
    return(intervalSa)
653 56 storres
654 52 storres
def pobyso_remez_canonical_sa_sa(func, \
655 52 storres
                                 degree, \
656 52 storres
                                 lowerBound, \
657 52 storres
                                 upperBound, \
658 52 storres
                                 weight = None, \
659 52 storres
                                 quality = None):
660 52 storres
    """
661 52 storres
    All arguments are Sage/Python.
662 52 storres
    The functions (func and weight) must be passed as expressions or strings.
663 52 storres
    Otherwise the function fails.
664 52 storres
    The return value is a pointer is a Sage polynomial.
665 52 storres
    """
666 52 storres
    var('zorglub')    # Dummy variable name for type check only.
667 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
668 52 storres
                                 degree, \
669 52 storres
                                 lowerBound, \
670 52 storres
                                 upperBound, \
671 52 storres
                                 weight = None, \
672 52 storres
                                 quality = None)
673 52 storres
    if parent(func) == parent("string"):
674 52 storres
        functionSa = eval(func)
675 52 storres
    # Expression test.
676 52 storres
    elif type(func) == type(zorglub):
677 52 storres
        functionSa = func
678 52 storres
    maxPrecision = 0
679 52 storres
    if polySo is None:
680 52 storres
        return(None)
681 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
682 52 storres
    RRRR = RealField(maxPrecision)
683 52 storres
    polynomialRing = RRRR[functionSa.variables()[0]]
684 52 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR)
685 52 storres
    polySa = polynomial(expSa, polynomialRing)
686 52 storres
    return(polySa)
687 52 storres
688 38 storres
def pobyso_remez_canonical(func, \
689 5 storres
                           degree, \
690 5 storres
                           lowerBound, \
691 5 storres
                           upperBound, \
692 38 storres
                           weight = "1", \
693 5 storres
                           quality = None):
694 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
695 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
696 51 storres
                                        degree, \
697 51 storres
                                        lowerBound, \
698 51 storres
                                        upperBound, \
699 51 storres
                                        weight, \
700 51 storres
                                        quality))
701 38 storres
def pobyso_remez_canonical_sa_so(func, \
702 38 storres
                                 degree, \
703 38 storres
                                 lowerBound, \
704 38 storres
                                 upperBound, \
705 52 storres
                                 weight = None, \
706 38 storres
                                 quality = None):
707 38 storres
    """
708 38 storres
    All arguments are Sage/Python.
709 51 storres
    The functions (func and weight) must be passed as expressions or strings.
710 51 storres
    Otherwise the function fails.
711 38 storres
    The return value is a pointer to a Sollya function.
712 38 storres
    """
713 52 storres
    var('zorglub')    # Dummy variable name for type check only.
714 52 storres
    currentVariableName = None
715 52 storres
    # The func argument can be of different types (string,
716 52 storres
    # symbolic expression...)
717 38 storres
    if parent(func) == parent("string"):
718 38 storres
        functionSo = sollya_lib_parse_string(func)
719 51 storres
    # Expression test.
720 52 storres
    elif type(func) == type(zorglub):
721 52 storres
        # Until we are able to translate Sage expressions into Sollya
722 52 storres
        # expressions : parse the string version.
723 52 storres
        currentVariableName = func.variables()[0]
724 52 storres
        sollya_lib_name_free_variable(str(currentVariableName))
725 52 storres
        functionSo = sollya_lib_parse_string(func._assume_str())
726 38 storres
    else:
727 38 storres
        return(None)
728 52 storres
    if weight is None:
729 52 storres
        weightSo = pobyso_constant_1_sa_so()
730 52 storres
    elif parent(weight) == parent("string"):
731 51 storres
        weightSo = sollya_lib_parse_string(func)
732 51 storres
    elif type(weight) == type(zorglub):
733 51 storres
        functionSo = sollya_lib_parse_string_sa_so(weight._assume_str())
734 51 storres
    else:
735 51 storres
        return(None)
736 5 storres
    degreeSo = pobyso_constant_from_int(degree)
737 38 storres
    rangeSo = pobyso_range_sa_so(lowerBound, upperBound)
738 38 storres
    if not quality is None:
739 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
740 52 storres
    else:
741 52 storres
        qualitySo = None
742 52 storres
    return(sollya_lib_remez(functionSo, \
743 52 storres
                            degreeSo, \
744 52 storres
                            rangeSo, \
745 52 storres
                            weightSo, \
746 52 storres
                            qualitySo, \
747 52 storres
                            None))
748 5 storres
749 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
750 38 storres
                                 degreeSo, \
751 38 storres
                                 rangeSo, \
752 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
753 38 storres
                                 qualitySo = None):
754 38 storres
    """
755 38 storres
    All arguments are pointers to Sollya objects.
756 38 storres
    The return value is a pointer to a Sollya function.
757 38 storres
    """
758 38 storres
    if not sollya_lib_obj_is_function(funcSo):
759 38 storres
        return(None)
760 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
761 38 storres
762 5 storres
def pobyso_set_canonical_off():
763 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
764 5 storres
765 5 storres
def pobyso_set_canonical_on():
766 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
767 5 storres
768 5 storres
def pobyso_set_prec(p):
769 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
770 38 storres
    return( pobyso_set_prec_sa_so(p))
771 38 storres
772 38 storres
def pobyso_set_prec_sa_so(p):
773 5 storres
    a = c_int(p)
774 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
775 5 storres
    sollya_lib_set_prec(precSo)
776 5 storres
777 54 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo, accuracySo):
778 54 storres
    return(sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
779 54 storres
                              accuracySo))
780 54 storres
781 58 storres
def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, rangeSo, \
782 58 storres
                                                errorTypeSo, \
783 58 storres
                                                sollyaPrecSo=None):
784 58 storres
    """
785 58 storres
    Compute the Taylor expansion with the variable change
786 58 storres
    x -> (x-intervalCenter) included.
787 58 storres
    """
788 58 storres
    # No global change of the working precision.
789 58 storres
    if not sollyaPrecSo is None:
790 58 storres
        initialPrecSo = sollya_lib_get_prec(None)
791 58 storres
        sollya_lib_set_prec(sollyaPrecSo)
792 58 storres
    #
793 58 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
794 58 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
795 58 storres
                                         intervalCenterSo, \
796 58 storres
                                         rangeSo, errorTypeSo, None)
797 58 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
798 58 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
799 58 storres
    polySo = taylorFormListSo[0]
800 58 storres
    errorRangeSo = taylorFormListSo[2]
801 58 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
802 58 storres
    changeVarExpSo = sollya_lib_build_function_sub(\
803 58 storres
                       sollya_lib_build_function_free_variable(),\
804 58 storres
                       sollya_lib_copy_obj(intervalCenterSo))
805 58 storres
    polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo)
806 58 storres
    # If changed, reset the Sollya working precision.
807 58 storres
    if not sollyaPrecSo is None:
808 58 storres
        sollya_lib_set_prec(initialPrecSo)
809 58 storres
        sollya_lib_clear_obj(initailPrecSo)
810 58 storres
    return([polyVarChangedSo, intervalCenterSo, maxErrorSo])
811 58 storres
# end pobyso_taylor_expansion_with_change_var_so_so
812 58 storres
813 56 storres
def pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, rangeSo, \
814 56 storres
                                                errorTypeSo, \
815 56 storres
                                                sollyaPrecSo=None):
816 58 storres
    """
817 58 storres
    Compute the Taylor expansion without the variable change
818 58 storres
    x -> x-intervalCenter.
819 58 storres
    """
820 56 storres
    # No global change of the working precision.
821 56 storres
    if not sollyaPrecSo is None:
822 56 storres
        initialPrecSo = sollya_lib_get_prec(None)
823 56 storres
        sollya_lib_set_prec(sollyaPrecSo)
824 56 storres
    #
825 56 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
826 56 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
827 56 storres
                                         intervalCenterSo, \
828 56 storres
                                         rangeSo, errorTypeSo, None)
829 56 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
830 56 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
831 56 storres
    polySo = taylorFormListSo[0]
832 56 storres
    errorRangeSo = taylorFormListSo[2]
833 56 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
834 56 storres
    # If changed, reset the Sollya working precision.
835 56 storres
    if not sollyaPrecSo is None:
836 56 storres
        sollya_lib_set_prec(initialPrecSo)
837 56 storres
        sollya_lib_clear_obj(initailPrecSo)
838 56 storres
    return([polySo, intervalCenterSo, maxErrorSo])
839 56 storres
# end pobyso_taylor_expansion_no_change_var_so_so
840 56 storres
841 5 storres
def pobyso_taylor(function, degree, point):
842 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
843 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
844 38 storres
845 56 storres
def pobyso_taylor_so_so(functionSo, degreeSo, pointSo):
846 56 storres
    return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
847 5 storres
848 5 storres
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
849 38 storres
    """ Legacy function. See ;"""
850 38 storres
851 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
852 38 storres
                            degree, \
853 43 storres
                            point, \
854 43 storres
                            precision, \
855 54 storres
                            interval=None, \
856 38 storres
                            errorType=None):
857 37 storres
    """
858 38 storres
    Compute the Taylor form of 'degree' for 'functionSa' at 'point'
859 37 storres
    for 'interval' with 'errorType'.
860 37 storres
    point: must be a Real or a Real interval.
861 37 storres
    return the Taylor form as an array
862 38 storres
    TODO: take care of the interval and of point when it is an interval;
863 38 storres
          when errorType is not None;
864 38 storres
          take care of the other elements of the Taylor form (coefficients errors and
865 38 storres
          delta.
866 37 storres
    """
867 37 storres
    # Absolute as the default error.
868 5 storres
    if errorType is None:
869 37 storres
        errorTypeSo = sollya_lib_absolute()
870 37 storres
    else:
871 37 storres
        #TODO: deal with the other case.
872 37 storres
        pass
873 38 storres
    varSa = functionSa.variables()[0]
874 37 storres
    pointBaseRingString = str(point.base_ring())
875 37 storres
    if not re.search('Real', pointBaseRingString):
876 37 storres
        return None
877 37 storres
    # Call Sollya but first "sollyafy" the arguments.
878 38 storres
    pobyso_name_free_variable_sa_so(str(varSa))
879 38 storres
    #pobyso_set_prec_sa_so(300)
880 37 storres
    # Sollyafy the function.
881 38 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
882 37 storres
    if sollya_lib_obj_is_error(functionSo):
883 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
884 37 storres
        return None
885 37 storres
    # Sollyafy the degree
886 37 storres
    degreeSo = sollya_lib_constant_from_int(int(degree))
887 37 storres
    # Sollyafy the point
888 37 storres
    if not re.search('Interval', pointBaseRingString):
889 38 storres
        pointSo  = pobyso_constant_sa_so(point)
890 37 storres
    else:
891 37 storres
        # TODO: deal with the interval case.
892 37 storres
        pass
893 37 storres
    # Call Sollya
894 37 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
895 37 storres
                                         None)
896 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
897 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
898 37 storres
    polySo = tfsAsList[0]
899 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
900 37 storres
    polyRealField = RealField(maxPrecision)
901 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
902 37 storres
    sollya_lib_close()
903 37 storres
    polynomialRing = polyRealField[str(varSa)]
904 37 storres
    polySa = polynomial(expSa, polynomialRing)
905 37 storres
    taylorFormSa = [polySa]
906 51 storres
    return(taylorFormSa)
907 51 storres
# End pobyso_taylor_form_sa_sa
908 54 storres
909 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
910 54 storres
                            errorTypeSo=None):
911 54 storres
    createdErrorType = False
912 51 storres
    if errorTypeSo is None:
913 51 storres
        errorTypeSo = sollya_lib_absolute()
914 54 storres
        createdErrorType = True
915 51 storres
    else:
916 51 storres
        #TODO: deal with the other case.
917 51 storres
        pass
918 51 storres
    if intervalSo is None:
919 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
920 54 storres
                                         errorTypeSo, None)
921 51 storres
    else:
922 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
923 54 storres
                                         intervalSo, errorTypeSo, None)
924 54 storres
    if createdErrorType:
925 54 storres
        sollya_lib_clear_obj(errorTypeSo)
926 51 storres
    return(resultSo)
927 51 storres
928 37 storres
929 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
930 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
931 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
932 38 storres
933 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
934 37 storres
    """
935 37 storres
    Return the string representation of a univariate polynomial with
936 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
937 37 storres
    Remember: Sage
938 37 storres
    """
939 37 storres
    polynomialRing = polySa.base_ring()
940 37 storres
    # A very expensive solution:
941 37 storres
    # -create a fake multivariate polynomial field with only one variable,
942 37 storres
    #   specifying a negative lexicographical order;
943 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
944 37 storres
                                     polynomialRing.variable_name(), \
945 37 storres
                                     1, order='neglex')
946 37 storres
    # - convert the univariate argument polynomial into a multivariate
947 37 storres
    #   version;
948 37 storres
    p = mpolynomialRing(polySa)
949 37 storres
    # - return the string representation of the converted form.
950 37 storres
    # There is no simple str() method defined for p's class.
951 37 storres
    return(p.__str__())
952 5 storres
#
953 5 storres
print pobyso_get_prec()
954 5 storres
pobyso_set_prec(165)
955 5 storres
print pobyso_get_prec()
956 5 storres
a=100
957 5 storres
print type(a)
958 5 storres
id(a)
959 5 storres
print "Max arity: ", pobyso_max_arity
960 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
961 56 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
962 56 storres
print "...Pobyso check done"