From da6a43364bf711a33f68ba1ffa674f75501640f2 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Sat, 21 Feb 2015 11:01:34 +0100 Subject: [PATCH] New way for random build polynoms --- docs/polynom.mdwn | 16 ++++++++++++++++ docs/polynom.ndwn | 0 pymath/polynom.py | 21 ++++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 docs/polynom.mdwn delete mode 100644 docs/polynom.ndwn diff --git a/docs/polynom.mdwn b/docs/polynom.mdwn new file mode 100644 index 0000000..c3007e9 --- /dev/null +++ b/docs/polynom.mdwn @@ -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 + diff --git a/docs/polynom.ndwn b/docs/polynom.ndwn deleted file mode 100644 index e69de29..0000000 diff --git a/pymath/polynom.py b/pymath/polynom.py index 6a8f10a..593e3ce 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -30,18 +30,37 @@ class Polynom(object): """Docstring for Polynom. """ @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 :param coefs_form: list of forms (one by coef) (ascending degree sorted) :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 {} + >>> 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) + # On créé les valeurs toutes concaténées dans un string 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)] + # Création du polynom return Polynom(coef = coefs, letter = letter) def __init__(self, coef = [1], letter = "x" ):