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

195 lines
4.4 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
"""
2018-03-17 07:28:43 +00:00
from multipledispatch import Dispatcher
2018-03-11 17:05:17 +00:00
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-17 08:55:32 +00:00
from .filters import special_case
2018-03-11 17:05:17 +00:00
2018-03-17 07:28:43 +00:00
multiply_doc = """ Multiply MOs
2018-03-11 17:05:17 +00:00
2018-03-17 07:28:43 +00:00
:param left: left MO
:param right: right MO
:returns: Tree or MO
2018-03-11 17:05:17 +00:00
2018-03-17 07:28:43 +00:00
"""
2018-03-11 17:05:17 +00:00
2018-03-17 07:28:43 +00:00
multiply = Dispatcher("multiply", doc=multiply_doc)
2018-03-11 17:05:17 +00:00
2018-03-17 08:55:32 +00:00
def multiply_filter(left, right):
""" Automatic multiply on MO
:param left: MO
:param right: MO
:returns: MO if it is a special case, nothing other wise
>>> a = MOnumber(1)
>>> b = MOFraction(1, 2)
>>> multiply(a, b)
<MOFraction 1 / 2>
>>> multiply(b, a)
<MOFraction 1 / 2>
>>> a = MOnumber(0)
>>> b = MOFraction(1, 2)
>>> multiply(a, b)
<MOnumber 0>
"""
try:
if left == 0:
return left
except TypeError:
pass
try:
if right == 0:
return right
except TypeError:
pass
try:
if left == 1:
return right
except TypeError:
pass
try:
if right == 1:
return left
except TypeError:
pass
2018-03-17 07:28:43 +00:00
@multiply.register(MOnumber, MOnumber)
2018-03-17 08:55:32 +00:00
@special_case(multiply_filter)
2018-03-11 17:05:17 +00:00
def monumber_monumber(left, right):
2018-03-17 07:28:43 +00:00
""" Simply multiply values
2018-03-11 17:05:17 +00:00
>>> a = MOnumber(4)
>>> b = MOnumber(6)
2018-03-17 07:28:43 +00:00
>>> multiply(a, b)
2018-03-11 17:05:17 +00:00
<MOnumber 24>
"""
return MO.factory(left.value * right.value)
2018-03-17 07:28:43 +00:00
@multiply.register(MOnumber, MOFraction)
2018-03-17 08:55:32 +00:00
@special_case(multiply_filter)
2018-03-11 17:05:17 +00:00
def monumber_mofraction(left, right):
2018-03-17 07:28:43 +00:00
""" Return division Tree with on the numertor MOnumber times numerator of MOFraction
2018-03-11 17:05:17 +00:00
>>> a = MOnumber(4)
>>> b = MOFraction(6, 5)
2018-03-17 07:28:43 +00:00
>>> print(multiply(a, b))
2018-03-11 17:05:17 +00:00
/
> *
| > 4
| > 6
> 5
>>> b = MOFraction(6, 5, True)
2018-03-17 07:28:43 +00:00
>>> print(multiply(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
"""
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-17 07:28:43 +00:00
@multiply.register(MOFraction, MOnumber)
2018-03-17 08:55:32 +00:00
@special_case(multiply_filter)
2018-03-11 17:05:17 +00:00
def mofraction_monumber(left, right):
2018-03-17 07:28:43 +00:00
""" Return division Tree with on the numertor MOnumber times numerator of MOFraction
2018-03-11 17:05:17 +00:00
>>> a = MOFraction(6, 5)
>>> b = MOnumber(4)
2018-03-17 07:28:43 +00:00
>>> print(multiply(a, b))
2018-03-11 17:05:17 +00:00
/
> *
| > 6
| > 4
> 5
"""
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-17 07:28:43 +00:00
@multiply.register(MOFraction, MOFraction)
2018-03-17 08:55:32 +00:00
@special_case(multiply_filter)
def mofraction_mofraction(left, right):
2018-03-17 07:28:43 +00:00
""" Multiply two mofractions (numertors together and denominators together)
>>> a = MOFraction(1, 5)
>>> b = MOFraction(4, 5)
2018-03-17 07:28:43 +00:00
>>> print(multiply(a, b))
/
> *
| > 1
| > 4
> *
| > 5
| > 5
"""
num = Tree("*", left.numerator, right.numerator)
denom = Tree("*", left.denominator, right.denominator)
return Tree("/", num, denom)
2018-03-17 07:28:43 +00:00
@multiply.register((MOnumber, MOFraction), MOstr)
2018-03-17 08:55:32 +00:00
@special_case(multiply_filter)
def moscalar_mostr(left, right):
2018-03-17 07:28:43 +00:00
""" Multiply a scalar with a letter to create a MOMonomial
>>> a = MOnumber(2)
>>> b = MOstr('x')
2018-03-17 07:28:43 +00:00
>>> multiply(a, b)
<MOMonomial 2x>
>>> a = MOFraction(1, 5)
2018-03-17 07:28:43 +00:00
>>> multiply(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(left, right)
2018-03-17 07:28:43 +00:00
@multiply.register(MOstr, (MOnumber, MOFraction))
2018-03-17 08:55:32 +00:00
@special_case(multiply_filter)
def mostr_moscalar(left, right):
2018-03-17 07:28:43 +00:00
""" Multiply a scalar with a letter to create a MOMonomial
>>> a = MOstr('x')
>>> b = MOnumber(2)
2018-03-17 07:28:43 +00:00
>>> multiply(a, b)
<MOMonomial 2x>
>>> b = MOFraction(1, 5)
2018-03-17 07:28:43 +00:00
>>> multiply(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(right, left)
2018-03-17 07:28:43 +00:00
@multiply.register((MOnumber, MOFraction), MOMonomial)
2018-03-17 08:55:32 +00:00
@special_case(multiply_filter)
def moscalar_monomonial(left, right):
""" Multiply a scalar with a monomial
>>> a = MOnumber(4)
>>> b = MOMonomial(5, 'x', 3)
2018-03-17 07:28:43 +00:00
# >>> print(multiply(a, b))
"""
coefficient = Tree('*', left, right._coefficient)
return Tree('*', coefficient, MOMonomial(1, right._variable, right._power))
2018-03-11 17:05:17 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del