2014-11-11 09:45:45 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
2014-11-11 15:52:30 +00:00
|
|
|
from .expression import Expression
|
2015-02-27 17:04:50 +00:00
|
|
|
from .explicable import Explicable
|
2014-11-11 20:00:45 +00:00
|
|
|
from .operator import op
|
2015-03-07 08:15:38 +00:00
|
|
|
from .generic import spe_zip, expand_list, isNumber, transpose_fill, flatten_list, isPolynom, isNumerand
|
2015-03-07 10:05:15 +00:00
|
|
|
from .render import txt,tex
|
2014-12-19 16:17:25 +00:00
|
|
|
from .random_expression import RdExpression
|
2014-11-11 20:00:45 +00:00
|
|
|
from itertools import chain
|
2015-02-27 21:16:18 +00:00
|
|
|
from functools import wraps
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
__all__ = ["Polynom"]
|
|
|
|
|
|
|
|
|
2014-12-22 19:42:18 +00:00
|
|
|
def power_cache(fun):
|
|
|
|
"""Decorator which cache calculated powers of polynoms """
|
|
|
|
cache = {}
|
2015-02-27 21:16:18 +00:00
|
|
|
@wraps(fun)
|
2014-12-22 19:42:18 +00:00
|
|
|
def cached_fun(self, power):
|
2015-02-21 10:20:42 +00:00
|
|
|
#print("cache -> ", cache)
|
2014-12-22 19:42:18 +00:00
|
|
|
if (tuple(self._coef), power) in cache.keys():
|
|
|
|
return cache[(tuple(self._coef), power)]
|
|
|
|
else:
|
|
|
|
poly_powered = fun(self, power)
|
|
|
|
cache[(tuple(self._coef), power)] = poly_powered
|
|
|
|
return poly_powered
|
|
|
|
return cached_fun
|
|
|
|
|
2015-02-27 17:04:50 +00:00
|
|
|
class Polynom(Explicable):
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
"""Docstring for Polynom. """
|
|
|
|
|
2014-12-19 16:17:25 +00:00
|
|
|
@classmethod
|
2015-03-08 17:51:53 +00:00
|
|
|
def random(self, coefs_form=[], conditions=[], letter = "x", degree = 0, name = "P"):
|
2014-12-19 16:17:25 +00:00
|
|
|
""" Create a random polynom from coefs_form and conditions
|
|
|
|
|
|
|
|
:param coefs_form: list of forms (one by coef) (ascending degree sorted)
|
|
|
|
:param conditions: condition on variables
|
2015-02-21 10:01:34 +00:00
|
|
|
:param letter: the letter for the polynom
|
|
|
|
:param degree: degree of the polynom (can't be used with coefs_form, it will be overwrite) - can't be higher than 26 (number of letters in alphabet)
|
2014-12-19 16:17:25 +00:00
|
|
|
|
|
|
|
/!\ variables need to be in brackets {}
|
|
|
|
|
2015-02-21 10:01:34 +00:00
|
|
|
>>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS
|
2015-02-27 18:24:27 +00:00
|
|
|
< Polynom ...
|
2015-02-21 10:01:34 +00:00
|
|
|
>>> Polynom.random(degree = 2) # doctest:+ELLIPSIS
|
2015-02-27 18:24:27 +00:00
|
|
|
< Polynom ...
|
2015-02-21 10:01:34 +00:00
|
|
|
>>> Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c)
|
2015-02-27 18:24:27 +00:00
|
|
|
< Polynom ...
|
2015-02-21 10:01:34 +00:00
|
|
|
>>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above
|
2015-02-27 18:24:27 +00:00
|
|
|
< Polynom ...
|
2015-02-21 10:01:34 +00:00
|
|
|
|
2014-12-19 16:17:25 +00:00
|
|
|
"""
|
2015-02-21 10:01:34 +00:00
|
|
|
if (degree > 0 and degree < 26):
|
|
|
|
# Générer assez de lettre pour les coefs
|
|
|
|
coefs_name = map(chr, range(97, 98+degree))
|
2015-02-27 21:26:53 +00:00
|
|
|
coefs_form = ["{" + i + "}" for i in coefs_name][::-1]
|
2015-02-21 10:01:34 +00:00
|
|
|
|
2014-12-19 16:17:25 +00:00
|
|
|
form = str(coefs_form)
|
2015-02-21 10:01:34 +00:00
|
|
|
# On créé les valeurs toutes concaténées dans un string
|
2014-12-19 16:17:25 +00:00
|
|
|
coefs = RdExpression(form, conditions)()
|
2015-02-21 10:01:34 +00:00
|
|
|
# On "parse" ce string pour créer les coefs
|
2014-12-19 16:29:12 +00:00
|
|
|
coefs = [eval(i) if type(i)==str else i for i in eval(coefs)]
|
2015-02-21 10:01:34 +00:00
|
|
|
# Création du polynom
|
2015-03-08 17:56:59 +00:00
|
|
|
return Polynom(coefs = coefs, letter = letter, name = name)
|
2014-12-19 16:17:25 +00:00
|
|
|
|
2015-03-08 17:56:59 +00:00
|
|
|
def __init__(self, coefs = [1], letter = "x", name = "P"):
|
2014-11-11 09:45:45 +00:00
|
|
|
"""Initiate the polynom
|
|
|
|
|
2014-11-11 15:52:30 +00:00
|
|
|
:param coef: coefficients of the polynom (ascending degree sorted)
|
|
|
|
3 possibles type of coefficent:
|
|
|
|
- a : simple "number". [1,2] designate 1 + 2x
|
|
|
|
- [a,b,c]: list of coeficient for same degree. [1,[2,3],4] designate 1 + 2x + 3x + 4x^2
|
|
|
|
- a: a Expression. [1, Expression("2+3"), 4] designate 1 + (2+3)x + 4x^2
|
2014-11-11 09:45:45 +00:00
|
|
|
:param letter: the string describing the unknown
|
2015-03-08 17:51:53 +00:00
|
|
|
:param name: Name of the polynom
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-03-08 17:51:53 +00:00
|
|
|
>>> P = Polynom([1, 2, 3])
|
|
|
|
>>> P.mainOp
|
2014-11-11 20:00:45 +00:00
|
|
|
'+'
|
2015-03-08 17:51:53 +00:00
|
|
|
>>> P.name
|
|
|
|
'P'
|
|
|
|
>>> P._letter
|
|
|
|
'x'
|
2014-11-11 20:00:45 +00:00
|
|
|
>>> Polynom([1]).mainOp
|
|
|
|
'*'
|
2015-03-07 17:22:46 +00:00
|
|
|
>>> Polynom([0, 0, 3]).mainOp
|
|
|
|
'*'
|
|
|
|
>>> Polynom([1, 2, 3])._letter
|
2014-11-11 20:00:45 +00:00
|
|
|
'x'
|
|
|
|
>>> Polynom([1, 2, 3], "y")._letter
|
|
|
|
'y'
|
2015-03-08 17:51:53 +00:00
|
|
|
>>> Polynom([1, 2, 3], name = "Q").name
|
|
|
|
'Q'
|
2014-11-11 09:45:45 +00:00
|
|
|
"""
|
2015-02-27 21:16:18 +00:00
|
|
|
super(Polynom, self).__init__()
|
2015-03-06 15:51:43 +00:00
|
|
|
self.feed_coef(coefs)
|
2014-11-11 09:45:45 +00:00
|
|
|
self._letter = letter
|
2015-03-08 17:51:53 +00:00
|
|
|
self.name = name
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
if self.is_monom():
|
2015-03-07 17:22:46 +00:00
|
|
|
self.mainOp = op.mul
|
2014-11-11 09:45:45 +00:00
|
|
|
else:
|
2015-03-07 17:22:46 +00:00
|
|
|
self.mainOp = op.add
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:00:45 +00:00
|
|
|
self._isPolynom = 1
|
2014-11-11 15:52:30 +00:00
|
|
|
|
2014-12-21 17:03:22 +00:00
|
|
|
def __call__(self, value):
|
|
|
|
""" Evaluate the polynom in value
|
|
|
|
|
|
|
|
:returns: Expression ready to be simplify
|
|
|
|
|
2015-03-07 08:15:38 +00:00
|
|
|
>>> P = Polynom([1, 2, 3])
|
|
|
|
>>> P(2)
|
|
|
|
17
|
|
|
|
>>> for i in P(2).explain():
|
|
|
|
... print(i)
|
|
|
|
3 \\times 2^{ 2 } + 2 \\times 2 + 1
|
|
|
|
3 \\times 4 + 4 + 1
|
|
|
|
12 + 4 + 1
|
|
|
|
16 + 1
|
|
|
|
17
|
|
|
|
>>> Q = P("1+h")
|
|
|
|
>>> print(Q)
|
|
|
|
3 h^{ 2 } + 8 h + 6
|
|
|
|
>>> R = P(Q)
|
2014-12-21 17:03:22 +00:00
|
|
|
"""
|
2015-03-07 08:15:38 +00:00
|
|
|
if isNumerand(value) or Expression.isExpression(value):
|
2015-02-27 17:04:50 +00:00
|
|
|
postfix_exp = [value if i==self._letter else i for i in self.postfix_tokens]
|
2014-12-22 09:13:20 +00:00
|
|
|
else:
|
2015-02-27 17:04:50 +00:00
|
|
|
postfix_exp = [Expression(value) if i==self._letter else i for i in self.postfix_tokens]
|
2014-12-22 09:13:20 +00:00
|
|
|
|
2015-03-07 08:15:38 +00:00
|
|
|
return Expression(postfix_exp).simplify()
|
2014-12-21 17:03:22 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
def feed_coef(self, l_coef):
|
|
|
|
"""Feed coef of the polynom. Manage differently whether it's a number or an expression
|
|
|
|
|
|
|
|
:l_coef: list of coef
|
|
|
|
"""
|
|
|
|
self._coef = []
|
|
|
|
for coef in l_coef:
|
|
|
|
if type(coef) == list and len(coef)==1:
|
|
|
|
self._coef.append(coef[0])
|
|
|
|
else:
|
|
|
|
self._coef.append(coef)
|
|
|
|
|
2014-12-22 14:54:07 +00:00
|
|
|
@property
|
|
|
|
def degree(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
"""Getting the degree fo the polynom
|
|
|
|
|
|
|
|
:returns: the degree of the polynom
|
|
|
|
|
2014-12-22 14:54:07 +00:00
|
|
|
>>> Polynom([1, 2, 3]).degree
|
2014-11-11 20:00:45 +00:00
|
|
|
2
|
2014-12-22 14:54:07 +00:00
|
|
|
>>> Polynom([1]).degree
|
2014-11-11 20:00:45 +00:00
|
|
|
0
|
2014-11-11 09:45:45 +00:00
|
|
|
"""
|
|
|
|
return len(self._coef) - 1
|
|
|
|
|
|
|
|
def is_monom(self):
|
|
|
|
"""is the polynom a monom (only one coefficent)
|
2014-11-11 20:00:45 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
:returns: 1 if yes 0 otherwise
|
2014-11-11 20:00:45 +00:00
|
|
|
|
|
|
|
>>> Polynom([1, 2, 3]).is_monom()
|
|
|
|
0
|
|
|
|
>>> Polynom([1]).is_monom()
|
|
|
|
1
|
2014-11-11 09:45:45 +00:00
|
|
|
"""
|
|
|
|
if len([i for i in self._coef if i != 0])==1:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def __str__(self):
|
2015-02-27 17:04:50 +00:00
|
|
|
return str(Expression(self.postfix_tokens))
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "< Polynom " + str(self._coef) + ">"
|
|
|
|
|
2015-02-27 17:04:50 +00:00
|
|
|
def __txt__(self):
|
2015-03-07 10:05:15 +00:00
|
|
|
return txt(self.postfix_tokens)
|
2015-02-27 17:04:50 +00:00
|
|
|
|
|
|
|
def __tex__(self):
|
2015-03-07 10:05:15 +00:00
|
|
|
return tex(self.postfix_tokens)
|
2015-02-27 17:04:50 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
def coef_postfix(self, a, i):
|
|
|
|
"""Return the postfix display of a coeficient
|
|
|
|
|
2014-11-11 20:00:45 +00:00
|
|
|
:param a: value for the coeficient (/!\ as a postfix list)
|
2014-11-11 09:45:45 +00:00
|
|
|
:param i: power
|
|
|
|
:returns: postfix tokens of coef
|
|
|
|
|
2014-12-21 15:04:44 +00:00
|
|
|
>>> p = Polynom()
|
|
|
|
>>> p.coef_postfix([3],2)
|
|
|
|
[3, 'x', 2, '^', '*']
|
|
|
|
>>> p.coef_postfix([0],1)
|
|
|
|
[]
|
|
|
|
>>> p.coef_postfix([3],0)
|
|
|
|
[3]
|
|
|
|
>>> p.coef_postfix([3],1)
|
|
|
|
[3, 'x', '*']
|
|
|
|
>>> p.coef_postfix([1],1)
|
|
|
|
['x']
|
|
|
|
>>> p.coef_postfix([1],2)
|
|
|
|
['x', 2, '^']
|
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
"""
|
2014-11-11 15:52:30 +00:00
|
|
|
# TODO: Couille certaine avec txt à qui il fait donner des opérateurs tout beau! |mar. nov. 11 13:08:35 CET 2014
|
2014-11-11 09:45:45 +00:00
|
|
|
ans =[]
|
2014-11-14 15:20:02 +00:00
|
|
|
if a == [0]:
|
|
|
|
pass
|
|
|
|
elif i == 0:
|
2014-11-11 09:45:45 +00:00
|
|
|
ans = a
|
|
|
|
elif i == 1:
|
2014-11-11 20:00:45 +00:00
|
|
|
ans = a * (a!=[1]) + [self._letter] + [op.mul] * (a!=[1])
|
2014-11-11 09:45:45 +00:00
|
|
|
else:
|
2014-11-11 20:00:45 +00:00
|
|
|
ans = a * (a!=[1]) + [self._letter, i, op.pw] + [op.mul] * (a!=[1])
|
2014-11-14 15:20:02 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
return ans
|
|
|
|
|
2014-11-11 20:00:45 +00:00
|
|
|
@property
|
2015-02-27 17:04:50 +00:00
|
|
|
def postfix_tokens(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
"""Return the postfix form of the polynom
|
|
|
|
|
|
|
|
:returns: the postfix list of polynom's tokens
|
|
|
|
|
2014-12-23 09:55:18 +00:00
|
|
|
>>> p = Polynom([1, 2])
|
2015-02-27 17:04:50 +00:00
|
|
|
>>> p.postfix_tokens
|
2014-12-23 09:55:18 +00:00
|
|
|
[2, 'x', '*', 1, '+']
|
|
|
|
>>> p = Polynom([1, -2])
|
2015-02-27 17:04:50 +00:00
|
|
|
>>> p.postfix_tokens
|
2014-12-23 09:55:18 +00:00
|
|
|
[2, 'x', '*', '-', 1, '+']
|
|
|
|
>>> p = Polynom([1,2,3])
|
2015-02-27 17:04:50 +00:00
|
|
|
>>> p.postfix_tokens
|
2014-12-23 09:55:18 +00:00
|
|
|
[3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
|
2015-02-27 17:58:01 +00:00
|
|
|
>>> p = Polynom([1])
|
|
|
|
>>> p.postfix_tokens
|
|
|
|
[1]
|
|
|
|
>>> p = Polynom([0])
|
|
|
|
>>> p.postfix_tokens
|
|
|
|
[0]
|
2014-12-23 09:55:18 +00:00
|
|
|
>>> p = Polynom([1,[2,3]])
|
2015-02-27 17:04:50 +00:00
|
|
|
>>> p.postfix_tokens
|
2014-12-23 09:55:18 +00:00
|
|
|
[2, 'x', '*', 3, 'x', '*', '+', 1, '+']
|
|
|
|
>>> p = Polynom([1,[2,-3]])
|
2015-02-27 17:04:50 +00:00
|
|
|
>>> p.postfix_tokens
|
2014-12-23 09:55:18 +00:00
|
|
|
[2, 'x', '*', 3, 'x', '*', '-', 1, '+']
|
|
|
|
>>> p = Polynom([1,[-2,-3]])
|
2015-02-27 17:04:50 +00:00
|
|
|
>>> p.postfix_tokens
|
2014-12-23 09:55:18 +00:00
|
|
|
[2, 'x', '*', '-', 3, 'x', '*', '-', 1, '+']
|
|
|
|
>>> from pymath.expression import Expression
|
|
|
|
>>> from pymath.operator import op
|
|
|
|
>>> e = Expression([2,3,op.add])
|
|
|
|
>>> p = Polynom([1,e])
|
2015-02-27 17:04:50 +00:00
|
|
|
>>> p.postfix_tokens
|
2014-12-23 09:55:18 +00:00
|
|
|
[2, 3, '+', 'x', '*', 1, '+']
|
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
"""
|
2015-02-27 17:58:01 +00:00
|
|
|
if self == 0:
|
|
|
|
return [0]
|
2014-12-21 15:04:44 +00:00
|
|
|
# TODO: Faudrait factoriser un peu tout ça..! |dim. déc. 21 16:02:34 CET 2014
|
2014-11-11 20:00:45 +00:00
|
|
|
postfix = []
|
2014-11-11 09:45:45 +00:00
|
|
|
for (i,a) in list(enumerate(self._coef))[::-1]:
|
2014-12-21 07:53:10 +00:00
|
|
|
operator = [op.add]
|
|
|
|
operator_sub1 = []
|
2014-11-11 20:00:45 +00:00
|
|
|
if type(a) == Expression:
|
2014-11-11 09:45:45 +00:00
|
|
|
# case coef is an arithmetic expression
|
2014-11-14 15:20:02 +00:00
|
|
|
c = self.coef_postfix(a.postfix_tokens,i)
|
|
|
|
if c != []:
|
|
|
|
postfix.append(c)
|
2014-12-20 16:42:31 +00:00
|
|
|
if len(postfix) > 1:
|
2014-12-21 07:53:10 +00:00
|
|
|
postfix += operator
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:00:45 +00:00
|
|
|
elif type(a) == list:
|
2014-11-11 09:45:45 +00:00
|
|
|
# case need to repeat the x^i
|
|
|
|
for b in a:
|
2014-12-23 09:55:18 +00:00
|
|
|
operator = [op.add]
|
|
|
|
operator_sub1 = []
|
2014-12-21 07:53:10 +00:00
|
|
|
if len(postfix) == 0 and isNumber(b) and b < 0:
|
2014-12-21 15:04:44 +00:00
|
|
|
try:
|
|
|
|
b = [(-b)[-1]]
|
|
|
|
except TypeError:
|
|
|
|
b = [-b]
|
2014-12-21 07:53:10 +00:00
|
|
|
operator_sub1 = [op.sub1]
|
|
|
|
elif len(postfix) > 0 and isNumber(b) and b < 0:
|
2014-12-21 15:04:44 +00:00
|
|
|
try:
|
|
|
|
b = [(-b)[-1]]
|
|
|
|
except TypeError:
|
|
|
|
b = [-b]
|
2014-12-21 07:53:10 +00:00
|
|
|
operator = [op.sub]
|
|
|
|
else:
|
|
|
|
b = [b]
|
|
|
|
c = self.coef_postfix(b,i)
|
2014-11-14 15:20:02 +00:00
|
|
|
if c != []:
|
|
|
|
postfix.append(c)
|
2014-12-20 16:42:31 +00:00
|
|
|
if len(postfix) > 1:
|
2014-12-21 07:53:10 +00:00
|
|
|
postfix += operator_sub1
|
|
|
|
postfix += operator
|
|
|
|
postfix += operator_sub1
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
elif a != 0:
|
2014-12-21 07:53:10 +00:00
|
|
|
if len(postfix) == 0 and a < 0:
|
2014-12-21 15:04:44 +00:00
|
|
|
try:
|
|
|
|
a = [(-a)[-1]]
|
|
|
|
except TypeError:
|
2014-12-21 07:53:10 +00:00
|
|
|
a = [-a]
|
2014-12-21 15:04:44 +00:00
|
|
|
operator_sub1 = [op.sub1]
|
2014-12-21 07:53:10 +00:00
|
|
|
elif len(postfix) > 0 and a < 0:
|
2014-12-21 15:04:44 +00:00
|
|
|
try:
|
|
|
|
a = [(-a)[-1]]
|
|
|
|
except TypeError:
|
|
|
|
a = [-a]
|
2014-12-21 07:53:10 +00:00
|
|
|
operator = [op.sub]
|
|
|
|
else:
|
|
|
|
a = [a]
|
|
|
|
|
|
|
|
c = self.coef_postfix(a,i)
|
2014-11-14 15:20:02 +00:00
|
|
|
if c != []:
|
|
|
|
postfix.append(c)
|
2014-12-20 16:42:31 +00:00
|
|
|
if len(postfix) > 1:
|
2014-12-21 07:53:10 +00:00
|
|
|
postfix += operator_sub1
|
|
|
|
postfix += operator
|
|
|
|
postfix += operator_sub1
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-12-20 16:42:31 +00:00
|
|
|
return flatten_list(postfix)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 15:52:30 +00:00
|
|
|
def conv2poly(self, other):
|
2015-02-27 17:58:01 +00:00
|
|
|
"""Convert anything number into a polynom
|
|
|
|
|
|
|
|
>>> P = Polynom([1,2,3])
|
|
|
|
>>> P.conv2poly(1)
|
|
|
|
< Polynom [1]>
|
|
|
|
>>> P.conv2poly(0)
|
|
|
|
< Polynom [0]>
|
|
|
|
|
|
|
|
"""
|
2014-12-22 09:50:34 +00:00
|
|
|
if isNumber(other) and not isPolynom(other):
|
2014-12-22 14:30:07 +00:00
|
|
|
return Polynom([other], letter = self._letter)
|
2014-12-22 09:50:34 +00:00
|
|
|
elif isPolynom(other):
|
2014-11-11 09:45:45 +00:00
|
|
|
return other
|
|
|
|
else:
|
|
|
|
raise ValueError(type(other) + " can't be converted into a polynom")
|
2014-11-11 15:52:30 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
def reduce(self):
|
|
|
|
"""Compute coefficients which have same degree
|
|
|
|
|
|
|
|
:returns: new Polynom with numbers coefficients
|
2015-02-27 17:04:50 +00:00
|
|
|
|
|
|
|
>>> P = Polynom([1,2,3])
|
|
|
|
>>> Q = P.reduce()
|
|
|
|
>>> Q
|
|
|
|
< Polynom [1, 2, 3]>
|
2015-02-27 18:24:27 +00:00
|
|
|
>>> Q.steps
|
2015-02-27 17:04:50 +00:00
|
|
|
[]
|
|
|
|
>>> P = Polynom([[1,2], [3,4,5], 6])
|
|
|
|
>>> Q = P.reduce()
|
|
|
|
>>> Q
|
|
|
|
< Polynom [3, 12, 6]>
|
|
|
|
>>> Q.steps
|
2015-02-27 17:58:01 +00:00
|
|
|
[< Polynom [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < Polynom [< <class 'pymath.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.expression.Expression'> [7, 5, '+'] >, 6]>, < Polynom [3, < <class 'pymath.expression.Expression'> [7, 5, '+'] >, 6]>]
|
2014-11-11 09:45:45 +00:00
|
|
|
"""
|
2015-02-27 17:04:50 +00:00
|
|
|
|
|
|
|
# TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015
|
|
|
|
|
2014-11-11 20:00:45 +00:00
|
|
|
# gather steps for every coeficients
|
|
|
|
coefs_steps = []
|
|
|
|
for coef in self._coef:
|
2014-11-11 09:45:45 +00:00
|
|
|
coef_steps = []
|
2014-12-20 19:36:15 +00:00
|
|
|
if type(coef) == list:
|
2014-11-11 20:00:45 +00:00
|
|
|
# On converti en postfix avec une addition
|
2014-12-22 13:20:56 +00:00
|
|
|
postfix_add = self.postfix_add([i for i in coef if i!=0])
|
2014-11-11 20:00:45 +00:00
|
|
|
# On converti en Expression
|
|
|
|
coef_exp = Expression(postfix_add)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-12-28 09:28:31 +00:00
|
|
|
with Expression.tmp_render():
|
2015-02-27 17:04:50 +00:00
|
|
|
coef_steps = list(coef_exp.simplify().explain())
|
2014-12-20 19:36:15 +00:00
|
|
|
|
2014-12-23 09:32:15 +00:00
|
|
|
#print('\t 1.coef_steps -> ', coef_steps)
|
|
|
|
|
2014-12-20 19:36:15 +00:00
|
|
|
elif type(coef) == Expression:
|
|
|
|
|
2014-12-28 09:28:31 +00:00
|
|
|
with Expression.tmp_render():
|
2015-02-27 17:04:50 +00:00
|
|
|
coef_steps = list(coef.simplify().explain())
|
2014-12-20 19:36:15 +00:00
|
|
|
|
2014-12-23 09:32:15 +00:00
|
|
|
#print('\t 2.coef_steps -> ', coef_steps)
|
|
|
|
|
2014-12-20 19:36:15 +00:00
|
|
|
else:
|
|
|
|
try:
|
2015-02-27 17:04:50 +00:00
|
|
|
coef_steps += coef.simplify().explaine()
|
2014-12-20 19:36:15 +00:00
|
|
|
except AttributeError:
|
2014-12-23 09:32:15 +00:00
|
|
|
coef_steps = [coef]
|
|
|
|
|
|
|
|
#print('\t 3.coef_steps -> ', coef_steps)
|
2014-11-11 20:00:45 +00:00
|
|
|
# On ajoute toutes ces étapes
|
|
|
|
coefs_steps.append(coef_steps)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-12-23 09:32:15 +00:00
|
|
|
#print('\t coefs_steps -> ', coefs_steps)
|
|
|
|
|
2014-11-11 20:00:45 +00:00
|
|
|
# On retourne la matrice
|
2015-02-27 17:04:50 +00:00
|
|
|
steps = []
|
2014-11-11 20:00:45 +00:00
|
|
|
for coefs in transpose_fill(coefs_steps):
|
2015-02-27 17:04:50 +00:00
|
|
|
steps.append(Polynom(coefs, self._letter))
|
|
|
|
|
|
|
|
ans, steps = steps[-1], steps[:-1]
|
|
|
|
ans.steps = steps
|
2014-11-14 15:20:02 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
return ans
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-03-06 16:00:09 +00:00
|
|
|
def derivate(self):
|
|
|
|
""" Return the derivated polynom
|
|
|
|
|
|
|
|
>>> P = Polynom([1, 2, 3])
|
|
|
|
>>> Q = P.derivate()
|
|
|
|
>>> Q
|
|
|
|
< Polynom [2, 6]>
|
2015-03-08 17:51:53 +00:00
|
|
|
>>> print(Q.name)
|
|
|
|
P'
|
2015-03-06 16:00:09 +00:00
|
|
|
>>> for i in Q.explain():
|
|
|
|
... print(i)
|
|
|
|
2 \\times 3 x + 1 \\times 2
|
|
|
|
6 x + 2
|
|
|
|
"""
|
|
|
|
derv_coefs = []
|
|
|
|
for (i,c) in enumerate(self._coef):
|
|
|
|
derv_coefs += [Expression([i, c, op.mul])]
|
2015-03-08 17:51:53 +00:00
|
|
|
|
|
|
|
ans = Polynom(derv_coefs[1:]).simplify()
|
|
|
|
ans.name = self.name + "'"
|
|
|
|
return ans
|
2015-03-06 16:00:09 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
@staticmethod
|
2014-11-11 20:00:45 +00:00
|
|
|
def postfix_add(numbers):
|
|
|
|
"""Convert a list of numbers into a postfix addition
|
|
|
|
|
|
|
|
:numbers: list of numbers
|
|
|
|
:returns: Postfix list of succecive attition of number
|
|
|
|
|
|
|
|
>>> Polynom.postfix_add([1])
|
|
|
|
[1]
|
|
|
|
>>> Polynom.postfix_add([1, 2])
|
|
|
|
[1, 2, '+']
|
|
|
|
>>> Polynom.postfix_add([1, 2, 3])
|
|
|
|
[1, 2, '+', 3, '+']
|
|
|
|
>>> Polynom.postfix_add(1)
|
|
|
|
[1]
|
|
|
|
"""
|
|
|
|
if not type(numbers) == list:
|
|
|
|
return [numbers]
|
2014-11-11 09:45:45 +00:00
|
|
|
else:
|
2014-11-11 20:00:45 +00:00
|
|
|
ans = [[a, op.add] if i!=0 else [a] for (i,a) in enumerate(numbers)]
|
|
|
|
return list(chain.from_iterable(ans))
|
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
def simplify(self):
|
|
|
|
"""Same as reduce """
|
|
|
|
return self.reduce()
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2014-12-22 09:31:05 +00:00
|
|
|
try:
|
|
|
|
o_poly = self.conv2poly(other)
|
|
|
|
return self._coef == o_poly._coef
|
|
|
|
except TypeError:
|
|
|
|
return 0
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __add__(self, other):
|
2015-02-27 17:58:01 +00:00
|
|
|
""" Overload +
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-02-27 17:58:01 +00:00
|
|
|
>>> P = Polynom([1,2,3])
|
|
|
|
>>> Q = Polynom([4,5])
|
|
|
|
>>> R = P+Q
|
|
|
|
>>> R
|
|
|
|
< Polynom [5, 7, 3]>
|
|
|
|
>>> R.steps
|
|
|
|
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < Polynom [< <class 'pymath.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.expression.Expression'> [2, 5, '+'] >, 3]>, < Polynom [< <class 'pymath.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.expression.Expression'> [2, 5, '+'] >, 3]>]
|
|
|
|
"""
|
2014-11-11 15:52:30 +00:00
|
|
|
o_poly = self.conv2poly(other)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 15:52:30 +00:00
|
|
|
n_coef = spe_zip(self._coef, o_poly._coef)
|
2014-12-22 14:30:07 +00:00
|
|
|
p = Polynom(n_coef, letter = self._letter)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-02-28 22:11:04 +00:00
|
|
|
ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.add])]
|
2015-02-27 17:58:01 +00:00
|
|
|
ans = p.simplify()
|
|
|
|
ans.steps = ini_step + ans.steps
|
|
|
|
return ans
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __radd__(self, other):
|
2015-02-27 18:24:27 +00:00
|
|
|
o_poly = self.conv2poly(other)
|
|
|
|
return o_poly.__add__(self)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __neg__(self):
|
2015-02-27 17:58:01 +00:00
|
|
|
""" overload - (as arity 1 operator)
|
|
|
|
|
|
|
|
>>> P = Polynom([1,2,3])
|
|
|
|
>>> Q = -P
|
|
|
|
>>> Q
|
|
|
|
< Polynom [-1, -2, -3]>
|
|
|
|
>>> Q.steps
|
|
|
|
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', '-'] >]
|
|
|
|
"""
|
2015-02-28 22:11:04 +00:00
|
|
|
ini_step = [Expression(self.postfix_tokens + [op.sub1])]
|
2015-02-27 17:58:01 +00:00
|
|
|
ans = Polynom([-i for i in self._coef], letter = self._letter).simplify()
|
|
|
|
ans.steps = ini_step + ans.steps
|
|
|
|
return ans
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __sub__(self, other):
|
2015-02-27 17:58:01 +00:00
|
|
|
""" overload -
|
|
|
|
|
|
|
|
>>> P = Polynom([1,2,3])
|
|
|
|
>>> Q = Polynom([4,5,6])
|
|
|
|
>>> R = P - Q
|
|
|
|
>>> R
|
|
|
|
< Polynom [-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, '-', '+'] >, < Polynom [< <class 'pymath.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.expression.Expression'> [3, -6, '+'] >]>, < Polynom [< <class 'pymath.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.expression.Expression'> [3, -6, '+'] >]>]
|
|
|
|
"""
|
2014-11-11 15:52:30 +00:00
|
|
|
o_poly = self.conv2poly(other)
|
2015-02-28 22:11:04 +00:00
|
|
|
ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])]
|
2014-11-11 09:45:45 +00:00
|
|
|
o_poly = -o_poly
|
2015-02-27 17:58:01 +00:00
|
|
|
#ini_step += o_poly.steps
|
|
|
|
|
|
|
|
ans = self + o_poly
|
|
|
|
ans.steps = ini_step + ans.steps
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-02-27 17:58:01 +00:00
|
|
|
return ans
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __rsub__(self, other):
|
2014-11-11 15:52:30 +00:00
|
|
|
o_poly = self.conv2poly(other)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-02-27 18:24:27 +00:00
|
|
|
return o_poly.__sub__(self)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __mul__(self, other):
|
2014-12-23 09:32:15 +00:00
|
|
|
""" Overload *
|
|
|
|
|
|
|
|
>>> p = Polynom([1,2])
|
|
|
|
>>> p*3
|
2015-02-27 18:24:27 +00:00
|
|
|
< 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, '*'] >]>]
|
2014-12-23 09:32:15 +00:00
|
|
|
>>> q = Polynom([0,0,4])
|
|
|
|
>>> q*3
|
2015-02-27 18:24:27 +00:00
|
|
|
< 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, '*'] >]>]
|
2014-12-23 09:32:15 +00:00
|
|
|
>>> r = Polynom([0,1])
|
|
|
|
>>> r*3
|
2015-02-27 18:24:27 +00:00
|
|
|
< Polynom [0, 3]>
|
|
|
|
>>> (r*3).steps
|
|
|
|
[[< <class 'pymath.expression.Expression'> ['x', 3, '*'] >]]
|
2014-12-23 09:32:15 +00:00
|
|
|
>>> p*q
|
2015-02-27 18:24:27 +00:00
|
|
|
< 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, '*'] >]>]
|
2014-12-23 09:32:15 +00:00
|
|
|
>>> p*r
|
2015-02-27 18:24:27 +00:00
|
|
|
< Polynom [0, 1, 2]>
|
2015-03-07 08:15:38 +00:00
|
|
|
>>> P = Polynom([1,2,3])
|
|
|
|
>>> Q = Polynom([4,5,6])
|
|
|
|
>>> P*Q
|
|
|
|
< Polynom [4, 13, 28, 27, 18]>
|
2014-12-23 09:32:15 +00:00
|
|
|
"""
|
2015-02-27 18:24:27 +00:00
|
|
|
# TODO: Je trouve qu'elle grille trop d'étapes... |ven. févr. 27 19:08:44 CET 2015
|
2014-11-11 15:52:30 +00:00
|
|
|
o_poly = self.conv2poly(other)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-03-07 08:15:38 +00:00
|
|
|
coefs = [0]*(self.degree + o_poly.degree + 1)
|
2014-11-11 09:45:45 +00:00
|
|
|
for (i,a) in enumerate(self._coef):
|
|
|
|
for (j,b) in enumerate(o_poly._coef):
|
|
|
|
if a == 0 or b == 0:
|
|
|
|
elem = 0
|
2014-12-23 09:32:15 +00:00
|
|
|
elif a==1:
|
|
|
|
elem = b
|
|
|
|
elif b==1:
|
|
|
|
elem = a
|
2014-11-11 09:45:45 +00:00
|
|
|
else:
|
2014-11-11 20:45:24 +00:00
|
|
|
elem = Expression([a, b, op.mul])
|
2015-03-07 08:15:38 +00:00
|
|
|
|
|
|
|
if coefs[i+j]==0:
|
|
|
|
coefs[i+j] = elem
|
|
|
|
elif elem != 0:
|
|
|
|
if type(coefs[i+j]) == list:
|
|
|
|
coefs[i+j] += [elem]
|
|
|
|
else:
|
|
|
|
coefs[i+j] = [coefs[i+j] , elem]
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-12-22 14:30:07 +00:00
|
|
|
p = Polynom(coefs, letter = self._letter)
|
2015-02-27 18:24:27 +00:00
|
|
|
ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.mul])]
|
|
|
|
ans = p.simplify()
|
2014-12-21 14:03:56 +00:00
|
|
|
|
2015-02-27 18:24:27 +00:00
|
|
|
ans.steps = [ini_step] + ans.steps
|
|
|
|
return ans
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def __rmul__(self, other):
|
2014-11-11 15:52:30 +00:00
|
|
|
o_poly = self.conv2poly(other)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
return o_poly.__mul__(self)
|
|
|
|
|
2014-12-22 19:42:18 +00:00
|
|
|
@power_cache
|
2014-12-22 14:54:07 +00:00
|
|
|
def __pow__(self, power):
|
2014-12-22 19:42:18 +00:00
|
|
|
""" Overload **
|
|
|
|
|
|
|
|
>>> p = Polynom([0,0,3])
|
|
|
|
>>> p**2
|
2015-02-27 21:16:18 +00:00
|
|
|
< Polynom [0, 0, 0, 0, 9]>
|
|
|
|
>>> (p**2).steps
|
|
|
|
[< <class 'pymath.expression.Expression'> [3, 'x', 2, '^', '*', 2, '^'] >, < Polynom [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>, < Polynom [0, 0, 0, 0, < <class 'pymath.expression.Expression'> [3, 2, '^'] >]>]
|
2014-12-22 19:42:18 +00:00
|
|
|
>>> p = Polynom([1,2])
|
|
|
|
>>> p**2
|
2015-02-27 21:16:18 +00:00
|
|
|
< Polynom [1, 4, 4]>
|
|
|
|
>>> (p**2).steps
|
|
|
|
[< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, '^'] >, [< <class 'pymath.expression.Expression'> [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < Polynom [1, < <class 'pymath.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.expression.Expression'> [2, 2, '*'] >]>, < Polynom [1, < <class 'pymath.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.expression.Expression'> [2, 2, '*'] >]>]
|
2014-12-23 09:32:15 +00:00
|
|
|
>>> p = Polynom([0,0,1])
|
|
|
|
>>> p**3
|
2015-02-27 21:16:18 +00:00
|
|
|
< Polynom [0, 0, 0, 0, 0, 0, 1]>
|
2015-03-07 08:15:38 +00:00
|
|
|
>>> p = Polynom([1,2,3])
|
|
|
|
>>> p**2
|
|
|
|
< Polynom [1, 4, 10, 12, 9]>
|
2014-12-22 19:42:18 +00:00
|
|
|
|
|
|
|
"""
|
2014-12-22 14:54:07 +00:00
|
|
|
if not type(power):
|
|
|
|
raise ValueError("Can't raise Polynom to {} power".format(str(power)))
|
|
|
|
|
2015-02-28 22:11:04 +00:00
|
|
|
ini_step = [Expression(self.postfix_tokens + [power, op.pw])]
|
2014-12-22 14:54:07 +00:00
|
|
|
|
|
|
|
if self.is_monom():
|
2014-12-23 09:32:15 +00:00
|
|
|
if self._coef[self.degree] == 1:
|
|
|
|
coefs = [0]*self.degree*power + [1]
|
|
|
|
p = Polynom(coefs, letter = self._letter)
|
2015-02-27 21:16:18 +00:00
|
|
|
ans = p
|
2014-12-23 09:32:15 +00:00
|
|
|
else:
|
|
|
|
coefs = [0]*self.degree*power + [Expression([self._coef[self.degree] , power, op.pw])]
|
|
|
|
p = Polynom(coefs, letter = self._letter)
|
2015-02-27 21:16:18 +00:00
|
|
|
ans = p.simplify()
|
2014-12-22 14:54:07 +00:00
|
|
|
else:
|
2014-12-22 19:42:18 +00:00
|
|
|
if power == 2:
|
2015-02-27 21:16:18 +00:00
|
|
|
ans = self * self
|
2014-12-22 19:42:18 +00:00
|
|
|
else:
|
2015-02-27 21:16:18 +00:00
|
|
|
# TODO: faudrait changer ça c'est pas très sérieux |ven. févr. 27 22:08:00 CET 2015
|
2014-12-23 09:34:51 +00:00
|
|
|
raise AttributeError("__pw__ not implemented yet when power is greatter than 2")
|
2014-12-22 14:54:07 +00:00
|
|
|
|
2015-02-27 21:16:18 +00:00
|
|
|
ans.steps = ini_step + ans.steps
|
|
|
|
return ans
|
2014-12-22 14:54:07 +00:00
|
|
|
|
|
|
|
def __xor__(self, power):
|
|
|
|
return self.__pow__(power)
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def test(p,q):
|
|
|
|
print("---------------------")
|
|
|
|
print("---------------------")
|
|
|
|
print("p : ",p)
|
|
|
|
print("q : ",q)
|
|
|
|
|
2014-12-21 17:03:22 +00:00
|
|
|
print("\n Plus ------")
|
|
|
|
print(p, "+", q)
|
|
|
|
for i in (p + q):
|
|
|
|
#print(repr(i))
|
2015-02-27 17:04:50 +00:00
|
|
|
#print("\t", str(i.postfix_tokens))
|
2014-12-21 17:03:22 +00:00
|
|
|
print(i)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-12-21 17:03:22 +00:00
|
|
|
print("\n Moins ------")
|
|
|
|
for i in (p - q):
|
|
|
|
#print(repr(i))
|
2015-02-27 17:04:50 +00:00
|
|
|
#print("\t", str(i.postfix_tokens))
|
2014-12-21 17:03:22 +00:00
|
|
|
print(i)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-14 16:01:30 +00:00
|
|
|
print("\n Multiplier ------")
|
|
|
|
for i in (p * q):
|
|
|
|
#print(repr(i))
|
2015-02-27 17:04:50 +00:00
|
|
|
#print("\t", str(i.postfix_tokens))
|
2014-11-14 16:01:30 +00:00
|
|
|
print(i)
|
2014-12-21 17:03:22 +00:00
|
|
|
|
2014-12-21 18:03:44 +00:00
|
|
|
print("\n Evaluer p ------")
|
|
|
|
for i in p(3).simplify():
|
|
|
|
print(i)
|
|
|
|
|
|
|
|
print("\n Evaluer q ------")
|
2014-12-21 17:03:22 +00:00
|
|
|
for i in q(3).simplify():
|
|
|
|
print(i)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-12-22 13:20:56 +00:00
|
|
|
#from .fraction import Fraction
|
2015-03-07 08:15:38 +00:00
|
|
|
#with Expression.tmp_render(txt):
|
|
|
|
# p = Polynom([1, 2, 3])
|
|
|
|
# q = Polynom([4, 5, 6])
|
|
|
|
# for i in (p*q).explain():
|
|
|
|
# print(i)
|
2015-02-27 21:26:53 +00:00
|
|
|
# r = Polynom([0,1])
|
|
|
|
# for i in (r*3).explain():
|
|
|
|
# print(i)
|
2015-02-27 18:24:27 +00:00
|
|
|
# print("q = ", q)
|
|
|
|
# r = q.reduce()
|
|
|
|
# print("r = ", r)
|
|
|
|
# for i in r.explain():
|
|
|
|
# print("q = ", i)
|
|
|
|
# print(p-q)
|
|
|
|
# for i in p-q:
|
|
|
|
# print(i)
|
2015-03-07 08:15:38 +00:00
|
|
|
#Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c)
|
2015-02-27 17:04:50 +00:00
|
|
|
|
|
|
|
|
2015-02-27 17:58:01 +00:00
|
|
|
import doctest
|
2015-02-27 21:26:53 +00:00
|
|
|
doctest.testmod(optionflags=doctest.ELLIPSIS)
|
2014-11-11 20:00:45 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|