New representation for objects

This commit is contained in:
Benjamin Bertrand 2016-03-07 14:36:13 +03:00
parent 04099040d4
commit 7f891956fc
4 changed files with 65 additions and 63 deletions

View File

@ -313,7 +313,7 @@ class AbstractPolynom(Explicable):
6 x^{ 2 } + ( 7 + 5 ) x + 3 6 x^{ 2 } + ( 7 + 5 ) x + 3
6 x^{ 2 } + 12 x + 3 6 x^{ 2 } + 12 x + 3
>>> Q.steps >>> Q.steps
[< AbstractPolynom x [[1, 2], [3, 4, 5], 6]>, < AbstractPolynom x [< <class 'pymath.calculus.expression.Expression'> [1, 2, +] >, < <class 'pymath.calculus.expression.Expression'> [3, 4, +, 5, +] >, 6]>, < AbstractPolynom x [3, < <class 'pymath.calculus.expression.Expression'> [7, 5, +] >, 6]>] [< AbstractPolynom x [[1, 2], [3, 4, 5], 6]>, < AbstractPolynom x [< Expression [1, 2, +]>, < Expression [3, 4, +, 5, +]>, 6]>, < AbstractPolynom x [3, < Expression [7, 5, +]>, 6]>]
""" """
# TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET # TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET
@ -393,7 +393,7 @@ class AbstractPolynom(Explicable):
3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4 3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4
3 x^{ 2 } + 7 x + 5 3 x^{ 2 } + 7 x + 5
>>> R.steps >>> R.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 5, 'x', *, 4, +, +] >, < AbstractPolynom x [[1, 4], [2, 5], 3]>, < AbstractPolynom x [< <class 'pymath.calculus.expression.Expression'> [1, 4, +] >, < <class 'pymath.calculus.expression.Expression'> [2, 5, +] >, 3]>] [< Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 5, 'x', *, 4, +, +]>, < AbstractPolynom x [[1, 4], [2, 5], 3]>, < AbstractPolynom x [< Expression [1, 4, +]>, < Expression [2, 5, +]>, 3]>]
""" """
o_poly = self.conv2poly(other) o_poly = self.conv2poly(other)
@ -418,7 +418,7 @@ class AbstractPolynom(Explicable):
>>> Q >>> Q
< AbstractPolynom x [-1, -2, -3]> < AbstractPolynom x [-1, -2, -3]>
>>> Q.steps >>> Q.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, -] >] [< Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, -]>]
""" """
ini_step = [Expression(self.postfix_tokens + [op.sub1])] ini_step = [Expression(self.postfix_tokens + [op.sub1])]
ans = AbstractPolynom([-i for i in self._coef], ans = AbstractPolynom([-i for i in self._coef],
@ -442,7 +442,7 @@ class AbstractPolynom(Explicable):
( 3 - 6 ) x^{ 2 } + ( 2 - 5 ) x + 1 - 4 ( 3 - 6 ) x^{ 2 } + ( 2 - 5 ) x + 1 - 4
- 3 x^{ 2 } - 3 x - 3 - 3 x^{ 2 } - 3 x - 3
>>> R.steps >>> R.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, 5, 'x', *, +, 4, +, -] >, < <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, -, 5, 'x', *, -, 4, -, +] >, < AbstractPolynom x [[1, -4], [2, -5], [3, -6]]>, < AbstractPolynom x [< <class 'pymath.calculus.expression.Expression'> [1, -4, +] >, < <class 'pymath.calculus.expression.Expression'> [2, -5, +] >, < <class 'pymath.calculus.expression.Expression'> [3, -6, +] >]>] [< Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, 5, 'x', *, +, 4, +, -]>, < Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, -, 5, 'x', *, -, 4, -, +]>, < AbstractPolynom x [[1, -4], [2, -5], [3, -6]]>, < AbstractPolynom x [< Expression [1, -4, +]>, < Expression [2, -5, +]>, < Expression [3, -6, +]>]>]
""" """
o_poly = self.conv2poly(other) o_poly = self.conv2poly(other)
ini_step = [Expression(self.postfix_tokens + ini_step = [Expression(self.postfix_tokens +
@ -471,21 +471,21 @@ class AbstractPolynom(Explicable):
2 \times 3 x + 3 2 \times 3 x + 3
6 x + 3 6 x + 3
>>> (p*3).steps >>> (p*3).steps
[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 3, *] >, < AbstractPolynom x [3, < <class 'pymath.calculus.expression.Expression'> [2, 3, *] >]>] [< Expression [2, 'x', *, 1, +, 3, *]>, < AbstractPolynom x [3, < Expression [2, 3, *]>]>]
>>> q = AbstractPolynom([0,0,4]) >>> q = AbstractPolynom([0,0,4])
>>> q*3 >>> q*3
< AbstractPolynom x [0, 0, 12]> < AbstractPolynom x [0, 0, 12]>
>>> (q*3).steps >>> (q*3).steps
[< <class 'pymath.calculus.expression.Expression'> [4, 'x', 2, ^, *, 3, *] >, < AbstractPolynom x [0, 0, < <class 'pymath.calculus.expression.Expression'> [4, 3, *] >]>] [< Expression [4, 'x', 2, ^, *, 3, *]>, < AbstractPolynom x [0, 0, < Expression [4, 3, *]>]>]
>>> r = AbstractPolynom([0,1]) >>> r = AbstractPolynom([0,1])
>>> r*3 >>> r*3
< AbstractPolynom x [0, 3]> < AbstractPolynom x [0, 3]>
>>> (r*3).steps >>> (r*3).steps
[< <class 'pymath.calculus.expression.Expression'> ['x', 3, *] >] [< Expression ['x', 3, *]>]
>>> p*q >>> p*q
< AbstractPolynom x [0, 0, 4, 8]> < AbstractPolynom x [0, 0, 4, 8]>
>>> (p*q).steps >>> (p*q).steps
[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *] >, < AbstractPolynom x [0, 0, 4, < <class 'pymath.calculus.expression.Expression'> [2, 4, *] >]>] [< Expression [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *]>, < AbstractPolynom x [0, 0, 4, < Expression [2, 4, *]>]>]
>>> p*r >>> p*r
< AbstractPolynom x [0, 1, 2]> < AbstractPolynom x [0, 1, 2]>
>>> P = AbstractPolynom([1,2,3]) >>> P = AbstractPolynom([1,2,3])
@ -536,12 +536,12 @@ class AbstractPolynom(Explicable):
>>> p**2 >>> p**2
< AbstractPolynom x [0, 0, 0, 0, 9]> < AbstractPolynom x [0, 0, 0, 0, 9]>
>>> (p**2).steps >>> (p**2).steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, ^] >, < AbstractPolynom x [0, 0, 0, 0, < <class 'pymath.calculus.expression.Expression'> [3, 2, ^] >]>] [< Expression [3, 'x', 2, ^, *, 2, ^]>, < AbstractPolynom x [0, 0, 0, 0, < Expression [3, 2, ^]>]>]
>>> p = AbstractPolynom([1,2]) >>> p = AbstractPolynom([1,2])
>>> p**2 >>> p**2
< AbstractPolynom x [1, 4, 4]> < AbstractPolynom x [1, 4, 4]>
>>> (p**2).steps >>> (p**2).steps
[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, ^] >, < <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, 'x', *, 1, +, *] >, < AbstractPolynom x [1, [2, 2], < <class 'pymath.calculus.expression.Expression'> [2, 2, *] >]>, < AbstractPolynom x [1, < <class 'pymath.calculus.expression.Expression'> [2, 2, +] >, 4]>] [< Expression [2, 'x', *, 1, +, 2, ^]>, < Expression [2, 'x', *, 1, +, 2, 'x', *, 1, +, *]>, < AbstractPolynom x [1, [2, 2], < Expression [2, 2, *]>]>, < AbstractPolynom x [1, < Expression [2, 2, +]>, 4]>]
>>> p = AbstractPolynom([0,0,1]) >>> p = AbstractPolynom([0,0,1])
>>> p**3 >>> p**3
< AbstractPolynom x [0, 0, 0, 0, 0, 0, 1]> < AbstractPolynom x [0, 0, 0, 0, 0, 0, 1]>

