rewrite expression.simplify and adapt Fraction to it
This commit is contained in:
parent
f968a25345
commit
bfafaac319
@ -103,30 +103,32 @@ class Expression(Explicable):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(expression.postfix_tokens) == 1:
|
# if len(expression.postfix_tokens) == 1:
|
||||||
token = expression.postfix_tokens[0]
|
# token = expression.postfix_tokens[0]
|
||||||
|
|
||||||
if isinstance(token, Explicable_int) or isinstance(token, int):
|
# if isinstance(token, Explicable_int) or isinstance(token, int):
|
||||||
return Explicable_int(token)
|
# return Explicable_int(token)
|
||||||
|
|
||||||
# TODO: J'en arrive au soucis même soucis qu'avec les fractions qui une fois simplifiée devrait être des Explicable_int. Mais comment on ne redéfini pas les opérations, ce sont les opérations des int qui se font et donc on perd toute l'historique. |sam. févr. 13 18:57:45 EAT 2016
|
# # TODO: J'en arrive au soucis même soucis qu'avec les fractions qui une fois simplifiée devrait être des Explicable_int. Mais comment on ne redéfini pas les opérations, ce sont les opérations des int qui se font et donc on perd toute l'historique. |sam. févr. 13 18:57:45 EAT 2016
|
||||||
if isinstance(token, Explicable_float) or isinstance(token, float):
|
# if isinstance(token, Explicable_float) or isinstance(token, float):
|
||||||
return Explicable_float(token)
|
# return Explicable_float(token)
|
||||||
|
|
||||||
elif hasattr(token, 'simplify') and hasattr(token, 'explain'):
|
# elif hasattr(token, 'simplify') and hasattr(token, 'explain'):
|
||||||
ans = expression.postfix_tokens[0]
|
# ans = expression.postfix_tokens[0]
|
||||||
return ans
|
# return ans
|
||||||
|
|
||||||
elif isinstance(token, str):
|
# elif isinstance(token, str):
|
||||||
from .polynom import Polynom
|
# from .polynom import Polynom
|
||||||
return Polynom([0,1], letter = token)
|
# return Polynom([0,1], letter = token)
|
||||||
|
|
||||||
else:
|
# else:
|
||||||
raise ValueError(
|
# raise ValueError(
|
||||||
"Unknow token type in Expression: {}".format(
|
# "Unknow token type in Expression: {}".format(
|
||||||
type(token)))
|
# type(token)))
|
||||||
|
|
||||||
else:
|
# else:
|
||||||
|
# expression._isExpression = 1
|
||||||
|
# return expression
|
||||||
expression._isExpression = 1
|
expression._isExpression = 1
|
||||||
return expression
|
return expression
|
||||||
|
|
||||||
@ -144,14 +146,29 @@ class Expression(Explicable):
|
|||||||
|
|
||||||
def simplify(self):
|
def simplify(self):
|
||||||
""" Compute entirely the expression and return the result with .steps attribute """
|
""" Compute entirely the expression and return the result with .steps attribute """
|
||||||
|
try:
|
||||||
self.compute_exp()
|
self.compute_exp()
|
||||||
|
except ComputeError:
|
||||||
|
try:
|
||||||
|
self.simplified = self.postfix_tokens[0].simplify()
|
||||||
|
except AttributeError:
|
||||||
|
if isinstance(self.postfix_tokens[0],int):
|
||||||
|
self.simplified = Explicable_int(self.postfix_tokens[0])
|
||||||
|
elif isinstance(self.postfix_tokens[0],float):
|
||||||
|
self.simplified = Explicable_float(self.postfix_tokens[0])
|
||||||
|
else:
|
||||||
|
self.simplified = self
|
||||||
|
|
||||||
|
else:
|
||||||
self.simplified = self.child.simplify()
|
self.simplified = self.child.simplify()
|
||||||
self.simplified.steps = self.child.steps + self.simplified.steps
|
self.simplified.steps = self.child.steps + self.simplified.steps
|
||||||
return self.simplified
|
return self.simplified
|
||||||
|
|
||||||
def compute_exp(self):
|
def compute_exp(self):
|
||||||
""" Create self.child with and stock steps in it """
|
""" Create self.child with and stock steps in it """
|
||||||
|
if len(self.postfix_tokens) == 1:
|
||||||
|
raise ComputeError("Nothing to compute in {}".format(self.postfix_tokens))
|
||||||
|
else:
|
||||||
ini_step = Expression(self.postfix_tokens)
|
ini_step = Expression(self.postfix_tokens)
|
||||||
|
|
||||||
tokenList = self.postfix_tokens.copy()
|
tokenList = self.postfix_tokens.copy()
|
||||||
@ -363,6 +380,11 @@ class Expression(Explicable):
|
|||||||
def __neg__(self):
|
def __neg__(self):
|
||||||
return Expression(self.postfix_tokens + [op.sub1])
|
return Expression(self.postfix_tokens + [op.sub1])
|
||||||
|
|
||||||
|
class ExpressionError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ComputeError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
def untest(exp):
|
def untest(exp):
|
||||||
a = Expression(exp)
|
a = Expression(exp)
|
||||||
|
@ -60,7 +60,7 @@ class Fraction(Explicable):
|
|||||||
ini_step = [Expression(self.postfix_tokens)]
|
ini_step = [Expression(self.postfix_tokens)]
|
||||||
|
|
||||||
if self._num == 0:
|
if self._num == 0:
|
||||||
return Expression([0])
|
return Expression([0]).simplify()
|
||||||
|
|
||||||
elif isinstance(self._num, Fraction) or isinstance(self._denom, Fraction):
|
elif isinstance(self._num, Fraction) or isinstance(self._denom, Fraction):
|
||||||
return self._num / self._denom
|
return self._num / self._denom
|
||||||
@ -74,7 +74,7 @@ class Fraction(Explicable):
|
|||||||
gcd_ = gcd(abs(self._num), abs(self._denom))
|
gcd_ = gcd(abs(self._num), abs(self._denom))
|
||||||
if gcd_ == self._denom:
|
if gcd_ == self._denom:
|
||||||
n_frac = self._num // gcd_
|
n_frac = self._num // gcd_
|
||||||
return Expression([n_frac])
|
return Expression([n_frac]).simplify()
|
||||||
|
|
||||||
elif gcd_ != 1:
|
elif gcd_ != 1:
|
||||||
n_frac = Fraction(self._num // gcd_, self._denom // gcd_)
|
n_frac = Fraction(self._num // gcd_, self._denom // gcd_)
|
||||||
|
Loading…
Reference in New Issue
Block a user