# Complex unknowns An equation or a source that contains the imaginary unit `sp.I` has a complex solution: the Schrödinger equation, a wave of one frequency, a wall that lets a wave out. Nothing else about the problem changes. This page shows what comes back, and where complex numbers work. ```python import numpy as np import sympy as sp import pdemagic as pm x, t = sp.symbols("x t", real=True) ``` ## A wave packet The Schrödinger equation of a free particle, started as a wave packet that moves to the right: ```python psi = sp.Function("psi")(x, t) free_particle = pm.Problem( sp.Eq(sp.I * sp.Derivative(psi, t), -sp.Derivative(psi, x, 2) / 2), domain=pm.FreeSpace(x), time=(t, 0.0, 2.0), initial={psi: sp.exp(-(x**2) + 3 * sp.I * x)}, ) packet = pm.solve(free_particle) assert packet[psi].dtype == complex # The packet moves and spreads, and the total probability stays what it was. density = abs(packet[psi]) ** 2 assert abs(density[-1].sum() / density[0].sum() - 1) < 1e-12 packet.plot() ``` The values are complex numbers, and a plot shows their magnitude. `packet.plot(part="real")` and `part="imag"` show the two parts instead. The equation is linear with constant coefficients, so it is [propagated exactly](time.md#linear-equations-are-propagated-exactly), like the heat equation. ## A potential A potential is a coefficient that varies in space, which calls for [time stepping](nonlinear-time.md). A potential that acts in one place works in free space. A particle runs into a barrier that is higher than its energy. Most of it is thrown back, and a part of it tunnels: ```python barrier = 6 * sp.exp(-(x**2)) tunnelling = pm.Problem( sp.Eq( sp.I * sp.Derivative(psi, t), -sp.Derivative(psi, x, 2) / 2 + barrier * psi, ), domain=pm.FreeSpace(x), time=(t, 0.0, 4.0), initial={psi: sp.exp(-((x + 6) ** 2) / 2 + 3 * sp.I * x)}, ) tunnel = pm.solve(tunnelling) (points,) = tunnel.axes density = abs(tunnel[psi]) ** 2 behind = density[-1][points > 2].sum() / density[0].sum() assert 0.02 < behind < 0.5 tunnel.plot() ``` A potential that grows without end, like that of the harmonic oscillator, needs a periodic domain, or a box, a round domain or a shape in the plane with boundary conditions. In a periodic one a displaced Gaussian swings back and forth without changing its shape, and after one period it is back where it started, with the opposite sign: ```python well = pm.Space(x, periodic={x: (-10, 10)}) oscillator = pm.Problem( sp.Eq( sp.I * sp.Derivative(psi, t), -sp.Derivative(psi, x, 2) / 2 + x**2 / 2 * psi, ), domain=well, time=(t, 0.0, 2 * np.pi), initial={psi: sp.exp(-((x - 1) ** 2) / 2)}, ) swing = pm.solve(oscillator) assert abs(swing[psi][-1] + swing[psi][0]).max() < 1e-8 ``` On a shape in the plane a potential that varies in space alone is not stepped: it is part of the modes of the shape, and [exact in time](time.md#on-a-shape). ## Without time A stationary equation is complex in the same way. The solution of the [Helmholtz equation](helmholtz.md) in free space is complex by nature: a wave that leaves has a phase. In a boundary condition a complex coefficient does the same at a wall. With $\partial w/\partial x - i k w = 0$ a wave of the wave number $k$ leaves through the face and is not reflected: ```python w = sp.Function("w")(x) k = 7 tube = pm.Problem( sp.Eq(w.diff(x, 2) + k**2 * w, 0), domain=pm.Box({x: (0, 1)}), boundary={(x, 0): sp.Eq(w, 1), (x, 1): sp.Eq(w.diff(x) - sp.I * k * w, 0)}, ) leaving = pm.solve(tube) (xs,) = leaving.axes assert abs(leaving[w] - np.exp(1j * k * xs)).max() < 1e-9 leaving.plot(part="real") ``` ## Functions of a complex unknown Expressions may use `sp.conjugate`, `sp.re`, `sp.im`, `sp.Abs` and `sp.arg`. The cubic term of the nonlinear Schrödinger equation is `sp.Abs(psi)**2 * psi`. In a nonlinear stationary equation these are not taken: Newton's method cannot differentiate them. ## Complex arrays An array of complex numbers is given as two arrays of real ones, its real and its imaginary part. Declare two data fields and write their sum: ```python points = np.linspace(-14.0, 14.0, 257) start = np.exp(-(points**2) + 1j * points) A = sp.Function("A")(x) B = sp.Function("B")(x) from_arrays = pm.Problem( sp.Eq(sp.I * sp.Derivative(psi, t), -sp.Derivative(psi, x, 2) / 2), domain=pm.FreeSpace(x), time=(t, 0.0, 0.5), initial={psi: A + sp.I * B}, data={ A: pm.GridData.from_axes(start.real, points), B: pm.GridData.from_axes(start.imag, points), }, ) assert pm.solve(from_arrays)[psi].shape == (21, 257) ``` [Data from arrays](data.md) says how arrays and grids go together. ## Where complex numbers work - Every problem in time, with one unknown or several. - The Helmholtz equation in free space. - Any linear equation on a periodic box, and Poisson's equation and its screened form in Cartesian coordinates, with complex sources. - Equations with boundary conditions: on boxes, round domains and shapes in the plane. Not yet: complex sources around [bodies](bodies.md), and on a disk, a ball or a cylinder with vacuum outside. ## See also - [Problems in time](time.md) and [Nonlinear equations in time](nonlinear-time.md), for how the Schrödinger equation is solved. - [Eigenproblems](eigen.md), for the states of a potential. - [Plotting](plotting.md), for the parts of a complex unknown. - Gallery: [a quantum wave packet](../gallery/quantum-wave-packet.md), [a soliton of light](../gallery/soliton-of-light.md) and [two antennas](../gallery/two-antennas.md). - Methods: [linear equations in time](../methods/propagator.md), which explains how far a wave packet travels in free space, and [the Helmholtz equation](../methods/helmholtz.md).