やあ、次数の多変量多項式の指数を取得するための一般的な式を見つけようとしてorder
います。n_variables
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)
# discard the first element, which is all zeros..
exps.next()
return exps
望ましいアウトはこれです:
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)
実際には、ジェネレーター オブジェクトが作成されるだけなので、このコードは高速に実行されます。このジェネレーターからの値でリストを埋めたい場合、主に への呼び出し回数が多いため、実行が実際に遅くなり始めますsum
。との典型的な値はorder
、n_variables
それぞれ 5 と 10 です。
どうすれば実行速度を大幅に改善できますか?
助けてくれてありがとう。
ダヴィデ・ラザニア