Statistiques
| Révision :

root / pobysoPythonSage / src / sageMpfr.spyx @ 81

Historique | Voir | Annoter | Télécharger (3,65 ko)

1
from sage.rings.real_mpfr cimport *
2
from sage.rings.real_mpfi cimport *
3
#
4
# Functions to manipulate the real RealNumber values.
5
#
6
# Import constants and declarations that are not exported
7
# from the Sage real_mpfr.pyx file.
8
# Notes:
9
# - notice how #define constants are imported.
10
# - mpfr_min_prec is declared with an unsigned int return value, not
11
#   a mpfr_prec_t since it is not recognized as type in Cython.
12
cdef extern from "mpfr.h":
13
    cdef int MPFR_PREC_MIN "MPFR_PREC_MIN"
14
    cdef unsigned int mpfr_min_prec(mpfr_t x)
15

    
16
cpdef int get_interval_value(RealIntervalFieldElement x):
17
    """
18
    Get the address of the value of a RealIntervalField Element. 
19
    This address can be used to to set the value as well.
20
    """
21
    return(<int>&(x.value))
22

    
23
cpdef int get_rn_value(RealNumber x):
24
    """
25
    Get the address of the value of a RealNumber. This address can be
26
    used to to set the value as well.
27
    """
28
    return(<int>&(x.value))
29
    
30
cpdef set_interval_value(RealIntervalFieldElement x, int p):
31
    """
32
    Set the value of x from MPFI interval p.
33
    """
34
    # We use the "array trick" to workaround the pointer dereferencing
35
    # issue (in Cython there is no unary operator "*").
36
    #
37
    mpfi_set(x.value, (<mpfi_t *>p)[0])
38

    
39
cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN):
40
    """
41
    Set the value of x from MPFR number p.
42
    """
43
    # We use the "array trick" to workaround the pointer dereferencing
44
    # issue (in Cython there is no unary operator "*").
45
    #
46
    mpfr_set(x.value, (<mpfr_t *>p)[0], rnd)
47
#
48
cpdef min_rn_size(RealNumber x):
49
    """
50
    Compute the minimum number of bits necessary to represent
51
    the x RealNumber's value.
52
    If x is a power of 2 (only 1 bit needed) return the MPFR
53
    MPFR_PREC_MIN value.
54
    """
55
    cdef int min_prec = mpfr_min_prec(x.value) 
56
    if min_prec < MPFR_PREC_MIN:
57
        return(MPFR_PREC_MIN)
58
    else:
59
        return(min_prec)
60

    
61
cpdef min_mpfr_size(int p):
62
    """
63
    Compute the minimum number of bits necessary to represent
64
    the MPFR value pointed by p.
65
    If x is a power of 2 (only 1 bit needed) return the MPFR
66
    MPFR_PREC_MIN value.
67
    """
68
    cdef int min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) 
69
    if min_prec < MPFR_PREC_MIN:
70
        return(MPFR_PREC_MIN)
71
    else:
72
        return(min_prec)
73

    
74
cpdef free_mpfi_num(int mpfiNum):
75
    mpfi_clear((<mpfi_t*>mpfiNum)[0])
76

    
77
cpdef free_mpfr_num(int mpfrNum):
78
    mpfr_clear((<mpfr_t*>mpfrNum)[0])
79

    
80
cpdef new_interval_num(unsigned int prec):
81
    cdef mpfi_t *newValue = NULL
82
    print "Precision : ", prec
83
    newValue = <mpfi_t*>malloc(sizeof(mpfi_t))
84
    mpfi_init2(newValue[0], prec)
85
    print "Creation done!"
86
    return(<int>newValue)
87

    
88
cpdef new_mpfr_num(unsigned int prec):
89
    cdef mpfr_t *newValue = NULL
90
    print "Precision : ", prec
91
    newValue = <mpfr_t*>malloc(sizeof(mpfr_t))
92
    mpfr_init2(newValue[0], prec)
93
    print "Creation done!"
94
    return(<int>newValue)
95

    
96
#
97

    
98
cpdef cmp_rn_value(RealNumber x, RealNumber y):
99
    """
100
    Compare two RN with the mpfr_cmp function
101
    """
102
    return(mpfr_cmp(x.value, y.value))
103
 
104
# Do not use this function!
105
# It changes the precision of the instance behind the curtain, but precision is
106
# class attribute. If the precision is increased, memory space is wasted and
107
# other problems may surface in subsequent operations.
108
# If the precision is decreased the semantics
109
# are broken since the expected precision (from the class attribute) is not
110
# the actual precision.
111
#
112
cpdef s_rn_value(RealNumber x, int p):
113
    mpfr_clear(x.value)
114
    mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0]))
115
    #x.__prec = mpfr_get_prec(x.value)
116
    printf("x.value prec: %d\n", mpfr_get_prec(x.value))
117
    mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN)