如何从SuiteSparseQR_factorization对象中找到(Q,R)?

How to find (Q, R ) from SuiteSparseQR_factorization object?

本文关键字:SuiteSparseQR 对象 factorization      更新时间:2023-10-16

在 SuiteSparse 的C++界面中,我可以使用

SuiteSparseQR_factorization <double> *QR;
QR = SuiteSparseQR_factorize(A) ;

计算矩阵 A 的 QR 分解,以便我可以重复使用 QR 进行进一步计算。但我想知道我可以直接从中获得真正的 Q,R 这个二维码对象?

SuiteSparse很棒,但界面可能会令人困惑。不幸的是,涉及SuiteSparseQR_factorization结构的方法,似乎是最方便的,在实践中对我来说效果不佳。例如,使用SuiteSparseQR_factorize然后使用稀疏矩阵输入参数SuiteSparseQR_qmult实际上首先将其转换为密集矩阵,这似乎完全没有必要!

相反,请使用

template <typename Entry> SuiteSparse_long SuiteSparseQR
(
// inputs, not modified
int ordering,           // all, except 3:given treated as 0:fixed
double tol,             // only accept singletons above tol
SuiteSparse_long econ,  // number of rows of C and R to return; a value
// less than the rank r of A is treated as r, and
// a value greater than m is treated as m.
int getCTX,             // if 0: return Z = C of size econ-by-bncols
// if 1: return Z = C' of size bncols-by-econ
// if 2: return Z = X of size econ-by-bncols
cholmod_sparse *A,      // m-by-n sparse matrix
// B is either sparse or dense.  If Bsparse is non-NULL, B is sparse and
// Bdense is ignored.  If Bsparse is NULL and Bdense is non-NULL, then B is
// dense.  B is not present if both are NULL.
cholmod_sparse *Bsparse,
cholmod_dense *Bdense,
// output arrays, neither allocated nor defined on input.
// Z is the matrix C, C', or X
cholmod_sparse **Zsparse,
cholmod_dense  **Zdense,
cholmod_sparse **R,     // the R factor
SuiteSparse_long **E,   // size n; fill-reducing ordering of A.
cholmod_sparse **H,     // the Householder vectors (m-by-nh)
SuiteSparse_long **HPinv,// size m; row permutation for H
cholmod_dense **HTau,   // size nh, Householder coefficients
// workspace and parameters
cholmod_common *cc
) ;

此方法将执行因式分解,然后可选地输出(除其他外(R,矩阵乘积Z = Q^T * B(或其转置 - B^T * Q(或线性系统的解。要获取 Q,请将 B 定义为单位矩阵。下面是获取 Q 和 R 的示例。

cholmod_common Common, * cc;
cc = &Common;
cholmod_l_start(cc);
cholmod_sparse *A;//assume you have already defined this
int ordering = SPQR_ORDERING_BEST;
double tol = 0;
Long econ = A->nrow;
int getCTX = 1;// Z = (Q^T * B)^T = B^T * Q
cholmod_sparse *B = cholmod_l_speye(A->nrow, A->nrow, CHOLMOD_REAL, cc);//the identity matrix
cholmod_sparse *Q, *R;//output pointers to the Q and R sparse matrices
SuiteSparseQR<double>(ordering, tol, econ, getCTX, A, B, NULL, &Q, NULL, &R, NULL, NULL, NULL, NULL, cc);

如果您希望任何其他输出在不使用显式形成的 Q 和/或 R 的情况下执行后续操作,则需要将 NULL 替换为其他指针,然后调用SuiteSparseQR_qmult