finaly finish to implement simplify and explain

This commit is contained in:
Lafrite 2015-04-06 17:57:13 +02:00
parent 437066e8e6
commit b1c3c3564e
6 changed files with 168 additions and 70 deletions

View File

@ -32,17 +32,25 @@ class Renderable(object):
>>> for i in exp.simplify().explain(): >>> for i in exp.simplify().explain():
... print(i) ... print(i)
2 \\times \\frac{ 3 }{ 5 } 2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 } \\frac{ 6 }{ 5 }
>>> with Expression.tmp_render(txt): >>> with Expression.tmp_render(txt):
... for i in exp.simplify().explain(): ... for i in exp.simplify().explain():
... print(i) ... print(i)
2 * 3 / 5 2 * 3 / 5
3 / 5 * 2
( 3 * 2 ) / 5
6 / 5 6 / 5
>>> for i in exp.simplify().explain(): >>> for i in exp.simplify().explain():
... print(i) ... print(i)
2 \\times \\frac{ 3 }{ 5 } 2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 } \\frac{ 6 }{ 5 }
# TODO: essayer de ne pas afficher ce changement de position. |lun. avril 6 17:29:56 CEST 2015
""" """
class TmpRenderEnv(object): class TmpRenderEnv(object):
def __enter__(self): def __enter__(self):
@ -65,8 +73,12 @@ class Explicable(Renderable):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.steps = [] self.steps = []
def explain(self): def explain(self, noself = True):
""" Generate and render steps which leed to itself """ """ Generate and render steps which leed to itself
:param noself: does explain return self
"""
old_s = '' old_s = ''
# les étapes pour l'atteindre # les étapes pour l'atteindre
try: try:
@ -81,10 +93,11 @@ class Explicable(Renderable):
except AttributeError: except AttributeError:
pass pass
# Lui même if noself:
new_s = self.STR_RENDER(self.postfix_tokens) # Lui même
if new_s != old_s: new_s = self.STR_RENDER(self.postfix_tokens)
yield new_s if new_s != old_s:
yield new_s

View File

