Statistiques
| Révision :

root / pobysoPythonSage / src / sageMpfr.spyx @ 52

Historique | Voir | Annoter | Télécharger (2,8 ko)

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

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

    
23
cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN):
24
    """
25
    Set the value of x from MPFR number p.
26
    """
27
    # We use the "array trick" to workaround the pointer dereferencing
28
    # issue (in Cython there is no unary operator "*").
29
    #
30
    mpfr_set(x.value, (<mpfr_t *>p)[0], rnd)
31
#
32
cpdef min_rn_size(RealNumber x):
33
    """
34
    Compute the minimum number of bits necessary to represent
35
    the x RealNumber's value.
36
    If x is a power of 2 (only 1 bit needed) return the MPFR
37
    MPFR_PREC_MIN value.
38
    """
39
    cdef int min_prec = mpfr_min_prec(x.value) 
40
    if min_prec < MPFR_PREC_MIN:
41
        return(MPFR_PREC_MIN)
42
    else:
43
        return(min_prec)
44

    
45
cpdef min_mpfr_size(int p):
46
    """
47
    Compute the minimum number of bits necessary to represent
48
    the MPFR value pointed by p.
49
    If x is a power of 2 (only 1 bit needed) return the MPFR
50
    MPFR_PREC_MIN value.
51
    """
52
    cdef int min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) 
53
    if min_prec < MPFR_PREC_MIN:
54
        return(MPFR_PREC_MIN)
55
    else:
56
        return(min_prec)
57

    
58
cpdef new_mpfr_num(unsigned int prec):
59
    cdef mpfr_t *newValue = NULL
60
    print "Precision : ", prec
61
    newValue = <mpfr_t*>malloc(sizeof(mpfr_t))
62
    mpfr_init2(newValue[0], prec)
63
    print "Creation done!"
64
    return(<int>newValue)
65

    
66
cpdef free_mpfr_num(int mpfrNum):
67
    mpfr_clear((<mpfr_t*>mpfrNum)[0])
68

    
69
#
70

    
71
cpdef cmp_rn_value(RealNumber x, RealNumber y):
72
    """
73
    Compare two RN with the mpfr_cmp function
74
    """
75
    return(mpfr_cmp(x.value, y.value))
76
 
77
# Do not use this function!
78
# It changes the precision of the instance behind the curtain, but precision is
79
# class attribute. If the precision is increased, memory space is wasted and
80
# other problems may surface in subsequent operations.
81
# If the precision is decreased the semantics
82
# are broken since the expected precision (from the class attribute) is not
83
# the actual precision.
84
#
85
cpdef s_rn_value(RealNumber x, int p):
86
    mpfr_clear(x.value)
87
    mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0]))
88
    #x.__prec = mpfr_get_prec(x.value)
89
    printf("x.value prec: %d\n", mpfr_get_prec(x.value))
90
    mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN)