Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 179

Historique | Voir | Annoter | Télécharger (51,75 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 85 storres
def pobyso_absolute_so_so():
89 85 storres
    return(sollya_lib_absolute(None))
90 85 storres
91 5 storres
def pobyso_autoprint(arg):
92 5 storres
    sollya_lib_autoprint(arg,None)
93 5 storres
94 38 storres
def pobyso_autoprint_so_so(arg):
95 38 storres
    sollya_lib_autoprint(arg,None)
96 54 storres
97 84 storres
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa, \
98 84 storres
                                 precisionSa=None):
99 84 storres
    """
100 84 storres
    Return a Sollya range from to 2 RealField Sage elements.
101 84 storres
    The Sollya range element has a sufficient precision to hold all
102 162 storres
    the digits of the widest of the Sage bounds.
103 84 storres
    """
104 84 storres
    # Sanity check.
105 84 storres
    if rnLowerBoundSa > rnUpperBoundSa:
106 84 storres
        return None
107 115 storres
    # Precision stuff.
108 85 storres
    if precisionSa is None:
109 84 storres
        # Check for the largest precision.
110 84 storres
        lbPrecSa = rnLowerBoundSa.parent().precision()
111 84 storres
        ubPrecSa = rnLowerBoundSa.parent().precision()
112 84 storres
        maxPrecSa = max(lbPrecSa, ubPrecSa)
113 84 storres
    else:
114 84 storres
        maxPrecSa = precisionSa
115 115 storres
    # From Sage to Sollya bounds.
116 162 storres
#    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa),
117 162 storres
#                                       maxPrecSa)
118 162 storres
    lowerBoundSo = pobyso_constant_sa_so(rnLowerBoundSa,
119 162 storres
                                         maxPrecSa)
120 162 storres
    upperBoundSo = pobyso_constant_sa_so(rnUpperBoundSa,
121 154 storres
                                       maxPrecSa)
122 162 storres
123 115 storres
    # From Sollya bounds to range.
124 84 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
125 84 storres
    # Back to original precision.
126 84 storres
    # Clean up
127 84 storres
    sollya_lib_clear_obj(lowerBoundSo)
128 84 storres
    sollya_lib_clear_obj(upperBoundSo)
129 154 storres
    return rangeSo
130 84 storres
# End pobyso_bounds_to_range_sa_so
131 84 storres
132 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
133 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
134 54 storres
135 85 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
136 54 storres
    """
137 85 storres
    Variable change in a function.
138 85 storres
    """
139 85 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
140 85 storres
# End pobyso_change_var_in_function_so_so
141 85 storres
142 85 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
143 85 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
144 85 storres
    return(resultSo)
145 85 storres
# End pobyso_chebyshevform_so_so.
146 85 storres
147 117 storres
def pobyso_clear_taylorform_sa_so(taylorFormSaSo):
148 117 storres
    """
149 117 storres
    This method is necessary to correctly clean up the memory from Taylor forms.
150 117 storres
    These are made of a Sollya object, a Sollya object list, a Sollya object.
151 117 storres
    For no clearly understood reason, sollya_lib_clear_object_list crashed
152 117 storres
    when applied to the object list.
153 117 storres
    Here, we decompose it into Sage list of Sollya objects references and we
154 117 storres
     clear them one by one.
155 117 storres
    """
156 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[0])
157 117 storres
    (coefficientsErrorsListSaSo, numElementsSa, isEndEllipticSa) = \
158 117 storres
        pobyso_get_list_elements_so_so(taylorFormSaSo[1])
159 117 storres
    for element in coefficientsErrorsListSaSo:
160 117 storres
        sollya_lib_clear_obj(element)
161 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[1])
162 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[2])
163 117 storres
# End pobyso_clear_taylorform_sa_so
164 117 storres
165 85 storres
def pobyso_cmp(rnArgSa, cteSo):
166 85 storres
    """
167 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
168 54 storres

169 54 storres
    Get the value of the Sollya constant into a RealNumber and compare
170 54 storres
    using MPFR. Could be optimized by working directly with a mpfr_t
171 54 storres
    for the intermediate number.
172 54 storres
    """
173 115 storres
    # Get the precision of the Sollya constant to build a Sage RealNumber
174 115 storres
    # with enough precision.to hold it.
175 5 storres
    precisionOfCte = c_int(0)
176 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
177 85 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, cteSo)
178 5 storres
    #print "Precision of constant: ", precisionOfCte
179 5 storres
    RRRR = RealField(precisionOfCte.value)
180 85 storres
    rnLocalSa = RRRR(0)
181 85 storres
    sollya_lib_get_constant(get_rn_value(rnLocalSa), cteSo)
182 115 storres
    #
183 115 storres
    ## Compare the Sage RealNumber version of the Sollya constant with rnArg.
184 85 storres
    return(cmp_rn_value(rnArgSa, rnLocal))
185 83 storres
# End pobyso_smp
186 5 storres
187 54 storres
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
188 54 storres
                                                     upperBoundSa):
189 54 storres
    """
190 85 storres
    TODO: completely rework and test.
191 54 storres
    """
192 85 storres
    pobyso = pobyso_name_free_variable_sa_so(funcSa.variables()[0])
193 159 storres
    funcSo = pobyso_parse_string(funcSa._assume_str().replace('_SAGE_VAR_', ''))
194 54 storres
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
195 54 storres
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
196 83 storres
    # Sollya return the infnorm as an interval.
197 54 storres
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
198 54 storres
    # Get the top bound and compute the binade top limit.
199 54 storres
    fMaxUpperBoundSa = fMaxSa.upper()
200 54 storres
    binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
201 54 storres
    # Put up together the function to use to compute the lower bound.
202 54 storres
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
203 159 storres
                                    '-(' + f._assume_str().replace('_SAGE_VAR_', '') + ')')
204 54 storres
    pobyso_autoprint(funcAuxSo)
205 83 storres
    # Clear the Sollya range before a new call to infnorm and issue the call.
206 54 storres
    sollya_lib_clear_obj(infnormSo)
207 54 storres
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
208 54 storres
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
209 54 storres
    sollya_lib_clear_obj(infnormSo)
210 85 storres
    fMinLowerBoundSa = binadeTopLimitSa - fMinSa.lower()
211 54 storres
    # Compute the maximum of the precisions of the different bounds.
212 54 storres
    maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
213 54 storres
                     fMaxUpperBoundSa.parent().precision()])
214 54 storres
    # Create a RealIntervalField and create an interval with the "good" bounds.
215 54 storres
    RRRI = RealIntervalField(maxPrecSa)
216 54 storres
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
217 83 storres
    # Free the unneeded Sollya objects
218 54 storres
    sollya_lib_clear_obj(funcSo)
219 54 storres
    sollya_lib_clear_obj(funcAuxSo)
220 54 storres
    sollya_lib_clear_obj(rangeSo)
221 54 storres
    return(imageIntervalSa)
222 83 storres
# End pobyso_compute_pos_function_abs_val_bounds_sa_sa
223 54 storres
224 5 storres
def pobyso_constant(rnArg):
225 38 storres
    """ Legacy function. See pobyso_constant_sa_so. """
226 38 storres
    return(pobyso_constant_sa_so(rnArg))
227 5 storres
228 84 storres
def pobyso_constant_sa_so(rnArgSa, precisionSa=None):
229 52 storres
    """
230 115 storres
    Create a Sollya constant from a Sage RealNumber.
231 52 storres
    """
232 84 storres
    # Precision stuff
233 84 storres
    if precisionSa is None:
