Feat(Core): Compute with power

This commit is contained in:
Bertrand Benjamin 2018-12-19 14:17:43 +01:00
parent 2551037b65
commit 80305671de

View File

@ -15,6 +15,7 @@ from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial
from ..MO.polynomial import MOpolynomial
from .filters import special_case
power_doc = """ Power of MOs
@ -92,6 +93,80 @@ def mofraction_monumber(left, right):
denom = Tree("^", left.denominator, right)
return Tree("/", num, denom)
@power.register(MOstrPower, MOnumber)
@special_case(power_filter)
def mostrpower_monumber(left, right):
""" Multiply powers
>>> P = MOstrPower("x", 4)
>>> a = MOnumber(2)
>>> print(power(P, a))
^
> x
> *
| > 4
| > 2
"""
power = Tree("*", left.power, right)
return Tree("^", left.variable, power)
@power.register(MOMonomial, MOnumber)
@special_case(power_filter)
def mostrpower_monumber(left, right):
""" Multiply powers and raise coef to the power
>>> P = MOMonomial(3,"x", 4)
>>> a = MOnumber(2)
>>> print(power(P, a))
*
> ^
| > 3
| > 2
> ^
| > x
| > *
| | > 4
| | > 2
"""
coef = Tree("^", left.coefficient, right)
power = Tree("*", left.power, right)
strpower = Tree("^", left.variable, power)
return Tree("*", coef, strpower)
@power.register(MOpolynomial, MOnumber)
@special_case(power_filter)
def mopolynomial_monumber(left, right):
""" Expand power to products of polynomials
>>> P = MOpolynomial('x', [1, -2, 3])
>>> a = MOnumber(2)
>>> print(power(P, a))
*
> +
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > +
| | > *
| | | > -2
| | | > x
| | > 1
> +
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > +
| | > *
| | | > -2
| | | > x
| | > 1
"""
return Tree.from_list("*", [left]*right.value)
# -----------------------------