Feat(compute): Multiply strpower and str done

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

View File

@ -41,7 +41,16 @@ Generate and compute like a student!
(2 / 3)^4 (2 / 3)^4
2^4 / 3^4 2^4 / 3^4
16 / 81 16 / 81
>>> e = Expression.from_str("x^2*x*x^4")
>>> e_simplified = e.simplify()
>>> print(e_simplified)
x^7
>>> for s in e_simplified.explain():
... print(s)
x^2 * x * x^4
x^3 * x^4
x^(3 + 4)
x^7
""" """
from .expression import Expression from .expression import Expression

View File

@ -208,6 +208,53 @@ def mostr_mostrpower(left, right):
f"have same variable (got {left.variable} and {right.variable})") f"have same variable (got {left.variable} and {right.variable})")
return MOstrPower(left.variable, right.power+1) 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' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -15,6 +15,7 @@ from .exceptions import TypingError
# from .minus import minus # from .minus import minus
# from .multiply import multiply # from .multiply import multiply
from .divide import divide 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 typing(node, left_v, right_v): def typing(node, left_v, right_v):

View File

@ -0,0 +1,45 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
Typing Power with MO
"""
from multipledispatch import Dispatcher
from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.monomial import MOstrPower
power_doc = """ Typing Power of MOs
:param left: left MO
:param right: right MO
:returns: Tree or MO
"""
power = Dispatcher("power", doc=power_doc)
@power.register(MOstr, MOnumber)
def monumber_monumber(left, right):
""" Create MOstrPower over powered MOstr
>>> a = MOstr("x")
>>> b = MOnumber(6)
>>> power(a, b)
<MOstrPower x^6>
"""
return MOstrPower(left, right)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del