HI, I am trying to find a general expression for obtaining the exponents of a multidimensional polynomial of order orderand with n_variables, like the one that is presented in this reference in equation (3).
Here is my current code that uses a generator itertools.product.
def generalized_taylor_expansion_exponents( order, n_variables ):
"""
Find the exponents of a multivariate polynomial expression of order
`order` and `n_variable` number of variables.
"""
exps = (p for p in itertools.product(range(order+1), repeat=n_variables) if sum(p) <= order)
exps.next()
return exps
Desired Result:
for i in generalized_taylor_expansion_exponents(order=3, n_variables=3):
print i
(0, 0, 1)
(0, 0, 2)
(0, 0, 3)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(0, 2, 0)
(0, 2, 1)
(0, 3, 0)
(1, 0, 0)
(1, 0, 1)
(1, 0, 2)
(1, 1, 0)
(1, 1, 1)
(1, 2, 0)
(2, 0, 0)
(2, 0, 1)
(2, 1, 0)
(3, 0, 0)
In fact, this code runs quickly because the generator object is created only. If I want to populate the list with values ββfrom this generator, execution really starts to slow down, mainly due to the large number of calls to sum. Typical values ββfor orderand n_variablesare 5 and 10, respectively.
How can I significantly increase the speed of execution?
Thanks for any help.
Davide Lasagna