Reduce for polynoms works
This commit is contained in:
parent
e2e3287420
commit
ce94c27430
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
|
|
||||||
from .expression import Expression
|
from .expression import Expression
|
||||||
|
from .explicable import Explicable
|
||||||
from .operator import op
|
from .operator import op
|
||||||
from .generic import spe_zip, expand_list, isNumber, transpose_fill, flatten_list, isPolynom
|
from .generic import spe_zip, expand_list, isNumber, transpose_fill, flatten_list, isPolynom
|
||||||
from .render import txt
|
from .render import txt
|
||||||
@ -25,7 +26,7 @@ def power_cache(fun):
|
|||||||
return poly_powered
|
return poly_powered
|
||||||
return cached_fun
|
return cached_fun
|
||||||
|
|
||||||
class Polynom(object):
|
class Polynom(Explicable):
|
||||||
|
|
||||||
"""Docstring for Polynom. """
|
"""Docstring for Polynom. """
|
||||||
|
|
||||||
@ -100,9 +101,9 @@ class Polynom(object):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
if isNumber(value):
|
if isNumber(value):
|
||||||
postfix_exp = [value if i==self._letter else i for i in self.postfix]
|
postfix_exp = [value if i==self._letter else i for i in self.postfix_tokens]
|
||||||
else:
|
else:
|
||||||
postfix_exp = [Expression(value) if i==self._letter else i for i in self.postfix]
|
postfix_exp = [Expression(value) if i==self._letter else i for i in self.postfix_tokens]
|
||||||
|
|
||||||
return Expression(postfix_exp)
|
return Expression(postfix_exp)
|
||||||
|
|
||||||
@ -147,11 +148,17 @@ class Polynom(object):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(Expression(self.postfix))
|
return str(Expression(self.postfix_tokens))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "< Polynom " + str(self._coef) + ">"
|
return "< Polynom " + str(self._coef) + ">"
|
||||||
|
|
||||||
|
def __txt__(self):
|
||||||
|
return self.postfix_tokens
|
||||||
|
|
||||||
|
def __tex__(self):
|
||||||
|
return self.postfix_tokens
|
||||||
|
|
||||||
def coef_postfix(self, a, i):
|
def coef_postfix(self, a, i):
|
||||||
"""Return the postfix display of a coeficient
|
"""Return the postfix display of a coeficient
|
||||||
|
|
||||||
@ -188,34 +195,34 @@ class Polynom(object):
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def postfix(self):
|
def postfix_tokens(self):
|
||||||
"""Return the postfix form of the polynom
|
"""Return the postfix form of the polynom
|
||||||
|
|
||||||
:returns: the postfix list of polynom's tokens
|
:returns: the postfix list of polynom's tokens
|
||||||
|
|
||||||
>>> p = Polynom([1, 2])
|
>>> p = Polynom([1, 2])
|
||||||
>>> p.postfix
|
>>> p.postfix_tokens
|
||||||
[2, 'x', '*', 1, '+']
|
[2, 'x', '*', 1, '+']
|
||||||
>>> p = Polynom([1, -2])
|
>>> p = Polynom([1, -2])
|
||||||
>>> p.postfix
|
>>> p.postfix_tokens
|
||||||
[2, 'x', '*', '-', 1, '+']
|
[2, 'x', '*', '-', 1, '+']
|
||||||
>>> p = Polynom([1,2,3])
|
>>> p = Polynom([1,2,3])
|
||||||
>>> p.postfix
|
>>> p.postfix_tokens
|
||||||
[3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
|
[3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
|
||||||
>>> p = Polynom([1,[2,3]])
|
>>> p = Polynom([1,[2,3]])
|
||||||
>>> p.postfix
|
>>> p.postfix_tokens
|
||||||
[2, 'x', '*', 3, 'x', '*', '+', 1, '+']
|
[2, 'x', '*', 3, 'x', '*', '+', 1, '+']
|
||||||
>>> p = Polynom([1,[2,-3]])
|
>>> p = Polynom([1,[2,-3]])
|
||||||
>>> p.postfix
|
>>> p.postfix_tokens
|
||||||
[2, 'x', '*', 3, 'x', '*', '-', 1, '+']
|
[2, 'x', '*', 3, 'x', '*', '-', 1, '+']
|
||||||
>>> p = Polynom([1,[-2,-3]])
|
>>> p = Polynom([1,[-2,-3]])
|
||||||
>>> p.postfix
|
>>> p.postfix_tokens
|
||||||
[2, 'x', '*', '-', 3, 'x', '*', '-', 1, '+']
|
[2, 'x', '*', '-', 3, 'x', '*', '-', 1, '+']
|
||||||
>>> from pymath.expression import Expression
|
>>> from pymath.expression import Expression
|
||||||
>>> from pymath.operator import op
|
>>> from pymath.operator import op
|
||||||
>>> e = Expression([2,3,op.add])
|
>>> e = Expression([2,3,op.add])
|
||||||
>>> p = Polynom([1,e])
|
>>> p = Polynom([1,e])
|
||||||
>>> p.postfix
|
>>> p.postfix_tokens
|
||||||
[2, 3, '+', 'x', '*', 1, '+']
|
[2, 3, '+', 'x', '*', 1, '+']
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -298,8 +305,22 @@ class Polynom(object):
|
|||||||
"""Compute coefficients which have same degree
|
"""Compute coefficients which have same degree
|
||||||
|
|
||||||
:returns: new Polynom with numbers coefficients
|
:returns: new Polynom with numbers coefficients
|
||||||
|
|
||||||
|
>>> P = Polynom([1,2,3])
|
||||||
|
>>> Q = P.reduce()
|
||||||
|
>>> Q
|
||||||
|
< Polynom [1, 2, 3]>
|
||||||
|
>>> Q.steps()
|
||||||
|
[]
|
||||||
|
>>> P = Polynom([[1,2], [3,4,5], 6])
|
||||||
|
>>> Q = P.reduce()
|
||||||
|
>>> Q
|
||||||
|
< Polynom [3, 12, 6]>
|
||||||
|
>>> Q.steps
|
||||||
"""
|
"""
|
||||||
steps = []
|
|
||||||
|
# TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015
|
||||||
|
|
||||||
# gather steps for every coeficients
|
# gather steps for every coeficients
|
||||||
coefs_steps = []
|
coefs_steps = []
|
||||||
for coef in self._coef:
|
for coef in self._coef:
|
||||||
@ -311,20 +332,20 @@ class Polynom(object):
|
|||||||
coef_exp = Expression(postfix_add)
|
coef_exp = Expression(postfix_add)
|
||||||
|
|
||||||
with Expression.tmp_render():
|
with Expression.tmp_render():
|
||||||
coef_steps = list(coef_exp.simplify())
|
coef_steps = list(coef_exp.simplify().explain())
|
||||||
|
|
||||||
#print('\t 1.coef_steps -> ', coef_steps)
|
#print('\t 1.coef_steps -> ', coef_steps)
|
||||||
|
|
||||||
elif type(coef) == Expression:
|
elif type(coef) == Expression:
|
||||||
|
|
||||||
with Expression.tmp_render():
|
with Expression.tmp_render():
|
||||||
coef_steps = list(coef.simplify())
|
coef_steps = list(coef.simplify().explain())
|
||||||
|
|
||||||
#print('\t 2.coef_steps -> ', coef_steps)
|
#print('\t 2.coef_steps -> ', coef_steps)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
coef_steps += coef.simplify()
|
coef_steps += coef.simplify().explaine()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
coef_steps = [coef]
|
coef_steps = [coef]
|
||||||
|
|
||||||
@ -335,9 +356,12 @@ class Polynom(object):
|
|||||||
#print('\t coefs_steps -> ', coefs_steps)
|
#print('\t coefs_steps -> ', coefs_steps)
|
||||||
|
|
||||||
# On retourne la matrice
|
# On retourne la matrice
|
||||||
ans = []
|
steps = []
|
||||||
for coefs in transpose_fill(coefs_steps):
|
for coefs in transpose_fill(coefs_steps):
|
||||||
ans.append(Polynom(coefs, self._letter))
|
steps.append(Polynom(coefs, self._letter))
|
||||||
|
|
||||||
|
ans, steps = steps[-1], steps[:-1]
|
||||||
|
ans.steps = steps
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
@ -513,19 +537,19 @@ def test(p,q):
|
|||||||
print(p, "+", q)
|
print(p, "+", q)
|
||||||
for i in (p + q):
|
for i in (p + q):
|
||||||
#print(repr(i))
|
#print(repr(i))
|
||||||
#print("\t", str(i.postfix))
|
#print("\t", str(i.postfix_tokens))
|
||||||
print(i)
|
print(i)
|
||||||
|
|
||||||
print("\n Moins ------")
|
print("\n Moins ------")
|
||||||
for i in (p - q):
|
for i in (p - q):
|
||||||
#print(repr(i))
|
#print(repr(i))
|
||||||
#print("\t", str(i.postfix))
|
#print("\t", str(i.postfix_tokens))
|
||||||
print(i)
|
print(i)
|
||||||
|
|
||||||
print("\n Multiplier ------")
|
print("\n Multiplier ------")
|
||||||
for i in (p * q):
|
for i in (p * q):
|
||||||
#print(repr(i))
|
#print(repr(i))
|
||||||
#print("\t", str(i.postfix))
|
#print("\t", str(i.postfix_tokens))
|
||||||
print(i)
|
print(i)
|
||||||
|
|
||||||
print("\n Evaluer p ------")
|
print("\n Evaluer p ------")
|
||||||
@ -541,14 +565,20 @@ if __name__ == '__main__':
|
|||||||
#from .fraction import Fraction
|
#from .fraction import Fraction
|
||||||
with Expression.tmp_render(txt):
|
with Expression.tmp_render(txt):
|
||||||
p = Polynom([10, -5])
|
p = Polynom([10, -5])
|
||||||
q = Polynom([3, -9])
|
print(p.reduce())
|
||||||
print(p-q)
|
q = Polynom([[1,2], [3,4,5], 6])
|
||||||
for i in p-q:
|
print("q = ", q)
|
||||||
print(i)
|
r = q.reduce()
|
||||||
|
print("r = ", r)
|
||||||
|
for i in r.explain():
|
||||||
|
print("q = ", i)
|
||||||
|
#print(p-q)
|
||||||
|
#for i in p-q:
|
||||||
|
# print(i)
|
||||||
|
|
||||||
|
|
||||||
import doctest
|
#import doctest
|
||||||
doctest.testmod()
|
#doctest.testmod()
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user