2018-03-14 08:42:43 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
from mapytex.calculus.core.tree import Tree
|
|
|
|
from .mo import MO, MOnumber, MOstr
|
|
|
|
from .exceptions import MOError
|
2018-03-17 05:45:23 +00:00
|
|
|
from ..renders import tree2txt, tree2tex
|
2018-03-14 08:42:43 +00:00
|
|
|
|
|
|
|
__all__ = ["MOMonomial"]
|
|
|
|
|
|
|
|
class MOMonomial(MO):
|
|
|
|
|
|
|
|
""" Monomial math object"""
|
|
|
|
|
|
|
|
def __init__(self, coefficient, variable, power = 1):
|
|
|
|
""" Initiate the MOMonomial
|
|
|
|
|
|
|
|
:param coefficient: coefficient of the monomial (a non zero constant)
|
|
|
|
:param variable: varaible of the monomial (a MOstr or later a MOSqrt)
|
|
|
|
:param power: non negative interger (MOnumber type)
|
|
|
|
|
|
|
|
>>> MOMonomial(4, "x", 2)
|
|
|
|
<MOMonomial 4x^2>
|
|
|
|
>>> MOMonomial(4, "x", 1)
|
|
|
|
<MOMonomial 4x>
|
2018-03-17 05:45:23 +00:00
|
|
|
>>> MOMonomial(1, "x", 2)
|
|
|
|
<MOMonomial x^2>
|
|
|
|
>>> MOMonomial(1, "x", 1)
|
|
|
|
<MOMonomial x>
|
2018-03-14 08:42:43 +00:00
|
|
|
>>> MOMonomial(4, 3, 1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The variable of a monomial should be convertible into MOstr
|
|
|
|
>>> MOMonomial(4, "x", 0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a monomial should be greater than 0
|
|
|
|
>>> MOMonomial(4, "x", -2)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a monomial should be greater than 0
|
|
|
|
>>> MOMonomial(4, "x", 2.4)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a monomial should be a integer
|
|
|
|
>>> MOMonomial(0, "x", 1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The coefficient of a monomial should not be 0
|
|
|
|
"""
|
|
|
|
_coefficient = MO.factory(coefficient)
|
|
|
|
if coefficient == 0:
|
|
|
|
raise MOError("The coefficient of a monomial should not be 0")
|
|
|
|
self._coefficient = _coefficient
|
|
|
|
|
|
|
|
_variable = MO.factory(variable)
|
|
|
|
if not isinstance(_variable, MOstr):
|
|
|
|
raise MOError("The variable of a monomial should be convertible into MOstr")
|
|
|
|
self._variable = _variable
|
|
|
|
|
|
|
|
_power = MO.factory(power)
|
|
|
|
if power <= 0:
|
|
|
|
raise MOError("The power of a monomial should be greater than 0")
|
|
|
|
elif not isinstance(_power.value, int):
|
|
|
|
raise MOError("The power of a monomial should be a integer")
|
|
|
|
self._power = _power
|
|
|
|
|
|
|
|
if self._power > 1:
|
|
|
|
self.value = Tree("*",
|
|
|
|
self._coefficient,
|
|
|
|
Tree("^",
|
|
|
|
self._variable,
|
|
|
|
self._power
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.value = Tree("*",
|
|
|
|
self._coefficient,
|
|
|
|
self._variable,
|
|
|
|
)
|
|
|
|
|
2018-03-17 05:45:23 +00:00
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2018-03-14 08:42:43 +00:00
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|