大家好,我最近在C++中利用R中的qr源代码编程一个qr分解的函数。主要是想求得矩阵qr分解后的pivot,R中qr分解的源代码如下
- qr.default <- function(x, tol = 1e-07, LAPACK = FALSE, ...)
- {
- x <- as.matrix(x)
- if(is.complex(x))
- return(structure(.Call("La_zgeqp3", x, PACKAGE = "base"), class="qr"))
- ## otherwise :
- if(!is.double(x))
- storage.mode(x) <- "double"
- if(LAPACK) {
- res <- .Call("La_dgeqp3", x, PACKAGE = "base")
- if(!is.null(cn <- colnames(x)))
- colnames(res$qr) <- cn[res$pivot]
- attr(res, "useLAPACK") <- TRUE
- class(res) <- "qr"
- return(res)
- }
- p <- ncol(x) # guaranteed to be integer
- n <- nrow(x)
- res <- .Fortran("dqrdc2",
- qr=x,
- n,
- n,
- p,
- as.double(tol),
- rank=integer(1L),
- qraux = double(p),
- pivot = as.integer(1L:p),
- double(2*p),
- PACKAGE="base")[c(1,6,7,8)]# c("qr", "rank", "qraux", "pivot")
- if(!is.null(cn <- colnames(x)))
- colnames(res$qr) <- cn[res$pivot]
- class(res) <- "qr"
- res
- }
复制代码C++的Eigen也有QR分解的函数,但是没有pivot 项。哪位高人知道如何将其转换为C++函数。