Mapytex/pymath/polynomDeg2.py

251 lines
8.1 KiB
Python
Raw Normal View History

2015-01-23 16:19:14 +00:00
#!/usr/bin/env python
# encoding: utf-8
from .polynom import Polynom
from .expression import Expression
from .fraction import Fraction
2015-01-23 16:19:14 +00:00
from .operator import op
2015-03-06 15:51:43 +00:00
from .random_expression import RdExpression
2015-04-07 06:46:59 +00:00
from sympy import sqrt, latex
#from sympy.fractions import Fraction as sp.Fraction
2015-01-23 16:19:14 +00:00
2015-03-06 15:51:43 +00:00
__all__ = ["Polynom_deg2"]
2015-01-23 16:19:14 +00:00
class Polynom_deg2(Polynom):
""" Degree 2 polynoms
2015-02-25 08:18:18 +00:00
Child of Polynom with some extra tools
2015-01-23 16:19:14 +00:00
"""
2015-03-06 15:51:43 +00:00
@classmethod
2015-03-08 18:05:15 +00:00
def random(self, coefs_form = ["{c}", "{b}", "{a}"], conditions = [], letter = "x", name = "P"):
2015-03-06 15:51:43 +00:00
""" Create a 2nd degree poly from coefs_form ans 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
"""
if len(coefs_form) != 3:
raise ValueError("Polynom_deg2 have to be degree 2 polynoms, they need 3 coefficients, {} are given".format(len(coefs_form)))
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
2015-03-08 18:05:15 +00:00
return Polynom_deg2(coefs = coefs, letter = letter, name = name)
2015-03-06 15:51:43 +00:00
2015-04-03 15:32:02 +00:00
def __init__(self, coefs = [0, 0, 1], letter = "x", name = "P", poly = 0):
if poly:
coefs = poly._coef
letter = poly._letter
name = poly.name
2015-02-25 08:18:18 +00:00
if len(coefs) < 3 or len(coefs) > 4:
raise ValueError("Polynom_deg2 have to be degree 2 polynoms, they need 3 coefficients, {} are given".format(len(coefs)))
if coefs[2] == 0:
raise ValueError("Polynom_deg2 have to be degree 2 polynoms, coefficient of x^2 can't be 0")
2015-03-08 17:51:53 +00:00
Polynom.__init__(self, coefs, letter, name = name)
2015-01-23 16:19:14 +00:00
@property
def a(self):
return self._coef[2]
@property
def b(self):
return self._coef[1]
@property
def c(self):
return self._coef[0]
@property
def delta(self):
"""Compute the discriminant expression
:returns: discriminant expression
2015-02-25 08:18:18 +00:00
>>> P = Polynom_deg2([1,2,3])
>>> P.delta
2015-03-06 13:43:09 +00:00
-8
>>> for i in P.delta.explain():
... print(i)
2^{ 2 } - 4 \\times 3 \\times 1
4 - 4 \\times 3
2015-02-25 08:18:18 +00:00
4 - 12
-8
2015-01-23 16:19:14 +00:00
"""
2015-02-25 08:18:18 +00:00
2015-03-06 13:43:09 +00:00
return Expression([self.b, 2, op.pw, 4, self.a, self.c, op.mul, op.mul, op.sub]).simplify()
2015-01-23 16:19:14 +00:00
@property
def alpha(self):
""" Compute alpha the abcisse of the extremum
>>> P = Polynom_deg2([1,2,3])
>>> P.alpha
2015-03-06 13:43:09 +00:00
< Fraction -1 / 3>
>>> for i in P.alpha.explain():
... print(i)
\\frac{ - 2 }{ 2 \\times 3 }
\\frac{ -2 }{ 6 }
\\frac{ -1 \\times 2 }{ 3 \\times 2 }
\\frac{ -1 }{ 3 }
"""
2015-03-06 13:43:09 +00:00
return Expression([self.b, op.sub1, 2, self.a, op.mul, op.div]).simplify()
@property
def beta(self):
""" Compute beta the extremum of self
>>> P = Polynom_deg2([1,2,3])
>>> P.beta
2015-03-06 13:43:09 +00:00
< Fraction 2 / 3>
>>> for i in P.beta.explain(): # Ça serait bien que l'on puisse enlever des étapes maintenant...
... print(i)
3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + 2 \\times \\frac{ -1 }{ 3 } + 1
3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + \\frac{ -1 }{ 3 } \\times 2 + 1
2015-06-17 13:13:33 +00:00
3 \\times \\frac{ ( -1 )^{ 2 } }{ 3^{ 2 } } + \\frac{ -1 \\times 2 }{ 3 } + 1
2015-02-25 09:32:27 +00:00
3 \\times \\frac{ 1 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 }{ 9 } \\times 3 + \\frac{ -1 }{ 3 } \\times 2 + 1
\\frac{ 1 \\times 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -1 \\times 2 }{ 3 } + 1
\\frac{ 1 \\times 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -2 }{ 3 } + 1
2015-02-25 09:32:27 +00:00
\\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 - 2 }{ 3 } + 1
2015-02-25 09:32:27 +00:00
\\frac{ -1 }{ 3 } + 1
\\frac{ -1 \\times 1 }{ 3 \\times 1 } + \\frac{ 1 \\times 3 }{ 1 \\times 3 }
\\frac{ -1 }{ 3 } + \\frac{ 3 }{ 3 }
\\frac{ -1 + 3 }{ 3 }
2015-02-25 09:32:27 +00:00
\\frac{ 2 }{ 3 }
"""
2015-04-21 15:31:23 +00:00
return self(self.alpha)
2015-03-06 17:13:51 +00:00
def roots(self, after_coma = 2):
2015-02-25 08:18:18 +00:00
""" Compute roots of the polynom
/!\ Can't manage nice rendering because of sqrt.
It use sympy to compute roots
2015-02-25 08:18:18 +00:00
# TODO: Pymath has to know how to compute with sqare root |mar. févr. 24 18:40:04 CET 2015
>>> P = Polynom_deg2([1, 1, 1])
>>> P.roots()
[]
>>> P = Polynom_deg2([1, 2, 1])
>>> P.roots()
[-1]
2015-02-25 08:18:18 +00:00
>>> P = Polynom_deg2([-1, 0, 1])
>>> P.roots()
['-1', '1']
>>> P = Polynom_deg2([1, 4, 1])
>>> P.roots()
['-2 - \\\\sqrt{3}', '-2 + \\\\sqrt{3}']
2015-01-23 16:19:14 +00:00
"""
2015-03-06 13:43:09 +00:00
if self.delta > 0:
self._roots = [latex((-self.b - sqrt(self.delta))/(2*self.a)), latex((-self.b + sqrt(self.delta))/(2*self.a))]
2015-03-06 13:43:09 +00:00
elif self.delta == 0:
self._roots = [Fraction(-self.b,2*self.a).simplify()]
2015-02-25 08:18:18 +00:00
else:
2015-03-06 17:13:51 +00:00
self._roots = []
return self._roots
def tbl_sgn_header(self):
""" Return header of the sign line for tkzTabLine"""
if self.delta > 0:
2015-05-22 13:44:43 +00:00
return "{$-\\infty$, $" + str(min(self.roots())) + "$ , $" + str( max(self.roots())) + "$ , $+\\infty$}"
2015-03-06 17:13:51 +00:00
elif self.delta == 0:
2015-05-22 13:44:43 +00:00
return "{$-\\infty$, $" + str(self.roots()[0]) + "$ , $+\\infty$}"
2015-03-06 17:13:51 +00:00
else:
return "{$-\\infty$, $+\\infty$}"
2015-02-25 08:18:18 +00:00
def tbl_sgn(self):
""" Return the sign line for tkzTabLine
>>> P = Polynom_deg2([2, 5, 2])
>>> print(P.tbl_sgn())
\\tkzTabLine{, +, z, -, z , +,}
2015-02-25 08:18:18 +00:00
>>> P = Polynom_deg2([2, 1, -2])
>>> print(P.tbl_sgn())
\\tkzTabLine{, -, z, +, z , -,}
2015-02-25 08:18:18 +00:00
>>> P = Polynom_deg2([1, 2, 1])
>>> print(P.tbl_sgn())
\\tkzTabLine{, +, z, +,}
2015-02-25 08:18:18 +00:00
>>> P = Polynom_deg2([0, 0, -2])
>>> print(P.tbl_sgn())
\\tkzTabLine{, -, z, -,}
2015-02-25 08:18:18 +00:00
>>> P = Polynom_deg2([1, 0, 1])
>>> print(P.tbl_sgn())
\\tkzTabLine{, +,}
2015-02-25 08:18:18 +00:00
>>> P = Polynom_deg2([-1, 0, -1])
>>> print(P.tbl_sgn())
\\tkzTabLine{, -,}
2015-02-25 08:18:18 +00:00
"""
2015-03-06 13:43:09 +00:00
if self.delta > 0:
2015-02-25 08:18:18 +00:00
if self.a > 0:
return "\\tkzTabLine{, +, z, -, z , +,}"
else:
return "\\tkzTabLine{, -, z, +, z , -,}"
2015-03-06 13:43:09 +00:00
elif self.delta == 0:
2015-02-25 08:18:18 +00:00
if self.a > 0:
return "\\tkzTabLine{, +, z, +,}"
else:
return "\\tkzTabLine{, -, z, -,}"
else:
if self.a > 0:
return "\\tkzTabLine{, +,}"
else:
return "\\tkzTabLine{, -,}"
2015-02-25 09:32:27 +00:00
def tbl_variation(self, limits = False):
"""Return the variation line for tkzTabVar
:param limit: Display or not limits in tabular
2015-02-25 09:32:27 +00:00
>>> P = Polynom_deg2([1,2,3])
>>> print(P.tbl_variation())
\\tkzTabVar{+/{}, -/{$\\frac{ 2 }{ 3 }$}, +/{}}
>>> print(P.tbl_variation(limits = True))
\\tkzTabVar{+/{$+\\infty$}, -/{$\\frac{ 2 }{ 3 }$}, +/{$+\\infty$}}
2015-01-23 16:19:14 +00:00
"""
2015-03-06 13:43:09 +00:00
beta = self.beta
2015-02-25 09:32:27 +00:00
if limits:
if self.a > 0:
return "\\tkzTabVar{+/{$+\\infty$}, -/{$" + str(beta) + "$}, +/{$+\\infty$}}"
else:
return "\\tkzTabVar{-/{$-\\infty$}, +/{$" + str(beta) + "$}, -/{$-\\infty$}}"
else:
if self.a > 0:
return "\\tkzTabVar{+/{}, -/{$" + str(beta) + "$}, +/{}}"
else:
return "\\tkzTabVar{-/{}, +/{$" + str(beta) + "$}, -/{}}"
2015-01-23 16:19:14 +00:00
if __name__ == '__main__':
2015-04-21 15:31:23 +00:00
#from .render import txt
#with Expression.tmp_render(txt):
# P = Polynom_deg2([2, 3, 4])
# print(P)
# print("\nDelta")
# for i in P.delta.explain():
# print(i)
# print("\nBeta")
# for i in P.beta.explain():
# print(i)
2015-02-25 08:18:18 +00:00
import doctest
doctest.testmod()
2015-01-23 16:19:14 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del