Merge branch 'polynom' into 2nd_deg

This commit is contained in:
Lafrite 2015-02-24 18:17:28 +01:00
commit 6053f9ba94
3 changed files with 37 additions and 2 deletions

16
docs/polynom.mdwn Normal file
View File

@ -0,0 +1,16 @@
# Les polynômes
## Créer des polynômes
### Générer un polynôme "fixe"
### Générer un polynôme aléatoirement
>>> P = Polynom.random(["{b}", "{a}"]) # Polynom du type ax + b
>>> print(P)
- 8 x - 3
>>> P = Polynom.random(degree = 2)
>>> print(P)
5 x^{ 2 } + 4 x - 7

View File

View File

@ -16,7 +16,7 @@ def power_cache(fun):
"""Decorator which cache calculated powers of polynoms """ """Decorator which cache calculated powers of polynoms """
cache = {} cache = {}
def cached_fun(self, power): def cached_fun(self, power):
print("cache -> ", cache) #print("cache -> ", cache)
if (tuple(self._coef), power) in cache.keys(): if (tuple(self._coef), power) in cache.keys():
return cache[(tuple(self._coef), power)] return cache[(tuple(self._coef), power)]
else: else:
@ -30,18 +30,37 @@ class Polynom(object):
"""Docstring for Polynom. """ """Docstring for Polynom. """
@classmethod @classmethod
def random(self, coefs_form=[], conditions=[], letter = "x"): def random(self, coefs_form=[], conditions=[], letter = "x", degree = 0):
""" Create a random polynom from coefs_form and conditions """ Create a random polynom from coefs_form and conditions
:param coefs_form: list of forms (one by coef) (ascending degree sorted) :param coefs_form: list of forms (one by coef) (ascending degree sorted)
:param conditions: condition on variables :param conditions: condition on variables
: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)
/!\ variables need to be in brackets {} /!\ variables need to be in brackets {}
>>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS
...
>>> Polynom.random(degree = 2) # doctest:+ELLIPSIS
...
>>> Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c)
...
>>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above
...
""" """
if (degree > 0 and degree < 26):
# Générer assez de lettre pour les coefs
coefs_name = map(chr, range(97, 98+degree))
coefs_form = ["{" + i + "}" for i in coefs_name].reverse()
form = str(coefs_form) form = str(coefs_form)
# On créé les valeurs toutes concaténées dans un string
coefs = RdExpression(form, conditions)() coefs = RdExpression(form, conditions)()
# On "parse" ce string pour créer les coefs
coefs = [eval(i) if type(i)==str else i for i in eval(coefs)] coefs = [eval(i) if type(i)==str else i for i in eval(coefs)]
# Création du polynom
return Polynom(coef = coefs, letter = letter) return Polynom(coef = coefs, letter = letter)
def __init__(self, coef = [1], letter = "x" ): def __init__(self, coef = [1], letter = "x" ):