Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 116

Historique | Voir | Annoter | Télécharger (44,9 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 84 storres
    sollyaCurrentPrecSo = pobyso_get_prec_so()
116 84 storres
    sollyaCurrentPrecSa = pobyso_constant_from_int_so_sa(sollyaCurrentPrecSo)
117 84 storres
    # Change the current Sollya precision only if necessary.
118 84 storres
    if maxPrecSa > sollyaCurrentPrecSa:
119 84 storres
        pobyso_set_prec_sa_so(maxPrecSa)
120 115 storres
    # From Sage to Sollya bounds.
121 84 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
122 84 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
123 115 storres
    # From Sollya bounds to range.
124 84 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
125 84 storres
    # Back to original precision.
126 84 storres
    if maxPrecSa > sollyaCurrentPrecSa:
127 84 storres
        sollya_lib_set_prec(sollyaCurrentPrecSo)
128 84 storres
    # Clean up
129 84 storres
    sollya_lib_clear_obj(sollyaCurrentPrecSo)
130 84 storres
    sollya_lib_clear_obj(lowerBoundSo)
131 84 storres
    sollya_lib_clear_obj(upperBoundSo)
132 84 storres
    return(rangeSo)
133 84 storres
# End pobyso_bounds_to_range_sa_so
134 84 storres
135 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
136 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
137 54 storres
138 85 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
139 54 storres
    """
140 85 storres
    Variable change in a function.
141 85 storres
    """
142 85 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
143 85 storres
# End pobyso_change_var_in_function_so_so
144 85 storres
145 85 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
146 85 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
147 85 storres
    return(resultSo)
148 85 storres
# End pobyso_chebyshevform_so_so.
149 85 storres
150 85 storres
def pobyso_cmp(rnArgSa, cteSo):
151 85 storres
    """
152 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
153 54 storres

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

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