2018-03-13 14:12: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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Divide MO
|
|
|
|
"""
|
|
|
|
|
2018-09-24 15:21:50 +00:00
|
|
|
from decimal import Decimal
|
2018-03-17 07:38:59 +00:00
|
|
|
from multipledispatch import Dispatcher
|
2018-03-13 14:12:41 +00:00
|
|
|
from ..tree import Tree
|
2018-12-21 10:26:37 +00:00
|
|
|
from ..MO import MO, MOnumber
|
2018-03-13 14:12:41 +00:00
|
|
|
from ..MO.fraction import MOFraction
|
|
|
|
from .exceptions import DivideError
|
2018-03-17 08:55:32 +00:00
|
|
|
from .filters import special_case
|
2018-03-13 14:12:41 +00:00
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
divide_doc = """ Dividing MOs
|
2018-03-13 14:12:41 +00:00
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
:param left: left MO
|
|
|
|
:param right: right MO
|
|
|
|
:returns: Tree or MO
|
2018-03-13 14:12:41 +00:00
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
"""
|
2018-03-13 14:12:41 +00:00
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
divide = Dispatcher("divide", doc=divide_doc)
|
2018-03-13 14:12:41 +00:00
|
|
|
|
2018-03-17 08:55:32 +00:00
|
|
|
def divide_filter(left, right):
|
|
|
|
""" Automatic divide on MO
|
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(1)
|
|
|
|
>>> divide(a, b)
|
|
|
|
<MOnumber 4>
|
|
|
|
>>> a = MOnumber(0)
|
|
|
|
>>> b = MOnumber(1)
|
|
|
|
>>> divide(a, b)
|
|
|
|
<MOnumber 0>
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(0)
|
|
|
|
>>> divide(a, b)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
mapytex.calculus.core.compute.exceptions.DivideError: Division by zero
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if left == 0:
|
|
|
|
return left
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
if right == 1:
|
|
|
|
return left
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
if right == 0:
|
|
|
|
raise DivideError("Division by zero")
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
@divide.register(MOnumber, MOnumber)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(divide_filter)
|
2018-03-13 14:12:41 +00:00
|
|
|
def monumber_monumber(left, right):
|
2018-03-17 07:38:59 +00:00
|
|
|
""" Divide 2 monumbers and return a MOFraction
|
2018-03-13 14:12:41 +00:00
|
|
|
|
2018-09-24 15:21:50 +00:00
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(6.2)
|
|
|
|
>>> monumber_monumber(a, b)
|
2018-10-05 09:18:09 +00:00
|
|
|
<MOnumber 0.6451612903225806266768278939>
|
2018-03-13 14:12:41 +00:00
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(6)
|
|
|
|
>>> monumber_monumber(a, b)
|
2018-09-24 15:21:50 +00:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
NotImplementedError: Can't divide 2 int. Need to create a Fraction instead
|
2018-03-13 14:12:41 +00:00
|
|
|
"""
|
2018-09-24 15:21:50 +00:00
|
|
|
if type(left.value) in [float, Decimal] or \
|
|
|
|
type(right.value) in [float, Decimal]:
|
2018-11-13 08:14:50 +00:00
|
|
|
return MO.factory(left.value / right.value)
|
2018-09-24 15:21:50 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError("Can't divide 2 int. Need to create a Fraction instead")
|
2018-03-13 14:12:41 +00:00
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
@divide.register(MOnumber, MOFraction)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(divide_filter)
|
2018-03-13 14:12:41 +00:00
|
|
|
def monumber_mofraction(left, right):
|
2018-03-17 07:38:59 +00:00
|
|
|
""" Divide a monumber and a mofraction by inverting MOFraction
|
2018-03-13 14:12:41 +00:00
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOFraction(6, 5)
|
|
|
|
>>> print(monumber_mofraction(a, b))
|
|
|
|
*
|
|
|
|
> 4
|
|
|
|
> /
|
|
|
|
| > 5
|
|
|
|
| > 6
|
|
|
|
>>> b = MOFraction(6, 5, True)
|
|
|
|
>>> print(monumber_mofraction(a, b))
|
|
|
|
*
|
|
|
|
> 4
|
|
|
|
> -
|
|
|
|
| > None
|
|
|
|
| > /
|
|
|
|
| | > 5
|
|
|
|
| | > 6
|
|
|
|
"""
|
|
|
|
return Tree("*", left, right.inverse())
|
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
@divide.register(MOFraction, MOnumber)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(divide_filter)
|
2018-03-13 14:12:41 +00:00
|
|
|
def mofraction_monumber(left, right):
|
2018-03-17 07:38:59 +00:00
|
|
|
""" Divide a monumber and a mofraction by inverting MOnumber
|
2018-03-13 14:12:41 +00:00
|
|
|
|
|
|
|
>>> a = MOFraction(6, 5)
|
|
|
|
>>> b = MOnumber(4)
|
|
|
|
>>> print(mofraction_monumber(a, b))
|
|
|
|
*
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
|
|
|
> /
|
|
|
|
| > 1
|
|
|
|
| > 4
|
|
|
|
"""
|
|
|
|
|
2018-10-05 09:18:09 +00:00
|
|
|
right_fraction = MOFraction(MOnumber(1), right)
|
2018-03-13 14:12:41 +00:00
|
|
|
return Tree("*", left, right_fraction)
|
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
@divide.register(MOFraction, MOFraction)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(divide_filter)
|
2018-03-13 14:12:41 +00:00
|
|
|
def mofraction_mofraction(left, right):
|
2018-03-17 07:38:59 +00:00
|
|
|
""" Divide two mofractions by inverting right MOFraction
|
2018-03-13 14:12:41 +00:00
|
|
|
|
|
|
|
>>> a = MOFraction(1, 5)
|
|
|
|
>>> b = MOFraction(4, 5)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
*
|
|
|
|
> /
|
|
|
|
| > 1
|
|
|
|
| > 5
|
|
|
|
> /
|
|
|
|
| > 5
|
|
|
|
| > 4
|
|
|
|
|
|
|
|
"""
|
|
|
|
return Tree("*", left, right.inverse())
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|