Feat: coefficients, delta and some roots for polynomial, Linear and Quadratic
This commit is contained in:
parent
a83b5ada8d
commit
50f77c4d60
@ -31,6 +31,7 @@ class Polynomial(Token):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, a, name="", ancestor=None):
|
def __init__(self, a, name="", ancestor=None):
|
||||||
|
""" Initiate Polynomial with a MO"""
|
||||||
if not isinstance(a, MO):
|
if not isinstance(a, MO):
|
||||||
if isinstance(a, str):
|
if isinstance(a, str):
|
||||||
raise TypeError
|
raise TypeError
|
||||||
@ -47,17 +48,37 @@ class Polynomial(Token):
|
|||||||
|
|
||||||
return cls(mo, name, ancestor)
|
return cls(mo, name, ancestor)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_coefficients(cls, coefficients):
|
||||||
|
""" Initiate polynomial from list of coefficients """
|
||||||
|
pass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def random(cls):
|
def random(cls):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
def __setitem__(self, key, item):
|
def __setitem__(self, key, item):
|
||||||
""" Use Polynomial like if they were a dictionnary to set coefficients """
|
""" Use Polynomial like if they were a dictionnary to set coefficients """
|
||||||
pass
|
raise NotImplemented("Can't set coefficient of a polynomial")
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
""" Use Polynomial like if they were a dictionnary to get coefficients """
|
""" Use Polynomial like if they were a dictionnary to get coefficients
|
||||||
pass
|
|
||||||
|
:examples:
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial
|
||||||
|
>>> P = Polynomial(MOpolynomial('x', [1, 2, 3]))
|
||||||
|
>>> P[0]
|
||||||
|
<MOnumber 1>
|
||||||
|
>>> P[1]
|
||||||
|
<MOnumber 2>
|
||||||
|
>>> P[2]
|
||||||
|
<MOnumber 3>
|
||||||
|
>>> P[3]
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
KeyError: 3
|
||||||
|
"""
|
||||||
|
return self._mo.coefficients[key]
|
||||||
|
|
||||||
def __call__(self, value):
|
def __call__(self, value):
|
||||||
""" Call a Polynomial to evaluate itself on value
|
""" Call a Polynomial to evaluate itself on value
|
||||||
@ -80,14 +101,42 @@ class Polynomial(Token):
|
|||||||
tree = tree.map_on_leaf(replace_var)
|
tree = tree.map_on_leaf(replace_var)
|
||||||
|
|
||||||
return Expression(tree).simplify()
|
return Expression(tree).simplify()
|
||||||
|
@property
|
||||||
|
def roots(self):
|
||||||
|
""" Get roots of the Polynomial """
|
||||||
|
raise NotImplemented("Can't compute roots not specific polynomial")
|
||||||
|
|
||||||
|
|
||||||
class Linear(Polynomial):
|
class Linear(Polynomial):
|
||||||
|
|
||||||
""" Token representing a linear """
|
""" Token representing a linear ax + b
|
||||||
|
|
||||||
|
:examples:
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial, MOMonomial
|
||||||
|
>>> P = Linear(MOpolynomial('x', [1, 2]))
|
||||||
|
>>> P
|
||||||
|
<Linear 2x + 1>
|
||||||
|
>>> P.a
|
||||||
|
<MOnumber 2>
|
||||||
|
>>> P.b
|
||||||
|
<MOnumber 1>
|
||||||
|
>>> P.roots
|
||||||
|
[<Fraction - 2 / 1>]
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, mo, name="", ancestor=None):
|
def __init__(self, mo, name="", ancestor=None):
|
||||||
|
""" Initiate Linear with MO
|
||||||
|
|
||||||
|
:examples:
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial, MOMonomial
|
||||||
|
>>> P = Linear(MOpolynomial('x', [1, 2]))
|
||||||
|
>>> P
|
||||||
|
<Linear 2x + 1>
|
||||||
|
>>> Q = Linear(MOMonomial(3, 'x', 1))
|
||||||
|
>>> Q
|
||||||
|
<Linear 3x>
|
||||||
|
"""
|
||||||
Polynomial.__init__(self, mo, name, ancestor)
|
Polynomial.__init__(self, mo, name, ancestor)
|
||||||
self._mathtype = "affine"
|
self._mathtype = "affine"
|
||||||
|
|
||||||
@ -95,12 +144,69 @@ class Linear(Polynomial):
|
|||||||
def random(cls):
|
def random(cls):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
|
@property
|
||||||
|
def a(self):
|
||||||
|
return self[1]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def b(self):
|
||||||
|
return self[0]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def roots(self):
|
||||||
|
""" Get the root of the polynomial
|
||||||
|
|
||||||
|
:examples:
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial, MOMonomial
|
||||||
|
>>> P = Linear(MOpolynomial('x', [1, 2]))
|
||||||
|
>>> P.roots
|
||||||
|
[<Fraction - 2 / 1>]
|
||||||
|
>>> #P = Linear(MOpolynomial('x', [1, -2]))
|
||||||
|
>>> #P.roots
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
return [Expression.from_str(f"-{self.a}/{self.b}").simplify()]
|
||||||
|
except AttributeError:
|
||||||
|
return [Expression.from_str(f"-{self.a}/{self.b}")]
|
||||||
|
|
||||||
|
|
||||||
class Quadratic(Polynomial):
|
class Quadratic(Polynomial):
|
||||||
|
|
||||||
""" Token representing a quadratic """
|
""" Token representing a quadratic ax^2 + bx + c
|
||||||
|
|
||||||
|
:examples:
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial
|
||||||
|
>>> P = Quadratic(MOpolynomial('x', [1, 2, 3]))
|
||||||
|
>>> P
|
||||||
|
<Quadratic 3x^2 + 2x + 1>
|
||||||
|
>>> P.a
|
||||||
|
<MOnumber 3>
|
||||||
|
>>> P.b
|
||||||
|
<MOnumber 2>
|
||||||
|
>>> P.c
|
||||||
|
<MOnumber 1>
|
||||||
|
>>> P.delta
|
||||||
|
<Integer - 8>
|
||||||
|
>>> for s in P.delta.explain():
|
||||||
|
... print(s)
|
||||||
|
2^2 - 4 * 3 * 1
|
||||||
|
4 - 12 * 1
|
||||||
|
4 - 12
|
||||||
|
- 8
|
||||||
|
>>> P.roots
|
||||||
|
[]
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, mo, name="", ancestor=None):
|
def __init__(self, mo, name="", ancestor=None):
|
||||||
|
""" Initiate Quadratic from MO
|
||||||
|
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial
|
||||||
|
>>> P = Quadratic(MOpolynomial('x', [1, 2, 3]))
|
||||||
|
>>> P
|
||||||
|
<Quadratic 3x^2 + 2x + 1>
|
||||||
|
"""
|
||||||
|
|
||||||
Polynomial.__init__(self, mo, name, ancestor)
|
Polynomial.__init__(self, mo, name, ancestor)
|
||||||
self._mathtype = "polynome du 2nd degré"
|
self._mathtype = "polynome du 2nd degré"
|
||||||
@ -109,6 +215,32 @@ class Quadratic(Polynomial):
|
|||||||
def random(cls):
|
def random(cls):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
|
@property
|
||||||
|
def a(self):
|
||||||
|
return self[2]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def b(self):
|
||||||
|
return self[1]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def c(self):
|
||||||
|
return self[0]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def delta(self):
|
||||||
|
return Expression.from_str(f"{self.b}^2-4*{self.a}*{self.c}").simplify()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def roots(self):
|
||||||
|
if self.delta._mo < 0:
|
||||||
|
return []
|
||||||
|
elif self.delta._mo == 0:
|
||||||
|
return [Expression.from_str(f"-{self.b}/(2*{self.a})").simplify()]
|
||||||
|
else:
|
||||||
|
raise NotImplemented("Todo!")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def extract_variable(leaf):
|
def extract_variable(leaf):
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user