西皮纽顿不接受我的海森矩阵?

2024-04-16 15:19:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个最小化程序,我正在努力让牛顿CG接受我的hessian矩阵。hessian的计算方法是numdifftools.赫斯迪亚格你知道吗

我定义了要最小化的函数

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矩阵。如果我打印一些给定坐标的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))

最小化例程给了我一个值错误(我已经检查了雅可比矩阵是好的)。你知道吗

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

Tags: 程序normnprange矩阵nnabscg