Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 209

Historique | Voir | Annoter | Télécharger (58 ko)

1 5 storres
"""
2 209 storres
@file pobyso.py
3 5 storres
Actual functions to use in Sage
4 5 storres
ST 2012-11-13
5 5 storres

6 5 storres
Command line syntax:
7 5 storres
  use from Sage (via the "load" or the "attach" commands)
8 5 storres

9 38 storres
pobyso functions come in five flavors:
10 57 storres
- the _so_so (arguments and returned objects are pointers to Sollya objects,
11 57 storres
  includes the void function and the no arguments function that return a
12 57 storres
  pointer to a Sollya object);
13 38 storres
- the _so_sa (argument are pointers to Sollya objects, returned objects are
14 57 storres
  Sage/Python objects or, more generally, information is transfered from the
15 57 storres
  Sollya world to Sage/Python world; e.g. functions without arguments that
16 57 storres
  return a Sage/Python object);
17 38 storres
- the _sa_so (arguments are Sage/Python objects, returned objects are
18 38 storres
  pointers to Sollya objects);
19 38 storres
- the sa_sa (arguments and returned objects are all Sage/Python objects);
20 51 storres
- a catch all flavor, without any suffix, (e. g. functions that have no argument
21 51 storres
  nor return value).
22 57 storres
This classification is not always very strict. Conversion functions from Sollya
23 57 storres
to Sage/Python are sometimes decorated with Sage/Python arguments to set
24 57 storres
the precision. These functions remain in the so_sa category.
25 5 storres
NOTES:
26 5 storres
Reported errors in Eclipse come from the calls to
27 5 storres
the Sollya library
28 5 storres

29 10 storres
ToDo (among other things):
30 10 storres
 -memory management.
31 5 storres
"""
32 5 storres
from ctypes import *
33 37 storres
import re
34 37 storres
from sage.symbolic.expression_conversions import polynomial
35 59 storres
from sage.symbolic.expression_conversions import PolynomialConverter
36 38 storres
"""
37 38 storres
Create the equivalent to an enum for the Sollya function types.
38 38 storres
"""
39 5 storres
(SOLLYA_BASE_FUNC_ABS,
40 5 storres
SOLLYA_BASE_FUNC_ACOS,
41 5 storres
    SOLLYA_BASE_FUNC_ACOSH,
42 5 storres
    SOLLYA_BASE_FUNC_ADD,
43 5 storres
    SOLLYA_BASE_FUNC_ASIN,
44 5 storres
    SOLLYA_BASE_FUNC_ASINH,
45 5 storres
    SOLLYA_BASE_FUNC_ATAN,
46 5 storres
    SOLLYA_BASE_FUNC_ATANH,
47 5 storres
    SOLLYA_BASE_FUNC_CEIL,
48 5 storres
    SOLLYA_BASE_FUNC_CONSTANT,
49 5 storres
    SOLLYA_BASE_FUNC_COS,
50 5 storres
    SOLLYA_BASE_FUNC_COSH,
51 5 storres
    SOLLYA_BASE_FUNC_DIV,
52 5 storres
    SOLLYA_BASE_FUNC_DOUBLE,
53 5 storres
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
54 5 storres
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
55 5 storres
    SOLLYA_BASE_FUNC_ERF,
56 5 storres
    SOLLYA_BASE_FUNC_ERFC,
57 5 storres
    SOLLYA_BASE_FUNC_EXP,
58 5 storres
    SOLLYA_BASE_FUNC_EXP_M1,
59 5 storres
    SOLLYA_BASE_FUNC_FLOOR,
60 5 storres
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
61 5 storres
    SOLLYA_BASE_FUNC_HALFPRECISION,
62 5 storres
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
63 5 storres
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
64 5 storres
    SOLLYA_BASE_FUNC_LOG,
65 5 storres
    SOLLYA_BASE_FUNC_LOG_10,
66 5 storres
    SOLLYA_BASE_FUNC_LOG_1P,
67 5 storres
    SOLLYA_BASE_FUNC_LOG_2,
68 5 storres
    SOLLYA_BASE_FUNC_MUL,
69 5 storres
    SOLLYA_BASE_FUNC_NEARESTINT,
70 5 storres
    SOLLYA_BASE_FUNC_NEG,
71 5 storres
    SOLLYA_BASE_FUNC_PI,
72 5 storres
    SOLLYA_BASE_FUNC_POW,
73 5 storres
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
74 5 storres
    SOLLYA_BASE_FUNC_QUAD,
75 5 storres
    SOLLYA_BASE_FUNC_SIN,
76 5 storres
    SOLLYA_BASE_FUNC_SINGLE,
77 5 storres
    SOLLYA_BASE_FUNC_SINH,
78 5 storres
    SOLLYA_BASE_FUNC_SQRT,
79 5 storres
    SOLLYA_BASE_FUNC_SUB,
80 5 storres
    SOLLYA_BASE_FUNC_TAN,
81 5 storres
    SOLLYA_BASE_FUNC_TANH,
82 5 storres
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
83 56 storres
print "\nSuperficial pobyso check..."
84 5 storres
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
85 5 storres
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
86 5 storres
87 5 storres
pobyso_max_arity = 9
88 5 storres
89 85 storres
def pobyso_absolute_so_so():
90 85 storres
    return(sollya_lib_absolute(None))
91 85 storres
92 5 storres
def pobyso_autoprint(arg):
93 5 storres
    sollya_lib_autoprint(arg,None)
94 5 storres
95 38 storres
def pobyso_autoprint_so_so(arg):
96 38 storres
    sollya_lib_autoprint(arg,None)
97 54 storres
98 84 storres
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa, \
99 84 storres
                                 precisionSa=None):
100 84 storres
    """
101 84 storres
    Return a Sollya range from to 2 RealField Sage elements.
102 84 storres
    The Sollya range element has a sufficient precision to hold all
103 162 storres
    the digits of the widest of the Sage bounds.
104 84 storres
    """
105 84 storres
    # Sanity check.
106 84 storres
    if rnLowerBoundSa > rnUpperBoundSa:
107 84 storres
        return None
108 115 storres
    # Precision stuff.
109 85 storres
    if precisionSa is None:
110 84 storres
        # Check for the largest precision.
111 84 storres
        lbPrecSa = rnLowerBoundSa.parent().precision()
112 84 storres
        ubPrecSa = rnLowerBoundSa.parent().precision()
113 84 storres
        maxPrecSa = max(lbPrecSa, ubPrecSa)
114 84 storres
    else:
115 84 storres
        maxPrecSa = precisionSa
116 115 storres
    # From Sage to Sollya bounds.
117 162 storres
#    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa),
118 162 storres
#                                       maxPrecSa)
119 162 storres
    lowerBoundSo = pobyso_constant_sa_so(rnLowerBoundSa,
120 162 storres
                                         maxPrecSa)
121 162 storres
    upperBoundSo = pobyso_constant_sa_so(rnUpperBoundSa,
122 154 storres
                                       maxPrecSa)
123 162 storres
124 115 storres
    # From Sollya bounds to range.
125 84 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
126 84 storres
    # Back to original precision.
127 84 storres
    # Clean up
128 84 storres
    sollya_lib_clear_obj(lowerBoundSo)
129 84 storres
    sollya_lib_clear_obj(upperBoundSo)
130 154 storres
    return rangeSo
131 84 storres
# End pobyso_bounds_to_range_sa_so
132 84 storres
133 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
134 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
135 54 storres
136 85 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
137 54 storres
    """
138 85 storres
    Variable change in a function.
139 85 storres
    """
140 85 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
141 85 storres
# End pobyso_change_var_in_function_so_so
142 85 storres
143 85 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
144 85 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
145 85 storres
    return(resultSo)
146 85 storres
# End pobyso_chebyshevform_so_so.
147 85 storres
148 117 storres
def pobyso_clear_taylorform_sa_so(taylorFormSaSo):
149 117 storres
    """
150 117 storres
    This method is necessary to correctly clean up the memory from Taylor forms.
151 117 storres
    These are made of a Sollya object, a Sollya object list, a Sollya object.
152 117 storres
    For no clearly understood reason, sollya_lib_clear_object_list crashed
153 117 storres
    when applied to the object list.
154 117 storres
    Here, we decompose it into Sage list of Sollya objects references and we
155 117 storres
     clear them one by one.
156 117 storres
    """
157 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[0])
158 117 storres
    (coefficientsErrorsListSaSo, numElementsSa, isEndEllipticSa) = \
159 117 storres
        pobyso_get_list_elements_so_so(taylorFormSaSo[1])
160 117 storres
    for element in coefficientsErrorsListSaSo:
161 117 storres
        sollya_lib_clear_obj(element)
162 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[1])
163 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[2])
164 117 storres
# End pobyso_clear_taylorform_sa_so
165 117 storres
166 85 storres
def pobyso_cmp(rnArgSa, cteSo):
167 85 storres
    """
168 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
169 54 storres

170 54 storres
    Get the value of the Sollya constant into a RealNumber and compare
171 54 storres
    using MPFR. Could be optimized by working directly with a mpfr_t
172 54 storres
    for the intermediate number.
173 54 storres
    """
174 115 storres
    # Get the precision of the Sollya constant to build a Sage RealNumber
175 115 storres
    # with enough precision.to hold it.
176 5 storres
    precisionOfCte = c_int(0)
177 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
178 85 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, cteSo)
179 5 storres
    #print "Precision of constant: ", precisionOfCte
180 5 storres
    RRRR = RealField(precisionOfCte.value)
