#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2017 lafrite # # Distributed under terms of the MIT license. """ Divide MO """ from multipledispatch import Dispatcher from ..tree import Tree from ..MO.mo import MO, MOnumber from ..MO.fraction import MOFraction from .exceptions import DivideError from .type_filter import args_are divide_doc = """ Dividing MOs :param left: left MO :param right: right MO :returns: Tree or MO """ divide = Dispatcher("divide", doc=divide_doc) @divide.register(MOnumber, MOnumber) def monumber_monumber(left, right): """ Divide 2 monumbers and return a MOFraction >>> a = MOnumber(4) >>> b = MOnumber(6) >>> monumber_monumber(a, b) """ return MOFraction(left, right) @divide.register(MOnumber, MOFraction) def monumber_mofraction(left, right): """ Divide a monumber and a mofraction by inverting MOFraction >>> 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()) @divide.register(MOFraction, MOnumber) def mofraction_monumber(left, right): """ Divide a monumber and a mofraction by inverting MOnumber >>> a = MOFraction(6, 5) >>> b = MOnumber(4) >>> print(mofraction_monumber(a, b)) * > / | > 6 | > 5 > / | > 1 | > 4 """ right_fraction = MOFraction(1, right) return Tree("*", left, right_fraction) @divide.register(MOFraction, MOFraction) def mofraction_mofraction(left, right): """ Divide two mofractions by inverting right MOFraction >>> a = MOFraction(1, 5) >>> b = MOFraction(4, 5) >>> print(mofraction_mofraction(a, b)) * > / | > 1 | > 5 > / | > 5 | > 4 """ return Tree("*", left, right.inverse()) #def divide(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) # >>> divide(a, b) # # >>> b = MOnumber(0) # >>> divide(a, b) # Traceback (most recent call last): # ... # mapytex.calculus.core.compute.exceptions.DivideError: Division by zero # # """ # try: # right.value # except AttributeError: # pass # else: # if right.value == 0: # raise DivideError("Division by zero") # # if right.value == 1: # return left # # return MULFUNCTIONS[(type(left), type(right))](left, right) # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: # cursor: 16 del