# [SciPy-user] Using odeint to solve complex-valued ode's

Robert Kern robert.kern at gmail.com
Fri Nov 17 17:21:33 CST 2006

```David L. Goldsmith wrote:
> Robert Kern wrote:
>>> This is correct from what I remember. I don't think that the underlying ODEPACK
>>> handles complex-valued ODEs (except as converted to a real vector-valued ODE)
> OK.  Perhaps you can save me a little time.

Probably easiest is to assemble your complex arrays from the "packed" inputs, do
your complex math, then "repack" the result into the real arrays on output.

Something like the following (untested):

def pack_into_real(cy):
""" Pack a complex array into a real array.
"""
# Too clever by half, but it works.
return N.column_stack([cy.real, cy.imag]).ravel()

def unpack_into_complex(y):
""" Unpack a complex array from a real array.
"""
return y[::2] + 1j*y[1::2]

def f(y, t0=0):
cy = unpack_into_complex(y)
cyp = N.dot(N.array([[0.0, 1],[-1,0]]), cy)
yp = pack_into_real(cyp)
return yp

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
```