181 85 storres
    rnLocalSa = RRRR(0)
182 85 storres
    sollya_lib_get_constant(get_rn_value(rnLocalSa), cteSo)
183 115 storres
    #
184 115 storres
    ## Compare the Sage RealNumber version of the Sollya constant with rnArg.
185 85 storres
    return(cmp_rn_value(rnArgSa, rnLocal))
186 83 storres
# End pobyso_smp
187 5 storres
188 54 storres
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
189 54 storres
                                                     upperBoundSa):
190 54 storres
    """
191 85 storres
    TODO: completely rework and test.
192 54 storres
    """
193 85 storres
    pobyso = pobyso_name_free_variable_sa_so(funcSa.variables()[0])
194 159 storres
    funcSo = pobyso_parse_string(funcSa._assume_str().replace('_SAGE_VAR_', ''))
195 54 storres
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
196 54 storres
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
197 83 storres
    # Sollya return the infnorm as an interval.
198 54 storres
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
199 54 storres
    # Get the top bound and compute the binade top limit.
200 54 storres
    fMaxUpperBoundSa = fMaxSa.upper()
201 54 storres
    binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
202 54 storres
    # Put up together the function to use to compute the lower bound.
203 54 storres
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
204 159 storres
                                    '-(' + f._assume_str().replace('_SAGE_VAR_', '') + ')')
205 54 storres
    pobyso_autoprint(funcAuxSo)
206 83 storres
    # Clear the Sollya range before a new call to infnorm and issue the call.
207 54 storres
    sollya_lib_clear_obj(infnormSo)
208 54 storres
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
209 54 storres
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
210 54 storres
    sollya_lib_clear_obj(infnormSo)
211 85 storres
    fMinLowerBoundSa = binadeTopLimitSa - fMinSa.lower()
212 54 storres
    # Compute the maximum of the precisions of the different bounds.
213 54 storres
    maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
214 54 storres
                     fMaxUpperBoundSa.parent().precision()])
215 54 storres
    # Create a RealIntervalField and create an interval with the "good" bounds.
216 54 storres
    RRRI = RealIntervalField(maxPrecSa)
217 54 storres
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
218 83 storres
    # Free the unneeded Sollya objects
219 54 storres
    sollya_lib_clear_obj(funcSo)
220 54 storres
    sollya_lib_clear_obj(funcAuxSo)
221 54 storres
    sollya_lib_clear_obj(rangeSo)
222 54 storres
    return(imageIntervalSa)
223 83 storres
# End pobyso_compute_pos_function_abs_val_bounds_sa_sa
224 54 storres
225 5 storres
def pobyso_constant(rnArg):
226 38 storres
    """ Legacy function. See pobyso_constant_sa_so. """
227 38 storres
    return(pobyso_constant_sa_so(rnArg))
228 5 storres
229 84 storres
def pobyso_constant_sa_so(rnArgSa, precisionSa=None):
230 52 storres
    """
231 115 storres
    Create a Sollya constant from a Sage RealNumber.
232 209 storres
    The sollya_lib_constant() function creates a constant
233 209 storres
    with the same precision as the source.
234 52 storres
    """
235 209 storres
    ## Precision stuff. If one wants to change precisions,
236 209 storres
    #  everything takes place in Sage. That only makes
237 209 storres
    #  sense if one wants to reduce the precision.
238 209 storres
    if not precisionSa is None:
239 209 storres
        RRR = RealField(precisionSa)
240 209 storres
        rnArgSa = RRR(rnArgSa)
241 209 storres
    #print rnArgSa, rnArgSa.precision()
242 115 storres
    # Sollya constant creation takes place here.
243 209 storres
    return sollya_lib_constant(get_rn_value(rnArgSa))
244 115 storres
# End pobyso_constant_sa_so
245 115 storres
246 55 storres
def pobyso_constant_0_sa_so():
247 115 storres
    """
248 115 storres
    Obvious.
249 115 storres
    """
250 55 storres
    return(pobyso_constant_from_int_sa_so(0))
251 55 storres
252 5 storres
def pobyso_constant_1():
253 115 storres
    """
254 115 storres
    Obvious.
255 115 storres
    Legacy function. See pobyso_constant_so_so.
256 115 storres
    """
257 52 storres
    return(pobyso_constant_1_sa_so())
258 5 storres
259 52 storres
def pobyso_constant_1_sa_so():
260 115 storres
    """
261 115 storres
    Obvious.
262 115 storres
    """
263 38 storres
    return(pobyso_constant_from_int_sa_so(1))
264 38 storres
265 5 storres
def pobyso_constant_from_int(anInt):
266 38 storres
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
267 38 storres
    return(pobyso_constant_from_int_sa_so(anInt))
268 38 storres
269 38 storres
def pobyso_constant_from_int_sa_so(anInt):
270 115 storres
    """
271 115 storres
    Get a Sollya constant from a Sage int.
272 115 storres
    """
273 200 storres
    return(sollya_lib_constant_from_int64(long(anInt)))
274 5 storres
275 84 storres
def pobyso_constant_from_int_so_sa(constSo):
276 84 storres
    """
277 117 storres
    Get a Sage int from a Sollya int constant.
278 115 storres
    Usefull for precision or powers in polynomials.
279 84 storres
    """
280 200 storres
    constSa = c_long(0)
281 200 storres
    sollya_lib_get_constant_as_int64(byref(constSa), constSo)
282 84 storres
    return(constSa.value)
283 84 storres
# End pobyso_constant_from_int_so_sa
284 84 storres
285 209 storres
def pobyso_constant_from_mpq_sa_so(rationalSa):
286 200 storres
    """
287 200 storres
    Make a Sollya constant from Sage rational.
288 209 storres
    The Sollya constant is an unevaluated expression.
289 209 storres
    Hence no precision argument is needed.
290 209 storres
    It is better to leave this way since Sollya has its own
291 209 storres
    optimized evaluation mecanism that tries very hard to
292 209 storres
    return exact values or at least faithful ones.
293 200 storres
    """
294 200 storres
    ratExprSo = \
295 200 storres
        sollya_lib_constant_from_mpq(sgmp_get_rational_value(rationalSa))
296 209 storres
    return ratExprSo
297 200 storres
# End pobyso_constant_from_mpq_sa_so.
298 200 storres
299 209 storres
def pobyso_constant_sollya_prec_sa_so(rnArgSa):
300 209 storres
    """
301 209 storres
    Create a Sollya constant from a Sage RealNumber at the
302 209 storres
    current precision in Sollya.
303 209 storres
    """
304 209 storres
    currentSollyaPrecSa = pobyso_get_prec_so_sa()
305 209 storres
    return pobyso_constant_sa_so(rnArgSa, currentSollyaPrecSa)
306 209 storres
# End pobyso_constant_sollya_prec_sa_so
307 209 storres
308 155 storres
def pobyso_error_so():
309 155 storres
    return sollya_lib_error(None)
310 155 storres
# End pobyso_error().
311 155 storres
312 209 storres
def pobyso_float_poly_sa_so(polySa, precSa = None):
313 209 storres
    """
314 209 storres
    Create a Sollya polynomial from a Sage RealField polynomial.
315 209 storres
    """
316 209 storres
    ## TODO: filter arguments.
317 209 storres
    ## Precision. If a precision is given, convert the polynomial
318 209 storres
    #  into the right polynomial field. If not convert it straight
319 209 storres
    #  to Sollya.
320 209 storres
    if not precSa is None:
321 209 storres
        RRR = RealField(precSa)
322 209 storres
        ## Create a Sage polynomial in the "right" precision.
323 209 storres
        P_RRR = RRR[polySa.variables()[0]]
324 209 storres
        polyFloatSa = P_RRR(polySa)
325 209 storres
    else:
326 209 storres
        polyFloatSa = polySa
327 209 storres
        precSa = polySa.parent().base_ring().precision()
328 209 storres
    ## Get exponents and coefficients.
329 209 storres
    exponentSa = polyFloatSa.exponents()
330 209 storres
    coefficientsSa = polyFloatSa.coefficients()
331 209 storres
    ## Build the polynomial.
332 209 storres
    polySo = None
333 209 storres
    for coefficientSa, exponentSa in zip(coefficientsSa, exponentSa):
334 209 storres
        #print coefficientSa.n(prec=precSa), exponentSa
335 209 storres
        coefficientSo = \
336 209 storres
            pobyso_constant_sa_so(coefficientSa)
337 209 storres
        #pobyso_autoprint(coefficientSo)
338 209 storres
        exponentSo = \
339 209 storres
            pobyso_constant_from_int_sa_so(exponentSa)
340 209 storres
        #pobyso_autoprint(exponentSo)
341 209 storres
        monomialSo = sollya_lib_build_function_pow(
342 209 storres
                       sollya_lib_build_function_free_variable(),
343 209 storres
                       exponentSo)
344 209 storres
        if polySo is None:
345 209 storres
            polySo = sollya_lib_build_function_mul(coefficientSo,
346 209 storres
                                                   monomialSo)
347 209 storres
        else:
348 209 storres
            polyTermSo = sollya_lib_build_function_mul(coefficientSo,
349 209 storres
                                                       monomialSo)
350 209 storres
            polySo = sollya_lib_build_function_add(polySo, polyTermSo)
351 209 storres
    return polySo
