Statistiques
| Révision :

root / pobysoPythonSage / src / sageMpfr.spyx @ 160

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