Feat: Test and validate roots (but not elegant)
This commit is contained in:
parent
7600962fe4
commit
f471a1efb3
@ -93,7 +93,7 @@ class Polynomial(Token):
|
||||
17
|
||||
"""
|
||||
return Expression(self._mo.tree)(value)
|
||||
|
||||
|
||||
def differentiate(self):
|
||||
""" Differentiate a polynome
|
||||
|
||||
@ -111,6 +111,7 @@ class Polynomial(Token):
|
||||
2 + 6x
|
||||
"""
|
||||
return Expression(self._mo.differentiate()).simplify()
|
||||
|
||||
@property
|
||||
def roots(self):
|
||||
""" Get roots of the Polynomial """
|
||||
@ -231,15 +232,24 @@ class Quadratic(Polynomial):
|
||||
|
||||
@property
|
||||
def a(self):
|
||||
return self[2]
|
||||
try:
|
||||
return self[2]
|
||||
except KeyError:
|
||||
return 0
|
||||
|
||||
@property
|
||||
def b(self):
|
||||
return self[1]
|
||||
try:
|
||||
return self[1]
|
||||
except KeyError:
|
||||
return 0
|
||||
|
||||
@property
|
||||
def c(self):
|
||||
return self[0]
|
||||
try:
|
||||
return self[0]
|
||||
except KeyError:
|
||||
return 0
|
||||
|
||||
@property
|
||||
def delta(self):
|
||||
@ -247,12 +257,33 @@ class Quadratic(Polynomial):
|
||||
|
||||
@property
|
||||
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:
|
||||
return []
|
||||
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:
|
||||
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'
|
||||
|
@ -80,10 +80,7 @@ class Token(object):
|
||||
return self._mo.__tex__
|
||||
|
||||
def _operate(self, other, operation):
|
||||
""" Make a operation between 2 Tokens,
|
||||
a Token and en Expression ora
|
||||
a Token an a builtin type
|
||||
"""
|
||||
""" Make a operation between 2 Tokens """
|
||||
from ..expression import Expression
|
||||
from ...core import Tree
|
||||
from . import factory
|
||||
@ -182,6 +179,30 @@ class Token(object):
|
||||
"""
|
||||
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'
|
||||
|
Loading…
Reference in New Issue
Block a user