352 209 storres
# End pobyso_float_poly_sa_so
353 209 storres
354 209 storres
def pobyso_float_poly_so_sa(polySo, realFieldSa=None):
355 209 storres
    """
356 209 storres
    Convert a Sollya polynomial into a Sage floating-point polynomial.
357 209 storres
    We assume that the polynomial is in canonical form.
358 209 storres
    If no realField is given, a RealField corresponding to the maximum
359 209 storres
    precision of the coefficients is internally computed.
360 209 storres
    The real field is not returned but can be easily retrieved from
361 209 storres
    the polynomial itself.
362 209 storres
    ALGORITHM:
363 209 storres
    - (optional) compute the RealField of the coefficients;
364 209 storres
    - convert the Sollya expression into a Sage expression;
365 209 storres
    - convert the Sage expression into a Sage polynomial
366 209 storres
    TODO: the canonical thing for the polynomial.
367 209 storres
    """
368 209 storres
    if realFieldSa is None:
369 209 storres
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
370 209 storres
        realFieldSa      = RealField(expressionPrecSa)
371 209 storres
    #print "Sollya expression before...",
372 209 storres
    #pobyso_autoprint(polySo)
373 209 storres
374 209 storres
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo,
375 209 storres
                                                             realFieldSa)
376 209 storres
    #print "...Sollya expression after.",
377 209 storres
    #pobyso_autoprint(polySo)
378 209 storres
    polyVariableSa = expressionSa.variables()[0]
379 209 storres
    polyRingSa     = realFieldSa[str(polyVariableSa)]
380 209 storres
    #print polyRingSa
381 209 storres
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
382 209 storres
    polynomialSa = polyRingSa(expressionSa)
383 209 storres
    return polynomialSa
384 209 storres
# End pobyso_float_poly_so_sa
385 209 storres
386 209 storres
387 5 storres
def pobyso_function_type_as_string(funcType):
388 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
389 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
390 38 storres
391 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
392 38 storres
    """
393 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
394 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
395 38 storres
    """
396 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
397 5 storres
        return "abs"
398 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
399 5 storres
        return "arccos"
400 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
401 5 storres
        return "arccosh"
402 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
403 5 storres
        return "+"
404 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
405 5 storres
        return "arcsin"
406 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
407 5 storres
        return "arcsinh"
408 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
409 5 storres
        return "arctan"
410 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
411 5 storres
        return "arctanh"
412 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
413 5 storres
        return "ceil"
414 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
415 5 storres
        return "cte"
416 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
417 5 storres
        return "cos"
418 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
419 5 storres
        return "cosh"
420 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
421 5 storres
        return "/"
422 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
423 5 storres
        return "double"
424 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
425 5 storres
        return "doubleDouble"
426 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
427 5 storres
        return "doubleDxtended"
428 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
429 5 storres
        return "erf"
430 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
431 5 storres
        return "erfc"
432 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
433 5 storres
        return "exp"
434 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
435 5 storres
        return "expm1"
436 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
437 5 storres
        return "floor"
438 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
439 5 storres
        return "freeVariable"
440 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
441 5 storres
        return "halfPrecision"
442 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
443 5 storres
        return "libraryConstant"
444 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
445 5 storres
        return "libraryFunction"
446 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
447 5 storres
        return "log"
448 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
449 5 storres
        return "log10"
450 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
451 5 storres
        return "log1p"
452 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
453 5 storres
        return "log2"
454 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
455 5 storres
        return "*"
456 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
457 5 storres
        return "round"
458 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
459 5 storres
        return "__neg__"
460 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
461 5 storres
        return "pi"
462 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
463 5 storres
        return "^"
464 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
465 5 storres
        return "procedureFunction"
466 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
467 5 storres
        return "quad"
468 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
469 5 storres
        return "sin"
470 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
471 5 storres
        return "single"
472 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
473 5 storres
        return "sinh"
474 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
475 5 storres
        return "sqrt"
476 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
477 5 storres
        return "-"
478 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
479 5 storres
        return "tan"
480 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
481 5 storres
        return "tanh"
482 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
483 5 storres
        return "tripleDouble"
484 5 storres
    else:
485 5 storres
        return None
486 5 storres
487 85 storres
def pobyso_get_constant(rnArgSa, constSo):
488 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
489 209 storres
    return pobyso_get_constant_so_sa(rnArgSa, constSo)
490 209 storres
# End pobyso_get_constant
491 38 storres
492 84 storres
def pobyso_get_constant_so_sa(rnArgSa, constSo):
493 52 storres
    """
494 85 storres
    Set the value of rnArgSo to the value of constSo in MPFR_RNDN mode.
495 52 storres
    rnArg must already exist and belong to some RealField.
496 85 storres
    We assume that constSo points to a Sollya constant.
497 52 storres
    """
498 209 storres
    outcome = sollya_lib_get_constant(get_rn_value(rnArgSa), constSo)
499 209 storres
    if outcome == 0: # Failure because constSo is not a constant expression.
500 209 storres
        return None
501 209 storres
    else:
502 209 storres
        return outcome
503 209 storres
# End  pobyso_get_constant_so_sa
504 209 storres
505 57 storres
def pobyso_get_constant_as_rn(ctExpSo):
506 83 storres
    """
507 83 storres
    Legacy function. See pobyso_get_constant_as_rn_so_sa.
508 83 storres
    """
509 57 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
510 38 storres
511 56 storres
def pobyso_get_constant_as_rn_so_sa(constExpSo):
512 83 storres
    """
513 83 storres
    Get a Sollya constant as a Sage "real number".
514 83 storres
    The precision of the floating-point number returned is that of the Sollya
515 83 storres
    constant.
516 83 storres
    """
517 209 storres
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
518 209 storres
    ## If the expression can not be exactly converted, None is returned.
519 209 storres
    #  In this case opt for the Sollya current expression.
520 209 storres
    if precisionSa is None:
521 209 storres
        precisionSa = pobyso_get_prec_so_sa()
522 56 storres
    RRRR = RealField(precisionSa)
523 56 storres
    rnSa = RRRR(0)
524 209 storres
    outcome = sollya_lib_get_constant(get_rn_value(rnSa), constExpSo)
525 209 storres
    if outcome == 0:
526 209 storres
        return None
527 209 storres
    else:
528 209 storres
        return rnSa
529 83 storres
# End pobyso_get_constant_as_rn_so_sa
530 38 storres
531 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
532 83 storres
    """
533 83 storres
    Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
534 83 storres
    """
535 209 storres
    return pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField)
536 209 storres
# End pobyso_get_constant_as_rn_with_rf
537 5 storres
538 56 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None):
539 83 storres
    """
540 83 storres
    Get a Sollya constant as a Sage "real number".
541 83 storres
    If no real field is specified, the precision of the floating-point number
542 85 storres
    returned is that of the Sollya constant.
543 83 storres
    Otherwise is is that of the real field. Hence rounding may happen.
544 83 storres
    """
545 56 storres
    if realFieldSa is None:
546 209 storres
        return pobyso_get_constant_as_rn_so_sa(ctExpSo)
547 56 storres
    rnSa = realFieldSa(0)
548 209 storres
    outcome = sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo)
549 209 storres
    if outcome == 0:
550 209 storres
        return None
551 209 storres
    else:
552 209 storres
        return rnSa
553 83 storres
# End pobyso_get_constant_as_rn_with_rf_so_sa
554 38 storres
555 5 storres
def pobyso_get_free_variable_name():
556 83 storres
    """
557 83 storres
    Legacy function. See pobyso_get_free_variable_name_so_sa.
558 83 storres
    """
559 38 storres
    return(pobyso_get_free_variable_name_so_sa())
560 38 storres
561 38 storres
def pobyso_get_free_variable_name_so_sa():
562 209 storres
    return sollya_lib_get_free_variable_name()
563 5 storres
564 38 storres
def pobyso_get_function_arity(expressionSo):
565 83 storres
    """
566 83 storres
    Legacy function. See pobyso_get_function_arity_so_sa.
567 83 storres
    """
568 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
569 38 storres
570 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
571 5 storres
    arity = c_int(0)
572 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
573 209 storres
    return int(arity.value)
574 5 storres
575 38 storres
def pobyso_get_head_function(expressionSo):
576 83 storres
    """
577 83 storres
    Legacy function. See pobyso_get_head_function_so_sa.
578 83 storres
    """
579 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
580 38 storres
581 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
582 5 storres
    functionType = c_int(0)
583 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
584 209 storres
    return int(functionType.value)
585 5 storres
586 56 storres
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
587 53 storres
    """
588 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
589 83 storres
    If no reaIntervalField is passed as an argument, the interval bounds are not
590 56 storres
    rounded: they are elements of RealIntervalField of the "right" precision
591 56 storres
    to hold all the digits.
592 53 storres
    """
593 53 storres
    prec = c_int(0)
594 56 storres
    if realIntervalFieldSa is None:
595 56 storres
        retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
596 56 storres
        if retval == 0:
597 209 storres
            return None
598 56 storres
        realIntervalFieldSa = RealIntervalField(prec.value)
599 56 storres
    intervalSa = realIntervalFieldSa(0,0)
600 53 storres
    retval = \
601 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
602 53 storres
                                           soRange)
603 53 storres
    if retval == 0:
604 209 storres
        return None
605 209 storres
    return intervalSa
606 56 storres
# End pobyso_get_interval_from_range_so_sa
607 56 storres
608 5 storres
def pobyso_get_list_elements(soObj):
609 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
610 209 storres
    return pobyso_get_list_elements_so_so(soObj)
