158 lines
4.8 KiB
Python
158 lines
4.8 KiB
Python
#! /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
|
|
from ..renders import tree2txt, tree2tex
|
|
|
|
__all__ = ["MOMonomial"]
|
|
|
|
class MOstrPower(MO):
|
|
|
|
""" Power of a MOstr """
|
|
|
|
def __init__(self, variable, power):
|
|
""" Initiate a MOstrPower
|
|
|
|
:param variable: variable of the monomial (a MOstr or later a MOSqrt)
|
|
:param power: non negative interger (MOnumber type)
|
|
|
|
>>> MOstrPower("x", 2)
|
|
<MOstrPower x^2>
|
|
>>> MOstrPower(3, 1)
|
|
Traceback (most recent call last):
|
|
...
|
|
mapytex.calculus.core.MO.exceptions.MOError: The variable of a monomial should be convertible into MOstr
|
|
>>> MOstrPower("x", 0)
|
|
Traceback (most recent call last):
|
|
...
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1
|
|
>>> MOstrPower("x", 1)
|
|
Traceback (most recent call last):
|
|
...
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1
|
|
>>> MOstrPower("x", -2)
|
|
Traceback (most recent call last):
|
|
...
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1
|
|
>>> MOstrPower("x", 2.4)
|
|
Traceback (most recent call last):
|
|
...
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a monomial should be a integer
|
|
|
|
"""
|
|
_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 <= 1:
|
|
raise MOError("The power of a MOstrPower should be greater than 1")
|
|
elif not isinstance(_power._tree, int):
|
|
raise MOError("The power of a monomial should be a integer")
|
|
self._power = _power
|
|
|
|
_tree = Tree("^",
|
|
self._variable,
|
|
self._power,
|
|
)
|
|
|
|
MO.__init__(self, _tree)
|
|
|
|
@property
|
|
def variable(self):
|
|
return self._variable
|
|
|
|
@property
|
|
def power(self):
|
|
return self._power
|
|
|
|
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: variable of the monomial (a MOstr, a MOstrPower)
|
|
:param power: degree of the monomial
|
|
|
|
>>> x = MOstr('x')
|
|
>>> MOMonomial(4, x)
|
|
<MOMonomial 4x>
|
|
>>> x = MOstrPower('x', 2)
|
|
>>> MOMonomial(4, x)
|
|
<MOMonomial 4x^2>
|
|
>>> MOMonomial(0, x)
|
|
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 isinstance(_variable, MOstrPower):
|
|
_power = MO.factory(_variable.power.value * power)
|
|
_variable = _variable.variable
|
|
elif isinstance(_variable, MOstr):
|
|
_variable = variable
|
|
_power = MO.factory(power)
|
|
else:
|
|
raise MOError(f"variable need to be a MOstrPower or a MOstr. Got {type(variable)}.")
|
|
|
|
self._variable = _variable
|
|
self._power = _power
|
|
|
|
if self._power == 1:
|
|
if self._coefficient == 1:
|
|
_tree = self._variable
|
|
else:
|
|
_tree = Tree("*",
|
|
self._coefficient,
|
|
self._variable
|
|
)
|
|
else:
|
|
if self._coefficient == 1:
|
|
_tree = MOstrPower(self._variable, self._power)
|
|
else:
|
|
_tree = Tree("*",
|
|
self._coefficient,
|
|
MOstrPower(self._variable, self._power)
|
|
)
|
|
|
|
MO.__init__(self, _tree)
|
|
|
|
@property
|
|
def coefficient(self):
|
|
return self._coefficient
|
|
|
|
@property
|
|
def strpower(self):
|
|
if self._power == 1:
|
|
return self.variable
|
|
return MOstrPower(self._variable, self._power)
|
|
|
|
@property
|
|
def variable(self):
|
|
return self._variable
|
|
|
|
@property
|
|
def power(self):
|
|
return self._power
|
|
|
|
# -----------------------------
|
|
# Reglages pour 'vim'
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
# cursor: 16 del
|