寻求总和(西格玛)功能的帮助

时间:2017-03-09 22:24:10

标签: python numpy sum

需要帮助来计算:

function

因此,y的总数等于x的数量,并且每个y用一个x和几个a计算。 我的代码列表如下,它给出了a0的正确结果。什么是计算这个的简单方法?也许不同的版本也可以验证结果。

非常感谢。

import numpy as np
import matplotlib.pyplot as plt

a = np.array([1,2,3,4],float) # here we can give several a
b = np.asarray(list(enumerate(a)))
x = np.linspace(0.0,1.0,10) 
y1 = [] 

for r in x:
    y1.append(np.exp(np.sum((1-r)**2*a*((2*b[:,0]+1)*r-1+r)*(r-1+r)**(b[:,0]-1)))) 

y1=np.asarray(y1)

1 个答案:

答案 0 :(得分:1)

你几乎可以在numpy中写出相同的字样:

def f(x, a):
    x, a = np.asanyarray(x), np.asanyarray(a)
    x = x[:, None]        # create new dimension to sum along
    i = np.arange(len(a)) # create counter
    return np.sum((1-x)**2 * a * ((2*i + 1) * x - (1-x)) * (x - (1-x))**(i-1), axis=-1)

作为旁注:你可以利用明显的代数简化。