Use multipledispatcher to define add for MO
This commit is contained in:
parent
0cef131c41
commit
9610ee1c7e
@ -16,58 +16,36 @@ from ..MO.fraction import MOFraction
|
|||||||
from .exceptions import AddError
|
from .exceptions import AddError
|
||||||
from .arithmetic import lcm
|
from .arithmetic import lcm
|
||||||
from .type_filter import args_are
|
from .type_filter import args_are
|
||||||
|
from multipledispatch import Dispatcher
|
||||||
|
|
||||||
|
add_doc = """ Adding MOs
|
||||||
def add(left, right):
|
|
||||||
""" Perform the addition of left and right
|
|
||||||
|
|
||||||
:param left: left MO
|
:param left: left MO
|
||||||
:param right: right MO
|
:param right: right MO
|
||||||
:returns: Tree or MO
|
:return: Tree or MO
|
||||||
|
|
||||||
>>> a = MOnumber(4)
|
"""
|
||||||
>>> b = MOnumber(6)
|
add = Dispatcher("add", doc=add_doc)
|
||||||
>>> add(a, b)
|
|
||||||
<MOnumber 10>
|
|
||||||
>>> b = MOnumber(0)
|
|
||||||
>>> add(a, b)
|
|
||||||
<MOnumber 4>
|
|
||||||
|
|
||||||
"""
|
@add.register(MOnumber, MOnumber)
|
||||||
if left.value == 0:
|
|
||||||
return right
|
|
||||||
elif right.value == 0:
|
|
||||||
return left
|
|
||||||
|
|
||||||
return ADDFUNCTIONS[(type(left), type(right))](left, right)
|
|
||||||
|
|
||||||
@args_are(MOnumber, MOnumber)
|
|
||||||
def monumber_monumber(left, right):
|
def monumber_monumber(left, right):
|
||||||
""" Adding 2 monumbers
|
""" Simply add MO value
|
||||||
|
|
||||||
:param left: left MOnumber
|
|
||||||
:param right: right MOnumber
|
|
||||||
:returns: MONumber
|
|
||||||
|
|
||||||
>>> a = MOnumber(4)
|
>>> a = MOnumber(4)
|
||||||
>>> b = MOnumber(6)
|
>>> b = MOnumber(6)
|
||||||
>>> monumber_monumber(a, b)
|
>>> add(a, b)
|
||||||
<MOnumber 10>
|
<MOnumber 10>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return MO.factory(left.value + right.value)
|
return MO.factory(left.value + right.value)
|
||||||
|
|
||||||
@args_are(MOnumber, MOFraction)
|
@add.register(MOnumber, MOFraction)
|
||||||
def monumber_mofraction(left, right):
|
def monumber_mofraction(left, right):
|
||||||
""" Adding a monumber and a mofraction
|
""" Return a tree with the MOnumber transformed into a MOFraction
|
||||||
|
|
||||||
:param left: a monumber
|
|
||||||
:param right: a mofraction
|
|
||||||
:returns: Tree with the number converted into a mofraction
|
|
||||||
|
|
||||||
>>> a = MOnumber(4)
|
>>> a = MOnumber(4)
|
||||||
>>> b = MOFraction(6, 5)
|
>>> b = MOFraction(6, 5)
|
||||||
>>> print(monumber_mofraction(a, b))
|
>>> print(add(a, b))
|
||||||
+
|
+
|
||||||
> /
|
> /
|
||||||
| > 4
|
| > 4
|
||||||
@ -79,17 +57,13 @@ def monumber_mofraction(left, right):
|
|||||||
left_fraction = MOFraction(left, MOnumber(1))
|
left_fraction = MOFraction(left, MOnumber(1))
|
||||||
return Tree("+", left_fraction, right)
|
return Tree("+", left_fraction, right)
|
||||||
|
|
||||||
@args_are(MOFraction, MOnumber)
|
@add.register(MOFraction, MOnumber)
|
||||||
def mofraction_monumber(left, right):
|
def mofraction_monumber(left, right):
|
||||||
""" Adding a monumber and a mofraction
|
""" Return a tree with the MOnumber transformed into a MOFraction
|
||||||
|
|
||||||
:param left: a mofraction
|
|
||||||
:param right: a monumber
|
|
||||||
:returns: Tree with the number converted into a mofraction
|
|
||||||
|
|
||||||
>>> a = MOFraction(6, 5)
|
>>> a = MOFraction(6, 5)
|
||||||
>>> b = MOnumber(4)
|
>>> b = MOnumber(4)
|
||||||
>>> print(mofraction_monumber(a, b))
|
>>> print(add(a, b))
|
||||||
+
|
+
|
||||||
> /
|
> /
|
||||||
| > 6
|
| > 6
|
||||||
@ -101,17 +75,15 @@ def mofraction_monumber(left, right):
|
|||||||
right_fraction = MOFraction(right, MOnumber(1))
|
right_fraction = MOFraction(right, MOnumber(1))
|
||||||
return Tree("+", left, right_fraction)
|
return Tree("+", left, right_fraction)
|
||||||
|
|
||||||
@args_are(MOFraction, MOFraction)
|
@add.register(MOFraction, MOFraction)
|
||||||
def mofraction_mofraction(left, right):
|
def mofraction_mofraction(left, right):
|
||||||
""" Adding two mofractions
|
""" 3 differents cases:
|
||||||
|
|
||||||
:param left: a mofraction
|
Fractions have same denomintor -> add numerator Tree
|
||||||
:param right: a mofraction
|
|
||||||
:returns: Tree
|
|
||||||
|
|
||||||
>>> a = MOFraction(1, 5)
|
>>> a = MOFraction(1, 5)
|
||||||
>>> b = MOFraction(4, 5)
|
>>> b = MOFraction(4, 5)
|
||||||
>>> print(mofraction_mofraction(a, b))
|
>>> print(add(a, b))
|
||||||
/
|
/
|
||||||
> +
|
> +
|
||||||
| > 1
|
| > 1
|
||||||
@ -137,6 +109,9 @@ def mofraction_mofraction(left, right):
|
|||||||
| | > None
|
| | > None
|
||||||
| | > 4
|
| | > 4
|
||||||
> 5
|
> 5
|
||||||
|
|
||||||
|
A denominator is a multiple of the other
|
||||||
|
|
||||||
>>> a = MOFraction(1, 2)
|
>>> a = MOFraction(1, 2)
|
||||||
>>> b = MOFraction(1, 4)
|
>>> b = MOFraction(1, 4)
|
||||||
>>> print(mofraction_mofraction(a, b))
|
>>> print(mofraction_mofraction(a, b))
|
||||||
@ -151,6 +126,9 @@ def mofraction_mofraction(left, right):
|
|||||||
> /
|
> /
|
||||||
| > 1
|
| > 1
|
||||||
| > 4
|
| > 4
|
||||||
|
|
||||||
|
Denominators are coprime
|
||||||
|
|
||||||
>>> a = MOFraction(1, 2)
|
>>> a = MOFraction(1, 2)
|
||||||
>>> b = MOFraction(1, 5)
|
>>> b = MOFraction(1, 5)
|
||||||
>>> print(mofraction_mofraction(a, b))
|
>>> print(mofraction_mofraction(a, b))
|
||||||
@ -194,14 +172,6 @@ def mofraction_mofraction(left, right):
|
|||||||
|
|
||||||
return Tree("+", left_frac, right_frac)
|
return Tree("+", left_frac, right_frac)
|
||||||
|
|
||||||
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
|
|
||||||
ADDFUNCTIONS = {
|
|
||||||
(MOnumber, MOnumber): monumber_monumber,
|
|
||||||
(MOnumber, MOFraction): monumber_mofraction,
|
|
||||||
(MOFraction, MOnumber): mofraction_monumber,
|
|
||||||
(MOFraction, MOFraction): mofraction_mofraction,
|
|
||||||
}
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||||
|
Loading…
Reference in New Issue
Block a user