__mul__ for poly still __pow__ and an issue with random

This commit is contained in:
Lafrite 2015-02-27 19:24:27 +01:00
parent 6e0d36f411
commit 8e63c64a65

View File

@ -42,13 +42,13 @@ class Polynom(Explicable):
/!\ variables need to be in brackets {} /!\ variables need to be in brackets {}
>>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS >>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS
... < Polynom ...
>>> Polynom.random(degree = 2) # doctest:+ELLIPSIS >>> Polynom.random(degree = 2) # doctest:+ELLIPSIS
... < Polynom ...
>>> Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c) >>> Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c)
... < Polynom ...
>>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above >>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above
... < Polynom ...
""" """
if (degree > 0 and degree < 26): if (degree > 0 and degree < 26):
@ -326,7 +326,7 @@ class Polynom(Explicable):
>>> Q = P.reduce() >>> Q = P.reduce()
>>> Q >>> Q
< Polynom [1, 2, 3]> < Polynom [1, 2, 3]>
>>> Q.steps() >>> Q.steps
[] []
>>> P = Polynom([[1,2], [3,4,5], 6]) >>> P = Polynom([[1,2], [3,4,5], 6])
>>> Q = P.reduce() >>> Q = P.reduce()
@ -437,7 +437,8 @@ class Polynom(Explicable):
return ans return ans
def __radd__(self, other): def __radd__(self, other):
return self.__add__(other) o_poly = self.conv2poly(other)
return o_poly.__add__(self)
def __neg__(self): def __neg__(self):
""" overload - (as arity 1 operator) """ overload - (as arity 1 operator)
@ -478,27 +479,35 @@ class Polynom(Explicable):
def __rsub__(self, other): def __rsub__(self, other):
o_poly = self.conv2poly(other) o_poly = self.conv2poly(other)
return o_poly.__sub__(-self) return o_poly.__sub__(self)
def __mul__(self, other): def __mul__(self, other):
""" Overload * """ Overload *
>>> p = Polynom([1,2]) >>> p = Polynom([1,2])
>>> p*3 >>> p*3
[< Polynom [3, < Expression [2, 3, '*']>]>, < Polynom [3, < Expression [2, 3, '*']>]>, < Polynom [3, 6]>] < Polynom [3, 6]>
>>> (p*3).steps
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 3, '*'] >], < Polynom [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>, < Polynom [3, < <class 'pymath.expression.Expression'> [2, 3, '*'] >]>]
>>> q = Polynom([0,0,4]) >>> q = Polynom([0,0,4])
>>> q*3 >>> q*3
[< Polynom [0, 0, < Expression [4, 3, '*']>]>, < Polynom [0, 0, < Expression [4, 3, '*']>]>, < Polynom [0, 0, 12]>] < Polynom [0, 0, 12]>
>>> (q*3).steps
[[< <class 'pymath.expression.Expression'> [4, 'x', 2, '^', '*', 3, '*'] >], < Polynom [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>, < Polynom [0, 0, < <class 'pymath.expression.Expression'> [4, 3, '*'] >]>]
>>> r = Polynom([0,1]) >>> r = Polynom([0,1])
>>> r*3 >>> r*3
[< Polynom [0, 3]>, < Polynom [0, 3]>] < Polynom [0, 3]>
>>> (r*3).steps
[[< <class 'pymath.expression.Expression'> ['x', 3, '*'] >]]
>>> p*q >>> p*q
[< Polynom [0, 0, 4, < Expression [2, 4, '*']>]>, < Polynom [0, 0, 4, < Expression [2, 4, '*']>]>, < Polynom [0, 0, 4, 8]>] < Polynom [0, 0, 4, 8]>
>>> (p*q).steps
[[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < Polynom [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>, < Polynom [0, 0, 4, < <class 'pymath.expression.Expression'> [2, 4, '*'] >]>]
>>> p*r >>> p*r
[< Polynom [0, 1, 2]>, < Polynom [0, 1, 2]>] < Polynom [0, 1, 2]>
""" """
steps = [] # TODO: Je trouve qu'elle grille trop d'étapes... |ven. févr. 27 19:08:44 CET 2015
o_poly = self.conv2poly(other) o_poly = self.conv2poly(other)
coefs = [] coefs = []
@ -521,13 +530,11 @@ class Polynom(Explicable):
coefs.append(elem) coefs.append(elem)
p = Polynom(coefs, letter = self._letter) p = Polynom(coefs, letter = self._letter)
steps.append(p) ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.mul])]
ans = p.simplify()
steps += p.simplify() ans.steps = [ini_step] + ans.steps
return ans
#print("steps -> \n", "\n".join(["\t {}".format(s.postfix) for s in steps]))
return steps
def __rmul__(self, other): def __rmul__(self, other):
o_poly = self.conv2poly(other) o_poly = self.conv2poly(other)
@ -617,17 +624,21 @@ def test(p,q):
if __name__ == '__main__': 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([1,2,3])
print(p.reduce()) q = Polynom([0, 2])
q = Polynom([[1,2], [3,4,5], 6]) for i in (p*q).explain():
print("q = ", q) print(i)
r = q.reduce() r = Polynom([0,1])
print("r = ", r) for i in (r*3).explain():
for i in r.explain(): print(i)
print("q = ", i) # print("q = ", q)
#print(p-q) # r = q.reduce()
#for i in p-q: # print("r = ", r)
# print(i) # for i in r.explain():
# print("q = ", i)
# print(p-q)
# for i in p-q:
# print(i)
import doctest import doctest