Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 54

Historique | Voir | Annoter | Télécharger (30,18 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 38 storres
- the _so_so (arguments and returned objects are pointers to Sollya objects, includes
10 38 storres
  the void function and the no arguments function that return a pointer to a Sollya
11 38 storres
  object);
12 38 storres
- the _so_sa (argument are pointers to Sollya objects, returned objects are
13 38 storres
  Sage/Python objects or, more generally, information is transfered from the Sollya
14 38 storres
  world to Sage/Python world);
15 38 storres
- the _sa_so (arguments are Sage/Python objects, returned objects are
16 38 storres
  pointers to Sollya objects);
17 38 storres
- the sa_sa (arguments and returned objects are all Sage/Python objects);
18 51 storres
- a catch all flavor, without any suffix, (e. g. functions that have no argument
19 51 storres
  nor return value).
20 5 storres
NOTES:
21 5 storres
Reported errors in Eclipse come from the calls to
22 5 storres
the Sollya library
23 5 storres

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

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

379 52 storres
    Arguments:
380 52 storres
    soExp -- a Sollya expression pointer
381 52 storres
    Return value:
382 52 storres
    A Python integer
383 38 storres
    TODO:
384 38 storres
    - error management;
385 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
386 38 storres
    """
387 5 storres
    maxPrecision = 0
388 52 storres
    minConstPrec = 0
389 52 storres
    currentConstPrec = 0
390 38 storres
    operator = pobyso_get_head_function_so_sa(soExp)
391 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
392 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
393 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp)
394 5 storres
        for i in xrange(arity):
395 5 storres
            maxPrecisionCandidate = \
396 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
397 5 storres
            if maxPrecisionCandidate > maxPrecision:
398 5 storres
                maxPrecision = maxPrecisionCandidate
399 5 storres
        return(maxPrecision)
400 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
401 52 storres
        minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
402 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
403 52 storres
        #print minConstPrec, " - ", currentConstPrec
404 52 storres
        return(pobyso_get_min_prec_of_constant_so_sa(soExp))
405 52 storres
406 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
407 5 storres
        return(0)
408 5 storres
    else:
409 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
410 5 storres
        return(0)
411 5 storres
412 52 storres
def pobyso_get_min_prec_of_constant_so_sa(soConstExp):
413 52 storres
    """
414 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
415 52 storres
    constant.
416 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
417 52 storres
    We assume that soCteExp is a point
418 52 storres
    """
419 52 storres
    constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp)
420 52 storres
    return(min_mpfr_size(get_rn_value(constExpAsRn)))
421 52 storres
422 5 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
423 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
424 38 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
425 38 storres
426 38 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR):
427 5 storres
    """
428 38 storres
    Get a Sage expression from a Sollya expression.
429 38 storres
    Currently only tested with polynomials with floating-point coefficients.
430 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
431 5 storres
    """
432 5 storres
    #pobyso_autoprint(sollyaExp)
433 5 storres
    operator = pobyso_get_head_function(sollyaExp)
434 5 storres
    # Constants and the free variable are special cases.
435 5 storres
    # All other operator are dealt with in the same way.
436 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
437 5 storres
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
438 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp)
439 5 storres
        if arity == 1:
440 38 storres
            sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
441 52 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \
442 52 storres
            realField) + ")")
443 5 storres
        elif arity == 2:
444 5 storres
            if operator == SOLLYA_BASE_FUNC_POW:
445 5 storres
                operatorAsString = "**"
446 5 storres
            else:
447 52 storres
                operatorAsString = \
448 52 storres
                    pobyso_function_type_as_string_so_sa(operator)
449 5 storres
            sageExp = \
450 38 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\
451 5 storres
              + " " + operatorAsString + " " + \
452 38 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
453 5 storres
        # We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
454 5 storres
        else:
455 5 storres
            sageExp = eval('None')
456 5 storres
        return(sageExp)
457 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
458 5 storres
        #print "This is a constant"
459 38 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
460 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
461 5 storres
        #print "This is free variable"
462 5 storres
        return(eval(sollya_lib_get_free_variable_name()))
463 5 storres
    else:
464 5 storres
        print "Unexpected"
465 5 storres
        return eval('None')
466 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
467 5 storres
468 38 storres
def pobyso_get_subfunctions(expressionSo):
469 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
470 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
471 38 storres
472 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
473 38 storres
    """
474 38 storres
    Get the subfunctions of an expression.
475 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
476 38 storres
    Could not figure out another way than that ugly list of declarations
477 38 storres
    to recover the addresses of the subfunctions.
478 38 storres
    Arity is limited to 9.
479 38 storres
    """
480 5 storres
    subf0 = c_int(0)
481 5 storres
    subf1 = c_int(0)
482 5 storres
    subf2 = c_int(0)
483 5 storres
    subf3 = c_int(0)
484 5 storres
    subf4 = c_int(0)
485 5 storres
    subf5 = c_int(0)
486 5 storres
    subf6 = c_int(0)
487 5 storres
    subf7 = c_int(0)
488 5 storres
    subf8 = c_int(0)
489 5 storres
    arity = c_int(0)
490 5 storres
    nullPtr = POINTER(c_int)()
491 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
492 5 storres
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
493 5 storres
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
494 5 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
495 5 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
496 5 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
497 5 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
498 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
499 5 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
500 5 storres
    subs = []
501 5 storres
    if arity.value > pobyso_max_arity:
502 38 storres
        return(0,[])
503 5 storres
    for i in xrange(arity.value):
504 5 storres
        subs.append(int(subfunctions[i].value))
505 5 storres
        #print subs[i]
506 5 storres
    return(int(arity.value), subs)
507 5 storres
508 5 storres
def pobyso_get_prec():
509 38 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
510 38 storres
    return(pobyso_get_prec_so_sa())
511 38 storres
512 38 storres
def pobyso_get_prec_so_sa():
513 38 storres
    """
514 38 storres
    Get the current default precision in Sollya.
515 38 storres
    The return value is Sage/Python int.
516 38 storres
    """
517 5 storres
    retc = sollya_lib_get_prec(None)
518 5 storres
    a = c_int(0)
519 5 storres
    sollya_lib_get_constant_as_int(byref(a), retc)
520 5 storres
    return(int(a.value))
521 5 storres
522 38 storres
def pobyso_get_prec_of_constant(ctExpSo):
523 38 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
524 38 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
525 38 storres
526 53 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
527 5 storres
    prec = c_int(0)
528 53 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
529 5 storres
    return(int(prec.value))
530 5 storres
531 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
532 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
533 54 storres
    return(None)
534 53 storres
535 37 storres
def pobyso_lib_init():
536 37 storres
    sollya_lib_init(None)
537 37 storres
538 37 storres
def pobyso_name_free_variable(freeVariableName):
539 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
540 38 storres
    pobyso_name_free_variable_sa_so(freeVariableName)
541 38 storres
542 38 storres
def pobyso_name_free_variable_sa_so(freeVariableName):
543 37 storres
    sollya_lib_name_free_variable(freeVariableName)
544 37 storres
545 5 storres
def pobyso_parse_string(string):
546 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
547 38 storres
    return(pobyso_parse_string_sa_so(string))
548 38 storres
549 38 storres
def pobyso_parse_string_sa_so(string):
550 5 storres
    return(sollya_lib_parse_string(string))
551 5 storres
552 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
553 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
554 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
555 38 storres
556 38 storres
def pobyso_range_sa_so(rnLowerBound, rnUpperBound):
557 5 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound))
558 5 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound))
559 5 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
560 5 storres
    return(rangeSo)
561 5 storres
562 52 storres
def pobyso_remez_canonical_sa_sa(func, \
563 52 storres
                                 degree, \
564 52 storres
                                 lowerBound, \
565 52 storres
                                 upperBound, \
566 52 storres
                                 weight = None, \
567 52 storres
                                 quality = None):
568 52 storres
    """
569 52 storres
    All arguments are Sage/Python.
570 52 storres
    The functions (func and weight) must be passed as expressions or strings.
571 52 storres
    Otherwise the function fails.
572 52 storres
    The return value is a pointer is a Sage polynomial.
573 52 storres
    """
574 52 storres
    var('zorglub')    # Dummy variable name for type check only.
575 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
576 52 storres
                                 degree, \
577 52 storres
                                 lowerBound, \
578 52 storres
                                 upperBound, \
579 52 storres
                                 weight = None, \
580 52 storres
                                 quality = None)
581 52 storres
    if parent(func) == parent("string"):
582 52 storres
        functionSa = eval(func)
583 52 storres
    # Expression test.
584 52 storres
    elif type(func) == type(zorglub):
585 52 storres
        functionSa = func
586 52 storres
    maxPrecision = 0
587 52 storres
    if polySo is None:
588 52 storres
        return(None)
589 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
590 52 storres
    RRRR = RealField(maxPrecision)
591 52 storres
    polynomialRing = RRRR[functionSa.variables()[0]]
592 52 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR)
593 52 storres
    polySa = polynomial(expSa, polynomialRing)
594 52 storres
    return(polySa)
595 52 storres
596 38 storres
def pobyso_remez_canonical(func, \
597 5 storres
                           degree, \
598 5 storres
                           lowerBound, \
599 5 storres
                           upperBound, \
600 38 storres
                           weight = "1", \
601 5 storres
                           quality = None):
602 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
603 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
604 51 storres
                                        degree, \
605 51 storres
                                        lowerBound, \
606 51 storres
                                        upperBound, \
607 51 storres
                                        weight, \
608 51 storres
                                        quality))
609 38 storres
def pobyso_remez_canonical_sa_so(func, \
610 38 storres
                                 degree, \
611 38 storres
                                 lowerBound, \
612 38 storres
                                 upperBound, \
613 52 storres
                                 weight = None, \
614 38 storres
                                 quality = None):
615 38 storres
    """
616 38 storres
    All arguments are Sage/Python.
617 51 storres
    The functions (func and weight) must be passed as expressions or strings.
618 51 storres
    Otherwise the function fails.
619 38 storres
    The return value is a pointer to a Sollya function.
620 38 storres
    """
621 52 storres
    var('zorglub')    # Dummy variable name for type check only.
622 52 storres
    currentVariableName = None
623 52 storres
    # The func argument can be of different types (string,
624 52 storres
    # symbolic expression...)
625 38 storres
    if parent(func) == parent("string"):
626 38 storres
        functionSo = sollya_lib_parse_string(func)
627 51 storres
    # Expression test.
628 52 storres
    elif type(func) == type(zorglub):
629 52 storres
        # Until we are able to translate Sage expressions into Sollya
630 52 storres
        # expressions : parse the string version.
631 52 storres
        currentVariableName = func.variables()[0]
632 52 storres
        sollya_lib_name_free_variable(str(currentVariableName))
633 52 storres
        functionSo = sollya_lib_parse_string(func._assume_str())
634 38 storres
    else:
635 38 storres
        return(None)
636 52 storres
    if weight is None:
637 52 storres
        weightSo = pobyso_constant_1_sa_so()
638 52 storres
    elif parent(weight) == parent("string"):
639 51 storres
        weightSo = sollya_lib_parse_string(func)
640 51 storres
    elif type(weight) == type(zorglub):
641 51 storres
        functionSo = sollya_lib_parse_string_sa_so(weight._assume_str())
642 51 storres
    else:
643 51 storres
        return(None)
644 5 storres
    degreeSo = pobyso_constant_from_int(degree)
645 38 storres
    rangeSo = pobyso_range_sa_so(lowerBound, upperBound)
646 38 storres
    if not quality is None:
647 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
648 52 storres
    else:
649 52 storres
        qualitySo = None
650 52 storres
    return(sollya_lib_remez(functionSo, \
651 52 storres
                            degreeSo, \
652 52 storres
                            rangeSo, \
653 52 storres
                            weightSo, \
654 52 storres
                            qualitySo, \
655 52 storres
                            None))
656 5 storres
657 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
658 38 storres
                                 degreeSo, \
659 38 storres
                                 rangeSo, \
660 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
661 38 storres
                                 qualitySo = None):
662 38 storres
    """
663 38 storres
    All arguments are pointers to Sollya objects.
664 38 storres
    The return value is a pointer to a Sollya function.
665 38 storres
    """
666 38 storres
    if not sollya_lib_obj_is_function(funcSo):
667 38 storres
        return(None)
668 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
669 38 storres
670 5 storres
def pobyso_set_canonical_off():
671 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
672 5 storres
673 5 storres
def pobyso_set_canonical_on():
674 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
675 5 storres
676 5 storres
def pobyso_set_prec(p):
677 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
678 38 storres
    return( pobyso_set_prec_sa_so(p))
679 38 storres
680 38 storres
def pobyso_set_prec_sa_so(p):
681 5 storres
    a = c_int(p)
682 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
683 5 storres
    sollya_lib_set_prec(precSo)
684 5 storres
685 54 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo, accuracySo):
686 54 storres
    return(sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
687 54 storres
                              accuracySo))
688 54 storres
689 5 storres
def pobyso_taylor(function, degree, point):
690 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
691 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
692 38 storres
693 38 storres
def pobyso_taylor_so_so(function, degree, point):
694 5 storres
    return(sollya_lib_taylor(function, degree, point))
695 5 storres
696 5 storres
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
697 38 storres
    """ Legacy function. See ;"""
698 38 storres
699 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
700 38 storres
                            degree, \
701 43 storres
                            point, \
702 43 storres
                            precision, \
703 54 storres
                            interval=None, \
704 38 storres
                            errorType=None):
705 37 storres
    """
706 38 storres
    Compute the Taylor form of 'degree' for 'functionSa' at 'point'
707 37 storres
    for 'interval' with 'errorType'.
708 37 storres
    point: must be a Real or a Real interval.
709 37 storres
    return the Taylor form as an array
710 38 storres
    TODO: take care of the interval and of point when it is an interval;
711 38 storres
          when errorType is not None;
712 38 storres
          take care of the other elements of the Taylor form (coefficients errors and
713 38 storres
          delta.
714 37 storres
    """
715 37 storres
    # Absolute as the default error.
716 5 storres
    if errorType is None:
717 37 storres
        errorTypeSo = sollya_lib_absolute()
718 37 storres
    else:
719 37 storres
        #TODO: deal with the other case.
720 37 storres
        pass
721 38 storres
    varSa = functionSa.variables()[0]
722 37 storres
    pointBaseRingString = str(point.base_ring())
723 37 storres
    if not re.search('Real', pointBaseRingString):
724 37 storres
        return None
725 37 storres
    # Call Sollya but first "sollyafy" the arguments.
726 38 storres
    pobyso_name_free_variable_sa_so(str(varSa))
727 38 storres
    #pobyso_set_prec_sa_so(300)
728 37 storres
    # Sollyafy the function.
729 38 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
730 37 storres
    if sollya_lib_obj_is_error(functionSo):
731 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
732 37 storres
        return None
733 37 storres
    # Sollyafy the degree
734 37 storres
    degreeSo = sollya_lib_constant_from_int(int(degree))
735 37 storres
    # Sollyafy the point
736 37 storres
    if not re.search('Interval', pointBaseRingString):
737 38 storres
        pointSo  = pobyso_constant_sa_so(point)
738 37 storres
    else:
739 37 storres
        # TODO: deal with the interval case.
740 37 storres
        pass
741 37 storres
    # Call Sollya
742 37 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
743 37 storres
                                         None)
744 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
745 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
746 37 storres
    polySo = tfsAsList[0]
747 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
748 37 storres
    polyRealField = RealField(maxPrecision)
749 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
750 37 storres
    sollya_lib_close()
751 37 storres
    polynomialRing = polyRealField[str(varSa)]
752 37 storres
    polySa = polynomial(expSa, polynomialRing)
753 37 storres
    taylorFormSa = [polySa]
754 51 storres
    return(taylorFormSa)
755 51 storres
# End pobyso_taylor_form_sa_sa
756 54 storres
757 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
758 54 storres
                            errorTypeSo=None):
759 54 storres
    createdErrorType = False
760 51 storres
    if errorTypeSo is None:
761 51 storres
        errorTypeSo = sollya_lib_absolute()
762 54 storres
        createdErrorType = True
763 51 storres
    else:
764 51 storres
        #TODO: deal with the other case.
765 51 storres
        pass
766 51 storres
    if intervalSo is None:
767 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
768 54 storres
                                         errorTypeSo, None)
769 51 storres
    else:
770 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
771 54 storres
                                         intervalSo, errorTypeSo, None)
772 54 storres
    if createdErrorType:
773 54 storres
        sollya_lib_clear_obj(errorTypeSo)
774 51 storres
    return(resultSo)
775 51 storres
776 37 storres
777 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
778 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
779 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
780 38 storres
781 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
782 37 storres
    """
783 37 storres
    Return the string representation of a univariate polynomial with
784 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
785 37 storres
    Remember: Sage
786 37 storres
    """
787 37 storres
    polynomialRing = polySa.base_ring()
788 37 storres
    # A very expensive solution:
789 37 storres
    # -create a fake multivariate polynomial field with only one variable,
790 37 storres
    #   specifying a negative lexicographical order;
791 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
792 37 storres
                                     polynomialRing.variable_name(), \
793 37 storres
                                     1, order='neglex')
794 37 storres
    # - convert the univariate argument polynomial into a multivariate
795 37 storres
    #   version;
796 37 storres
    p = mpolynomialRing(polySa)
797 37 storres
    # - return the string representation of the converted form.
798 37 storres
    # There is no simple str() method defined for p's class.
799 37 storres
    return(p.__str__())
800 5 storres
#
801 5 storres
print "Superficial test of pobyso:"
802 5 storres
print pobyso_get_prec()
803 5 storres
pobyso_set_prec(165)
804 5 storres
print pobyso_get_prec()
805 5 storres
a=100
806 5 storres
print type(a)
807 5 storres
id(a)
808 5 storres
print "Max arity: ", pobyso_max_arity
809 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
810 5 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)