solve bugs with poly_facto still one!

This commit is contained in:
Lafrite 2015-04-07 08:46:59 +02:00
parent df305d0e0d
commit 983bc0fdc2
4 changed files with 51 additions and 37 deletions

View File

@ -277,9 +277,9 @@ class AbstractPolynom(Explicable):
>>> P = AbstractPolynom([1,2,3])
>>> P.conv2poly(1)
< AbstractPolynom [1]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [1]>
>>> P.conv2poly(0)
< AbstractPolynom [0]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [0]>
"""
if isNumber(other) and not isPolynom(other):
@ -297,15 +297,15 @@ class AbstractPolynom(Explicable):
>>> P = AbstractPolynom([1,2,3])
>>> Q = P.reduce()
>>> Q
< AbstractPolynom [1, 2, 3]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [1, 2, 3]>
>>> Q.steps
[]
>>> P = AbstractPolynom([[1,2], [3,4,5], 6])
>>> Q = P.reduce()
>>> Q
< AbstractPolynom [3, 12, 6]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [3, 12, 6]>
>>> Q.steps
[< AbstractPolynom [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < AbstractPolynom [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [7, 5, '+'] >, 6]>, < AbstractPolynom [3, < <class 'pymath.expression.Expression'> [7, 5, '+'] >, 6]>]
[< <class 'pymath.abstract_polynom.AbstractPolynom'> [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < <class 'pymath.abstract_polynom.AbstractPolynom'> [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
@ -399,9 +399,9 @@ class AbstractPolynom(Explicable):
>>> Q = AbstractPolynom([4,5])
>>> R = P+Q
>>> R
< AbstractPolynom [5, 7, 3]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [5, 7, 3]>
>>> R.steps
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < AbstractPolynom [< <class 'pymath.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.expression.Expression'> [2, 5, '+'] >, 3]>, < AbstractPolynom [< <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, '+', '+'] >, < <class 'pymath.abstract_polynom.AbstractPolynom'> [< <class 'pymath.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.expression.Expression'> [2, 5, '+'] >, 3]>]
"""
o_poly = self.conv2poly(other)
@ -423,7 +423,7 @@ class AbstractPolynom(Explicable):
>>> P = AbstractPolynom([1,2,3])
>>> Q = -P
>>> Q
< AbstractPolynom [-1, -2, -3]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [-1, -2, -3]>
>>> Q.steps
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', '-'] >]
"""
@ -439,9 +439,15 @@ class AbstractPolynom(Explicable):
>>> Q = AbstractPolynom([4,5,6])
>>> R = P - Q
>>> R
< AbstractPolynom [-3, -3, -3]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [-3, -3, -3]>
>>> 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, '-', '+'] >, < AbstractPolynom [< <class 'pymath.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.expression.Expression'> [3, -6, '+'] >]>, < AbstractPolynom [< <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, '-', '+'] >, < <class 'pymath.abstract_polynom.AbstractPolynom'> [< <class 'pymath.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.expression.Expression'> [3, -6, '+'] >]>]
>>> for i in R.explain():
... print(i)
3 x^{ 2 } + 2 x + 1 - ( 6 x^{ 2 } + 5 x + 4 )
3 x^{ 2 } + 2 x + 1 - 6 x^{ 2 } - 5 x - 4
( 3 - 6 ) x^{ 2 } + ( 2 - 5 ) x + 1 - 4
- 3 x^{ 2 } - 3 x - 3
"""
o_poly = self.conv2poly(other)
ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])]
@ -463,29 +469,29 @@ class AbstractPolynom(Explicable):
>>> p = AbstractPolynom([1,2])
>>> p*3
< AbstractPolynom [3, 6]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [3, 6]>
>>> (p*3).steps
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 3, '*'] >], < AbstractPolynom [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>, < AbstractPolynom [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>]
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 3, '*'] >], < <class 'pymath.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>]
>>> q = AbstractPolynom([0,0,4])
>>> q*3
< AbstractPolynom [0, 0, 12]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 0, 12]>
>>> (q*3).steps
[[< <class 'pymath.expression.Expression'> [4, 'x', 2, '^', '*', 3, '*'] >], < AbstractPolynom [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>, < AbstractPolynom [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>]
[[< <class 'pymath.expression.Expression'> [4, 'x', 2, '^', '*', 3, '*'] >], < <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>]
>>> r = AbstractPolynom([0,1])
>>> r*3
< AbstractPolynom [0, 3]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 3]>
>>> (r*3).steps
[[< <class 'pymath.expression.Expression'> ['x', 3, '*'] >]]
>>> p*q
< AbstractPolynom [0, 0, 4, 8]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 0, 4, 8]>
>>> (p*q).steps
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < AbstractPolynom [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>, < AbstractPolynom [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>]
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>]
>>> p*r
< AbstractPolynom [0, 1, 2]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 1, 2]>
>>> P = AbstractPolynom([1,2,3])
>>> Q = AbstractPolynom([4,5,6])
>>> P*Q
< AbstractPolynom [4, 13, 28, 27, 18]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [4, 13, 28, 27, 18]>
"""
# TODO: Je trouve qu'elle grille trop d'étapes... |ven. févr. 27 19:08:44 CET 2015
o_poly = self.conv2poly(other)
@ -528,20 +534,20 @@ class AbstractPolynom(Explicable):
>>> p = AbstractPolynom([0,0,3])
>>> p**2
< AbstractPolynom [0, 0, 0, 0, 9]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 9]>
>>> (p**2).steps
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, '^'] >, < AbstractPolynom [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>, < AbstractPolynom [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>]
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, '^'] >, < <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>]
>>> p = AbstractPolynom([1,2])
>>> p**2
< AbstractPolynom [1, 4, 4]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [1, 4, 4]>
>>> (p**2).steps
[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, '^'] >, [< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < AbstractPolynom [1, < <class 'pymath.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.expression.Expression'> [2, 2, '*'] >]>, < AbstractPolynom [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, '+', '*'] >], < <class 'pymath.abstract_polynom.AbstractPolynom'> [1, < <class 'pymath.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.expression.Expression'> [2, 2, '*'] >]>]
>>> p = AbstractPolynom([0,0,1])
>>> p**3
< AbstractPolynom [0, 0, 0, 0, 0, 0, 1]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 0, 0, 1]>
>>> p = AbstractPolynom([1,2,3])
>>> p**2
< AbstractPolynom [1, 4, 10, 12, 9]>
< <class 'pymath.abstract_polynom.AbstractPolynom'> [1, 4, 10, 12, 9]>
"""
if not type(power):

