2018-10-08 15:57:17 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Power with MO
|
|
|
|
"""
|
|
|
|
|
|
|
|
from multipledispatch import Dispatcher
|
|
|
|
from ..tree import Tree
|
2018-12-21 10:26:37 +00:00
|
|
|
from ..MO import MO, MOnumber, MOstr
|
2018-10-08 15:57:17 +00:00
|
|
|
from ..MO.fraction import MOFraction
|
|
|
|
from ..MO.monomial import MOstrPower, MOMonomial
|
2018-12-19 13:17:43 +00:00
|
|
|
from ..MO.polynomial import MOpolynomial
|
2018-10-08 15:57:17 +00:00
|
|
|
from .filters import special_case
|
|
|
|
|
|
|
|
power_doc = """ Power of MOs
|
|
|
|
|
|
|
|
:param left: left MO
|
|
|
|
:param right: right MO
|
|
|
|
:returns: Tree or MO
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
power = Dispatcher("power", doc=power_doc)
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-10-08 15:57:17 +00:00
|
|
|
def power_filter(left, right):
|
|
|
|
""" Automatic power on MO
|
|
|
|
|
|
|
|
:param left: MO
|
|
|
|
:param right: MO
|
|
|
|
:returns: MO if it is a special case, nothing other wise
|
|
|
|
|
|
|
|
>>> a = MOnumber(5)
|
|
|
|
>>> b = MOnumber(1)
|
|
|
|
>>> power(a, b)
|
|
|
|
<MOnumber 5>
|
|
|
|
>>> a = MOnumber(5)
|
|
|
|
>>> b = MOnumber(0)
|
|
|
|
>>> power(a, b)
|
|
|
|
<MOnumber 1>
|
|
|
|
>>> a = MOFraction(1, 2)
|
|
|
|
>>> b = MOnumber(1)
|
|
|
|
>>> power(a, b)
|
|
|
|
<MOFraction 1 / 2>
|
|
|
|
>>> a = MOFraction(1, 2)
|
|
|
|
>>> b = MOnumber(0)
|
|
|
|
>>> power(a, b)
|
|
|
|
<MOnumber 1>
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if right == 0:
|
|
|
|
return MOnumber(1)
|
|
|
|
elif right == 1:
|
|
|
|
return left
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-10-08 15:57:17 +00:00
|
|
|
@power.register(MOnumber, MOnumber)
|
|
|
|
@special_case(power_filter)
|
|
|
|
def monumber_monumber(left, right):
|
|
|
|
""" Simply power values
|
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(6)
|
|
|
|
>>> power(a, b)
|
|
|
|
<MOnumber 4096>
|
|
|
|
|
|
|
|
"""
|
|
|
|
return MO.factory(left.value ** right.value)
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-10-08 15:57:17 +00:00
|
|
|
@power.register(MOFraction, MOnumber)
|
|
|
|
@special_case(power_filter)
|
|
|
|
def mofraction_monumber(left, right):
|
|
|
|
""" Return division Tree with on the numertor MOnumber times numerator of MOFraction
|
|
|
|
|
|
|
|
>>> a = MOFraction(3, 2)
|
|
|
|
>>> b = MOnumber(2)
|
|
|
|
>>> print(power(a, b))
|
2018-10-10 08:13:58 +00:00
|
|
|
/
|
|
|
|
> ^
|
|
|
|
| > 3
|
|
|
|
| > 2
|
|
|
|
> ^
|
|
|
|
| > 2
|
|
|
|
| > 2
|
2018-10-08 15:57:17 +00:00
|
|
|
"""
|
2018-10-10 08:13:58 +00:00
|
|
|
num = Tree("^", left.numerator, right)
|
|
|
|
denom = Tree("^", left.denominator, right)
|
|
|
|
return Tree("/", num, denom)
|
2018-10-08 15:57:17 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-19 13:17:43 +00:00
|
|
|
@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)
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-19 13:17:43 +00:00
|
|
|
@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)
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-19 13:17:43 +00:00
|
|
|
@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))
|
|
|
|
*
|
2018-12-21 11:20:13 +00:00
|
|
|
> 3x^2 - 2x + 1
|
|
|
|
> 3x^2 - 2x + 1
|
2018-12-19 13:17:43 +00:00
|
|
|
"""
|
2019-05-14 04:55:56 +00:00
|
|
|
return Tree.from_list("*", [left] * right.value)
|
2018-10-08 15:57:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|