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
|
|
|
|
"""
|
|
|
|
|
2018-03-17 08:55:32 +00:00
|
|
|
from functools import wraps
|
2018-03-17 07:38:59 +00:00
|
|
|
from multipledispatch import Dispatcher
|
2018-03-11 15:34:41 +00:00
|
|
|
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-17 08:55:32 +00:00
|
|
|
from .filters import special_case
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
add_doc = """ Adding MOs
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
:param left: left MO
|
|
|
|
:param right: right MO
|
|
|
|
:return: Tree or MO
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
"""
|
2018-03-17 08:55:32 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
add = Dispatcher("add", doc=add_doc)
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 08:55:32 +00:00
|
|
|
def add_filter(left, right):
|
|
|
|
""" Automatic add on MO
|
|
|
|
|
|
|
|
:param left: MO
|
|
|
|
:param right: MO
|
|
|
|
:returns: MO if it is a special case, nothing other wise
|
|
|
|
|
|
|
|
>>> a = MOnumber(0)
|
|
|
|
>>> b = MOFraction(1, 2)
|
|
|
|
>>> add(a, b)
|
|
|
|
<MOFraction 1 / 2>
|
|
|
|
>>> add(b, a)
|
|
|
|
<MOFraction 1 / 2>
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if left == 0:
|
|
|
|
return right
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
if right == 0:
|
|
|
|
return left
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOnumber, MOnumber)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-11 15:34:41 +00:00
|
|
|
def monumber_monumber(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" Simply add MO value
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(6)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> add(a, b)
|
2018-03-11 15:34:41 +00:00
|
|
|
<MOnumber 10>
|
|
|
|
|
|
|
|
"""
|
|
|
|
return MO.factory(left.value + right.value)
|
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOnumber, MOFraction)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-11 15:34:41 +00:00
|
|
|
def monumber_mofraction(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" Return a tree with the MOnumber transformed into a MOFraction
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOFraction(6, 5)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> print(add(a, b))
|
2018-03-11 15:34:41 +00:00
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > 4
|
|
|
|
| > 1
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
|
|
|
"""
|
|
|
|
left_fraction = MOFraction(left, MOnumber(1))
|
|
|
|
return Tree("+", left_fraction, right)
|
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOFraction, MOnumber)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-11 15:34:41 +00:00
|
|
|
def mofraction_monumber(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" Return a tree with the MOnumber transformed into a MOFraction
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
>>> a = MOFraction(6, 5)
|
|
|
|
>>> b = MOnumber(4)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> print(add(a, b))
|
2018-03-11 15:34:41 +00:00
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
|
|
|
> /
|
|
|
|
| > 4
|
|
|
|
| > 1
|
|
|
|
"""
|
|
|
|
right_fraction = MOFraction(right, MOnumber(1))
|
|
|
|
return Tree("+", left, right_fraction)
|
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOFraction, MOFraction)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-12 04:34:26 +00:00
|
|
|
def mofraction_mofraction(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" 3 differents cases:
|
2018-03-12 04:34:26 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
Fractions have same denomintor -> add numerator Tree
|
2018-03-12 04:34:26 +00:00
|
|
|
|
|
|
|
>>> a = MOFraction(1, 5)
|
|
|
|
>>> b = MOFraction(4, 5)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> print(add(a, b))
|
2018-03-12 04:34:26 +00:00
|
|
|
/
|
|
|
|
> +
|
|
|
|
| > 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
|
2018-03-17 07:04:53 +00:00
|
|
|
|
|
|
|
A denominator is a multiple of the other
|
|
|
|
|
2018-03-12 04:34:26 +00:00
|
|
|
>>> a = MOFraction(1, 2)
|
|
|
|
>>> b = MOFraction(1, 4)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > *
|
|
|
|
| | > 1
|
|
|
|
| | > 2
|
|
|
|
| > *
|
|
|
|
| | > 2
|
|
|
|
| | > 2
|
|
|
|
> /
|
|
|
|
| > 1
|
|
|
|
| > 4
|
2018-03-17 07:04:53 +00:00
|
|
|
|
|
|
|
Denominators are coprime
|
|
|
|
|
2018-03-12 04:34:26 +00:00
|
|
|
>>> 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
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|