pass doctest for abstract_polynom expression fraction generic and

polynom
This commit is contained in:
Benjamin Bertrand 2016-02-27 12:29:05 +03:00
parent c2cdbea001
commit 1b2d778ff6
5 changed files with 89 additions and 150 deletions

View File

@ -42,15 +42,15 @@ class AbstractPolynom(Explicable):
>>> P = AbstractPolynom([1, 2, 3])
>>> P.mainOp
'+'
+
>>> P.name
'P'
>>> P._letter
'x'
>>> AbstractPolynom([1]).mainOp
'*'
*
>>> AbstractPolynom([0, 0, 3]).mainOp
'*'
*
>>> AbstractPolynom([1, 2, 3])._letter
'x'
>>> AbstractPolynom([1, 2, 3], "y")._letter
@ -139,17 +139,17 @@ class AbstractPolynom(Explicable):
>>> p = AbstractPolynom()
>>> p.coef_postfix([3],2)
[3, 'x', 2, '^', '*']
[3, 'x', 2, ^, *]
>>> p.coef_postfix([0],1)
[]
>>> p.coef_postfix([3],0)
[3]
>>> p.coef_postfix([3],1)
[3, 'x', '*']
[3, 'x', *]
>>> p.coef_postfix([1],1)
['x']
>>> p.coef_postfix([1],2)
['x', 2, '^']
['x', 2, ^]
"""
ans = []
@ -173,13 +173,13 @@ class AbstractPolynom(Explicable):
>>> p = AbstractPolynom([1, 2])
>>> p.postfix_tokens
[2, 'x', '*', 1, '+']
[2, 'x', *, 1, +]
>>> p = AbstractPolynom([1, -2])
>>> p.postfix_tokens
[2, 'x', '*', '-', 1, '+']
[2, 'x', *, -, 1, +]
>>> p = AbstractPolynom([1,2,3])
>>> p.postfix_tokens
[3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
[3, 'x', 2, ^, *, 2, 'x', *, +, 1, +]
>>> p = AbstractPolynom([1])
>>> p.postfix_tokens
[1]
@ -188,19 +188,19 @@ class AbstractPolynom(Explicable):
[0]
>>> p = AbstractPolynom([1,[2,3]])
>>> p.postfix_tokens
[2, 'x', '*', 3, 'x', '*', '+', 1, '+']
[2, 'x', *, 3, 'x', *, +, 1, +]
>>> p = AbstractPolynom([1,[2,-3]])
>>> p.postfix_tokens
[2, 'x', '*', 3, 'x', '*', '-', 1, '+']
[2, 'x', *, 3, 'x', *, -, 1, +]
>>> p = AbstractPolynom([1,[-2,-3]])
>>> p.postfix_tokens
[2, 'x', '*', '-', 3, 'x', '*', '-', 1, '+']
[2, 'x', *, -, 3, 'x', *, -, 1, +]
>>> from pymath.calculus.expression import Expression
>>> from pymath.calculus.operator import op
>>> e = Expression([2,3,op.add])
>>> p = AbstractPolynom([1,e])
>>> p.postfix_tokens
[2, 3, '+', 'x', '*', 1, '+']
[2, 3, +, 'x', *, 1, +]
"""
if self == 0:
@ -312,7 +312,7 @@ class AbstractPolynom(Explicable):
6 x^{ 2 } + ( 7 + 5 ) x + 3
6 x^{ 2 } + 12 x + 3
>>> Q.steps
[< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, 2], [3, 4, 5], 6]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.calculus.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [7, 5, '+'] >, 6]>]
[< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, 2], [3, 4, 5], 6]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 2, +] >, < <class 'pymath.calculus.expression.Expression'> [3, 4, +, 5, +] >, 6]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [7, 5, +] >, 6]>]
"""
# TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET
@ -397,7 +397,7 @@ class AbstractPolynom(Explicable):
3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4
3 x^{ 2 } + 7 x + 5
>>> R.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, 4], [2, 5], 3]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.calculus.expression.Expression'> [2, 5, '+'] >, 3]>]
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 5, 'x', *, 4, +, +] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, 4], [2, 5], 3]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 4, +] >, < <class 'pymath.calculus.expression.Expression'> [2, 5, +] >, 3]>]
"""
o_poly = self.conv2poly(other)
@ -422,7 +422,7 @@ class AbstractPolynom(Explicable):
>>> Q
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [-1, -2, -3]>
>>> Q.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', '-'] >]
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, -] >]
"""
ini_step = [Expression(self.postfix_tokens + [op.sub1])]
ans = AbstractPolynom([-i for i in self._coef],
@ -446,7 +446,7 @@ class AbstractPolynom(Explicable):
( 3 - 6 ) x^{ 2 } + ( 2 - 5 ) x + 1 - 4
- 3 x^{ 2 } - 3 x - 3
>>> 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, '-', '+'] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, -4], [2, -5], [3, -6]]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.calculus.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.calculus.expression.Expression'> [3, -6, '+'] >]>]
[< <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, -, +] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, -4], [2, -5], [3, -6]]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, -4, +] >, < <class 'pymath.calculus.expression.Expression'> [2, -5, +] >, < <class 'pymath.calculus.expression.Expression'> [3, -6, +] >]>]
"""
o_poly = self.conv2poly(other)
ini_step = [Expression(self.postfix_tokens +
@ -471,21 +471,21 @@ class AbstractPolynom(Explicable):
>>> p*3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, 6]>
>>> (p*3).steps
[[< <class 'pymath.calculus.expression.Expression'> [2, 'x', '*', 1, '+', 3, '*'] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [2, 3, '*'] >]>]
[[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 3, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [2, 3, *] >]>]
>>> q = AbstractPolynom([0,0,4])
>>> q*3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 12]>
>>> (q*3).steps
[[< <class 'pymath.calculus.expression.Expression'> [4, 'x', 2, '^', '*', 3, '*'] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, < <class 'pymath.calculus.expression.Expression'> [4, 3, '*'] >]>]
[[< <class 'pymath.calculus.expression.Expression'> [4, 'x', 2, ^, *, 3, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, < <class 'pymath.calculus.expression.Expression'> [4, 3, *] >]>]
>>> r = AbstractPolynom([0,1])
>>> r*3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 3]>
>>> (r*3).steps
[[< <class 'pymath.calculus.expression.Expression'> ['x', 3, '*'] >]]
[[< <class 'pymath.calculus.expression.Expression'> ['x', 3, *] >]]
>>> p*q
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, 8]>
>>> (p*q).steps
[[< <class 'pymath.calculus.expression.Expression'> [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, < <class 'pymath.calculus.expression.Expression'> [2, 4, '*'] >]>]
[[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, < <class 'pymath.calculus.expression.Expression'> [2, 4, *] >]>]
>>> p*r
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 1, 2]>
>>> P = AbstractPolynom([1,2,3])
@ -536,12 +536,12 @@ class AbstractPolynom(Explicable):
>>> p**2
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 9]>
>>> (p**2).steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, '^'] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, < <class 'pymath.calculus.expression.Expression'> [3, 2, '^'] >]>]
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, ^] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, < <class 'pymath.calculus.expression.Expression'> [3, 2, ^] >]>]
>>> p = AbstractPolynom([1,2])
>>> p**2
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, 4, 4]>
>>> (p**2).steps
[< <class 'pymath.calculus.expression.Expression'> [2, 'x', '*', 1, '+', 2, '^'] >, [< <class 'pymath.calculus.expression.Expression'> [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, [2, 2], < <class 'pymath.calculus.expression.Expression'> [2, 2, '*'] >]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, < <class 'pymath.calculus.expression.Expression'> [2, 2, '+'] >, 4]>]
[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, ^] >, [< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, 'x', *, 1, +, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, [2, 2], < <class 'pymath.calculus.expression.Expression'> [2, 2, *] >]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, < <class 'pymath.calculus.expression.Expression'> [2, 2, +] >, 4]>]
>>> p = AbstractPolynom([0,0,1])
>>> p**3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 0, 0, 1]>

View File

@ -23,7 +23,7 @@ def pstf_factory(pstf_tokens):
>>> from .operator import op
>>> pstf_t = [2, 3, op.add]
>>> pstf_factory(pstf_t)
< <class 'pymath.calculus.expression.Expression'> [2, 3, '+'] >
< <class 'pymath.calculus.expression.Expression'> [2, 3, +] >
>>> pstf_factory([2])
2
>>> type(pstf_factory([2]))
@ -99,11 +99,11 @@ class Expression(Explicable):
>>> with Expression.tmp_render():
... for i in exp.simplify().explain():
... i
< <class 'pymath.calculus.expression.Expression'> [2, 3, 5, '/', '*'] >
< <class 'pymath.calculus.expression.Expression'> [2, < Fraction 3 / 5>, '*'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 5>, 2, '*'] >
< <class 'pymath.calculus.expression.Expression'> [3, 2, '*', 5, '/'] >
< <class 'pymath.calculus.expression.Expression'> [6, 5, '/'] >
< <class 'pymath.calculus.expression.Expression'> [2, 3, 5, /, *] >
< <class 'pymath.calculus.expression.Expression'> [2, < Fraction 3 / 5>, *] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 5>, 2, *] >
< <class 'pymath.calculus.expression.Expression'> [3, 2, *, 5, /] >
< <class 'pymath.calculus.expression.Expression'> [6, 5, /] >
>>> from .render import txt
>>> with Expression.tmp_render(txt):
... for i in exp.simplify().explain():
@ -330,13 +330,13 @@ class Expression(Explicable):
>>> a = Expression("1+2")
>>> print(a.postfix_tokens)
[1, 2, '+']
[1, 2, +]
>>> b = Expression("3+4")
>>> print(b.postfix_tokens)
[3, 4, '+']
[3, 4, +]
>>> c = a + b
>>> print(c.postfix_tokens)
[1, 2, '+', 3, 4, '+', '+']
[1, 2, +, 3, 4, +, +]
"""
return self.operate(other, op.add)
@ -348,13 +348,13 @@ class Expression(Explicable):
>>> a = Expression("1+2")
>>> print(a.postfix_tokens)
[1, 2, '+']
[1, 2, +]
>>> b = Expression("3+4")
>>> print(b.postfix_tokens)
[3, 4, '+']
[3, 4, +]
>>> c = a - b
>>> print(c.postfix_tokens)
[1, 2, '+', 3, 4, '+', '-']
[1, 2, +, 3, 4, +, -]
"""
return self.operate(other, op.sub)
@ -366,13 +366,13 @@ class Expression(Explicable):
>>> a = Expression("1+2")
>>> print(a.postfix_tokens)
[1, 2, '+']
[1, 2, +]
>>> b = Expression("3+4")
>>> print(b.postfix_tokens)
[3, 4, '+']
[3, 4, +]
>>> c = a * b
>>> print(c.postfix_tokens)
[1, 2, '+', 3, 4, '+', '*']
[1, 2, +, 3, 4, +, *]
"""
return self.operate(other, op.mul)
@ -384,13 +384,13 @@ class Expression(Explicable):
>>> a = Expression("1+2")
>>> print(a.postfix_tokens)
[1, 2, '+']
[1, 2, +]
>>> b = Expression("3+4")
>>> print(b.postfix_tokens)
[3, 4, '+']
[3, 4, +]
>>> c = a / b
>>> print(c.postfix_tokens)
[1, 2, '+', 3, 4, '+', '/']
[1, 2, +, 3, 4, +, /]
>>>
"""
return self.operate(other, op.div)

View File

@ -98,7 +98,7 @@ class Fraction(Explicable):
>>> f = Fraction(3, 5)
>>> f.postfix_tokens
[3, 5, '/']
[3, 5, /]
"""
if self._denom == 1:
@ -134,29 +134,29 @@ class Fraction(Explicable):
>>> f + g
< Fraction 7 / 6>
>>> print("\\n".join([repr(i) for i in (f+g).steps]))
< <class 'pymath.calculus.expression.Expression'> [1, 2, '/', 2, 3, '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, '/', 4, 6, '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, '+'] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, '/', 4, 6, '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [3, 4, '+', 6, '/'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, 3, /, +] >
< <class 'pymath.calculus.expression.Expression'> [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, +] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, +] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, +] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, +] >
< <class 'pymath.calculus.expression.Expression'> [3, 4, +, 6, /] >
>>> f + 2
< Fraction 5 / 2>
>>> print("\\n".join([repr(i) for i in (f+2).steps]))
< <class 'pymath.calculus.expression.Expression'> [1, 2, '/', 2, '+'] >
< <class 'pymath.calculus.expression.Expression'> [1, 1, '*', 2, 1, '*', '/', 2, 2, '*', 1, 2, '*', '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, '/', 4, 2, '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 4 / 2>, '+'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, '/', 4, 2, '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [1, 4, '+', 2, '/'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, +] >
< <class 'pymath.calculus.expression.Expression'> [1, 1, *, 2, 1, *, /, 2, 2, *, 1, 2, *, /, +] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 4, 2, /, +] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 4 / 2>, +] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 4, 2, /, +] >
< <class 'pymath.calculus.expression.Expression'> [1, 4, +, 2, /] >
>>> f = Fraction(3, 4)
>>> g = Fraction(5, 4)
>>> f + g
2
>>> print("\\n".join([repr(i) for i in (f+g).steps]))
< <class 'pymath.calculus.expression.Expression'> [3, 4, '/', 5, 4, '/', '+'] >
< <class 'pymath.calculus.expression.Expression'> [3, 5, '+', 4, '/'] >
< <class 'pymath.calculus.expression.Expression'> [8, 4, '/'] >
< <class 'pymath.calculus.expression.Expression'> [3, 4, /, 5, 4, /, +] >
< <class 'pymath.calculus.expression.Expression'> [3, 5, +, 4, /] >
< <class 'pymath.calculus.expression.Expression'> [8, 4, /] >
>>> f+0
< Fraction 3 / 4>
>>> (f+0).steps
@ -219,12 +219,12 @@ class Fraction(Explicable):
>>> f - g
< Fraction -1 / 6>
>>> print("\\n".join([repr(i) for i in (f-g).steps]))
< <class 'pymath.calculus.expression.Expression'> [1, 2, '/', 2, 3, '/', '-'] >
< <class 'pymath.calculus.expression.Expression'> [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '-'] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, '/', 4, 6, '/', '-'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, '-'] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, '/', 4, 6, '/', '-'] >
< <class 'pymath.calculus.expression.Expression'> [3, 4, '-', 6, '/'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, /, 2, 3, /, -] >
< <class 'pymath.calculus.expression.Expression'> [1, 3, *, 2, 3, *, /, 2, 2, *, 3, 2, *, /, -] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, -] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 6>, < Fraction 4 / 6>, -] >
< <class 'pymath.calculus.expression.Expression'> [3, 6, /, 4, 6, /, -] >
< <class 'pymath.calculus.expression.Expression'> [3, 4, -, 6, /] >
>>> f - 0
< Fraction 1 / 2>
>>> (f-0).steps
@ -292,7 +292,7 @@ class Fraction(Explicable):
>>> -f
< Fraction 1 / 2>
>>> (-f).steps
[< <class 'pymath.calculus.expression.Expression'> [-1, -2, '/'] >]
[< <class 'pymath.calculus.expression.Expression'> [-1, -2, /] >]
"""
f = Fraction(-self._num, self._denom)
@ -308,24 +308,24 @@ class Fraction(Explicable):
>>> f*g
< Fraction 1 / 3>
>>> print("\\n".join([repr(i) for i in (f*g).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 2 / 3>, '*'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, '*', 2, 3, '*', '/'] >
< <class 'pymath.calculus.expression.Expression'> [1, 3, '/'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, < Fraction 2 / 3>, *] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 2, 3, *, /] >
< <class 'pymath.calculus.expression.Expression'> [1, 3, /] >
>>> f * 0
0
>>> (f*0).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 0, '*'] >]
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 0, *] >]
>>> f*1
< Fraction 1 / 2>
>>> (f*1).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 1, '*'] >]
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 1, *] >]
>>> f*4
2
>>> print("\\n".join([repr(i) for i in (f*4).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 4, '*'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, '*', 2, '*', 1, 2, '*', '/'] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, '*', 1, '/'] >
< <class 'pymath.calculus.expression.Expression'> [2, 1, '/'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 1 / 2>, 4, *] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 2, *, 1, 2, *, /] >
< <class 'pymath.calculus.expression.Expression'> [1, 2, *, 1, /] >
< <class 'pymath.calculus.expression.Expression'> [2, 1, /] >
"""
steps = [Expression([self, other, op.mul])]
@ -442,26 +442,26 @@ class Fraction(Explicable):
>>> f**0
1
>>> (f**0).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 0, '^'] >]
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 0, ^] >]
>>> f**1
< Fraction 3 / 4>
>>> (f**1).steps
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 1, '^'] >]
[< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 1, ^] >]
>>> f**3
< Fraction 27 / 64>
>>> print("\\n".join([repr(i) for i in (f**3).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 3, '^'] >
< <class 'pymath.calculus.expression.Expression'> [3, 3, '^', 4, 3, '^', '/'] >
< <class 'pymath.calculus.expression.Expression'> [27, 64, '/'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 4>, 3, ^] >
< <class 'pymath.calculus.expression.Expression'> [3, 3, ^, 4, 3, ^, /] >
< <class 'pymath.calculus.expression.Expression'> [27, 64, /] >
>>> f = Fraction(6, 4)
>>> f**3
< Fraction 27 / 8>
>>> print("\\n".join([repr(i) for i in (f**3).steps]))
< <class 'pymath.calculus.expression.Expression'> [< Fraction 6 / 4>, 3, '^'] >
< <class 'pymath.calculus.expression.Expression'> [6, 3, '^', 4, 3, '^', '/'] >
< <class 'pymath.calculus.expression.Expression'> [216, 64, '/'] >
< <class 'pymath.calculus.expression.Expression'> [216, 64, '/'] >
< <class 'pymath.calculus.expression.Expression'> [27, 8, '*', 8, 8, '*', '/'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 6 / 4>, 3, ^] >
< <class 'pymath.calculus.expression.Expression'> [6, 3, ^, 4, 3, ^, /] >
< <class 'pymath.calculus.expression.Expression'> [216, 64, /] >
< <class 'pymath.calculus.expression.Expression'> [216, 64, /] >
< <class 'pymath.calculus.expression.Expression'> [27, 8, *, 8, 8, *, /] >
"""
if not isinstance(power, int):