View File

@ -24,7 +24,7 @@ def pstf_factory(pstf_tokens):
>>> from .operator import op >>> from .operator import op
>>> pstf_t = [2, 3, op.add] >>> pstf_t = [2, 3, op.add]
>>> pstf_factory(pstf_t) >>> pstf_factory(pstf_t)
< <class 'pymath.calculus.expression.Expression'> [2, 3, +] > < Expression [2, 3, +]>
>>> pstf_factory([2]) >>> pstf_factory([2])
2 2
>>> type(pstf_factory([2])) >>> type(pstf_factory([2]))
@ -84,11 +84,11 @@ class Expression(Explicable):
>>> with Expression.tmp_render(): >>> with Expression.tmp_render():
... for i in exp.simplify().explain(): ... for i in exp.simplify().explain():
... i ... i
< <class 'pymath.calculus.expression.Expression'> [2, 3, 5, /, *] > < Expression [2, 3, 5, /, *]>
< <class 'pymath.calculus.expression.Expression'> [2, < Fraction 3 / 5>, *] > < Expression [2, < Fraction 3 / 5>, *]>
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 5>, 2, *] > < Expression [< Fraction 3 / 5>, 2, *]>
< <class 'pymath.calculus.expression.Expression'> [3, 2, *, 5, /] > < Expression [3, 2, *, 5, /]>
< <class 'pymath.calculus.expression.Expression'> [6, 5, /] > < 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():
@ -152,10 +152,6 @@ class Expression(Explicable):
""" """
return self.STR_RENDER(self.postfix_tokens) return self.STR_RENDER(self.postfix_tokens)
def __repr__(self):
return " ".join(["<", str(self.__class__),
str(self.postfix_tokens), ">"])
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: try:
@ -262,7 +258,7 @@ class Expression(Explicable):
>>> f = Expression('3*4+5') >>> f = Expression('3*4+5')
>>> f1 = f.simplify() >>> f1 = f.simplify()
>>> Expression.develop_steps([e1, f1, op.add]) >>> Expression.develop_steps([e1, f1, op.add])
[< <class 'pymath.calculus.expression.Expression'> [1, 2, +, 3, 4, *, 5, +, +] >, < <class 'pymath.calculus.expression.Expression'> [3, 12, 5, +, +] >] [< Expression [1, 2, +, 3, 4, *, 5, +, +]>, < Expression [3, 12, 5, +, +]>]
>>> e = Expression('1+2') >>> e = Expression('1+2')
>>> e1 = e.simplify() >>> e1 = e.simplify()
>>> f = Expression('3*4+5') >>> f = Expression('3*4+5')
@ -270,7 +266,7 @@ class Expression(Explicable):
>>> g = Expression('6+7') >>> g = Expression('6+7')
>>> g1 = g.simplify() >>> g1 = g.simplify()
>>> Expression.develop_steps([e1, f1, op.add, g1, op.mul]) >>> Expression.develop_steps([e1, f1, op.add, g1, op.mul])
[< <class 'pymath.calculus.expression.Expression'> [1, 2, +, 3, 4, *, 5, +, +, 6, 7, +, *] >, < <class 'pymath.calculus.expression.Expression'> [3, 12, 5, +, +, 13, *] >] [< Expression [1, 2, +, 3, 4, *, 5, +, +, 6, 7, +, *]>, < Expression [3, 12, 5, +, +, 13, *]>]
""" """
with Expression.tmp_render(): with Expression.tmp_render():
tmp_steps = list(Explicable().merge_history(tokenList)) tmp_steps = list(Explicable().merge_history(tokenList))

