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