root / pobysoPythonSage / src / sageSLZ / sageRationalOperations.sage @ 129
Historique | Voir | Annoter | Télécharger (3,18 ko)
1 |
r""" |
---|---|
2 |
Some small tools needed to manipulate lists of rational numbers. |
3 |
|
4 |
AUTHORS: |
5 |
- S.T. (2013-04): initial version |
6 |
|
7 |
Examples: |
8 |
TODO |
9 |
""" |
10 |
print "sageRationalOperations loading..." |
11 |
def sro_denominators(rationalList = None): |
12 |
""" |
13 |
Compute the list of the denominators of a rational numbers list. |
14 |
""" |
15 |
# No argument. |
16 |
if rationalList is None: |
17 |
raise TypeError('numerators takes a list of rationals.') |
18 |
try: |
19 |
listLength = len(rationalList) |
20 |
except Exception: |
21 |
print "denominators:", rationalList, \ |
22 |
"does not understand the len() function." |
23 |
return([]) |
24 |
if listLength == 0: |
25 |
return([]) |
26 |
if listLength == 1: |
27 |
try: |
28 |
return([QQ(rationalList[0]).denominator()]) |
29 |
except Exception: |
30 |
print "denominators:", rationalList[0], \ |
31 |
"has no \"denominator()\" member." |
32 |
return([]) |
33 |
denominatorsList = [] |
34 |
for i in xrange(listLength): |
35 |
try: |
36 |
denominatorsList.append(QQ(rationalList[i]).denominator()) |
37 |
except Exception: |
38 |
print "denominators:", rationalList[i], \ |
39 |
"has no \"denominator()\" member." |
40 |
return([]) |
41 |
return(denominatorsList) |
42 |
# End sro_denominators |
43 |
|
44 |
def sro_lcmm(rationalList = None): |
45 |
""" |
46 |
Compute the lcm of an sequence (list, tuple) of rational numbers. |
47 |
In fact, we can (should and do) use lcm instead: it accepts a list argument. |
48 |
Kept here has a model for this kind of functions. |
49 |
""" |
50 |
# No argument. |
51 |
if rationalList is None: |
52 |
raise TypeError('lcmm takes a list of rationals') |
53 |
# Try the len function. |
54 |
try: |
55 |
listLength = len(rationalList) |
56 |
except Exception: |
57 |
print "lcmm:", rationalList, "does not understand the len() function." |
58 |
return(0) |
59 |
# An empty list: return 0 |
60 |
if listLength == 0: |
61 |
return(0) |
62 |
# Only one element, return it, wahtever it is. |
63 |
if listLength == 1: |
64 |
return(rationalList[0]) |
65 |
try: |
66 |
return(reduce(lcm, rationalList)) |
67 |
except Exception: |
68 |
print "Exception raised in lcmm!" |
69 |
return(0) |
70 |
# End sro_lcmm |
71 |
|
72 |
def sro_numerators(rationalList = None): |
73 |
""" |
74 |
Compute the list of the numerators of a rational numbers list. |
75 |
""" |
76 |
# No argument. |
77 |
if rationalList is None: |
78 |
raise TypeError('numerators takes a list of rationals.') |
79 |
try: |
80 |
listLength = len(rationalList) |
81 |
except Exception: |
82 |
print "numerators:", rationalList, \ |
83 |
"does not understand the len() function." |
84 |
return([]) |
85 |
if listLength == 0: |
86 |
return([]) |
87 |
if listLength == 1: |
88 |
try: |
89 |
return(QQ(rationalList[0]).numerator()) |
90 |
except Exception: |
91 |
print "denominators:", rationalList[0], \ |
92 |
"has no \"numerator()\" member." |
93 |
return([]) |
94 |
numeratorsList = [] |
95 |
for i in xrange(listLength): |
96 |
try: |
97 |
numeratorsList.append(QQ(rationalList[i]).numerator()) |
98 |
except Exception: |
99 |
print "numerators:", rationalList[i], \ |
100 |
"has no \"numerator()\" member." |
101 |
return([]) |
102 |
return(numeratorsList) |
103 |
# End sro_numerators |
104 |
|
105 |
print "\t...sageRationalOperations loaded" |