我正在尝试计算最终索引不同的乘积,但没有得到我期望的结果:
from sympy.abc import n, k
from sympy import IndexedBase, Product
g = IndexedBase('g')
gg = {g[0]: 1, g[1]: 2, g[2]: 3, g[3]: 4, g[4]: 5, g[5]: 6}
my_product = Product(g[n]**-1, (n, 0, k-1))
result = list()
for i in range(len(gg)):
s = {k: i, **gg} # substitions
result.append(my_product.evalf(subs=s))
print(result)
结果是:
[1.00000000000000, 1.00000000000000, 1.00000000000000, 1.00000000000000, 1.00000000000000, 1.00000000000000]
预期结果是:
[1.00000000000000, 1.00000000000000, 0.500000000000000, 0.166666666666667, 0.0416666666666667, 0.00833333333333333]
答案 0 :(得分:1)
我不确定这里应该执行什么操作,但是您可以得到我认为想要的结果
result.append(my_product.subs(k, i).doit().subs(gg))
有了,我得到:
[1, 1, 1/2, 1/6, 1/24, 1/120]