Debug C++ (cpp11) code in R to evaluate functions with integers or doubles

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

image

This is what you want to do with cpp11?

A <- matrix(1:4, nrow = 2, ncol = 2)
A
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4
A * -1
#>      [,1] [,2]
#> [1,]   -1   -3
#> [2,]   -2   -4

Created on 2023-02-06 with reprex v2.0.2

Not really, I passed an elemental example of the same in Rcpp and cpp11, both examples work.
My problem is with more complex matrix operations, which return errors related to integer vs decimal for the expected data types.

Provide a reprex? See the FAQ. Include working C++ as standalone.

Thanks. As I mentioned in the OP, the issue is detailed here error with cpp_partialDerivative_other() · Issue #36 · pachadotdev/fixest2 · GitHub.

I have this function that does not like a matrix of integers: fixest2/05_04_misc_derivatives.cpp at cpp11_wip · pachadotdev/fixest2 · GitHub

I know it's an intricate problem, that was why I asked about how to debug C++ code.

Maybe someone will be interested in hunting down a clear statement of the problem in an outside forum. The expectations here are for a reprex with only external links to data when necessary.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.