234 84 storres
        precisionSa = rnArgSa.parent().precision()
235 85 storres
    currentSollyaPrecisionSo = sollya_lib_get_prec()
236 85 storres
    currentSollyaPrecisionSa = \
237 85 storres
        pobyso_constant_from_int(currentSollyaPrecisionSo)
238 115 storres
    # Sollya constant creation takes place here.
239 84 storres
    if precisionSa > currentSollyaPrecisionSa:
240 84 storres
        pobyso_set_prec_sa_so(precisionSa)
241 84 storres
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
242 154 storres
        pobyso_set_prec_so_so(currentSollyaPrecisionSo)
243 84 storres
    else:
244 84 storres
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
245 85 storres
    sollya_lib_clear_obj(currentSollyaPrecisionSo)
246 154 storres
    return constantSo
247 115 storres
# End pobyso_constant_sa_so
248 115 storres
249 55 storres
def pobyso_constant_0_sa_so():
250 115 storres
    """
251 115 storres
    Obvious.
252 115 storres
    """
253 55 storres
    return(pobyso_constant_from_int_sa_so(0))
254 55 storres
255 5 storres
def pobyso_constant_1():
256 115 storres
    """
257 115 storres
    Obvious.
258 115 storres
    Legacy function. See pobyso_constant_so_so.
259 115 storres
    """
260 52 storres
    return(pobyso_constant_1_sa_so())
261 5 storres
262 52 storres
def pobyso_constant_1_sa_so():
263 115 storres
    """
264 115 storres
    Obvious.
265 115 storres
    """
266 38 storres
    return(pobyso_constant_from_int_sa_so(1))
267 38 storres
268 5 storres
def pobyso_constant_from_int(anInt):
269 38 storres
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
270 38 storres
    return(pobyso_constant_from_int_sa_so(anInt))
271 38 storres
272 38 storres
def pobyso_constant_from_int_sa_so(anInt):
273 115 storres
    """
274 115 storres
    Get a Sollya constant from a Sage int.
275 115 storres
    """
276 5 storres
    return(sollya_lib_constant_from_int(int(anInt)))
277 5 storres
278 84 storres
def pobyso_constant_from_int_so_sa(constSo):
279 84 storres
    """
280 117 storres
    Get a Sage int from a Sollya int constant.
281 115 storres
    Usefull for precision or powers in polynomials.
282 84 storres
    """
283 84 storres
    constSa = c_int(0)
284 84 storres
    sollya_lib_get_constant_as_int(byref(constSa), constSo)
285 84 storres
    return(constSa.value)
286 84 storres
# End pobyso_constant_from_int_so_sa
287 84 storres
288 155 storres
def pobyso_error_so():
289 155 storres
    return sollya_lib_error(None)
290 155 storres
# End pobyso_error().
291 155 storres
292 5 storres
def pobyso_function_type_as_string(funcType):
293 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
294 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
295 38 storres
296 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
297 38 storres
    """
298 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
299 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
300 38 storres
    """
301 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
302 5 storres
        return "abs"
303 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
304 5 storres
        return "arccos"
305 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
306 5 storres
        return "arccosh"
307 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
308 5 storres
        return "+"
309 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
310 5 storres
        return "arcsin"
311 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
312 5 storres
        return "arcsinh"
313 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
314 5 storres
        return "arctan"
315 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
316 5 storres
        return "arctanh"
317 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
318 5 storres
        return "ceil"
319 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
320 5 storres
        return "cte"
321 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
322 5 storres
        return "cos"
323 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
324 5 storres
        return "cosh"
325 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
326 5 storres
        return "/"
327 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
328 5 storres
        return "double"
329 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
330 5 storres
        return "doubleDouble"
331 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
332 5 storres
        return "doubleDxtended"
333 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
334 5 storres
        return "erf"
335 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
336 5 storres
        return "erfc"
337 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
338 5 storres
        return "exp"
339 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
340 5 storres
        return "expm1"
341 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
342 5 storres
        return "floor"
343 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
344 5 storres
        return "freeVariable"
345 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
346 5 storres
        return "halfPrecision"
347 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
348 5 storres
        return "libraryConstant"
349 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
350 5 storres
        return "libraryFunction"
351 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
352 5 storres
        return "log"
353 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
354 5 storres
        return "log10"
355 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
356 5 storres
        return "log1p"
357 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
358 5 storres
        return "log2"
359 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
360 5 storres
        return "*"
361 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
362 5 storres
        return "round"
363 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
364 5 storres
        return "__neg__"
365 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
366 5 storres
        return "pi"
367 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
368 5 storres
        return "^"
369 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
370 5 storres
        return "procedureFunction"
371 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
372 5 storres
        return "quad"
373 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
374 5 storres
        return "sin"
375 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
376 5 storres
        return "single"
377 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
378 5 storres
        return "sinh"
379 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
380 5 storres
        return "sqrt"
381 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
382 5 storres
        return "-"
383 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
384 5 storres
        return "tan"
385 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
386 5 storres
        return "tanh"
387 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
388 5 storres
        return "tripleDouble"
389 5 storres
    else:
390 5 storres
        return None
391 5 storres
392 85 storres
def pobyso_get_constant(rnArgSa, constSo):
393 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
394 85 storres
    return(pobyso_get_constant_so_sa(rnArgSa, constSo))
395 38 storres
396 84 storres
def pobyso_get_constant_so_sa(rnArgSa, constSo):
397 52 storres
    """
398 85 storres
    Set the value of rnArgSo to the value of constSo in MPFR_RNDN mode.
399 52 storres
    rnArg must already exist and belong to some RealField.
400 85 storres
    We assume that constSo points to a Sollya constant.
401 52 storres
    """
402 84 storres
    return(sollya_lib_get_constant(get_rn_value(rnArgSa), constSo))
403 5 storres
404 57 storres
def pobyso_get_constant_as_rn(ctExpSo):
405 83 storres
    """
406 83 storres
    Legacy function. See pobyso_get_constant_as_rn_so_sa.
407 83 storres
    """
408 57 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
409 38 storres
410 56 storres
def pobyso_get_constant_as_rn_so_sa(constExpSo):
411 83 storres
    """
412 83 storres
    Get a Sollya constant as a Sage "real number".
413 83 storres
    The precision of the floating-point number returned is that of the Sollya
414 83 storres
    constant.
415 83 storres
    """
416 57 storres
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
417 56 storres
    RRRR = RealField(precisionSa)
418 56 storres
    rnSa = RRRR(0)
419 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), constExpSo)
420 56 storres
    return(rnSa)
421 83 storres
# End pobyso_get_constant_as_rn_so_sa
422 38 storres
423 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
424 83 storres
    """
425 83 storres
    Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
426 83 storres
    """
427 38 storres
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
428 5 storres
429 56 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None):
430 83 storres
    """
431 83 storres
    Get a Sollya constant as a Sage "real number".
432 83 storres
    If no real field is specified, the precision of the floating-point number
433 85 storres
    returned is that of the Sollya constant.
434 83 storres
    Otherwise is is that of the real field. Hence rounding may happen.
435 83 storres
    """
436 56 storres
    if realFieldSa is None:
437 85 storres
        sollyaPrecSa = pobyso_get_prec_of_constant_so_sa(ctExpSo)
438 56 storres
        realFieldSa = RealField(sollyaPrecSa)
439 56 storres
    rnSa = realFieldSa(0)
440 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo)
441 56 storres
    return(rnSa)
