Feat: Polynoms can be differentiate!!!!

This commit is contained in:
Bertrand Benjamin 2019-07-15 17:48:59 +02:00
parent 8389a319f0
commit 41385ec561
6 changed files with 123 additions and 0 deletions

View File

@ -101,6 +101,24 @@ class Polynomial(Token):
tree = tree.map_on_leaf(replace_var)
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
def roots(self):
""" Get roots of the Polynomial """
@ -120,6 +138,8 @@ class Linear(Polynomial):
<MOnumber 2>
>>> P.b
<MOnumber 1>
>>> P.differentiate()
<Integer 2>
>>> P.roots
[<Fraction - 2 / 1>]
@ -194,6 +214,8 @@ class Quadratic(Polynomial):
4 - 12 * 1
4 - 12
- 8
>>> P.differentiate()
<Linear 2 + 6x>
>>> P.roots
[]

View File

@ -166,6 +166,16 @@ class MOnumber(Atom):
except AttributeError:
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):
@ -260,6 +270,16 @@ class MOstr(Atom):
def degree(self):
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'

View File

@ -8,6 +8,7 @@
from mapytex.calculus.core.tree import Tree
from .mo import Molecule, MO
from .atoms import MOnumber
__all__ = ["MOFraction"]
@ -71,6 +72,22 @@ class MOFraction(Molecule):
""" return the inverse fraction """
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'

View File

@ -97,6 +97,10 @@ class MO(ABC):
"""
return self._signature
def differentiate(self):
raise NotImplementedError
class Atom(MO):

View File

@ -110,6 +110,19 @@ class MOstrPower(Molecule):
'monome2'
"""
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):
@ -258,6 +271,31 @@ class MOMonomial(Molecule):
"""
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'

View File

@ -126,6 +126,28 @@ class MOpolynomial(Molecule):
"""
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'