Adapt Fraction to steps

This commit is contained in:
Benjamin Bertrand 2016-03-10 16:42:27 +03:00
parent c314b55376
commit fba7f16762

View File

@ -6,6 +6,7 @@ from .generic import isNumber, postfix_op
from .operator import op
from .expression import Expression
from .explicable import Explicable
from .step import Step
from copy import copy
@ -47,6 +48,8 @@ class Fraction(Explicable):
>>> f = Fraction(6,9)
>>> f.simplify()
< Fraction 2 / 3>
>>> f.simplify().steps
[< Step [6, 9, /]>, < Step [2, 3, *, 3, 3, *, /]>]
>>> for i in f.simplify().explain():
... print(i)
\\frac{ 6 }{ 9 }
@ -57,7 +60,7 @@ class Fraction(Explicable):
0
"""
ini_step = [Expression(self.postfix_tokens)]
ini_step = [Step(self.postfix_tokens)]
if self._num == 0:
return Expression([0]).simplify()
@ -78,9 +81,11 @@ class Fraction(Explicable):
elif gcd_ != 1:
n_frac = Fraction(self._num // gcd_, self._denom // gcd_)
ini_step += [Expression([n_frac._num, gcd_, op.mul,
n_frac._denom, gcd_, op.mul,
op.div])]
ini_step += [Step([
n_frac._num, gcd_, op.mul,
n_frac._denom, gcd_, op.mul,
op.div
])]
n_frac.this_append_before(ini_step)
return n_frac
@ -129,29 +134,37 @@ class Fraction(Explicable):
>>> f + g
< Fraction 7 / 6>
>>> print("\\n".join([repr(i) for i in (f+g).steps]))
< Expression [1, 2, /, 2, 3, /, +]>
< Expression [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, +]>
< Expression [3, 6, /, 4, 6, /, +]>
< Expression [< Fraction 3 / 6>, < Fraction 4 / 6>, +]>
< Expression [3, 6, /, 4, 6, /, +]>
< Expression [3, 4, +, 6, /]>
< Step [1, 2, /, 2, 3, /, +]>
< Step [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, +]>
< Step [3, 6, /, 4, 6, /, +]>
< Step [3, 6, /, 4, 6, /, +]>
< Step [3, 6, /, 4, 6, /, +]>
< Step [3, 6, /, 4, 6, /, +]>
< Step [3, 6, /, 4, 6, /, +]>
< Step [3, 4, +, 6, /]>
< Step [7, 6, /]>
>>> f + 2
< Fraction 5 / 2>
>>> print("\\n".join([repr(i) for i in (f+2).steps]))
< Expression [1, 2, /, 2, +]>
< Expression [1, 1, *, 2, 1, *, /, 2, 2, *, 1, 2, *, /, +]>
< Expression [1, 2, /, 4, 2, /, +]>
< Expression [< Fraction 1 / 2>, < Fraction 4 / 2>, +]>
< Expression [1, 2, /, 4, 2, /, +]>
< Expression [1, 4, +, 2, /]>
< Step [1, 2, /, 2, +]>
< Step [1, 1, *, 2, 1, *, /, 2, 2, *, 1, 2, *, /, +]>
< Step [1, 2, /, 4, 2, /, +]>
< Step [1, 2, /, 4, 2, /, +]>
< Step [1, 2, /, 4, 2, /, +]>
< Step [1, 2, /, 4, 2, /, +]>
< Step [1, 2, /, 4, 2, /, +]>
< Step [1, 4, +, 2, /]>
< Step [5, 2, /]>
>>> f = Fraction(3, 4)
>>> g = Fraction(5, 4)
>>> f + g
2
>>> print("\\n".join([repr(i) for i in (f+g).steps]))
< Expression [3, 4, /, 5, 4, /, +]>
< Expression [3, 5, +, 4, /]>
< Expression [8, 4, /]>
< Step [3, 4, /, 5, 4, /, +]>
< Step [3, 5, +, 4, /]>
< Step [8, 4, /]>
< Step [8, 4, /]>
< Step [8, 4, /]>
>>> f+0
< Fraction 3 / 4>
>>> (f+0).steps
@ -193,7 +206,7 @@ class Fraction(Explicable):
op.add])
ans = exp.simplify()
ini_step = Expression(self.postfix_tokens +
ini_step = Step(self.postfix_tokens +
number.postfix_tokens + [op.add])
ans.this_append_before([ini_step])
return ans
@ -214,12 +227,15 @@ class Fraction(Explicable):
>>> f - g
< Fraction -1 / 6>
>>> print("\\n".join([repr(i) for i in (f-g).steps]))
< Expression [1, 2, /, 2, 3, /, -]>
< Expression [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, -]>
< Expression [3, 6, /, 4, 6, /, -]>
< Expression [< Fraction 3 / 6>, < Fraction 4 / 6>, -]>
< Expression [3, 6, /, 4, 6, /, -]>
< Expression [3, 4, -, 6, /]>
< Step [1, 2, /, 2, 3, /, -]>
< Step [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, -]>
< Step [3, 6, /, 4, 6, /, -]>
< Step [3, 6, /, 4, 6, /, -]>
< Step [3, 6, /, 4, 6, /, -]>
< Step [3, 6, /, 4, 6, /, -]>
< Step [3, 6, /, 4, 6, /, -]>
< Step [3, 4, -, 6, /]>
< Step [-1, 6, /]>
>>> f - 0
< Fraction 1 / 2>
>>> (f-0).steps
@ -259,7 +275,7 @@ class Fraction(Explicable):
op.div,
op.sub])
ini_step = Expression(self.postfix_tokens +
ini_step = Step(self.postfix_tokens +
number.postfix_tokens + [op.sub])
ans = exp.simplify()
ans.this_append_before([ini_step])
@ -287,7 +303,7 @@ class Fraction(Explicable):
>>> -f
< Fraction 1 / 2>
>>> (-f).steps
[< Expression [-1, -2, /]>]
[< Step [-1, -2, /]>]
"""
f = Fraction(-self._num, self._denom)
@ -303,27 +319,30 @@ class Fraction(Explicable):
>>> f*g
< Fraction 1 / 3>
>>> print("\\n".join([repr(i) for i in (f*g).steps]))
< Expression [< Fraction 1 / 2>, < Fraction 2 / 3>, *]>
< Expression [1, 2, *, 2, 3, *, /]>
< Expression [1, 3, /]>
< Step [1, 2, /, 2, 3, /, *]>
< Step [1, 2, *, 2, 3, *, /]>
< Step [1, 3, /]>
< Step [1, 3, /]>
>>> f * 0
0
>>> (f*0).steps
[< Expression [< Fraction 1 / 2>, 0, *]>]
[< Step [1, 2, /, 0, *]>]
>>> f*1
< Fraction 1 / 2>
>>> (f*1).steps
[< Expression [< Fraction 1 / 2>, 1, *]>]
[< Step [1, 2, /, 1, *]>]
>>> f*4
2
>>> print("\\n".join([repr(i) for i in (f*4).steps]))
< Expression [< Fraction 1 / 2>, 4, *]>
< Expression [1, 2, *, 2, *, 1, 2, *, /]>
< Expression [1, 2, *, 1, /]>
< Expression [2, 1, /]>
< Step [1, 2, /, 4, *]>
< Step [1, 2, *, 2, *, 1, 2, *, /]>
< Step [1, 2, *, 1, /]>
< Step [2, 1, /]>
< Step [2, 1, /]>
< Step [2]>
"""
steps = [Expression([self, other, op.mul])]
steps = [Step([self, other, op.mul])]
if other == 0:
exp = Expression([0])
@ -337,7 +356,7 @@ class Fraction(Explicable):
[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]))
steps.append(Step(num_s + denom_s + [op.div]))
num = [self._num] + [int(other / gcd1), op.mul] * (int(other / gcd1) != 1)
denom = [int(self._denom / gcd1)]
@ -378,7 +397,7 @@ class Fraction(Explicable):
num2 = [number._num]
denom1 = [self._denom]
steps.append(Expression(num1_s + num2_s + [op.mul] +
steps.append(Step(num1_s + num2_s + [op.mul] +
denom1_s + denom2_s + [op.mul, op.div]))
exp = Expression(postfix_op(num1 + num2, op.mul, 1) +
@ -416,7 +435,7 @@ class Fraction(Explicable):
number = self.convert2fraction(other)
ini_step = Expression(self.postfix_tokens +
ini_step = Step(self.postfix_tokens +
number.postfix_tokens + [op.div])
number = Fraction(number._denom, number._num)
@ -437,26 +456,30 @@ class Fraction(Explicable):
>>> f**0
1
>>> (f**0).steps
[< Expression [< Fraction 3 / 4>, 0, ^]>]
[< Step [3, 4, /, 0, ^]>]
>>> f**1
< Fraction 3 / 4>
>>> (f**1).steps
[< Expression [< Fraction 3 / 4>, 1, ^]>]
[< Step [3, 4, /, 1, ^]>]
>>> f**3
< Fraction 27 / 64>
>>> print("\\n".join([repr(i) for i in (f**3).steps]))
< Expression [< Fraction 3 / 4>, 3, ^]>
< Expression [3, 3, ^, 4, 3, ^, /]>
< Expression [27, 64, /]>
< Step [3, 4, /, 3, ^]>
< Step [3, 3, ^, 4, 3, ^, /]>
< Step [27, 64, /]>
< Step [27, 64, /]>
< Step [27, 64, /]>
>>> f = Fraction(6, 4)
>>> f**3
< Fraction 27 / 8>
>>> print("\\n".join([repr(i) for i in (f**3).steps]))
< Expression [< Fraction 6 / 4>, 3, ^]>
< Expression [6, 3, ^, 4, 3, ^, /]>
< Expression [216, 64, /]>
< Expression [216, 64, /]>
< Expression [27, 8, *, 8, 8, *, /]>
< Step [6, 4, /, 3, ^]>
< Step [6, 3, ^, 4, 3, ^, /]>
< Step [216, 64, /]>
< Step [216, 64, /]>
< Step [216, 64, /]>
< Step [216, 64, /]>
< Step [27, 8, *, 8, 8, *, /]>
"""
if not isinstance(power, int):
@ -464,14 +487,17 @@ class Fraction(Explicable):
"Can't raise fraction to power {}".format(
str(power)))
ini_step = Expression([self, power, op.pw])
ini_step = Step([self, power, op.pw])
if power == 0:
exp = Expression([1])
elif power == 1:
exp = copy(self)
else:
exp = Expression(
[self._num, power, op.pw, self._denom, power, op.pw, op.div])
exp = Expression([
self._num, power, op.pw,
self._denom, power, op.pw,
op.div
])
ans = exp.simplify()
ans.this_append_before([ini_step])