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

148 lines
3.4 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.
"""
Multiply MO
"""
from multipledispatch import Dispatcher
from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOMonomial
from .exceptions import MultiplyError
from .type_filter import args_are
multiply_doc = """ Multiply MOs
:param left: left MO
:param right: right MO
:returns: Tree or MO
"""
multiply = Dispatcher("multiply", doc=multiply_doc)
@multiply.register(MOnumber, MOnumber)
def monumber_monumber(left, right):
""" Simply multiply values
>>> a = MOnumber(4)
>>> b = MOnumber(6)
>>> multiply(a, b)
<MOnumber 24>
"""
return MO.factory(left.value * right.value)
@multiply.register(MOnumber, MOFraction)
def monumber_mofraction(left, right):
""" Return division Tree with on the numertor MOnumber times numerator of MOFraction
>>> a = MOnumber(4)
>>> b = MOFraction(6, 5)
>>> print(multiply(a, b))
/
> *
| > 4
| > 6
> 5
>>> b = MOFraction(6, 5, True)
>>> print(multiply(a, b))
/
> *
| > 4
| > -
| | > None
| | > 6
> 5
"""
num = Tree("*", left, right.numerator)
return Tree("/", num, right._denominator)
@multiply.register(MOFraction, MOnumber)
def mofraction_monumber(left, right):
""" Return division Tree with on the numertor MOnumber times numerator of MOFraction
>>> a = MOFraction(6, 5)
>>> b = MOnumber(4)
>>> print(multiply(a, b))
/
> *
| > 6
| > 4
> 5
"""
num = Tree("*", left.numerator, right)
return Tree("/", num, left._denominator)
@multiply.register(MOFraction, MOFraction)
def mofraction_mofraction(left, right):
""" Multiply two mofractions (numertors together and denominators together)
>>> a = MOFraction(1, 5)
>>> b = MOFraction(4, 5)
>>> print(multiply(a, b))
/
> *
| > 1
| > 4
> *
| > 5
| > 5
"""
num = Tree("*", left.numerator, right.numerator)
denom = Tree("*", left.denominator, right.denominator)
return Tree("/", num, denom)
@multiply.register((MOnumber, MOFraction), MOstr)
def moscalar_mostr(left, right):
""" Multiply a scalar with a letter to create a MOMonomial
>>> a = MOnumber(2)
>>> b = MOstr('x')
>>> multiply(a, b)
<MOMonomial 2x>
>>> a = MOFraction(1, 5)
>>> multiply(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(left, right)
@multiply.register(MOstr, (MOnumber, MOFraction))
def mostr_moscalar(left, right):
""" Multiply a scalar with a letter to create a MOMonomial
>>> a = MOstr('x')
>>> b = MOnumber(2)
>>> multiply(a, b)
<MOMonomial 2x>
>>> b = MOFraction(1, 5)
>>> multiply(a, b)
<MOMonomial 1 / 5x>
"""
return MOMonomial(right, left)
@multiply.register((MOnumber, MOFraction), MOMonomial)
def moscalar_monomonial(left, right):
""" Multiply a scalar with a monomial
>>> a = MOnumber(4)
>>> b = MOMonomial(5, 'x', 3)
# >>> print(multiply(a, b))
"""
coefficient = Tree('*', left, right._coefficient)
return Tree('*', coefficient, MOMonomial(1, right._variable, right._power))
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del