611 38 storres
612 117 storres
def pobyso_get_list_elements_so_so(objectListSo):
613 51 storres
    """
614 118 storres
    Get the Sollya list elements as a Sage/Python array of Sollya objects.
615 118 storres

616 118 storres
    INPUT:
617 118 storres
    - objectListSo: a Sollya list of Sollya objects.
618 118 storres

619 118 storres
    OUTPUT:
620 118 storres
    - a Sage/Python tuple made of:
621 118 storres
      - a Sage/Python list of Sollya objects,
622 118 storres
      - a Sage/Python int holding the number of elements,
623 118 storres
      - a Sage/Python int stating (!= 0) that the list is end-elliptic.
624 118 storres
    NOTE::
625 118 storres
        We recover the addresses of the Sollya object from the list of pointers
626 118 storres
        returned by sollya_lib_get_list_elements. The list itself is freed.
627 118 storres
    TODO::
628 118 storres
        Figure out what to do with numElements since the number of elements
629 118 storres
        can easily be recovered from the list itself.
630 118 storres
        Ditto for isEndElliptic.
631 51 storres
    """
632 5 storres
    listAddress = POINTER(c_longlong)()
633 5 storres
    numElements = c_int(0)
634 5 storres
    isEndElliptic = c_int(0)
635 117 storres
    listAsSageList = []
636 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
637 54 storres
                                          byref(numElements),\
638 54 storres
                                          byref(isEndElliptic),\
639 117 storres
                                          objectListSo)
640 5 storres
    if result == 0 :
641 5 storres
        return None
642 5 storres
    for i in xrange(0, numElements.value, 1):
643 118 storres
       #listAsSageList.append(sollya_lib_copy_obj(listAddress[i]))
644 118 storres
       listAsSageList.append(listAddress[i])
645 117 storres
       # Clear each of the elements returned by Sollya.
646 118 storres
       #sollya_lib_clear_obj(listAddress[i])
647 117 storres
    # Free the list itself.
648 117 storres
    sollya_lib_free(listAddress)
649 209 storres
    return (listAsSageList, numElements.value, isEndElliptic.value)
650 5 storres
651 38 storres
def pobyso_get_max_prec_of_exp(soExp):
652 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
653 209 storres
    return pobyso_get_max_prec_of_exp_so_sa(soExp)
654 5 storres
655 85 storres
def pobyso_get_max_prec_of_exp_so_sa(expSo):
656 38 storres
    """
657 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
658 52 storres

659 52 storres
    Arguments:
660 52 storres
    soExp -- a Sollya expression pointer
661 52 storres
    Return value:
662 52 storres
    A Python integer
663 38 storres
    TODO:
664 38 storres
    - error management;
665 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
666 38 storres
    """
667 5 storres
    maxPrecision = 0
668 52 storres
    minConstPrec = 0
669 52 storres
    currentConstPrec = 0
670 85 storres
    operator = pobyso_get_head_function_so_sa(expSo)
671 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
672 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
673 85 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(expSo)
674 5 storres
        for i in xrange(arity):
675 5 storres
            maxPrecisionCandidate = \
676 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
677 5 storres
            if maxPrecisionCandidate > maxPrecision:
678 5 storres
                maxPrecision = maxPrecisionCandidate
679 209 storres
        return maxPrecision
680 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
681 85 storres
        #minConstPrec = pobyso_get_min_prec_of_constant_so_sa(expSo)
682 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
683 52 storres
        #print minConstPrec, " - ", currentConstPrec
684 209 storres
        return pobyso_get_min_prec_of_constant_so_sa(expSo)
685 52 storres
686 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
687 209 storres
        return 0
688 5 storres
    else:
689 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
690 209 storres
        return 0
691 5 storres
692 85 storres
def pobyso_get_min_prec_of_constant_so_sa(constExpSo):
693 52 storres
    """
694 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
695 52 storres
    constant.
696 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
697 209 storres
    We assume that constExpSo is a pointer to a Sollay constant expression.
698 52 storres
    """
699 85 storres
    constExpAsRnSa = pobyso_get_constant_as_rn_so_sa(constExpSo)
700 85 storres
    return(min_mpfr_size(get_rn_value(constExpAsRnSa)))
701 52 storres
702 200 storres
def pobyso_get_poly_so_sa(polySo, realFieldSa=None):
703 200 storres
    """
704 200 storres
    Convert a Sollya polynomial into a Sage polynomial.
705 209 storres
    Legacy function. Use pobyso_float_poly_so_sa() instead.
706 200 storres
    """
707 209 storres
    return pobyso_float_poly_so_sa(polySo,realField)
708 200 storres
# End pobyso_get_poly_so_sa
709 200 storres
710 200 storres
def pobyso_get_prec():
711 200 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
712 209 storres
    return pobyso_get_prec_so_sa()
713 200 storres
714 200 storres
def pobyso_get_prec_so():
715 200 storres
    """
716 200 storres
    Get the current default precision in Sollya.
717 200 storres
    The return value is a Sollya object.
718 200 storres
    Usefull when modifying the precision back and forth by avoiding
719 200 storres
    extra conversions.
720 200 storres
    """
721 209 storres
    return sollya_lib_get_prec(None)
722 200 storres
723 200 storres
def pobyso_get_prec_so_sa():
724 200 storres
    """
725 200 storres
    Get the current default precision in Sollya.
726 200 storres
    The return value is Sage/Python int.
727 200 storres
    """
728 200 storres
    precSo = sollya_lib_get_prec(None)
729 200 storres
    precSa = c_int(0)
730 200 storres
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
731 200 storres
    sollya_lib_clear_obj(precSo)
732 200 storres
    return int(precSa.value)
733 200 storres
# End pobyso_get_prec_so_sa.
734 200 storres
735 209 storres
def pobyso_get_prec_so_so_sa():
736 209 storres
    """
737 209 storres
    Return the current precision both as a Sollya object and a
738 209 storres
    Sage integer as hybrid tuple.
739 209 storres
    To avoid multiple calls for precision manipulations.
740 209 storres
    """
741 209 storres
    precSo = sollya_lib_get_prec(None)
742 209 storres
    precSa = c_int(0)
743 209 storres
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
744 209 storres
    return (precSo, precSa)
745 200 storres
746 200 storres
def pobyso_get_prec_of_constant(ctExpSo):
747 200 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
748 209 storres
    return pobyso_get_prec_of_constant_so_sa(ctExpSo)
749 200 storres
750 200 storres
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
751 200 storres
    """
752 200 storres
    Tries to find a precision to represent ctExpSo without rounding.
753 200 storres
    If not possible, returns None.
754 200 storres
    """
755 200 storres
    prec = c_int(0)
756 200 storres
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
757 200 storres
    if retc == 0:
758 209 storres
        return None
759 209 storres
    return int(prec.value)
760 200 storres
761 200 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
762 200 storres
    """
763 200 storres
    Returns the number of bits elements of a range are coded with.
764 200 storres
    """
765 200 storres
    prec = c_int(0)
766 200 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
767 200 storres
    if retc == 0:
768 200 storres
        return(None)
769 209 storres
    return int(prec.value)
770 200 storres
# End pobyso_get_prec_of_range_so_sa()
771 200 storres
772 85 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExpSo, realField = RR):
773 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
774 209 storres
    return pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo,
775 209 storres
                                                     realField = RR)
776 38 storres
777 85 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, realFieldSa = RR):
778 5 storres
    """
779 38 storres
    Get a Sage expression from a Sollya expression.
780 38 storres
    Currently only tested with polynomials with floating-point coefficients.
781 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
782 5 storres
    """
783 5 storres
    #pobyso_autoprint(sollyaExp)
784 85 storres
    operatorSa = pobyso_get_head_function_so_sa(sollyaExpSo)
785 83 storres
    sollyaLibFreeVariableName = sollya_lib_get_free_variable_name()
786 5 storres
    # Constants and the free variable are special cases.
787 5 storres
    # All other operator are dealt with in the same way.
788 85 storres
    if (operatorSa != SOLLYA_BASE_FUNC_CONSTANT) and \
789 85 storres
       (operatorSa != SOLLYA_BASE_FUNC_FREE_VARIABLE):
790 85 storres
        (aritySa, subexpressionsSa) = pobyso_get_subfunctions_so_sa(sollyaExpSo)
791 85 storres
        if aritySa == 1:
792 85 storres
            sageExpSa = eval(pobyso_function_type_as_string_so_sa(operatorSa) + \
793 85 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], \
794 85 storres
            realFieldSa) + ")")
795 85 storres
        elif aritySa == 2:
796 63 storres
            # We do not get through the preprocessor.
797 63 storres
            # The "^" operator is then a special case.
798 85 storres
            if operatorSa == SOLLYA_BASE_FUNC_POW:
799 85 storres
                operatorAsStringSa = "**"
800 5 storres
            else:
801 85 storres
                operatorAsStringSa = \
802 85 storres
                    pobyso_function_type_as_string_so_sa(operatorSa)
803 85 storres
            sageExpSa = \
804 85 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], realFieldSa)"\
805 85 storres
              + " " + operatorAsStringSa + " " + \
806 85 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[1], realFieldSa)")
807 63 storres
        # We do not know yet how to deal with arity >= 3
808 63 storres
        # (is there any in Sollya anyway?).
809 5 storres
        else:
810 85 storres
            sageExpSa = eval('None')
811 209 storres
        return sageExpSa
812 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_CONSTANT:
813 5 storres
        #print "This is a constant"
814 85 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExpSo, realFieldSa)
815 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_FREE_VARIABLE:
816 5 storres
        #print "This is free variable"
817 209 storres
        return eval(sollyaLibFreeVariableName)
