Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 79

Historique | Voir | Annoter | Télécharger (37,12 ko)

1 5 storres
"""
2 5 storres
Actual functions to use in Sage
3 5 storres
ST 2012-11-13
4 5 storres

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

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

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

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

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