From 5f5fcc5a790f003e77f6d7e66134ed1c81d8ae32 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 11 Mar 2018 20:05:17 +0300 Subject: [PATCH] Multiply with MOnumber and MOFraction --- mapytex/calculus/core/compute/exceptions.py | 3 + mapytex/calculus/core/compute/multiply.py | 135 ++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 mapytex/calculus/core/compute/multiply.py diff --git a/mapytex/calculus/core/compute/exceptions.py b/mapytex/calculus/core/compute/exceptions.py index a0444f7..b804e01 100644 --- a/mapytex/calculus/core/compute/exceptions.py +++ b/mapytex/calculus/core/compute/exceptions.py @@ -16,6 +16,9 @@ class ComputeError(Exception): class AddError(ComputeError): pass +class MultiplyError(ComputeError): + pass + # ----------------------------- # Reglages pour 'vim' diff --git a/mapytex/calculus/core/compute/multiply.py b/mapytex/calculus/core/compute/multiply.py new file mode 100644 index 0000000..e19e9dd --- /dev/null +++ b/mapytex/calculus/core/compute/multiply.py @@ -0,0 +1,135 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 lafrite +# +# Distributed under terms of the MIT license. + +""" +Multiply MO +""" + +from ..tree import Tree +from ..operator import OPERATORS +from ..MO.mo import MO, MOnumber, MOstr +from ..MO.fraction import MOFraction +from .exceptions import MultiplyError + + +def multiply(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) + >>> multiply(a, b) + + >>> b = MOnumber(0) + >>> multiply(a, b) + + + """ + if left.value == 0 or right.value == 0: + return MOnumber(0) + elif left.value == 1: + return right + elif right.value == 1: + return left + + return MULFUNCTIONS[(type(left), type(right))](left, right) + +def monumber_monumber(left, right): + """ Multiply 2 monumbers + + :param left: left MOnumber + :param right: right MOnumber + :returns: MONumber + + >>> a = MOnumber(4) + >>> b = MOnumber(6) + >>> monumber_monumber(a, b) + + + """ + return MO.factory(left.value * right.value) + +def monumber_mofraction(left, right): + """ Multiply a monumber and a mofraction + + :param left: a monumber + :param right: a mofraction + :returns: Tree with the multiplication on the numerator + + >>> a = MOnumber(4) + >>> b = MOFraction(6, 5) + >>> print(monumber_mofraction(a, b)) + / + > * + | > 4 + | > 6 + > 5 + >>> b = MOFraction(6, 5, True) + >>> print(monumber_mofraction(a, b)) + - + > None + > / + | > * + | | > 4 + | | > 6 + | > 5 + + """ + if not isinstance(left, MOnumber) or not isinstance(right, MOFraction): + raise MultiplyError(f"Wrong type for left (got {left.__class__.__name__}) \ + or right (got {right.__class__.__name__})") + + num = Tree("*", left, right._numerator) + frac = Tree("/", num, right._denominator) + if right.negative: + return Tree("-", None, frac) + + return frac + +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 + | > 4 + > 5 + """ + + if not isinstance(left, MOFraction) or not isinstance(right, MOnumber): + raise MultiplyError(f"Wrong type for left (got {left.__class__.__name__})" + f"or right (got {right.__class__.__name__})") + + num = Tree("*", left._numerator, right) + frac = Tree("/", num, left._denominator) + if left.negative: + return Tree("-", None, frac) + + return frac + +# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018 +MULFUNCTIONS = { + (MOnumber, MOnumber): monumber_monumber, + (MOnumber, MOFraction): monumber_mofraction, + (MOFraction, MOnumber): mofraction_monumber, + } + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del