Skip to content
Snippets Groups Projects
Commit 5d87aa26 authored by Martin Bauer's avatar Martin Bauer
Browse files

More robust moment specification (now supports non-rational coefficients)

parent e8a2a7ac
Branches
Tags
No related merge requests found
......@@ -195,18 +195,20 @@ def polynomialToExponentRepresentation(polynomial, dim=3):
x, y, z = MOMENT_SYMBOLS
polynomial = polynomial.expand()
coeffExpTupleRepresentation = []
for expr, coefficient in polynomial.as_coefficients_dict().items():
summands = [polynomial] if polynomial.func != sp.Add else polynomial.args
for expr in summands:
if len(expr.atoms(sp.Symbol) - set(MOMENT_SYMBOLS)) > 0:
raise ValueError("Invalid moment polynomial: " + str(expr))
x_exp, y_exp, z_exp = sp.Wild('xexp'), sp.Wild('yexp'), sp.Wild('zc')
matchRes = expr.match(x**x_exp * y**y_exp * z**z_exp)
c, x_exp, y_exp, z_exp = sp.Wild('c'), sp.Wild('xexp'), sp.Wild('yexp'), sp.Wild('zc')
matchRes = expr.match(c * x**x_exp * y**y_exp * z**z_exp)
assert matchRes[x_exp].is_integer and matchRes[y_exp].is_integer and matchRes[z_exp].is_integer
expTuple = (int(matchRes[x_exp]), int(matchRes[y_exp]), int(matchRes[z_exp]),)
if dim < 3:
for i in range(dim, 3):
assert expTuple[i] == 0, "Used symbols in polynomial are not representable in that dimension"
expTuple = expTuple[:dim]
coeffExpTupleRepresentation.append((coefficient, expTuple))
coeffExpTupleRepresentation.append((matchRes[c], expTuple))
return coeffExpTupleRepresentation
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment