Problems in using qr decomposition with RcppArmadillo

Viewed 119

I need qr decomposition from Armadillo thru Rcpp. The following R code (with the economic QR) does run:

# test matrix:
m<-5; n<-4
set.seed(123)
X <- replicate(n, runif(m))

sourceCpp(code='
#include <iostream>
#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;

//[[Rcpp::export]]
List QRdec_econ(arma::mat X) {
  int n = X.n_cols;
  int m = X.n_rows;
  arma::mat Q(m, n);
  Q.fill(0);
  arma::mat R(n, n);
  R.fill(0);

  arma::qr_econ(Q,R,X);

  return List::create(_["Q"] = Q,
                      _["R"] = R
  );
}'
)

QRdec_econ(as.matrix(X))

enter image description here

On the contrary, the following code (very similar, just with qr rather than qr_econ) fails at compilation time:

sourceCpp(code='
#include <iostream>
#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;

//[[Rcpp::export]]
List CPHHQR_arma(arma::mat X) {
  int n = X.n_cols;
  int m = X.n_rows;
  arma::mat Q(m, m);
  Q.fill(0);
  arma::mat R(m, n);
  R.fill(0);
  arma::umat P(n, n);
  P.fill(0);

  arma::qr(Q,R,P,X,"matrix");

return List::create(_["Q"] = Q,
                    _["R"] = R,
                    _["P"] = P
);
}'
)

enter image description here

Any suggestions, please, for such a weird code behaviour? Many thanks in advance.

0 Answers
Related