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

130 lines
2.7 KiB
Python
Raw Normal View History

#! /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 with typing
"""
from multipledispatch import Dispatcher
from ..tree import Tree
from ..MO import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial
2019-07-15 08:42:13 +00:00
from ..compute.filters import special_case
multiply_doc = """ Multiply MOs
:param left: left MO
:param right: right MO
2018-11-14 15:04:53 +00:00
:returns: MO
"""
multiply = Dispatcher("multiply", doc=multiply_doc)
2019-10-13 19:01:31 +00:00
2019-07-15 08:42:13 +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
"""
2019-07-15 15:27:38 +00:00
try:
if left == 0:
return left
except TypeError:
pass
try:
if right == 0:
return right
except TypeError:
pass
2019-07-15 08:42:13 +00:00
try:
if left == 1:
return right
except TypeError:
pass
try:
if right == 1:
return left
except TypeError:
pass
2019-05-14 04:55:56 +00:00
2019-10-13 19:01:31 +00:00
@multiply.register((MOnumber, MOFraction), MOstr)
2019-07-15 08:42:13 +00:00
@special_case(multiply_filter)
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 / 5 * x>
"""
return MOMonomial(left, right)
2019-05-14 04:55:56 +00:00
@multiply.register(MOstr, (MOnumber, MOFraction))
2019-07-15 08:42:13 +00:00
@special_case(multiply_filter)
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 / 5 * x>
"""
return MOMonomial(right, left)
2019-05-14 04:55:56 +00:00
@multiply.register((MOnumber, MOFraction), MOstrPower)
2019-07-15 08:42:13 +00:00
@special_case(multiply_filter)
def moscalar_mostrpower(left, right):
""" Multiply a scalar with a MOstrPower
>>> a = MOnumber(4)
>>> x = MOstrPower('x', 4)
2018-11-14 15:04:53 +00:00
>>> multiply(a, x)
<MOMonomial 4x^4>
2019-07-15 08:42:13 +00:00
>>> a = MOnumber(1)
>>> x = MOstrPower('x', 4)
>>> multiply(a, x)
<MOstrPower x^4>
"""
2019-10-13 19:01:31 +00:00
# if left == 1:
2019-07-15 08:42:13 +00:00
# return right
return MOMonomial(left, right)
2019-05-14 04:55:56 +00:00
@multiply.register(MOstrPower, (MOnumber, MOFraction))
2019-07-15 08:42:13 +00:00
@special_case(multiply_filter)
def mostrpower_moscalar(left, right):
""" Multiply a MOstrPower with a scalar
>>> a = MOnumber(4)
>>> x = MOstrPower('x', 4)
2018-11-14 15:04:53 +00:00
>>> multiply(x, a)
<MOMonomial 4x^4>
"""
return MOMonomial(right, left)
2019-05-14 04:55:56 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del