442 83 storres
# End pobyso_get_constant_as_rn_with_rf_so_sa
443 38 storres
444 5 storres
def pobyso_get_free_variable_name():
445 83 storres
    """
446 83 storres
    Legacy function. See pobyso_get_free_variable_name_so_sa.
447 83 storres
    """
448 38 storres
    return(pobyso_get_free_variable_name_so_sa())
449 38 storres
450 38 storres
def pobyso_get_free_variable_name_so_sa():
451 5 storres
    return(sollya_lib_get_free_variable_name())
452 5 storres
453 38 storres
def pobyso_get_function_arity(expressionSo):
454 83 storres
    """
455 83 storres
    Legacy function. See pobyso_get_function_arity_so_sa.
456 83 storres
    """
457 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
458 38 storres
459 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
460 5 storres
    arity = c_int(0)
461 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
462 5 storres
    return(int(arity.value))
463 5 storres
464 38 storres
def pobyso_get_head_function(expressionSo):
465 83 storres
    """
466 83 storres
    Legacy function. See pobyso_get_head_function_so_sa.
467 83 storres
    """
468 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
469 38 storres
470 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
471 5 storres
    functionType = c_int(0)
472 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
473 5 storres
    return(int(functionType.value))
474 5 storres
475 56 storres
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
476 53 storres
    """
477 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
478 83 storres
    If no reaIntervalField is passed as an argument, the interval bounds are not
479 56 storres
    rounded: they are elements of RealIntervalField of the "right" precision
480 56 storres
    to hold all the digits.
481 53 storres
    """
482 53 storres
    prec = c_int(0)
483 56 storres
    if realIntervalFieldSa is None:
484 56 storres
        retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
485 56 storres
        if retval == 0:
486 56 storres
            return(None)
487 56 storres
        realIntervalFieldSa = RealIntervalField(prec.value)
488 56 storres
    intervalSa = realIntervalFieldSa(0,0)
489 53 storres
    retval = \
490 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
491 53 storres
                                           soRange)
492 53 storres
    if retval == 0:
493 53 storres
        return(None)
494 53 storres
    return(intervalSa)
495 56 storres
# End pobyso_get_interval_from_range_so_sa
496 56 storres
497 5 storres
def pobyso_get_list_elements(soObj):
498 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
499 38 storres
    return(pobyso_get_list_elements_so_so(soObj))
500 38 storres
501 117 storres
def pobyso_get_list_elements_so_so(objectListSo):
502 51 storres
    """
503 118 storres
    Get the Sollya list elements as a Sage/Python array of Sollya objects.
504 118 storres

505 118 storres
    INPUT:
506 118 storres
    - objectListSo: a Sollya list of Sollya objects.
507 118 storres

508 118 storres
    OUTPUT:
509 118 storres
    - a Sage/Python tuple made of:
510 118 storres
      - a Sage/Python list of Sollya objects,
511 118 storres
      - a Sage/Python int holding the number of elements,
512 118 storres
      - a Sage/Python int stating (!= 0) that the list is end-elliptic.
513 118 storres
    NOTE::
514 118 storres
        We recover the addresses of the Sollya object from the list of pointers
515 118 storres
        returned by sollya_lib_get_list_elements. The list itself is freed.
516 118 storres
    TODO::
517 118 storres
        Figure out what to do with numElements since the number of elements
518 118 storres
        can easily be recovered from the list itself.
519 118 storres
        Ditto for isEndElliptic.
520 51 storres
    """
521 5 storres
    listAddress = POINTER(c_longlong)()
522 5 storres
    numElements = c_int(0)
523 5 storres
    isEndElliptic = c_int(0)
524 117 storres
    listAsSageList = []
525 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
526 54 storres
                                          byref(numElements),\
527 54 storres
                                          byref(isEndElliptic),\
528 117 storres
                                          objectListSo)
529 5 storres
    if result == 0 :
530 5 storres
        return None
531 5 storres
    for i in xrange(0, numElements.value, 1):
532 118 storres
       #listAsSageList.append(sollya_lib_copy_obj(listAddress[i]))
533 118 storres
       listAsSageList.append(listAddress[i])
534 117 storres
       # Clear each of the elements returned by Sollya.
535 118 storres
       #sollya_lib_clear_obj(listAddress[i])
536 117 storres
    # Free the list itself.
537 117 storres
    sollya_lib_free(listAddress)
538 117 storres
    return(listAsSageList, numElements.value, isEndElliptic.value)
539 5 storres
540 38 storres
def pobyso_get_max_prec_of_exp(soExp):
541 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
542 38 storres
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
543 5 storres
544 85 storres
def pobyso_get_max_prec_of_exp_so_sa(expSo):
545 38 storres
    """
546 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
547 52 storres

548 52 storres
    Arguments:
549 52 storres
    soExp -- a Sollya expression pointer
550 52 storres
    Return value:
551 52 storres
    A Python integer
552 38 storres
    TODO:
553 38 storres
    - error management;
554 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
555 38 storres
    """
556 5 storres
    maxPrecision = 0
557 52 storres
    minConstPrec = 0
558 52 storres
    currentConstPrec = 0
559 85 storres
    operator = pobyso_get_head_function_so_sa(expSo)
560 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
561 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
562 85 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(expSo)
563 5 storres
        for i in xrange(arity):
564 5 storres
            maxPrecisionCandidate = \
565 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
566 5 storres
            if maxPrecisionCandidate > maxPrecision:
567 5 storres
                maxPrecision = maxPrecisionCandidate
568 5 storres
        return(maxPrecision)
569 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
570 85 storres
        #minConstPrec = pobyso_get_min_prec_of_constant_so_sa(expSo)
571 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
572 52 storres
        #print minConstPrec, " - ", currentConstPrec
573 85 storres
        return(pobyso_get_min_prec_of_constant_so_sa(expSo))
574 52 storres
575 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
576 5 storres
        return(0)
577 5 storres
    else:
578 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
579 5 storres
        return(0)
580 5 storres
581 85 storres
def pobyso_get_min_prec_of_constant_so_sa(constExpSo):
582 52 storres
    """
583 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
584 52 storres
    constant.
585 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
586 85 storres
    We assume that constExpSo is a point
587 52 storres
    """
588 85 storres
    constExpAsRnSa = pobyso_get_constant_as_rn_so_sa(constExpSo)
589 85 storres
    return(min_mpfr_size(get_rn_value(constExpAsRnSa)))
590 52 storres
591 85 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExpSo, realField = RR):
592 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
593 85 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, \
594 85 storres
                                                     realField = RR))
595 38 storres
596 85 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, realFieldSa = RR):
597 5 storres
    """
598 38 storres
    Get a Sage expression from a Sollya expression.
599 38 storres
    Currently only tested with polynomials with floating-point coefficients.
600 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
601 5 storres
    """
602 5 storres
    #pobyso_autoprint(sollyaExp)
603 85 storres
    operatorSa = pobyso_get_head_function_so_sa(sollyaExpSo)
604 83 storres
    sollyaLibFreeVariableName = sollya_lib_get_free_variable_name()
605 5 storres
    # Constants and the free variable are special cases.
606 5 storres
    # All other operator are dealt with in the same way.
607 85 storres
    if (operatorSa != SOLLYA_BASE_FUNC_CONSTANT) and \
608 85 storres
       (operatorSa != SOLLYA_BASE_FUNC_FREE_VARIABLE):
609 85 storres
        (aritySa, subexpressionsSa) = pobyso_get_subfunctions_so_sa(sollyaExpSo)
610 85 storres
        if aritySa == 1:
611 85 storres
            sageExpSa = eval(pobyso_function_type_as_string_so_sa(operatorSa) + \
612 85 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], \
613 85 storres
            realFieldSa) + ")")