818 5 storres
    else:
819 5 storres
        print "Unexpected"
820 5 storres
        return eval('None')
821 185 storres
# End pobyso_get_sage_exp_from_sollya_exp_so_sa
822 73 storres
823 185 storres
824 38 storres
def pobyso_get_subfunctions(expressionSo):
825 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
826 209 storres
    return pobyso_get_subfunctions_so_sa(expressionSo)
827 200 storres
# End pobyso_get_subfunctions.
828 200 storres
829 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
830 38 storres
    """
831 38 storres
    Get the subfunctions of an expression.
832 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
833 55 storres
    S.T.: Could not figure out another way than that ugly list of declarations
834 83 storres
    to recover the addresses of the subfunctions.
835 83 storres
    We limit ourselves to arity 8 functions.
836 38 storres
    """
837 5 storres
    subf0 = c_int(0)
838 5 storres
    subf1 = c_int(0)
839 5 storres
    subf2 = c_int(0)
840 5 storres
    subf3 = c_int(0)
841 5 storres
    subf4 = c_int(0)
842 5 storres
    subf5 = c_int(0)
843 5 storres
    subf6 = c_int(0)
844 5 storres
    subf7 = c_int(0)
845 5 storres
    subf8 = c_int(0)
846 5 storres
    arity = c_int(0)
847 5 storres
    nullPtr = POINTER(c_int)()
848 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
849 83 storres
      byref(subf0), byref(subf1), byref(subf2), byref(subf3), \
850 83 storres
      byref(subf4), byref(subf5),\
851 83 storres
      byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
852 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
853 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
854 83 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), \
855 83 storres
#    byref(cast(subfunctions[3], POINTER(c_int))), \
856 83 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), \
857 83 storres
#    byref(cast(subfunctions[5], POINTER(c_int))), \
858 83 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), \
859 83 storres
#    byref(cast(subfunctions[7], POINTER(c_int))), \
860 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
861 83 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \
862 83 storres
                    subf8]
863 5 storres
    subs = []
864 5 storres
    if arity.value > pobyso_max_arity:
865 38 storres
        return(0,[])
866 5 storres
    for i in xrange(arity.value):
867 5 storres
        subs.append(int(subfunctions[i].value))
868 5 storres
        #print subs[i]
869 209 storres
    return (int(arity.value), subs)
870 200 storres
# End pobyso_get_subfunctions_so_sa
871 5 storres
872 155 storres
def pobyso_guess_degree_sa_sa(functionSa, intervalSa, approxErrorSa,
873 155 storres
                              weightSa=None, degreeBoundSa=None):
874 155 storres
    """
875 155 storres
    Sa_sa variant of the solly_guessdegree function.
876 155 storres
    Return 0 if something goes wrong.
877 155 storres
    """
878 159 storres
    functionAsStringSa = functionSa._assume_str().replace('_SAGE_VAR_', '')
879 154 storres
    functionSo = pobyso_parse_string_sa_so(functionAsStringSa)
880 155 storres
    if pobyso_is_error_so_sa(functionSo):
881 155 storres
        sollya_lib_clear_obj(functionSo)
882 155 storres
        return 0
883 154 storres
    rangeSo = pobyso_interval_to_range_sa_so(intervalSa)
884 155 storres
    # The approximation error is expected to be a floating point number.
885 155 storres
    if pobyso_is_floating_point_number_sa_sa(approxErrorSa):
886 155 storres
        approxErrorSo = pobyso_constant_sa_so(approxErrorSa)
887 155 storres
    else:
888 155 storres
        approxErrorSo = pobyso_constant_sa_so(RR(approxErrorSa))
889 154 storres
    if not weightSa is None:
890 159 storres
        weightAsStringSa = weightSa._assume_str().replace('_SAGE_VAR_', '')
891 154 storres
        weightSo = pobyso_parse_string_sa_so(weightAsStringSa)
892 166 storres
        if pobyso_is_error_so_sa(weightSo):
893 155 storres
            sollya_lib_clear_obj(functionSo)
894 155 storres
            sollya_lib_clear_obj(rangeSo)
895 155 storres
            sollya_lib_clear_obj(approxErrorSo)
896 155 storres
            sollya_lib_clear_obj(weightSo)
897 155 storres
            return 0
898 154 storres
    else:
899 154 storres
        weightSo = None
900 154 storres
    if not degreeBoundSa is None:
901 154 storres
        degreeBoundSo = pobyso_constant_from_int_sa_so(degreeBoundSa)
902 154 storres
    else:
903 154 storres
        degreeBoundSo = None
904 154 storres
    guessedDegreeSa = pobyso_guess_degree_so_sa(functionSo,
905 162 storres
                                                rangeSo,
906 162 storres
                                                approxErrorSo,
907 162 storres
                                                weightSo,
908 162 storres
                                                degreeBoundSo)
909 154 storres
    sollya_lib_clear_obj(functionSo)
910 154 storres
    sollya_lib_clear_obj(rangeSo)
911 155 storres
    sollya_lib_clear_obj(approxErrorSo)
912 154 storres
    if not weightSo is None:
913 154 storres
        sollya_lib_clear_obj(weightSo)
914 154 storres
    if not degreeBoundSo is None:
915 154 storres
        sollya_lib_clear_obj(degreeBoundSo)
916 154 storres
    return guessedDegreeSa
917 154 storres
# End poyso_guess_degree_sa_sa
918 154 storres
919 153 storres
def pobyso_guess_degree_so_sa(functionSo, rangeSo, errorSo, weightSo=None, \
920 154 storres
                              degreeBoundSo=None):
921 154 storres
    """
922 154 storres
    Thin wrapper around the guessdegree function.
923 154 storres
    Nevertheless, some precision control stuff has been appended.
924 154 storres
    """
925 154 storres
    # Deal with Sollya internal precision issues: if it is too small,
926 154 storres
    # compared with the error, increases it to about twice -log2(error).
927 154 storres
    errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(errorSo)
928 154 storres
    log2ErrorSa = errorSa.log2()
929 154 storres
    if log2ErrorSa < 0:
930 154 storres
        neededPrecisionSa = int(2 * int(-log2ErrorSa) / 64) * 64
931 154 storres
    else:
932 154 storres
        neededPrecisionSa = int(2 * int(log2ErrorSa) / 64) * 64
933 154 storres
    #print "Needed precision:", neededPrecisionSa
934 154 storres
    currentPrecSa = pobyso_get_prec_so_sa()
935 154 storres
    if neededPrecisionSa > currentPrecSa:
936 154 storres
        currentPrecSo = pobyso_get_prec_so()
937 154 storres
        pobyso_set_prec_sa_so(neededPrecisionSa)
938 166 storres
    #print "Guessing degree..."
939 153 storres
    # weightSo and degreeBoundsSo are optional arguments.
940 162 storres
    # As declared, sollya_lib_guessdegree must take 5 arguments.
941 153 storres
    if weightSo is None:
942 162 storres
        degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, errorSo,
943 162 storres
                                               0, 0, None)
944 154 storres
    elif degreeBoundSo is None:
945 153 storres
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, \
946 162 storres
                                                errorSo, weightSo, 0, None)
947 153 storres
    else:
948 153 storres
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, errorSo, \
949 154 storres
                                                weightSo, degreeBoundSo, None)
950 166 storres
    #print "...degree guess done."
951 154 storres
    # Restore internal precision, if applicable.
952 154 storres
    if neededPrecisionSa > currentPrecSa:
953 154 storres
        pobyso_set_prec_so_so(currentPrecSo)
954 154 storres
        sollya_lib_clear_obj(currentPrecSo)
955 154 storres
    degreeIntervalSa = pobyso_range_to_interval_so_sa(degreeRangeSo)
956 154 storres
    sollya_lib_clear_obj(degreeRangeSo)
957 154 storres
    # When ok, both bounds match.
958 154 storres
    # When the degree bound is too low, the upper bound is the degree
959 154 storres
    # for which the error can be honored.
960 154 storres
    # When it really goes wrong, the upper bound is infinity.
961 154 storres
    if degreeIntervalSa.lower() == degreeIntervalSa.upper():
962 154 storres
        return int(degreeIntervalSa.lower())
963 154 storres
    else:
964 154 storres
        if degreeIntervalSa.upper().is_infinity():
965 154 storres
            return None
966 154 storres
        else:
967 154 storres
            return int(degreeIntervalSa.upper())
968 154 storres
    # End pobyso_guess_degree_so_sa
969 153 storres
970 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
971 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
972 209 storres
    return None
973 53 storres
974 84 storres
def pobyso_interval_to_range_sa_so(intervalSa, precisionSa=None):
975 84 storres
    if precisionSa is None:
976 84 storres
        precisionSa = intervalSa.parent().precision()
977 84 storres
    intervalSo = pobyso_bounds_to_range_sa_so(intervalSa.lower(),\
978 84 storres
                                              intervalSa.upper(),\
979 84 storres
                                              precisionSa)
980 209 storres
    return intervalSo
981 84 storres
# End pobyso_interval_to_range_sa_so
982 84 storres
983 155 storres
def pobyso_is_error_so_sa(objSo):
984 155 storres
    """
985 155 storres
    Thin wrapper around the sollya_lib_obj_is_error() function.
986 155 storres
    """
987 155 storres
    if sollya_lib_obj_is_error(objSo) != 0:
988 155 storres
        return True
989 155 storres
    else:
990 155 storres
        return False
