root / pobysoPythonSage / src / sageMpfr.spyx @ 160
Historique | Voir | Annoter | Télécharger (4,77 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 |
# - before mpfr_min_prec() is declared, we need to "ctypedef" mpfr_prec_t |
13 |
# if we want to be consistent with MPFR notations. The actual type comes |
14 |
# from mpfr.h but, according to documentation, any type of same general |
15 |
# kind would have been OK. |
16 |
cdef extern from "mpfr.h": |
17 |
ctypedef unsigned short mpfr_prec_t |
18 |
cdef mpfr_prec_t MPFR_PREC_MIN "MPFR_PREC_MIN" |
19 |
cdef mpfr_prec_t mpfr_min_prec(mpfr_t x) |
20 |
|
21 |
cpdef int get_interval_value(RealIntervalFieldElement x): |
22 |
""" |
23 |
Get the address of the value of a RealIntervalField Element. |
24 |
This address can be used to to set the value as well. |
25 |
""" |
26 |
return(<int>&(x.value)) |
27 |
|
28 |
cpdef int get_rn_value(RealNumber x): |
29 |
""" |
30 |
Get the address of the value of a RealNumber. This address can be |
31 |
used to to set the value as well. |
32 |
""" |
33 |
return(<int>&(x.value)) |
34 |
|
35 |
cpdef set_interval_value(RealIntervalFieldElement x, int p): |
36 |
""" |
37 |
Set the value of x from MPFI interval p. |
38 |
""" |
39 |
# We use the "array trick" to workaround the pointer dereferencing |
40 |
# issue (in Cython there is no unary operator "*"). |
41 |
# |
42 |
mpfi_set(x.value, (<mpfi_t *>p)[0]) |
43 |
|
44 |
cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN): |
45 |
""" |
46 |
Set the value of x from MPFR number p. |
47 |
""" |
48 |
# We use the "array trick" to workaround the pointer dereferencing |
49 |
# issue (in Cython there is no unary operator "*"). |
50 |
mpfr_set(x.value, (<mpfr_t *>p)[0], rnd) |
51 |
# |
52 |
cpdef mpfr_prec_t min_rn_size(RealNumber x): |
53 |
""" |
54 |
Compute the minimum number of bits necessary to represent |
55 |
the x Sage RealNumber's value. |
56 |
If x is a power of 2 (only 1 bit needed) return the MPFR |
57 |
MPFR_PREC_MIN value. |
58 |
""" |
59 |
cdef mpfr_prec_t min_prec = mpfr_min_prec(x.value) |
60 |
if min_prec < MPFR_PREC_MIN: |
61 |
return(MPFR_PREC_MIN) |
62 |
else: |
63 |
return(min_prec) |
64 |
# |
65 |
cpdef min_mpfr_size(int p): |
66 |
""" |
67 |
Compute the minimum number of bits necessary to represent |
68 |
the MPFR value pointed by the mpfr_t pointer p (hence the int type). |
69 |
If *p is a power of 2 (only 1 bit needed) return the MPFR |
70 |
MPFR_PREC_MIN value. |
71 |
""" |
72 |
# We use the "array trick" to workaround the pointer dereferencing |
73 |
# issue (in Cython there is no unary operator "*"). |
74 |
cdef mpfr_prec_t min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) |
75 |
if min_prec < MPFR_PREC_MIN: |
76 |
return(MPFR_PREC_MIN) |
77 |
else: |
78 |
return(min_prec) |
79 |
|
80 |
cpdef free_mpfi_num(int mpfiNum): |
81 |
""" |
82 |
Free a MPFI interval pointed by mpfiNum (hece the int type). |
83 |
""" |
84 |
# We use the "array trick" to workaround the pointer dereferencing |
85 |
# issue (in Cython there is no unary operator "*"). |
86 |
mpfi_clear((<mpfi_t*>mpfiNum)[0]) |
87 |
|
88 |
cpdef free_mpfr_num(int mpfrNum): |
89 |
""" |
90 |
Free a MPFR number pointed by mpfrNum (hece the int type). |
91 |
""" |
92 |
# We use the "array trick" to workaround the pointer dereferencing |
93 |
# issue (in Cython there is no unary operator "*"). |
94 |
mpfr_clear((<mpfr_t*>mpfrNum)[0]) |
95 |
|
96 |
cpdef int new_interval_num(unsigned int prec): |
97 |
""" |
98 |
Create a newly allocated MPFI interval to precision prec. |
99 |
""" |
100 |
cdef mpfi_t *newValue = NULL |
101 |
print "Precision : ", prec |
102 |
newValue = <mpfi_t*>malloc(sizeof(mpfi_t)) |
103 |
mpfi_init2(newValue[0], prec) |
104 |
print "Creation done!" |
105 |
return(<int>newValue) |
106 |
|
107 |
cpdef int new_mpfr_num(unsigned int prec): |
108 |
cdef mpfr_t *newValue = NULL |
109 |
print "Precision : ", prec |
110 |
newValue = <mpfr_t*>malloc(sizeof(mpfr_t)) |
111 |
mpfr_init2(newValue[0], prec) |
112 |
print "Creation done!" |
113 |
return(<int>newValue) |
114 |
|
115 |
# |
116 |
|
117 |
cpdef cmp_rn_value(RealNumber x, RealNumber y): |
118 |
""" |
119 |
Compare two RN with the mpfr_cmp function |
120 |
""" |
121 |
return(mpfr_cmp(x.value, y.value)) |
122 |
|
123 |
cpdef cmp_interval_value(RealIntervalFieldElement x, |
124 |
RealIntervalFieldElement y): |
125 |
""" |
126 |
Compare two intervals with the mpfi_cmp function |
127 |
""" |
128 |
return(mpfi_cmp(x.value, y.value)) |
129 |
|
130 |
# Do not use this function! |
131 |
# It changes the precision of the instance behind the curtain, but precision is |
132 |
# class attribute. If the precision is increased, memory space is wasted and |
133 |
# other problems may surface in subsequent operations. |
134 |
# If the precision is decreased the semantics |
135 |
# are broken since the expected precision (from the class attribute) is not |
136 |
# the actual precision. |
137 |
# |
138 |
cpdef s_rn_value(RealNumber x, int p): |
139 |
mpfr_clear(x.value) |
140 |
mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0])) |
141 |
#x.__prec = mpfr_get_prec(x.value) |
142 |
printf("x.value prec: %d\n", mpfr_get_prec(x.value)) |
143 |
mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN) |