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

201 lines
4.9 KiB
Python
Raw Normal View History

2018-03-11 17:05:17 +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.
"""
Multiply MO
"""
from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr
2018-03-11 17:05:17 +00:00
from ..MO.fraction import MOFraction
from ..MO.monomial import MOMonomial
2018-03-11 17:05:17 +00:00
from .exceptions import MultiplyError
2018-03-15 13:37:48 +00:00
from .type_filter import args_are
2018-03-11 17:05:17 +00:00
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)
<MOnumber 24>
>>> b = MOnumber(0)
>>> multiply(a, b)
<MOnumber 0>
"""
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)
2018-03-15 13:37:48 +00:00
@args_are(MOnumber, MOnumber)
2018-03-11 17:05:17 +00:00
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)
<MOnumber 24>
"""
return MO.factory(left.value * right.value)
2018-03-15 13:37:48 +00:00
@args_are(MOnumber, MOFraction)
2018-03-11 17:05:17 +00:00
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))
2018-03-12 04:34:26 +00:00
/
> *
| > 4
| > -
| | > None
2018-03-11 17:05:17 +00:00
| | > 6
2018-03-12 04:34:26 +00:00
> 5
2018-03-11 17:05:17 +00:00
"""
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__})")
2018-03-12 04:34:26 +00:00
num = Tree("*", left, right.numerator)
return Tree("/", num, right._denominator)
2018-03-11 17:05:17 +00:00
2018-03-15 13:37:48 +00:00
@args_are(MOFraction, MOnumber)
2018-03-11 17:05:17 +00:00
def mofraction_monumber(left, right):
""" Multiply a monumber and a mofraction
2018-03-11 17:05:17 +00:00
: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__})")
2018-03-12 04:34:26 +00:00
num = Tree("*", left.numerator, right)
return Tree("/", num, left._denominator)
2018-03-11 17:05:17 +00:00
2018-03-15 13:37:48 +00:00
@args_are(MOFraction, MOFraction)
def mofraction_mofraction(left, right):
""" Multiply two mofractions
:param left: a mofraction
:param right: a mofraction
:returns: Tree with a new calculus
>>> a = MOFraction(1, 5)
>>> b = MOFraction(4, 5)
>>> print(mofraction_mofraction(a, b))
/
> *
| > 1
| > 4
> *
| > 5
| > 5
"""
if not isinstance(left, MOFraction) or not isinstance(right, MOFraction):
raise AddError(f"Wrong type for left (got {left.__class__.__name__})"
f"or right (got {right.__class__.__name__})")
num = Tree("*", left.numerator, right.numerator)
denom = Tree("*", left.denominator, right.denominator)
return Tree("/", num, denom)
@args_are((MOnumber, MOFraction), MOstr)
def moscalar_mostr(left, right):
""" Multiply a scalar with a letter
>>> a = MOnumber(2)
>>> b = MOstr('x')
>>> moscalar_mostr(a, b)
<MOMonomial 2x>
>>> a = MOFraction(1, 5)
>>> moscalar_mostr(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(left, right)
@args_are(MOstr, (MOnumber, MOFraction))
def mostr_moscalar(left, right):
""" Multiply a scalar with a letter
>>> a = MOstr('x')
>>> b = MOnumber(2)
>>> mostr_moscalar(a, b)
<MOMonomial 2x>
>>> b = MOFraction(1, 5)
>>> mostr_moscalar(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(right, left)
@args_are((MOnumber, MOFraction), MOMonomial)
def moscalar_monomonial(left, right):
""" Multiply a scalar with a monomial
>>> a = MOnumber(4)
>>> b = MOMonomial(5, 'x', 3)
# >>> print(moscalar_monomonial(a, b))
"""
coefficient = Tree('*', left, right._coefficient)
return Tree('*', coefficient, MOMonomial(1, right._variable, right._power))
2018-03-11 17:05:17 +00:00
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
MULFUNCTIONS = {
2018-03-13 11:43:48 +00:00
(MOnumber, MOnumber): monumber_monumber,
(MOnumber, MOFraction): monumber_mofraction,
(MOFraction, MOnumber): mofraction_monumber,
(MOFraction, MOFraction): mofraction_mofraction,
2018-03-13 11:43:48 +00:00
}
2018-03-11 17:05:17 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del