614 85 storres
        elif aritySa == 2:
615 63 storres
            # We do not get through the preprocessor.
616 63 storres
            # The "^" operator is then a special case.
617 85 storres
            if operatorSa == SOLLYA_BASE_FUNC_POW:
618 85 storres
                operatorAsStringSa = "**"
619 5 storres
            else:
620 85 storres
                operatorAsStringSa = \
621 85 storres
                    pobyso_function_type_as_string_so_sa(operatorSa)
622 85 storres
            sageExpSa = \
623 85 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], realFieldSa)"\
624 85 storres
              + " " + operatorAsStringSa + " " + \
625 85 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[1], realFieldSa)")
626 63 storres
        # We do not know yet how to deal with arity >= 3
627 63 storres
        # (is there any in Sollya anyway?).
628 5 storres
        else:
629 85 storres
            sageExpSa = eval('None')
630 85 storres
        return(sageExpSa)
631 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_CONSTANT:
632 5 storres
        #print "This is a constant"
633 85 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExpSo, realFieldSa)
634 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_FREE_VARIABLE:
635 5 storres
        #print "This is free variable"
636 83 storres
        return(eval(sollyaLibFreeVariableName))
637 5 storres
    else:
638 5 storres
        print "Unexpected"
639 5 storres
        return eval('None')
640 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
641 73 storres
642 73 storres
def pobyso_get_poly_sa_so(polySo, realFieldSa=None):
643 83 storres
    """
644 83 storres
    Create a Sollya polynomial from a Sage polynomial.
645 83 storres
    """
646 73 storres
    pass
647 73 storres
# pobyso_get_poly_sa_so
648 73 storres
649 62 storres
def pobyso_get_poly_so_sa(polySo, realFieldSa=None):
650 57 storres
    """
651 57 storres
    Convert a Sollya polynomial into a Sage polynomial.
652 62 storres
    We assume that the polynomial is in canonical form.
653 124 storres
    If no realField is given, a RealField corresponding to the maximum
654 124 storres
    precision of the coefficients is internally computed.
655 124 storres
    The real field is not returned but can be easily retrieved from
656 124 storres
    the polynomial itself.
657 124 storres
    ALGORITHM:
658 57 storres
    - (optional) compute the RealField of the coefficients;
659 57 storres
    - convert the Sollya expression into a Sage expression;
660 57 storres
    - convert the Sage expression into a Sage polynomial
661 59 storres
    TODO: the canonical thing for the polynomial.
662 57 storres
    """
663 57 storres
    if realFieldSa is None:
664 57 storres
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
665 57 storres
        realFieldSa = RealField(expressionPrecSa)
666 63 storres
    #print "Sollya expression before...",
667 63 storres
    #pobyso_autoprint(polySo)
668 63 storres
669 57 storres
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \
670 57 storres
                                                             realFieldSa)
671 63 storres
    #print "...Sollya expression after.",
672 63 storres
    #pobyso_autoprint(polySo)
673 57 storres
    polyVariableSa = expressionSa.variables()[0]
674 59 storres
    polyRingSa = realFieldSa[str(polyVariableSa)]
675 81 storres
    #print polyRingSa
676 62 storres
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
677 62 storres
    polynomialSa = polyRingSa(expressionSa)
678 57 storres
    return(polynomialSa)
679 57 storres
# End pobyso_get_sage_poly_from_sollya_poly
680 57 storres
681 38 storres
def pobyso_get_subfunctions(expressionSo):
682 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
683 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
684 38 storres
685 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
686 38 storres
    """
687 38 storres
    Get the subfunctions of an expression.
688 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
689 55 storres
    S.T.: Could not figure out another way than that ugly list of declarations
690 83 storres
    to recover the addresses of the subfunctions.
691 83 storres
    We limit ourselves to arity 8 functions.
692 38 storres
    """
693 5 storres
    subf0 = c_int(0)
694 5 storres
    subf1 = c_int(0)
695 5 storres
    subf2 = c_int(0)
696 5 storres
    subf3 = c_int(0)
697 5 storres
    subf4 = c_int(0)
698 5 storres
    subf5 = c_int(0)
699 5 storres
    subf6 = c_int(0)
700 5 storres
    subf7 = c_int(0)
701 5 storres
    subf8 = c_int(0)
702 5 storres
    arity = c_int(0)
703 5 storres
    nullPtr = POINTER(c_int)()
704 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
705 83 storres
      byref(subf0), byref(subf1), byref(subf2), byref(subf3), \
706 83 storres
      byref(subf4), byref(subf5),\
707 83 storres
      byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
708 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
709 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
710 83 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), \
711 83 storres
#    byref(cast(subfunctions[3], POINTER(c_int))), \
712 83 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), \
713 83 storres
#    byref(cast(subfunctions[5], POINTER(c_int))), \
714 83 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), \
715 83 storres
#    byref(cast(subfunctions[7], POINTER(c_int))), \
716 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
717 83 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \
718 83 storres
                    subf8]
719 5 storres
    subs = []
720 5 storres
    if arity.value > pobyso_max_arity:
721 38 storres
        return(0,[])
722 5 storres
    for i in xrange(arity.value):
723 5 storres
        subs.append(int(subfunctions[i].value))
724 5 storres
        #print subs[i]
725 5 storres
    return(int(arity.value), subs)
726 5 storres
727 5 storres
def pobyso_get_prec():
728 38 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
729 38 storres
    return(pobyso_get_prec_so_sa())
730 38 storres
731 84 storres
def pobyso_get_prec_so():
732 84 storres
    """
733 84 storres
    Get the current default precision in Sollya.
734 84 storres
    The return value is a Sollya object.
735 84 storres
    Usefull when modifying the precision back and forth by avoiding
736 84 storres
    extra conversions.
737 84 storres
    """
738 84 storres
    return(sollya_lib_get_prec(None))
739 84 storres
740 38 storres
def pobyso_get_prec_so_sa():
741 38 storres
    """
742 38 storres
    Get the current default precision in Sollya.
743 38 storres
    The return value is Sage/Python int.
744 38 storres
    """
745 56 storres
    precSo = sollya_lib_get_prec(None)
746 56 storres
    precSa = c_int(0)
747 56 storres
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
748 56 storres
    sollya_lib_clear_obj(precSo)
749 154 storres
    return int(precSa.value)
750 83 storres
# End pobyso_get_prec_so_sa.
751 5 storres
752 38 storres
def pobyso_get_prec_of_constant(ctExpSo):
753 38 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
754 38 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
755 38 storres
756 56 storres
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
757 56 storres
    prec = c_int(0)
758 56 storres
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
759 56 storres
    if retc == 0:
760 56 storres
        return(None)
761 56 storres
    return(int(prec.value))
762 56 storres
763 53 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
764 5 storres
    prec = c_int(0)
765 53 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
766 56 storres
    if retc == 0:
767 56 storres
        return(None)
768 5 storres
    return(int(prec.value))
769 153 storres
# End pobyso_get_prec_of_range_so_sa()
770 5 storres
771 155 storres
def pobyso_guess_degree_sa_sa(functionSa, intervalSa, approxErrorSa,
772 155 storres
                              weightSa=None, degreeBoundSa=None):
773 155 storres
    """
774 155 storres
    Sa_sa variant of the solly_guessdegree function.
775 155 storres
    Return 0 if something goes wrong.
776 155 storres
    """
777 159 storres
    functionAsStringSa = functionSa._assume_str().replace('_SAGE_VAR_', '')
