Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 158

Historique | Voir | Annoter | Télécharger (51,09 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 84 storres
    the digits 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 154 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa),
117 154 storres
                                       maxPrecSa)
118 154 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa),
119 154 storres
                                       maxPrecSa)
120 115 storres
    # From Sollya bounds to range.
121 84 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
122 84 storres
    # Back to original precision.
123 84 storres
    # Clean up
124 84 storres
    sollya_lib_clear_obj(lowerBoundSo)
125 84 storres
    sollya_lib_clear_obj(upperBoundSo)
126 154 storres
    return rangeSo
127 84 storres
# End pobyso_bounds_to_range_sa_so
128 84 storres
129 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
130 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
131 54 storres
132 85 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
133 54 storres
    """
134 85 storres
    Variable change in a function.
135 85 storres
    """
136 85 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
137 85 storres
# End pobyso_change_var_in_function_so_so
138 85 storres
139 85 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
140 85 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
141 85 storres
    return(resultSo)
142 85 storres
# End pobyso_chebyshevform_so_so.
143 85 storres
144 117 storres
def pobyso_clear_taylorform_sa_so(taylorFormSaSo):
145 117 storres
    """
146 117 storres
    This method is necessary to correctly clean up the memory from Taylor forms.
147 117 storres
    These are made of a Sollya object, a Sollya object list, a Sollya object.
148 117 storres
    For no clearly understood reason, sollya_lib_clear_object_list crashed
149 117 storres
    when applied to the object list.
150 117 storres
    Here, we decompose it into Sage list of Sollya objects references and we
151 117 storres
     clear them one by one.
152 117 storres
    """
153 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[0])
154 117 storres
    (coefficientsErrorsListSaSo, numElementsSa, isEndEllipticSa) = \
155 117 storres
        pobyso_get_list_elements_so_so(taylorFormSaSo[1])
156 117 storres
    for element in coefficientsErrorsListSaSo:
157 117 storres
        sollya_lib_clear_obj(element)
158 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[1])
159 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[2])
160 117 storres
# End pobyso_clear_taylorform_sa_so
161 117 storres
162 85 storres
def pobyso_cmp(rnArgSa, cteSo):
163 85 storres
    """
164 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
165 54 storres

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

502 118 storres
    INPUT:
503 118 storres
    - objectListSo: a Sollya list of Sollya objects.
504 118 storres

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

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