Fix(compute): split multiply between compute and type
This commit is contained in:
parent
ddab3cebad
commit
b24069a72d
@ -145,70 +145,6 @@ def mofraction_mofraction(left, right):
|
|||||||
denom = Tree("*", left.denominator, right.denominator)
|
denom = Tree("*", left.denominator, right.denominator)
|
||||||
return Tree("/", num, denom)
|
return Tree("/", num, denom)
|
||||||
|
|
||||||
@multiply.register((MOnumber, MOFraction), MOstr)
|
|
||||||
@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 / 5x>
|
|
||||||
"""
|
|
||||||
return MOMonomial(left, right)
|
|
||||||
|
|
||||||
@multiply.register(MOstr, (MOnumber, MOFraction))
|
|
||||||
@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 / 5x>
|
|
||||||
"""
|
|
||||||
return MOMonomial(right, left)
|
|
||||||
|
|
||||||
@multiply.register((MOnumber, MOFraction), MOstrPower)
|
|
||||||
@special_case(multiply_filter)
|
|
||||||
def moscalar_mostrpower(left, right):
|
|
||||||
""" Multiply a scalar with a MOstrPower
|
|
||||||
|
|
||||||
>>> a = MOnumber(4)
|
|
||||||
>>> x = MOstrPower('x', 4)
|
|
||||||
>>> print(multiply(a, x))
|
|
||||||
*
|
|
||||||
> 4
|
|
||||||
> ^
|
|
||||||
| > x
|
|
||||||
| > 4
|
|
||||||
|
|
||||||
"""
|
|
||||||
return MOMonomial(left, right)
|
|
||||||
|
|
||||||
@multiply.register(MOstrPower, (MOnumber, MOFraction))
|
|
||||||
@special_case(multiply_filter)
|
|
||||||
def mostrpower_moscalar(left, right):
|
|
||||||
""" Multiply a MOstrPower with a scalar
|
|
||||||
|
|
||||||
>>> a = MOnumber(4)
|
|
||||||
>>> x = MOstrPower('x', 4)
|
|
||||||
>>> print(multiply(x, a))
|
|
||||||
*
|
|
||||||
> 4
|
|
||||||
> ^
|
|
||||||
| > x
|
|
||||||
| > 4
|
|
||||||
|
|
||||||
"""
|
|
||||||
return MOMonomial(right, left)
|
|
||||||
|
|
||||||
@multiply.register((MOnumber, MOFraction), MOMonomial)
|
@multiply.register((MOnumber, MOFraction), MOMonomial)
|
||||||
@special_case(multiply_filter)
|
@special_case(multiply_filter)
|
||||||
def moscalar_monomonial(left, right):
|
def moscalar_monomonial(left, right):
|
||||||
|
138
mapytex/calculus/core/typing/multiply.py
Normal file
138
mapytex/calculus/core/typing/multiply.py
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
#! /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.mo import MO, MOnumber, MOstr
|
||||||
|
from ..MO.fraction import MOFraction
|
||||||
|
from ..MO.monomial import MOstrPower, MOMonomial
|
||||||
|
from .exceptions import MultiplyError
|
||||||
|
from .filters import special_case
|
||||||
|
|
||||||
|
multiply_doc = """ Multiply MOs
|
||||||
|
|
||||||
|
:param left: left MO
|
||||||
|
:param right: right MO
|
||||||
|
:returns: Tree or MO
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
multiply = Dispatcher("multiply", doc=multiply_doc)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
@multiply.register((MOnumber, MOFraction), MOstr)
|
||||||
|
@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 / 5x>
|
||||||
|
"""
|
||||||
|
return MOMonomial(left, right)
|
||||||
|
|
||||||
|
@multiply.register(MOstr, (MOnumber, MOFraction))
|
||||||
|
@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 / 5x>
|
||||||
|
"""
|
||||||
|
return MOMonomial(right, left)
|
||||||
|
|
||||||
|
@multiply.register((MOnumber, MOFraction), MOstrPower)
|
||||||
|
@special_case(multiply_filter)
|
||||||
|
def moscalar_mostrpower(left, right):
|
||||||
|
""" Multiply a scalar with a MOstrPower
|
||||||
|
|
||||||
|
>>> a = MOnumber(4)
|
||||||
|
>>> x = MOstrPower('x', 4)
|
||||||
|
>>> print(multiply(a, x))
|
||||||
|
*
|
||||||
|
> 4
|
||||||
|
> ^
|
||||||
|
| > x
|
||||||
|
| > 4
|
||||||
|
|
||||||
|
"""
|
||||||
|
return MOMonomial(left, right)
|
||||||
|
|
||||||
|
@multiply.register(MOstrPower, (MOnumber, MOFraction))
|
||||||
|
@special_case(multiply_filter)
|
||||||
|
def mostrpower_moscalar(left, right):
|
||||||
|
""" Multiply a MOstrPower with a scalar
|
||||||
|
|
||||||
|
>>> a = MOnumber(4)
|
||||||
|
>>> x = MOstrPower('x', 4)
|
||||||
|
>>> print(multiply(x, a))
|
||||||
|
*
|
||||||
|
> 4
|
||||||
|
> ^
|
||||||
|
| > x
|
||||||
|
| > 4
|
||||||
|
|
||||||
|
"""
|
||||||
|
return MOMonomial(right, left)
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
|
# Reglages pour 'vim'
|
||||||
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||||
|
# cursor: 16 del
|
Loading…
Reference in New Issue
Block a user