778 154 storres
    functionSo = pobyso_parse_string_sa_so(functionAsStringSa)
779 155 storres
    if pobyso_is_error_so_sa(functionSo):
780 155 storres
        sollya_lib_clear_obj(functionSo)
781 155 storres
        return 0
782 154 storres
    rangeSo = pobyso_interval_to_range_sa_so(intervalSa)
783 155 storres
    # The approximation error is expected to be a floating point number.
784 155 storres
    if pobyso_is_floating_point_number_sa_sa(approxErrorSa):
785 155 storres
        approxErrorSo = pobyso_constant_sa_so(approxErrorSa)
786 155 storres
    else:
787 155 storres
        approxErrorSo = pobyso_constant_sa_so(RR(approxErrorSa))
788 154 storres
    if not weightSa is None:
789 159 storres
        weightAsStringSa = weightSa._assume_str().replace('_SAGE_VAR_', '')
790 154 storres
        weightSo = pobyso_parse_string_sa_so(weightAsStringSa)
791 166 storres
        if pobyso_is_error_so_sa(weightSo):
792 155 storres
            sollya_lib_clear_obj(functionSo)
793 155 storres
            sollya_lib_clear_obj(rangeSo)
794 155 storres
            sollya_lib_clear_obj(approxErrorSo)
795 155 storres
            sollya_lib_clear_obj(weightSo)
796 155 storres
            return 0
797 154 storres
    else:
798 154 storres
        weightSo = None
799 154 storres
    if not degreeBoundSa is None:
800 154 storres
        degreeBoundSo = pobyso_constant_from_int_sa_so(degreeBoundSa)
801 154 storres
    else:
802 154 storres
        degreeBoundSo = None
803 154 storres
    guessedDegreeSa = pobyso_guess_degree_so_sa(functionSo,
804 162 storres
                                                rangeSo,
805 162 storres
                                                approxErrorSo,
806 162 storres
                                                weightSo,
807 162 storres
                                                degreeBoundSo)
808 154 storres
    sollya_lib_clear_obj(functionSo)
809 154 storres
    sollya_lib_clear_obj(rangeSo)
810 155 storres
    sollya_lib_clear_obj(approxErrorSo)
811 154 storres
    if not weightSo is None:
812 154 storres
        sollya_lib_clear_obj(weightSo)
813 154 storres
    if not degreeBoundSo is None:
814 154 storres
        sollya_lib_clear_obj(degreeBoundSo)
815 154 storres
    return guessedDegreeSa
816 154 storres
# End poyso_guess_degree_sa_sa
817 154 storres
818 153 storres
def pobyso_guess_degree_so_sa(functionSo, rangeSo, errorSo, weightSo=None, \
819 154 storres
                              degreeBoundSo=None):
820 154 storres
    """
821 154 storres
    Thin wrapper around the guessdegree function.
822 154 storres
    Nevertheless, some precision control stuff has been appended.
823 154 storres
    """
824 154 storres
    # Deal with Sollya internal precision issues: if it is too small,
825 154 storres
    # compared with the error, increases it to about twice -log2(error).
826 154 storres
    errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(errorSo)
827 154 storres
    log2ErrorSa = errorSa.log2()
828 154 storres
    if log2ErrorSa < 0:
829 154 storres
        neededPrecisionSa = int(2 * int(-log2ErrorSa) / 64) * 64
830 154 storres
    else:
831 154 storres
        neededPrecisionSa = int(2 * int(log2ErrorSa) / 64) * 64
832 154 storres
    #print "Needed precision:", neededPrecisionSa
833 154 storres
    currentPrecSa = pobyso_get_prec_so_sa()
834 154 storres
    if neededPrecisionSa > currentPrecSa:
835 154 storres
        currentPrecSo = pobyso_get_prec_so()
836 154 storres
        pobyso_set_prec_sa_so(neededPrecisionSa)
837 166 storres
    #print "Guessing degree..."
838 153 storres
    # weightSo and degreeBoundsSo are optional arguments.
839 162 storres
    # As declared, sollya_lib_guessdegree must take 5 arguments.
840 153 storres
    if weightSo is None:
841 162 storres
        degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, errorSo,
842 162 storres
                                               0, 0, None)
843 154 storres
    elif degreeBoundSo is None:
844 153 storres
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, \
845 162 storres
                                                errorSo, weightSo, 0, None)
846 153 storres
    else:
847 153 storres
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, errorSo, \
848 154 storres
                                                weightSo, degreeBoundSo, None)
849 166 storres
    #print "...degree guess done."
850 154 storres
    # Restore internal precision, if applicable.
851 154 storres
    if neededPrecisionSa > currentPrecSa:
852 154 storres
        pobyso_set_prec_so_so(currentPrecSo)
853 154 storres
        sollya_lib_clear_obj(currentPrecSo)
854 154 storres
    degreeIntervalSa = pobyso_range_to_interval_so_sa(degreeRangeSo)
855 154 storres
    sollya_lib_clear_obj(degreeRangeSo)
856 154 storres
    # When ok, both bounds match.
857 154 storres
    # When the degree bound is too low, the upper bound is the degree
858 154 storres
    # for which the error can be honored.
859 154 storres
    # When it really goes wrong, the upper bound is infinity.
860 154 storres
    if degreeIntervalSa.lower() == degreeIntervalSa.upper():
861 154 storres
        return int(degreeIntervalSa.lower())
862 154 storres
    else:
863 154 storres
        if degreeIntervalSa.upper().is_infinity():
864 154 storres
            return None
865 154 storres
        else:
866 154 storres
            return int(degreeIntervalSa.upper())
867 154 storres
    # End pobyso_guess_degree_so_sa
868 153 storres
869 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
870 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
871 54 storres
    return(None)
872 53 storres
873 84 storres
def pobyso_interval_to_range_sa_so(intervalSa, precisionSa=None):
874 84 storres
    if precisionSa is None:
875 84 storres
        precisionSa = intervalSa.parent().precision()
876 84 storres
    intervalSo = pobyso_bounds_to_range_sa_so(intervalSa.lower(),\
877 84 storres
                                              intervalSa.upper(),\
878 84 storres
                                              precisionSa)
879 84 storres
    return(intervalSo)
880 84 storres
# End pobyso_interval_to_range_sa_so
881 84 storres
882 155 storres
def pobyso_is_error_so_sa(objSo):
883 155 storres
    """
884 155 storres
    Thin wrapper around the sollya_lib_obj_is_error() function.
885 155 storres
    """
886 155 storres
    if sollya_lib_obj_is_error(objSo) != 0:
887 155 storres
        return True
888 155 storres
    else:
889 155 storres
        return False
890 155 storres
# End pobyso_is_error-so_sa
891 155 storres
892 155 storres
def pobyso_is_floating_point_number_sa_sa(numberSa):
893 155 storres
    """
894 155 storres
    Check whether a Sage number is floating point
895 155 storres
    """
896 155 storres
    return numberSa.parent().__class__ is RR.__class__
897 155 storres
898 37 storres
def pobyso_lib_init():
899 37 storres
    sollya_lib_init(None)
900 116 storres
901 116 storres
def pobyso_lib_close():
902 116 storres
    sollya_lib_close(None)
903 37 storres
904 85 storres
def pobyso_name_free_variable(freeVariableNameSa):
905 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
906 85 storres
    pobyso_name_free_variable_sa_so(freeVariableNameSa)
907 38 storres
908 85 storres
def pobyso_name_free_variable_sa_so(freeVariableNameSa):
909 83 storres
    """
910 83 storres
    Set the free variable name in Sollya from a Sage string.
911 83 storres
    """
