Statistiques
| Révision :

root / pobysoPythonSage / src / sageMpfr.spyx @ 130

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

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