rcpparrayfire

RcppArrayFire passing a matrix row as af::array input


In this simple example I would like to subset a matrix by row and pass it to another cpp function; the example demonstrates this works by passing an input array to the other function first.

#include "RcppArrayFire.h"

using namespace Rcpp;

af::array theta_check_cpp( af::array theta){

  if(*theta(1).host<double>() >= 1){
    theta(1) = 0;
  }

  return theta;
}

// [[Rcpp::export]]
af::array theta_check(RcppArrayFire::typed_array<f64> theta){

  const int theta_size = theta.dims()[0];

  af::array X(2, theta_size);
  X(0,  af::seq(theta_size)) = theta_check_cpp( theta );
  X(1,  af::seq(theta_size)) = theta;
  // return X;
  Rcpp::Rcout << " works till here";
  return theta_check_cpp( X.row(1) );
}


/*** R
theta <- c( 2, 2, 2)
theta_check(theta)
*/

Solution

  • The constructor you are using to create X has an argument ty for the data type, which defaults to f32. Therefore X uses 32 bit floats and you cannot extract a 64 bit host pointer from that. Either use

    af::array X(2, theta_size, f64);
    

    to create an array using 64 bit doubles, or extract a 32 bit host pointer via

    if(*theta(1).host<float>() >= 1){
       ...