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