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
|
2018-12-21 10:26:37 +00:00
|
|
|
from .mo import Molecule
|
|
|
|
from . import MO, MOnumber, MOstr
|
2018-03-14 08:42:43 +00:00
|
|
|
from .exceptions import MOError
|
|
|
|
|
|
|
|
__all__ = ["MOMonomial"]
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
class MOstrPower(Molecule):
|
2018-03-14 08:42:43 +00:00
|
|
|
|
2018-03-18 07:34:46 +00:00
|
|
|
""" Power of a MOstr """
|
2018-03-14 08:42:43 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
MAINOP = "^"
|
|
|
|
|
2018-03-18 07:34:46 +00:00
|
|
|
def __init__(self, variable, power):
|
|
|
|
""" Initiate a MOstrPower
|
2018-03-14 08:42:43 +00:00
|
|
|
|
2018-03-18 07:34:46 +00:00
|
|
|
:param variable: variable of the monomial (a MOstr or later a MOSqrt)
|
2018-03-14 08:42:43 +00:00
|
|
|
:param power: non negative interger (MOnumber type)
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> s = MOstrPower("x", 2)
|
|
|
|
>>> s
|
2018-03-18 07:34:46 +00:00
|
|
|
<MOstrPower x^2>
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> print(s)
|
2018-12-21 11:20:13 +00:00
|
|
|
x^2
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> print(s.__txt__)
|
|
|
|
x^2
|
|
|
|
>>> print(s.__tex__)
|
|
|
|
x^{2}
|
2018-03-18 07:34:46 +00:00
|
|
|
>>> MOstrPower(3, 1)
|
2018-03-14 08:42:43 +00:00
|
|
|
Traceback (most recent call last):
|
2018-11-13 09:58:32 +00:00
|
|
|
...
|
2018-03-14 08:42:43 +00:00
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The variable of a monomial should be convertible into MOstr
|
2018-03-18 07:34:46 +00:00
|
|
|
>>> MOstrPower("x", 0)
|
2018-03-14 08:42:43 +00:00
|
|
|
Traceback (most recent call last):
|
2018-11-13 09:58:32 +00:00
|
|
|
...
|
2018-03-18 07:34:46 +00:00
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1
|
|
|
|
>>> MOstrPower("x", 1)
|
2018-03-14 08:42:43 +00:00
|
|
|
Traceback (most recent call last):
|
2018-11-13 09:58:32 +00:00
|
|
|
...
|
2018-03-18 07:34:46 +00:00
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1
|
|
|
|
>>> MOstrPower("x", -2)
|
2018-03-14 08:42:43 +00:00
|
|
|
Traceback (most recent call last):
|
2018-11-13 09:58:32 +00:00
|
|
|
...
|
2018-03-18 07:34:46 +00:00
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1
|
|
|
|
>>> MOstrPower("x", 2.4)
|
2018-03-14 08:42:43 +00:00
|
|
|
Traceback (most recent call last):
|
2018-11-13 09:58:32 +00:00
|
|
|
...
|
2018-03-18 07:34:46 +00:00
|
|
|
mapytex.calculus.core.MO.exceptions.MOError: The power of a monomial should be a integer
|
2018-03-14 08:42:43 +00:00
|
|
|
|
2018-03-18 07:34:46 +00:00
|
|
|
"""
|
2018-03-14 08:42:43 +00:00
|
|
|
_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)
|
2018-03-18 07:34:46 +00:00
|
|
|
if power <= 1:
|
|
|
|
raise MOError("The power of a MOstrPower should be greater than 1")
|
2018-12-21 10:26:37 +00:00
|
|
|
elif not isinstance(_power.content, int):
|
2018-03-14 08:42:43 +00:00
|
|
|
raise MOError("The power of a monomial should be a integer")
|
|
|
|
self._power = _power
|
2018-11-13 09:58:32 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
_tree = Tree("^", self._variable, self._power)
|
2018-11-13 09:58:32 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
Molecule.__init__(self, _tree)
|
2018-12-07 14:09:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def coefficients(self):
|
|
|
|
""" Dictionnary of coefficients
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> p = MOstrPower("x", 2)
|
|
|
|
>>> p.coefficients
|
|
|
|
{2: <MOnumber 1>}
|
|
|
|
"""
|
|
|
|
|
|
|
|
return {self.power.value: MOnumber(1)}
|
|
|
|
|
2018-03-18 07:34:46 +00:00
|
|
|
@property
|
|
|
|
def variable(self):
|
|
|
|
return self._variable
|
|
|
|
|
|
|
|
@property
|
|
|
|
def power(self):
|
2018-12-07 14:29:34 +00:00
|
|
|
""" MO version """
|
2018-03-18 07:34:46 +00:00
|
|
|
return self._power
|
|
|
|
|
2018-12-07 14:29:34 +00:00
|
|
|
@property
|
|
|
|
def degree(self):
|
|
|
|
""" python version """
|
|
|
|
return self._power.value
|
|
|
|
|
2018-11-23 09:05:36 +00:00
|
|
|
@property
|
|
|
|
def signature(self):
|
|
|
|
""" Name of the mo in the API
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> MOstrPower("x", 3).signature
|
|
|
|
'monome3'
|
|
|
|
>>> MOstrPower("x", 2).signature
|
|
|
|
'monome2'
|
|
|
|
"""
|
|
|
|
return f"monome{self.power}"
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
class MOMonomial(Molecule):
|
2018-03-18 07:34:46 +00:00
|
|
|
|
|
|
|
""" Monomial math object"""
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
MAINOP = "*"
|
|
|
|
|
2018-11-13 09:58:32 +00:00
|
|
|
def __init__(self, coefficient, variable, power=1):
|
2018-03-18 07:34:46 +00:00
|
|
|
""" Initiate the MOMonomial
|
|
|
|
|
|
|
|
:param coefficient: coefficient of the monomial (a non zero constant)
|
2018-11-13 09:58:32 +00:00
|
|
|
:param variable: variable of the monomial (a MOstr, a MOstrPower)
|
|
|
|
:param power: degree of the monomial
|
2018-03-18 07:34:46 +00:00
|
|
|
|
|
|
|
>>> x = MOstr('x')
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> m = MOMonomial(4, x)
|
|
|
|
>>> m
|
2018-03-18 07:34:46 +00:00
|
|
|
<MOMonomial 4x>
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> print(m)
|
2018-12-21 11:20:13 +00:00
|
|
|
4x
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> print(m.__txt__)
|
|
|
|
4x
|
|
|
|
>>> print(m.__tex__)
|
|
|
|
4x
|
2018-03-18 07:34:46 +00:00
|
|
|
>>> x = MOstrPower('x', 2)
|
|
|
|
>>> MOMonomial(4, x)
|
|
|
|
<MOMonomial 4x^2>
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> m = MOMonomial(4, 'x')
|
|
|
|
>>> m
|
2018-12-07 10:21:40 +00:00
|
|
|
<MOMonomial 4x>
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> print(m)
|
2018-12-21 11:20:13 +00:00
|
|
|
4x
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> print(m.__txt__)
|
|
|
|
4x
|
|
|
|
>>> print(m.__tex__)
|
|
|
|
4x
|
2018-12-07 10:21:40 +00:00
|
|
|
>>> MOMonomial(4, 'x', 1)
|
|
|
|
<MOMonomial 4x>
|
|
|
|
>>> MOMonomial(4, 'x', 2)
|
|
|
|
<MOMonomial 4x^2>
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> x2 = MOstrPower('x', 2)
|
|
|
|
>>> MOMonomial(4, x2, 3)
|
2018-12-07 10:21:40 +00:00
|
|
|
<MOMonomial 4x^6>
|
2018-03-18 07:34:46 +00:00
|
|
|
>>> 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")
|
2018-12-21 10:26:37 +00:00
|
|
|
elif coefficient == 1:
|
2019-05-14 04:55:56 +00:00
|
|
|
raise MOError(
|
|
|
|
"The coefficient of a monomial should not be 1, it is a MOstrPower or MOstr"
|
|
|
|
)
|
2018-03-18 07:34:46 +00:00
|
|
|
self._coefficient = _coefficient
|
|
|
|
|
2018-11-13 14:55:55 +00:00
|
|
|
_variable = MO.factory(variable)
|
|
|
|
if isinstance(_variable, MOstrPower):
|
|
|
|
_power = MO.factory(_variable.power.value * power)
|
|
|
|
_variable = _variable.variable
|
|
|
|
elif isinstance(_variable, MOstr):
|
2018-11-13 09:58:32 +00:00
|
|
|
_power = MO.factory(power)
|
|
|
|
else:
|
2019-05-14 04:55:56 +00:00
|
|
|
raise MOError(
|
|
|
|
f"variable need to be a MOstrPower or a MOstr. Got {type(variable)}."
|
|
|
|
)
|
2018-03-18 07:34:46 +00:00
|
|
|
|
2018-11-13 09:58:32 +00:00
|
|
|
self._variable = _variable
|
|
|
|
self._power = _power
|
2018-03-18 07:34:46 +00:00
|
|
|
|
2018-12-07 10:21:40 +00:00
|
|
|
try:
|
|
|
|
if self._coefficient.value != 1:
|
|
|
|
_tree = Tree("*", self._coefficient, self.strpower)
|
2018-11-13 10:40:21 +00:00
|
|
|
else:
|
2018-12-07 10:21:40 +00:00
|
|
|
_tree = self.strpower
|
|
|
|
except AttributeError:
|
|
|
|
_tree = Tree("*", self._coefficient, self.strpower)
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
Molecule.__init__(self, _tree)
|
2018-03-18 07:34:46 +00:00
|
|
|
|
2018-12-21 11:26:11 +00:00
|
|
|
def __str__(self):
|
|
|
|
if self._coefficient != -1:
|
|
|
|
return super(MOMonomial, self).__str__()
|
|
|
|
else:
|
|
|
|
return "- " + self.strpower.__str__()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __txt__(self):
|
|
|
|
if self._coefficient != -1:
|
|
|
|
return super(MOMonomial, self).__txt__
|
|
|
|
else:
|
|
|
|
return "- " + self.strpower.__txt__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __tex__(self):
|
|
|
|
if self._coefficient != -1:
|
|
|
|
return super(MOMonomial, self).__tex__
|
|
|
|
else:
|
|
|
|
return "- " + self.strpower.__tex__
|
|
|
|
|
2018-03-17 05:45:23 +00:00
|
|
|
@property
|
2018-03-18 07:34:46 +00:00
|
|
|
def coefficient(self):
|
|
|
|
return self._coefficient
|
2018-03-17 05:45:23 +00:00
|
|
|
|
2018-12-07 14:09:39 +00:00
|
|
|
@property
|
|
|
|
def coefficients(self):
|
|
|
|
""" Dictionnary of coefficients
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> p = MOMonomial(3, "x", 2)
|
|
|
|
>>> p.coefficients
|
|
|
|
{2: <MOnumber 3>}
|
|
|
|
"""
|
|
|
|
|
|
|
|
return {self.power.value: self._coefficient}
|
|
|
|
|
2018-03-17 05:45:23 +00:00
|
|
|
@property
|
2018-03-18 07:34:46 +00:00
|
|
|
def strpower(self):
|
2018-11-19 11:21:59 +00:00
|
|
|
if self._power == 1:
|
|
|
|
return self.variable
|
2018-11-13 09:58:32 +00:00
|
|
|
return MOstrPower(self._variable, self._power)
|
2018-03-17 05:45:23 +00:00
|
|
|
|
2018-03-18 07:34:46 +00:00
|
|
|
@property
|
|
|
|
def variable(self):
|
2018-11-13 09:58:32 +00:00
|
|
|
return self._variable
|
2018-03-18 07:34:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def power(self):
|
2018-11-13 09:58:32 +00:00
|
|
|
return self._power
|
2018-03-17 05:45:23 +00:00
|
|
|
|
2018-11-23 09:05:36 +00:00
|
|
|
@property
|
2018-12-07 14:29:34 +00:00
|
|
|
def degree(self):
|
|
|
|
return self._power.value
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-07 14:29:34 +00:00
|
|
|
@property
|
2018-11-23 09:05:36 +00:00
|
|
|
def signature(self):
|
|
|
|
""" Name of the mo in the API
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> MOMonomial(2, "x").signature
|
|
|
|
'monome1'
|
|
|
|
>>> MOMonomial(4, "x", 2).signature
|
|
|
|
'monome2'
|
|
|
|
"""
|
|
|
|
return f"monome{self.power}"
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-03-14 08:42:43 +00:00
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|