View File

@ -129,29 +129,29 @@ class Fraction(Explicable):
>>> f + g >>> f + g
< Fraction 7 / 6> < Fraction 7 / 6>
>>> print("\\n".join([repr(i) for i in (f+g).steps])) >>> print("\\n".join([repr(i) for i in (f+g).steps]))
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, 3, /, +] > < Expression [1, 2, /, 2, 3, /, +]>
< <class 'pymath.calculus.expression.Expression'> [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, +] > < Expression [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, +]>
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, +] > < Expression [3, 6, /, 4, 6, /, +]>
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, +] > < Expression [< Fraction 3 / 6>, < Fraction 4 / 6>, +]>
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, +] > < Expression [3, 6, /, 4, 6, /, +]>
< <class 'pymath.calculus.expression.Expression'> [3, 4, +, 6, /] > < Expression [3, 4, +, 6, /]>
>>> f + 2 >>> f + 2
< Fraction 5 / 2> < Fraction 5 / 2>
>>> print("\\n".join([repr(i) for i in (f+2).steps])) >>> print("\\n".join([repr(i) for i in (f+2).steps]))
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, +] > < Expression [1, 2, /, 2, +]>
< <class 'pymath.calculus.expression.Expression'> [1, 1, *, 2, 1, *, /, 2, 2, *, 1, 2, *, /, +] > < Expression [1, 1, *, 2, 1, *, /, 2, 2, *, 1, 2, *, /, +]>
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 4, 2, /, +] > < Expression [1, 2, /, 4, 2, /, +]>
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 4 / 2>, +] > < Expression [< Fraction 1 / 2>, < Fraction 4 / 2>, +]>
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 4, 2, /, +] > < Expression [1, 2, /, 4, 2, /, +]>
< <class 'pymath.calculus.expression.Expression'> [1, 4, +, 2, /] > < 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
>>> print("\\n".join([repr(i) for i in (f+g).steps])) >>> print("\\n".join([repr(i) for i in (f+g).steps]))
< <class 'pymath.calculus.expression.Expression'> [3, 4, /, 5, 4, /, +] > < Expression [3, 4, /, 5, 4, /, +]>
< <class 'pymath.calculus.expression.Expression'> [3, 5, +, 4, /] > < Expression [3, 5, +, 4, /]>
< <class 'pymath.calculus.expression.Expression'> [8, 4, /] > < Expression [8, 4, /]>
>>> f+0 >>> f+0
< Fraction 3 / 4> < Fraction 3 / 4>
>>> (f+0).steps >>> (f+0).steps
@ -214,12 +214,12 @@ class Fraction(Explicable):
>>> f - g >>> f - g
< Fraction -1 / 6> < Fraction -1 / 6>
>>> print("\\n".join([repr(i) for i in (f-g).steps])) >>> print("\\n".join([repr(i) for i in (f-g).steps]))
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, 3, /, -] > < Expression [1, 2, /, 2, 3, /, -]>
< <class 'pymath.calculus.expression.Expression'> [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, -] > < Expression [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, -]>
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, -] > < Expression [3, 6, /, 4, 6, /, -]>
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, -] > < Expression [< Fraction 3 / 6>, < Fraction 4 / 6>, -]>
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, -] > < Expression [3, 6, /, 4, 6, /, -]>
< <class 'pymath.calculus.expression.Expression'> [3, 4, -, 6, /] > < Expression [3, 4, -, 6, /]>
>>> f - 0 >>> f - 0
< Fraction 1 / 2> < Fraction 1 / 2>
>>> (f-0).steps >>> (f-0).steps
@ -287,7 +287,7 @@ class Fraction(Explicable):
>>> -f >>> -f
< Fraction 1 / 2> < Fraction 1 / 2>
>>> (-f).steps >>> (-f).steps
[< <class 'pymath.calculus.expression.Expression'> [-1, -2, /] >] [< Expression [-1, -2, /]>]
""" """
f = Fraction(-self._num, self._denom) f = Fraction(-self._num, self._denom)
@ -303,24 +303,24 @@ class Fraction(Explicable):
>>> f*g >>> f*g
< Fraction 1 / 3> < Fraction 1 / 3>
>>> print("\\n".join([repr(i) for i in (f*g).steps])) >>> print("\\n".join([repr(i) for i in (f*g).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 2 / 3>, *] > < Expression [< Fraction 1 / 2>, < Fraction 2 / 3>, *]>
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 2, 3, *, /] > < Expression [1, 2, *, 2, 3, *, /]>
< <class 'pymath.calculus.expression.Expression'> [1, 3, /] > < Expression [1, 3, /]>
>>> f * 0 >>> f * 0
0 0
>>> (f*0).steps >>> (f*0).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 0, *] >] [< Expression [< Fraction 1 / 2>, 0, *]>]
>>> f*1 >>> f*1
< Fraction 1 / 2> < Fraction 1 / 2>
>>> (f*1).steps >>> (f*1).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 1, *] >] [< Expression [< Fraction 1 / 2>, 1, *]>]
>>> f*4 >>> f*4
2 2
>>> print("\\n".join([repr(i) for i in (f*4).steps])) >>> print("\\n".join([repr(i) for i in (f*4).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 4, *] > < Expression [< Fraction 1 / 2>, 4, *]>
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 2, *, 1, 2, *, /] > < Expression [1, 2, *, 2, *, 1, 2, *, /]>
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 1, /] > < Expression [1, 2, *, 1, /]>
< <class 'pymath.calculus.expression.Expression'> [2, 1, /] > < Expression [2, 1, /]>
""" """
steps = [Expression([self, other, op.mul])] steps = [Expression([self, other, op.mul])]
@ -437,26 +437,26 @@ class Fraction(Explicable):
>>> f**0 >>> f**0
1 1
>>> (f**0).steps >>> (f**0).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 0, ^] >] [< Expression [< Fraction 3 / 4>, 0, ^]>]
>>> f**1 >>> f**1
< Fraction 3 / 4> < Fraction 3 / 4>
>>> (f**1).steps >>> (f**1).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 1, ^] >] [< Expression [< Fraction 3 / 4>, 1, ^]>]
>>> f**3 >>> f**3
< Fraction 27 / 64> < Fraction 27 / 64>
>>> print("\\n".join([repr(i) for i in (f**3).steps])) >>> print("\\n".join([repr(i) for i in (f**3).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 3, ^] > < Expression [< Fraction 3 / 4>, 3, ^]>
< <class 'pymath.calculus.expression.Expression'> [3, 3, ^, 4, 3, ^, /] > < Expression [3, 3, ^, 4, 3, ^, /]>
< <class 'pymath.calculus.expression.Expression'> [27, 64, /] > < Expression [27, 64, /]>
>>> f = Fraction(6, 4) >>> f = Fraction(6, 4)
>>> f**3 >>> f**3
< Fraction 27 / 8> < Fraction 27 / 8>
>>> print("\\n".join([repr(i) for i in (f**3).steps])) >>> print("\\n".join([repr(i) for i in (f**3).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 6 / 4>, 3, ^] > < Expression [< Fraction 6 / 4>, 3, ^]>
< <class 'pymath.calculus.expression.Expression'> [6, 3, ^, 4, 3, ^, /] > < Expression [6, 3, ^, 4, 3, ^, /]>
< <class 'pymath.calculus.expression.Expression'> [216, 64, /] > < Expression [216, 64, /]>
< <class 'pymath.calculus.expression.Expression'> [216, 64, /] > < Expression [216, 64, /]>
< <class 'pymath.calculus.expression.Expression'> [27, 8, *, 8, 8, *, /] > < Expression [27, 8, *, 8, 8, *, /]>
""" """
if not isinstance(power, int): if not isinstance(power, int):

View File

@ -139,6 +139,12 @@ class Renderable(object):
Renderable.set_render(self.old_render) Renderable.set_render(self.old_render)
return TmpRenderEnv() return TmpRenderEnv()
def __repr__(self):
return "< {cls} {pstf_tokens}>".format(
cls = str(self.__class__).split('.')[-1][:-2],
pstf_tokens = str(self.postfix_tokens)
)
def __eq__(self, other): def __eq__(self, other):
""" Two Renderable objects are the same if they have same postfix_tokens """ """ Two Renderable objects are the same if they have same postfix_tokens """
try: try: