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 .arithmetic import lcm
|
||||
from .type_filter import args_are
|
||||
from multipledispatch import Dispatcher
|
||||
|
||||
|
||||
def add(left, right):
|
||||
""" Perform the addition of left and right
|
||||
add_doc = """ Adding MOs
|
||||
|
||||
:param left: left MO
|
||||
:param right: right MO
|
||||
:returns: Tree or MO
|
||||
:return: Tree or MO
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> b = MOnumber(6)
|
||||
>>> add(a, b)
|
||||
<MOnumber 10>
|
||||
>>> b = MOnumber(0)
|
||||
>>> add(a, b)
|
||||
<MOnumber 4>
|
||||
"""
|
||||
add = Dispatcher("add", doc=add_doc)
|
||||
|
||||
"""
|
||||
if left.value == 0:
|
||||
return right
|
||||
elif right.value == 0:
|
||||
return left
|
||||
|
||||
return ADDFUNCTIONS[(type(left), type(right))](left, right)
|
||||
|
||||
@args_are(MOnumber, MOnumber)
|
||||
@add.register(MOnumber, MOnumber)
|
||||
def monumber_monumber(left, right):
|
||||
""" Adding 2 monumbers
|
||||
|
||||
:param left: left MOnumber
|
||||
:param right: right MOnumber
|
||||
:returns: MONumber
|
||||
""" Simply add MO value
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> b = MOnumber(6)
|
||||
>>> monumber_monumber(a, b)
|
||||
>>> add(a, b)
|
||||
<MOnumber 10>
|
||||
|
||||
"""
|
||||
return MO.factory(left.value + right.value)
|
||||
|
||||
@args_are(MOnumber, MOFraction)
|
||||
@add.register(MOnumber, MOFraction)
|
||||
def monumber_mofraction(left, right):
|
||||
""" Adding a monumber and a mofraction
|
||||
|
||||
:param left: a monumber
|
||||
:param right: a mofraction
|
||||
:returns: Tree with the number converted into a mofraction
|
||||
""" Return a tree with the MOnumber transformed into a MOFraction
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> b = MOFraction(6, 5)
|
||||
>>> print(monumber_mofraction(a, b))
|
||||
>>> print(add(a, b))
|
||||
+
|
||||
> /
|
||||
| > 4
|
||||
@ -79,17 +57,13 @@ def monumber_mofraction(left, right):
|
||||
left_fraction = MOFraction(left, MOnumber(1))
|
||||
return Tree("+", left_fraction, right)
|
||||
|
||||
@args_are(MOFraction, MOnumber)
|
||||
@add.register(MOFraction, MOnumber)
|
||||
def mofraction_monumber(left, right):
|
||||
""" Adding a monumber and a mofraction
|
||||
|
||||
:param left: a mofraction
|
||||
:param right: a monumber
|
||||
:returns: Tree with the number converted into a mofraction
|
||||
""" Return a tree with the MOnumber transformed into a MOFraction
|
||||
|
||||
>>> a = MOFraction(6, 5)
|
||||
>>> b = MOnumber(4)
|
||||
>>> print(mofraction_monumber(a, b))
|
||||
>>> print(add(a, b))
|
||||
+
|
||||
> /
|
||||
| > 6
|
||||
@ -101,17 +75,15 @@ def mofraction_monumber(left, right):
|
||||
right_fraction = MOFraction(right, MOnumber(1))
|
||||
return Tree("+", left, right_fraction)
|
||||
|
||||
@args_are(MOFraction, MOFraction)
|
||||
@add.register(MOFraction, MOFraction)
|
||||
def mofraction_mofraction(left, right):
|
||||
""" Adding two mofractions
|
||||
""" 3 differents cases:
|
||||
|
||||
:param left: a mofraction
|
||||
:param right: a mofraction
|
||||
:returns: Tree
|
||||
Fractions have same denomintor -> add numerator Tree
|
||||
|
||||
>>> a = MOFraction(1, 5)
|
||||
>>> b = MOFraction(4, 5)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
>>> print(add(a, b))
|
||||
/
|
||||
> +
|
||||
| > 1
|
||||
@ -137,6 +109,9 @@ def mofraction_mofraction(left, right):
|
||||
| | > None
|
||||
| | > 4
|
||||
> 5
|
||||
|
||||
A denominator is a multiple of the other
|
||||
|
||||
>>> a = MOFraction(1, 2)
|
||||
>>> b = MOFraction(1, 4)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
@ -151,6 +126,9 @@ def mofraction_mofraction(left, right):
|
||||
> /
|
||||
| > 1
|
||||
| > 4
|
||||
|
||||
Denominators are coprime
|
||||
|
||||
>>> a = MOFraction(1, 2)
|
||||
>>> b = MOFraction(1, 5)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
@ -194,14 +172,6 @@ def mofraction_mofraction(left, right):
|
||||
|
||||
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'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
Loading…
Reference in New Issue
Block a user