1

I have a void function x2y that updates x to y through their addresses.

The Rcpp code is like this

#include <RcppArmadillo.h>
#include <iostream>

//[[Rcpp::export]]
void x2y(const Rcpp::NumericVector& x,
         Rcpp::NumericVector& y)
{
  y = 1.0 / (1.0 + Rcpp::exp(-x));

  std::cout << "output(0)= " << y(0) << std::endl;
}

If y is initialized with NAs it is not updated:

x <- c(-1, 0, 2)

y <- c(NA, NA, NA)
x2y(x, y)
print(y)
[1] NA NA NA

But if

y <- c(NaN, NaN, NaN)
x2y(x, y)
print(y)
[1] 0.2689414 0.5000000 0.8807971

or if

y <- c(-99, -99, -99)
x2y(x, y)
print(y)
[1] 0.2689414 0.5000000 0.8807971

This doesn't make sense to me. Can somebody explain why this happens?

utobi
  • 279
  • 8
  • 16
  • 3
    This has been discussed a million times, look eg for references to 'Rcpp proxy model'. The (sole !!) interface we have to R is pointer based. – Dirk Eddelbuettel Feb 07 '18 at 11:50

0 Answers0