sm1116 sm1116 - 1 month ago 12
C++ Question

Same RCPP function returs different output if print statment added

A C++ function I wrote using RCPP gives different output depending on whether or not I have a Rcout or Rprintf statement in the code. The code below returns 1 (the correct value) for the function with a print statement (H_sigma_1) but 2 when I remove the print statement (H_sigma_2). I use Ubuntu 16.04.1 and was able to reproduce the issue on CentOS 6.8 but not on Windows 10. Therefore, this seems to be a Linux issue.

library(Rcpp)

cppFunction (
"double H_sigma_1(IntegerVector sigma, NumericMatrix J, NumericVector h)
{
double first_sum, second_sum = 0;
int n = sigma.size();

for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
// skip inside loop if i >= j to stop double counting
if(i >= j) {continue;}
first_sum += J(i, j) * sigma[i] * sigma[j];
Rcout << first_sum << std::endl;
}
second_sum += h[i] * sigma[i];
}
return(-1.0 * first_sum - second_sum);
}"
)

cppFunction (
"double H_sigma_2(IntegerVector sigma, NumericMatrix J, NumericVector h)
{
double first_sum, second_sum = 0;
int n = sigma.size();

for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
// skip inside loop if i >= j to stop double counting
if(i >= j) {continue;}
first_sum += J(i, j) * sigma[i] * sigma[j];
// Rcout << first_sum << std::endl;
}
second_sum += h[i] * sigma[i];
}
return(-1.0 * first_sum - second_sum);
}"
)

n = 2
params = rep(1, n)
h = rep(params[1], n)
J = toeplitz(c(0, params[2], rep(0, n - 2)))

H_sigma_1(c(-1, -1), J, h)
H_sigma_2(c(-1, -1), J, h)

###################### OUTPUT ##################
> H_sigma_1(c(-1, -1), J, h)
1
[1] 1

> H_sigma_2(c(-1, -1), J, h)
[1] 2

Answer

The problem that you ran into is not explicitly declaring starting values for sums before using them.

See compiler warning flags:

file11d36f564b15.cpp:17:5: warning: variable 'first_sum' is uninitialized when used here [-Wuninitialized]
    first_sum += J(i, j) * sigma[i] * sigma[j];
    ^~~~~~~~~
file11d36f564b15.cpp:8:21: note: initialize the variable 'first_sum' to silence this warning
    double first_sum, second_sum = 0;

You immediately use:

first_sum += J(i, j) * sigma[i] * sigma[j];

without setting a first_sum = ...;


Furthermore, another issue is:

second_sum = 0;

initializes a double with an integer value. While this issue is minor in scope, to correct this, all one has to do is use 0.0 instead of 0.

second_sum = 0.0;

This also applies for first_sum.


Code with above fixes:

library(Rcpp)

cppFunction ( 
    "double H_sigma_1(IntegerVector sigma, NumericMatrix J, NumericVector h)
    {
    double first_sum = 0.0, second_sum = 0.0;
    int n = sigma.size();

    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        // skip inside loop if i >= j to stop double counting
        if(i >= j) {continue;}
        first_sum += J(i, j) * sigma[i] * sigma[j];
      }
      second_sum += h[i] * sigma[i];
    }
    return(-1.0 * first_sum - second_sum);
    }"
)

cppFunction ( 
    "double H_sigma_1(IntegerVector sigma, NumericMatrix J, NumericVector h)
    {
    double first_sum = 0.0, second_sum = 0.0;
    int n = sigma.size();

    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        // skip inside loop if i >= j to stop double counting
        if(i >= j) {continue;}
        first_sum += J(i, j) * sigma[i] * sigma[j];
        Rcout << first_sum << std::endl;
      }
      second_sum += h[i] * sigma[i];
    }
    return(-1.0 * first_sum - second_sum);
    }"
)

Test:

n = 2
params = rep(1, n) 
h = rep(params[1], n)
J = toeplitz(c(0, params[2], rep(0, n - 2)))

H_sigma_1(c(-1, -1), J, h)

Output:

1
[1] 1

Test 2:

H_sigma_2(c(-1, -1), J, h)

Output:

[1] 1
Comments