Feat(compute): Start compute with power

This commit is contained in:
Bertrand Benjamin 2018-10-08 17:57:17 +02:00
parent 5e254a26eb
commit 78e781be62
2 changed files with 96 additions and 3 deletions

View File

@ -12,9 +12,10 @@ Computing with MO
from .exceptions import ComputeError from .exceptions import ComputeError
from .add import add from .add import add
from .divide import divide
from .minus import minus from .minus import minus
from .multiply import multiply from .multiply import multiply
from .divide import divide from .power import power
from ..MO.mo import MOnumber, MOstr from ..MO.mo import MOnumber, MOstr
from ..MO.fraction import MOFraction from ..MO.fraction import MOFraction
@ -30,6 +31,7 @@ OPERATIONS = {
"-": minus, "-": minus,
"*": multiply, "*": multiply,
"/": divide, "/": divide,
"^": power,
} }
def compute(node, left_v, right_v): def compute(node, left_v, right_v):

View File

@ -0,0 +1,91 @@
#! /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
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial
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)
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
@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)
@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))
"""
raise NotImplementedError
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del