Feat: Polynoms can be differentiate!!!!
This commit is contained in:
parent
b53de690d5
commit
9d9224fcba
@ -101,6 +101,24 @@ 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()
|
||||||
|
|
||||||
|
def differentiate(self):
|
||||||
|
""" Differentiate a polynome
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> from ...core.MO.polynomial import MOpolynomial
|
||||||
|
>>> P = Polynomial(MOpolynomial('x', [1, 2, 3]))
|
||||||
|
>>> P
|
||||||
|
<Polynomial 3x^2 + 2x + 1>
|
||||||
|
>>> P.differentiate()
|
||||||
|
<Linear 2 + 6x>
|
||||||
|
>>> for s in P.differentiate().explain():
|
||||||
|
... print(s)
|
||||||
|
0 + 2 + 3 * 2x
|
||||||
|
2 + 3 * 2 * x
|
||||||
|
2 + 6x
|
||||||
|
"""
|
||||||
|
return Expression(self._mo.differentiate()).simplify()
|
||||||
@property
|
@property
|
||||||
def roots(self):
|
def roots(self):
|
||||||
""" Get roots of the Polynomial """
|
""" Get roots of the Polynomial """
|
||||||
@ -120,6 +138,8 @@ class Linear(Polynomial):
|
|||||||
<MOnumber 2>
|
<MOnumber 2>
|
||||||
>>> P.b
|
>>> P.b
|
||||||
<MOnumber 1>
|
<MOnumber 1>
|
||||||
|
>>> P.differentiate()
|
||||||
|
<Integer 2>
|
||||||
>>> P.roots
|
>>> P.roots
|
||||||
[<Fraction - 2 / 1>]
|
[<Fraction - 2 / 1>]
|
||||||
|
|
||||||
@ -194,6 +214,8 @@ class Quadratic(Polynomial):
|
|||||||
4 - 12 * 1
|
4 - 12 * 1
|
||||||
4 - 12
|
4 - 12
|
||||||
- 8
|
- 8
|
||||||
|
>>> P.differentiate()
|
||||||
|
<Linear 2 + 6x>
|
||||||
>>> P.roots
|
>>> P.roots
|
||||||
[]
|
[]
|
||||||
|
|
||||||
|
@ -166,6 +166,16 @@ class MOnumber(Atom):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
return self.value < other
|
return self.value < other
|
||||||
|
|
||||||
|
def differentiate(self):
|
||||||
|
""" differentiate a number and get 0
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> a = MOnumber(3)
|
||||||
|
>>> a.differentiate()
|
||||||
|
<MOnumber 0>
|
||||||
|
"""
|
||||||
|
return MOnumber(0)
|
||||||
|
|
||||||
|
|
||||||
class MOstr(Atom):
|
class MOstr(Atom):
|
||||||
|
|
||||||
@ -260,6 +270,16 @@ class MOstr(Atom):
|
|||||||
def degree(self):
|
def degree(self):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
def differentiate(self):
|
||||||
|
""" differentiate a variable and get 1
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> a = MOstr("x")
|
||||||
|
>>> a.differentiate()
|
||||||
|
<MOnumber 1>
|
||||||
|
"""
|
||||||
|
return MOnumber(1)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
from mapytex.calculus.core.tree import Tree
|
from mapytex.calculus.core.tree import Tree
|
||||||
from .mo import Molecule, MO
|
from .mo import Molecule, MO
|
||||||
|
from .atoms import MOnumber
|
||||||
|
|
||||||
__all__ = ["MOFraction"]
|
__all__ = ["MOFraction"]
|
||||||
|
|
||||||
@ -71,6 +72,22 @@ class MOFraction(Molecule):
|
|||||||
""" return the inverse fraction """
|
""" return the inverse fraction """
|
||||||
return MOFraction(self._denominator, self._numerator, self.negative)
|
return MOFraction(self._denominator, self._numerator, self.negative)
|
||||||
|
|
||||||
|
def differentiate(self):
|
||||||
|
""" differentiate a fraction and get something!
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> a = MOFraction(2, 3)
|
||||||
|
>>> a.differentiate()
|
||||||
|
<MOnumber 0>
|
||||||
|
"""
|
||||||
|
d_num = self.numerator.differentiate()
|
||||||
|
d_denom = self.denominator.differentiate()
|
||||||
|
|
||||||
|
if d_num == 0 and d_denom == 0:
|
||||||
|
return MOnumber(0)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
@ -97,6 +97,10 @@ class MO(ABC):
|
|||||||
"""
|
"""
|
||||||
return self._signature
|
return self._signature
|
||||||
|
|
||||||
|
def differentiate(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Atom(MO):
|
class Atom(MO):
|
||||||
|
|
||||||
|
@ -110,6 +110,19 @@ class MOstrPower(Molecule):
|
|||||||
'monome2'
|
'monome2'
|
||||||
"""
|
"""
|
||||||
return f"monome{self.power}"
|
return f"monome{self.power}"
|
||||||
|
def differentiate(self):
|
||||||
|
""" differentiate a MOstrPower and get a tree
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> a = MOstrPower('x', 3)
|
||||||
|
>>> print(a.differentiate())
|
||||||
|
*
|
||||||
|
> 3
|
||||||
|
> x^2
|
||||||
|
"""
|
||||||
|
if self._power > 2:
|
||||||
|
return Tree('*', self.power, MOstrPower(self.variable, self._power._value-1))
|
||||||
|
return Tree('*', self.power, MOstr(self.variable))
|
||||||
|
|
||||||
|
|
||||||
class MOMonomial(Molecule):
|
class MOMonomial(Molecule):
|
||||||
@ -258,6 +271,31 @@ class MOMonomial(Molecule):
|
|||||||
"""
|
"""
|
||||||
return f"monome{self.power}"
|
return f"monome{self.power}"
|
||||||
|
|
||||||
|
def differentiate(self):
|
||||||
|
""" Differentiate a MOMonomial and get a tree
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> x = MOstr('x')
|
||||||
|
>>> m = MOMonomial(4, x)
|
||||||
|
>>> m
|
||||||
|
<MOMonomial 4x>
|
||||||
|
>>> print(m.differentiate())
|
||||||
|
4
|
||||||
|
>>> m = MOMonomial(4, 'x', 2)
|
||||||
|
>>> m
|
||||||
|
<MOMonomial 4x^2>
|
||||||
|
>>> print(m.differentiate())
|
||||||
|
*
|
||||||
|
> 4
|
||||||
|
> *
|
||||||
|
| > 2
|
||||||
|
| > x
|
||||||
|
|
||||||
|
"""
|
||||||
|
if self.power == 1:
|
||||||
|
return self.coefficient
|
||||||
|
return Tree("*", self.coefficient, self.strpower.differentiate())
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
@ -126,6 +126,28 @@ class MOpolynomial(Molecule):
|
|||||||
"""
|
"""
|
||||||
return self._monomials
|
return self._monomials
|
||||||
|
|
||||||
|
def differentiate(self):
|
||||||
|
""" Differentiate a MOMonomial and get a tree
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> p = MOpolynomial('x', [1, 2, 3])
|
||||||
|
>>> print(p)
|
||||||
|
3x^2 + 2x + 1
|
||||||
|
>>> print(p.differentiate())
|
||||||
|
+
|
||||||
|
> 0
|
||||||
|
> +
|
||||||
|
| > 2
|
||||||
|
| > *
|
||||||
|
| | > 3
|
||||||
|
| | > *
|
||||||
|
| | | > 2
|
||||||
|
| | | > x
|
||||||
|
|
||||||
|
"""
|
||||||
|
monomials_d = [m.differentiate() for m in self.monomials.values()]
|
||||||
|
return Tree.from_list("+", monomials_d)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
Loading…
Reference in New Issue
Block a user