# What can we do with complex numbers in Stan?

I’m wrapping up support for complex number types in the Stan math library. Now I’m wondering what we can do with complex numbers in statistical models.

Functions operating in the complex domain

The initial plan is to add some matrix functions that use complex numbers internally:

• fast fourier transforms
• asymmetric eigendecomposition
• Schur decomposition

The eigendecomposition and Schur decomposition are already working. We need to settle on an interface for FFTs before adding those. Are there other functions like these we should add?

Complex random variables?

But what if we add a complex number type to the language? That’d essentially give us complex random variables (and constants) as first-class citizens. We’d have something like the following.

```complex x;             // declaration

x = complex(-1, 2.9);  // constructor

x.real = 1.3;          // setters
x.imag = -2.5;

complex y = sqrt(x);   // complex sqrt

real yr = y.real;      // getters
real yi = y.imag;
```

Technically, we’re treating a complex number just like a pair (the elements being the real and imaginary components). We will be able to differentiate with respect to real and/or imaginary components, and thus differentiate through functions operating in the complex domain, but we’re not taking derivatives with respect to an entire complex number.

It’s easy in the math library to create a matrix of complex numbers, but we’ll need new types in the language: complex_matrix, complex_vector, complex_row_vector. Arrays will just follow from general principles.

Applications, anyone?

So what can we do with complex random variables if we have them?

A quick search online found a discussion of complex variables on stats.stackexchange which mirrors some of our internal discussions.

I have no idea where we’d use something like characteristic functions in the Stan language or complex polynomial roots, but am hoping someone else can clue me in.