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

147 lines
3.1 KiB
Python
Raw Permalink Normal View History

2018-03-12 16:27:01 +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.
"""
Minus MO: take the opposit
"""
2018-03-17 07:15:46 +00:00
from multipledispatch import Dispatcher
2018-03-12 16:27:01 +00:00
from .exceptions import MinusError
from ..MO import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial
from ..MO.polynomial import MOpolynomial
from ..tree import Tree
2018-03-12 16:27:01 +00:00
2018-03-17 07:15:46 +00:00
minus_doc = """ Opposite of a MO
2018-03-12 16:27:01 +00:00
2018-03-17 07:15:46 +00:00
:param left: None
:param right: right MO
:returns: Tree or MO
2018-03-12 16:27:01 +00:00
2018-03-17 07:15:46 +00:00
"""
2018-03-12 16:27:01 +00:00
2018-03-17 07:15:46 +00:00
minus = Dispatcher("minus", doc=minus_doc)
2018-03-12 16:27:01 +00:00
2019-05-14 04:55:56 +00:00
2018-03-17 07:15:46 +00:00
@minus.register(type(None), MOnumber)
def monumber(_, right):
2018-03-12 16:27:01 +00:00
"""
>>> a = MOnumber(4)
2018-03-17 07:15:46 +00:00
>>> minus(None, a)
<MOnumber -4>
2018-03-12 16:27:01 +00:00
"""
2019-05-14 04:55:56 +00:00
return MO.factory(-right.value)
2018-03-12 16:27:01 +00:00
2018-03-17 07:15:46 +00:00
@minus.register(type(None), MOFraction)
def mofraction(_, right):
2018-03-17 07:15:46 +00:00
""" 4 differents cases
2018-03-12 16:27:01 +00:00
2018-03-17 07:15:46 +00:00
Either fraction , numerator or denominator is negative
2018-03-12 16:27:01 +00:00
>>> a = MOFraction(6, 5)
2018-03-17 07:15:46 +00:00
>>> print(minus(None, a))
- 6 / 5
2018-03-17 07:15:46 +00:00
The fraction is negative
2018-03-12 16:27:01 +00:00
>>> a = MOFraction(6, 5, True)
2018-03-17 07:15:46 +00:00
>>> print(minus(None, a))
6 / 5
2018-03-17 07:15:46 +00:00
Numerator is negative
>>> a = MOFraction(-6, 5)
>>> print(minus(None, a))
6 / 5
2018-03-17 07:15:46 +00:00
Denominators is negative
>>> a = MOFraction(6, -5)
>>> print(minus(None, a))
6 / 5
2018-03-12 16:27:01 +00:00
"""
if right.negative:
return MOFraction(right._numerator, right._denominator)
try:
if right._numerator.value < 0:
return MOFraction(-right._numerator.value, right._denominator)
2018-03-12 16:27:01 +00:00
except TypeError:
pass
try:
if right._denominator.value < 0:
return MOFraction(right._numerator, -right._denominator.value)
2018-03-12 16:27:01 +00:00
except TypeError:
pass
return MOFraction(right._numerator, right._denominator, True)
2019-05-14 04:55:56 +00:00
@minus.register(type(None), MOstr)
def mostr(_, right):
""" Opposite of 'x' is '-x'
:example:
>>> x = MOstr("x")
>>> print(minus(None, x))
- x
"""
return MOMonomial(-1, right)
2019-05-14 04:55:56 +00:00
@minus.register(type(None), MOstrPower)
def mostrpower(_, right):
""" Opposite of 'x^n' is '-x^n'
:example:
>>> x2 = MOstrPower("x", 2)
>>> print(minus(None, x2))
- x^2
"""
return MOMonomial(-1, right.variable, right.power)
2019-05-14 04:55:56 +00:00
@minus.register(type(None), MOMonomial)
def momonomial(_, right):
""" Opposite of 'ax^n' is '-ax^n'
:example:
>>> tx2 = MOMonomial(3, "x", 2)
>>> print(minus(None, tx2))
- 3x^2
"""
try:
return MOMonomial(-right.coefficient.value, right.variable, right.power)
except TypeError:
coef = Tree("-", None, right.coefficient)
return Tree("*", coef, right.strpower)
2019-05-14 04:55:56 +00:00
@minus.register(type(None), MOpolynomial)
def mopolynomial(_, right):
""" Opposite of a polynomial
:example:
>>> P = MOpolynomial('x', [1, -2, 3])
>>> print(minus(None, P))
- 3x^2 + 2x - 1
"""
neg_coefs = {p: -c.value for (p, c) in right.coefficients.items()}
return MOpolynomial(right.variable, neg_coefs)
2019-05-14 04:55:56 +00:00
2018-03-12 16:27:01 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del