0
votes

I have to characterize a very large polynomial expression of many variables (x1,x2,....,xn) as follow:

  • the power of each of the monomial terms forms an ordered tuple which will be saved as the keys of a dictionary.
  • the coefficient of that monomic expression is saved as the value of that particular key.

Example: if p=Poly(3*x1**2 - x1*x2 + 5*x2 +7)

dic={"(2,0)":3 , "(1,1)":-1 , "(0,1)": 5 , "(0,0)": 7}

It is very important that the first component of the tuple always be the power associated with x1 and if I run the same code again with a new poly with no x1 variable, it put a zero in its first component.

1

1 Answers

0
votes

The Poly method as_dict does that, if you specify the variables and their order as the second argument of Poly, for example Poly(..., (x1, x2)). Assuming the symbols x1, x2 were already created, this works as follows.

>>> p = Poly(3*x1**2 - x1*x2 + 5*x2 + 7, (x1, x2))
>>> p.as_dict()
{(0, 0): 7, (0, 1): 5, (1, 1): -1, (2, 0): 3}
>>> p = Poly(x2 + 5*x2 + 7, (x1, x2))
>>> p.as_dict()
{(0, 0): 7, (0, 1): 6}