View File

@ -299,11 +299,11 @@ def postfix_op(numbers, operator, neutral = 0):
>>> postfix_op([1], op.add)
[1]
>>> postfix_op([1, 2], op.add)
[1, 2, '+']
[1, 2, +]
>>> postfix_op([1, 2, 3], op.add)
[1, 2, '+', 3, '+']
[1, 2, +, 3, +]
>>> postfix_op([1, 2, 3], op.mul)
[1, 2, '*', 3, '*']
[1, 2, *, 3, *]
>>> postfix_op(1, op.add)
[1]
>>> postfix_op([], op.add)

View File

@ -103,15 +103,15 @@ class Polynom(AbstractPolynom):
>>> P = Polynom([1, 2, 3])
>>> P.mainOp
'+'
+
>>> P.name
'P'
>>> P._letter
'x'
>>> Polynom([1]).mainOp
'*'
*
>>> Polynom([0, 0, 3]).mainOp
'*'
*
>>> Polynom([1, 2, 3])._letter
'x'
>>> Polynom([1, 2, 3], "y")._letter
@ -182,67 +182,6 @@ for name, func in inspect.getmembers(Polynom):
setattr(Polynom, name, polynom_factory(func))
if __name__ == '__main__':
#from .fraction import Fraction
# with Expression.tmp_render(txt):
# p = Polynom([1, 2, 3])
# q = Polynom([4, 5, 6])
# for i in (p*q).explain():
# print(i)
# r = Polynom([0,1])
# for i in (r*3).explain():
# print(i)
# print("q = ", q)
# r = q.reduce()
# print("r = ", r)
# for i in r.explain():
# print("q = ", i)
# print(p-q)
# for i in p-q:
# print(i)
# Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg
# 2 with positive Delta (ax^2 + bx + c)
#import doctest
# doctest.testmod(optionflags=doctest.ELLIPSIS)
# while True:
# P = Polynom.random(degree = 2)
# e = Expression.random("{a}/{b}")
# try:
# P(e)
# except RuntimeError:
# print(" P -> " + str(P))
# print(" e -> " + str(e))
#
# import sys
# sys.setrecursionlimit(100)
from .fraction import Fraction
from itertools import permutations
P = Polynom([-5, 6, -4])
f = Fraction(2, 5)
P(f)
try:
P(f)
except Exception as e:
print(e)
print("-----------------\n")
f = Fraction(2, 15)
print(str(P).replace('x', '(' + str(f) + ')'), "= ", P(f))
print("-----------------\n")
f = Fraction(2, 3)
print(P(f))
#coefs_p = [[(i-2),(j-2)] for i,j in permutations(range(20),2)]
#fractions = [Fraction(i,j) for i,j in coefs_p if j!=0]
# for f in fractions:
# try:
# P(f)
# #print("ok f -> " + str(f))
# except RuntimeError:
# print(" f -> " + str(f))
# -----------------------------