Style: apply black
This commit is contained in:
@@ -25,15 +25,10 @@ from ..MO.polynomial import MOpolynomial
|
||||
from itertools import product
|
||||
from tabulate import tabulate
|
||||
|
||||
MOS = [ MOnumber, MOstr, MOFraction, MOstrPower, MOMonomial, MOpolynomial]
|
||||
MOS = [MOnumber, MOstr, MOFraction, MOstrPower, MOMonomial, MOpolynomial]
|
||||
|
||||
OPERATIONS = {"+": add, "-": minus, "*": multiply, "/": divide, "^": power}
|
||||
|
||||
OPERATIONS = {
|
||||
"+": add,
|
||||
"-": minus,
|
||||
"*": multiply,
|
||||
"/": divide,
|
||||
"^": power,
|
||||
}
|
||||
|
||||
def compute(node, left_v, right_v):
|
||||
"""
|
||||
@@ -60,6 +55,7 @@ def compute(node, left_v, right_v):
|
||||
|
||||
return operation(left_v, right_v)
|
||||
|
||||
|
||||
def compute_capacities(node):
|
||||
""" Test an operation through all MOs
|
||||
|
||||
@@ -72,21 +68,19 @@ def compute_capacities(node):
|
||||
|
||||
"""
|
||||
op = OPERATIONS[node]
|
||||
lines = [[node] + [mo.__name__ for mo in MOS]]
|
||||
lines = [[node] + [mo.__name__ for mo in MOS]]
|
||||
for left_mo in MOS:
|
||||
lines.append(
|
||||
[left_mo.__name__] + \
|
||||
[(left_mo, i) in op.funcs for i in MOS]
|
||||
)
|
||||
lines.append([left_mo.__name__] + [(left_mo, i) in op.funcs for i in MOS])
|
||||
return lines
|
||||
|
||||
|
||||
def describe_compute():
|
||||
""" Explain which operation are handle by compue """
|
||||
|
||||
ans = "Implemented compute operations among MOs"
|
||||
for op in OPERATIONS:
|
||||
ans += "\n"
|
||||
ans += tabulate(compute_capacities(op), tablefmt='grid')
|
||||
ans += tabulate(compute_capacities(op), tablefmt="grid")
|
||||
return ans
|
||||
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ add_doc = """ Adding MOs
|
||||
|
||||
add = Dispatcher("add", doc=add_doc)
|
||||
|
||||
|
||||
def add_filter(left, right):
|
||||
""" Automatic add on MO
|
||||
|
||||
@@ -59,6 +60,7 @@ def add_filter(left, right):
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
@add.register(MOnumber, MOnumber)
|
||||
@special_case(add_filter)
|
||||
def monumber_monumber(left, right):
|
||||
@@ -72,6 +74,7 @@ def monumber_monumber(left, right):
|
||||
"""
|
||||
return MO.factory(left.value + right.value)
|
||||
|
||||
|
||||
@add.register(MOnumber, MOFraction)
|
||||
@special_case(add_filter)
|
||||
def monumber_mofraction(left, right):
|
||||
@@ -87,6 +90,7 @@ def monumber_mofraction(left, right):
|
||||
left_fraction = MOFraction(left, MOnumber(1))
|
||||
return Tree("+", left_fraction, right)
|
||||
|
||||
|
||||
@add.register(MOFraction, MOnumber)
|
||||
@special_case(add_filter)
|
||||
def mofraction_monumber(left, right):
|
||||
@@ -103,6 +107,7 @@ def mofraction_monumber(left, right):
|
||||
right_fraction = MOFraction(right, MOnumber(1))
|
||||
return Tree("+", left, right_fraction)
|
||||
|
||||
|
||||
@add.register(MOFraction, MOFraction)
|
||||
@special_case(add_filter)
|
||||
def mofraction_mofraction(left, right):
|
||||
@@ -200,6 +205,7 @@ def mofraction_mofraction(left, right):
|
||||
|
||||
return Tree("+", left_frac, right_frac)
|
||||
|
||||
|
||||
@add.register(MOstr, MOstr)
|
||||
@special_case(add_filter)
|
||||
def mostr_mostr(left, right):
|
||||
@@ -215,6 +221,7 @@ def mostr_mostr(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):
|
||||
@@ -230,6 +237,7 @@ 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 moscalar_mopolynomial(left, right):
|
||||
@@ -254,7 +262,8 @@ def moscalar_mopolynomial(left, right):
|
||||
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)
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOpolynomial, (MOnumber, MOFraction))
|
||||
@special_case(add_filter)
|
||||
@@ -280,7 +289,8 @@ def mopolynomial_moscalar(left, right):
|
||||
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)
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOstr, MOpolynomial)
|
||||
@special_case(add_filter)
|
||||
@@ -313,6 +323,7 @@ def mostr_mopolynomial(left, right):
|
||||
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOpolynomial, MOstr)
|
||||
@special_case(add_filter)
|
||||
def mopolynomial_mostr(left, right):
|
||||
@@ -344,6 +355,7 @@ def mopolynomial_mostr(left, right):
|
||||
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOstrPower, MOpolynomial)
|
||||
@special_case(add_filter)
|
||||
def mostrpower_mopolynomial(left, right):
|
||||
@@ -387,6 +399,7 @@ def mostrpower_mopolynomial(left, right):
|
||||
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOpolynomial, MOstrPower)
|
||||
@special_case(add_filter)
|
||||
def mopolynomial_mostrpower(left, right):
|
||||
@@ -431,6 +444,7 @@ def mopolynomial_mostrpower(left, right):
|
||||
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOMonomial, MOpolynomial)
|
||||
@special_case(add_filter)
|
||||
def momonomial_mopolynomial(left, right):
|
||||
@@ -474,6 +488,7 @@ def momonomial_mopolynomial(left, right):
|
||||
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOpolynomial, MOMonomial)
|
||||
@special_case(add_filter)
|
||||
def mopolynomial_momonomial(left, right):
|
||||
@@ -517,6 +532,7 @@ def mopolynomial_momonomial(left, right):
|
||||
|
||||
return Tree.from_list("+", adds)
|
||||
|
||||
|
||||
@add.register(MOpolynomial, MOpolynomial)
|
||||
@special_case(add_filter)
|
||||
def mopolynomial_mopolynomial(left, right):
|
||||
@@ -563,7 +579,7 @@ def mopolynomial_mopolynomial(left, right):
|
||||
| > 2
|
||||
|
||||
"""
|
||||
common_degree = set(left.monomials.keys()).intersection(right.monomials.keys())
|
||||
common_degree = set(left.monomials.keys()).intersection(right.monomials.keys())
|
||||
if not common_degree:
|
||||
raise NotImplementedError("No degree in common, no calculus to do")
|
||||
|
||||
@@ -573,6 +589,7 @@ def mopolynomial_mopolynomial(left, right):
|
||||
|
||||
return Tree.from_list("+", list(merge_monomials.values())[::-1])
|
||||
|
||||
|
||||
@add.register(MOstr, MOMonomial)
|
||||
@special_case(add_filter)
|
||||
def mostr_momonomial(left, right):
|
||||
@@ -593,6 +610,7 @@ def mostr_momonomial(left, right):
|
||||
add_scal = Tree("+", 1, right.coefficient)
|
||||
return Tree("*", add_scal, left)
|
||||
|
||||
|
||||
@add.register(MOMonomial, MOstr)
|
||||
@special_case(add_filter)
|
||||
def momonomial_mostr(left, right):
|
||||
@@ -613,6 +631,7 @@ def momonomial_mostr(left, right):
|
||||
add_scal = Tree("+", left.coefficient, 1)
|
||||
return Tree("*", add_scal, right)
|
||||
|
||||
|
||||
@add.register(MOstrPower, MOMonomial)
|
||||
@special_case(add_filter)
|
||||
def mostrpower_momonomial(left, right):
|
||||
@@ -633,6 +652,7 @@ def mostrpower_momonomial(left, right):
|
||||
add_scal = Tree("+", 1, right.coefficient)
|
||||
return Tree("*", add_scal, left)
|
||||
|
||||
|
||||
@add.register(MOMonomial, MOstrPower)
|
||||
@special_case(add_filter)
|
||||
def momonomial_mostrpower(left, right):
|
||||
@@ -653,6 +673,7 @@ def momonomial_mostrpower(left, right):
|
||||
add_scal = Tree("+", left.coefficient, 1)
|
||||
return Tree("*", add_scal, right)
|
||||
|
||||
|
||||
@add.register(MOMonomial, MOMonomial)
|
||||
@special_case(add_filter)
|
||||
def momonomial_momonomial(left, right):
|
||||
|
||||
@@ -28,6 +28,7 @@ divide_doc = """ Dividing MOs
|
||||
|
||||
divide = Dispatcher("divide", doc=divide_doc)
|
||||
|
||||
|
||||
def divide_filter(left, right):
|
||||
""" Automatic divide on MO
|
||||
|
||||
@@ -62,6 +63,7 @@ def divide_filter(left, right):
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
|
||||
@divide.register(MOnumber, MOnumber)
|
||||
@special_case(divide_filter)
|
||||
def monumber_monumber(left, right):
|
||||
@@ -78,11 +80,13 @@ def monumber_monumber(left, right):
|
||||
...
|
||||
NotImplementedError: Can't divide 2 int. Need to create a Fraction instead
|
||||
"""
|
||||
if type(left.value) in [float, Decimal] or \
|
||||
type(right.value) in [float, Decimal]:
|
||||
if type(left.value) in [float, Decimal] or type(right.value) in [float, Decimal]:
|
||||
return MO.factory(left.value / right.value)
|
||||
else:
|
||||
raise NotImplementedError("Can't divide 2 int. Need to create a Fraction instead")
|
||||
raise NotImplementedError(
|
||||
"Can't divide 2 int. Need to create a Fraction instead"
|
||||
)
|
||||
|
||||
|
||||
@divide.register(MOnumber, MOFraction)
|
||||
@special_case(divide_filter)
|
||||
@@ -103,6 +107,7 @@ def monumber_mofraction(left, right):
|
||||
"""
|
||||
return Tree("*", left, right.inverse())
|
||||
|
||||
|
||||
@divide.register(MOFraction, MOnumber)
|
||||
@special_case(divide_filter)
|
||||
def mofraction_monumber(left, right):
|
||||
@@ -119,6 +124,7 @@ def mofraction_monumber(left, right):
|
||||
right_fraction = MOFraction(MOnumber(1), right)
|
||||
return Tree("*", left, right_fraction)
|
||||
|
||||
|
||||
@divide.register(MOFraction, MOFraction)
|
||||
@special_case(divide_filter)
|
||||
def mofraction_mofraction(left, right):
|
||||
@@ -134,6 +140,7 @@ def mofraction_mofraction(left, right):
|
||||
"""
|
||||
return Tree("*", left, right.inverse())
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
||||
@@ -10,18 +10,23 @@
|
||||
Exceptions for computing
|
||||
"""
|
||||
|
||||
|
||||
class ComputeError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AddError(ComputeError):
|
||||
pass
|
||||
|
||||
|
||||
class MinusError(ComputeError):
|
||||
pass
|
||||
|
||||
|
||||
class MultiplyError(ComputeError):
|
||||
pass
|
||||
|
||||
|
||||
class DivideError(ComputeError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ Decorator to filter MO before operate
|
||||
from functools import wraps
|
||||
from .exceptions import ComputeError
|
||||
|
||||
|
||||
def args_are(left_type, right_type):
|
||||
""" Decorator which filter arguments type
|
||||
|
||||
@@ -21,21 +22,27 @@ def args_are(left_type, right_type):
|
||||
:returns: a decorator which will allow only some type
|
||||
|
||||
"""
|
||||
|
||||
def type_filter(func):
|
||||
@wraps(func)
|
||||
def filtered_func(left, right):
|
||||
if not isinstance(left, left_type):
|
||||
raise ComputeError("Wrong type for left argument"
|
||||
f"Require {left_type}, got {left.__class__.__name__}"
|
||||
)
|
||||
raise ComputeError(
|
||||
"Wrong type for left argument"
|
||||
f"Require {left_type}, got {left.__class__.__name__}"
|
||||
)
|
||||
if not isinstance(right, right_type):
|
||||
raise ComputeError("Wrong type for right argument"
|
||||
f"Require {right_type}, got {right.__class__.__name__}"
|
||||
)
|
||||
raise ComputeError(
|
||||
"Wrong type for right argument"
|
||||
f"Require {right_type}, got {right.__class__.__name__}"
|
||||
)
|
||||
return func(left, right)
|
||||
|
||||
return filtered_func
|
||||
|
||||
return type_filter
|
||||
|
||||
|
||||
def special_case(filter):
|
||||
""" Decorate operation to filter special cases before call the function
|
||||
|
||||
@@ -43,6 +50,7 @@ def special_case(filter):
|
||||
:returns: decorator
|
||||
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
@wraps(func)
|
||||
def _func(left, right):
|
||||
@@ -50,9 +58,12 @@ def special_case(filter):
|
||||
if ans is None:
|
||||
return func(left, right)
|
||||
return ans
|
||||
|
||||
return _func
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
||||
@@ -28,6 +28,7 @@ minus_doc = """ Opposite of a MO
|
||||
|
||||
minus = Dispatcher("minus", doc=minus_doc)
|
||||
|
||||
|
||||
@minus.register(type(None), MOnumber)
|
||||
def monumber(_, right):
|
||||
"""
|
||||
@@ -37,7 +38,8 @@ def monumber(_, right):
|
||||
<MOnumber - 4>
|
||||
|
||||
"""
|
||||
return MO.factory(- right.value)
|
||||
return MO.factory(-right.value)
|
||||
|
||||
|
||||
@minus.register(type(None), MOFraction)
|
||||
def mofraction(_, right):
|
||||
@@ -84,6 +86,7 @@ def mofraction(_, right):
|
||||
|
||||
return MOFraction(right._numerator, right._denominator, True)
|
||||
|
||||
|
||||
@minus.register(type(None), MOstr)
|
||||
def mostr(_, right):
|
||||
""" Opposite of 'x' is '-x'
|
||||
@@ -95,6 +98,7 @@ def mostr(_, right):
|
||||
"""
|
||||
return MOMonomial(-1, right)
|
||||
|
||||
|
||||
@minus.register(type(None), MOstrPower)
|
||||
def mostrpower(_, right):
|
||||
""" Opposite of 'x^n' is '-x^n'
|
||||
@@ -106,6 +110,7 @@ def mostrpower(_, right):
|
||||
"""
|
||||
return MOMonomial(-1, right.variable, right.power)
|
||||
|
||||
|
||||
@minus.register(type(None), MOMonomial)
|
||||
def momonomial(_, right):
|
||||
""" Opposite of 'ax^n' is '-ax^n'
|
||||
@@ -121,6 +126,7 @@ def momonomial(_, right):
|
||||
coef = Tree("-", None, right.coefficient)
|
||||
return Tree("*", coef, right.strpower)
|
||||
|
||||
|
||||
@minus.register(type(None), MOpolynomial)
|
||||
def mopolynomial(_, right):
|
||||
""" Opposite of a polynomial
|
||||
@@ -133,6 +139,7 @@ def mopolynomial(_, right):
|
||||
neg_coefs = {p: -c.value for (p, c) in right.coefficients.items()}
|
||||
return MOpolynomial(right.variable, neg_coefs)
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
||||
@@ -28,6 +28,7 @@ multiply_doc = """ Multiply MOs
|
||||
|
||||
multiply = Dispatcher("multiply", doc=multiply_doc)
|
||||
|
||||
|
||||
def multiply_filter(left, right):
|
||||
""" Automatic multiply on MO
|
||||
|
||||
@@ -68,6 +69,7 @@ def multiply_filter(left, right):
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
|
||||
@multiply.register(MOnumber, MOnumber)
|
||||
@special_case(multiply_filter)
|
||||
def monumber_monumber(left, right):
|
||||
@@ -81,6 +83,7 @@ def monumber_monumber(left, right):
|
||||
"""
|
||||
return MO.factory(left.value * right.value)
|
||||
|
||||
|
||||
@multiply.register(MOnumber, MOFraction)
|
||||
@special_case(multiply_filter)
|
||||
def monumber_mofraction(left, right):
|
||||
@@ -108,6 +111,7 @@ def monumber_mofraction(left, right):
|
||||
num = Tree("*", left, right.numerator)
|
||||
return Tree("/", num, right._denominator)
|
||||
|
||||
|
||||
@multiply.register(MOFraction, MOnumber)
|
||||
@special_case(multiply_filter)
|
||||
def mofraction_monumber(left, right):
|
||||
@@ -125,6 +129,7 @@ def mofraction_monumber(left, right):
|
||||
num = Tree("*", left.numerator, right)
|
||||
return Tree("/", num, left._denominator)
|
||||
|
||||
|
||||
@multiply.register(MOFraction, MOFraction)
|
||||
@special_case(multiply_filter)
|
||||
def mofraction_mofraction(left, right):
|
||||
@@ -145,6 +150,7 @@ def mofraction_mofraction(left, right):
|
||||
denom = Tree("*", left.denominator, right.denominator)
|
||||
return Tree("/", num, denom)
|
||||
|
||||
|
||||
@multiply.register((MOnumber, MOFraction), MOMonomial)
|
||||
@special_case(multiply_filter)
|
||||
def moscalar_monomonial(left, right):
|
||||
@@ -161,8 +167,9 @@ def moscalar_monomonial(left, right):
|
||||
> x^4
|
||||
|
||||
"""
|
||||
coefficient = Tree('*', left, right.coefficient)
|
||||
return Tree('*', coefficient, right.strpower)
|
||||
coefficient = Tree("*", left, right.coefficient)
|
||||
return Tree("*", coefficient, right.strpower)
|
||||
|
||||
|
||||
@multiply.register(MOMonomial, (MOnumber, MOFraction))
|
||||
@special_case(multiply_filter)
|
||||
@@ -180,8 +187,9 @@ def monomonial_moscalar(left, right):
|
||||
> x^4
|
||||
|
||||
"""
|
||||
coefficient = Tree('*', right, left.coefficient)
|
||||
return Tree('*', coefficient, left.strpower)
|
||||
coefficient = Tree("*", right, left.coefficient)
|
||||
return Tree("*", coefficient, left.strpower)
|
||||
|
||||
|
||||
@multiply.register(MOstr, MOstrPower)
|
||||
@special_case(multiply_filter)
|
||||
@@ -200,9 +208,12 @@ def mostr_mostrpower(left, right):
|
||||
NotImplementedError: Can't multiply MOstr and MOstrPower if they don'thave same variable (got x and y)
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOstr and MOstrPower if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
return MOstrPower(left.variable, right.power.value+1)
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOstr and MOstrPower if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})"
|
||||
)
|
||||
return MOstrPower(left.variable, right.power.value + 1)
|
||||
|
||||
|
||||
@multiply.register(MOstrPower, MOstr)
|
||||
@special_case(multiply_filter)
|
||||
@@ -221,9 +232,12 @@ def mostr_mostrpower(left, right):
|
||||
NotImplementedError: Can't multiply MOstr and MOstrPower if they don'thave same variable (got x and y)
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("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.value+1)
|
||||
raise NotImplementedError(
|
||||
"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.value + 1)
|
||||
|
||||
|
||||
@multiply.register(MOstr, MOstr)
|
||||
@special_case(multiply_filter)
|
||||
@@ -243,10 +257,13 @@ def mostr_mostr(left, right):
|
||||
NotImplementedError: Can't multiply MOstr and MOstr if they don'thave same variable (got y and x)
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOstr and MOstr if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOstr and MOstr if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})"
|
||||
)
|
||||
return MOstrPower(left.variable, 2)
|
||||
|
||||
|
||||
@multiply.register(MOstrPower, MOstrPower)
|
||||
@special_case(multiply_filter)
|
||||
def mostr_mostrpower(left, right):
|
||||
@@ -268,11 +285,14 @@ def mostr_mostrpower(left, right):
|
||||
NotImplementedError: Can't multiply MOstrPower and MOstrPower if they don'thave same variable (got x and y)
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOstrPower and MOstrPower if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOstrPower 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)
|
||||
|
||||
|
||||
@multiply.register(MOstrPower, MOMonomial)
|
||||
@special_case(multiply_filter)
|
||||
def mostrpower_momonomial(left, right):
|
||||
@@ -296,12 +316,15 @@ def mostrpower_momonomial(left, right):
|
||||
NotImplementedError: Can't multiply MOstrPower and Monomial if they don'thave same variable (got x and y)
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOstrPower and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOstrPower and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})"
|
||||
)
|
||||
power = Tree("+", left.power, right.power)
|
||||
monome = Tree("^", left.variable, power)
|
||||
return Tree("*", right.coefficient, monome)
|
||||
|
||||
|
||||
@multiply.register(MOMonomial, MOstrPower)
|
||||
@special_case(multiply_filter)
|
||||
def momonomial_mostr(left, right):
|
||||
@@ -326,12 +349,15 @@ def momonomial_mostr(left, right):
|
||||
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOstrPower and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOstrPower and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})"
|
||||
)
|
||||
power = Tree("+", left.power, right.power)
|
||||
monome = Tree("^", left.variable, power)
|
||||
return Tree("*", left.coefficient, monome)
|
||||
|
||||
|
||||
@multiply.register(MOstr, MOMonomial)
|
||||
@special_case(multiply_filter)
|
||||
def mostr_momonomial(left, right):
|
||||
@@ -349,9 +375,12 @@ def mostr_momonomial(left, right):
|
||||
NotImplementedError: Can't multiply MOstr and Monomial if they don'thave same variable (got x and y)
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOstr and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
return MOMonomial(right.coefficient, right.variable, right.power.value+1)
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOstr and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})"
|
||||
)
|
||||
return MOMonomial(right.coefficient, right.variable, right.power.value + 1)
|
||||
|
||||
|
||||
@multiply.register(MOMonomial, MOstr)
|
||||
@special_case(multiply_filter)
|
||||
@@ -371,9 +400,12 @@ def momonomial_mostr(left, right):
|
||||
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOstr and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
return MOMonomial(left.coefficient, left.variable, left.power.value+1)
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOstr and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})"
|
||||
)
|
||||
return MOMonomial(left.coefficient, left.variable, left.power.value + 1)
|
||||
|
||||
|
||||
@multiply.register(MOMonomial, MOMonomial)
|
||||
@special_case(multiply_filter)
|
||||
@@ -401,15 +433,17 @@ def momonomial_momonomial(left, right):
|
||||
|
||||
"""
|
||||
if left.variable != right.variable:
|
||||
raise NotImplementedError("Can't multiply MOMonomial and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})")
|
||||
raise NotImplementedError(
|
||||
"Can't multiply MOMonomial and Monomial if they don't"
|
||||
f"have same variable (got {left.variable} and {right.variable})"
|
||||
)
|
||||
powers = Tree("+", left.power, right.power)
|
||||
monome = Tree("^", left.variable, powers)
|
||||
coefs = Tree("*", left.coefficient, right.coefficient)
|
||||
return Tree("*", coefs, monome)
|
||||
|
||||
@multiply.register((MOnumber, MOFraction, MOstr, MOstrPower, MOMonomial), \
|
||||
MOpolynomial)
|
||||
|
||||
@multiply.register((MOnumber, MOFraction, MOstr, MOstrPower, MOMonomial), MOpolynomial)
|
||||
@special_case(multiply_filter)
|
||||
def lotsmo_mopolynomial(left, right):
|
||||
""" Multiply a scalar and a MOMonomial
|
||||
@@ -492,13 +526,11 @@ def lotsmo_mopolynomial(left, right):
|
||||
|
||||
|
||||
"""
|
||||
coefs = [Tree("*", left, monom) \
|
||||
for monom in list(right.monomials.values())[::-1]\
|
||||
]
|
||||
coefs = [Tree("*", left, monom) for monom in list(right.monomials.values())[::-1]]
|
||||
return Tree.from_list("+", coefs)
|
||||
|
||||
@multiply.register(MOpolynomial, \
|
||||
(MOnumber, MOFraction, MOstr, MOstrPower, MOMonomial))
|
||||
|
||||
@multiply.register(MOpolynomial, (MOnumber, MOFraction, MOstr, MOstrPower, MOMonomial))
|
||||
@special_case(multiply_filter)
|
||||
def mopolynomial_lotsmo(left, right):
|
||||
""" Multiply a MOpolynomial with nearly everything
|
||||
@@ -581,11 +613,10 @@ def mopolynomial_lotsmo(left, right):
|
||||
| | > 3x^2
|
||||
|
||||
"""
|
||||
coefs = [Tree("*", monom, right) \
|
||||
for monom in list(left.monomials.values())[::-1] \
|
||||
]
|
||||
coefs = [Tree("*", monom, right) for monom in list(left.monomials.values())[::-1]]
|
||||
return Tree.from_list("+", coefs)
|
||||
|
||||
|
||||
@multiply.register(MOpolynomial, MOpolynomial)
|
||||
@special_case(multiply_filter)
|
||||
def mopolynomial_mopolynomial(left, right):
|
||||
@@ -619,10 +650,11 @@ def mopolynomial_mopolynomial(left, right):
|
||||
| | | > 4
|
||||
|
||||
"""
|
||||
coefs = [Tree("*", l_monom, r_monom) \
|
||||
for l_monom in list(left.monomials.values())[::-1] \
|
||||
for r_monom in list(right.monomials.values())[::-1] \
|
||||
]
|
||||
coefs = [
|
||||
Tree("*", l_monom, r_monom)
|
||||
for l_monom in list(left.monomials.values())[::-1]
|
||||
for r_monom in list(right.monomials.values())[::-1]
|
||||
]
|
||||
return Tree.from_list("+", coefs)
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ power_doc = """ Power of MOs
|
||||
|
||||
power = Dispatcher("power", doc=power_doc)
|
||||
|
||||
|
||||
def power_filter(left, right):
|
||||
""" Automatic power on MO
|
||||
|
||||
@@ -60,6 +61,7 @@ def power_filter(left, right):
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
|
||||
@power.register(MOnumber, MOnumber)
|
||||
@special_case(power_filter)
|
||||
def monumber_monumber(left, right):
|
||||
@@ -73,6 +75,7 @@ def monumber_monumber(left, right):
|
||||
"""
|
||||
return MO.factory(left.value ** right.value)
|
||||
|
||||
|
||||
@power.register(MOFraction, MOnumber)
|
||||
@special_case(power_filter)
|
||||
def mofraction_monumber(left, right):
|
||||
@@ -93,6 +96,7 @@ def mofraction_monumber(left, right):
|
||||
denom = Tree("^", left.denominator, right)
|
||||
return Tree("/", num, denom)
|
||||
|
||||
|
||||
@power.register(MOstrPower, MOnumber)
|
||||
@special_case(power_filter)
|
||||
def mostrpower_monumber(left, right):
|
||||
@@ -110,6 +114,7 @@ def mostrpower_monumber(left, right):
|
||||
power = Tree("*", left.power, right)
|
||||
return Tree("^", left.variable, power)
|
||||
|
||||
|
||||
@power.register(MOMonomial, MOnumber)
|
||||
@special_case(power_filter)
|
||||
def mostrpower_monumber(left, right):
|
||||
@@ -133,6 +138,7 @@ def mostrpower_monumber(left, right):
|
||||
strpower = Tree("^", left.variable, power)
|
||||
return Tree("*", coef, strpower)
|
||||
|
||||
|
||||
@power.register(MOpolynomial, MOnumber)
|
||||
@special_case(power_filter)
|
||||
def mopolynomial_monumber(left, right):
|
||||
@@ -145,8 +151,7 @@ def mopolynomial_monumber(left, right):
|
||||
> 3x^2 - 2x + 1
|
||||
> 3x^2 - 2x + 1
|
||||
"""
|
||||
return Tree.from_list("*", [left]*right.value)
|
||||
|
||||
return Tree.from_list("*", [left] * right.value)
|
||||
|
||||
|
||||
# -----------------------------
|
||||
|
||||
Reference in New Issue
Block a user