stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

[BUG] `transform_inits` code for complex numbers is incorrect. #1304

Closed WardBrian closed 1 year ago

WardBrian commented 1 year ago

transform_inits_impl generates z = in__.read<local_scalar_t__>(); to read in a complex number z, but this should be z = in__.read<std::complex<local_scalar_t__>>();

The result is that z has a 0 imaginary component and subsequent parameters are read in from the wrong part of the buffer.

I have a partial fix (for scalars) in a branch https://github.com/stan-dev/stanc3/tree/fix/deserializer-read-containers. Containers requires some additional logic in one of the overloads of transform_inits

Model Code

Model which exhibits the issue ```stan parameters { complex_vector[2] z ;//= [1+2i,3+4i]'; } model { print(z); } ``` ```json {"z":[[1,2], [3,4]]} ``` ```shell ./test_model sample num_warmup=1 num_samples=1 init=params.json ``` prints `[(1,0),(3,0)]`

Environment:

Stan 2.31.0