R: Fast Cartesian calculation of two numeric matrices

I have two large numeric matrices and you want to calculate their Cartesian product in R. Is there a way to do this with higher performance and less memory usage than with my current approach?

EDIT: I added a version of Rcpp that is already much better than my first approach of only R. Since I have no experience with Rcpp or RcppArmadillo: is there a faster / more standard way to write this Rcpp function?

m1 <- matrix(sample(0:9, size=110000, replace = TRUE), ncol = 110)
m2 <- matrix(sample(0:9, size=110000, replace = TRUE), ncol = 110)

#Current approach:
m3 <- apply(m1, 1, function(x) x * t(m2))
matrix(m3, ncol = 110, byrow = TRUE)

#EDIT - Rcpp approach
library(Rcpp)
#assuming ncol(m1) == ncol(m2)
cppFunction('IntegerMatrix cartProd(IntegerMatrix m1, IntegerMatrix m2) {
  int nrow1 = m1.nrow(), ncol = m1.ncol(), nrow2 = m2.nrow();
  int orow = 0;
  IntegerMatrix out(nrow1 * nrow2, ncol);

  for (int r1 = 0; r1 < nrow1; r1++) {
    for (int r2 = 0; r2 < nrow2; r2++) {    
      for (int c = 0; c < ncol; c++){
        out(orow, c) = m1(r1, c) * m2(r2, c);
      }
      orow++;
    }
  }
  return out;
}')
m5 <- cartProd(m1, m2)
+4
source share
1 answer

, , - ++ , . Armadillo Rcpp, , R. , , . .


armadillo , Rcpp. armadillo, each_row(), . Rcpp, pass-by-reference const arma::imat&. integer .

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]

// --- Version 1

// [[Rcpp::export]]
arma::imat cartProd_arma(const arma::imat& m1, const arma::imat&  m2) {
  int nrow1 = m1.n_rows, ncol = m1.n_cols, nrow2 = m2.n_rows, orow = 0;
  arma::imat out(nrow1 * nrow2, ncol);

  for (int r1 = 0; r1 < nrow1; ++r1) {
    for (int r2 = 0; r2 < nrow2; ++r2) {
      out.row(orow) = m1.row(r1) % m2.row(r2);
      orow++;
    }
  }
  return out;
}

// --- Version 2

// [[Rcpp::export]]
arma::imat cartProd_arma2(const arma::imat& m1, const arma::imat&  m2) {
  int nrow1 = m1.n_rows, ncol = m1.n_cols, nrow2 = m2.n_rows, orow = 0;
  arma::imat out(nrow1 * nrow2, ncol);

  for (int r1 = 0; r1 < nrow1; ++r1) {
    out.submat(orow, 0, orow + nrow2 - 1, ncol - 1) = m1.row(r1) % m2.each_row();
    orow += nrow2;
  }
  return out;
}

,

all.equal( cartProd(m1, m2), cartProd_arma(m1, m2))
# [1] TRUE
all.equal( cartProd(m1, m2), cartProd_arma2(m1, m2))
# [1] TRUE

, , , , , . , , @user20650.

# OP initial R only solution with slight modifications
op_R = function(m1, m2){
  m2 <- t(m2)
  m3 <- matrix(apply(m1, 1, function(x) x * m2), ncol = ncol(m1), byrow = TRUE)
}

# user20650 comment
so_comment <- function(m1, m2){
  m4 <- matrix(rep(t(m1), each=nrow(m1)) * c(m2), ncol=nrow(m1))
}

library("microbenchmark")
out <- microbenchmark(op_r = op_R(m1, m2), so_comment_r = so_comment(m1, m2), 
                 rcpp = cartProd(m1, m2), arma_v1 = cartProd_arma(m1, m2),
                 arma_v2 = cartProd_arma2(m1, m2),
                 times = 50)
out
# Unit: milliseconds
#         expr       min        lq      mean    median        uq       max neval
#         op_r 1615.6572 1693.0526 1793.0515 1771.7353 1886.0988 2053.7050    50
# so_comment_r 2778.0971 2856.6429 2950.5837 2936.7459 3021.4249 3344.4401    50
#         rcpp  463.6743  482.3118  565.0525  582.1660  614.3714  699.3516    50
#      arma_v1  597.9004  620.5888  713.4101  726.7572  783.4225  820.3770    50
#      arma_v2  384.7205  401.9744  490.5118  503.5007  574.6840  622.9876    50

, , cartProd_arma2, armadillo, , cartProd, Rcpp.

+3

Source: https://habr.com/ru/post/1666101/


All Articles