start adapting polynom
This commit is contained in:
parent
73e6c74c72
commit
4920bbd27d
@ -2,6 +2,8 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
|
||||||
|
from itertools import zip_longest
|
||||||
|
|
||||||
class Stack(object):
|
class Stack(object):
|
||||||
"""Docstring for Stack """
|
"""Docstring for Stack """
|
||||||
|
|
||||||
@ -243,6 +245,27 @@ def convolution_dict(D1, D2, op = lambda x,y:x*y,\
|
|||||||
|
|
||||||
return new_dict
|
return new_dict
|
||||||
|
|
||||||
|
def spe_zip(l1,l2):
|
||||||
|
"""Zip two lists, if a list is longer, only it's element are taken
|
||||||
|
|
||||||
|
>>> spe_zip([1,2], [3,4])
|
||||||
|
[[1, 3], [2, 4]]
|
||||||
|
>>> spe_zip([1,2], [3,4,5])
|
||||||
|
[[1, 3], [2, 4], 5]
|
||||||
|
"""
|
||||||
|
|
||||||
|
tmp = list(zip_longest(l1,l2))
|
||||||
|
ans = []
|
||||||
|
for i in tmp:
|
||||||
|
if None in i:
|
||||||
|
j = [a for a in i if i != None][-1]
|
||||||
|
else:
|
||||||
|
j = list(i)
|
||||||
|
ans.append(j)
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def isOperator(exp):
|
def isOperator(exp):
|
||||||
"""Check if the expression is an opération in "+-*/:^"
|
"""Check if the expression is an opération in "+-*/:^"
|
||||||
|
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
|
||||||
from pymath.fraction import Fraction
|
from .expression import Expression
|
||||||
from pymath.expression import Expression
|
from .generic import spe_zip, sum_postfix, expand_list, isNumber
|
||||||
from pymath.generic import mix_list, sum_postfix, expand_list
|
from .render import txt
|
||||||
|
|
||||||
__all__ = ["Polynom"]
|
__all__ = ["Polynom"]
|
||||||
|
|
||||||
@ -16,11 +16,11 @@ class Polynom(object):
|
|||||||
def __init__(self, coef = [1], letter = "x" ):
|
def __init__(self, coef = [1], letter = "x" ):
|
||||||
"""Initiate the polynom
|
"""Initiate the polynom
|
||||||
|
|
||||||
:param coef: coefficients of the polynom (ascending degree sorted) - can be a list of list of coefficients.
|
:param coef: coefficients of the polynom (ascending degree sorted)
|
||||||
3 possibles type of list:
|
3 possibles type of coefficent:
|
||||||
- [a,b,..] simple list of coefficients. [1,2] designate 1 + 2x
|
- a : simple "number". [1,2] designate 1 + 2x
|
||||||
- [a, [c,b],...] list of coefficients with x^i repeated. [1,[2,3],4) designate 1 + 2x + 3x + 4x^2
|
- [a,b,c]: list of coeficient for same degree. [1,[2,3],4] designate 1 + 2x + 3x + 4x^2
|
||||||
- [a, [b,c,"+"],...] list of coefficients with arithmetic expression (postfix form) in. [1, [2,3,"+"], 4] designate 1 + (2+3)x + 4x^2
|
- a: a Expression. [1, Expression("2+3"), 4] designate 1 + (2+3)x + 4x^2
|
||||||
:param letter: the string describing the unknown
|
:param letter: the string describing the unknown
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -33,6 +33,8 @@ class Polynom(object):
|
|||||||
else:
|
else:
|
||||||
self.mainOp = "+"
|
self.mainOp = "+"
|
||||||
|
|
||||||
|
self._isPolynom
|
||||||
|
|
||||||
def feed_coef(self, l_coef):
|
def feed_coef(self, l_coef):
|
||||||
"""Feed coef of the polynom. Manage differently whether it's a number or an expression
|
"""Feed coef of the polynom. Manage differently whether it's a number or an expression
|
||||||
|
|
||||||
@ -64,7 +66,6 @@ class Polynom(object):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
# TODO: Voir si on peut utiliser un render |sam. juin 14 08:56:16 CEST 2014
|
# TODO: Voir si on peut utiliser un render |sam. juin 14 08:56:16 CEST 2014
|
||||||
from .renders import txt
|
|
||||||
return txt(self.get_postfix())
|
return txt(self.get_postfix())
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@ -78,6 +79,7 @@ class Polynom(object):
|
|||||||
:returns: postfix tokens of coef
|
:returns: postfix tokens of coef
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
# TODO: Couille certaine avec txt à qui il fait donner des opérateurs tout beau! |mar. nov. 11 13:08:35 CET 2014
|
||||||
ans =[]
|
ans =[]
|
||||||
if a == 0:
|
if a == 0:
|
||||||
return ans
|
return ans
|
||||||
@ -125,15 +127,22 @@ class Polynom(object):
|
|||||||
|
|
||||||
return self._postfix
|
return self._postfix
|
||||||
|
|
||||||
def convert_into_poly(self, other):
|
def conv2poly(self, other):
|
||||||
"""Convert anything (int and fract) into a polynom """
|
"""Convert anything number into a polynom"""
|
||||||
if type(other) in [int, Fraction]:
|
if isNumber(other) and not self.isPolynom(other):
|
||||||
return Polynom([other])
|
return Polynom([other])
|
||||||
elif type(other) == Polynom:
|
elif self.isPolynom(other):
|
||||||
return other
|
return other
|
||||||
else:
|
else:
|
||||||
raise ValueError(type(other) + " can't be converted into a polynom")
|
raise ValueError(type(other) + " can't be converted into a polynom")
|
||||||
|
|
||||||
|
def isPolynom(self, other):
|
||||||
|
try:
|
||||||
|
exp._isPolynom
|
||||||
|
except AttributeError:
|
||||||
|
return 0
|
||||||
|
return 1
|
||||||
|
|
||||||
def reduce(self):
|
def reduce(self):
|
||||||
"""Compute coefficients which have same degree
|
"""Compute coefficients which have same degree
|
||||||
|
|
||||||
@ -142,13 +151,12 @@ class Polynom(object):
|
|||||||
steps = []
|
steps = []
|
||||||
for a in self._coef:
|
for a in self._coef:
|
||||||
coef_steps = []
|
coef_steps = []
|
||||||
if type(a) == list and str(a[-1]) in "+-*^":
|
if type(a) == Expression:
|
||||||
# case coef is an arithmetic expression
|
# case coef is an arithmetic expression
|
||||||
exp = Expression(a)
|
coef_steps = list(a.simplify(render = lambda x:x))
|
||||||
coef_steps = list(exp.simplify(render = lambda x:x))
|
|
||||||
|
|
||||||
steps.append(coef_steps)
|
steps.append(coef_steps)
|
||||||
elif type(a) == list and str(a[-1]) not in "+-*^":
|
elif type(a) == list:
|
||||||
# case need to repeat the x^i
|
# case need to repeat the x^i
|
||||||
if [i for i in a if type(i) == list] != []:
|
if [i for i in a if type(i) == list] != []:
|
||||||
# first we simplify arithmetic exp
|
# first we simplify arithmetic exp
|
||||||
@ -189,19 +197,15 @@ class Polynom(object):
|
|||||||
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if type(other) in [int, Fraction, Polynom] or other.isnumeric():
|
o_poly = self.conv2poly(other)
|
||||||
o_poly = self.convert_into_poly(other)
|
|
||||||
|
|
||||||
return self._coef == o_poly._coef
|
return self._coef == o_poly._coef
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
steps = []
|
steps = []
|
||||||
|
|
||||||
o_poly = self.convert_into_poly(other)
|
o_poly = self.conv2poly(other)
|
||||||
|
|
||||||
n_coef = mix_list(self._coef, o_poly._coef)
|
n_coef = spe_zip(self._coef, o_poly._coef)
|
||||||
p = Polynom(n_coef)
|
p = Polynom(n_coef)
|
||||||
steps.append(p)
|
steps.append(p)
|
||||||
|
|
||||||
@ -215,19 +219,19 @@ class Polynom(object):
|
|||||||
return Polynom([-i for i in self._coef], letter = self._letter)
|
return Polynom([-i for i in self._coef], letter = self._letter)
|
||||||
|
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
o_poly = self.convert_into_poly(other)
|
o_poly = self.conv2poly(other)
|
||||||
o_poly = -o_poly
|
o_poly = -o_poly
|
||||||
|
|
||||||
return self.__add__(o_poly)
|
return self.__add__(o_poly)
|
||||||
|
|
||||||
def __rsub__(self, other):
|
def __rsub__(self, other):
|
||||||
o_poly = self.convert_into_poly(other)
|
o_poly = self.conv2poly(other)
|
||||||
|
|
||||||
return o_poly.__sub__(-self)
|
return o_poly.__sub__(-self)
|
||||||
|
|
||||||
def __mul__(self, other):
|
def __mul__(self, other):
|
||||||
steps = []
|
steps = []
|
||||||
o_poly = self.convert_into_poly(other)
|
o_poly = self.conv2poly(other)
|
||||||
|
|
||||||
coefs = []
|
coefs = []
|
||||||
for (i,a) in enumerate(self._coef):
|
for (i,a) in enumerate(self._coef):
|
||||||
@ -251,7 +255,7 @@ class Polynom(object):
|
|||||||
return steps
|
return steps
|
||||||
|
|
||||||
def __rmul__(self, other):
|
def __rmul__(self, other):
|
||||||
o_poly = self.convert_into_poly(other)
|
o_poly = self.conv2poly(other)
|
||||||
|
|
||||||
return o_poly.__mul__(self)
|
return o_poly.__mul__(self)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user