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