Skip to content

JSON reader misreads a complex value inside an array of tuples #3437

Description

@jgabry

If there's a complex number nested in a tuple that is itself in an array, the JSON reader reads it incorrectly. The same tuple outside an array is handled fine.

data {
  array[2] tuple(real, complex) d;
  array[2] tuple(complex, real) e;
  tuple(real, complex) f;
}
generated quantities {
  real d11 = d[1].1;
  real d12r = get_real(d[1].2);
  real d12i = get_imag(d[1].2);
  real d21 = d[2].1;
  real d22r = get_real(d[2].2);
  real d22i = get_imag(d[2].2);
  real e11r = get_real(e[1].1);
  real e11i = get_imag(e[1].1);
  real e12 = e[1].2;
  real e21r = get_real(e[2].1);
  real e21i = get_imag(e[2].1);
  real e22 = e[2].2;
  real f1 = f.1;
  real f2r = get_real(f.2);
  real f2i = get_imag(f.2);
}
{"d":[{"1":1.0,"2":[2.0,3.0]},{"1":4.0,"2":[5.0,6.0]}],
 "e":[{"1":[7.0,8.0],"2":9.0},{"1":[10.0,11.0],"2":12.0}],
 "f":{"1":13.0,"2":[14.0,15.0]}}

Run with method=sample algorithm=fixed_param num_samples=1.

I noticed this when working on better support for complex numbers and tuples in cmdstanr. I asked Claude to make this table (it also helped generate the Stan code above):

value expected read
d12r, d12i 2, 3 2, 5
d22r, d22i 5, 6 2.2e-314, 2.2e-314
e11r, e11i 7, 8 7, 10
e21r, e21i 10, 11 2.2e-314, 2.2e-314

The real parts of both elements end up in the first complex value and the second is just uninitialized. The tuple's real element (d11, d21, e12, e22) and the non-array tuple f seem OK.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions