给定A一个对称的N x N数组,最简单的解决方案是np.triu(A,1).sum(),但是我想知道是否存在更快、需要更少内存的方法。看起来(A.sum() - np.diag(A).sum())/2在大型数组上速度更快,但是如何避免从np.diag创建甚至是N x 1数组?双重嵌套的for循环不需要额外的内存,但在Python中显然不是这样的。
我在这里实现了这一点,假设A是对角的,C是标识: A_inv = np.diag(1./np.diag(A)) # Fast matrix inversion of a diagonal.这仅仅是因为我在将Python代码与LAPACK进行比较,还是还有其他原因呢?下面是我使用einsum的实现,使用einsum_path进行优化 A_inv = np.diag(1.1), (0
我试着在这篇文章中使用这个解决方案:elimination the linear dependent columns of a non-square matrix in python 但是,它显示: ValueError:项目错误长度为541而不是15714。Q, R = np.linalg.qr(fixeff.T)
fixeff[np.abs(np.diag(R))>=1e-10] fixeff是我所描述的矩阵。