root / pobysoPythonSage / src / sageSLZ / sageRationalOperations.sage @ 69
Historique | Voir | Annoter | Télécharger (2,76 ko)
1 |
def lcmm(rationalList = None): |
---|---|
2 |
""" |
3 |
Compute the lcm of an sequence (list, tuple) of numbers |
4 |
""" |
5 |
# No argument. |
6 |
if rationalList is None: |
7 |
raise TypeError('lcmm takes a list of rationals') |
8 |
# Try the len function. |
9 |
try: |
10 |
listLength = len(rationalList) |
11 |
except Exception: |
12 |
print "lcmm:", rationalList, "does not understand the len() function." |
13 |
return(0) |
14 |
# An empty list: return 0 |
15 |
if listLength == 0: |
16 |
return(0) |
17 |
# Only one element, return it, wahtever it is. |
18 |
if listLength == 1: |
19 |
return(rationalList[0]) |
20 |
try: |
21 |
return(reduce(lcm, rationalList)) |
22 |
except Exception: |
23 |
print "Exception raised in lcmm!" |
24 |
return(0) |
25 |
|
26 |
def denominators(rationalList = None): |
27 |
""" |
28 |
Compute the list of the denominators of a rational numbers list. |
29 |
""" |
30 |
# No argument. |
31 |
if rationalList is None: |
32 |
raise TypeError('numerators takes a list of rationals.') |
33 |
try: |
34 |
listLength = len(rationalList) |
35 |
except Exception: |
36 |
print "denominators:", rationalList, \ |
37 |
"does not understand the len() function." |
38 |
return([]) |
39 |
if listLength == 0: |
40 |
return([]) |
41 |
if listLength == 1: |
42 |
try: |
43 |
return(QQ(rationalList[0]).denominator()) |
44 |
except Exception: |
45 |
print "denominators:", rationalList[0], \ |
46 |
"has no \"denominator()\" member." |
47 |
return([]) |
48 |
denominatorsList = [] |
49 |
for i in xrange(listLength): |
50 |
try: |
51 |
denominatorsList.append(QQ(rationalList[i]).denominator()) |
52 |
except Exception: |
53 |
print "denominators:", rationalList[i], \ |
54 |
"has no \"denominator()\" member." |
55 |
return([]) |
56 |
return(denominatorsList) |
57 |
|
58 |
def numerators(rationalList = None): |
59 |
""" |
60 |
Compute the list of the numerators of a rational numbers list. |
61 |
""" |
62 |
# No argument. |
63 |
if rationalList is None: |
64 |
raise TypeError('numerators takes a list of rationals.') |
65 |
try: |
66 |
listLength = len(rationalList) |
67 |
except Exception: |
68 |
print "numerators:", rationalList, \ |
69 |
"does not understand the len() function." |
70 |
return([]) |
71 |
if listLength == 0: |
72 |
return([]) |
73 |
if listLength == 1: |
74 |
try: |
75 |
return(QQ(rationalList[0]).numerator()) |
76 |
except Exception: |
77 |
print "denominators:", rationalList[0], \ |
78 |
"has no \"numerator()\" member." |
79 |
return([]) |
80 |
numeratorsList = [] |
81 |
for i in xrange(listLength): |
82 |
try: |
83 |
numeratorsList.append(QQ(rationalList[i]).numerator()) |
84 |
except Exception: |
85 |
print "numerators:", rationalList[i], \ |
86 |
"has no \"numerator()\" member." |
87 |
return([]) |
88 |
return(numeratorsList) |