将SuiteSparse.SPQR.QRSparseQ转换为SparseMatrixCSC? [英] Converting SuiteSparse.SPQR.QRSparseQ to SparseMatrixCSC?

查看:79
本文介绍了将SuiteSparse.SPQR.QRSparseQ转换为SparseMatrixCSC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,就是将本机稀疏格式转换为稀疏矩阵的QR分解要花很多时间.但是,我需要CSC格式的它才能将其用于进一步的计算.

I have this problem that converting the native sparse format for the QR decomposition of a sparse Matrix takes forever. However, I need it in the CSC format to use it for further computations.

using LinearAlgebra, SparseArrays
N = 1000
A = sprand(N,N,1e-4)
@time F = qr(A)
@time F.Q
@time Q_sparse = sparse(F.Q)

0.000420 seconds (1.15 k allocations: 241.017 KiB)
0.000008 seconds (6 allocations: 208 bytes)
6.067351 seconds (2.00 M allocations: 15.140 GiB, 36.25% gc time)

有什么建议吗?

推荐答案

好的,我发现了问题.对于其他尝试这样做的人:

Okay, I found the problem. For other people trying to do it:

factors = F.Q.factors
τ       = F.Q.τ
Nτ = size(factors)[2]
Isp = sparse(I(N));
@time Q_constr = prod(Isp - factors[:,i]*τ[i]*factors[:,i]' for i in 1:Nτ)
Q_constr ≈ Q_sparse

0.084461 seconds (62.64 k allocations: 3.321 MiB, 18.28% gc time)
true

您看到方法sparse(F.Q)在某种程度上使用了错误的表示形式.如果像我上面那样构造Q,它将更快.

You see that the method sparse(F.Q) is somehow using the wrong representation. If you construct Q as I did above, it will be considerably faster.

这篇关于将SuiteSparse.SPQR.QRSparseQ转换为SparseMatrixCSC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