2018-03-11 15:34:41 +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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Adding MO
|
|
|
|
"""
|
|
|
|
|
2018-03-17 08:55:32 +00:00
|
|
|
from functools import wraps
|
2018-03-17 07:38:59 +00:00
|
|
|
from multipledispatch import Dispatcher
|
2018-03-11 15:34:41 +00:00
|
|
|
from ..tree import Tree
|
2018-11-13 11:12:36 +00:00
|
|
|
from ..MO.mo import MO, MOnumber, MOstr
|
2018-03-11 15:34:41 +00:00
|
|
|
from ..MO.fraction import MOFraction
|
2018-11-13 11:12:36 +00:00
|
|
|
from ..MO.monomial import MOstrPower, MOMonomial
|
2018-11-13 13:42:40 +00:00
|
|
|
from ..MO.polynomial import MOpolynomial
|
2018-03-11 15:34:41 +00:00
|
|
|
from .exceptions import AddError
|
2018-03-12 04:34:26 +00:00
|
|
|
from .arithmetic import lcm
|
2018-03-17 08:55:32 +00:00
|
|
|
from .filters import special_case
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
add_doc = """ Adding MOs
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 07:38:59 +00:00
|
|
|
:param left: left MO
|
|
|
|
:param right: right MO
|
|
|
|
:return: Tree or MO
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
"""
|
2018-03-17 08:55:32 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
add = Dispatcher("add", doc=add_doc)
|
2018-03-11 15:34:41 +00:00
|
|
|
|
2018-03-17 08:55:32 +00:00
|
|
|
def add_filter(left, right):
|
|
|
|
""" Automatic add on MO
|
|
|
|
|
|
|
|
:param left: MO
|
|
|
|
:param right: MO
|
|
|
|
:returns: MO if it is a special case, nothing other wise
|
|
|
|
|
|
|
|
>>> a = MOnumber(0)
|
|
|
|
>>> b = MOFraction(1, 2)
|
|
|
|
>>> add(a, b)
|
|
|
|
<MOFraction 1 / 2>
|
|
|
|
>>> add(b, a)
|
|
|
|
<MOFraction 1 / 2>
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if left == 0:
|
|
|
|
return right
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
if right == 0:
|
|
|
|
return left
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2018-11-13 15:19:02 +00:00
|
|
|
try:
|
|
|
|
if left.variable != right.variable:
|
|
|
|
raise NotImplementedError("Can't add 2 polynomes with not same letter")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2018-03-17 08:55:32 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOnumber, MOnumber)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-11 15:34:41 +00:00
|
|
|
def monumber_monumber(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" Simply add MO value
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOnumber(6)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> add(a, b)
|
2018-03-11 15:34:41 +00:00
|
|
|
<MOnumber 10>
|
|
|
|
|
|
|
|
"""
|
|
|
|
return MO.factory(left.value + right.value)
|
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOnumber, MOFraction)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-11 15:34:41 +00:00
|
|
|
def monumber_mofraction(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" Return a tree with the MOnumber transformed into a MOFraction
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> b = MOFraction(6, 5)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> print(add(a, b))
|
2018-03-11 15:34:41 +00:00
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > 4
|
|
|
|
| > 1
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
|
|
|
"""
|
|
|
|
left_fraction = MOFraction(left, MOnumber(1))
|
|
|
|
return Tree("+", left_fraction, right)
|
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOFraction, MOnumber)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-11 15:34:41 +00:00
|
|
|
def mofraction_monumber(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" Return a tree with the MOnumber transformed into a MOFraction
|
2018-03-11 15:34:41 +00:00
|
|
|
|
|
|
|
>>> a = MOFraction(6, 5)
|
|
|
|
>>> b = MOnumber(4)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> print(add(a, b))
|
2018-03-11 15:34:41 +00:00
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
|
|
|
> /
|
|
|
|
| > 4
|
|
|
|
| > 1
|
|
|
|
"""
|
|
|
|
right_fraction = MOFraction(right, MOnumber(1))
|
|
|
|
return Tree("+", left, right_fraction)
|
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
@add.register(MOFraction, MOFraction)
|
2018-03-17 08:55:32 +00:00
|
|
|
@special_case(add_filter)
|
2018-03-12 04:34:26 +00:00
|
|
|
def mofraction_mofraction(left, right):
|
2018-03-17 07:04:53 +00:00
|
|
|
""" 3 differents cases:
|
2018-03-12 04:34:26 +00:00
|
|
|
|
2018-03-17 07:04:53 +00:00
|
|
|
Fractions have same denomintor -> add numerator Tree
|
2018-03-12 04:34:26 +00:00
|
|
|
|
|
|
|
>>> a = MOFraction(1, 5)
|
|
|
|
>>> b = MOFraction(4, 5)
|
2018-03-17 07:04:53 +00:00
|
|
|
>>> print(add(a, b))
|
2018-03-12 04:34:26 +00:00
|
|
|
/
|
|
|
|
> +
|
|
|
|
| > 1
|
|
|
|
| > 4
|
|
|
|
> 5
|
|
|
|
>>> a = MOFraction(1, 5, True)
|
|
|
|
>>> b = MOFraction(4, 5)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
/
|
|
|
|
> +
|
|
|
|
| > -
|
|
|
|
| | > None
|
|
|
|
| | > 1
|
|
|
|
| > 4
|
|
|
|
> 5
|
|
|
|
>>> a = MOFraction(1, 5)
|
|
|
|
>>> b = MOFraction(4, 5, True)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
/
|
|
|
|
> +
|
|
|
|
| > 1
|
|
|
|
| > -
|
|
|
|
| | > None
|
|
|
|
| | > 4
|
|
|
|
> 5
|
2018-03-17 07:04:53 +00:00
|
|
|
|
|
|
|
A denominator is a multiple of the other
|
|
|
|
|
2018-03-12 04:34:26 +00:00
|
|
|
>>> a = MOFraction(1, 2)
|
|
|
|
>>> b = MOFraction(1, 4)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > *
|
|
|
|
| | > 1
|
|
|
|
| | > 2
|
|
|
|
| > *
|
|
|
|
| | > 2
|
|
|
|
| | > 2
|
|
|
|
> /
|
|
|
|
| > 1
|
|
|
|
| > 4
|
2018-03-17 07:04:53 +00:00
|
|
|
|
|
|
|
Denominators are coprime
|
|
|
|
|
2018-03-12 04:34:26 +00:00
|
|
|
>>> a = MOFraction(1, 2)
|
|
|
|
>>> b = MOFraction(1, 5)
|
|
|
|
>>> print(mofraction_mofraction(a, b))
|
|
|
|
+
|
|
|
|
> /
|
|
|
|
| > *
|
|
|
|
| | > 1
|
|
|
|
| | > 5
|
|
|
|
| > *
|
|
|
|
| | > 2
|
|
|
|
| | > 5
|
|
|
|
> /
|
|
|
|
| > *
|
|
|
|
| | > 1
|
|
|
|
| | > 2
|
|
|
|
| > *
|
|
|
|
| | > 5
|
|
|
|
| | > 2
|
|
|
|
"""
|
|
|
|
if left.denominator == right.denominator:
|
|
|
|
num = Tree("+", left.numerator, right.numerator)
|
|
|
|
return Tree("/", num, left.denominator)
|
|
|
|
|
2018-11-13 08:14:50 +00:00
|
|
|
denom_lcm = lcm(left.denominator.value, right.denominator.value)
|
2018-03-12 04:34:26 +00:00
|
|
|
|
2018-11-13 08:14:50 +00:00
|
|
|
if left.denominator.value == denom_lcm:
|
2018-03-12 04:34:26 +00:00
|
|
|
left_frac = left
|
|
|
|
else:
|
2018-11-13 08:14:50 +00:00
|
|
|
multiply_by = MO.factory(denom_lcm // left.denominator.value)
|
|
|
|
left_num = Tree("*", left.numerator, multiply_by)
|
|
|
|
left_denom = Tree("*", left.denominator, multiply_by)
|
2018-03-12 04:34:26 +00:00
|
|
|
left_frac = Tree("/", left_num, left_denom)
|
|
|
|
|
2018-11-13 08:14:50 +00:00
|
|
|
if right.denominator.value == denom_lcm:
|
2018-03-12 04:34:26 +00:00
|
|
|
right_frac = right
|
|
|
|
else:
|
2018-11-13 08:14:50 +00:00
|
|
|
multiply_by = MO.factory(denom_lcm // right.denominator.value)
|
|
|
|
right_num = Tree("*", right.numerator, multiply_by)
|
|
|
|
right_denom = Tree("*", right.denominator, multiply_by)
|
2018-03-12 04:34:26 +00:00
|
|
|
right_frac = Tree("/", right_num, right_denom)
|
|
|
|
|
|
|
|
return Tree("+", left_frac, right_frac)
|
|
|
|
|
2018-11-13 11:12:36 +00:00
|
|
|
@add.register(MOstr, MOstr)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mostr_mostr(left, right):
|
|
|
|
""" Add 2 MOstr
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOstr("x")
|
|
|
|
>>> b = MOstr("x")
|
|
|
|
>>> add(a, b)
|
|
|
|
<MOMonomial 2x>
|
|
|
|
"""
|
|
|
|
if left != right:
|
|
|
|
raise NotImplementedError("Can't add 2 MOstr with not same letter")
|
|
|
|
return MOMonomial(2, left)
|
|
|
|
|
|
|
|
@add.register(MOstrPower, MOstrPower)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mostrpower_mostrpower(left, right):
|
|
|
|
""" Add 2 MOstrPower
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOstrPower("x", 2)
|
|
|
|
>>> b = MOstrPower("x", 2)
|
|
|
|
>>> add(a, b)
|
|
|
|
<MOMonomial 2x^2>
|
|
|
|
"""
|
|
|
|
if left.power != right.power:
|
|
|
|
raise NotImplementedError("Can't add 2 MOstrPower with not same power")
|
|
|
|
return MOMonomial(2, left.variable, left.power)
|
|
|
|
|
2018-11-13 13:42:40 +00:00
|
|
|
@add.register((MOnumber, MOFraction), MOpolynomial)
|
|
|
|
@special_case(add_filter)
|
2018-11-13 15:19:02 +00:00
|
|
|
def moscalar_mopolynomial(left, right):
|
2018-11-13 13:42:40 +00:00
|
|
|
""" Add a scalar to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOnumber(1)
|
|
|
|
>>> b = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
2018-11-13 15:19:02 +00:00
|
|
|
> *
|
|
|
|
| > 4
|
|
|
|
| > ^
|
|
|
|
| | > x
|
|
|
|
| | > 2
|
2018-11-13 13:42:40 +00:00
|
|
|
> +
|
|
|
|
| > *
|
2018-11-13 15:19:02 +00:00
|
|
|
| | > 3
|
|
|
|
| | > x
|
|
|
|
| > +
|
|
|
|
| | > 1
|
|
|
|
| | > 2
|
|
|
|
"""
|
|
|
|
if 0 not in right.coefficients.keys():
|
|
|
|
raise NotImplementedError("Polynomial with no constant, no calculus to do")
|
|
|
|
|
|
|
|
right_const = right.monomials[0]
|
|
|
|
right_top = [mo for deg, mo in right.monomials.items() if deg > 0][::-1]
|
|
|
|
|
|
|
|
adds = right_top + [Tree("+", left, right_const)]
|
|
|
|
return Tree.from_list('+', adds)
|
|
|
|
|
|
|
|
@add.register(MOpolynomial, (MOnumber, MOFraction))
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mopolynomial_moscalar(left, right):
|
|
|
|
""" Add a scalar to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> b = MOnumber(1)
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> *
|
|
|
|
| > 4
|
|
|
|
| > ^
|
|
|
|
| | > x
|
|
|
|
| | > 2
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
|
|
|
| > +
|
|
|
|
| | > 2
|
|
|
|
| | > 1
|
|
|
|
"""
|
|
|
|
if 0 not in left.coefficients.keys():
|
|
|
|
raise NotImplementedError("Polynomial with no constant, no calculus to do")
|
|
|
|
|
|
|
|
left_const = left.monomials[0]
|
|
|
|
left_top = [mo for deg, mo in left.monomials.items() if deg > 0][::-1]
|
|
|
|
|
|
|
|
adds = left_top + [Tree("+", left_const, right)]
|
|
|
|
return Tree.from_list('+', adds)
|
|
|
|
|
|
|
|
@add.register(MOstr, MOpolynomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mostr_mopolynomial(left, right):
|
|
|
|
""" Add a str to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOstr("x")
|
|
|
|
>>> b = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> *
|
|
|
|
| > 4
|
|
|
|
| > ^
|
|
|
|
| | > x
|
|
|
|
| | > 2
|
|
|
|
> +
|
|
|
|
| > +
|
|
|
|
| | > x
|
|
|
|
| | > *
|
|
|
|
| | | > 3
|
|
|
|
| | | > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
if 1 not in right.coefficients.keys():
|
|
|
|
raise NotImplementedError("Polynomial with no constant, no calculus to do")
|
|
|
|
|
|
|
|
right_coef = right.monomials[1]
|
|
|
|
add_coefs = Tree("+", left, right_coef)
|
|
|
|
|
|
|
|
right_top = [mo for deg, mo in right.monomials.items() if deg > 1][::-1]
|
|
|
|
right_bot = [mo for deg, mo in right.monomials.items() if deg < 1][::-1]
|
|
|
|
|
|
|
|
adds = right_top + [add_coefs] + right_bot
|
|
|
|
|
|
|
|
return Tree.from_list("+", adds)
|
|
|
|
|
|
|
|
@add.register(MOpolynomial, MOstr)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mopolynomial_mostr(left, right):
|
|
|
|
""" Add a str to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> b = MOstr("x")
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> *
|
|
|
|
| > 4
|
|
|
|
| > ^
|
|
|
|
| | > x
|
|
|
|
| | > 2
|
|
|
|
> +
|
|
|
|
| > +
|
|
|
|
| | > *
|
|
|
|
| | | > 3
|
|
|
|
| | | > x
|
|
|
|
| | > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
if 1 not in left.coefficients.keys():
|
|
|
|
raise NotImplementedError("No degree in common")
|
|
|
|
|
|
|
|
left_coef = left.monomials[1]
|
|
|
|
add_coefs = Tree("+", left_coef, right)
|
|
|
|
|
|
|
|
left_top = [mo for deg, mo in left.monomials.items() if deg > 1][::-1]
|
|
|
|
left_bot = [mo for deg, mo in left.monomials.items() if deg < 1][::-1]
|
|
|
|
|
|
|
|
adds = left_top + [add_coefs] + left_bot
|
|
|
|
|
|
|
|
return Tree.from_list("+", adds)
|
|
|
|
|
|
|
|
@add.register(MOstrPower, MOpolynomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mostrpower_mopolynomial(left, right):
|
|
|
|
""" Add a strpower to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOstrPower("x", 2)
|
|
|
|
>>> b = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > ^
|
|
|
|
| | > x
|
|
|
|
| | > 2
|
|
|
|
| > *
|
2018-11-13 13:42:40 +00:00
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
2018-11-13 15:19:02 +00:00
|
|
|
> +
|
2018-11-13 13:42:40 +00:00
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
2018-11-13 15:19:02 +00:00
|
|
|
| > 2
|
|
|
|
>>> b = MOpolynomial("x", [2, 3, 4, 5])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
2018-11-13 13:42:40 +00:00
|
|
|
> +
|
2018-11-13 15:19:02 +00:00
|
|
|
| > *
|
|
|
|
| | > 5
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 3
|
|
|
|
| > +
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
| | > *
|
|
|
|
| | | > 4
|
|
|
|
| | | > ^
|
|
|
|
| | | | > x
|
|
|
|
| | | | > 2
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
2018-11-13 13:42:40 +00:00
|
|
|
| > 2
|
|
|
|
"""
|
2018-11-13 15:19:02 +00:00
|
|
|
if left.power not in right.coefficients.keys():
|
|
|
|
raise NotImplementedError("No degree in common")
|
2018-11-13 13:42:40 +00:00
|
|
|
|
2018-11-13 15:19:02 +00:00
|
|
|
right_mono = right.monomials[left.power]
|
|
|
|
add_coefs = Tree("+", left, right_mono)
|
2018-11-13 13:42:40 +00:00
|
|
|
|
2018-11-13 15:19:02 +00:00
|
|
|
right_top = [mo for deg, mo in right.monomials.items() if deg > left.power][::-1]
|
|
|
|
right_bot = [mo for deg, mo in right.monomials.items() if deg < left.power][::-1]
|
2018-11-13 13:42:40 +00:00
|
|
|
|
2018-11-13 15:19:02 +00:00
|
|
|
adds = right_top + [add_coefs] + right_bot
|
|
|
|
|
|
|
|
return Tree.from_list("+", adds)
|
|
|
|
|
|
|
|
@add.register(MOpolynomial, MOstrPower)
|
2018-11-13 13:42:40 +00:00
|
|
|
@special_case(add_filter)
|
2018-11-13 15:19:02 +00:00
|
|
|
def mopolynomial_mostrpower(left, right):
|
|
|
|
""" Add a strpower to a polynomial
|
2018-11-13 13:42:40 +00:00
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOpolynomial("x", [2, 3, 4])
|
2018-11-13 15:19:02 +00:00
|
|
|
>>> b = MOstrPower("x", 2)
|
2018-11-13 13:42:40 +00:00
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
2018-11-13 15:19:02 +00:00
|
|
|
| > ^
|
|
|
|
| | > x
|
|
|
|
| | > 2
|
|
|
|
> +
|
2018-11-13 13:42:40 +00:00
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
2018-11-13 15:19:02 +00:00
|
|
|
| > 2
|
|
|
|
>>> a = MOpolynomial("x", [2, 3, 4, 5])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
2018-11-13 13:42:40 +00:00
|
|
|
> +
|
2018-11-13 15:19:02 +00:00
|
|
|
| > *
|
|
|
|
| | > 5
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 3
|
|
|
|
| > +
|
|
|
|
| | > *
|
|
|
|
| | | > 4
|
|
|
|
| | | > ^
|
|
|
|
| | | | > x
|
|
|
|
| | | | > 2
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
2018-11-13 13:42:40 +00:00
|
|
|
| > 2
|
|
|
|
"""
|
2018-11-13 15:19:02 +00:00
|
|
|
if right.power not in left.coefficients.keys():
|
|
|
|
raise NotImplementedError("No degree in common")
|
|
|
|
|
|
|
|
left_mono = left.monomials[right.power]
|
|
|
|
add_coefs = Tree("+", left_mono, right)
|
|
|
|
|
|
|
|
left_top = [mo for deg, mo in left.monomials.items() if deg > right.power][::-1]
|
|
|
|
left_bot = [mo for deg, mo in left.monomials.items() if deg < right.power][::-1]
|
|
|
|
|
|
|
|
adds = left_top + [add_coefs] + left_bot
|
|
|
|
|
|
|
|
return Tree.from_list("+", adds)
|
|
|
|
|
|
|
|
@add.register(MOMonomial, MOpolynomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def momonomial_mopolynomial(left, right):
|
|
|
|
""" Add a monomial to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOMonomial(10, "x", 2)
|
|
|
|
>>> b = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 10
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
| > *
|
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
|
|
|
| > 2
|
|
|
|
>>> b = MOpolynomial("x", [2, 3, 4, 5])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 5
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 3
|
|
|
|
| > +
|
|
|
|
| | > *
|
|
|
|
| | | > 10
|
|
|
|
| | | > ^
|
|
|
|
| | | | > x
|
|
|
|
| | | | > 2
|
|
|
|
| | > *
|
|
|
|
| | | > 4
|
|
|
|
| | | > ^
|
|
|
|
| | | | > x
|
|
|
|
| | | | > 2
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
if left.power not in right.coefficients.keys():
|
|
|
|
raise NotImplementedError("No degree in common")
|
|
|
|
|
|
|
|
right_mono = right.monomials[left.power]
|
|
|
|
add_coefs = Tree("+", left, right_mono)
|
|
|
|
|
|
|
|
right_top = [mo for deg, mo in right.monomials.items() if deg > left.power][::-1]
|
|
|
|
right_bot = [mo for deg, mo in right.monomials.items() if deg < left.power][::-1]
|
|
|
|
|
|
|
|
adds = right_top + [add_coefs] + right_bot
|
|
|
|
|
|
|
|
return Tree.from_list("+", adds)
|
|
|
|
|
|
|
|
@add.register(MOpolynomial, MOMonomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mopolynomial_momonomial(left, right):
|
|
|
|
""" Add a monomial to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> b = MOMonomial(10, "x", 2)
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
| > *
|
|
|
|
| | > 10
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
|
|
|
| > 2
|
|
|
|
>>> a = MOpolynomial("x", [2, 3, 4, 5])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 5
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 3
|
|
|
|
| > +
|
|
|
|
| | > *
|
|
|
|
| | | > 4
|
|
|
|
| | | > ^
|
|
|
|
| | | | > x
|
|
|
|
| | | | > 2
|
|
|
|
| | > *
|
|
|
|
| | | > 10
|
|
|
|
| | | > ^
|
|
|
|
| | | | > x
|
|
|
|
| | | | > 2
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 3
|
|
|
|
| | > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
if right.power not in left.coefficients.keys():
|
|
|
|
raise NotImplementedError("No degree in common")
|
|
|
|
|
|
|
|
left_mono = left.monomials[right.power]
|
|
|
|
add_coefs = Tree("+", left_mono, right)
|
|
|
|
|
|
|
|
left_top = [mo for deg, mo in left.monomials.items() if deg > right.power][::-1]
|
|
|
|
left_bot = [mo for deg, mo in left.monomials.items() if deg < right.power][::-1]
|
|
|
|
|
|
|
|
adds = left_top + [add_coefs] + left_bot
|
|
|
|
|
|
|
|
return Tree.from_list("+", adds)
|
|
|
|
|
|
|
|
@add.register(MOpolynomial, MOpolynomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mopolynomial_mopolynomial(left, right):
|
|
|
|
""" Add a polynomial to a polynomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOpolynomial("x", [2, 3, 4])
|
|
|
|
>>> b = MOpolynomial("x", [5, 6, 7])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
| > *
|
|
|
|
| | > 7
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
> +
|
|
|
|
| > +
|
|
|
|
| | > *
|
|
|
|
| | | > 3
|
|
|
|
| | | > x
|
|
|
|
| | > *
|
|
|
|
| | | > 6
|
|
|
|
| | | > x
|
|
|
|
| > +
|
|
|
|
| | > 2
|
|
|
|
| | > 5
|
|
|
|
>>> b = MOpolynomial("x", [0, 3, 4])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
| > *
|
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
> +
|
|
|
|
| > +
|
|
|
|
| | > *
|
|
|
|
| | | > 3
|
|
|
|
| | | > x
|
|
|
|
| | > *
|
|
|
|
| | | > 3
|
|
|
|
| | | > x
|
|
|
|
| > 2
|
|
|
|
>>> b = MOpolynomial("x", [0, 3, 0, 5])
|
|
|
|
>>> print(add(a, b))
|
|
|
|
+
|
|
|
|
> +
|
|
|
|
| > *
|
|
|
|
| | > 5
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 3
|
|
|
|
| > *
|
|
|
|
| | > 4
|
|
|
|
| | > ^
|
|
|
|
| | | > x
|
|
|
|
| | | > 2
|
|
|
|
> +
|
|
|
|
| > +
|
|
|
|
| | > *
|
|
|
|
| | | > 3
|
|
|
|
| | | > x
|
|
|
|
| | > *
|
|
|
|
| | | > 3
|
|
|
|
| | | > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
common_degree = set(left.monomials.keys()).intersection(right.monomials.keys())
|
|
|
|
if not common_degree:
|
2018-11-14 09:04:27 +00:00
|
|
|
raise NotImplementedError("No degree in common, no calculus to do")
|
2018-11-13 13:42:40 +00:00
|
|
|
|
2018-11-13 15:19:02 +00:00
|
|
|
merge_monomials = {**left.monomials, **right.monomials}
|
|
|
|
for deg in common_degree:
|
|
|
|
merge_monomials[deg] = Tree("+", left.monomials[deg], right.monomials[deg])
|
2018-11-13 13:42:40 +00:00
|
|
|
|
2018-11-13 15:19:02 +00:00
|
|
|
return Tree.from_list("+", list(merge_monomials.values())[::-1])
|
2018-11-13 13:42:40 +00:00
|
|
|
|
2018-11-14 08:00:19 +00:00
|
|
|
@add.register(MOstr, MOMonomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mostr_momonomial(left, right):
|
|
|
|
""" Add a str to a Monomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOstr("x")
|
|
|
|
>>> b = MOMonomial(2, "x")
|
|
|
|
>>> print(add(a, b))
|
|
|
|
*
|
|
|
|
> +
|
|
|
|
| > 1
|
|
|
|
| > 2
|
|
|
|
> x
|
|
|
|
"""
|
|
|
|
if right.power != 1:
|
|
|
|
raise NotImplementedError("Monomial is more than deg 1")
|
|
|
|
add_scal = Tree("+", 1, right.coefficient)
|
|
|
|
return Tree("*", add_scal, left)
|
|
|
|
|
|
|
|
@add.register(MOMonomial, MOstr)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def momonomial_mostr(left, right):
|
|
|
|
""" Add a str to a Monomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOMonomial(2, "x")
|
|
|
|
>>> b = MOstr("x")
|
|
|
|
>>> print(add(a, b))
|
|
|
|
*
|
|
|
|
> +
|
|
|
|
| > 2
|
|
|
|
| > 1
|
|
|
|
> x
|
|
|
|
"""
|
|
|
|
if left.power != 1:
|
|
|
|
raise NotImplementedError("Monomial is more than deg 1")
|
|
|
|
add_scal = Tree("+", left.coefficient, 1)
|
|
|
|
return Tree("*", add_scal, right)
|
|
|
|
|
|
|
|
@add.register(MOstrPower, MOMonomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def mostrpower_momonomial(left, right):
|
|
|
|
""" Add a strpower to a Monomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOstrPower("x", 2)
|
|
|
|
>>> b = MOMonomial(3, "x", 2)
|
|
|
|
>>> print(add(a, b))
|
|
|
|
*
|
|
|
|
> +
|
|
|
|
| > 1
|
|
|
|
| > 3
|
|
|
|
> ^
|
|
|
|
| > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
if right.power != left.power:
|
|
|
|
raise NotImplementedError("MOs does not have same degree")
|
|
|
|
add_scal = Tree("+", 1, right.coefficient)
|
|
|
|
return Tree("*", add_scal, left)
|
|
|
|
|
|
|
|
@add.register(MOMonomial, MOstrPower)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def momonomial_mostrpower(left, right):
|
|
|
|
""" Add a strpower to a Monomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOMonomial(3, "x", 2)
|
|
|
|
>>> b = MOstrPower("x", 2)
|
|
|
|
>>> print(add(a, b))
|
|
|
|
*
|
|
|
|
> +
|
|
|
|
| > 3
|
|
|
|
| > 1
|
|
|
|
> ^
|
|
|
|
| > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
if left.power != right.power:
|
|
|
|
raise NotImplementedError("MOs does not have same degree")
|
|
|
|
add_scal = Tree("+", left.coefficient, 1)
|
|
|
|
return Tree("*", add_scal, right)
|
|
|
|
|
|
|
|
@add.register(MOMonomial, MOMonomial)
|
|
|
|
@special_case(add_filter)
|
|
|
|
def momonomial_momonomial(left, right):
|
|
|
|
""" Add a Monomial to a Monomial
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOMonomial(3, "x", 2)
|
|
|
|
>>> b = MOMonomial(4, "x", 2)
|
|
|
|
>>> print(add(a, b))
|
|
|
|
*
|
|
|
|
> +
|
|
|
|
| > 3
|
|
|
|
| > 4
|
|
|
|
> ^
|
|
|
|
| > x
|
|
|
|
| > 2
|
|
|
|
"""
|
|
|
|
if left.power != right.power:
|
|
|
|
raise NotImplementedError("MOs does not have same degree")
|
|
|
|
add_scal = Tree("+", left.coefficient, right.coefficient)
|
|
|
|
return Tree("*", add_scal, right.strpower)
|
|
|
|
|
|
|
|
|
2018-03-11 15:34:41 +00:00
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|