@ -38,23 +38,30 @@ class Expression(Explicable):
>>> for i in exp.simplify().explain(): >>> for i in exp.simplify().explain():
... print(i) ... print(i)
2 \\times \\frac{ 3 }{ 5 } 2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 } \\frac{ 6 }{ 5 }
>>> with Expression.tmp_render(): >>> with Expression.tmp_render():
... for i in exp.simplify().explain(): ... for i in exp.simplify().explain():
... i ... i
< <class 'pymath.expression.Expression'> [2, 3, 5, '/', '*'] > < <class 'pymath.expression.Expression'> [2, 3, 5, '/', '*'] >
< <class 'pymath.expression.Expression'> [2, < Fraction 3 / 5>, '*'] > < <class 'pymath.expression.Expression'> [2, < Fraction 3 / 5>, '*'] >
< <class 'pymath.expression.Expression'> [2, < Fraction 3 / 5>, '*'] > < <class 'pymath.expression.Expression'> [3, 5, '/', 2, '*'] >
< <class 'pymath.expression.Expression'> [3, 2, '*', 5, '/'] >
< <class 'pymath.expression.Expression'> [6, 5, '/'] > < <class 'pymath.expression.Expression'> [6, 5, '/'] >
>>> from .render import txt >>> from .render import txt
>>> with Expression.tmp_render(txt): >>> with Expression.tmp_render(txt):
... for i in exp.simplify().explain(): ... for i in exp.simplify().explain():
... print(i) ... print(i)
2 * 3 / 5 2 * 3 / 5
3 / 5 * 2
( 3 * 2 ) / 5
6 / 5 6 / 5
>>> for i in exp.simplify().explain(): >>> for i in exp.simplify().explain():
... print(i) ... print(i)
2 \\times \\frac{ 3 }{ 5 } 2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 } \\frac{ 6 }{ 5 }
""" """
@ -117,19 +124,34 @@ 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 """
#print("\tSimplify self -> ", repr(self))
self.compute_exp() self.compute_exp()
#print("\t End Compute Simplify self -> ", self)
self.simplified = self.child.simplify() self.simplified = self.child.simplify()
try: #print("\t self.simplified -> ", repr(self.simplified))
self.simplified.steps = self.child.steps + self.simplified.steps #print("\t self.child -> ", repr(self.child))
except AttributeError: if self.simplified != self.child:
pass try:
#print('\t\t in try')
#print("\t\t self.child-> ", self.child)
#print("\t\t|-> self.child.steps -> ", self.child.steps)
#print("\t\t self.simplified -> ", self.simplified)
#print("\t\t|-> self.simplified.steps -> ", self.simplified.steps)
self.simplified.steps = self.child.steps + self.simplified.steps
#print("\t\t|--> self.simplified.steps -> ", self.simplified.steps)
except AttributeError:
pass
#print("\t self -> ", self)
#print("\t self.simplified.steps ->\n\t\t ", self.simplified.steps)
#print("\tEnd simplify self -> ", repr(self))
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 """
child_steps = [self.postfix_tokens] ini_step = Expression(self.postfix_tokens)
tokenList = self.postfix_tokens.copy() tokenList = self.postfix_tokens.copy()
tmpTokenList = [] tmpTokenList = []
@ -185,9 +207,17 @@ class Expression(Explicable):
del tokenList[0:2] del tokenList[0:2]
tmpTokenList += tokenList tmpTokenList += tokenList
#print("\t ----------------")
#print("\t self -> ", repr(self))
#print("\t tmpTokenList -> ", tmpTokenList)
self.child = Expression(tmpTokenList) self.child = Expression(tmpTokenList)
self.child.steps = self.develop_steps(tmpTokenList) if self.child.postfix_tokens == ini_step.postfix_tokens:
self.child.steps = self.develop_steps(tmpTokenList)
else:
self.child.steps = [ini_step] + self.develop_steps(tmpTokenList)
#print("\t\t self -> ", repr(self))
#print("\t self.child -> ", repr(self.child))
#print("\t self.child.steps -> ", repr(self.child.steps))
def develop_steps(self, tokenList): def develop_steps(self, tokenList):
""" From a list of tokens, it develops steps of each tokens """ """ From a list of tokens, it develops steps of each tokens """
@ -198,10 +228,16 @@ class Expression(Explicable):
tmp_steps.append([i for i in t.explain()]) tmp_steps.append([i for i in t.explain()])
else: else:
tmp_steps.append(t) tmp_steps.append(t)
tmp_steps = expand_list(tmp_steps) #print("\t\t tokenList -> ", tokenList)
steps = [Expression(s) for s in tmp_steps] #print("\t\t 1.tmp_steps -> ", tmp_steps)
if max([len(i) if type(i) == list else 1 for i in tmp_steps]) == 1:
return steps return []
else:
tmp_steps = expand_list(tmp_steps)[:-1]
#print("\t\t 2.tmp_steps -> ", tmp_steps)
steps = [Expression(s) for s in tmp_steps]
#print("\t\t steps -> ", steps)
return steps
@classmethod @classmethod
def isExpression(self, other): def isExpression(self, other):
@ -322,7 +358,7 @@ class Expression(Explicable):
return Expression(self.postfix_tokens + [op.sub1]) return Expression(self.postfix_tokens + [op.sub1])
def test(exp): def untest(exp):
a = Expression(exp) a = Expression(exp)
b = a.simplify() b = a.simplify()
@ -342,20 +378,20 @@ if __name__ == '__main__':
#Expression.set_render(txt) #Expression.set_render(txt)
#exp = "2 ^ 3 * 5" #exp = "2 ^ 3 * 5"
#test(exp) #untest(exp)
#exp = "2x + 5" #exp = "2x + 5"
#test(exp) #untest(exp)
#Expression.set_render(tex) #Expression.set_render(tex)
#test(exp1) #untest(exp1)
#from pymath.operator import op #from pymath.operator import op
#exp = [2, 3, op.pw, 5, op.mul] #exp = [2, 3, op.pw, 5, op.mul]
#test(exp) #untest(exp)
#test([Expression(exp1), Expression(exp), op.add]) #untest([Expression(exp1), Expression(exp), op.add])
#exp = "1 + 3 * 5" #exp = "1 + 3 * 5"
#e = Expression(exp) #e = Expression(exp)
@ -363,50 +399,50 @@ if __name__ == '__main__':
#print(f) #print(f)
#exp = "2 * 3 * 3 * 5" #exp = "2 * 3 * 3 * 5"
#test(exp) #untest(exp)
#exp = "2 * 3 + 3 * 5" #exp = "2 * 3 + 3 * 5"
#test(exp) #untest(exp)
#exp = "2 * ( 3 + 4 ) + 3 * 5" #exp = "2 * ( 3 + 4 ) + 3 * 5"
#test(exp) #untest(exp)
#exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5" #exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5"
#test(exp) #untest(exp)
# #
#exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5" #exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
#test(exp) #untest(exp)
# #
#exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )" #exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
#test(exp) #untest(exp)
# #
#exp = "2 + 5 * ( 3 - 4 )" #exp = "2 + 5 * ( 3 - 4 )"
#test(exp) #untest(exp)
#exp = "( 2 + 5 ) * ( 3 - 4 )^4" #exp = "( 2 + 5 ) * ( 3 - 4 )^4"
#test(exp) #untest(exp)
#exp = "( 2 + 5 ) * ( 3 * 4 )" #exp = "( 2 + 5 ) * ( 3 * 4 )"
#test(exp) #untest(exp)
#exp = "( 2 + 5 - 1 ) / ( 3 * 4 )" #exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
#test(exp) #untest(exp)
#exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12" #exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
#test(exp) #untest(exp)
#exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2" #exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2"
#test(exp) #untest(exp)
#exp="(-2+5)/(3*4)+1/12+5*5" #exp="(-2+5)/(3*4)+1/12+5*5"
#test(exp) #untest(exp)
#exp="-2*4(12 + 1)(3-12)" #exp="-2*4(12 + 1)(3-12)"
#test(exp) #untest(exp)
#exp="(-2+5)/(3*4)+1/12+5*5" #exp="(-2+5)/(3*4)+1/12+5*5"
#test(exp) #untest(exp)
# TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014 # TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014
#exp="-2*(-a)(12 + 1)(3-12)" #exp="-2*(-a)(12 + 1)(3-12)"
@ -415,15 +451,27 @@ if __name__ == '__main__':
## Can't handle it yet!! ## Can't handle it yet!!
#exp="-(-2)" #exp="-(-2)"
#test(exp) #untest(exp)
#print("\n") #print("\n")
#exp = Expression.random("({a} + 3)({b} - 1)", ["{a} > 4"]) #exp = Expression.random("({a} + 3)({b} - 1)", ["{a} > 4"])
#for i in exp.simplify(): #for i in exp.simplify():
# print(i) # print(i)
import doctest from .fraction import Fraction
doctest.testmod() f1 = Fraction(3,5)
f2 = Fraction(5,10)
q = f1+f2
print("---------")
print(q.steps)
print("---------")
for i in q.explain():
print(i)
#import doctest
#doctest.testmod()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