View File

@ -18,11 +18,15 @@ def polynom_factory(func):
@wraps(func)
def wrapper(*args, **kwrds):
P = func(*args, **kwrds)
if isinstance(P,Polynom) and P.degree == 2:
if issubclass(type(P),AbstractPolynom) and P.degree == 2:
from .polynomDeg2 import Polynom_deg2
new_P = Polynom_deg2(poly=P)
new_P.steps = P.steps
return new_P
elif issubclass(type(P),AbstractPolynom):
new_P = Polynom(poly=P)
new_P.steps = P.steps
return new_P
else:
return P
return wrapper
@ -54,13 +58,15 @@ class Polynom(AbstractPolynom):
/!\ variables need to be in brackets {}
>>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS
< Polynom ...
< <class 'pymath.polynom.Polynom'> ...
>>> Polynom.random(degree = 2) # doctest:+ELLIPSIS
< Polynom ...
< <class 'pymath.polynomDeg2.Polynom_deg2'> ...
>>> Polynom.random(degree = 3) # doctest:+ELLIPSIS
< <class 'pymath.polynom.Polynom'> ...
>>> Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c)
< Polynom ...
< <class 'pymath.polynomDeg2.Polynom_deg2'> ...
>>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above
< Polynom ...
< <class 'pymath.polynomDeg2.Polynom_deg2'> ...
"""
if (degree > 0 and degree < 26):
@ -76,7 +82,7 @@ class Polynom(AbstractPolynom):
# Création du polynom
return Polynom(coefs = coefs, letter = letter, name = name)
def __init__(self, coefs = [1], letter = "x", name = "P"):
def __init__(self, coefs = [1], letter = "x", name = "P", poly = 0):
"""Initiate the polynom
:param coef: coefficients of the polynom (ascending degree sorted)
@ -105,6 +111,11 @@ class Polynom(AbstractPolynom):
>>> Polynom([1, 2, 3], name = "Q").name
'Q'
"""
if poly:
coefs = poly._coef
letter = poly._letter
name = poly.name
super(Polynom, self).__init__(coefs, letter, name)
def __call__(self, value):
@ -140,7 +151,7 @@ class Polynom(AbstractPolynom):
>>> P = Polynom([1, 2, 3])
>>> Q = P.derivate()
>>> Q
< Polynom [2, 6]>
< <class 'pymath.polynom.Polynom'> [2, 6]>
>>> print(Q.name)
P'
>>> for i in Q.explain():

View File

@ -5,6 +5,7 @@ from .polynom import Polynom
from .expression import Expression
from .operator import op
from .random_expression import RdExpression
from math import sqrt
__all__ = ["Polynom_deg2"]
@ -219,10 +220,6 @@ class Polynom_deg2(Polynom):
return "\\tkzTabVar{-/{}, +/{$" + str(beta) + "$}, -/{}}"
if __name__ == '__main__':
# from .render import txt
# with Expression.tmp_render(txt):

View File

@ -12,7 +12,7 @@ def str2tokens(exp):
>>> str2tokens('2*3+4')
[2, 3, '*', 4, '+']
>>> str2tokens('2x+4')
[2, < Polynom [0, 1]>, '*', 4, '+']
[2, < <class 'pymath.polynom.Polynom'> [0, 1]>, '*', 4, '+']
"""
in_tokens = str2in_tokens(exp)
post_tokens = in2post_fix(in_tokens)