Statistiques
| Révision :

root / pobysoPythonSage / src / sageMpfr.spyx @ 283

Historique | Voir | Annoter | Télécharger (5,16 ko)

1
#cimport cython 
2
from libc.stdlib          cimport malloc,free
3
from libc.stdio cimport   printf
4
from sage.rings.real_mpfr cimport *
5
from sage.rings.real_mpfi cimport *
6
#
7
# Functions to manipulate the real RealNumber values.
8
#
9
# Import constants and declarations that are not exported
10
# from the Sage real_mpfr.pyx file.
11
# Notes:
12
# - notice how #define constants are imported.
13
# - before mpfr_min_prec() is declared, we need to "ctypedef" mpfr_prec_t
14
#   if we want to be consistent with MPFR notations. The actual type comes 
15
#   from mpfr.h but, according to documentation, any type of same general 
16
#   kind would have been OK.
17
cdef extern from "mpfr.h":
18
    ctypedef unsigned short mpfr_prec_t
19
    cdef mpfr_prec_t MPFR_PREC_MIN "MPFR_PREC_MIN"
20
    cdef mpfr_rnd_t MPFR_RNDN "MPFR_RNDN"
21
    cdef mpfr_prec_t mpfr_min_prec(mpfr_t x)
22

    
23
cpdef unsigned long int deref(unsigned long int p, int index):
24
    return((<unsigned long int*>p)[index])
25

    
26
cpdef unsigned long int get_interval_value(RealIntervalFieldElement x):
27
    """
28
    Get the address of the value of a RealIntervalField Element. 
29
    This address can be used to to set the value as well.
30
    """
31
    return(<unsigned long int>&(x.value))
32

    
33
cpdef unsigned long int get_rn_value(RealNumber x):
34
    """
35
    Get the address of the value of a RealNumber. This address can be
36
    used to to set the value as well.
37
    """
38
    return(<unsigned long int>(&(x.value)))
39
    
40
cpdef set_interval_value(RealIntervalFieldElement x, unsigned long int p):
41
    """
42
    Set the value of x from MPFI interval p.
43
    """
44
    # We use the "array trick" to workaround the pointer dereferencing
45
    # issue (in Cython there is no unary operator "*").
46
    #
47
    mpfi_set(x.value, (<mpfi_t *>p)[0])
48

    
49
cpdef set_rn_value(RealNumber x, unsigned long int p, mpfr_rnd_t rnd=MPFR_RNDN):
50
    """
51
    Set the value of x from MPFR number p.
52
    """
53
    # We use the "array trick" to workaround the pointer dereferencing
54
    # issue (in Cython there is no unary operator "*").
55
    mpfr_set(x.value, (<mpfr_t *>p)[0], rnd)
56
#
57
cpdef mpfr_prec_t min_rn_size(RealNumber x):
58
    """
59
    Compute the minimum number of bits necessary to represent
60
    the x Sage RealNumber's value.
61
    If x is a power of 2 (only 1 bit needed) return the MPFR
62
    MPFR_PREC_MIN value.
63
    """
64
    cdef mpfr_prec_t min_prec = mpfr_min_prec(x.value) 
65
    if min_prec < MPFR_PREC_MIN:
66
        return(MPFR_PREC_MIN)
67
    else:
68
        return(min_prec)
69
#
70
cpdef min_mpfr_size(unsigned long int p):
71
    """
72
    Compute the minimum number of bits necessary to represent
73
    the MPFR value pointed by the mpfr_t pointer p (hence the int type).
74
    If *p is a power of 2 (only 1 bit needed) return the MPFR
75
    MPFR_PREC_MIN value.
76
    """
77
    # We use the "array trick" to workaround the pointer dereferencing
78
    # issue (in Cython there is no unary operator "*").
79
    cdef mpfr_prec_t min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) 
80
    if min_prec < MPFR_PREC_MIN:
81
        return(MPFR_PREC_MIN)
82
    else:
83
        return(min_prec)
84

    
85
cpdef free_mpfi_num(unsigned long int mpfiNum):
86
    """
87
    Free a MPFI interval pointed by mpfiNum (hece the unsigned long int type).
88
    """
89
    # We use the "array trick" to workaround the pointer dereferencing
90
    # issue (in Cython there is no unary operator "*").
91
    mpfi_clear((<mpfi_t*>mpfiNum)[0])
92

    
93
cpdef free_mpfr_num(unsigned long int mpfrNum):
94
    """
95
    Free a MPFR number pointed by mpfrNum (hece the int type).
96
    """
97
    # We use the "array trick" to workaround the pointer dereferencing
98
    # issue (in Cython there is no unary operator "*").
99
    mpfr_clear((<mpfr_t*>mpfrNum)[0])
100

    
101
cpdef unsigned long int new_interval_num(unsigned int prec):
102
    """
103
    Create a newly allocated MPFI interval to precision prec.
104
    """
105
    cdef mpfi_t *newValue = NULL
106
    print "Precision : ", prec
107
    newValue = <mpfi_t*>malloc(sizeof(mpfi_t))
108
    mpfi_init2(newValue[0], prec)
109
    print "Creation done!"
110
    return(<unsigned long int>newValue)
111

    
112
cpdef unsigned long int new_mpfr_num(unsigned int prec):
113
    cdef mpfr_t *newValue = NULL
114
    print "Precision : ", prec
115
    newValue = <mpfr_t*>malloc(sizeof(mpfr_t))
116
    mpfr_init2(newValue[0], prec)
117
    print "Creation done!"
118
    return(<unsigned long int>newValue)
119

    
120
#
121

    
122
cpdef cmp_interval_values(RealIntervalFieldElement x, 
123
                         RealIntervalFieldElement y):
124
    """
125
    Compare two intervals with the mpfi_cmp function
126
    """
127
    return(mpfi_cmp(x.value, y.value))
128
 
129
cpdef cmp_rn_values(RealNumber x, RealNumber y):
130
    """
131
    Compare two RN with the mpfr_cmp function
132
    """
133
    return(mpfr_cmp(x.value, y.value))
134
 
135
# Do not use this function!
136
# It changes the precision of the instance behind the curtain, but precision is
137
# class attribute. If the precision is increased, memory space is wasted and
138
# other problems may surface in subsequent operations.
139
# If the precision is decreased the semantics
140
# are broken since the expected precision (from the class attribute) is not
141
# the actual precision.
142
#
143
cpdef s_rn_value(RealNumber x, unsigned long int p):
144
    mpfr_clear(x.value)
145
    mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0]))
146
    #x.__prec = mpfr_get_prec(x.value)
147
    printf("x.value prec: %d\n", mpfr_get_prec(x.value))
148
    mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN)