From 6e2cc967813cb6fc7b11058d95f2a75fa10d2bc7 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 14 Mar 2018 11:42:43 +0300 Subject: [PATCH] Start MOMonomial class --- mapytex/calculus/core/MO/monomial.py | 85 ++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 mapytex/calculus/core/MO/monomial.py diff --git a/mapytex/calculus/core/MO/monomial.py b/mapytex/calculus/core/MO/monomial.py new file mode 100644 index 0000000..a995862 --- /dev/null +++ b/mapytex/calculus/core/MO/monomial.py @@ -0,0 +1,85 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 lafrite +# +# 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(4, "x", 1) + + >>> 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