Does setting a transformed parameter to be equal to another parameter make them point to the same place in memory

i have a parameter matrix and an observed matrix of same size. in some cells of the observed matrix are nans, these are fine but they signify that i shouldn’t model with those cells in the matrixes. Because i want my code to be as efficient as possible I want to convert the two matrixes to vectors and then model their relationship in vectorized fashion. so if i initialize a vector and then go through the parameter matrix and set values in the vector to be equal to cells in the matrix can i use the vector to model the values in the matrix cell? will they in effect point to the same space in memory?

thanks!

No, that would copy the values from the matrix to the vector

1 Like

thanks for the answer!
is there a way i could do this so that the cells in the vector point to the right cells in the parameter matrix?

No, there’s no way to tell Stan to map a container to an existing area in memory

1 Like

Now having said that, we’re working on pushing expression templates further through our math evals.

I’m not 100% sure that’s what @flatSTANley was asking. When we copy Stan autodiff variables, the variables point to the same place in memory. Our underlying implementation follows the pointer to implementation (PIMPL) pattern, which will copy the underlying memory reference for an autodiff variable.

Consider this Stan program:

parameters {
  real a;
}
transformed parameters {
  real a_copy = a;
}

Then both a and a_copy point to the same autodiff variable. But a and a_copy are not in the same place in memory.

Now consider the vector version:

parameters {
  vector[10] a;
}
transformed parameters {
  vector[5] a_slice = a[4:8];
}

Here, a_slice[1] and a[4] point to the same autodiff variable in memory. But the two containers have different memory.

You can see that the containers are different, because if we set a value in a_slice it doesn’t set a value in a, it just replaces the value in a_slice with the new value.

1 Like