2018-01-21 15:16:08 +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-03-11 15:34:41 +00:00
|
|
|
Computing with MO
|
2018-01-21 15:16:08 +00:00
|
|
|
"""
|
|
|
|
|
2018-03-11 15:34:41 +00:00
|
|
|
from .exceptions import ComputeError
|
2018-03-13 02:46:14 +00:00
|
|
|
from .add import add
|
2018-10-08 15:57:17 +00:00
|
|
|
from .divide import divide
|
2018-03-13 02:46:14 +00:00
|
|
|
from .minus import minus
|
|
|
|
from .multiply import multiply
|
2018-10-08 15:57:17 +00:00
|
|
|
from .power import power
|
2018-03-13 02:46:14 +00:00
|
|
|
|
2018-09-28 08:43:12 +00:00
|
|
|
from ..MO.mo import MOnumber, MOstr
|
|
|
|
from ..MO.fraction import MOFraction
|
|
|
|
from ..MO.monomial import MOstrPower, MOMonomial
|
|
|
|
|
|
|
|
from itertools import product
|
|
|
|
from tabulate import tabulate
|
|
|
|
|
|
|
|
MOS = [ MOnumber, MOstr, MOFraction, MOstrPower, MOMonomial ]
|
|
|
|
|
2018-03-13 02:46:14 +00:00
|
|
|
OPERATIONS = {
|
2018-10-08 15:57:17 +00:00
|
|
|
"+": add,
|
|
|
|
"-": minus,
|
2018-03-13 02:46:14 +00:00
|
|
|
"*": multiply,
|
2018-03-13 15:57:38 +00:00
|
|
|
"/": divide,
|
2018-10-08 15:57:17 +00:00
|
|
|
"^": power,
|
2018-03-13 02:46:14 +00:00
|
|
|
}
|
2018-03-07 13:26:06 +00:00
|
|
|
|
2018-01-21 15:16:08 +00:00
|
|
|
def compute(node, left_v, right_v):
|
|
|
|
"""
|
|
|
|
Computing a node
|
|
|
|
|
|
|
|
:example:
|
|
|
|
|
2018-03-13 02:46:14 +00:00
|
|
|
>>> from ..MO.mo import MOnumber
|
|
|
|
>>> compute("+", MOnumber(1), MOnumber(2))
|
|
|
|
<MOnumber 3>
|
|
|
|
>>> compute("-", None, MOnumber(2))
|
|
|
|
<MOnumber - 2>
|
|
|
|
>>> compute("*", MOnumber(1), MOnumber(2))
|
|
|
|
<MOnumber 2>
|
|
|
|
>>> compute("~", MOnumber(1), MOnumber(2))
|
2018-03-07 13:26:06 +00:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2018-10-10 08:46:58 +00:00
|
|
|
mapytex.calculus.core.compute.exceptions.ComputeError: Unknown operation (~) in compute
|
2018-01-21 15:16:08 +00:00
|
|
|
"""
|
2018-03-13 02:46:14 +00:00
|
|
|
try:
|
|
|
|
operation = OPERATIONS[node]
|
|
|
|
except KeyError:
|
2018-10-10 08:46:58 +00:00
|
|
|
raise ComputeError(f"Unknown operation ({node}) in compute")
|
2018-03-13 02:46:14 +00:00
|
|
|
|
|
|
|
return operation(left_v, right_v)
|
2018-01-21 15:16:08 +00:00
|
|
|
|
2018-09-28 08:43:12 +00:00
|
|
|
def compute_capacities(node):
|
|
|
|
""" Test an operation through all MOs
|
|
|
|
|
|
|
|
:param operation: string which represent an (mo, mo) -> mo
|
|
|
|
:returns: { (motype, motype): True/False } when it's implemented
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> compute_capacities("+")
|
|
|
|
[['+', 'MOnumber', 'MOstr', 'MOFraction', 'MOstrPower', 'MOMonomial'], ['MOnumber', True, False, True, False, False], ['MOstr', False, False, False, False, False], ['MOFraction', True, False, True, False, False], ['MOstrPower', False, False, False, False, False], ['MOMonomial', False, False, False, False, False]]
|
|
|
|
|
|
|
|
"""
|
|
|
|
op = OPERATIONS[node]
|
|
|
|
lines = [[node] + [mo.__name__ for mo in MOS]]
|
|
|
|
for left_mo in MOS:
|
|
|
|
lines.append(
|
|
|
|
[left_mo.__name__] + \
|
|
|
|
[(left_mo, i) in op.funcs for i in MOS]
|
|
|
|
)
|
|
|
|
return lines
|
|
|
|
|
2018-10-01 06:54:15 +00:00
|
|
|
def describe_compute():
|
|
|
|
""" Explain which operation are handle by compue """
|
|
|
|
|
|
|
|
ans = "Implemented compute operations among MOs"
|
|
|
|
for op in OPERATIONS:
|
|
|
|
ans += "\n"
|
|
|
|
ans += tabulate(compute_capacities(op), tablefmt='grid')
|
|
|
|
return ans
|
2018-09-28 08:43:12 +00:00
|
|
|
|
2018-01-21 15:16:08 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|