Mapytex/mapytex/calculus/core/compute/minus.py

98 lines
1.8 KiB
Python

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
Minus MO: take the opposit
"""
from multipledispatch import Dispatcher
from ..MO.mo import MO, MOnumber
from ..MO.fraction import MOFraction
from .exceptions import MinusError
minus_doc = """ Opposite of a MO
:param left: None
:param right: right MO
:returns: Tree or MO
"""
minus = Dispatcher("minus", doc=minus_doc)
@minus.register(type(None), MOnumber)
def monumber(left, right):
"""
>>> a = MOnumber(4)
>>> minus(None, a)
<MOnumber - 4>
"""
return MO.factory(- right.value)
@minus.register(type(None), MOFraction)
def mofraction(left, right):
""" 4 differents cases
Either fraction , numerator or denominator is negative
>>> a = MOFraction(6, 5)
>>> print(minus(None, a))
-
> None
> /
| > 6
| > 5
The fraction is negative
>>> a = MOFraction(6, 5, True)
>>> print(minus(None, a))
/
> 6
> 5
Numerator is negative
>>> a = MOFraction(-6, 5)
>>> print(minus(None, a))
/
> 6
> 5
Denominators is negative
>>> a = MOFraction(6, -5)
>>> print(minus(None, 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)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del