Scipy Newton-CG不接受我的粗体矩阵?

时间:2016-03-13 19:39:45

标签: python scipy

我正在运行最小化例程,我正在努力让Newton-CG接受我的粗体矩阵。使用numdifftools.Hessdiag

计算粗麻布

我定义了要最小化的函数;

def energy(x0, L, N, U, t, mu):

    mu = mu.reshape(L, L, N)
    x0 = x0.reshape(L, L, N, 2)
    ff = x0[:, :, :, 0] + 1j*x0[:, :, :, 1]

    nn = np.array(range(N))  # [0, 1, ..., N-1]
    norm = np.zeros((L,L))

    for i_x in range(0,L):
        for i_y in range(0,L):
            norm[i_x,i_y] = np.sum(np.abs(ff[i_x,i_y,:])**2)
    norm = np.repeat(norm, N)
    norm = norm.reshape(L, L, N)

    E = np.abs(np.sum((np.abs(ff)**2/norm * (U*nn*(nn-1)/2 - mu*nn))))

    return E

然后计算最小化例程的Hessian矩阵。如果我打印一些给定坐标的粗糙矩阵,它的评估就像我希望的那样。

Hfun = nd.Hessdiag(energy)
x0 = np.tile(guess, (L,L,1,1))
x0 = x0.reshape(L*L*N*2)

print Hfun(x0, L, N, U, t, mu)

x_min = scipy.optimize.minimize(energy, x0, method='Newton-CG', jac=energy_grad, hess=Hfun, args=(L, N, U, t, mu))

尽管最小化例程给了我一个值错误(我已经检查过jacobian就好了)。

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

0 个答案:

没有答案