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:
|
||||
token = expression.postfix_tokens[0]
|
||||
# if len(expression.postfix_tokens) == 1:
|
||||
# token = expression.postfix_tokens[0]
|
||||
|
||||
if isinstance(token, Explicable_int) or isinstance(token, int):
|
||||
return Explicable_int(token)
|
||||
# if isinstance(token, Explicable_int) or isinstance(token, int):
|
||||
# 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
|
||||
if isinstance(token, Explicable_float) or isinstance(token, float):
|
||||
return Explicable_float(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
|
||||
# if isinstance(token, Explicable_float) or isinstance(token, float):
|
||||
# return Explicable_float(token)
|
||||
|
||||
elif hasattr(token, 'simplify') and hasattr(token, 'explain'):
|
||||
ans = expression.postfix_tokens[0]
|
||||
return ans
|
||||
# elif hasattr(token, 'simplify') and hasattr(token, 'explain'):
|
||||
# ans = expression.postfix_tokens[0]
|
||||
# return ans
|
||||
|
||||
elif isinstance(token, str):
|
||||
from .polynom import Polynom
|
||||
return Polynom([0,1], letter = token)
|
||||
# elif isinstance(token, str):
|
||||
# from .polynom import Polynom
|
||||
# return Polynom([0,1], letter = token)
|
||||
|
||||
else:
|
||||
raise ValueError(
|
||||
"Unknow token type in Expression: {}".format(
|
||||
type(token)))
|
||||
# else:
|
||||
# raise ValueError(
|
||||
# "Unknow token type in Expression: {}".format(
|
||||
# type(token)))
|
||||
|
||||
else:
|
||||
# else:
|
||||
# expression._isExpression = 1
|
||||
# return expression
|
||||
expression._isExpression = 1
|
||||
return expression
|
||||
|
||||
@ -144,14 +146,29 @@ class Expression(Explicable):
|
||||
|
||||
def simplify(self):
|
||||
""" Compute entirely the expression and return the result with .steps attribute """
|
||||
try:
|
||||
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.steps = self.child.steps + self.simplified.steps
|
||||
return self.simplified
|
||||
|
||||
def compute_exp(self):
|
||||
""" 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)
|
||||
|
||||
tokenList = self.postfix_tokens.copy()
|
||||
@ -363,6 +380,11 @@ class Expression(Explicable):
|
||||
def __neg__(self):
|
||||
return Expression(self.postfix_tokens + [op.sub1])
|
||||
|
||||
class ExpressionError(Exception):
|
||||
pass
|
||||
|
||||
class ComputeError(Exception):
|
||||
pass
|
||||
|
||||
def untest(exp):
|
||||
a = Expression(exp)
|
||||
|
@ -60,7 +60,7 @@ class Fraction(Explicable):
|
||||
ini_step = [Expression(self.postfix_tokens)]
|
||||
|
||||
if self._num == 0:
|
||||
return Expression([0])
|
||||
return Expression([0]).simplify()
|
||||
|
||||
elif isinstance(self._num, Fraction) or isinstance(self._denom, Fraction):
|
||||
return self._num / self._denom
|
||||
@ -74,7 +74,7 @@ class Fraction(Explicable):
|
||||
gcd_ = gcd(abs(self._num), abs(self._denom))
|
||||
if gcd_ == self._denom:
|
||||
n_frac = self._num // gcd_
|
||||
return Expression([n_frac])
|
||||
return Expression([n_frac]).simplify()
|
||||
|
||||
elif gcd_ != 1:
|
||||
n_frac = Fraction(self._num // gcd_, self._denom // gcd_)
|
||||
|
Loading…
Reference in New Issue
Block a user