root / pobysoPythonSage / src / sageMpfr.spyx @ 90
Historique | Voir | Annoter | Télécharger (3,71 ko)
1 |
#cimport cython |
---|---|
2 |
from libc.stdlib cimport malloc,free |
3 |
from sage.rings.real_mpfr cimport * |
4 |
from sage.rings.real_mpfi cimport * |
5 |
# |
6 |
# Functions to manipulate the real RealNumber values. |
7 |
# |
8 |
# Import constants and declarations that are not exported |
9 |
# from the Sage real_mpfr.pyx file. |
10 |
# Notes: |
11 |
# - notice how #define constants are imported. |
12 |
# - mpfr_min_prec is declared with an unsigned int return value, not |
13 |
# a mpfr_prec_t since it is not recognized as type in Cython. |
14 |
cdef extern from "mpfr.h": |
15 |
cdef int MPFR_PREC_MIN "MPFR_PREC_MIN" |
16 |
cdef unsigned int mpfr_min_prec(mpfr_t x) |
17 |
|
18 |
cpdef int get_interval_value(RealIntervalFieldElement x): |
19 |
""" |
20 |
Get the address of the value of a RealIntervalField Element. |
21 |
This address can be used to to set the value as well. |
22 |
""" |
23 |
return(<int>&(x.value)) |
24 |
|
25 |
cpdef int get_rn_value(RealNumber x): |
26 |
""" |
27 |
Get the address of the value of a RealNumber. This address can be |
28 |
used to to set the value as well. |
29 |
""" |
30 |
return(<int>&(x.value)) |
31 |
|
32 |
cpdef set_interval_value(RealIntervalFieldElement x, int p): |
33 |
""" |
34 |
Set the value of x from MPFI interval p. |
35 |
""" |
36 |
# We use the "array trick" to workaround the pointer dereferencing |
37 |
# issue (in Cython there is no unary operator "*"). |
38 |
# |
39 |
mpfi_set(x.value, (<mpfi_t *>p)[0]) |
40 |
|
41 |
cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN): |
42 |
""" |
43 |
Set the value of x from MPFR number p. |
44 |
""" |
45 |
# We use the "array trick" to workaround the pointer dereferencing |
46 |
# issue (in Cython there is no unary operator "*"). |
47 |
# |
48 |
mpfr_set(x.value, (<mpfr_t *>p)[0], rnd) |
49 |
# |
50 |
cpdef min_rn_size(RealNumber x): |
51 |
""" |
52 |
Compute the minimum number of bits necessary to represent |
53 |
the x RealNumber's value. |
54 |
If x is a power of 2 (only 1 bit needed) return the MPFR |
55 |
MPFR_PREC_MIN value. |
56 |
""" |
57 |
cdef int min_prec = mpfr_min_prec(x.value) |
58 |
if min_prec < MPFR_PREC_MIN: |
59 |
return(MPFR_PREC_MIN) |
60 |
else: |
61 |
return(min_prec) |
62 |
|
63 |
cpdef min_mpfr_size(int p): |
64 |
""" |
65 |
Compute the minimum number of bits necessary to represent |
66 |
the MPFR value pointed by p. |
67 |
If x is a power of 2 (only 1 bit needed) return the MPFR |
68 |
MPFR_PREC_MIN value. |
69 |
""" |
70 |
cdef int min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) |
71 |
if min_prec < MPFR_PREC_MIN: |
72 |
return(MPFR_PREC_MIN) |
73 |
else: |
74 |
return(min_prec) |
75 |
|
76 |
cpdef free_mpfi_num(int mpfiNum): |
77 |
mpfi_clear((<mpfi_t*>mpfiNum)[0]) |
78 |
|
79 |
cpdef free_mpfr_num(int mpfrNum): |
80 |
mpfr_clear((<mpfr_t*>mpfrNum)[0]) |
81 |
|
82 |
cpdef new_interval_num(unsigned int prec): |
83 |
cdef mpfi_t *newValue = NULL |
84 |
print "Precision : ", prec |
85 |
newValue = <mpfi_t*>malloc(sizeof(mpfi_t)) |
86 |
mpfi_init2(newValue[0], prec) |
87 |
print "Creation done!" |
88 |
return(<int>newValue) |
89 |
|
90 |
cpdef new_mpfr_num(unsigned int prec): |
91 |
cdef mpfr_t *newValue = NULL |
92 |
print "Precision : ", prec |
93 |
newValue = <mpfr_t*>malloc(sizeof(mpfr_t)) |
94 |
mpfr_init2(newValue[0], prec) |
95 |
print "Creation done!" |
96 |
return(<int>newValue) |
97 |
|
98 |
# |
99 |
|
100 |
cpdef cmp_rn_value(RealNumber x, RealNumber y): |
101 |
""" |
102 |
Compare two RN with the mpfr_cmp function |
103 |
""" |
104 |
return(mpfr_cmp(x.value, y.value)) |
105 |
|
106 |
# Do not use this function! |
107 |
# It changes the precision of the instance behind the curtain, but precision is |
108 |
# class attribute. If the precision is increased, memory space is wasted and |
109 |
# other problems may surface in subsequent operations. |
110 |
# If the precision is decreased the semantics |
111 |
# are broken since the expected precision (from the class attribute) is not |
112 |
# the actual precision. |
113 |
# |
114 |
cpdef s_rn_value(RealNumber x, int p): |
115 |
mpfr_clear(x.value) |
116 |
mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0])) |
117 |
#x.__prec = mpfr_get_prec(x.value) |
118 |
printf("x.value prec: %d\n", mpfr_get_prec(x.value)) |
119 |
mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN) |