Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 9

Historique | Voir | Annoter | Télécharger (14,31 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 5 storres
NOTES:
9 5 storres
Reported errors in Eclipse come from the calls to
10 5 storres
the Sollya library
11 5 storres

12 5 storres
ToDo (among other things): memory management.
13 5 storres
"""
14 5 storres
from ctypes import *
15 5 storres
16 5 storres
(SOLLYA_BASE_FUNC_ABS,
17 5 storres
SOLLYA_BASE_FUNC_ACOS,
18 5 storres
    SOLLYA_BASE_FUNC_ACOSH,
19 5 storres
    SOLLYA_BASE_FUNC_ADD,
20 5 storres
    SOLLYA_BASE_FUNC_ASIN,
21 5 storres
    SOLLYA_BASE_FUNC_ASINH,
22 5 storres
    SOLLYA_BASE_FUNC_ATAN,
23 5 storres
    SOLLYA_BASE_FUNC_ATANH,
24 5 storres
    SOLLYA_BASE_FUNC_CEIL,
25 5 storres
    SOLLYA_BASE_FUNC_CONSTANT,
26 5 storres
    SOLLYA_BASE_FUNC_COS,
27 5 storres
    SOLLYA_BASE_FUNC_COSH,
28 5 storres
    SOLLYA_BASE_FUNC_DIV,
29 5 storres
    SOLLYA_BASE_FUNC_DOUBLE,
30 5 storres
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
31 5 storres
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
32 5 storres
    SOLLYA_BASE_FUNC_ERF,
33 5 storres
    SOLLYA_BASE_FUNC_ERFC,
34 5 storres
    SOLLYA_BASE_FUNC_EXP,
35 5 storres
    SOLLYA_BASE_FUNC_EXP_M1,
36 5 storres
    SOLLYA_BASE_FUNC_FLOOR,
37 5 storres
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
38 5 storres
    SOLLYA_BASE_FUNC_HALFPRECISION,
39 5 storres
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
40 5 storres
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
41 5 storres
    SOLLYA_BASE_FUNC_LOG,
42 5 storres
    SOLLYA_BASE_FUNC_LOG_10,
43 5 storres
    SOLLYA_BASE_FUNC_LOG_1P,
44 5 storres
    SOLLYA_BASE_FUNC_LOG_2,
45 5 storres
    SOLLYA_BASE_FUNC_MUL,
46 5 storres
    SOLLYA_BASE_FUNC_NEARESTINT,
47 5 storres
    SOLLYA_BASE_FUNC_NEG,
48 5 storres
    SOLLYA_BASE_FUNC_PI,
49 5 storres
    SOLLYA_BASE_FUNC_POW,
50 5 storres
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
51 5 storres
    SOLLYA_BASE_FUNC_QUAD,
52 5 storres
    SOLLYA_BASE_FUNC_SIN,
53 5 storres
    SOLLYA_BASE_FUNC_SINGLE,
54 5 storres
    SOLLYA_BASE_FUNC_SINH,
55 5 storres
    SOLLYA_BASE_FUNC_SQRT,
56 5 storres
    SOLLYA_BASE_FUNC_SUB,
57 5 storres
    SOLLYA_BASE_FUNC_TAN,
58 5 storres
    SOLLYA_BASE_FUNC_TANH,
59 5 storres
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
60 5 storres
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
61 5 storres
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
62 5 storres
63 5 storres
pobyso_max_arity = 9
64 5 storres
65 5 storres
def pobyso_autoprint(arg):
66 5 storres
    sollya_lib_autoprint(arg,None)
67 5 storres
68 5 storres
def pobyso_cmp(rnArg, soCte):
69 5 storres
    precisionOfCte = c_int(0)
70 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
71 5 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, soCte)
72 5 storres
    #print "Precision of constant: ", precisionOfCte
73 5 storres
    RRRR = RealField(precisionOfCte.value)
74 5 storres
    rnLocal = RRRR(0)
75 5 storres
    sollya_lib_get_constant(get_rn_value(rnLocal), soCte)
76 5 storres
    #print "rnDummy: ", rnDummy
77 5 storres
    # Compare the local Sage RealNumber with rnArg.
78 5 storres
    return(cmp_rn_value(rnArg, rnLocal))
79 5 storres
80 5 storres
def pobyso_constant(rnArg):
81 5 storres
    return (sollya_lib_constant(get_rn_value(rnArg)))
82 5 storres
83 5 storres
def pobyso_constant_1():
84 5 storres
    return(pobyso_constant_from_int(1))
85 5 storres
86 5 storres
def pobyso_constant_from_int(anInt):
87 5 storres
    return(sollya_lib_constant_from_int(int(anInt)))
88 5 storres
89 5 storres
# Numeric Sollya function codes -> Sage mathematical function names
90 5 storres
def pobyso_function_type_as_string(funcType):
91 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
92 5 storres
        return "abs"
93 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
94 5 storres
        return "arccos"
95 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
96 5 storres
        return "arccosh"
97 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
98 5 storres
        return "+"
99 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
100 5 storres
        return "arcsin"
101 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
102 5 storres
        return "arcsinh"
103 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
104 5 storres
        return "arctan"
105 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
106 5 storres
        return "arctanh"
107 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
108 5 storres
        return "ceil"
109 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
110 5 storres
        return "cte"
111 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
112 5 storres
        return "cos"
113 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
114 5 storres
        return "cosh"
115 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
116 5 storres
        return "/"
117 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
118 5 storres
        return "double"
119 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
120 5 storres
        return "doubleDouble"
121 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
122 5 storres
        return "doubleDxtended"
123 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
124 5 storres
        return "erf"
125 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
126 5 storres
        return "erfc"
127 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
128 5 storres
        return "exp"
129 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
130 5 storres
        return "expm1"
131 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
132 5 storres
        return "floor"
133 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
134 5 storres
        return "freeVariable"
135 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
136 5 storres
        return "halfPrecision"
137 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
138 5 storres
        return "libraryConstant"
139 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
140 5 storres
        return "libraryFunction"
141 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
142 5 storres
        return "log"
143 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
144 5 storres
        return "log10"
145 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
146 5 storres
        return "log1p"
147 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
148 5 storres
        return "log2"
149 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
150 5 storres
        return "*"
151 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
152 5 storres
        return "round"
153 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
154 5 storres
        return "__neg__"
155 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
156 5 storres
        return "pi"
157 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
158 5 storres
        return "^"
159 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
160 5 storres
        return "procedureFunction"
161 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
162 5 storres
        return "quad"
163 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
164 5 storres
        return "sin"
165 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
166 5 storres
        return "single"
167 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
168 5 storres
        return "sinh"
169 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
170 5 storres
        return "sqrt"
171 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
172 5 storres
        return "-"
173 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
174 5 storres
        return "tan"
175 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
176 5 storres
        return "tanh"
177 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
178 5 storres
        return "tripleDouble"
179 5 storres
    else:
180 5 storres
        return None
181 5 storres
182 5 storres
def pobyso_get_constant(rnArg, soConst):
183 5 storres
    set_rn_value(rnArg, soConst)
184 5 storres
185 5 storres
def pobyso_get_constant_as_rn(ctExp):
186 5 storres
    precision  = pobyso_get_prec_of_constant(ctExp)
187 5 storres
    RRRR = RealField(precision)
188 5 storres
    rn = RRRR(0)
189 5 storres
    sollya_lib_get_constant(get_rn_value(rn), ctExp)
190 5 storres
    return(rn)
191 5 storres
192 5 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
193 5 storres
    rn = realField(0)
194 5 storres
    sollya_lib_get_constant(get_rn_value(rn), ctExp)
195 5 storres
    return(rn)
196 5 storres
def pobyso_get_free_variable_name():
197 5 storres
    return(sollya_lib_get_free_variable_name())
198 5 storres
199 5 storres
def pobyso_get_function_arity(expression):
200 5 storres
    arity = c_int(0)
201 5 storres
    sollya_lib_get_function_arity(byref(arity),expression)
202 5 storres
    return(int(arity.value))
203 5 storres
204 5 storres
def pobyso_get_head_function(expression):
205 5 storres
    functionType = c_int(0)
206 5 storres
    sollya_lib_get_head_function(byref(functionType), expression, None)
207 5 storres
    return(int(functionType.value))
208 5 storres
209 5 storres
def pobyso_get_list_elements(soObj):
210 5 storres
    # Type for array of pointers to sollya_obj_t
211 5 storres
    listAddress = POINTER(c_longlong)()
212 5 storres
    numElements = c_int(0)
213 5 storres
    isEndElliptic = c_int(0)
214 5 storres
    listAsList = []
215 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
216 5 storres
                                        byref(numElements),\
217 5 storres
                                        byref(isEndElliptic),\
218 5 storres
                                        soObj)
219 5 storres
    if result == 0 :
220 5 storres
        return None
221 5 storres
    for i in xrange(0, numElements.value, 1):
222 5 storres
        print "address ", i, " ->", listAddress[i]
223 5 storres
        listAsList.append(listAddress[i])
224 5 storres
    return(listAsList, numElements.value, isEndElliptic.value)
225 5 storres
226 5 storres
227 5 storres
# Get the maximum precision used for the numbers in a
228 5 storres
# Sollya expression.
229 5 storres
# ToDo:
230 5 storres
# - error management;
231 5 storres
# - correctly deal with numerical type such as DOUBLEEXTENDED.
232 5 storres
def pobyso_get_max_prec_of_exp(soExp):
233 5 storres
    maxPrecision = 0
234 5 storres
    operator = pobyso_get_head_function(soExp)
235 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
236 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
237 5 storres
        (arity, subexpressions) = pobyso_get_subfunctions(soExp)
238 5 storres
        for i in xrange(arity):
239 5 storres
            maxPrecisionCandidate = \
240 5 storres
            pobyso_get_max_prec_of_exp(subexpressions[i])
241 5 storres
            if maxPrecisionCandidate > maxPrecision:
242 5 storres
                maxPrecision = maxPrecisionCandidate
243 5 storres
        return(maxPrecision)
244 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
245 5 storres
        #print pobyso_get_prec_of_constant(soExp)
246 5 storres
        return(pobyso_get_prec_of_constant(soExp))
247 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
248 5 storres
        return(0)
249 5 storres
    else:
250 5 storres
        print "pobyso_get_max_prec_of_exp: unexepected operator."
251 5 storres
        return(0)
252 5 storres
253 5 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
254 5 storres
    """
255 5 storres
    Get a Sage expression from a Sollya expression, currently only tested
256 5 storres
    with polynomials with floating-point coefficients.
257 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
258 5 storres
    """
259 5 storres
    #pobyso_autoprint(sollyaExp)
260 5 storres
    operator = pobyso_get_head_function(sollyaExp)
261 5 storres
    # Constants and the free variable are special cases.
262 5 storres
    # All other operator are dealt with in the same way.
263 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
264 5 storres
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
265 5 storres
        (arity, subexpressions) = pobyso_get_subfunctions(sollyaExp)
266 5 storres
        if arity == 1:
267 5 storres
            sageExp = eval(pobyso_function_type_as_string(operator) + \
268 5 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp(subexpressions[0], realField)\
269 5 storres
             + ")")
270 5 storres
        elif arity == 2:
271 5 storres
            if operator == SOLLYA_BASE_FUNC_POW:
272 5 storres
                operatorAsString = "**"
273 5 storres
            else:
274 5 storres
                operatorAsString = pobyso_function_type_as_string(operator)
275 5 storres
            sageExp = \
276 5 storres
              eval("pobyso_get_sage_exp_from_sollya_exp(subexpressions[0], realField)"\
277 5 storres
              + " " + operatorAsString + " " + \
278 5 storres
                   "pobyso_get_sage_exp_from_sollya_exp(subexpressions[1], realField)")
279 5 storres
        # We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
280 5 storres
        else:
281 5 storres
            sageExp = eval('None')
282 5 storres
        return(sageExp)
283 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
284 5 storres
        #print "This is a constant"
285 5 storres
        return pobyso_get_constant_as_rn_with_rf(sollyaExp, realField)
286 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
287 5 storres
        #print "This is free variable"
288 5 storres
        return(eval(sollya_lib_get_free_variable_name()))
289 5 storres
    else:
290 5 storres
        print "Unexpected"
291 5 storres
        return eval('None')
292 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
293 5 storres
294 5 storres
def pobyso_get_subfunctions(expression):
295 5 storres
    subf0 = c_int(0)
296 5 storres
    subf1 = c_int(0)
297 5 storres
    subf2 = c_int(0)
298 5 storres
    subf3 = c_int(0)
299 5 storres
    subf4 = c_int(0)
300 5 storres
    subf5 = c_int(0)
301 5 storres
    subf6 = c_int(0)
302 5 storres
    subf7 = c_int(0)
303 5 storres
    subf8 = c_int(0)
304 5 storres
    arity = c_int(0)
305 5 storres
    nullPtr = POINTER(c_int)()
306 5 storres
    sollya_lib_get_subfunctions(expression, byref(arity), \
307 5 storres
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
308 5 storres
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
309 5 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
310 5 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
311 5 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
312 5 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
313 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
314 5 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
315 5 storres
    subs = []
316 5 storres
    if arity.value > pobyso_max_arity:
317 5 storres
        return(None,None)
318 5 storres
    for i in xrange(arity.value):
319 5 storres
        subs.append(int(subfunctions[i].value))
320 5 storres
        #print subs[i]
321 5 storres
    return(int(arity.value), subs)
322 5 storres
323 5 storres
def pobyso_get_prec():
324 5 storres
    retc = sollya_lib_get_prec(None)
325 5 storres
    a = c_int(0)
326 5 storres
    sollya_lib_get_constant_as_int(byref(a), retc)
327 5 storres
    return(int(a.value))
328 5 storres
329 5 storres
def pobyso_get_prec_of_constant(ctExp):
330 5 storres
    prec = c_int(0)
331 5 storres
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExp, None)
332 5 storres
    return(int(prec.value))
333 5 storres
334 5 storres
def pobyso_parse_string(string):
335 5 storres
    return(sollya_lib_parse_string(string))
336 5 storres
337 5 storres
def pobyso_univar_polynomial_print_reverse(polySa):
338 5 storres
    """
339 5 storres
    Return the string representation of a univariate polynomial with
340 5 storres
    monomial ordered in the x^0..x^n order of the monomials.
341 5 storres
    Remember: Sage
342 5 storres
    """
343 5 storres
    polynomialRing = polySa.base_ring()
344 5 storres
    # A very expensive solution:
345 5 storres
    # -create a fake multivariate polynomial field with only one variable,
346 5 storres
    #   specifying a negative lexicographical order;
347 5 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
348 5 storres
                                     polynomialRing.variable_name(), \
349 5 storres
                                     1, order='neglex')
350 5 storres
    # - convert the univariate argument polynomial into a multivariate
351 5 storres
    #   version;
352 5 storres
    p = mpolynomialRing(polySa)
353 5 storres
    # - return the string representation of the converted form.
354 5 storres
    # There is no simple str() method defined for p's class.
355 5 storres
    return(p.__str__())
356 5 storres
357 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
358 5 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound))
359 5 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound))
360 5 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
361 5 storres
    return(rangeSo)
362 5 storres
363 5 storres
def pobyso_remez_canonical(function, \
364 5 storres
                           degree, \
365 5 storres
                           lowerBound, \
366 5 storres
                           upperBound, \
367 5 storres
                           weightSo = pobyso_constant_1(),
368 5 storres
                           quality = None):
369 5 storres
    if parent(function) == parent("string"):
370 5 storres
        functionSo = sollya_lib_parse_string(function)
371 5 storres
    #    print "Is string!"
372 5 storres
    elif sollya_lib_obj_is_function(function):
373 5 storres
        functionSo = function
374 5 storres
    #    print "Is Function!"
375 5 storres
    degreeSo = pobyso_constant_from_int(degree)
376 5 storres
    rangeSo = pobyso_range(lowerBound, upperBound)
377 5 storres
    return(sollya_lib_remez(functionSo, degreeSo, rangeSo, quality, None))
378 5 storres
379 5 storres
def pobyso_set_canonical_off():
380 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
381 5 storres
382 5 storres
def pobyso_set_canonical_on():
383 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
384 5 storres
385 5 storres
def pobyso_set_prec(p):
386 5 storres
    a = c_int(p)
387 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
388 5 storres
    sollya_lib_set_prec(precSo)
389 5 storres
390 5 storres
def pobyso_taylor(function, degree, point):
391 5 storres
    return(sollya_lib_taylor(function, degree, point))
392 5 storres
393 5 storres
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
394 5 storres
    if errorType is None:
395 5 storres
        errorType = sollya_lib_absolute()
396 5 storres
    return(sollya_lib_taylorform(function, degree, point, errorType, None))
397 5 storres
#
398 5 storres
print "Superficial test of pobyso:"
399 5 storres
print pobyso_get_prec()
400 5 storres
pobyso_set_prec(165)
401 5 storres
print pobyso_get_prec()
402 5 storres
a=100
403 5 storres
print type(a)
404 5 storres
id(a)
405 5 storres
print "Max arity: ", pobyso_max_arity
406 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
407 5 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)