2018-03-08 14:01:47 +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.
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2018-03-14 08:04:13 +00:00
|
|
|
from .exceptions import MOError
|
2018-03-10 13:08:28 +00:00
|
|
|
from ..renders import tree2txt, tree2tex
|
2018-03-09 16:31:46 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
__all__ = ["MO"]
|
2018-03-10 05:44:01 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
class MO(ABC):
|
2018-03-08 14:01:47 +00:00
|
|
|
|
|
|
|
"""MO for math object
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
It is an abstract class with wrap recognizable math objects.
|
2018-03-13 11:43:48 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
There is 2 types of MO:
|
|
|
|
- Atom which are compose of single value builtin.
|
|
|
|
- Molecule which are more complex objects organised in a tree.
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
"""
|
2018-03-13 11:43:48 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
MAINOP = None
|
2018-03-08 14:01:47 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def factory(cls, value):
|
2018-03-14 08:42:10 +00:00
|
|
|
""" Factory to ensure that a value is a MO before using it
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
Idempotent
|
2018-03-14 08:42:10 +00:00
|
|
|
|
|
|
|
>>> MO.factory("x")
|
|
|
|
<MOstr x>
|
|
|
|
>>> MO.factory(2)
|
|
|
|
<MOnumber 2>
|
|
|
|
>>> MO.factory(2.3)
|
|
|
|
<MOnumber 2.29999999999999982236431605997495353221893310546875>
|
|
|
|
>>> x = MO.factory("x")
|
|
|
|
>>> MO.factory(x)
|
|
|
|
<MOstr x>
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> from decimal import Decimal
|
|
|
|
>>> MO.factory(Decimal("2.3"))
|
|
|
|
<MOnumber 2.3>
|
2018-03-14 08:42:10 +00:00
|
|
|
"""
|
2018-12-21 10:26:37 +00:00
|
|
|
if isinstance(value, MO):
|
2018-03-11 15:34:41 +00:00
|
|
|
return value
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
return Atom.factory(value)
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def content(self):
|
|
|
|
""" content of the mo """
|
|
|
|
pass
|
2018-03-08 14:01:47 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2020-12-12 22:14:44 +00:00
|
|
|
return f"<{self.__class__.__name__} {self.__str__()}>"
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
@abstractmethod
|
2018-03-08 14:01:47 +00:00
|
|
|
def __str__(self):
|
2018-12-21 10:26:37 +00:00
|
|
|
pass
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2018-03-18 07:34:15 +00:00
|
|
|
def __hash__(self):
|
2018-12-21 10:26:37 +00:00
|
|
|
try:
|
|
|
|
return self._tree.__hash__()
|
|
|
|
except AttributeError:
|
|
|
|
return self._value.__hash__()
|
2018-03-18 07:34:15 +00:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
""" == a MOnumber """
|
|
|
|
try:
|
2018-12-21 10:26:37 +00:00
|
|
|
return self.content == other.content
|
2018-03-18 07:34:15 +00:00
|
|
|
except AttributeError:
|
2018-12-21 10:26:37 +00:00
|
|
|
return self.content == other
|
2018-03-10 05:44:01 +00:00
|
|
|
|
2018-11-23 09:05:36 +00:00
|
|
|
@property
|
|
|
|
def signature(self):
|
|
|
|
""" Name of the mo in the API
|
2018-12-21 10:26:37 +00:00
|
|
|
|
2018-11-23 09:05:36 +00:00
|
|
|
:example:
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> from .atoms import MOnumber, MOstr
|
2018-11-23 09:05:36 +00:00
|
|
|
>>> MOnumber(3).signature
|
|
|
|
'scalar'
|
|
|
|
>>> MOstr("x").signature
|
|
|
|
'monome1'
|
|
|
|
"""
|
|
|
|
return self._signature
|
|
|
|
|
2019-07-15 15:48:59 +00:00
|
|
|
def differentiate(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
class Atom(MO):
|
2018-03-09 16:31:46 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
""" Base Math Object with only one component.
|
2018-03-09 16:31:46 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
It is a wrapping of int, Décimal and str builtin python object
|
|
|
|
|
|
|
|
Its wrapping builtin can be access throw .value property
|
|
|
|
"""
|
2018-03-09 16:31:46 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
MAINOP = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def factory(cls, value):
|
|
|
|
""" Build the appropriate atom from the value
|
|
|
|
"""
|
|
|
|
for sub in cls.__subclasses__():
|
|
|
|
try:
|
|
|
|
return sub(value)
|
|
|
|
except MOError:
|
|
|
|
pass
|
|
|
|
raise MOError(f"Can't build an atom from {type(value)}")
|
|
|
|
|
|
|
|
def __init__(self, value):
|
|
|
|
""" Initiate an atom MO
|
2018-03-09 16:31:46 +00:00
|
|
|
"""
|
|
|
|
try:
|
2018-12-21 10:26:37 +00:00
|
|
|
# if the value is already an atom
|
|
|
|
self._value = value.value
|
2018-03-09 16:31:46 +00:00
|
|
|
except AttributeError:
|
2018-12-21 10:26:37 +00:00
|
|
|
self._value = value
|
|
|
|
|
|
|
|
self.is_scalar = True
|
|
|
|
self._signature = None
|
2018-03-09 16:31:46 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
@property
|
|
|
|
def value(self):
|
|
|
|
return self._value
|
2018-03-09 16:31:46 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
@property
|
|
|
|
def content(self):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.value)
|
2018-11-13 08:14:50 +00:00
|
|
|
|
2018-03-10 05:03:55 +00:00
|
|
|
@property
|
|
|
|
def __txt__(self):
|
2018-12-21 10:26:37 +00:00
|
|
|
return str(self.value)
|
2018-03-10 05:03:55 +00:00
|
|
|
|
2018-03-10 13:08:28 +00:00
|
|
|
@property
|
|
|
|
def __tex__(self):
|
2018-12-21 10:26:37 +00:00
|
|
|
return str(self.value)
|
2018-03-10 13:08:28 +00:00
|
|
|
|
2018-03-13 11:43:48 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
class Molecule(MO):
|
2018-03-13 15:57:38 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
""" Complex Math Object composed of multiple components
|
2018-03-13 15:57:38 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
It is a wrapping of tree
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2020-12-12 22:14:44 +00:00
|
|
|
Its wrapping tree can be access through .tree property
|
2018-12-21 10:26:37 +00:00
|
|
|
"""
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
MAINOP = None
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2020-12-12 22:14:44 +00:00
|
|
|
def __init__(self, tree):
|
2018-12-21 10:26:37 +00:00
|
|
|
""" Initiate the MO
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
It should be idempotent.
|
2018-03-08 14:01:47 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
2020-12-12 22:14:44 +00:00
|
|
|
self._tree = tree._tree
|
2018-03-08 14:01:47 +00:00
|
|
|
except AttributeError:
|
2020-12-12 22:14:44 +00:00
|
|
|
self._tree = tree
|
2018-03-08 14:01:47 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
self.is_scalar = True
|
|
|
|
self._signature = None
|
2018-03-18 07:34:15 +00:00
|
|
|
|
|
|
|
@property
|
2018-12-21 10:26:37 +00:00
|
|
|
def tree(self):
|
|
|
|
return self._tree
|
2018-03-13 11:43:48 +00:00
|
|
|
|
2018-12-07 14:09:39 +00:00
|
|
|
@property
|
2018-12-21 10:26:37 +00:00
|
|
|
def content(self):
|
2020-12-12 22:14:44 +00:00
|
|
|
return self.tree
|
2018-12-07 14:09:39 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
def __str__(self):
|
2020-12-12 22:14:44 +00:00
|
|
|
return tree2txt(self.tree)
|
2018-12-21 10:26:37 +00:00
|
|
|
|
|
|
|
|
2018-03-08 14:01:47 +00:00
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|