root / pobysoPythonSage / src / sageSLZ / sageRationalOperations.sage @ 93
Historique | Voir | Annoter | Télécharger (3,18 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 |
print "sageRationalOperations loading..." |
| 9 |
def sro_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 |
# End sro_denominators |
| 41 |
|
| 42 |
def sro_lcmm(rationalList = None): |
| 43 |
""" |
| 44 |
Compute the lcm of an sequence (list, tuple) of rational numbers. |
| 45 |
In fact, we can (should and do) use lcm instead: it accepts a list argument. |
| 46 |
Kept here has a model for this kind of functions. |
| 47 |
""" |
| 48 |
# No argument. |
| 49 |
if rationalList is None: |
| 50 |
raise TypeError('lcmm takes a list of rationals')
|
| 51 |
# Try the len function. |
| 52 |
try: |
| 53 |
listLength = len(rationalList) |
| 54 |
except Exception: |
| 55 |
print "lcmm:", rationalList, "does not understand the len() function." |
| 56 |
return(0) |
| 57 |
# An empty list: return 0 |
| 58 |
if listLength == 0: |
| 59 |
return(0) |
| 60 |
# Only one element, return it, wahtever it is. |
| 61 |
if listLength == 1: |
| 62 |
return(rationalList[0]) |
| 63 |
try: |
| 64 |
return(reduce(lcm, rationalList)) |
| 65 |
except Exception: |
| 66 |
print "Exception raised in lcmm!" |
| 67 |
return(0) |
| 68 |
# End sro_lcmm |
| 69 |
|
| 70 |
def sro_numerators(rationalList = None): |
| 71 |
""" |
| 72 |
Compute the list of the numerators of a rational numbers list. |
| 73 |
""" |
| 74 |
# No argument. |
| 75 |
if rationalList is None: |
| 76 |
raise TypeError('numerators takes a list of rationals.')
|
| 77 |
try: |
| 78 |
listLength = len(rationalList) |
| 79 |
except Exception: |
| 80 |
print "numerators:", rationalList, \ |
| 81 |
"does not understand the len() function." |
| 82 |
return([]) |
| 83 |
if listLength == 0: |
| 84 |
return([]) |
| 85 |
if listLength == 1: |
| 86 |
try: |
| 87 |
return(QQ(rationalList[0]).numerator()) |
| 88 |
except Exception: |
| 89 |
print "denominators:", rationalList[0], \ |
| 90 |
"has no \"numerator()\" member." |
| 91 |
return([]) |
| 92 |
numeratorsList = [] |
| 93 |
for i in xrange(listLength): |
| 94 |
try: |
| 95 |
numeratorsList.append(QQ(rationalList[i]).numerator()) |
| 96 |
except Exception: |
| 97 |
print "numerators:", rationalList[i], \ |
| 98 |
"has no \"numerator()\" member." |
| 99 |
return([]) |
| 100 |
return(numeratorsList) |
| 101 |
# End sro_numerators |
| 102 |
|
| 103 |
print "\t...sageRationalOperations loaded" |