2018-03-11 15:34:41 +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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Adding MO
|
|
|
|
"""
|
|
|
|
|
|
|
|
from ..tree import Tree
|
2018-03-13 11:43:48 +00:00
|
|
|
from ..MO.mo import MO, MOnumber
|
2018-03-11 15:34:41 +00:00
|
|
|
from ..MO.fraction import MOFraction
|
|
|
|
from .exceptions import AddError
|
2018-03-12 04:34:26 +00:00
|
|
|
from .arithmetic import lcm
|
2018-03-15 13:37:48 +00:00
|
|
|
from .type_filter import args_are
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add(left, right):
|
|
|
|
""" Perform the addition of left and right
|
|
|
|
|
|
|
|
:param left: left MO
|
|
|
|
:param right: right MO
|
|
|
|
:returns: Tree or MO
|
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(6)
|
|
|
|
>>> add(a, b)
|
|
|
|
<MOnumber 10>
|
|
|
|
>>> b = MOnumber(0)
|
|
|
|
>>> add(a, b)
|
|
|
|
<MOnumber 4>
|
|
|
|
|
|
|
|
"""
|
|
|
|
if left.value == 0:
|
|
|
|
return right
|
|
|
|
elif right.value == 0:
|
|
|
|
return left
|
|
|
|
|
|
|
|
return ADDFUNCTIONS[(type(left), type(right))](left, right)
|
|
|
|
|
2018-03-15 13:37:48 +00:00
|
|
|
@args_are(MOnumber, MOnumber)
|
2018-03-11 15:34:41 +00:00
|
|
|
def monumber_monumber(left, right):
|
|
|
|
""" Adding 2 monumbers
|
|
|
|
|
|
|
|
:param left: left MOnumber
|
|
|
|
:param right: right MOnumber
|
|
|
|
:returns: MONumber
|
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(6)
|
|
|
|
>>> monumber_monumber(a, b)
|
|
|
|
<MOnumber 10>
|
|
|
|
|
|
|
|
"""
|
|
|
|
return MO.factory(left.value + right.value)
|
|
|
|
|
2018-03-15 13:37:48 +00:00
|
|
|
@args_are(MOnumber, MOFraction)
|
2018-03-11 15:34:41 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOFraction(6, 5)
|
|
|
|
>>> print(monumber_mofraction(a, b))
|
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > 4
|
|
|
|
| > 1
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
|
|
|
"""
|
|
|
|
left_fraction = MOFraction(left, MOnumber(1))
|
|
|
|
return Tree("+", left_fraction, right)
|
|
|
|
|
2018-03-15 13:37:48 +00:00
|
|
|
@args_are(MOFraction, MOnumber)
|
2018-03-11 15:34:41 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
>>> a = MOFraction(6, 5)
|
|
|
|
>>> b = MOnumber(4)
|
|
|
|
>>> print(mofraction_monumber(a, b))
|
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
|
|
|
> /
|
|
|
|
| > 4
|
|
|
|
| > 1
|
|
|
|
"""
|
|
|
|
right_fraction = MOFraction(right, MOnumber(1))
|
|
|
|
return Tree("+", left, right_fraction)
|
|
|
|
|
2018-03-15 13:37:48 +00:00
|
|
|
@args_are(MOFraction, MOFraction)
|
2018-03-12 04:34:26 +00:00
|
|
|
def mofraction_mofraction(left, right):
|
|
|
|
""" Adding two mofractions
|
|
|
|
|
|
|
|
:param left: a mofraction
|
|
|
|
:param right: a mofraction
|
|
|
|
:returns: Tree
|
|
|
|
|
|
|
|
>>> a = MOFraction(1, 5)
|
|
|
|
>>> b = MOFraction(4, 5)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
/
|
|
|
|
> +
|
|
|
|
| > 1
|
|
|
|
| > 4
|
|
|
|
> 5
|
|
|
|
>>> a = MOFraction(1, 5, True)
|
|
|
|
>>> b = MOFraction(4, 5)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
/
|
|
|
|
> +
|
|
|
|
| > -
|
|
|
|
| | > None
|
|
|
|
| | > 1
|
|
|
|
| > 4
|
|
|
|
> 5
|
|
|
|
>>> a = MOFraction(1, 5)
|
|
|
|
>>> b = MOFraction(4, 5, True)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
/
|
|
|
|
> +
|
|
|
|
| > 1
|
|
|
|
| > -
|
|
|
|
| | > None
|
|
|
|
| | > 4
|
|
|
|
> 5
|
|
|
|
>>> a = MOFraction(1, 2)
|
|
|
|
>>> b = MOFraction(1, 4)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > *
|
|
|
|
| | > 1
|
|
|
|
| | > 2
|
|
|
|
| > *
|
|
|
|
| | > 2
|
|
|
|
| | > 2
|
|
|
|
> /
|
|
|
|
| > 1
|
|
|
|
| > 4
|
|
|
|
>>> a = MOFraction(1, 2)
|
|
|
|
>>> b = MOFraction(1, 5)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > *
|
|
|
|
| | > 1
|
|
|
|
| | > 5
|
|
|
|
| > *
|
|
|
|
| | > 2
|
|
|
|
| | > 5
|
|
|
|
> /
|
|
|
|
| > *
|
|
|
|
| | > 1
|
|
|
|
| | > 2
|
|
|
|
| > *
|
|
|
|
| | > 5
|
|
|
|
| | > 2
|
|
|
|
"""
|
|
|
|
if left.denominator == right.denominator:
|
|
|
|
num = Tree("+", left.numerator, right.numerator)
|
|
|
|
return Tree("/", num, left.denominator)
|
|
|
|
|
|
|
|
denom_lcm = lcm(left.denominator, right.denominator)
|
|
|
|
|
|
|
|
if left.denominator == denom_lcm:
|
|
|
|
left_frac = left
|
|
|
|
else:
|
|
|
|
multiply_by = denom_lcm // left.denominator
|
|
|
|
left_num = Tree("*", left.numerator, MO.factory(multiply_by))
|
|
|
|
left_denom = Tree("*", left.denominator, MO.factory(multiply_by))
|
|
|
|
left_frac = Tree("/", left_num, left_denom)
|
|
|
|
|
|
|
|
if right.denominator == denom_lcm:
|
|
|
|
right_frac = right
|
|
|
|
else:
|
|
|
|
multiply_by = denom_lcm // right.denominator
|
|
|
|
right_num = Tree("*", right.numerator, MO.factory(multiply_by))
|
|
|
|
right_denom = Tree("*", right.denominator, MO.factory(multiply_by))
|
|
|
|
right_frac = Tree("/", right_num, right_denom)
|
|
|
|
|
|
|
|
return Tree("+", left_frac, right_frac)
|
|
|
|
|
2018-03-11 15:34:41 +00:00
|
|
|
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
|
|
|
|
ADDFUNCTIONS = {
|
2018-03-13 11:43:48 +00:00
|
|
|
(MOnumber, MOnumber): monumber_monumber,
|
|
|
|
(MOnumber, MOFraction): monumber_mofraction,
|
|
|
|
(MOFraction, MOnumber): mofraction_monumber,
|
|
|
|
(MOFraction, MOFraction): mofraction_mofraction,
|
|
|
|
}
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|