991 155 storres
# End pobyso_is_error-so_sa
992 155 storres
993 155 storres
def pobyso_is_floating_point_number_sa_sa(numberSa):
994 155 storres
    """
995 209 storres
    Check whether a Sage number is floating point.
996 209 storres
    Exception stuff added because numbers other than
997 209 storres
    floating-point ones do not have the is_real() attribute.
998 155 storres
    """
999 209 storres
    try:
1000 209 storres
        return numberSa.is_real()
1001 209 storres
    except AttributeError:
1002 209 storres
        return False
1003 209 storres
# End pobyso_is_floating_piont_number_sa_sa
1004 155 storres
1005 37 storres
def pobyso_lib_init():
1006 37 storres
    sollya_lib_init(None)
1007 116 storres
1008 116 storres
def pobyso_lib_close():
1009 116 storres
    sollya_lib_close(None)
1010 37 storres
1011 85 storres
def pobyso_name_free_variable(freeVariableNameSa):
1012 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
1013 85 storres
    pobyso_name_free_variable_sa_so(freeVariableNameSa)
1014 38 storres
1015 85 storres
def pobyso_name_free_variable_sa_so(freeVariableNameSa):
1016 83 storres
    """
1017 83 storres
    Set the free variable name in Sollya from a Sage string.
1018 83 storres
    """
1019 85 storres
    sollya_lib_name_free_variable(freeVariableNameSa)
1020 37 storres
1021 5 storres
def pobyso_parse_string(string):
1022 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
1023 209 storres
    return pobyso_parse_string_sa_so(string)
1024 38 storres
1025 38 storres
def pobyso_parse_string_sa_so(string):
1026 83 storres
    """
1027 155 storres
    Get the Sollya expression computed from a Sage string or
1028 155 storres
    a Sollya error object if parsing failed.
1029 83 storres
    """
1030 209 storres
    return sollya_lib_parse_string(string)
1031 5 storres
1032 200 storres
def pobyso_precision_so_sa(ctExpSo):
1033 209 storres
    """
1034 209 storres
    Computes the necessary precision to represent a number.
1035 209 storres
    If x is not zero, it can be uniquely written as x = m · 2e
1036 209 storres
    where m is an odd integer and e is an integer.
1037 209 storres
    precision(x) returns the number of bits necessary to write m
1038 209 storres
    in binary (i.e. ceil(log2(m))).
1039 209 storres
    """
1040 209 storres
    #TODO: take care of the special case: 0, @NaN@, @Inf@
1041 200 storres
    precisionSo = sollya_lib_precision(ctExpSo)
1042 200 storres
    precisionSa = pobyso_constant_from_int_so_sa(precisionSo)
1043 200 storres
    sollya_lib_clear_obj(precisionSo)
1044 200 storres
    return precisionSa
1045 200 storres
# End pobyso_precision_so_sa
1046 200 storres
1047 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
1048 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
1049 209 storres
    return pobyso_range_sa_so(rnLowerBound, rnUpperBound)
1050 38 storres
1051 5 storres
1052 85 storres
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalFieldSa = None):
1053 83 storres
    """
1054 83 storres
    Get a Sage interval from a Sollya range.
1055 83 storres
    If no realIntervalField is given as a parameter, the Sage interval
1056 83 storres
    precision is that of the Sollya range.
1057 85 storres
    Otherwise, the precision is that of the realIntervalField. In this case
1058 85 storres
    rounding may happen.
1059 83 storres
    """
1060 85 storres
    if realIntervalFieldSa is None:
1061 56 storres
        precSa = pobyso_get_prec_of_range_so_sa(rangeSo)
1062 85 storres
        realIntervalFieldSa = RealIntervalField(precSa)
1063 56 storres
    intervalSa = \
1064 85 storres
        pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalFieldSa)
1065 209 storres
    return intervalSa
1066 209 storres
# End pobyso_range_to_interval_so_sa
1067 56 storres
1068 209 storres
def pobyso_rat_poly_sa_so(polySa, precSa = None):
1069 209 storres
    """
1070 209 storres
    Create a Sollya polynomial from a Sage rational polynomial.
1071 209 storres
    """
1072 209 storres
    ## TODO: filter arguments.
1073 209 storres
    ## Precision. If no precision is given, use the current precision
1074 209 storres
    #  of Sollya.
1075 209 storres
    if precSa is None:
1076 209 storres
        precSa =  pobyso_get_prec_so_sa()
1077 209 storres
    #print "Precision:",  precSa
1078 209 storres
    RRR = RealField(precSa)
1079 209 storres
    ## Create a Sage polynomial in the "right" precision.
1080 209 storres
    P_RRR = RRR[polySa.variables()[0]]
1081 209 storres
    polyFloatSa = P_RRR(polySa)
1082 209 storres
    ## Make sure no precision is provided.
1083 209 storres
    return pobyso_float_poly_sa_so(polyFloatSa)
1084 209 storres
1085 209 storres
# End pobyso_rat_poly_sa_so
1086 209 storres
1087 52 storres
def pobyso_remez_canonical_sa_sa(func, \
1088 52 storres
                                 degree, \
1089 52 storres
                                 lowerBound, \
1090 52 storres
                                 upperBound, \
1091 52 storres
                                 weight = None, \
1092 52 storres
                                 quality = None):
1093 52 storres
    """
1094 52 storres
    All arguments are Sage/Python.
1095 52 storres
    The functions (func and weight) must be passed as expressions or strings.
1096 52 storres
    Otherwise the function fails.
1097 83 storres
    The return value is a Sage polynomial.
1098 52 storres
    """
1099 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
1100 83 storres
    # zorglub is "symbolic expression".
1101 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
1102 52 storres
                                 degree, \
1103 52 storres
                                 lowerBound, \
1104 52 storres
                                 upperBound, \
1105 85 storres
                                 weight, \
1106 85 storres
                                 quality)
1107 83 storres
    # String test
1108 52 storres
    if parent(func) == parent("string"):
1109 52 storres
        functionSa = eval(func)
1110 52 storres
    # Expression test.
1111 52 storres
    elif type(func) == type(zorglub):
1112 52 storres
        functionSa = func
1113 83 storres
    else:
1114 83 storres
        return None
1115 83 storres
    #
1116 52 storres
    maxPrecision = 0
1117 52 storres
    if polySo is None:
1118 52 storres
        return(None)
1119 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
1120 85 storres
    RRRRSa = RealField(maxPrecision)
1121 85 storres
    polynomialRingSa = RRRRSa[functionSa.variables()[0]]
1122 85 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRRSa)
1123 85 storres
    polySa = polynomial(expSa, polynomialRingSa)
1124 83 storres
    sollya_lib_clear_obj(polySo)
1125 52 storres
    return(polySa)
1126 85 storres
# End pobyso_remez_canonical_sa_sa
1127 52 storres
1128 38 storres
def pobyso_remez_canonical(func, \
1129 5 storres
                           degree, \
1130 5 storres
                           lowerBound, \
1131 5 storres
                           upperBound, \
1132 38 storres
                           weight = "1", \
1133 5 storres
                           quality = None):
1134 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
1135 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
1136 51 storres
                                        degree, \
1137 51 storres
                                        lowerBound, \
1138 51 storres
                                        upperBound, \
1139 51 storres
                                        weight, \
1140 51 storres
                                        quality))
1141 200 storres
# End pobyso_remez_canonical.
1142 200 storres
1143 38 storres
def pobyso_remez_canonical_sa_so(func, \
1144 38 storres
                                 degree, \
1145 38 storres
                                 lowerBound, \
1146 38 storres
                                 upperBound, \
1147 52 storres
                                 weight = None, \
1148 38 storres
                                 quality = None):
1149 38 storres
    """
1150 38 storres
    All arguments are Sage/Python.
1151 51 storres
    The functions (func and weight) must be passed as expressions or strings.
1152 51 storres
    Otherwise the function fails.
1153 38 storres
    The return value is a pointer to a Sollya function.
1154 38 storres
    """
1155 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
1156 83 storres
    # zorglub is "symbolic expression".
1157 85 storres
    currentVariableNameSa = None
1158 52 storres
    # The func argument can be of different types (string,
1159 52 storres
    # symbolic expression...)
1160 38 storres
    if parent(func) == parent("string"):
1161 85 storres
        localFuncSa = eval(func)
1162 85 storres
        if len(localFuncSa.variables()) > 0:
1163 85 storres
            currentVariableNameSa = localFuncSa.variables()[0]
1164 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
1165 159 storres
            functionSo = \
1166 159 storres
              sollya_lib_parse_string(localFuncSa._assume_str().replace('_SAGE_VAR_', ''))
1167 51 storres
    # Expression test.
1168 52 storres
    elif type(func) == type(zorglub):
1169 52 storres
        # Until we are able to translate Sage expressions into Sollya
1170 52 storres
        # expressions : parse the string version.
1171 85 storres
        if len(func.variables()) > 0:
1172 85 storres
            currentVariableNameSa = func.variables()[0]
1173 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
1174 159 storres
            functionSo = \
1175 159 storres
              sollya_lib_parse_string(func._assume_str().replace('_SAGE_VAR_', ''))
1176 38 storres
    else:
1177 38 storres
        return(None)
1178 85 storres
    if weight is None: # No weight given -> 1.
1179 52 storres
        weightSo = pobyso_constant_1_sa_so()
1180 85 storres
    elif parent(weight) == parent("string"): # Weight given as string: parse it.
1181 51 storres
        weightSo = sollya_lib_parse_string(func)
1182 85 storres
    elif type(weight) == type(zorglub): # Weight given as symbolice expression.
