__repr__ for *Polynom
This commit is contained in:
parent
976cbbdf03
commit
04099040d4
@ -126,7 +126,10 @@ class AbstractPolynom(Explicable):
|
||||
return str(Expression(self.postfix_tokens))
|
||||
|
||||
def __repr__(self):
|
||||
return "< " + str(self.__class__) + " " + str(self._coef) + ">"
|
||||
return "< {cls} {letter} {coefs}>".format(
|
||||
cls = str(self.__class__).split('.')[-1][:-2],
|
||||
letter = str(self._letter),
|
||||
coefs = str(self._coef))
|
||||
|
||||
def coef_postfix(self, a, i):
|
||||
"""Return the postfix display of a coeficient
|
||||
@ -275,9 +278,9 @@ class AbstractPolynom(Explicable):
|
||||
|
||||
>>> P = AbstractPolynom([1,2,3])
|
||||
>>> P.conv2poly(1)
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1]>
|
||||
< AbstractPolynom x [1]>
|
||||
>>> P.conv2poly(0)
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0]>
|
||||
< AbstractPolynom x [0]>
|
||||
|
||||
"""
|
||||
if isNumber(other) and not isPolynom(other):
|
||||
@ -296,13 +299,13 @@ class AbstractPolynom(Explicable):
|
||||
>>> P = AbstractPolynom([1,2,3])
|
||||
>>> Q = P.reduce()
|
||||
>>> Q
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, 2, 3]>
|
||||
< AbstractPolynom x [1, 2, 3]>
|
||||
>>> Q.steps
|
||||
[]
|
||||
>>> P = AbstractPolynom([[1,2], [3,4,5], 6])
|
||||
>>> Q = P.reduce()
|
||||
>>> Q
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, 12, 6]>
|
||||
< AbstractPolynom x [3, 12, 6]>
|
||||
>>> for i in Q.explain():
|
||||
... print(i)
|
||||
6 x^{ 2 } + 3 x + 4 x + 5 x + 1 + 2
|
||||
@ -310,7 +313,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]>]
|
||||
[< 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]>]
|
||||
"""
|
||||
|
||||
# TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET
|
||||
@ -382,7 +385,7 @@ class AbstractPolynom(Explicable):
|
||||
>>> Q = AbstractPolynom([4,5])
|
||||
>>> R = P+Q
|
||||
>>> R
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [5, 7, 3]>
|
||||
< AbstractPolynom x [5, 7, 3]>
|
||||
>>> for i in R.explain():
|
||||
... print(i)
|
||||
3 x^{ 2 } + 2 x + 1 + 5 x + 4
|
||||
@ -390,7 +393,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, +, +] >, < AbstractPolynom x [[1, 4], [2, 5], 3]>, < AbstractPolynom x [< <class 'pymath.calculus.expression.Expression'> [1, 4, +] >, < <class 'pymath.calculus.expression.Expression'> [2, 5, +] >, 3]>]
|
||||
"""
|
||||
o_poly = self.conv2poly(other)
|
||||
|
||||
@ -413,7 +416,7 @@ class AbstractPolynom(Explicable):
|
||||
>>> P = AbstractPolynom([1,2,3])
|
||||
>>> Q = -P
|
||||
>>> Q
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [-1, -2, -3]>
|
||||
< AbstractPolynom x [-1, -2, -3]>
|
||||
>>> Q.steps
|
||||
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, -] >]
|
||||
"""
|
||||
@ -430,7 +433,7 @@ class AbstractPolynom(Explicable):
|
||||
>>> Q = AbstractPolynom([4,5,6])
|
||||
>>> R = P - Q
|
||||
>>> R
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [-3, -3, -3]>
|
||||
< AbstractPolynom x [-3, -3, -3]>
|
||||
>>> for i in R.explain():
|
||||
... print(i)
|
||||
3 x^{ 2 } + 2 x + 1 - ( 6 x^{ 2 } + 5 x + 4 )
|
||||
@ -439,7 +442,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, -, +] >, < 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, +] >]>]
|
||||
"""
|
||||
o_poly = self.conv2poly(other)
|
||||
ini_step = [Expression(self.postfix_tokens +
|
||||
@ -461,34 +464,34 @@ class AbstractPolynom(Explicable):
|
||||
|
||||
>>> p = AbstractPolynom([1,2])
|
||||
>>> p*3
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, 6]>
|
||||
< AbstractPolynom x [3, 6]>
|
||||
>>> for i in (p*3).explain():
|
||||
... print(i)
|
||||
( 2 x + 1 ) \times 3
|
||||
2 \times 3 x + 3
|
||||
6 x + 3
|
||||
>>> (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, *] >, < AbstractPolynom x [3, < <class 'pymath.calculus.expression.Expression'> [2, 3, *] >]>]
|
||||
>>> q = AbstractPolynom([0,0,4])
|
||||
>>> q*3
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 12]>
|
||||
< AbstractPolynom x [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, *] >, < AbstractPolynom x [0, 0, < <class 'pymath.calculus.expression.Expression'> [4, 3, *] >]>]
|
||||
>>> r = AbstractPolynom([0,1])
|
||||
>>> r*3
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 3]>
|
||||
< AbstractPolynom x [0, 3]>
|
||||
>>> (r*3).steps
|
||||
[< <class 'pymath.calculus.expression.Expression'> ['x', 3, *] >]
|
||||
>>> p*q
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, 8]>
|
||||
< AbstractPolynom x [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, ^, *, *] >, < AbstractPolynom x [0, 0, 4, < <class 'pymath.calculus.expression.Expression'> [2, 4, *] >]>]
|
||||
>>> p*r
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 1, 2]>
|
||||
< AbstractPolynom x [0, 1, 2]>
|
||||
>>> P = AbstractPolynom([1,2,3])
|
||||
>>> Q = AbstractPolynom([4,5,6])
|
||||
>>> P*Q
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [4, 13, 28, 27, 18]>
|
||||
< AbstractPolynom x [4, 13, 28, 27, 18]>
|
||||
"""
|
||||
o_poly = self.conv2poly(other)
|
||||
|
||||
@ -531,20 +534,20 @@ class AbstractPolynom(Explicable):
|
||||
|
||||
>>> p = AbstractPolynom([0,0,3])
|
||||
>>> p**2
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 9]>
|
||||
< AbstractPolynom x [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, ^] >, < AbstractPolynom x [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]>
|
||||
< AbstractPolynom x [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, +, *] >, < AbstractPolynom x [1, [2, 2], < <class 'pymath.calculus.expression.Expression'> [2, 2, *] >]>, < AbstractPolynom x [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]>
|
||||
< AbstractPolynom x [0, 0, 0, 0, 0, 0, 1]>
|
||||
>>> p = AbstractPolynom([1,2,3])
|
||||
>>> p**2
|
||||
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, 4, 10, 12, 9]>
|
||||
< AbstractPolynom x [1, 4, 10, 12, 9]>
|
||||
|
||||
"""
|
||||
if not type(power):
|
||||
|
@ -67,15 +67,15 @@ class Polynom(AbstractPolynom):
|
||||
/!\ variables need to be in brackets {}
|
||||
|
||||
>>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS
|
||||
< <class 'pymath.calculus.polynom.Polynom'> ...
|
||||
< Polynom x ...
|
||||
>>> Polynom.random(degree = 2) # doctest:+ELLIPSIS
|
||||
< <class 'pymath.calculus.polynomDeg2.Polynom_deg2'> ...
|
||||
< Polynom_deg2 x ...>
|
||||
>>> Polynom.random(degree = 3) # doctest:+ELLIPSIS
|
||||
< <class 'pymath.calculus.polynom.Polynom'> ...
|
||||
< Polynom x ...>
|
||||
>>> Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c)
|
||||
< <class 'pymath.calculus.polynomDeg2.Polynom_deg2'> ...
|
||||
< Polynom_deg2 x ...>
|
||||
>>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above
|
||||
< <class 'pymath.calculus.polynomDeg2.Polynom_deg2'> ...
|
||||
< Polynom_deg2 x ...>
|
||||
|
||||
"""
|
||||
if (degree > 0 and degree < 26):
|
||||
@ -161,7 +161,7 @@ class Polynom(AbstractPolynom):
|
||||
>>> P = Polynom([1, 2, 3])
|
||||
>>> Q = P.derivate()
|
||||
>>> Q
|
||||
< <class 'pymath.calculus.polynom.Polynom'> [2, 6]>
|
||||
< Polynom x [2, 6]>
|
||||
>>> print(Q.name)
|
||||
P'
|
||||
>>> for i in Q.explain():
|
||||
|
@ -17,7 +17,7 @@ def str2tokens(exp):
|
||||
>>> str2tokens('2*3+4')
|
||||
[2, 3, *, 4, +]
|
||||
>>> str2tokens('2x+4')
|
||||
[2, < <class 'pymath.calculus.polynom.Polynom'> [0, 1]>, *, 4, +]
|
||||
[2, < Polynom x [0, 1]>, *, 4, +]
|
||||
"""
|
||||
in_tokens = str2in_tokens(exp)
|
||||
post_tokens = in2post_fix(in_tokens)
|
||||
@ -40,7 +40,7 @@ def str2in_tokens(exp):
|
||||
>>> str2in_tokens('2.3*3+4')
|
||||
[Decimal('2.3'), '*', 3, '+', 4]
|
||||
>>> str2in_tokens('a*3+4')
|
||||
[< <class 'pymath.calculus.polynom.Polynom'> [0, 1]>, '*', 3, '+', 4]
|
||||
[< Polynom a [0, 1]>, '*', 3, '+', 4]
|
||||
"""
|
||||
tokens = ['', '']
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user