Feat(Compute): Add MOpolynomial with scalar

This commit is contained in:
Bertrand Benjamin 2018-11-13 14:42:40 +01:00
parent a32fd7cbc6
commit 17153dd345
3 changed files with 224 additions and 0 deletions

View File

@ -73,6 +73,10 @@ class MOpolynomial(MO):
def degree(self):
return self._power
@property
def coefficients(self):
return self._coefs
# -----------------------------
# Reglages pour 'vim'

View File

@ -16,6 +16,7 @@ from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial
from ..MO.polynomial import MOpolynomial
from .exceptions import AddError
from .arithmetic import lcm
from .filters import special_case
@ -236,6 +237,72 @@ def mostrpower_mostrpower(left, right):
raise NotImplementedError("Can't add 2 MOstrPower with not same power")
return MOMonomial(2, left.variable, left.power)
@add.register((MOnumber, MOFraction), MOpolynomial)
@special_case(add_filter)
def monumber_mofraction(left, right):
""" Add a scalar to a polynomial
:example:
>>> a = MOnumber(1)
>>> b = MOpolynomial("x", [2, 3, 4])
>>> print(add(a, b))
+
> +
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > *
| | > 3
| | > x
> +
| > 1
| > 2
"""
if 0 not in right.coefficients.keys():
raise NotImplementedError("Polynomial with no constant, no calculus to do")
right_const = right.coefficients[0]
right_other = {k:v for k,v in right.coefficients.items() if k != 0}
right_remain = MOpolynomial(right.variable, right_other)
add_consts = Tree("+", left, right_const)
return Tree('+', right_remain, add_consts)
@add.register(MOpolynomial, (MOnumber, MOFraction))
@special_case(add_filter)
def monumber_mofraction(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
> +
| > 1
| > 2
"""
if 0 not in left.coefficients.keys():
raise NotImplementedError("Polynomial with no constant, no calculus to do")
left_const = left.coefficients[0]
left_other = {k:v for k,v in left.coefficients.items() if k != 0}
left_remain = MOpolynomial(left.variable, left_other)
add_consts = Tree("+", right, left_const)
return Tree('+', left_remain, add_consts)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -0,0 +1,153 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
Add MO with typing
"""
from multipledispatch import Dispatcher
from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.monomial import MOstrPower, MOMonomial
from ..MO.polynomial import MOpolynomial
from ..MO.fraction import MOFraction
add_doc = """ Add MOs
:param left: left MO
:param right: right MO
:returns: Tree or MO
"""
add = Dispatcher("add", doc=add_doc)
@add.register((MOnumber, MOFraction), MOstr)
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
>>> a = MOnumber(2)
>>> b = MOstr('x')
>>> add(a, b)
<MOpolynomial x + 2>
>>> a = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial x + 1 / 5>
"""
return MOpolynomial(right, [left, 1])
@add.register(MOstr, (MOnumber, MOFraction))
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
>>> a = MOstr('x')
>>> b = MOnumber(2)
>>> add(a, b)
<MOpolynomial x + 2>
>>> b = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial x + 1 / 5>
"""
return MOpolynomial(left, [right, 1])
@add.register((MOnumber, MOFraction), MOstrPower)
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
>>> a = MOnumber(2)
>>> b = MOstrPower('x', 3)
>>> add(a, b)
<MOpolynomial x^3 + 2>
>>> a = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial x^3 + 1 / 5>
"""
return MOpolynomial(right.variable, {0: left, right.power: 1})
@add.register(MOstrPower, (MOnumber, MOFraction))
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
>>> a = MOstrPower('x', 3)
>>> b = MOnumber(2)
>>> add(a, b)
<MOpolynomial x^3 + 2>
>>> b = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial x^3 + 1 / 5>
"""
return MOpolynomial(left.variable, {0: right, left.power: 1})
@add.register((MOnumber, MOFraction), MOMonomial)
def moscalar_mostr(left, right):
""" add a scalar with a MOMonomial to create a MOpolynomial
>>> a = MOnumber(2)
>>> b = MOMonomial(3, 'x', 4)
>>> add(a, b)
<MOpolynomial 3x^4 + 2>
>>> a = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial 3x^4 + 1 / 5>
"""
return MOpolynomial(right.variable,
{0: left, right.power: right.coefficient}
)
@add.register(MOMonomial, (MOnumber, MOFraction))
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
>>> a = MOMonomial(3, 'x', 4)
>>> b = MOnumber(2)
>>> add(a, b)
<MOpolynomial 3x^4 + 2>
>>> b = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial 3x^4 + 1 / 5>
"""
return MOpolynomial(left.variable,
{0: right, left.power: left.coeffient}
)
@add.register(MOstr, MOstrPower)
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
>>> a = MOstr('x')
>>> b = MOstrPower('x', 3)
>>> add(a, b)
<MOpolynomial x^3 + x>
>>> b = MOstrPower('x', 2)
>>> add(a, b)
<MOpolynomial x^2 + x>
"""
if left != right.variable:
raise
return MOpolynomial(left , {1: 1, right.power: 1})
@add.register(MOstrPower, MOstr)
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
>>> a = MOstrPower('x', 3)
>>> b = MOstr('x')
>>> add(a, b)
<MOpolynomial x^3 + x>
>>> a = MOstrPower('x', 2)
>>> add(a, b)
<MOpolynomial x^2 + x>
"""
if right != left.variable:
raise
return MOpolynomial(right , {1: 1, left.power: 1})
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del