2013-07-19 13:11:40 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
2014-02-21 05:01:34 +00:00
|
|
|
from .arithmetic import gcd
|
2016-02-14 15:34:32 +00:00
|
|
|
from .generic import isNumber, postfix_op
|
2014-11-14 15:20:02 +00:00
|
|
|
from .operator import op
|
2015-02-27 16:46:16 +00:00
|
|
|
from .expression import Expression
|
2015-02-27 10:43:20 +00:00
|
|
|
from .explicable import Explicable
|
|
|
|
from copy import copy
|
2014-12-27 15:04:30 +00:00
|
|
|
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-02-27 17:02:34 +00:00
|
|
|
__all__ = ['Fraction']
|
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
|
2015-02-27 16:46:16 +00:00
|
|
|
class Fraction(Explicable):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""Fractions!"""
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
def __init__(self, num, denom=1):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""To initiate a fraction we need a numerator and a denominator
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
:param num: the numerator
|
|
|
|
:param denom: the denominator
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
2015-02-27 10:43:20 +00:00
|
|
|
super(Fraction, self).__init__()
|
2013-08-07 23:12:11 +00:00
|
|
|
self._num = num
|
2015-04-27 17:24:20 +00:00
|
|
|
if denom == 0:
|
|
|
|
raise ZeroDivisionError("Can't create Fraction: division by zero")
|
2013-08-07 23:12:11 +00:00
|
|
|
self._denom = denom
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-11-08 17:15:04 +00:00
|
|
|
self.isNumber = 1
|
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
def simplify(self):
|
2016-02-13 03:29:26 +00:00
|
|
|
"""Simplify the fraction
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
:returns: steps to simplify the fraction or the fraction if there is nothing to do
|
2014-12-27 15:04:30 +00:00
|
|
|
|
|
|
|
>>> f = Fraction(3, 6)
|
|
|
|
>>> f.simplify()
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 1 / 2>
|
|
|
|
>>> for i in f.simplify().explain():
|
|
|
|
... print(i)
|
|
|
|
\\frac{ 3 }{ 6 }
|
|
|
|
\\frac{ 1 \\times 3 }{ 2 \\times 3 }
|
|
|
|
\\frac{ 1 }{ 2 }
|
|
|
|
>>> f = Fraction(6,9)
|
|
|
|
>>> f.simplify()
|
|
|
|
< Fraction 2 / 3>
|
|
|
|
>>> for i in f.simplify().explain():
|
|
|
|
... print(i)
|
|
|
|
\\frac{ 6 }{ 9 }
|
|
|
|
\\frac{ 2 \\times 3 }{ 3 \\times 3 }
|
|
|
|
\\frac{ 2 }{ 3 }
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f = Fraction(0,3)
|
|
|
|
>>> f.simplify()
|
2015-02-27 10:43:20 +00:00
|
|
|
0
|
2014-12-27 15:04:30 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
2015-02-27 10:43:20 +00:00
|
|
|
ini_step = [Expression(self.postfix_tokens)]
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-02-21 10:05:25 +00:00
|
|
|
if self._num == 0:
|
2016-02-13 17:54:13 +00:00
|
|
|
return Expression([0]).simplify()
|
2014-02-21 10:05:25 +00:00
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
elif isinstance(self._num, Fraction) or isinstance(self._denom, Fraction):
|
2015-06-22 07:13:07 +00:00
|
|
|
return self._num / self._denom
|
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
elif self._denom < 0:
|
2013-10-30 18:34:39 +00:00
|
|
|
n_frac = Fraction(-self._num, -self._denom)
|
2015-02-27 10:43:20 +00:00
|
|
|
ans = n_frac.simplify()
|
2016-03-02 13:58:38 +00:00
|
|
|
ans.this_append_before(ini_step)
|
2015-02-27 10:43:20 +00:00
|
|
|
return ans
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
gcd_ = gcd(abs(self._num), abs(self._denom))
|
|
|
|
if gcd_ == self._denom:
|
|
|
|
n_frac = self._num // gcd_
|
2016-02-13 17:54:13 +00:00
|
|
|
return Expression([n_frac]).simplify()
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
elif gcd_ != 1:
|
2016-02-13 04:04:08 +00:00
|
|
|
n_frac = Fraction(self._num // gcd_, self._denom // gcd_)
|
2016-03-02 13:16:18 +00:00
|
|
|
ini_step += [Expression([n_frac._num, gcd_, op.mul,
|
|
|
|
n_frac._denom, gcd_, op.mul,
|
2016-02-13 04:04:08 +00:00
|
|
|
op.div])]
|
2013-10-28 13:49:31 +00:00
|
|
|
|
2016-03-02 13:58:38 +00:00
|
|
|
n_frac.this_append_before(ini_step)
|
2015-02-27 10:43:20 +00:00
|
|
|
return n_frac
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
else:
|
2015-04-21 06:37:25 +00:00
|
|
|
return copy(self)
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-12-27 15:04:30 +00:00
|
|
|
@property
|
2015-02-27 10:43:20 +00:00
|
|
|
def postfix_tokens(self):
|
2014-12-27 15:04:30 +00:00
|
|
|
"""Postfix form of the fraction
|
|
|
|
|
|
|
|
>>> f = Fraction(3, 5)
|
2015-02-27 10:43:20 +00:00
|
|
|
>>> f.postfix_tokens
|
2016-02-27 09:29:05 +00:00
|
|
|
[3, 5, /]
|
2014-12-27 15:04:30 +00:00
|
|
|
|
|
|
|
"""
|
2013-08-07 23:12:11 +00:00
|
|
|
if self._denom == 1:
|
2014-12-27 15:04:30 +00:00
|
|
|
return [self._num]
|
2013-08-07 23:12:11 +00:00
|
|
|
else:
|
2014-12-27 15:04:30 +00:00
|
|
|
return [self._num, self._denom, op.div]
|
|
|
|
|
|
|
|
def __str__(self):
|
2015-02-27 10:43:20 +00:00
|
|
|
return str(Expression(self.postfix_tokens))
|
2013-10-28 13:49:31 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2016-02-13 04:04:08 +00:00
|
|
|
return "< Fraction {num} / {denom}>".format(
|
|
|
|
num=self._num, denom=self._denom)
|
2014-02-21 10:08:55 +00:00
|
|
|
|
|
|
|
def __float__(self):
|
|
|
|
return self._num / self._denom
|
2014-05-28 16:39:08 +00:00
|
|
|
|
|
|
|
def convert2fraction(self, other):
|
2014-05-28 17:00:23 +00:00
|
|
|
""" Convert a other into a fraction """
|
2016-02-13 04:04:08 +00:00
|
|
|
if isinstance(other, Fraction):
|
|
|
|
# cool
|
2013-08-07 23:12:11 +00:00
|
|
|
number = other
|
|
|
|
else:
|
|
|
|
number = Fraction(other)
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-05-28 16:39:08 +00:00
|
|
|
return number
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2014-05-28 16:39:08 +00:00
|
|
|
def __add__(self, other):
|
2014-12-27 15:04:30 +00:00
|
|
|
""" overload +
|
|
|
|
|
|
|
|
>>> f = Fraction(1, 2)
|
|
|
|
>>> g = Fraction(2, 3)
|
|
|
|
>>> f + g
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 7 / 6>
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f+g).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, 3, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 4, +, 6, /] >
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f + 2
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 5 / 2>
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f+2).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 1, *, 2, 1, *, /, 2, 2, *, 1, 2, *, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 4, 2, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 4 / 2>, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 4, 2, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 4, +, 2, /] >
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f = Fraction(3, 4)
|
|
|
|
>>> g = Fraction(5, 4)
|
|
|
|
>>> f + g
|
2015-02-27 10:43:20 +00:00
|
|
|
2
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f+g).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 4, /, 5, 4, /, +] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 5, +, 4, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [8, 4, /] >
|
2015-02-27 10:43:20 +00:00
|
|
|
>>> f+0
|
|
|
|
< Fraction 3 / 4>
|
|
|
|
>>> (f+0).steps
|
|
|
|
[]
|
2014-12-27 15:04:30 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2014-06-27 08:31:10 +00:00
|
|
|
if other == 0:
|
2015-02-27 10:43:20 +00:00
|
|
|
return copy(self)
|
2014-06-27 08:31:10 +00:00
|
|
|
|
2014-05-28 16:39:08 +00:00
|
|
|
number = self.convert2fraction(other)
|
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
if self._denom == number._denom:
|
|
|
|
com_denom = self._denom
|
|
|
|
num1 = self._num
|
|
|
|
num2 = number._num
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-12-28 09:47:19 +00:00
|
|
|
exp = Expression([num1, num2, op.add, com_denom, op.div])
|
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
else:
|
|
|
|
gcd_denom = gcd(self._denom, number._denom)
|
|
|
|
coef1 = number._denom // gcd_denom
|
|
|
|
coef2 = self._denom // gcd_denom
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
exp = Expression([self._num,
|
|
|
|
coef1,
|
|
|
|
op.mul,
|
|
|
|
self._denom,
|
|
|
|
coef1,
|
|
|
|
op.mul,
|
|
|
|
op.div,
|
|
|
|
number._num,
|
|
|
|
coef2,
|
|
|
|
op.mul,
|
|
|
|
number._denom,
|
|
|
|
coef2,
|
|
|
|
op.mul,
|
|
|
|
op.div,
|
|
|
|
op.add])
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
ans = exp.simplify()
|
2016-02-13 04:04:08 +00:00
|
|
|
ini_step = Expression(self.postfix_tokens +
|
|
|
|
number.postfix_tokens + [op.add])
|
2016-03-02 13:58:38 +00:00
|
|
|
ans.this_append_before([ini_step])
|
2015-02-27 10:43:20 +00:00
|
|
|
return ans
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
def __radd__(self, other):
|
2014-06-27 08:31:10 +00:00
|
|
|
if other == 0:
|
2015-02-27 10:43:20 +00:00
|
|
|
return Expression(self.postfix_tokens)
|
2014-06-27 08:31:10 +00:00
|
|
|
|
2014-05-28 16:39:08 +00:00
|
|
|
number = self.convert2fraction(other)
|
2014-02-22 06:23:42 +00:00
|
|
|
|
2014-05-28 16:49:19 +00:00
|
|
|
return number + self
|
2014-02-22 06:23:42 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
def __sub__(self, other):
|
2014-12-27 15:04:30 +00:00
|
|
|
""" overload -
|
|
|
|
|
|
|
|
>>> f = Fraction(1, 2)
|
|
|
|
>>> g = Fraction(2, 3)
|
|
|
|
>>> f - g
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction -1 / 6>
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f-g).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, 3, /, -] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, -] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, -] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, -] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, -] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 4, -, 6, /] >
|
2015-02-27 10:43:20 +00:00
|
|
|
>>> f - 0
|
|
|
|
< Fraction 1 / 2>
|
|
|
|
>>> (f-0).steps
|
|
|
|
[]
|
2014-12-27 15:04:30 +00:00
|
|
|
|
|
|
|
"""
|
2014-06-27 08:31:10 +00:00
|
|
|
if other == 0:
|
2015-02-27 10:43:20 +00:00
|
|
|
return copy(self)
|
2014-06-27 08:31:10 +00:00
|
|
|
|
2014-05-28 16:39:08 +00:00
|
|
|
number = self.convert2fraction(other)
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
if self._denom == number._denom:
|
|
|
|
com_denom = self._denom
|
|
|
|
num1 = self._num
|
|
|
|
num2 = number._num
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-12-28 09:47:19 +00:00
|
|
|
exp = Expression([num1, num2, op.sub, com_denom, op.div])
|
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
else:
|
|
|
|
gcd_denom = gcd(self._denom, number._denom)
|
|
|
|
coef1 = number._denom // gcd_denom
|
|
|
|
coef2 = self._denom // gcd_denom
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
exp = Expression([self._num,
|
|
|
|
coef1,
|
|
|
|
op.mul,
|
|
|
|
self._denom,
|
|
|
|
coef1,
|
|
|
|
op.mul,
|
|
|
|
op.div,
|
|
|
|
number._num,
|
|
|
|
coef2,
|
|
|
|
op.mul,
|
|
|
|
number._denom,
|
|
|
|
coef2,
|
|
|
|
op.mul,
|
|
|
|
op.div,
|
|
|
|
op.sub])
|
|
|
|
|
|
|
|
ini_step = Expression(self.postfix_tokens +
|
|
|
|
number.postfix_tokens + [op.sub])
|
2015-02-27 10:43:20 +00:00
|
|
|
ans = exp.simplify()
|
2016-03-02 13:58:38 +00:00
|
|
|
ans.this_append_before([ini_step])
|
2015-02-27 10:43:20 +00:00
|
|
|
return ans
|
2014-01-17 17:10:38 +00:00
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
def __rsub__(self, other):
|
2014-06-27 08:31:10 +00:00
|
|
|
if other == 0:
|
2015-02-27 10:43:20 +00:00
|
|
|
return copy(self)
|
2014-06-27 08:31:10 +00:00
|
|
|
|
2014-05-28 16:39:08 +00:00
|
|
|
number = self.convert2fraction(other)
|
2014-02-22 06:23:42 +00:00
|
|
|
|
2014-05-28 16:49:19 +00:00
|
|
|
return number - self
|
2014-02-22 06:23:42 +00:00
|
|
|
|
2014-01-17 17:10:38 +00:00
|
|
|
def __neg__(self):
|
2015-02-27 10:43:20 +00:00
|
|
|
""" overload - (as arity 1 operator)
|
2014-12-27 15:04:30 +00:00
|
|
|
|
|
|
|
>>> f = Fraction(1, 2)
|
|
|
|
>>> -f
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction -1 / 2>
|
|
|
|
>>> (-f).steps
|
|
|
|
[]
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f = Fraction(1, -2)
|
|
|
|
>>> f
|
|
|
|
< Fraction 1 / -2>
|
|
|
|
>>> -f
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 1 / 2>
|
|
|
|
>>> (-f).steps
|
2016-02-27 09:29:05 +00:00
|
|
|
[< <class 'pymath.calculus.expression.Expression'> [-1, -2, /] >]
|
2014-12-27 15:04:30 +00:00
|
|
|
|
|
|
|
"""
|
2014-11-14 16:00:26 +00:00
|
|
|
f = Fraction(-self._num, self._denom)
|
2016-02-13 04:04:08 +00:00
|
|
|
ans = f.simplify()
|
2014-12-28 09:47:19 +00:00
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
return ans
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
def __mul__(self, other):
|
2014-12-27 15:04:30 +00:00
|
|
|
""" overload *
|
|
|
|
|
|
|
|
>>> f = Fraction(1, 2)
|
|
|
|
>>> g = Fraction(2, 3)
|
|
|
|
>>> f*g
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 1 / 3>
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f*g).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 2 / 3>, *] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 2, 3, *, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 3, /] >
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f * 0
|
2015-02-27 10:43:20 +00:00
|
|
|
0
|
|
|
|
>>> (f*0).steps
|
2016-02-27 09:29:05 +00:00
|
|
|
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 0, *] >]
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f*1
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 1 / 2>
|
|
|
|
>>> (f*1).steps
|
2016-02-27 09:29:05 +00:00
|
|
|
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 1, *] >]
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f*4
|
2015-02-27 10:43:20 +00:00
|
|
|
2
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f*4).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 4, *] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 2, *, 1, 2, *, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 1, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [2, 1, /] >
|
2014-12-27 15:04:30 +00:00
|
|
|
|
|
|
|
"""
|
2016-02-14 15:16:40 +00:00
|
|
|
steps = [Expression([self, other, op.mul])]
|
2013-11-16 19:50:42 +00:00
|
|
|
|
2014-06-27 08:31:10 +00:00
|
|
|
if other == 0:
|
2016-02-14 15:16:40 +00:00
|
|
|
exp = Expression([0])
|
2014-06-27 08:31:10 +00:00
|
|
|
elif other == 1:
|
2016-03-06 15:18:01 +00:00
|
|
|
exp = copy(self)
|
2014-12-28 09:47:19 +00:00
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
elif isinstance(other, int):
|
2014-06-27 08:31:10 +00:00
|
|
|
gcd1 = gcd(other, self._denom)
|
|
|
|
if gcd1 != 1:
|
2016-02-14 15:16:40 +00:00
|
|
|
num_s = [self._num] + \
|
|
|
|
[int(other / gcd1), op.mul] * (int(other / gcd1) != 1) + \
|
|
|
|
[gcd1, op.mul]
|
|
|
|
denom_s = [int(self._denom / gcd1), gcd1, op.mul]
|
|
|
|
steps.append(Expression(num_s + denom_s + [op.div]))
|
|
|
|
|
2016-03-06 15:18:01 +00:00
|
|
|
num = [self._num] + [int(other / gcd1), op.mul] * (int(other / gcd1) != 1)
|
2016-02-14 15:16:40 +00:00
|
|
|
denom = [int(self._denom / gcd1)]
|
2014-06-27 08:31:10 +00:00
|
|
|
else:
|
2014-11-14 15:20:02 +00:00
|
|
|
num = [self._num, other, op.mul]
|
2014-06-27 08:31:10 +00:00
|
|
|
denom = [self._denom]
|
|
|
|
|
2014-12-28 09:47:19 +00:00
|
|
|
exp = Expression(num + denom + [op.div])
|
2014-05-28 18:00:39 +00:00
|
|
|
|
|
|
|
else:
|
2014-06-27 08:31:10 +00:00
|
|
|
number = self.convert2fraction(other)
|
|
|
|
|
2016-02-13 03:29:26 +00:00
|
|
|
gcd1 = gcd(self._num, number._denom)
|
2014-06-27 08:31:10 +00:00
|
|
|
if gcd1 != 1:
|
2016-02-14 15:16:40 +00:00
|
|
|
num1_s = [gcd1] + [int(self._num / gcd1), op.mul] * (int(self._num / gcd1) != 1)
|
|
|
|
denom2_s = [gcd1] + [int(number._denom / gcd1), op.mul] * (int(number._denom / gcd1) != 1)
|
|
|
|
|
2016-02-14 15:34:32 +00:00
|
|
|
num1 = [int(self._num / gcd1)] * (int(self._num / gcd1) != 1)
|
|
|
|
denom2 = [int(number._denom / gcd1)] * (int(self._denom / gcd1) != 1)
|
2014-06-27 08:31:10 +00:00
|
|
|
else:
|
2016-02-14 15:16:40 +00:00
|
|
|
num1_s = [self._num]
|
|
|
|
denom2_s = [number._denom]
|
|
|
|
|
2014-06-27 08:31:10 +00:00
|
|
|
num1 = [self._num]
|
|
|
|
denom2 = [number._denom]
|
2014-05-28 18:00:39 +00:00
|
|
|
|
2016-02-13 03:29:26 +00:00
|
|
|
gcd2 = gcd(self._denom, number._num)
|
2014-06-27 08:31:10 +00:00
|
|
|
if gcd2 != 1:
|
2016-02-14 15:16:40 +00:00
|
|
|
num2_s = [gcd2] + [int(number._num / gcd2), op.mul] * (int(number._num / gcd2) != 1)
|
|
|
|
denom1_s = [gcd2] + [int(self._denom / gcd2), op.mul] * (int(self._denom / gcd2) != 1)
|
|
|
|
|
2016-02-14 15:34:32 +00:00
|
|
|
num2 = [int(number._num / gcd2)] * (int(number._num / gcd2) != 1)
|
|
|
|
denom1 = [int(self._denom / gcd2)] * (int(number._denom / gcd2) != 1)
|
2014-06-27 08:31:10 +00:00
|
|
|
else:
|
2016-02-14 15:16:40 +00:00
|
|
|
num2_s = [number._num]
|
|
|
|
denom1_s = [self._denom]
|
|
|
|
|
2014-06-27 08:31:10 +00:00
|
|
|
num2 = [number._num]
|
|
|
|
denom1 = [self._denom]
|
2014-05-28 18:00:39 +00:00
|
|
|
|
2016-03-06 15:18:01 +00:00
|
|
|
steps.append(Expression(num1_s + num2_s + [op.mul] +
|
|
|
|
denom1_s + denom2_s + [op.mul, op.div]))
|
2016-02-14 15:16:40 +00:00
|
|
|
|
2016-02-14 15:34:32 +00:00
|
|
|
exp = Expression(postfix_op(num1 + num2, op.mul, 1) +
|
|
|
|
postfix_op(denom1 + denom2, op.mul, 1) +
|
|
|
|
[op.div])
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
ans = exp.simplify()
|
2016-02-14 15:16:40 +00:00
|
|
|
ans.steps = steps + ans.steps
|
2015-02-27 10:43:20 +00:00
|
|
|
return ans
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
def __rmul__(self, other):
|
2014-06-27 09:01:44 +00:00
|
|
|
return self * other
|
2014-02-22 06:23:42 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
def __truediv__(self, other):
|
2015-02-27 10:43:20 +00:00
|
|
|
""" overload /
|
|
|
|
|
|
|
|
>>> f = Fraction(1,2)
|
|
|
|
>>> g = Fraction(3,4)
|
|
|
|
>>> f / 0
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ZeroDivisionError: division by zero
|
|
|
|
>>> f / 1
|
|
|
|
< Fraction 1 / 2>
|
|
|
|
>>> (f/1).steps
|
|
|
|
[]
|
|
|
|
>>> f / g
|
|
|
|
< Fraction 2 / 3>
|
|
|
|
|
|
|
|
"""
|
2014-06-27 08:31:10 +00:00
|
|
|
if other == 0:
|
|
|
|
raise ZeroDivisionError("division by zero")
|
|
|
|
elif other == 1:
|
2015-02-27 10:43:20 +00:00
|
|
|
return copy(self)
|
2014-06-27 08:31:10 +00:00
|
|
|
|
2014-05-28 16:39:08 +00:00
|
|
|
number = self.convert2fraction(other)
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
ini_step = Expression(self.postfix_tokens +
|
|
|
|
number.postfix_tokens + [op.div])
|
2015-02-27 10:43:20 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
number = Fraction(number._denom, number._num)
|
2015-02-27 10:43:20 +00:00
|
|
|
ans = self * number
|
2013-08-07 23:12:11 +00:00
|
|
|
|
2016-03-02 13:58:38 +00:00
|
|
|
ans.this_append_before([ini_step])
|
2015-02-27 10:43:20 +00:00
|
|
|
return ans
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
def __rtruediv__(self, other):
|
2014-05-28 16:39:08 +00:00
|
|
|
number = self.convert2fraction(other)
|
2014-02-22 06:23:42 +00:00
|
|
|
|
2014-05-28 16:49:19 +00:00
|
|
|
return number / self
|
2014-02-22 06:23:42 +00:00
|
|
|
|
2014-12-22 18:56:58 +00:00
|
|
|
def __pow__(self, power):
|
|
|
|
""" overload **
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2014-12-22 18:56:58 +00:00
|
|
|
>>> f = Fraction(3, 4)
|
|
|
|
>>> f**0
|
2015-02-27 10:43:20 +00:00
|
|
|
1
|
|
|
|
>>> (f**0).steps
|
2016-02-27 09:29:05 +00:00
|
|
|
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 0, ^] >]
|
2014-12-22 18:56:58 +00:00
|
|
|
>>> f**1
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 3 / 4>
|
|
|
|
>>> (f**1).steps
|
2016-02-27 09:29:05 +00:00
|
|
|
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 1, ^] >]
|
2014-12-22 18:56:58 +00:00
|
|
|
>>> f**3
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 27 / 64>
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f**3).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 3, ^] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [3, 3, ^, 4, 3, ^, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [27, 64, /] >
|
2014-12-22 18:56:58 +00:00
|
|
|
>>> f = Fraction(6, 4)
|
|
|
|
>>> f**3
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 27 / 8>
|
2015-04-06 15:57:13 +00:00
|
|
|
>>> print("\\n".join([repr(i) for i in (f**3).steps]))
|
2016-02-27 09:29:05 +00:00
|
|
|
< <class 'pymath.calculus.expression.Expression'> [< Fraction 6 / 4>, 3, ^] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [6, 3, ^, 4, 3, ^, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [216, 64, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [216, 64, /] >
|
|
|
|
< <class 'pymath.calculus.expression.Expression'> [27, 8, *, 8, 8, *, /] >
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2014-12-22 18:56:58 +00:00
|
|
|
"""
|
2016-02-13 04:04:08 +00:00
|
|
|
if not isinstance(power, int):
|
|
|
|
raise ValueError(
|
|
|
|
"Can't raise fraction to power {}".format(
|
|
|
|
str(power)))
|
2014-12-22 18:56:58 +00:00
|
|
|
|
2016-02-14 15:16:40 +00:00
|
|
|
ini_step = Expression([self, power, op.pw])
|
2014-12-22 18:56:58 +00:00
|
|
|
if power == 0:
|
2016-02-14 15:16:40 +00:00
|
|
|
exp = Expression([1])
|
2014-12-22 18:56:58 +00:00
|
|
|
elif power == 1:
|
2016-02-14 15:16:40 +00:00
|
|
|
exp = copy(self)
|
2014-12-22 18:56:58 +00:00
|
|
|
else:
|
2016-02-13 04:04:08 +00:00
|
|
|
exp = Expression(
|
|
|
|
[self._num, power, op.pw, self._denom, power, op.pw, op.div])
|
2015-02-27 10:43:20 +00:00
|
|
|
|
2016-02-14 15:16:40 +00:00
|
|
|
ans = exp.simplify()
|
2016-03-02 13:58:38 +00:00
|
|
|
ans.this_append_before([ini_step])
|
2016-02-14 15:16:40 +00:00
|
|
|
return ans
|
2014-12-22 18:56:58 +00:00
|
|
|
|
|
|
|
def __xor__(self, power):
|
|
|
|
""" overload ^
|
2014-12-27 15:04:30 +00:00
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
Work like **
|
2014-12-27 15:04:30 +00:00
|
|
|
|
2014-12-22 18:56:58 +00:00
|
|
|
>>> f = Fraction(3, 4)
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f^0
|
2015-02-27 10:43:20 +00:00
|
|
|
1
|
2014-12-27 15:04:30 +00:00
|
|
|
>>> f^1
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 3 / 4>
|
2014-12-22 18:56:58 +00:00
|
|
|
>>> f^3
|
2015-02-27 10:43:20 +00:00
|
|
|
< Fraction 27 / 64>
|
2014-12-22 18:56:58 +00:00
|
|
|
"""
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2014-12-22 18:56:58 +00:00
|
|
|
return self.__pow__(power)
|
|
|
|
|
2014-02-28 07:49:03 +00:00
|
|
|
def __abs__(self):
|
|
|
|
return Fraction(abs(self._num), abs(self._denom))
|
|
|
|
|
2014-02-21 17:02:34 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
""" == """
|
2014-11-14 15:20:02 +00:00
|
|
|
if isNumber(other):
|
|
|
|
number = self.convert2fraction(other)
|
2014-02-21 17:02:34 +00:00
|
|
|
|
2014-11-14 15:20:02 +00:00
|
|
|
return self._num * number._denom == self._denom * number._num
|
|
|
|
else:
|
|
|
|
return 0
|
2014-02-21 17:02:34 +00:00
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
def __lt__(self, other):
|
2014-02-21 17:02:34 +00:00
|
|
|
""" < """
|
2014-05-28 16:52:46 +00:00
|
|
|
return float(self) < float(other)
|
2013-10-28 13:49:31 +00:00
|
|
|
|
|
|
|
def __le__(self, other):
|
2014-02-21 17:02:34 +00:00
|
|
|
""" <= """
|
2014-05-28 16:52:46 +00:00
|
|
|
return float(self) <= float(other)
|
|
|
|
|
|
|
|
def __gt__(self, other):
|
|
|
|
""" > """
|
|
|
|
return float(self) > float(other)
|
|
|
|
|
|
|
|
def __ge__(self, other):
|
|
|
|
""" >= """
|
|
|
|
return float(self) >= float(other)
|
2013-07-19 13:11:40 +00:00
|
|
|
|
2015-02-27 10:43:20 +00:00
|
|
|
def __copy__(self):
|
|
|
|
""" Copying the fraction removing steps where it is from """
|
|
|
|
return Fraction(self._num, self._denom)
|
|
|
|
|
2013-07-19 13:11:40 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
2016-02-13 03:29:26 +00:00
|
|
|
# cursor: 16 del
|