Model compiles on windows but not on linux

This model compiles fine on my Windows computer but it gives me an error on linux:

data {
  int<lower=0> J;             // 
  real y[J];                  // 
  int<lower=0> n[J];          // 
}

parameters {
  real mu;                    // 
  real<lower=0> nu;           // 
  real<lower=0> tau;          // 
  vector[J] eta;
}

transformed parameters {
  vector[J] theta; 
  vector<lower = 0>[J] sigma;
  theta = mu + tau * eta;
  for (j in 1:J) sigma[j] = nu/sqrt(n[j]);
}

model {
  eta ~ normal(0, 1);
  y ~ normal(theta, sigma);
}

This is the error I get on linux:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! fileda15802fa8.cpp: In member function ‘T__ modelda70c1bd88_stan_da1b9222be_namespace::modelda70c1bd88_stan_da1b9222be::log_prob(std::vector<T2>&, std::vector<int>&, std::ostream*) const’:
fileda15802fa8.cpp:283:101: error: call of overloaded ‘sqrt(const int&)’ is ambiguous
                 stan::math::assign(get_base1_lhs(sigma,j,"sigma",1), (nu / sqrt(get_base1(n,j,"n",1))));
                                                                                                     ^
In file included from /usr/include/features.h:364:0,
                 from /usr/include/stdint.h:25,
                 from /usr/lib/gcc/x86_64-linux-gnu/6/include/stdint.h:9,
                 from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/memory/stack_alloc.hpp:6,
                 from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4,
                 from /usr/local/lib/R/sit
In addition: Warning message:
running command '/usr/local/lib/R/bin/R CMD SHLIB fileda15802fa8.cpp 2> fileda15802fa8.cpp.err.txt' had status 1 
Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! fileda15802fa8.cpp: In member function ‘T__ modelda70c1bd88_stan_da1b9222be_namespace::modelda70c1bd88_stan_da1b9222be::log_prob(std::vector<T2>&, std::vector<int>&, std::ostream*) const’:
fileda15802fa8.cpp:283:101: error: call of overloaded ‘sqrt(const int&)’ is ambiguous
                 stan::math::assign(get_base1_lhs(sigma,j,"sigma",1), (nu / sqrt(get_base1(n,j,"n",1))));
                                                                                                     ^
In file included from /usr/include/features.h:364:0,
                 from /usr/include/stdint.h:25,
                 from /usr/lib/gcc/x86_64-linux-gnu/6/include/stdint.h:9,
                 from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/memory/stack_alloc.hpp:6,
                 from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4,
                 from /usr/local/lib/R/sit

Am I doing something wrong? If so, how can I fix it?

Looks like part of the problem is that on your Linux machine, your compiler uses a more modern C++ compiler, leading to the issue discussed here: https://stackoverflow.com/questions/6233132/c-compiler-error-ambiguous-call-to-overloaded-function

Basically, in older C++, sqrt(1) just became sqrt(static_cast<double>(1)), since the older sqrt() function only took values of type double. However, in newer C++, there are function signatures for sqrt() that can take a type of float, double, or long double, so it’s no longer clear whether sqrt(1) should became sqrt(static_cast<double>(1)), sqrt(static_cast<float>(1)), etc.

I think what may work is if you rewrote the for loop in your transformed parameters block like this:

for (j in 1:J) {
    real n_real = n[j];
    sigma[j] = nu/sqrt(n_real);
}

Hopefully, that should force n[j] to be converted to a double precision value, eliminating the ambiguity.

1 Like

That did the trick. Thanks!