Render momonial and multiply with scalar

This commit is contained in:
Bertrand Benjamin 2018-03-17 08:45:23 +03:00
parent b0ff919693
commit 0cef131c41
2 changed files with 80 additions and 1 deletions

View File

@ -9,6 +9,7 @@
from mapytex.calculus.core.tree import Tree
from .mo import MO, MOnumber, MOstr
from .exceptions import MOError
from ..renders import tree2txt, tree2tex
__all__ = ["MOMonomial"]
@ -27,6 +28,10 @@ class MOMonomial(MO):
<MOMonomial 4x^2>
>>> MOMonomial(4, "x", 1)
<MOMonomial 4x>
>>> MOMonomial(1, "x", 2)
<MOMonomial x^2>
>>> MOMonomial(1, "x", 1)
<MOMonomial x>
>>> MOMonomial(4, 3, 1)
Traceback (most recent call last):
...
@ -79,6 +84,39 @@ class MOMonomial(MO):
self._variable,
)
@property
def __txt__(self):
try:
if self._coefficient == 1:
try:
return tree2txt(self.value.right_value)
except AttributeError:
return str(self.value.right_value)
except TypeError:
pass
try:
return tree2txt(self.value)
except AttributeError:
return str(self.value)
@property
def __tex__(self):
try:
if self._coefficient == 1:
try:
return tree2tex(self.value.right_value)
except AttributeError:
return str(self.value.right_value)
except TypeError:
pass
try:
return tree2tex(self.value)
except AttributeError:
return str(self.value)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -11,8 +11,9 @@ Multiply MO
"""
from ..tree import Tree
from ..MO.mo import MO, MOnumber
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOMonomial
from .exceptions import MultiplyError
from .type_filter import args_are
@ -145,6 +146,46 @@ def mofraction_mofraction(left, right):
denom = Tree("*", left.denominator, right.denominator)
return Tree("/", num, denom)
@args_are((MOnumber, MOFraction), MOstr)
def moscalar_mostr(left, right):
""" Multiply a scalar with a letter
>>> a = MOnumber(2)
>>> b = MOstr('x')
>>> moscalar_mostr(a, b)
<MOMonomial 2x>
>>> a = MOFraction(1, 5)
>>> moscalar_mostr(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(left, right)
@args_are(MOstr, (MOnumber, MOFraction))
def mostr_moscalar(left, right):
""" Multiply a scalar with a letter
>>> a = MOstr('x')
>>> b = MOnumber(2)
>>> mostr_moscalar(a, b)
<MOMonomial 2x>
>>> b = MOFraction(1, 5)
>>> mostr_moscalar(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(right, left)
@args_are((MOnumber, MOFraction), MOMonomial)
def moscalar_monomonial(left, right):
""" Multiply a scalar with a monomial
>>> a = MOnumber(4)
>>> b = MOMonomial(5, 'x', 3)
# >>> print(moscalar_monomonial(a, b))
"""
coefficient = Tree('*', left, right._coefficient)
return Tree('*', coefficient, MOMonomial(1, right._variable, right._power))
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
MULFUNCTIONS = {
(MOnumber, MOnumber): monumber_monumber,