7.8 Vectorized accessor functions

Much like with complex scalars, two functions are defined to get the real and imaginary components of complex-valued objects.

7.8.1 Type “demotion”

These functions return the same shape (e.g., matrix, vector, row vector, or array) object as their input, but demoted to a real type. For example, get_real(complex_matrix M) yields a matrix containing the real component of each value in M.

The following table contains examples of what this notation can mean:

Type T Type T_demoted
complex real
complex_vector vector
complex_row_vector row_vector
complex_matrix matrix
array[] complex array[] real
array[,,] complex array[,,] real

7.8.2 Real and imaginary component accessor functions

T_demoted get_real(T x)
Given an object of complex type T, return the same shape object but of type real by getting the real component of each element of x.
Available since 2.30

T_demoted get_imag(T x)
Given an object of complex type T, return the same shape object but of type real by getting the imaginary component of each element of x.
Available since 2.30

For example, given the Stan declaration

  complex_vector[2] z = [3+4i, 5+6i]';

A call get_real(z) will yield the vector [3, 5]', and a call get_imag(z) will yield the vector [4, 6]'.