Is there something like #define in Stan?

Is there a way to do something like a C++ #define in Stan?

I’ve looked in the manual and not found it; I’ve looked in rstan, and I found that I could use #include there for what appears to inject code into the post-translation source code, and I’ve seen both a proposal for adding #include by @Bob_Carpenter (Syntax and scope for Stan language includes) and the related github discussion (https://github.com/stan-dev/stan/issues/2224).

But, my hope is to have a #define statement like C so that I don’t have to remember some of the constants that I’d like to reuse in my code. It would make life easier because we would have fewer typos and inconsistencies within code where we need the same constant in multiple places. I could achieve the scope almost like I’d like in the transformed data block, but it doesn’t (seem to) have scope within defined functions.

Updating an example from the Torsten manual (figure 7,page 12 of raw.githubusercontent.com/charlesm93/example-models/feature/issue-70-PKPDexamples-torsten/PKPD/torsten/torstenManual.pdf), I’d like to do something like:

#define CL_INDEX 1
#define Q_INDEX 2
#define V1_INDEX 3
#define V2_INDEX 4
#define KA_INDEX 5
functions{
  # define ODE system for two compartment model
  real[] twoCptModelODE(real t,
                        real[] x,
                        real[] theta,
                        real[] rate, # in this example, rate is treated as data
                        int[] dummy) {
    real Q;
    real CL;
    real V1;
    real V2;
    real ka;
    real k12;
    real k21;
    real k10;
    real y[3];
    CL = theta[CL_INDEX];
    Q = theta[Q_INDEX];
    V1 = theta[V1_INDEX];
    V2 = theta[V2_INDEX];
    ka = theta[KA_INDEX];
    k10 = CL / V1;
    k12 = Q / V1;
    k21 = Q / V2;
    y[1] = -ka*x[1];
    y[2] = ka*x[1] - (k10 + k12)*x[2] + k21*x[3];
    y[3] = k12*x[2] - k21*x[3];
    return y;
  }
}

transformed parameters {
  # ...
  theta[1][CL_INDEX] = CL;
  theta[1][Q_INDEX] = Q;
  theta[1][V1_INDEX] = V1;
  theta[1][V2_INDEX] = V2;
  theta[1][KA_INDEX] = ka;
  # ...
}

Even better would be if I could also use syntax like the #define CNAME (...) syntax:

#define PARAM_PACKING ({CL, Q, V1, V2, ka})

Hi Bill, to solve your immediate problem I would just abuse the #include syntax to get your constants in. It’s the sort of thing rstanarm does. Long-term we have some disagreement about whether allowing these global-scope things does more harm than good in terms of debugging (I’ve already seen programs posted with bugs in #include-ed functions that were not/could not be shared, it’ s not to help people in those situations). I’m sure somebody else knows better what the current state of that discussion is.

Nope, no macros. You can define constants in the transformed data block or you can define functions that return constant values.

The general #include mechanism should be merged into Stan soon, but it’s unlikely to hit the interfaces before Stan 2.17 (that’s two releases from now); there’s a stopgap in place in RStan now, but it doesn’t work in other interfaces.