From e6abc208a0b472ae7fecfed3483a6f4617ba6993 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 10 Oct 2018 10:28:38 +0200 Subject: [PATCH] Feat(compute): Multiply strpower and str done Discover bug with str and * printing --- mapytex/calculus/API/__init__.py | 11 +++++- mapytex/calculus/core/compute/multiply.py | 47 +++++++++++++++++++++++ mapytex/calculus/core/typing/__init__.py | 2 + mapytex/calculus/core/typing/power.py | 45 ++++++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 mapytex/calculus/core/typing/power.py diff --git a/mapytex/calculus/API/__init__.py b/mapytex/calculus/API/__init__.py index 999ed70..8210e50 100644 --- a/mapytex/calculus/API/__init__.py +++ b/mapytex/calculus/API/__init__.py @@ -41,7 +41,16 @@ Generate and compute like a student! (2 / 3)^4 2^4 / 3^4 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 diff --git a/mapytex/calculus/core/compute/multiply.py b/mapytex/calculus/core/compute/multiply.py index 08089aa..1bc8deb 100644 --- a/mapytex/calculus/core/compute/multiply.py +++ b/mapytex/calculus/core/compute/multiply.py @@ -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) + + >>> 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: diff --git a/mapytex/calculus/core/typing/__init__.py b/mapytex/calculus/core/typing/__init__.py index babc408..20276d0 100644 --- a/mapytex/calculus/core/typing/__init__.py +++ b/mapytex/calculus/core/typing/__init__.py @@ -15,6 +15,7 @@ from .exceptions import TypingError # from .minus import minus # from .multiply import multiply from .divide import divide +from .power import power from ..MO.mo import MOnumber, MOstr from ..MO.fraction import MOFraction @@ -30,6 +31,7 @@ OPERATIONS = { # "-": minus, # "*": multiply, "/": divide, + "^": power, } def typing(node, left_v, right_v): diff --git a/mapytex/calculus/core/typing/power.py b/mapytex/calculus/core/typing/power.py new file mode 100644 index 0000000..07f3ae8 --- /dev/null +++ b/mapytex/calculus/core/typing/power.py @@ -0,0 +1,45 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 lafrite +# +# 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) + + + """ + return MOstrPower(left, right) + + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del