1183 159 storres
        functionSo = \
1184 159 storres
          sollya_lib_parse_string_sa_so(weight._assume_str().replace('_SAGE_VAR_', ''))
1185 51 storres
    else:
1186 51 storres
        return(None)
1187 5 storres
    degreeSo = pobyso_constant_from_int(degree)
1188 85 storres
    rangeSo = pobyso_bounds_to_range_sa_so(lowerBound, upperBound)
1189 38 storres
    if not quality is None:
1190 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
1191 52 storres
    else:
1192 52 storres
        qualitySo = None
1193 83 storres
1194 83 storres
    remezPolySo = sollya_lib_remez(functionSo, \
1195 83 storres
                                   degreeSo, \
1196 83 storres
                                   rangeSo, \
1197 83 storres
                                   weightSo, \
1198 83 storres
                                   qualitySo, \
1199 83 storres
                                   None)
1200 83 storres
    sollya_lib_clear_obj(functionSo)
1201 83 storres
    sollya_lib_clear_obj(degreeSo)
1202 83 storres
    sollya_lib_clear_obj(rangeSo)
1203 83 storres
    sollya_lib_clear_obj(weightSo)
1204 83 storres
    if not qualitySo is None:
1205 85 storres
        sollya_lib_clear_obj(qualitySo)
1206 83 storres
    return(remezPolySo)
1207 83 storres
# End pobyso_remez_canonical_sa_so
1208 83 storres
1209 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
1210 38 storres
                                 degreeSo, \
1211 38 storres
                                 rangeSo, \
1212 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
1213 38 storres
                                 qualitySo = None):
1214 38 storres
    """
1215 38 storres
    All arguments are pointers to Sollya objects.
1216 38 storres
    The return value is a pointer to a Sollya function.
1217 38 storres
    """
1218 38 storres
    if not sollya_lib_obj_is_function(funcSo):
1219 38 storres
        return(None)
1220 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
1221 200 storres
# End pobyso_remez_canonical_so_so.
1222 200 storres
1223 5 storres
def pobyso_set_canonical_off():
1224 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
1225 5 storres
1226 5 storres
def pobyso_set_canonical_on():
1227 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
1228 5 storres
1229 5 storres
def pobyso_set_prec(p):
1230 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
1231 85 storres
    pobyso_set_prec_sa_so(p)
1232 38 storres
1233 38 storres
def pobyso_set_prec_sa_so(p):
1234 5 storres
    a = c_int(p)
1235 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
1236 85 storres
    sollya_lib_set_prec(precSo, None)
1237 5 storres
1238 85 storres
def pobyso_set_prec_so_so(newPrecSo):
1239 85 storres
    sollya_lib_set_prec(newPrecSo, None)
1240 54 storres
1241 85 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo = None,\
1242 85 storres
                         accuracySo = None):
1243 58 storres
    """
1244 85 storres
    Computes the supnorm of the approximation error between the given
1245 85 storres
    polynomial and function.
1246 85 storres
    errorTypeSo defaults to "absolute".
1247 85 storres
    accuracySo defaults to 2^(-40).
1248 85 storres
    """
1249 85 storres
    if errorTypeSo is None:
1250 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1251 85 storres
        errorTypeIsNone = True
1252 85 storres
    else:
1253 85 storres
        errorTypeIsNone = False
1254 85 storres
    #
1255 85 storres
    if accuracySo is None:
1256 85 storres
        # Notice the **!
1257 85 storres
        accuracySo = pobyso_constant_sa_so(RR(2**(-40)))
1258 85 storres
        accuracyIsNone = True
1259 85 storres
    else:
1260 85 storres
        accuracyIsNone = False
1261 85 storres
    pobyso_autoprint(accuracySo)
1262 85 storres
    resultSo = \
1263 85 storres
        sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
1264 85 storres
                              accuracySo)
1265 85 storres
    if errorTypeIsNone:
1266 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1267 85 storres
    if accuracyIsNone:
1268 85 storres
        sollya_lib_clear_obj(accuracySo)
1269 85 storres
    return resultSo
1270 85 storres
# End pobyso_supnorm_so_so
1271 85 storres
1272 162 storres
def pobyso_taylor_expansion_no_change_var_so_so(functionSo,
1273 162 storres
                                                degreeSo,
1274 162 storres
                                                rangeSo,
1275 162 storres
                                                errorTypeSo=None,
1276 162 storres
                                                sollyaPrecSo=None):
1277 85 storres
    """
1278 162 storres
    Compute the Taylor expansion without the variable change
1279 162 storres
    x -> x-intervalCenter.
1280 58 storres
    """
1281 58 storres
    # No global change of the working precision.
1282 58 storres
    if not sollyaPrecSo is None:
1283 58 storres
        initialPrecSo = sollya_lib_get_prec(None)
1284 58 storres
        sollya_lib_set_prec(sollyaPrecSo)
1285 85 storres
    # Error type stuff: default to absolute.
1286 85 storres
    if errorTypeSo is None:
1287 85 storres
        errorTypeIsNone = True
1288 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1289 85 storres
    else:
1290 85 storres
        errorTypeIsNone = False
1291 162 storres
    intervalCenterSo = sollya_lib_mid(rangeSo, None)
1292 162 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo,
1293 162 storres
                                         intervalCenterSo,
1294 58 storres
                                         rangeSo, errorTypeSo, None)
1295 117 storres
    # taylorFormListSaSo is a Python list of Sollya objects references that
1296 117 storres
    # are copies of the elements of taylorFormSo.
1297 117 storres
    # pobyso_get_list_elements_so_so clears taylorFormSo.
1298 162 storres
    (taylorFormListSaSo, numElementsSa, isEndEllipticSa) = \
1299 58 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
1300 162 storres
    polySo = sollya_lib_copy_obj(taylorFormListSaSo[0])
1301 162 storres
    #print "Num elements:", numElementsSa
1302 162 storres
    sollya_lib_clear_obj(taylorFormSo)
1303 162 storres
    #polySo = taylorFormListSaSo[0]
1304 162 storres
    #errorRangeSo = sollya_lib_copy_obj(taylorFormListSaSo[2])
1305 162 storres
    errorRangeSo = taylorFormListSaSo[2]
1306 181 storres
    # No copy_obj needed here: a new objects are created.
1307 181 storres
    maxErrorSo    = sollya_lib_sup(errorRangeSo)
1308 181 storres
    minErrorSo    = sollya_lib_inf(errorRangeSo)
1309 181 storres
    absMaxErrorSo = sollya_lib_abs(maxErrorSo)
1310 181 storres
    absMinErrorSo = sollya_lib_abs(minErrorSo)
1311 181 storres
    sollya_lib_clear_obj(maxErrorSo)
1312 181 storres
    sollya_lib_clear_obj(minErrorSo)
1313 181 storres
    absMaxErrorSa = pobyso_get_constant_as_rn_so_sa(absMaxErrorSo)
1314 181 storres
    absMinErrorSa = pobyso_get_constant_as_rn_so_sa(absMinErrorSo)
1315 58 storres
    # If changed, reset the Sollya working precision.
1316 58 storres
    if not sollyaPrecSo is None:
1317 58 storres
        sollya_lib_set_prec(initialPrecSo)
1318 83 storres
        sollya_lib_clear_obj(initialPrecSo)
1319 85 storres
    if errorTypeIsNone:
1320 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1321 162 storres
    pobyso_clear_taylorform_sa_so(taylorFormListSaSo)
1322 181 storres
    if absMaxErrorSa > absMinErrorSa:
1323 181 storres
        sollya_lib_clear_obj(absMinErrorSo)
1324 181 storres
        return((polySo, intervalCenterSo, absMaxErrorSo))
1325 181 storres
    else:
1326 181 storres
        sollya_lib_clear_obj(absMaxErrorSo)
1327 181 storres
        return((polySo, intervalCenterSo, absMinErrorSo))
1328 162 storres
# end pobyso_taylor_expansion_no_change_var_so_so
1329 58 storres
1330 162 storres
def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, \
1331 162 storres
                                                  rangeSo, \
1332 162 storres
                                                  errorTypeSo=None, \
1333 162 storres
                                                  sollyaPrecSo=None):
1334 58 storres
    """
1335 162 storres
    Compute the Taylor expansion with the variable change
1336 162 storres
    x -> (x-intervalCenter) included.
1337 58 storres
    """
1338 56 storres
    # No global change of the working precision.
1339 56 storres
    if not sollyaPrecSo is None:
1340 56 storres
        initialPrecSo = sollya_lib_get_prec(None)
1341 56 storres
        sollya_lib_set_prec(sollyaPrecSo)
1342 162 storres
    #
1343 85 storres
    # Error type stuff: default to absolute.
1344 85 storres
    if errorTypeSo is None:
1345 85 storres
        errorTypeIsNone = True
1346 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1347 85 storres
    else:
1348 85 storres
        errorTypeIsNone = False
1349 162 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
1350 162 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
1351 162 storres
                                         intervalCenterSo, \
1352 56 storres
                                         rangeSo, errorTypeSo, None)
1353 116 storres
    # taylorFormListSaSo is a Python list of Sollya objects references that
1354 116 storres
    # are copies of the elements of taylorFormSo.
1355 116 storres
    # pobyso_get_list_elements_so_so clears taylorFormSo.
1356 162 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
1357 56 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
1358 162 storres
    polySo = taylorFormListSo[0]
1359 162 storres
    errorRangeSo = taylorFormListSo[2]
1360 181 storres
    maxErrorSo    = sollya_lib_sup(errorRangeSo)
1361 181 storres
    minErrorSo    = sollya_lib_inf(errorRangeSo)
1362 181 storres
    absMaxErrorSo = sollya_lib_abs(maxErrorSo)
1363 181 storres
    absMinErrorSo = sollya_lib_abs(minErrorSo)
1364 181 storres
    sollya_lib_clear_obj(maxErrorSo)
1365 181 storres
    sollya_lib_clear_obj(minErrorSo)
1366 181 storres
    absMaxErrorSa = pobyso_get_constant_as_rn_so_sa(absMaxErrorSo)
1367 181 storres
    absMinErrorSa = pobyso_get_constant_as_rn_so_sa(absMinErrorSo)
1368 162 storres
    changeVarExpSo = sollya_lib_build_function_sub(\
1369 162 storres
                       sollya_lib_build_function_free_variable(),\
1370 162 storres
                       sollya_lib_copy_obj(intervalCenterSo))
1371 181 storres
    polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo)
1372 181 storres
    sollya_lib_clear_obj(polySo)
1373 162 storres
    sollya_lib_clear_obj(changeVarExpSo)
1374 56 storres
    # If changed, reset the Sollya working precision.
1375 56 storres
    if not sollyaPrecSo is None:
1376 56 storres
        sollya_lib_set_prec(initialPrecSo)
1377 63 storres
        sollya_lib_clear_obj(initialPrecSo)
1378 85 storres
    if errorTypeIsNone:
1379 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1380 162 storres
    sollya_lib_clear_obj(taylorFormSo)
1381 162 storres
    # Do not clear maxErrorSo.
1382 181 storres
    if absMaxErrorSa > absMinErrorSa:
1383 181 storres
        sollya_lib_clear_obj(absMinErrorSo)
1384 181 storres
        return((polyVarChangedSo, intervalCenterSo, absMaxErrorSo))
1385 181 storres
    else:
1386 181 storres
        sollya_lib_clear_obj(absMaxErrorSo)
1387 181 storres
        return((polyVarChangedSo, intervalCenterSo, absMinErrorSo))
1388 162 storres
# end pobyso_taylor_expansion_with_change_var_so_so
1389 56 storres
1390 5 storres
def pobyso_taylor(function, degree, point):
1391 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
1392 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
1393 38 storres
1394 56 storres
def pobyso_taylor_so_so(functionSo, degreeSo, pointSo):
1395 56 storres
    return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
1396 5 storres
1397 85 storres
def pobyso_taylorform(function, degree, point = None,
1398 85 storres
                      interval = None, errorType=None):
1399 85 storres
    """ Legacy function. See pobyso_taylorform_sa_sa;"""
1400 38 storres
1401 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
1402 84 storres
                            degreeSa, \
1403 84 storres
                            pointSa, \
1404 84 storres
                            intervalSa=None, \
1405 84 storres
                            errorTypeSa=None, \
1406 84 storres
                            precisionSa=None):
1407 37 storres
    """
1408 85 storres
    Compute the Taylor form of 'degreeSa' for 'functionSa' at 'pointSa'
1409 85 storres
    for 'intervalSa' with 'errorTypeSa' (a string) using 'precisionSa'.
1410 37 storres
    point: must be a Real or a Real interval.
1411 37 storres
    return the Taylor form as an array
1412 83 storres
    TODO: take care of the interval and of the point when it is an interval;
1413 38 storres
          when errorType is not None;
1414 83 storres
          take care of the other elements of the Taylor form (coefficients
1415 83 storres
          errors and delta.
1416 37 storres
    """
1417 37 storres
    # Absolute as the default error.
1418 84 storres
    if errorTypeSa is None:
1419 37 storres
        errorTypeSo = sollya_lib_absolute()
1420 84 storres
    elif errorTypeSa == "relative":
1421 84 storres
        errorTypeSo = sollya_lib_relative()
1422 84 storres
    elif errortypeSa == "absolute":
1423 84 storres
        errorTypeSo = sollya_lib_absolute()
1424 37 storres
    else:
1425 84 storres
        # No clean up needed.
1426 84 storres
        return None
1427 84 storres
    # Global precision stuff
1428 84 storres
    precisionChangedSa = False
1429 84 storres
    currentSollyaPrecSo = pobyso_get_prec_so()
1430 84 storres
    currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo)
1431 84 storres
    if not precisionSa is None:
1432 84 storres
        if precisionSa > currentSollyaPrecSa:
1433 84 storres
            pobyso_set_prec_sa_so(precisionSa)
1434 84 storres
            precisionChangedSa = True
1435 84 storres
1436 85 storres
    if len(functionSa.variables()) > 0:
1437 85 storres
        varSa = functionSa.variables()[0]
1438 85 storres
        pobyso_name_free_variable_sa_so(str(varSa))
1439 84 storres
    # In any case (point or interval) the parent of pointSa has a precision
1440 84 storres
    # method.
1441 84 storres
    pointPrecSa = pointSa.parent().precision()
1442 84 storres
    if precisionSa > pointPrecSa:
1443 84 storres
        pointPrecSa = precisionSa
1444 84 storres
    # In any case (point or interval) pointSa has a base_ring() method.
1445 84 storres
    pointBaseRingString = str(pointSa.base_ring())
1446 84 storres
    if re.search('Interval', pointBaseRingString) is None: # Point
1447 84 storres
        pointSo = pobyso_constant_sa_so(pointSa, pointPrecSa)
1448 84 storres
    else: # Interval.
1449 84 storres
        pointSo = pobyso_interval_to_range_sa_so(pointSa, pointPrecSa)
1450 37 storres
    # Sollyafy the function.
1451 159 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str().replace('_SAGE_VAR_', ''))
1452 37 storres
    if sollya_lib_obj_is_error(functionSo):
1453 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
1454 37 storres
        return None
1455 37 storres
    # Sollyafy the degree
1456 84 storres
    degreeSo = sollya_lib_constant_from_int(int(degreeSa))
1457 37 storres
    # Sollyafy the point
1458 37 storres
    # Call Sollya
1459 83 storres
    taylorFormSo = \
1460 83 storres
        sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
1461 37 storres
                                         None)
1462 85 storres
    sollya_lib_clear_obj(functionSo)
1463 85 storres
    sollya_lib_clear_obj(degreeSo)
1464 85 storres
    sollya_lib_clear_obj(pointSo)
1465 85 storres
    sollya_lib_clear_obj(errorTypeSo)
1466 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
1467 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
1468 37 storres
    polySo = tfsAsList[0]
1469 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
1470 37 storres
    polyRealField = RealField(maxPrecision)
1471 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
1472 84 storres
    if precisionChangedSa:
1473 84 storres
        sollya_lib_set_prec(currentSollyaPrecSo)
1474 84 storres
        sollya_lib_clear_obj(currentSollyaPrecSo)
1475 37 storres
    polynomialRing = polyRealField[str(varSa)]
1476 37 storres
    polySa = polynomial(expSa, polynomialRing)
1477 37 storres
    taylorFormSa = [polySa]
1478 85 storres
    # Final clean-up.
1479 85 storres
    sollya_lib_clear_obj(taylorFormSo)
1480 51 storres
    return(taylorFormSa)
1481 51 storres
# End pobyso_taylor_form_sa_sa
1482 54 storres
1483 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
1484 54 storres
                            errorTypeSo=None):
1485 54 storres
    createdErrorType = False
1486 51 storres
    if errorTypeSo is None:
1487 51 storres
        errorTypeSo = sollya_lib_absolute()
1488 54 storres
        createdErrorType = True
1489 51 storres
    else:
1490 51 storres
        #TODO: deal with the other case.
1491 51 storres
        pass
1492 51 storres
    if intervalSo is None:
1493 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1494 54 storres
                                         errorTypeSo, None)
1495 51 storres
    else:
1496 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1497 54 storres
                                         intervalSo, errorTypeSo, None)
1498 54 storres
    if createdErrorType:
1499 54 storres
        sollya_lib_clear_obj(errorTypeSo)
1500 51 storres
    return(resultSo)
1501 51 storres
1502 37 storres
1503 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
1504 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
1505 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
1506 38 storres
1507 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
1508 37 storres
    """
1509 37 storres
    Return the string representation of a univariate polynomial with
1510 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
1511 37 storres
    Remember: Sage
1512 37 storres
    """
1513 37 storres
    polynomialRing = polySa.base_ring()
1514 37 storres
    # A very expensive solution:
1515 37 storres
    # -create a fake multivariate polynomial field with only one variable,
1516 37 storres
    #   specifying a negative lexicographical order;
1517 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
1518 37 storres
                                     polynomialRing.variable_name(), \
1519 37 storres
                                     1, order='neglex')
1520 37 storres
    # - convert the univariate argument polynomial into a multivariate
1521 37 storres
    #   version;
1522 37 storres
    p = mpolynomialRing(polySa)
1523 37 storres
    # - return the string representation of the converted form.
1524 37 storres
    # There is no simple str() method defined for p's class.
1525 37 storres
    return(p.__str__())
1526 5 storres
#
1527 5 storres
print pobyso_get_prec()
1528 5 storres
pobyso_set_prec(165)
1529 5 storres
print pobyso_get_prec()
1530 5 storres
a=100
1531 5 storres
print type(a)
1532 5 storres
id(a)
1533 5 storres
print "Max arity: ", pobyso_max_arity
1534 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
1535 56 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
1536 56 storres
print "...Pobyso check done"