Feat(compute): Multiply strpower and str done

Discover bug with str and * printing
This commit is contained in:
2018-10-10 10:28:38 +02:00
parent ddf15e4276
commit e6abc208a0
4 changed files with 104 additions and 1 deletions

View File

@@ -208,6 +208,53 @@ def mostr_mostrpower(left, right):
f"have same variable (got {left.variable} and {right.variable})")
return MOstrPower(left.variable, right.power+1)
@multiply.register(MOstrPower, MOstr)
@special_case(multiply_filter)
def mostr_mostrpower(left, right):
""" Multiply a MOstr and a MOstrPower
>>> a = MOstrPower('x', 4)
>>> b = MOstr('x')
>>> multiply(a, b)
<MOstrPower x^5>
>>> a = MOstrPower('y', 4)
>>> b = MOstr('x')
>>> multiply(a, b)
Traceback (most recent call last):
...
mapytex.calculus.core.compute.exceptions.MultiplyError: Can't multiply MOstr and MOstrPower if they don'thave same variable (got x and y)
"""
if left.variable != right.variable:
raise MultiplyError("Can't multiply MOstr and MOstrPower if they don't"
f"have same variable (got {left.variable} and {right.variable})")
return MOstrPower(left.variable, left.power+1)
@multiply.register(MOstrPower, MOstrPower)
@special_case(multiply_filter)
def mostr_mostrpower(left, right):
""" Multiply a MOstrPower and a MOstrPower
>>> a = MOstrPower('x', 2)
>>> b = MOstrPower('x', 4)
>>> print(multiply(a, b))
^
> x
> +
| > 2
| > 4
>>> a = MOstrPower('x', 2)
>>> b = MOstrPower('y', 4)
>>> multiply(a, b)
Traceback (most recent call last):
...
mapytex.calculus.core.compute.exceptions.MultiplyError: Can't multiply MOstr and MOstrPower if they don'thave same variable (got x and y)
"""
if left.variable != right.variable:
raise MultiplyError("Can't multiply MOstr and MOstrPower if they don't"
f"have same variable (got {left.variable} and {right.variable})")
power = Tree("+", left.power, right.power)
return Tree("^", left.variable, power)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: