adapt polynomDeg2 to new explain style

This commit is contained in:
Lafrite 2015-03-06 14:43:09 +01:00
parent b7156a5ac4
commit b6eccba836

View File

@ -39,18 +39,16 @@ class Polynom_deg2(Polynom):
>>> P = Polynom_deg2([1,2,3]) >>> P = Polynom_deg2([1,2,3])
>>> P.delta >>> P.delta
< Expression [2, 2, '^', 4, 3, 1, '*', '*', '-']> -8
>>> for i in P.delta.simplify(): >>> for i in P.delta.explain():
... print(i) ... print(i)
2^{ 2 } - 4 \\times 3 \\times 1 2^{ 2 } - 4 \\times 3 \\times 1
4 - 4 \\times 3 4 - 4 \\times 3
4 - 12 4 - 12
-8 -8
>>> P.delta.simplified()
-8
""" """
return Expression([self.b, 2, op.pw, 4, self.a, self.c, op.mul, op.mul, op.sub]) return Expression([self.b, 2, op.pw, 4, self.a, self.c, op.mul, op.mul, op.sub]).simplify()
@property @property
def alpha(self): def alpha(self):
@ -58,19 +56,15 @@ class Polynom_deg2(Polynom):
>>> P = Polynom_deg2([1,2,3]) >>> P = Polynom_deg2([1,2,3])
>>> P.alpha >>> P.alpha
< Expression [2, '-', 2, 3, '*', '/']> < Fraction -1 / 3>
>>> for i in P.alpha.simplify(): >>> for i in P.alpha.explain():
... print(i) ... print(i)
\\frac{ - 2 }{ 2 \\times 3 } \\frac{ - 2 }{ 2 \\times 3 }
\\frac{ -2 }{ 6 } \\frac{ -2 }{ 6 }
\\frac{ ( -1 ) \\times 2 }{ 3 \\times 2 } \\frac{ ( -1 ) \\times 2 }{ 3 \\times 2 }
\\frac{ -1 }{ 3 } \\frac{ -1 }{ 3 }
\\frac{ -2 }{ 6 }
>>> P.alpha.simplified() # Bug avec les fractions ici, on devrait avoir -1/3 pas -2/6...
< Fraction -2 / 6>
""" """
return Expression([self.b, op.sub1, 2, self.a, op.mul, op.div]) return Expression([self.b, op.sub1, 2, self.a, op.mul, op.div]).simplify()
@property @property
def beta(self): def beta(self):
@ -78,31 +72,16 @@ class Polynom_deg2(Polynom):
>>> P = Polynom_deg2([1,2,3]) >>> P = Polynom_deg2([1,2,3])
>>> P.beta >>> P.beta
< Expression [3, < Fraction -2 / 6>, 2, '^', '*', 2, < Fraction -2 / 6>, '*', '+', 1, '+']>
>>> for i in P.beta.simplify(): # Ça serait bien que l'on puisse enlever des étapes maintenant...
... print(i)
3 \\times \\frac{ -2 }{ 6 }^{ 2 } + 2 \\times \\frac{ -2 }{ 6 } + 1
3 \\times \\frac{ ( -2 )^{ 2 } }{ 6^{ 2 } } + \\frac{ ( -2 ) \\times 1 \\times 2 }{ 3 \\times 2 } + 1
3 \\times \\frac{ 4 }{ 36 } + \\frac{ ( -2 ) \\times 2 }{ 6 } + 1
3 \\times \\frac{ 1 \\times 4 }{ 9 \\times 4 } + \\frac{ -4 }{ 6 } + 1
3 \\times \\frac{ 1 }{ 9 } + \\frac{ ( -2 ) \\times 2 }{ 3 \\times 2 } + 1
3 \\times \\frac{ 1 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 \\times 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -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
\\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 + ( -2 ) }{ 3 } + 1
\\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 }
\\frac{ 2 }{ 3 }
>>> P.beta.simplified()
< Fraction 2 / 3> < 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 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1
\\frac{ -1 }{ 3 } + 1
\\frac{ 2 }{ 3 }
""" """
return self(self.alpha.simplified()) return self(self.alpha).simplify()
def roots(self): def roots(self):
""" Compute roots of the polynom """ Compute roots of the polynom
@ -121,9 +100,9 @@ class Polynom_deg2(Polynom):
>>> P.roots() >>> P.roots()
[-1.0, 1.0] [-1.0, 1.0]
""" """
if self.delta.simplified() > 0: if self.delta > 0:
self.roots = [(-self.b - sqrt(self.delta.simplified()))/(2*self.a), (-self.b + sqrt(self.delta.simplified()))/(2*self.a)] self.roots = [(-self.b - sqrt(self.delta))/(2*self.a), (-self.b + sqrt(self.delta))/(2*self.a)]
elif self.delta.simplified() == 0: elif self.delta == 0:
self.roots = [-self.b /(2*self.a)] self.roots = [-self.b /(2*self.a)]
else: else:
self.roots = [] self.roots = []
@ -151,12 +130,12 @@ class Polynom_deg2(Polynom):
>>> print(P.tbl_sgn()) >>> print(P.tbl_sgn())
\\tkzTabLine{, -,} \\tkzTabLine{, -,}
""" """
if self.delta.simplified() > 0: if self.delta > 0:
if self.a > 0: if self.a > 0:
return "\\tkzTabLine{, +, z, -, z , +,}" return "\\tkzTabLine{, +, z, -, z , +,}"
else: else:
return "\\tkzTabLine{, -, z, +, z , -,}" return "\\tkzTabLine{, -, z, +, z , -,}"
elif self.delta.simplified() == 0: elif self.delta == 0:
if self.a > 0: if self.a > 0:
return "\\tkzTabLine{, +, z, +,}" return "\\tkzTabLine{, +, z, +,}"
else: else:
@ -179,7 +158,7 @@ class Polynom_deg2(Polynom):
\\tkzTabVar{+/{$+\\infty$}, -/{$\\frac{ 2 }{ 3 }$}, +/{$+\\infty$}} \\tkzTabVar{+/{$+\\infty$}, -/{$\\frac{ 2 }{ 3 }$}, +/{$+\\infty$}}
""" """
beta = self.beta.simplified() beta = self.beta
if limits: if limits:
if self.a > 0: if self.a > 0:
return "\\tkzTabVar{+/{$+\\infty$}, -/{$" + str(beta) + "$}, +/{$+\\infty$}}" return "\\tkzTabVar{+/{$+\\infty$}, -/{$" + str(beta) + "$}, +/{$+\\infty$}}"