Start MOMonomial class

This commit is contained in:
Bertrand Benjamin 2018-03-14 11:42:43 +03:00
parent 3aa7c92f54
commit 6e2cc96781
1 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,85 @@
#! /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
__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>
>>> 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,
)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del