Statistiques
| Révision :

root / pobysoPythonSage / src / sageSLZ / sageRationalOperations.sage @ 79

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

1
"""
2
Some small tools needed to manipulate lists of rational numbers
3

    
4
AUTHORS:
5

    
6
- Serge Torres: first operations set (2013-04) 
7
"""
8

    
9
def denominators(rationalList = None):
10
    """
11
    Compute the list of the denominators of a rational numbers list.
12
    """
13
    # No argument.
14
    if rationalList is None:
15
        raise TypeError('numerators takes a list of rationals.')
16
    try:
17
        listLength = len(rationalList)
18
    except Exception:
19
        print "denominators:", rationalList, \
20
                "does not understand the len() function."
21
        return([])
22
    if listLength == 0:
23
        return([])
24
    if listLength == 1:
25
        try:
26
            return([QQ(rationalList[0]).denominator()])
27
        except Exception:
28
            print "denominators:", rationalList[0], \
29
                    "has no \"denominator()\" member." 
30
            return([])
31
    denominatorsList = []    
32
    for i in xrange(listLength):
33
        try:
34
            denominatorsList.append(QQ(rationalList[i]).denominator())
35
        except Exception:
36
            print "denominators:", rationalList[i], \
37
                    "has no \"denominator()\" member." 
38
            return([])
39
    return(denominatorsList)
40

    
41
def lcmm(rationalList = None):
42
    """ 
43
    Compute the lcm of an sequence (list, tuple) of rational numbers.
44
    """
45
    # No argument.
46
    if rationalList is None:
47
        raise TypeError('lcmm takes a list of rationals')
48
    # Try the len function.
49
    try:
50
        listLength = len(rationalList)
51
    except Exception:
52
        print "lcmm:", rationalList, "does not understand the len() function."
53
        return(0)
54
    # An empty list: return 0
55
    if listLength == 0:
56
        return(0)
57
    # Only one element, return it, wahtever it is.
58
    if listLength == 1:
59
        return(rationalList[0])
60
    try:
61
        return(reduce(lcm, rationalList))
62
    except Exception:
63
        print "Exception raised in lcmm!"
64
        return(0)
65
    
66
def numerators(rationalList = None):
67
    """
68
    Compute the list of the numerators of a rational numbers list.
69
    """
70
    # No argument.
71
    if rationalList is None:
72
        raise TypeError('numerators takes a list of rationals.')
73
    try:
74
        listLength = len(rationalList)
75
    except Exception:
76
        print "numerators:", rationalList, \
77
                "does not understand the len() function."
78
        return([])
79
    if listLength == 0:
80
        return([])
81
    if listLength == 1:
82
        try:
83
            return(QQ(rationalList[0]).numerator())
84
        except Exception:
85
            print "denominators:", rationalList[0], \
86
                    "has no \"numerator()\" member." 
87
            return([])
88
    numeratorsList = []    
89
    for i in xrange(listLength):
90
        try:
91
            numeratorsList.append(QQ(rationalList[i]).numerator())
92
        except Exception:
93
            print "numerators:", rationalList[i], \
94
                    "has no \"numerator()\" member." 
95
            return([])
96
    return(numeratorsList)