912 85 storres
    sollya_lib_name_free_variable(freeVariableNameSa)
913 37 storres
914 5 storres
def pobyso_parse_string(string):
915 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
916 38 storres
    return(pobyso_parse_string_sa_so(string))
917 38 storres
918 38 storres
def pobyso_parse_string_sa_so(string):
919 83 storres
    """
920 155 storres
    Get the Sollya expression computed from a Sage string or
921 155 storres
    a Sollya error object if parsing failed.
922 83 storres
    """
923 5 storres
    return(sollya_lib_parse_string(string))
924 5 storres
925 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
926 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
927 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
928 38 storres
929 5 storres
930 85 storres
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalFieldSa = None):
931 83 storres
    """
932 83 storres
    Get a Sage interval from a Sollya range.
933 83 storres
    If no realIntervalField is given as a parameter, the Sage interval
934 83 storres
    precision is that of the Sollya range.
935 85 storres
    Otherwise, the precision is that of the realIntervalField. In this case
936 85 storres
    rounding may happen.
937 83 storres
    """
938 85 storres
    if realIntervalFieldSa is None:
939 56 storres
        precSa = pobyso_get_prec_of_range_so_sa(rangeSo)
940 85 storres
        realIntervalFieldSa = RealIntervalField(precSa)
941 56 storres
    intervalSa = \
942 85 storres
        pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalFieldSa)
943 56 storres
    return(intervalSa)
944 56 storres
945 52 storres
def pobyso_remez_canonical_sa_sa(func, \
946 52 storres
                                 degree, \
947 52 storres
                                 lowerBound, \
948 52 storres
                                 upperBound, \
949 52 storres
                                 weight = None, \
950 52 storres
                                 quality = None):
951 52 storres
    """
952 52 storres
    All arguments are Sage/Python.
953 52 storres
    The functions (func and weight) must be passed as expressions or strings.
954 52 storres
    Otherwise the function fails.
955 83 storres
    The return value is a Sage polynomial.
956 52 storres
    """
957 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
958 83 storres
    # zorglub is "symbolic expression".
959 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
960 52 storres
                                 degree, \
961 52 storres
                                 lowerBound, \
962 52 storres
                                 upperBound, \
963 85 storres
                                 weight, \
964 85 storres
                                 quality)
965 83 storres
    # String test
966 52 storres
    if parent(func) == parent("string"):
967 52 storres
        functionSa = eval(func)
968 52 storres
    # Expression test.
969 52 storres
    elif type(func) == type(zorglub):
970 52 storres
        functionSa = func
971 83 storres
    else:
972 83 storres
        return None
973 83 storres
    #
974 52 storres
    maxPrecision = 0
975 52 storres
    if polySo is None:
976 52 storres
        return(None)
977 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
978 85 storres
    RRRRSa = RealField(maxPrecision)
979 85 storres
    polynomialRingSa = RRRRSa[functionSa.variables()[0]]
980 85 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRRSa)
981 85 storres
    polySa = polynomial(expSa, polynomialRingSa)
982 83 storres
    sollya_lib_clear_obj(polySo)
983 52 storres
    return(polySa)
984 85 storres
# End pobyso_remez_canonical_sa_sa
985 52 storres
986 38 storres
def pobyso_remez_canonical(func, \
987 5 storres
                           degree, \
988 5 storres
                           lowerBound, \
989 5 storres
                           upperBound, \
990 38 storres
                           weight = "1", \
991 5 storres
                           quality = None):
992 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
993 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
994 51 storres
                                        degree, \
995 51 storres
                                        lowerBound, \
996 51 storres
                                        upperBound, \
997 51 storres
                                        weight, \
998 51 storres
                                        quality))
999 38 storres
def pobyso_remez_canonical_sa_so(func, \
1000 38 storres
                                 degree, \
1001 38 storres
                                 lowerBound, \
1002 38 storres
                                 upperBound, \
1003 52 storres
                                 weight = None, \
1004 38 storres
                                 quality = None):
1005 38 storres
    """
1006 38 storres
    All arguments are Sage/Python.
1007 51 storres
    The functions (func and weight) must be passed as expressions or strings.
1008 51 storres
    Otherwise the function fails.
1009 38 storres
    The return value is a pointer to a Sollya function.
1010 38 storres
    """
1011 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
1012 83 storres
    # zorglub is "symbolic expression".
1013 85 storres
    currentVariableNameSa = None
1014 52 storres
    # The func argument can be of different types (string,
1015 52 storres
    # symbolic expression...)
1016 38 storres
    if parent(func) == parent("string"):
1017 85 storres
        localFuncSa = eval(func)
1018 85 storres
        if len(localFuncSa.variables()) > 0:
1019 85 storres
            currentVariableNameSa = localFuncSa.variables()[0]
1020 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
1021 159 storres
            functionSo = \
1022 159 storres
              sollya_lib_parse_string(localFuncSa._assume_str().replace('_SAGE_VAR_', ''))
1023 51 storres
    # Expression test.
1024 52 storres
    elif type(func) == type(zorglub):
1025 52 storres
        # Until we are able to translate Sage expressions into Sollya
1026 52 storres
        # expressions : parse the string version.
1027 85 storres
        if len(func.variables()) > 0:
1028 85 storres
            currentVariableNameSa = func.variables()[0]
1029 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
1030 159 storres
            functionSo = \
1031 159 storres
              sollya_lib_parse_string(func._assume_str().replace('_SAGE_VAR_', ''))
1032 38 storres
    else:
1033 38 storres
        return(None)
1034 85 storres
    if weight is None: # No weight given -> 1.
1035 52 storres
        weightSo = pobyso_constant_1_sa_so()
1036 85 storres
    elif parent(weight) == parent("string"): # Weight given as string: parse it.
1037 51 storres
        weightSo = sollya_lib_parse_string(func)
1038 85 storres
    elif type(weight) == type(zorglub): # Weight given as symbolice expression.
1039 159 storres
        functionSo = \
1040 159 storres
          sollya_lib_parse_string_sa_so(weight._assume_str().replace('_SAGE_VAR_', ''))
1041 51 storres
    else:
1042 51 storres
        return(None)
1043 5 storres
    degreeSo = pobyso_constant_from_int(degree)
1044 85 storres
    rangeSo = pobyso_bounds_to_range_sa_so(lowerBound, upperBound)
1045 38 storres
    if not quality is None:
1046 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
1047 52 storres
    else:
1048 52 storres
        qualitySo = None
1049 83 storres
1050 83 storres
    remezPolySo = sollya_lib_remez(functionSo, \
1051 83 storres
                                   degreeSo, \
1052 83 storres
                                   rangeSo, \
1053 83 storres
                                   weightSo, \
1054 83 storres
                                   qualitySo, \
1055 83 storres
                                   None)
1056 83 storres
    sollya_lib_clear_obj(functionSo)
1057 83 storres
    sollya_lib_clear_obj(degreeSo)
1058 83 storres
    sollya_lib_clear_obj(rangeSo)
1059 83 storres
    sollya_lib_clear_obj(weightSo)
1060 83 storres
    if not qualitySo is None:
1061 85 storres
        sollya_lib_clear_obj(qualitySo)
1062 83 storres
    return(remezPolySo)
1063 83 storres
# End pobyso_remez_canonical_sa_so
1064 83 storres
1065 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
1066 38 storres
                                 degreeSo, \
1067 38 storres
                                 rangeSo, \
1068 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
1069 38 storres
                                 qualitySo = None):
1070 38 storres
    """
1071 38 storres
    All arguments are pointers to Sollya objects.
1072 38 storres
    The return value is a pointer to a Sollya function.
1073 38 storres
    """
1074 38 storres
    if not sollya_lib_obj_is_function(funcSo):
1075 38 storres
        return(None)
1076 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
1077 38 storres
1078 5 storres
def pobyso_set_canonical_off():
1079 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
1080 5 storres
1081 5 storres
def pobyso_set_canonical_on():
1082 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
1083 5 storres
1084 5 storres
def pobyso_set_prec(p):
1085 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
1086 85 storres
    pobyso_set_prec_sa_so(p)
1087 38 storres
1088 38 storres
def pobyso_set_prec_sa_so(p):
1089 5 storres
    a = c_int(p)
1090 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
1091 85 storres
    sollya_lib_set_prec(precSo, None)
1092 5 storres
1093 85 storres
def pobyso_set_prec_so_so(newPrecSo):
1094 85 storres
    sollya_lib_set_prec(newPrecSo, None)
1095 54 storres
1096 85 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo = None,\
1097 85 storres
                         accuracySo = None):
1098 58 storres
    """
1099 85 storres
    Computes the supnorm of the approximation error between the given
1100 85 storres
    polynomial and function.
1101 85 storres
    errorTypeSo defaults to "absolute".
1102 85 storres
    accuracySo defaults to 2^(-40).
1103 85 storres
    """
1104 85 storres
    if errorTypeSo is None:
1105 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1106 85 storres
        errorTypeIsNone = True
1107 85 storres
    else:
1108 85 storres
        errorTypeIsNone = False
1109 85 storres
    #
1110 85 storres
    if accuracySo is None:
1111 85 storres
        # Notice the **!
1112 85 storres
        accuracySo = pobyso_constant_sa_so(RR(2**(-40)))
1113 85 storres
        accuracyIsNone = True
1114 85 storres
    else:
1115 85 storres
        accuracyIsNone = False
1116 85 storres
    pobyso_autoprint(accuracySo)
1117 85 storres
    resultSo = \
1118 85 storres
        sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
1119 85 storres
                              accuracySo)
1120 85 storres
    if errorTypeIsNone:
1121 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1122 85 storres
    if accuracyIsNone:
1123 85 storres
        sollya_lib_clear_obj(accuracySo)
1124 85 storres
    return resultSo
1125 85 storres
# End pobyso_supnorm_so_so
1126 85 storres
1127 162 storres
def pobyso_taylor_expansion_no_change_var_so_so(functionSo,
1128 162 storres
                                                degreeSo,
1129 162 storres
                                                rangeSo,
1130 162 storres
                                                errorTypeSo=None,
1131 162 storres
                                                sollyaPrecSo=None):
1132 85 storres
    """
1133 162 storres
    Compute the Taylor expansion without the variable change
1134 162 storres
    x -> x-intervalCenter.
1135 58 storres
    """
1136 58 storres
    # No global change of the working precision.
1137 58 storres
    if not sollyaPrecSo is None:
1138 58 storres
        initialPrecSo = sollya_lib_get_prec(None)
1139 58 storres
        sollya_lib_set_prec(sollyaPrecSo)
1140 85 storres
    # Error type stuff: default to absolute.
1141 85 storres
    if errorTypeSo is None:
1142 85 storres
        errorTypeIsNone = True
1143 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1144 85 storres
    else:
1145 85 storres
        errorTypeIsNone = False
1146 162 storres
    intervalCenterSo = sollya_lib_mid(rangeSo, None)
1147 162 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo,
1148 162 storres
                                         intervalCenterSo,
1149 58 storres
                                         rangeSo, errorTypeSo, None)
1150 117 storres
    # taylorFormListSaSo is a Python list of Sollya objects references that
1151 117 storres
    # are copies of the elements of taylorFormSo.
1152 117 storres
    # pobyso_get_list_elements_so_so clears taylorFormSo.
1153 162 storres
    (taylorFormListSaSo, numElementsSa, isEndEllipticSa) = \
1154 58 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
1155 162 storres
    polySo = sollya_lib_copy_obj(taylorFormListSaSo[0])
1156 162 storres
    #print "Num elements:", numElementsSa
1157 162 storres
    sollya_lib_clear_obj(taylorFormSo)
1158 162 storres
    #polySo = taylorFormListSaSo[0]
1159 162 storres
    #errorRangeSo = sollya_lib_copy_obj(taylorFormListSaSo[2])
1160 162 storres
    errorRangeSo = taylorFormListSaSo[2]
1161 162 storres
    # No copy_obj needed here: a new object is created.
1162 58 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
1163 58 storres
    # If changed, reset the Sollya working precision.
1164 58 storres
    if not sollyaPrecSo is None:
1165 58 storres
        sollya_lib_set_prec(initialPrecSo)
1166 83 storres
        sollya_lib_clear_obj(initialPrecSo)
1167 85 storres
    if errorTypeIsNone:
1168 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1169 162 storres
    pobyso_clear_taylorform_sa_so(taylorFormListSaSo)
1170 162 storres
    return((polySo, intervalCenterSo, maxErrorSo))
1171 162 storres
# end pobyso_taylor_expansion_no_change_var_so_so
1172 58 storres
1173 162 storres
def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, \
1174 162 storres
                                                  rangeSo, \
1175 162 storres
                                                  errorTypeSo=None, \
1176 162 storres
                                                  sollyaPrecSo=None):
1177 58 storres
    """
1178 162 storres
    Compute the Taylor expansion with the variable change
1179 162 storres
    x -> (x-intervalCenter) included.
1180 58 storres
    """
1181 56 storres
    # No global change of the working precision.
1182 56 storres
    if not sollyaPrecSo is None:
1183 56 storres
        initialPrecSo = sollya_lib_get_prec(None)
1184 56 storres
        sollya_lib_set_prec(sollyaPrecSo)
1185 162 storres
    #
1186 85 storres
    # Error type stuff: default to absolute.
1187 85 storres
    if errorTypeSo is None:
1188 85 storres
        errorTypeIsNone = True
1189 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1190 85 storres
    else:
1191 85 storres
        errorTypeIsNone = False
1192 162 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
1193 162 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
1194 162 storres
                                         intervalCenterSo, \
1195 56 storres
                                         rangeSo, errorTypeSo, None)
1196 116 storres
    # taylorFormListSaSo is a Python list of Sollya objects references that
1197 116 storres
    # are copies of the elements of taylorFormSo.
1198 116 storres
    # pobyso_get_list_elements_so_so clears taylorFormSo.
1199 162 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
1200 56 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
1201 162 storres
    polySo = taylorFormListSo[0]
1202 162 storres
    errorRangeSo = taylorFormListSo[2]
1203 56 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
1204 162 storres
    changeVarExpSo = sollya_lib_build_function_sub(\
1205 162 storres
                       sollya_lib_build_function_free_variable(),\
1206 162 storres
                       sollya_lib_copy_obj(intervalCenterSo))
1207 162 storres
    polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo)
1208 162 storres
    sollya_lib_clear_obj(changeVarExpSo)
1209 56 storres
    # If changed, reset the Sollya working precision.
1210 56 storres
    if not sollyaPrecSo is None:
1211 56 storres
        sollya_lib_set_prec(initialPrecSo)
1212 63 storres
        sollya_lib_clear_obj(initialPrecSo)
1213 85 storres
    if errorTypeIsNone:
1214 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1215 162 storres
    sollya_lib_clear_obj(taylorFormSo)
1216 162 storres
    # Do not clear maxErrorSo.