@ -136,18 +136,29 @@ class Fraction(Explicable):
>>> g = Fraction(2, 3) >>> g = Fraction(2, 3)
>>> f + g >>> f + g
< Fraction 7 / 6> < Fraction 7 / 6>
>>> (f+g).steps >>> print("\\n".join([repr(i) for i in (f+g).steps]))
[< <class 'pymath.expression.Expression'> [1, 2, '/', 2, 3, '/', '+'] >, [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '+'], [3, 6, '/', 4, 6, '/', '+'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '+'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '+']] < <class 'pymath.expression.Expression'> [1, 2, '/', 2, 3, '/', '+'] >
< <class 'pymath.expression.Expression'> [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '+'] >
< <class 'pymath.expression.Expression'> [3, 6, '/', 4, 6, '/', '+'] >
< <class 'pymath.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, '+'] >
< <class 'pymath.expression.Expression'> [3, 6, '/', 4, 6, '/', '+'] >
< <class 'pymath.expression.Expression'> [3, 4, '+', 6, '/'] >
>>> f + 2 >>> f + 2
< Fraction 5 / 2> < Fraction 5 / 2>
>>> (f+2).steps >>> print("\\n".join([repr(i) for i in (f+2).steps]))
[< <class 'pymath.expression.Expression'> [1, 2, '/', 2, '+'] >, [1, 1, '*', 2, 1, '*', '/', 2, 2, '*', 1, 2, '*', '/', '+'], [1, 2, '/', 4, 2, '/', '+'], [< Fraction 1 / 2>, < Fraction 4 / 2>, '+'], [< Fraction 1 / 2>, < Fraction 4 / 2>, '+']] < <class 'pymath.expression.Expression'> [1, 2, '/', 2, '+'] >
< <class 'pymath.expression.Expression'> [1, 1, '*', 2, 1, '*', '/', 2, 2, '*', 1, 2, '*', '/', '+'] >
< <class 'pymath.expression.Expression'> [1, 2, '/', 4, 2, '/', '+'] >
< <class 'pymath.expression.Expression'> [< Fraction 1 / 2>, < Fraction 4 / 2>, '+'] >
< <class 'pymath.expression.Expression'> [1, 2, '/', 4, 2, '/', '+'] >
< <class 'pymath.expression.Expression'> [1, 4, '+', 2, '/'] >
>>> f = Fraction(3, 4) >>> f = Fraction(3, 4)
>>> g = Fraction(5, 4) >>> g = Fraction(5, 4)
>>> f + g >>> f + g
2 2
>>> (f+g).steps >>> print("\\n".join([repr(i) for i in (f+g).steps]))
[< <class 'pymath.expression.Expression'> [3, 4, '/', 5, 4, '/', '+'] >, [3, 5, '+', 4, '/'], [8, 4, '/']] < <class 'pymath.expression.Expression'> [3, 4, '/', 5, 4, '/', '+'] >
< <class 'pymath.expression.Expression'> [3, 5, '+', 4, '/'] >
>>> f+0 >>> f+0
< Fraction 3 / 4> < Fraction 3 / 4>
>>> (f+0).steps >>> (f+0).steps
@ -174,8 +185,8 @@ class Fraction(Explicable):
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]) 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])
ini_step = Expression(self.postfix_tokens + number.postfix_tokens + [op.add])
ans = exp.simplify() ans = exp.simplify()
ini_step = Expression(self.postfix_tokens + number.postfix_tokens + [op.add])
ans.steps = [ini_step] + ans.steps ans.steps = [ini_step] + ans.steps
return ans return ans
@ -194,8 +205,13 @@ class Fraction(Explicable):
>>> g = Fraction(2, 3) >>> g = Fraction(2, 3)
>>> f - g >>> f - g
< Fraction -1 / 6> < Fraction -1 / 6>
>>> (f-g).steps >>> print("\\n".join([repr(i) for i in (f-g).steps]))
[< <class 'pymath.expression.Expression'> [1, 2, '/', 2, 3, '/', '-'] >, [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '-'], [3, 6, '/', 4, 6, '/', '-'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '-'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '-']] < <class 'pymath.expression.Expression'> [1, 2, '/', 2, 3, '/', '-'] >
< <class 'pymath.expression.Expression'> [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '-'] >
< <class 'pymath.expression.Expression'> [3, 6, '/', 4, 6, '/', '-'] >
< <class 'pymath.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, '-'] >
< <class 'pymath.expression.Expression'> [3, 6, '/', 4, 6, '/', '-'] >
< <class 'pymath.expression.Expression'> [3, 4, '-', 6, '/'] >
>>> f - 0 >>> f - 0
< Fraction 1 / 2> < Fraction 1 / 2>
>>> (f-0).steps >>> (f-0).steps
@ -263,8 +279,12 @@ class Fraction(Explicable):
>>> g = Fraction(2, 3) >>> g = Fraction(2, 3)
>>> f*g >>> f*g
< Fraction 1 / 3> < Fraction 1 / 3>
>>> (f*g).steps >>> print("\\n".join([repr(i) for i in (f*g).steps]))
[< <class 'pymath.expression.Expression'> [1, 2, '/', 2, 3, '/', '*'] >, [1, 1, 2, '*', '*', 1, 2, '*', 3, '*', '/'], [1, 2, '*', 2, 3, '*', '/'], [2, 6, '/'], < <class 'pymath.expression.Expression'> [2, 6, '/'] >, < <class 'pymath.expression.Expression'> [1, 2, '*', 3, 2, '*', '/'] >] < <class 'pymath.expression.Expression'> [1, 2, '/', 2, 3, '/', '*'] >
< <class 'pymath.expression.Expression'> [1, 1, 2, '*', '*', 1, 2, '*', 3, '*', '/'] >
< <class 'pymath.expression.Expression'> [1, 2, '*', 2, 3, '*', '/'] >
< <class 'pymath.expression.Expression'> [2, 6, '/'] >
< <class 'pymath.expression.Expression'> [1, 2, '*', 3, 2, '*', '/'] >
>>> f * 0 >>> f * 0
0 0
>>> (f*0).steps >>> (f*0).steps
@ -275,8 +295,10 @@ class Fraction(Explicable):
[] []
>>> f*4 >>> f*4
2 2
>>> (f*4).steps >>> print("\\n".join([repr(i) for i in (f*4).steps]))
[< <class 'pymath.expression.Expression'> [1, 2, '/', 4, '*'] >, [1, 2, '*', 2, '*', 1, 2, '*', '/'], [2, 2, '*', 2, '/'], [4, 2, '/']] < <class 'pymath.expression.Expression'> [1, 2, '/', 4, '*'] >
< <class 'pymath.expression.Expression'> [1, 2, '*', 2, '*', 1, 2, '*', '/'] >
< <class 'pymath.expression.Expression'> [2, 2, '*', 2, '/'] >
""" """
steps = [] steps = []
@ -381,13 +403,17 @@ class Fraction(Explicable):
[] []
>>> f**3 >>> f**3
< Fraction 27 / 64> < Fraction 27 / 64>
>>> (f**3).steps >>> print("\\n".join([repr(i) for i in (f**3).steps]))
[< <class 'pymath.expression.Expression'> [3, 4, '/', 3, '^'] >, [3, 3, '^', 4, 3, '^', '/'], [27, 64, '/'], [27, 64, '/']] < <class 'pymath.expression.Expression'> [3, 4, '/', 3, '^'] >
< <class 'pymath.expression.Expression'> [3, 3, '^', 4, 3, '^', '/'] >
>>> f = Fraction(6, 4) >>> f = Fraction(6, 4)
>>> f**3 >>> f**3
< Fraction 27 / 8> < Fraction 27 / 8>
>>> (f**3).steps >>> print("\\n".join([repr(i) for i in (f**3).steps]))
[< <class 'pymath.expression.Expression'> [6, 4, '/', 3, '^'] >, [6, 3, '^', 4, 3, '^', '/'], [216, 64, '/'], < <class 'pymath.expression.Expression'> [216, 64, '/'] >, < <class 'pymath.expression.Expression'> [27, 8, '*', 8, 8, '*', '/'] >] < <class 'pymath.expression.Expression'> [6, 4, '/', 3, '^'] >
< <class 'pymath.expression.Expression'> [6, 3, '^', 4, 3, '^', '/'] >
< <class 'pymath.expression.Expression'> [216, 64, '/'] >
< <class 'pymath.expression.Expression'> [27, 8, '*', 8, 8, '*', '/'] >
""" """
if not type(power) == int: if not type(power) == int:

