I am writing a matrix function in cpp11, but that function crashes the RStudio session, that doesn't happen with Rcpp.
I have created a GitHub issue for my particular case (error with cpp_partialDerivative_other() · Issue #36 · pachadotdev/fixest2 · GitHub), where if I pass a matrix of integers I have a crash, but if I pass my matrix as is (i.e., with values such as 1,2,3,4 as doubles), then cpp11 doesn't like it and says
ℹ Loading fixest2Warning: [fenegbin] Optimization failed at iteration 1. Reason: Error : Invalid input type, expected 'integer' actual 'double'
How can I debug my functions in a consistent way?
I use R mostly and I do not know much about C++, I just try to translate my statistical ideas into decent code.
I tried with simple examples and it is quite clear that both cpp11 and Rcpp do the same, like this:
cpp11
cpp11::cpp_source("multiply_by_minus_one_cpp11.cpp")
A = matrix(1:4, nrow = 2, ncol = 2)
mode(A) <- "integer"
multiply_by_minus_one(A)
#include "cpp11/matrix.hpp"
#include "cpp11/integers.hpp"
using namespace cpp11;
[[cpp11::register]] integers_matrix<> multiply_by_minus_one(writable::integers_matrix<> X) {
int i;
int j;
for (i = 0; 2; i ++) {
for (j = 0; j < i; j++) {
X(i,j) = (-1) * X(i,j);
}
}
return(X);
}
Rcpp
Rcpp::sourceCpp("multiply_by_minus_one_rcpp.cpp")
A = matrix(1:4, nrow = 2, ncol = 2)
# mode(A) <- "integer"
multiply_by_minus_one(A)
#include "Rcpp.h"
using namespace Rcpp;
// [[Rcpp::export]]
IntegerMatrix multiply_by_minus_one(IntegerMatrix X) {
int i;
int j;
for (i = 0; i <= 1; i ++) {
for (j = 0; j <= 1; j++) {
X(i,j) = (-1) * X(i,j);
}
}
return(X);
}
I've come to "cry" here after I got a bad reply on SO