From 25f791bd81500ffd07a2034b1716c136ae5fdf0d Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 12 Mar 2018 19:27:01 +0300 Subject: [PATCH] Minus MO --- mapytex/calculus/core/compute/exceptions.py | 3 + mapytex/calculus/core/compute/minus.py | 102 ++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 mapytex/calculus/core/compute/minus.py diff --git a/mapytex/calculus/core/compute/exceptions.py b/mapytex/calculus/core/compute/exceptions.py index b804e01..bfd24a5 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 MinusError(ComputeError): + pass + class MultiplyError(ComputeError): pass diff --git a/mapytex/calculus/core/compute/minus.py b/mapytex/calculus/core/compute/minus.py new file mode 100644 index 0000000..b815e4a --- /dev/null +++ b/mapytex/calculus/core/compute/minus.py @@ -0,0 +1,102 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 lafrite +# +# Distributed under terms of the MIT license. + +""" +Minus MO: take the opposit +""" + +from ..tree import Tree +from ..operator import OPERATORS +from ..MO.mo import MO, MOnumber, MOstr +from ..MO.fraction import MOFraction +from .exceptions import MinusError +from .arithmetic import lcm + + +def minus(left, right): + """ Perform the minusition of left and right + + :param left: left MO + :param right: right MO + :returns: Tree or MO + + >>> a = MOnumber(4) + >>> minus(None, a) + + >>> a = MOnumber(-4) + >>> minus(None, a) + + >>> b = MOnumber(0) + >>> minus(None, b) + + + """ + if not left is None: + raise MinusError(f"'-' is a 1 arity operator, left should be None (got {left})") + + return MINUSFUNCTIONS[type(right)](right) + +def monumber(right): + """ Minusing monumber + + :param right: right MOnumber + :returns: MONumber + + >>> a = MOnumber(4) + >>> monumber(a) + + + """ + return MO.factory(- right.value) + +def mofraction(right): + """ Minusing a mofraction + + :param right: a mofraction + :returns: Tree with the number converted into a mofraction + + >>> a = MOFraction(6, 5) + >>> print(mofraction(a)) + - + > None + > / + | > 6 + | > 5 + >>> a = MOFraction(6, 5, True) + >>> print(mofraction(a)) + / + > 6 + > 5 + """ + if right.negative: + return MOFraction(right._numerator, right._denominator) + + try: + if right._numerator < 0: + return MOFraction(-right._numerator, right._denominator) + except TypeError: + pass + try: + + if right._denominator < 0: + return MOFraction(right._numerator, -right._denominator) + except TypeError: + pass + + return MOFraction(right._numerator, right._denominator, True) + +# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018 +MINUSFUNCTIONS = { + MOnumber: monumber, + MOFraction: mofraction, + } + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del