繁体   English   中英

为什么通过 Cholesky 分解反转正定矩阵比使用 numpy 进行常规反转慢?

[英]Why is inverting a positive definite matrix via Cholesky decomposition slower than regular inversion with numpy?

从数学np.linalg.inv(X)通过 Cholesky 分解反转正定矩阵比仅使用np.linalg.inv(X)更快。 然而,当我对两者进行试验时,结果证明 Cholesky 分解的性能更差!

# Inversion through Cholesky
p = X.shape[0]
Ip = np.eye(p)
%timeit scipy.linalg.cho_solve(scipy.linalg.cho_factor(X,lower=True), Ip)

The slowest run took 17.96 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 107 µs per loop


# Simple inversion
%timeit np.linalg.inv(X)

The slowest run took 58.81 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 25.9 µs per loop

后者时间更短。 为什么是这样? Rchol2inv(chol(X))通常比solve(X)快。

我对 1000x1000 矩阵进行了比较,通过 Cholesky 的反演速度大约是其两倍。

也许你的矩阵太小了。 我刚刚在 Matlab 中使用 Cholesky 分解和 LU 分解测试了 $2\\times2$ 矩阵的矩阵求逆。 999999 重复使用 Cholesky 需要 5 秒,而使用 LU 只需要 3.4 秒。 确实,Cholesky 后回代的算法有较小的大 O 结果,但该结果是渐近结果,仅适用于大矩阵。

#LU decomposition
tic
for i=1:999999
    (V_i(:,:,2)+[0 1e-10;0 0])\eye(2);
end
toc
Elapsed time is 3.430676 seconds.

#Cholesky
tic
for i=1:999999
    (V_i(:,:,2))\eye(2);
end
toc
Elapsed time is 4.824175 seconds.

np.linalg.inv 使用并行处理。 运行这两个函数时检查 CPU 峰值。 如果您使用并行处理来运行多个 inv 或 Cholesky,您会发现 Cholesky 更快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM