Révision 160

pobysoPythonSage/src/ppsTestFunctions.sage (revision 160)
1
"""
2
@file ppsTestFunctions.sage
3

  
4
Module for functions used in all sorts of tests.
5

  
6
AUTHORS:
7
- S.T. (2015-05-03): initial version
8

  
9
TODO:
10
- almost everything!
11

  
12
"""
13
print "ppsTestFunctions loading..."
14
import inspect
15
import resource
16
import timeit
17

  
18

  
19
def pps_memory_usage():
20
    rusage_denom = RR("1024")
21
    if sys.platform == 'darwin':
22
        # ... it seems that in OSX the output is different units ...
23
        rusage_denom = rusage_denom * rusage_denom
24
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
25
    return mem
26
# End memory_usage_resource()
27

  
28
def pps_test_wrapper(func, *args, **kwargs):
29
    def wrapped():
30
        return func(*args, **kwargs)
31
    return wrapped
32

  
33
print "\t...ppsTestFunctions loaded"
34

  
pobysoPythonSage/src/testPobyso.sage (revision 160)
5 5
- S.T. (2014-02-26): initial version
6 6

  
7 7
TODO:
8
- everything!
8
- almost everything!
9 9

  
10 10
"""
11 11

  
......
20 20
    rusage_denom = RR("1024")
21 21
    if sys.platform == 'darwin':
22 22
        # ... it seems that in OSX the output is different units ...
23
        rusage_denom = rusage_denom * rusage_denom
23
        rusage_denom = rusage_denom^2
24 24
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
25 25
    return mem
26 26
# End memory_usage_resource()
pobysoPythonSage/src/testSageMpfr.sage (revision 160)
1
"""
2
@file testSageMpfr.sage
3
Test module for pobyso.py functions.
4

  
5
AUTHORS:
6
- S.T. (2015-05-04): initial version
7

  
8
TODO:
9
- almost everything!
10

  
11
"""
12
print "\ntestSageMpfr loading..."
13
attach(str('/home/storres/recherche/arithmetique/pobysoPythonSage/src/ppsTestFunctions.sage'))
14
#
15

  
16
def test_get_interval_value(repeat=10, number=100):
17
    functionName =  inspect.stack()[0][3]
18
    print "Running", inspect.stack()[0][3], "..."
19
    intervalSa = RIF(1,2)
20
    #
21
    def test(intervalSa):
22
        intervalSaValue = get_interval_value(intervalSa)
23
    #
24
    wrapped = pps_test_wrapper(test,intervalSa)
25
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))        
26
    print "\t...", functionName, "done."
27
    return timing
28

  
29
print "\t...testSageMpfr loaded"
pobysoPythonSage/src/sageMpfr.spyx (revision 160)
1 1
#cimport cython 
2
from libc.stdlib cimport malloc,free
2
from libc.stdlib          cimport malloc,free
3 3
from sage.rings.real_mpfr cimport *
4 4
from sage.rings.real_mpfi cimport *
5 5
#
......
9 9
# from the Sage real_mpfr.pyx file.
10 10
# Notes:
11 11
# - notice how #define constants are imported.
12
# - mpfr_min_prec() is declared with an unsigned int return value, not
13
#   a mpfr_prec_t since it is not recognized as type in Cython.
12
# - before mpfr_min_prec() is declared, we need to "ctypedef" mpfr_prec_t
13
#   if we want to be consistent with MPFR notations. The actual type comes 
14
#   from mpfr.h but, according to documentation, any type of same general 
15
#   kind would have been OK.
14 16
cdef extern from "mpfr.h":
15
    cdef int MPFR_PREC_MIN "MPFR_PREC_MIN"
16
    cdef unsigned int mpfr_min_prec(mpfr_t x)
17
    ctypedef unsigned short mpfr_prec_t
18
    cdef mpfr_prec_t MPFR_PREC_MIN "MPFR_PREC_MIN"
19
    cdef mpfr_prec_t mpfr_min_prec(mpfr_t x)
17 20

  
18 21
cpdef int get_interval_value(RealIntervalFieldElement x):
19 22
    """
......
44 47
    """
45 48
    # We use the "array trick" to workaround the pointer dereferencing
46 49
    # issue (in Cython there is no unary operator "*").
47
    #
48 50
    mpfr_set(x.value, (<mpfr_t *>p)[0], rnd)
49 51
#
50
cpdef min_rn_size(RealNumber x):
52
cpdef mpfr_prec_t min_rn_size(RealNumber x):
51 53
    """
52 54
    Compute the minimum number of bits necessary to represent
53
    the x RealNumber's value.
55
    the x Sage RealNumber's value.
54 56
    If x is a power of 2 (only 1 bit needed) return the MPFR
55 57
    MPFR_PREC_MIN value.
56 58
    """
57
    cdef int min_prec = mpfr_min_prec(x.value) 
59
    cdef mpfr_prec_t min_prec = mpfr_min_prec(x.value) 
58 60
    if min_prec < MPFR_PREC_MIN:
59 61
        return(MPFR_PREC_MIN)
60 62
    else:
61 63
        return(min_prec)
62

  
64
#
63 65
cpdef min_mpfr_size(int p):
64 66
    """
65 67
    Compute the minimum number of bits necessary to represent
66
    the MPFR value pointed by p.
67
    If x is a power of 2 (only 1 bit needed) return the MPFR
68
    the MPFR value pointed by the mpfr_t pointer p (hence the int type).
69
    If *p is a power of 2 (only 1 bit needed) return the MPFR
68 70
    MPFR_PREC_MIN value.
69 71
    """
70
    cdef int min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) 
72
    # We use the "array trick" to workaround the pointer dereferencing
73
    # issue (in Cython there is no unary operator "*").
74
    cdef mpfr_prec_t min_prec = mpfr_min_prec((<mpfr_t *>p)[0]) 
71 75
    if min_prec < MPFR_PREC_MIN:
72 76
        return(MPFR_PREC_MIN)
73 77
    else:
74 78
        return(min_prec)
75 79

  
76 80
cpdef free_mpfi_num(int mpfiNum):
81
    """
82
    Free a MPFI interval pointed by mpfiNum (hece the int type).
83
    """
84
    # We use the "array trick" to workaround the pointer dereferencing
85
    # issue (in Cython there is no unary operator "*").
77 86
    mpfi_clear((<mpfi_t*>mpfiNum)[0])
78 87

  
79 88
cpdef free_mpfr_num(int mpfrNum):
89
    """
90
    Free a MPFR number pointed by mpfrNum (hece the int type).
91
    """
92
    # We use the "array trick" to workaround the pointer dereferencing
93
    # issue (in Cython there is no unary operator "*").
80 94
    mpfr_clear((<mpfr_t*>mpfrNum)[0])
81 95

  
82
cpdef new_interval_num(unsigned int prec):
96
cpdef int new_interval_num(unsigned int prec):
97
    """
98
    Create a newly allocated MPFI interval to precision prec.
99
    """
83 100
    cdef mpfi_t *newValue = NULL
84 101
    print "Precision : ", prec
85 102
    newValue = <mpfi_t*>malloc(sizeof(mpfi_t))
......
87 104
    print "Creation done!"
88 105
    return(<int>newValue)
89 106

  
90
cpdef new_mpfr_num(unsigned int prec):
107
cpdef int new_mpfr_num(unsigned int prec):
91 108
    cdef mpfr_t *newValue = NULL
92 109
    print "Precision : ", prec
93 110
    newValue = <mpfr_t*>malloc(sizeof(mpfr_t))
......
103 120
    """
104 121
    return(mpfr_cmp(x.value, y.value))
105 122
 
123
cpdef cmp_interval_value(RealIntervalFieldElement x, 
124
                         RealIntervalFieldElement y):
125
    """
126
    Compare two intervals with the mpfi_cmp function
127
    """
128
    return(mpfi_cmp(x.value, y.value))
129
 
106 130
# Do not use this function!
107 131
# It changes the precision of the instance behind the curtain, but precision is
108 132
# class attribute. If the precision is increased, memory space is wasted and

Formats disponibles : Unified diff