Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 35

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