2

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.

  • I usually start by checking that the code in the (generally excellent) documentation works: http://arma.sourceforge.net/docs.html#qr and then work my way up from there. – Dirk Eddelbuettel Sep 02 '20 at 12:57
  • 2
    I can't reproduce this, it runs and finds solution for me – user20650 Sep 02 '20 at 12:58
  • What version of Armadillo are you using?, as pivoting was introduce in the not to distant past https://sourceforge.net/p/arma/news/2020/06/armadillo-c-linear-algebra-version-9900/ – user20650 Sep 02 '20 at 13:03
  • @user20650, I'm using Armadillo thru RcppArmadillo v. 0.9.850.1. However, the qr function compiles properly on my laptop, not on my desktop (a Windows 10 box). – Antonio Piemontese Sep 03 '20 at 12:53
  • Perhaps your laptop has a different version -- `packageVersion("RcppArmadillo")` ? . But if you upgrade I'd think you will be okay [ v. 0.9.850.1 dates from 2020-02-09 which is before the above link says pivoting was introduced] – user20650 Sep 03 '20 at 13:21
  • 1
    @user20650, thanks so much, it was just that old rcpparmadillo version! – Antonio Piemontese Sep 03 '20 at 21:34

0 Answers0