View File

@ -364,7 +364,7 @@ class Polynom(Explicable):
>>> Q >>> Q
< Polynom [3, 12, 6]> < Polynom [3, 12, 6]>
>>> Q.steps >>> Q.steps
[< Polynom [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < Polynom [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [7, 5, '+'] >, 6]>, < Polynom [3, < <class 'pymath.expression.Expression'> [7, 5, '+'] >, 6]>] [< Polynom [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < Polynom [3, < <class 'pymath.expression.Expression'> [7, 5, '+'] >, 6]>]
""" """
# TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015 # TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015
@ -477,7 +477,7 @@ class Polynom(Explicable):
>>> R >>> R
< Polynom [5, 7, 3]> < Polynom [5, 7, 3]>
>>> R.steps >>> R.steps
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < Polynom [< <class 'pymath.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.expression.Expression'> [2, 5, '+'] >, 3]>, < Polynom [< <class 'pymath.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.expression.Expression'> [2, 5, '+'] >, 3]>] [< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < Polynom [< <class 'pymath.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.expression.Expression'> [2, 5, '+'] >, 3]>]
""" """
o_poly = self.conv2poly(other) o_poly = self.conv2poly(other)
@ -517,7 +517,7 @@ class Polynom(Explicable):
>>> R >>> R
< Polynom [-3, -3, -3]> < Polynom [-3, -3, -3]>
>>> R.steps >>> R.steps
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', 5, 'x', '*', '+', 4, '+', '-'] >, < <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', '-', 5, 'x', '*', '-', 4, '-', '+'] >, < Polynom [< <class 'pymath.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.expression.Expression'> [3, -6, '+'] >]>, < Polynom [< <class 'pymath.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.expression.Expression'> [3, -6, '+'] >]>] [< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', 5, 'x', '*', '+', 4, '+', '-'] >, < <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', '-', 5, 'x', '*', '-', 4, '-', '+'] >, < Polynom [< <class 'pymath.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.expression.Expression'> [3, -6, '+'] >]>]
""" """
o_poly = self.conv2poly(other) o_poly = self.conv2poly(other)
ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])] ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])]
@ -541,12 +541,12 @@ class Polynom(Explicable):
>>> p*3 >>> p*3
< Polynom [3, 6]> < Polynom [3, 6]>
>>> (p*3).steps >>> (p*3).steps
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 3, '*'] >], < Polynom [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>, < Polynom [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>] [[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 3, '*'] >], < Polynom [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>]
>>> q = Polynom([0,0,4]) >>> q = Polynom([0,0,4])
>>> q*3 >>> q*3
< Polynom [0, 0, 12]> < Polynom [0, 0, 12]>
>>> (q*3).steps >>> (q*3).steps
[[< <class 'pymath.expression.Expression'> [4, 'x', 2, '^', '*', 3, '*'] >], < Polynom [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>, < Polynom [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>] [[< <class 'pymath.expression.Expression'> [4, 'x', 2, '^', '*', 3, '*'] >], < Polynom [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>]
>>> r = Polynom([0,1]) >>> r = Polynom([0,1])
>>> r*3 >>> r*3
< Polynom [0, 3]> < Polynom [0, 3]>
@ -555,7 +555,7 @@ class Polynom(Explicable):
>>> p*q >>> p*q
< Polynom [0, 0, 4, 8]> < Polynom [0, 0, 4, 8]>
>>> (p*q).steps >>> (p*q).steps
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < Polynom [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>, < Polynom [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>] [[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < Polynom [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>]
>>> p*r >>> p*r
< Polynom [0, 1, 2]> < Polynom [0, 1, 2]>
>>> P = Polynom([1,2,3]) >>> P = Polynom([1,2,3])
@ -606,12 +606,12 @@ class Polynom(Explicable):
>>> p**2 >>> p**2
< Polynom [0, 0, 0, 0, 9]> < Polynom [0, 0, 0, 0, 9]>
>>> (p**2).steps >>> (p**2).steps
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, '^'] >, < Polynom [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>, < Polynom [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>] [< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, '^'] >, < Polynom [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>]
>>> p = Polynom([1,2]) >>> p = Polynom([1,2])
>>> p**2 >>> p**2
< Polynom [1, 4, 4]> < Polynom [1, 4, 4]>
>>> (p**2).steps >>> (p**2).steps
[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, '^'] >, [< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < Polynom [1, < <class 'pymath.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.expression.Expression'> [2, 2, '*'] >]>, < Polynom [1, < <class 'pymath.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.expression.Expression'> [2, 2, '*'] >]>] [< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, '^'] >, [< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < Polynom [1, < <class 'pymath.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.expression.Expression'> [2, 2, '*'] >]>]
>>> p = Polynom([0,0,1]) >>> p = Polynom([0,0,1])
>>> p**3 >>> p**3
< Polynom [0, 0, 0, 0, 0, 0, 1]> < Polynom [0, 0, 0, 0, 0, 0, 1]>

View File

@ -83,7 +83,7 @@ class Polynom_deg2(Polynom):
... print(i) ... print(i)
\\frac{ - 2 }{ 2 \\times 3 } \\frac{ - 2 }{ 2 \\times 3 }
\\frac{ -2 }{ 6 } \\frac{ -2 }{ 6 }
\\frac{ ( -1 ) \\times 2 }{ 3 \\times 2 } \\frac{ -1 \\times 2 }{ 3 \\times 2 }
\\frac{ -1 }{ 3 } \\frac{ -1 }{ 3 }
""" """
return Expression([self.b, op.sub1, 2, self.a, op.mul, op.div]).simplify() return Expression([self.b, op.sub1, 2, self.a, op.mul, op.div]).simplify()
@ -97,10 +97,21 @@ class Polynom_deg2(Polynom):
< Fraction 2 / 3> < Fraction 2 / 3>
>>> for i in P.beta.explain(): # Ça serait bien que l'on puisse enlever des étapes maintenant... >>> for i in P.beta.explain(): # Ça serait bien que l'on puisse enlever des étapes maintenant...
... print(i) ... print(i)
3 \\times \\frac{ -1 }{ 3 }^{ 2 } + 2 \\times \\frac{ -1 }{ 3 } + 1 3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + 2 \\times \\frac{ -1 }{ 3 } + 1
3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + \\frac{ -1 }{ 3 } \\times 2 + 1
3 \\times \\frac{ -1^{ 2 } }{ 3^{ 2 } } + \\frac{ -1 \\times 2 }{ 3 } + 1
3 \\times \\frac{ 1 }{ 9 } + \\frac{ -2 }{ 3 } + 1 3 \\times \\frac{ 1 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 }{ 9 } \\times 3 + \\frac{ -1 }{ 3 } \\times 2 + 1
\\frac{ 1 \\times 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -1 \\times 2 }{ 3 } + 1
\\frac{ 1 \\times 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1 \\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 - 2 }{ 3 } + 1
\\frac{ -1 }{ 3 } + 1 \\frac{ -1 }{ 3 } + 1
\\frac{ -1 \\times 1 }{ 3 \\times 1 } + \\frac{ 1 \\times 3 }{ 1 \\times 3 }
\\frac{ -1 }{ 3 } + \\frac{ 3 }{ 3 }
\\frac{ -1 + 3 }{ 3 }
\\frac{ 2 }{ 3 } \\frac{ 2 }{ 3 }
""" """
return self(self.alpha).simplify() return self(self.alpha).simplify()

View File

@ -91,7 +91,7 @@ def in2post_fix(infix_tokens):
@return: the corresponding postfix list of tokens. @return: the corresponding postfix list of tokens.
>>> in2post_fix([op.par, 2, op.add, 5, op.sub, 1, ')', op.div, op.par, 3, op.mul, 4, ')']) >>> in2post_fix([op.par, 2, op.add, 5, op.sub, 1, ')', op.div, op.par, 3, op.mul, 4, ')'])
[2, 5, '+', 1, '-', 3, 4, '*', '/'] [2, 5, 1, '-', '+', 3, 4, '*', '/']
>>> in2post_fix([op.sub1, op.par, op.sub1, 2, ')']) >>> in2post_fix([op.sub1, op.par, op.sub1, 2, ')'])
[2, '-', '-'] [2, '-', '-']
>>> in2post_fix([op.sub1, op.par, op.sub1, 2, op.add, 3, op.mul, 4, ')']) >>> in2post_fix([op.sub1, op.par, op.sub1, 2, op.add, 3, op.mul, 4, ')'])