Feat: Test and validate roots (but not elegant)
This commit is contained in:
parent
a5f062a151
commit
a32b684b6b
@ -93,7 +93,7 @@ class Polynomial(Token):
|
|||||||
17
|
17
|
||||||
"""
|
"""
|
||||||
return Expression(self._mo.tree)(value)
|
return Expression(self._mo.tree)(value)
|
||||||
|
|
||||||
def differentiate(self):
|
def differentiate(self):
|
||||||
""" Differentiate a polynome
|
""" Differentiate a polynome
|
||||||
|
|
||||||
@ -111,6 +111,7 @@ class Polynomial(Token):
|
|||||||
2 + 6x
|
2 + 6x
|
||||||
"""
|
"""
|
||||||
return Expression(self._mo.differentiate()).simplify()
|
return Expression(self._mo.differentiate()).simplify()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def roots(self):
|
def roots(self):
|
||||||
""" Get roots of the Polynomial """
|
""" Get roots of the Polynomial """
|
||||||
@ -231,15 +232,24 @@ class Quadratic(Polynomial):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def a(self):
|
def a(self):
|
||||||
return self[2]
|
try:
|
||||||
|
return self[2]
|
||||||
|
except KeyError:
|
||||||
|
return 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def b(self):
|
def b(self):
|
||||||
return self[1]
|
try:
|
||||||
|
return self[1]
|
||||||
|
except KeyError:
|
||||||
|
return 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def c(self):
|
def c(self):
|
||||||
return self[0]
|
try:
|
||||||
|
return self[0]
|
||||||
|
except KeyError:
|
||||||
|
return 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def delta(self):
|
def delta(self):
|
||||||
@ -247,12 +257,33 @@ class Quadratic(Polynomial):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def roots(self):
|
def roots(self):
|
||||||
|
""" Roots of the polynom
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial
|
||||||
|
>>> P = Quadratic(MOpolynomial('x', [1, 0, 1]))
|
||||||
|
>>> P.roots
|
||||||
|
[]
|
||||||
|
>>> P = Quadratic(MOpolynomial('x', [4, -4, 1]))
|
||||||
|
>>> P.roots
|
||||||
|
[2.0]
|
||||||
|
>>> P = Quadratic(MOpolynomial('x', [1, 0, -1]))
|
||||||
|
>>> P.roots
|
||||||
|
[-1.0, 1.0]
|
||||||
|
"""
|
||||||
if self.delta._mo < 0:
|
if self.delta._mo < 0:
|
||||||
return []
|
return []
|
||||||
elif self.delta._mo == 0:
|
elif self.delta._mo == 0:
|
||||||
return [Expression.from_str(f"-{self.b}/(2*{self.a})").simplify()]
|
#return [Expression.from_str(f"-{self.b}/(2*{self.a})").simplify()]
|
||||||
|
return [round(eval(f"-{self.b}/(2*{self.a})"), 2)]
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("Todo!")
|
from math import sqrt
|
||||||
|
roots = [
|
||||||
|
round(eval(f"(-{self.b}-sqrt({self.delta}))/(2*{self.a})"), 2),
|
||||||
|
round(eval(f"(-{self.b}+sqrt({self.delta}))/(2*{self.a})"), 2),
|
||||||
|
]
|
||||||
|
roots.sort()
|
||||||
|
return roots
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
@ -80,10 +80,7 @@ class Token(object):
|
|||||||
return self._mo.__tex__
|
return self._mo.__tex__
|
||||||
|
|
||||||
def _operate(self, other, operation):
|
def _operate(self, other, operation):
|
||||||
""" Make a operation between 2 Tokens,
|
""" Make a operation between 2 Tokens """
|
||||||
a Token and en Expression ora
|
|
||||||
a Token an a builtin type
|
|
||||||
"""
|
|
||||||
from ..expression import Expression
|
from ..expression import Expression
|
||||||
from ...core import Tree
|
from ...core import Tree
|
||||||
from . import factory
|
from . import factory
|
||||||
@ -182,6 +179,30 @@ class Token(object):
|
|||||||
"""
|
"""
|
||||||
return self._operate(other, "/")
|
return self._operate(other, "/")
|
||||||
|
|
||||||
|
def _get_soul(self, other=None):
|
||||||
|
""" Get the builtin soul of self or other """
|
||||||
|
if isinstance(other, Token):
|
||||||
|
return other._mo._value
|
||||||
|
elif not other is None:
|
||||||
|
return other
|
||||||
|
return self._mo._value
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self._get_soul() == self._get_soul(other)
|
||||||
|
|
||||||
|
def __gt__(self, other):
|
||||||
|
return self._get_soul() > self._get_soul(other)
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
return self._get_soul() < self._get_soul(other)
|
||||||
|
|
||||||
|
def __ge__(self, other):
|
||||||
|
return self._get_soul() >= self._get_soul(other)
|
||||||
|
|
||||||
|
def __le__(self, other):
|
||||||
|
return self._get_soul() <= self._get_soul(other)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
Loading…
Reference in New Issue
Block a user