Tue, 02 Feb 2016 20:47:47 +0200
Stuff
164 | 1 | def gcd(a, b): |
2 | """ | |
3 | Calculate the Greatest Common Divisor of a and b. | |
4 | ||
5 | Unless b==0, the result will have the same sign as b (so that when | |
6 | b is divided by it, the result comes out positive). | |
7 | """ | |
8 | while b: | |
9 | a, b = b, a % b | |
10 | return a | |
11 | ||
12 | class Fraction (object): | |
13 | def __init__ (self, numerator, denominator = 1): | |
14 | if denominator == 0: | |
15 | raise ValueError ('Denominator may not be 0') | |
16 | ||
17 | self.numerator = int (numerator) | |
18 | self.denominator = int (denominator) | |
19 | self.simplify() | |
20 | ||
21 | @property | |
22 | def real (self): | |
23 | if self.numerator.real: | |
24 | return Fraction(self.numerator.real, self.denominator.real) | |
25 | else: | |
26 | return Fraction(0,1) | |
27 | ||
28 | @property | |
29 | def imag (self): | |
30 | if self.numerator.imag: | |
31 | return Fraction(self.numerator.imag, self.denominator.imag) | |
32 | else: | |
33 | return Fraction(0,1) | |
34 | ||
35 | def simplify (self): | |
36 | divisor = gcd (self.numerator, self.denominator) | |
37 | self.numerator //= divisor | |
38 | self.denominator //= divisor | |
39 | ||
40 | def __repr__ (self): | |
41 | return '%s(%d, %d)' % (self.__class__.__name__, self.numerator, self.denominator) | |
42 | ||
43 | def __str__ (self): | |
44 | if self.numerator > self.denominator: | |
45 | if self.numerator % self.denominator == 0: | |
46 | return str (self.numerator // self.denominator) | |
47 | else: | |
48 | return '%d + (%d / %d)' % (self.numerator // self.denominator, | |
49 | self.numerator % self.denominator, self.denominator) | |
50 | else: | |
51 | return str (self.numerator) + '/' + str (self.denominator) | |
52 | ||
53 | def __add__ (self, other): | |
54 | if isinstance (other, Fraction): | |
55 | n1, n2, d1, d2 = (self.numerator, other.numerator, self.denominator, other.denominator) | |
56 | return Fraction ((n1 * d2) + (n2 * d1), d1 * d2) | |
57 | else: | |
58 | return Fraction (self.numerator + (other * self.denominator), self.denominator) | |
59 | ||
60 | def __radd__ (self, other): | |
61 | return self + other | |
62 | ||
63 | def __sub__ (self, other): | |
64 | if isinstance (other, Fraction): | |
65 | return self.__add__ (Fraction (-other.numerator, other.denominator)) | |
66 | else: | |
67 | return self.__add__ (-other) | |
68 | ||
69 | def __rsub__ (self, other): | |
70 | return -self + other | |
71 | ||
72 | def __mul__ (self, other): | |
73 | if isinstance (other, Fraction): | |
74 | return Fraction (self.numerator * other.numerator, self.denominator * other.denominator) | |
75 | elif isinstance (other, float): | |
76 | return (self.numerator / self.denominator) * other | |
77 | elif isinstance (other, int): | |
78 | return Fraction (self.numerator * int (other), self.denominator) | |
79 | ||
80 | def __rmul__ (self, other): | |
81 | return self * other | |
82 | ||
83 | def __truediv__ (self, other): | |
84 | if isinstance (other, Fraction): | |
85 | return Fraction (self.numerator * other.denominator, self.denominator * other.numerator) | |
86 | else: | |
87 | return Fraction (self.numerator, self.denominator * int (other)) | |
88 | ||
89 | def __rtruediv__ (self, other): | |
90 | return other * Fraction (self.denominator, self.numerator) | |
91 | ||
92 | def __neg__ (self): | |
93 | return Fraction (-self.numerator, self.denominator) | |
94 | ||
95 | def __abs__ (self): | |
96 | return Fraction (abs (self.numerator), self.denominator) |