1217 162 storres
    return((polyVarChangedSo, intervalCenterSo, maxErrorSo))
1218 162 storres
# end pobyso_taylor_expansion_with_change_var_so_so
1219 56 storres
1220 5 storres
def pobyso_taylor(function, degree, point):
1221 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
1222 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
1223 38 storres
1224 56 storres
def pobyso_taylor_so_so(functionSo, degreeSo, pointSo):
1225 56 storres
    return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
1226 5 storres
1227 85 storres
def pobyso_taylorform(function, degree, point = None,
1228 85 storres
                      interval = None, errorType=None):
1229 85 storres
    """ Legacy function. See pobyso_taylorform_sa_sa;"""
1230 38 storres
1231 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
1232 84 storres
                            degreeSa, \
1233 84 storres
                            pointSa, \
1234 84 storres
                            intervalSa=None, \
1235 84 storres
                            errorTypeSa=None, \
1236 84 storres
                            precisionSa=None):
1237 37 storres
    """
1238 85 storres
    Compute the Taylor form of 'degreeSa' for 'functionSa' at 'pointSa'
1239 85 storres
    for 'intervalSa' with 'errorTypeSa' (a string) using 'precisionSa'.
1240 37 storres
    point: must be a Real or a Real interval.
1241 37 storres
    return the Taylor form as an array
1242 83 storres
    TODO: take care of the interval and of the point when it is an interval;
1243 38 storres
          when errorType is not None;
1244 83 storres
          take care of the other elements of the Taylor form (coefficients
1245 83 storres
          errors and delta.
1246 37 storres
    """
1247 37 storres
    # Absolute as the default error.
1248 84 storres
    if errorTypeSa is None:
1249 37 storres
        errorTypeSo = sollya_lib_absolute()
1250 84 storres
    elif errorTypeSa == "relative":
1251 84 storres
        errorTypeSo = sollya_lib_relative()
1252 84 storres
    elif errortypeSa == "absolute":
1253 84 storres
        errorTypeSo = sollya_lib_absolute()
1254 37 storres
    else:
1255 84 storres
        # No clean up needed.
1256 84 storres
        return None
1257 84 storres
    # Global precision stuff
1258 84 storres
    precisionChangedSa = False
1259 84 storres
    currentSollyaPrecSo = pobyso_get_prec_so()
1260 84 storres
    currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo)
1261 84 storres
    if not precisionSa is None:
1262 84 storres
        if precisionSa > currentSollyaPrecSa:
1263 84 storres
            pobyso_set_prec_sa_so(precisionSa)
1264 84 storres
            precisionChangedSa = True
1265 84 storres
1266 85 storres
    if len(functionSa.variables()) > 0:
1267 85 storres
        varSa = functionSa.variables()[0]
1268 85 storres
        pobyso_name_free_variable_sa_so(str(varSa))
1269 84 storres
    # In any case (point or interval) the parent of pointSa has a precision
1270 84 storres
    # method.
1271 84 storres
    pointPrecSa = pointSa.parent().precision()
1272 84 storres
    if precisionSa > pointPrecSa:
1273 84 storres
        pointPrecSa = precisionSa
1274 84 storres
    # In any case (point or interval) pointSa has a base_ring() method.
1275 84 storres
    pointBaseRingString = str(pointSa.base_ring())
1276 84 storres
    if re.search('Interval', pointBaseRingString) is None: # Point
1277 84 storres
        pointSo = pobyso_constant_sa_so(pointSa, pointPrecSa)
1278 84 storres
    else: # Interval.
1279 84 storres
        pointSo = pobyso_interval_to_range_sa_so(pointSa, pointPrecSa)
1280 37 storres
    # Sollyafy the function.
1281 159 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str().replace('_SAGE_VAR_', ''))
1282 37 storres
    if sollya_lib_obj_is_error(functionSo):
1283 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
1284 37 storres
        return None
1285 37 storres
    # Sollyafy the degree
1286 84 storres
    degreeSo = sollya_lib_constant_from_int(int(degreeSa))
1287 37 storres
    # Sollyafy the point
1288 37 storres
    # Call Sollya
1289 83 storres
    taylorFormSo = \
1290 83 storres
        sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
1291 37 storres
                                         None)
1292 85 storres
    sollya_lib_clear_obj(functionSo)
1293 85 storres
    sollya_lib_clear_obj(degreeSo)
1294 85 storres
    sollya_lib_clear_obj(pointSo)
1295 85 storres
    sollya_lib_clear_obj(errorTypeSo)
1296 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
1297 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
1298 37 storres
    polySo = tfsAsList[0]
1299 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
1300 37 storres
    polyRealField = RealField(maxPrecision)
1301 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
1302 84 storres
    if precisionChangedSa:
1303 84 storres
        sollya_lib_set_prec(currentSollyaPrecSo)
1304 84 storres
        sollya_lib_clear_obj(currentSollyaPrecSo)
1305 37 storres
    polynomialRing = polyRealField[str(varSa)]
1306 37 storres
    polySa = polynomial(expSa, polynomialRing)
1307 37 storres
    taylorFormSa = [polySa]
1308 85 storres
    # Final clean-up.
1309 85 storres
    sollya_lib_clear_obj(taylorFormSo)
1310 51 storres
    return(taylorFormSa)
1311 51 storres
# End pobyso_taylor_form_sa_sa
1312 54 storres
1313 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
1314 54 storres
                            errorTypeSo=None):
1315 54 storres
    createdErrorType = False
1316 51 storres
    if errorTypeSo is None:
1317 51 storres
        errorTypeSo = sollya_lib_absolute()
1318 54 storres
        createdErrorType = True
1319 51 storres
    else:
1320 51 storres
        #TODO: deal with the other case.
1321 51 storres
        pass
1322 51 storres
    if intervalSo is None:
1323 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1324 54 storres
                                         errorTypeSo, None)
1325 51 storres
    else:
1326 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1327 54 storres
                                         intervalSo, errorTypeSo, None)
1328 54 storres
    if createdErrorType:
1329 54 storres
        sollya_lib_clear_obj(errorTypeSo)
1330 51 storres
    return(resultSo)
1331 51 storres
1332 37 storres
1333 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
1334 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
1335 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
1336 38 storres
1337 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
1338 37 storres
    """
1339 37 storres
    Return the string representation of a univariate polynomial with
1340 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
1341 37 storres
    Remember: Sage
1342 37 storres
    """
1343 37 storres
    polynomialRing = polySa.base_ring()
1344 37 storres
    # A very expensive solution:
1345 37 storres
    # -create a fake multivariate polynomial field with only one variable,
1346 37 storres
    #   specifying a negative lexicographical order;
1347 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
1348 37 storres
                                     polynomialRing.variable_name(), \
1349 37 storres
                                     1, order='neglex')
1350 37 storres
    # - convert the univariate argument polynomial into a multivariate
1351 37 storres
    #   version;
1352 37 storres
    p = mpolynomialRing(polySa)
1353 37 storres
    # - return the string representation of the converted form.
1354 37 storres
    # There is no simple str() method defined for p's class.
1355 37 storres
    return(p.__str__())
1356 5 storres
#
1357 5 storres
print pobyso_get_prec()
1358 5 storres
pobyso_set_prec(165)
1359 5 storres
print pobyso_get_prec()
1360 5 storres
a=100
1361 5 storres
print type(a)
1362 5 storres
id(a)
1363 5 storres
print "Max arity: ", pobyso_max_arity
1364 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
1365 56 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
1366 56 storres
print "...Pobyso check done"