Simple Rational class for testing mixed arithmetic.
| 143 | numbers.Complex.register(SymbolicComplex) |
| 144 | |
| 145 | class Rat: |
| 146 | """Simple Rational class for testing mixed arithmetic.""" |
| 147 | def __init__(self, n, d): |
| 148 | self.numerator = n |
| 149 | self.denominator = d |
| 150 | def __mul__(self, other): |
| 151 | if isinstance(other, F): |
| 152 | return NotImplemented |
| 153 | return self.__class__(self.numerator * other.numerator, |
| 154 | self.denominator * other.denominator) |
| 155 | def __rmul__(self, other): |
| 156 | return self.__class__(other.numerator * self.numerator, |
| 157 | other.denominator * self.denominator) |
| 158 | def __truediv__(self, other): |
| 159 | if isinstance(other, F): |
| 160 | return NotImplemented |
| 161 | return self.__class__(self.numerator * other.denominator, |
| 162 | self.denominator * other.numerator) |
| 163 | def __rtruediv__(self, other): |
| 164 | return self.__class__(other.numerator * self.denominator, |
| 165 | other.denominator * self.numerator) |
| 166 | def __mod__(self, other): |
| 167 | if isinstance(other, F): |
| 168 | return NotImplemented |
| 169 | d = self.denominator * other.numerator |
| 170 | return self.__class__(self.numerator * other.denominator % d, d) |
| 171 | def __rmod__(self, other): |
| 172 | d = other.denominator * self.numerator |
| 173 | return self.__class__(other.numerator * self.denominator % d, d) |
| 174 | |
| 175 | return self.__class__(other.numerator / self.numerator, |
| 176 | other.denominator / self.denominator) |
| 177 | def __pow__(self, other): |
| 178 | if isinstance(other, F): |
| 179 | return NotImplemented |
| 180 | return self.__class__(self.numerator ** other, |
| 181 | self.denominator ** other) |
| 182 | def __float__(self): |
| 183 | return self.numerator / self.denominator |
| 184 | def __eq__(self, other): |
| 185 | if self.__class__ != other.__class__: |
| 186 | return NotImplemented |
| 187 | return (typed_approx_eq(self.numerator, other.numerator) and |
| 188 | typed_approx_eq(self.denominator, other.denominator)) |
| 189 | def __repr__(self): |
| 190 | return f'{self.__class__.__name__}({self.numerator!r}, {self.denominator!r})' |
| 191 | numbers.Rational.register(Rat) |
| 192 | |
| 193 | class Root: |
no outgoing calls