# Copyright 2026 Philippe Billet assisted by LLMs in free mode: chatGPT, Qwen, Deepseek, Gemini, Claude, le chat Mistral.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
microlocal.py — Unified microlocal analysis toolkit for 1D and 2D problems
============================================================================
Overview
--------
The `microlocal` module provides a high‑level interface for studying the propagation of singularities and constructing semiclassical approximations for linear partial differential equations. It builds upon the dedicated `wkb` (WKB approximations) and `caustics` (catastrophe classification and ray caustic detection) modules, adding dimension‑agnostic functions for the core concepts of microlocal analysis:
* **Characteristic variety** `Char(P) = {(x,ξ) : p(x,ξ)=0}` – the set of phase‑space points where the principal symbol vanishes, indicating where singularities can propagate.
* **Bicharacteristic flow** – Hamilton’s equations for the principal symbol, whose integral curves (bicharacteristics) govern the propagation of wave‑front sets.
* **Wavefront set** `WF(u)` – a refinement of the singular support that also records the directions (frequencies) in which the singularity occurs. The module visualises how an initial wavefront set evolves under the flow.
* **Wavefront-set animation** – `animate_wavefront_set` animates `WF(u)` while one initial coordinate (`x`, `y`, `ξ` or `η`) sweeps a range: each frame is the propagated wavefront set of a family of seeds (point source, grid with a phase, rectangle, curve, ...), shown in 2D projections or in a 3D view of the 4D phase space. See *Animating the wavefront set* below.
* **WKB approximation** (re‑exported from `wkb`) – asymptotic solutions of the form `u ≈ A e^{iS/ε}`.
* **Caustics and Maslov index** – detection of caustics (where rays focus) and computation of the associated Maslov phase shifts, crucial for correct semiclassical quantisation.
* **Bohr–Sommerfeld quantisation** (1D) – semiclassical energy levels for bound states.
* **Operator visualisation and interactive analysis** – a comprehensive suite for plotting symbol amplitude, phase, micro‑support, characteristic gradients, and launching interactive `ipywidgets` dashboards.
* **Matrix-valued symbols and pseudospectra** – extraction of characteristic branches via eigenvalue computation for matrix operators, and visualisation of ε‑pseudospectra.
* **PDE solution rendering** – rendering and animation tools for scalar, matrix, and wave equation fields in 1D and 2D space-time.
All functions automatically detect the spatial dimension (1 or 2) from the input data, making the module usable for both one‑dimensional and two‑dimensional problems without changing the calling syntax.
Mathematical background
-----------------------
In microlocal analysis, a linear partial differential operator `P` is studied via its **principal symbol** `p(x,ξ)`, a function on the cotangent bundle `T*ℝⁿ`. The **characteristic variety** is the zero set of `p`. Singularities of a distribution `u` satisfying `P u ≈ 0` are confined to the characteristic variety and propagate along **bicharacteristics** – integral curves of the Hamiltonian vector field
X_p = ( ∂p/∂ξ , –∂p/∂x ).
The **wavefront set** `WF(u)` is a closed conic subset of `T*ℝⁿ \\ {0}` that records both the location `x` and the direction `ξ` of the singularity. If `(x₀,ξ₀) ∉ WF(u)`, then `u` is smooth in a neighbourhood of `x₀` in the direction `ξ₀`. The fundamental theorem of microlocal analysis states that `WF(Pu) ⊆ WF(u)` and that `WF(u) \\ WF(Pu)` is contained in the characteristic variety and is invariant under the bicharacteristic flow.
The **WKB method** seeks solutions of the form `u(x) = e^{iS(x)/ε} (a₀(x) + ε a₁(x) + …)`. The phase `S` satisfies the eikonal equation `p(x,∇S)=0`, and the amplitudes `a_k` satisfy transport equations along bicharacteristics. This construction breaks down at **caustics**, where rays focus; the Maslov index `μ` (a signed count of caustic crossings) provides a phase correction `e^{iμπ/2}` that restores uniformity.
The module integrates these concepts into a coherent toolkit, allowing the user to:
* Symbolically compute the characteristic variety.
* Numerically integrate bicharacteristics with symplectic integrators.
* Visualise the evolution of wavefront sets, including 2D and 3D singularity animations and animated families of wavefront sets (`animate_wavefront_set`).
* Obtain semiclassical spectra via Bohr–Sommerfeld quantisation (1D).
* Detect caustics and compute the Maslov index (using the `caustics` module).
* Interactively explore symbol properties such as amplitude, phase, and micro-support.
* Analyse matrix-valued symbols by computing their characteristic branches.
* Plot ε-pseudospectra and overlay complex eigenvalues.
* Render scalar, matrix, and wave PDE solutions in space-time.
Animating the wavefront set
---------------------------
`animate_wavefront_set(symbol, tspan, ...)` is the animated counterpart of `plot_wavefront_set`. Each frame shows the wavefront set obtained by propagating a family of seed singularities along the bicharacteristics over `tspan`; the animation variable is the initial coordinate that is given a range (exactly one of `x_range`, `y_range`, `xi_range`, `eta_range`), the others being fixed by `x0`, `y0`, `xi0`, `eta0`.
*Seed families* (2D; in 1D the seeds are a fan of frequencies at the point `x0`), selected by `seed_family`:
* ``'circle'`` (default) – point source: a small circle of seeds with outward covectors, as in `visualize_wavefront_set`.
* ``'fiber'`` – all directions of the covector at one point, run in both time directions. Modulo ξ → -ξ this is a Möbius strip, best seen in 3D with the sign-invariant coordinates ``'u'`` = ξ²-η² and ``'v'`` = 2ξη.
* ``'grid'`` – seeds on a rectangular grid (`corner_ul`, `corner_lr`, `step`) with a constant covector or the covector ∇S(x, y) of a phase function `phase`, i.e. the initial Lagrangian manifold of a WKB or plane wave.
* ``'rectangle'`` – wavefront set of the indicator of a rectangle: edge conormals plus a fan of directions at each corner.
* ``'curve'`` – conormal bundle of a parametrised curve (x(s), y(s)): a name from the catalogue `WAVEFRONT_CURVES` (20 curves: ellipse, parabola, astroid, cardioid, lemniscate, spirals, ...), two sympy expressions or two callables. With `covector_angle` the covector direction is prescribed instead of the normal, which makes 'circle' and 'fiber' special cases of 'curve'.
*Views.* All the projections of `plot_wavefront_set` are available (including ``'full'`` with the four 2D slices of the 4D phase space), plus ``projection='3d'``: three coordinates `coords3d` as axes (position, frequency, time, or the invariants ``'u'``, ``'v'``) and a fourth quantity `color_by` as colour, so that all four dimensions of `T*ℝ²` are visible. The camera is controlled by `view`, `rotate` and `ortho`. Axis limits are fixed over the whole animation (`pad`, `xlim`, `ylim`, `zlim`, or `fixed_limits=False` to re-fit each frame).
*Performance.* All the rays of all the frames are integrated at once with a vectorised version of the fixed-step symplectic scheme of `bicharacteristic_flow` (`fast=True`, the default), which is orders of magnitude faster than ray-by-ray integration.
The function returns a `matplotlib.animation.FuncAnimation`; use ``HTML(anim.to_jshtml())`` in a notebook or `save_path` to write a file.
Examples::
# Point source whose position moves vertically, 4 slices of phase space
anim = animate_wavefront_set(p, tspan=(0, 4), x0=1, xi0=1, eta0=1,
y_range=(-5, 5), seeds=100, projection='full')
# Plane-wave grid with a focusing phase, in 3D (x, y, t) with eta as colour
anim = animate_wavefront_set(p, tspan=(0, 1), seed_family='grid',
corner_ul=(-1, 1), corner_lr=(1, -1), step=0.125,
phase=-(x**2 + y**2)/4, y_range=(-2, 2),
projection='3d', coords3d=('x', 'y', 't'),
color_by='eta', ortho=True)
# Conormals of an astroid, then of a user-defined curve
anim = animate_wavefront_set(p, tspan=(0, 2), seed_family='curve',
curve='astroid', seeds=120, xi0=1, eta0=0,
x_range=(-1, 1))
References
----------
.. [1] Hörmander, L. *The Analysis of Linear Partial Differential Operators I*, Springer, 1983. Chapter 8: Wave Front Sets.
.. [2] Duistermaat, J. J. *Fourier Integral Operators*, Courant Institute Lecture Notes, 1996.
.. [3] Maslov, V. P. & Fedoriuk, M. V. *Semi‑Classical Approximation in Quantum Mechanics*, Reidel, 1981.
.. [4] Zworski, M. *Semiclassical Analysis*, American Mathematical Society, 2012. Chapter 3: Propagation of Singularities.
.. [5] Taylor, M. E. *Partial Differential Equations II*, Springer, 2011. Chapter 8: Microlocal Analysis.
"""
from imports import *
from wkb import *
from caustics import *
# ----------------------------------------------------------------------
# Dimension detection helper (used internally by microlocal functions)
# ----------------------------------------------------------------------
def _infer_dim(
symbol: Expr,
dim: Optional[int] = None
) -> int:
"""
Infer the spatial dimension (1 or 2) from a SymPy symbol or an explicit argument.
Relies on the presence of common variable names: 'x', 'xi' for 1D,
and 'x', 'y', 'xi', 'eta' for 2D.
Parameters
----------
symbol : sympy.Expr
The symbolic expression representing the principal symbol.
dim : int, optional
Explicit dimension (1 or 2). If provided, overrides inference.
Returns
-------
int
The inferred or provided dimension (1 or 2).
Raises
------
ValueError
If `dim` is provided but is not 1 or 2.
"""
if dim is not None:
if dim not in (1, 2):
raise ValueError('dim must be 1 or 2')
return dim
free_vars = set(symbol.free_symbols)
if any((str(v) in ('y', 'eta') for v in free_vars)):
return 2
return 1
# ----------------------------------------------------------------------
# Characteristic variety
# ----------------------------------------------------------------------
[docs]
def characteristic_variety(
symbol: Expr,
dim: Optional[int] = None,
tol: float = 1e-08
) -> Dict[str, Any]:
"""
Compute the characteristic variety of a pseudo-differential operator.
Char(P) = { (x,ξ) in T*ℝ : p(x,ξ)=0 } (1D)
or
Char(P) = { (x,y,ξ,η) in T*ℝ² : p(x,y,ξ,η)=0 } (2D)
Parameters
----------
symbol : sympy expression
Principal symbol p.
dim : int, optional
Dimension (1 or 2). If None, inferred from symbol.
tol : float
Tolerance for zero detection (unused, kept for compatibility).
Returns
-------
dict
A dictionary containing the following keys:
- 'implicit' : sympy expression
The symbol expression.
- 'equation' : sympy Eq
The equation `symbol = 0`.
- 'explicit' : list or None
List of explicit solutions ξ(x) (1D only), or None if no explicit solution exists.
- 'function' : callable
A callable that evaluates the symbol.
"""
dim = _infer_dim(symbol, dim)
if dim == 1:
x, xi = symbols('x xi', real=True)
char_eq = Eq(symbol, 0)
try:
xi_solutions = solve(symbol, xi)
explicit = [simplify(sol) for sol in xi_solutions]
except:
explicit = None
func = lambdify((x, xi), symbol, 'numpy')
return {'implicit': symbol, 'equation': char_eq, 'explicit': explicit, 'function': func}
else:
x, y, xi, eta = symbols('x y xi eta', real=True)
char_eq = Eq(symbol, 0)
func = lambdify((x, y, xi, eta), symbol, 'numpy')
return {'implicit': symbol, 'equation': char_eq, 'explicit': None, 'function': func}
# ----------------------------------------------------------------------
# Bicharacteristic flow
# ----------------------------------------------------------------------
[docs]
def bicharacteristic_flow(
symbol: Expr,
z0: Sequence[float],
tspan: Tuple[float, float],
dim: Optional[int] = None,
method: Literal['rk45', 'symplectic', 'verlet', 'hamiltonian'] = 'symplectic',
n_steps: int = 1000
) -> Dict[str, np.ndarray]:
"""
Integrate the bicharacteristic flow on the cotangent bundle.
Hamilton's equations: ẋ = ∂p/∂ξ, ξ̇ = -∂p/∂x (1D)
or ẋ = ∂p/∂ξ, ẏ = ∂p/∂η, ξ̇ = -∂p/∂x, η̇ = -∂p/∂y (2D)
Parameters
----------
symbol : sympy expression
Principal symbol.
z0 : tuple
Initial condition on T*M. For 1D: (x₀, ξ₀); for 2D: (x₀, y₀, ξ₀, η₀).
tspan : tuple
(t_start, t_end).
dim : int, optional
Dimension. If None, inferred from length of z0.
method : str
Integration method: 'rk45', 'symplectic', or 'verlet' (2D only).
n_steps : int
Number of time steps.
Returns
-------
dict
Trajectory data with keys 't', 'x', 'xi' (1D) and also 'y','eta' (2D),
plus 'symbol_value'.
"""
if dim is None:
dim = 2 if len(z0) == 4 else 1
if dim == 1:
return _bichar_flow_1d(symbol, z0, tspan, method, n_steps)
else:
return _bichar_flow_2d(symbol, z0, tspan, method, n_steps)
def _bichar_flow_1d(
symbol: Expr,
z0: Sequence[float],
tspan: Tuple[float, float],
method: str,
n_steps: int
) -> Dict[str, np.ndarray]:
"""
Integrate the 1D bicharacteristic flow and the associated stability matrix.
For a Hamiltonian system defined by the principal symbol p(x,ξ), the
bicharacteristics (Hamiltonian flow) satisfy
dx/dt = ∂p/∂ξ, dξ/dt = –∂p/∂x.
Simultaneously, the 2×2 Jacobian matrix
J(t) = [[ ∂x/∂x₀ , ∂x/∂ξ₀ ],
[ ∂ξ/∂x₀ , ∂ξ/∂ξ₀ ]]
obeys the linearised variational equation
dJ/dt = A(t) J, J(0) = I₂,
where
A(t) = [[ ∂²p/(∂ξ∂x) , ∂²p/∂ξ² ],
[ –∂²p/∂x² , –∂²p/(∂x∂ξ) ]]
evaluated along the trajectory.
Parameters
----------
symbol : sympy.Expr
Principal symbol p(x,ξ). Must depend on the real symbols `x` and `xi`.
z0 : tuple of float
Initial condition (x₀, ξ₀).
tspan : tuple of float
Integration interval (t_start, t_end).
method : {'rk45', 'symplectic', 'hamiltonian'}
Integration scheme:
- 'rk45' : adaptive Runge‑Kutta 4(5) from `scipy.integrate.solve_ivp`.
The state is augmented with the four entries of J.
- 'symplectic': fixed‑step symplectic Euler (position half‑step,
momentum full‑step). The linearised map is derived
from the exact discretisation of the variational
equations, preserving the symplectic structure up to
the order of the integrator.
- 'hamiltonian': alias for 'symplectic' (kept for backward compatibility).
n_steps : int
Number of output points (including the initial state). For fixed‑step
methods this equals the number of integration steps; for 'rk45' it
specifies the number of equally spaced time values at which the
solution is evaluated.
Returns
-------
dict
A dictionary containing the following fields:
- 't' : 1D ndarray of shape (n_steps,) – time points.
- 'x' : 1D ndarray – position trajectory.
- 'xi' : 1D ndarray – momentum trajectory.
- 'symbol_value' : 1D ndarray – p(x,ξ) evaluated along the trajectory.
- 'J11', 'J12', 'J21', 'J22' : 1D ndarrays – components of the 2×2
stability matrix J(t) at each time point.
Notes
-----
- The function uses symbolic differentiation (`sympy.diff`) and generates
NumPy callables via `lambdify`. The symbols must be named exactly `x` and
`xi` (case‑sensitive).
- For the symplectic Euler method, the linearised map M that advances the
Jacobian from step i to i+1 is:
M = [[ 1 + Δt·a11 + Δt²·a12·a21 , Δt·a12·(1 + Δt·a22) ],
[ Δt·a21 , 1 + Δt·a22 ]]
where a11, a12, a21, a22 are the entries of A(t) evaluated at the
beginning of the step. This map is applied as J_{i+1} = M · J_i.
- This function is intended for internal use and is not part of the public
API of the `microlocal` module.
"""
x, xi = symbols('x xi', real=True)
dp_dxi = diff(symbol, xi)
dp_dx = diff(symbol, x)
f_x = lambdify((x, xi), dp_dxi, 'numpy')
f_xi = lambdify((x, xi), -dp_dx, 'numpy')
d2p_dx2 = diff(symbol, x, x)
d2p_dxi2 = diff(symbol, xi, xi)
d2p_dxdxi = diff(symbol, x, xi)
A11 = diff(symbol, xi, x)
A12 = d2p_dxi2
A21 = -d2p_dx2
A22 = -d2p_dxdxi
A_func = lambdify((x, xi), (A11, A12, A21, A22), 'numpy')
p_func = lambdify((x, xi), symbol, 'numpy')
if method == 'rk45':
def ode(t, z):
xv, xiv = (z[0], z[1])
J11, J12, J21, J22 = (z[2], z[3], z[4], z[5])
a11, a12, a21, a22 = A_func(xv, xiv)
dJ11 = a11 * J11 + a12 * J21
dJ12 = a11 * J12 + a12 * J22
dJ21 = a21 * J11 + a22 * J21
dJ22 = a21 * J12 + a22 * J22
return [f_x(xv, xiv), f_xi(xv, xiv), dJ11, dJ12, dJ21, dJ22]
z0_aug = [z0[0], z0[1], 1.0, 0.0, 0.0, 1.0]
sol = solve_ivp(ode, tspan, z0_aug, method='RK45', t_eval=np.linspace(tspan[0], tspan[1], n_steps), rtol=1e-09, atol=1e-12)
return {'t': sol.t, 'x': sol.y[0], 'xi': sol.y[1], 'symbol_value': p_func(sol.y[0], sol.y[1]), 'J11': sol.y[2], 'J12': sol.y[3], 'J21': sol.y[4], 'J22': sol.y[5]}
elif method in ('symplectic', 'hamiltonian'):
dt = (tspan[1] - tspan[0]) / n_steps
t = np.linspace(tspan[0], tspan[1], n_steps)
x_vals = np.zeros(n_steps)
xi_vals = np.zeros(n_steps)
J11_vals = np.zeros(n_steps)
J12_vals = np.zeros(n_steps)
J21_vals = np.zeros(n_steps)
J22_vals = np.zeros(n_steps)
x_vals[0], xi_vals[0] = z0
J11_vals[0], J12_vals[0] = (1.0, 0.0)
J21_vals[0], J22_vals[0] = (0.0, 1.0)
for i in range(n_steps - 1):
xv, xiv = (x_vals[i], xi_vals[i])
a11, a12, a21, a22 = A_func(xv, xiv)
xi_new = xiv + dt * f_xi(xv, xiv)
x_new = xv + dt * f_x(xv, xi_new)
M11 = 1.0 + dt * a11 + dt * dt * a12 * a21
M12 = dt * a12 * (1.0 + dt * a22)
M21 = dt * a21
M22 = 1.0 + dt * a22
J11_new = M11 * J11_vals[i] + M12 * J21_vals[i]
J12_new = M11 * J12_vals[i] + M12 * J22_vals[i]
J21_new = M21 * J11_vals[i] + M22 * J21_vals[i]
J22_new = M21 * J12_vals[i] + M22 * J22_vals[i]
x_vals[i + 1] = x_new
xi_vals[i + 1] = xi_new
J11_vals[i + 1] = J11_new
J12_vals[i + 1] = J12_new
J21_vals[i + 1] = J21_new
J22_vals[i + 1] = J22_new
return {'t': t, 'x': x_vals, 'xi': xi_vals, 'symbol_value': p_func(x_vals, xi_vals), 'J11': J11_vals, 'J12': J12_vals, 'J21': J21_vals, 'J22': J22_vals}
else:
raise ValueError('Invalid method for 1D flow')
def _bichar_flow_2d(
symbol: Expr,
z0: Sequence[float],
tspan: Tuple[float, float],
method: str,
n_steps: int
) -> Dict[str, np.ndarray]:
"""
Compute the bicharacteristic (Hamiltonian) flow for a 2D symbol
and the associated stability matrix.
The Hamiltonian system is defined by the symbol p(x, y, ξ, η):
dx/dt = ∂p/∂ξ, dξ/dt = -∂p/∂x,
dy/dt = ∂p/∂η, dη/dt = -∂p/∂y.
Simultaneously, the 2×2 stability matrix J(t) = ∂(x,y)/∂(x₀,y₀)
(the Jacobian of the spatial part of the flow with respect to initial
positions) is propagated according to
dJ/dt = H_px · J, J(0) = I₂,
where H_px is the 2×2 matrix of mixed derivatives:
H_px = [[ ∂²p/(∂ξ∂x), ∂²p/(∂ξ∂y) ],
[ ∂²p/(∂η∂x), ∂²p/(∂η∂y) ]].
Parameters
----------
symbol : sympy.Expr
Symbolic expression for the Hamiltonian p(x, y, ξ, η). Must depend
on the real variables x, y, xi, eta (the names are fixed in the
function body).
z0 : tuple of float
Initial condition (x0, y0, xi0, eta0).
tspan : tuple of float
Time interval (t0, t1).
method : {'rk45', 'symplectic', 'verlet'}
Integration method:
- 'rk45' : adaptive Runge‑Kutta 4(5) from `scipy.integrate.solve_ivp`.
The state is augmented with the four entries of J.
- 'symplectic' : fixed‑step symplectic Euler (position half‑step,
momentum full‑step). Stability matrix uses forward Euler.
- 'verlet' : fixed‑step Störmer‑Verlet (leapfrog). Stability matrix
uses the midpoint value of H_px for the update.
n_steps : int
Number of output points (including the initial state). For fixed‑step
methods this equals the number of integration steps; for 'rk45' it is
the number of equally spaced time values at which the solution is
evaluated.
Returns
-------
dict
A dictionary containing:
- 't' : 1D ndarray of shape (n_steps,) – time points.
- 'x','y' : 1D ndarrays – trajectory in position.
- 'xi','eta': 1D ndarrays – trajectory in momentum.
- 'symbol_value' : 1D ndarray – p(x,y,ξ,η) evaluated along the trajectory.
- 'J11','J12','J21','J22' : 1D ndarrays – components of the 2×2
stability matrix J(t) at each time point.
Notes
-----
- The function uses symbolic differentiation (`sympy.diff`) and generates
NumPy callables via `lambdify`. The symbols *must* be named exactly
`x`, `y`, `xi`, `eta` (case‑sensitive).
- For 'rk45', high relative/absolute tolerances (1e‑9, 1e‑12) are used to
ensure accurate stability propagation.
- For the symplectic and Verlet schemes, the stability update is a simple
first‑order method (forward Euler or midpoint) that is consistent with
the overall integrator order but may not preserve symplecticity of the
linearised flow.
- This function is intended for internal use in pseudo‑differential
operator construction and is not part of the public API.
"""
x, y, xi, eta = symbols('x y xi eta', real=True)
dp_dxi = diff(symbol, xi)
dp_deta = diff(symbol, eta)
dp_dx = diff(symbol, x)
dp_dy = diff(symbol, y)
f_x = lambdify((x, y, xi, eta), dp_dxi, 'numpy')
f_y = lambdify((x, y, xi, eta), dp_deta, 'numpy')
f_xi = lambdify((x, y, xi, eta), -dp_dx, 'numpy')
f_eta = lambdify((x, y, xi, eta), -dp_dy, 'numpy')
p_func = lambdify((x, y, xi, eta), symbol, 'numpy')
H_px_sym = Matrix([[diff(symbol, xi, x), diff(symbol, xi, y)], [diff(symbol, eta, x), diff(symbol, eta, y)]])
H_px_func = lambdify((x, y, xi, eta), H_px_sym, 'numpy')
def _H_px(xv, yv, xiv, etav):
return np.asarray(H_px_func(xv, yv, xiv, etav), dtype=float).reshape(2, 2)
if method == 'rk45':
z0_aug = list(z0) + [1.0, 0.0, 0.0, 1.0]
def ode(t, z):
xv, yv, xiv, etav = (z[0], z[1], z[2], z[3])
J = z[4:].reshape(2, 2)
dJ = _H_px(xv, yv, xiv, etav) @ J
return [f_x(xv, yv, xiv, etav), f_y(xv, yv, xiv, etav), f_xi(xv, yv, xiv, etav), f_eta(xv, yv, xiv, etav), *dJ.ravel()]
sol = solve_ivp(ode, tspan, z0_aug, method='RK45', t_eval=np.linspace(tspan[0], tspan[1], n_steps), rtol=1e-09, atol=1e-12)
return {'t': sol.t, 'x': sol.y[0], 'y': sol.y[1], 'xi': sol.y[2], 'eta': sol.y[3], 'symbol_value': p_func(sol.y[0], sol.y[1], sol.y[2], sol.y[3]), 'J11': sol.y[4], 'J12': sol.y[5], 'J21': sol.y[6], 'J22': sol.y[7]}
elif method in ('symplectic', 'verlet'):
dt = (tspan[1] - tspan[0]) / n_steps
t = np.linspace(tspan[0], tspan[1], n_steps)
x_vals = np.zeros(n_steps)
y_vals = np.zeros(n_steps)
xi_vals = np.zeros(n_steps)
eta_vals = np.zeros(n_steps)
J_vals = np.zeros((n_steps, 2, 2))
x_vals[0], y_vals[0], xi_vals[0], eta_vals[0] = z0
J_vals[0] = np.eye(2)
if method == 'symplectic':
for i in range(n_steps - 1):
xv, yv = (x_vals[i], y_vals[i])
xiv, etav = (xi_vals[i], eta_vals[i])
xi_new = xiv + dt * f_xi(xv, yv, xiv, etav)
eta_new = etav + dt * f_eta(xv, yv, xiv, etav)
x_new = xv + dt * f_x(xv, yv, xi_new, eta_new)
y_new = yv + dt * f_y(xv, yv, xi_new, eta_new)
x_vals[i + 1] = x_new
y_vals[i + 1] = y_new
xi_vals[i + 1] = xi_new
eta_vals[i + 1] = eta_new
J_vals[i + 1] = J_vals[i] + dt * (_H_px(xv, yv, xiv, etav) @ J_vals[i])
else:
for i in range(n_steps - 1):
xv, yv = (x_vals[i], y_vals[i])
xiv, etav = (xi_vals[i], eta_vals[i])
xi_half = xiv + 0.5 * dt * f_xi(xv, yv, xiv, etav)
eta_half = etav + 0.5 * dt * f_eta(xv, yv, xiv, etav)
x_new = xv + dt * f_x(xv, yv, xi_half, eta_half)
y_new = yv + dt * f_y(xv, yv, xi_half, eta_half)
xi_new = xi_half + 0.5 * dt * f_xi(x_new, y_new, xi_half, eta_half)
eta_new = eta_half + 0.5 * dt * f_eta(x_new, y_new, xi_half, eta_half)
x_vals[i + 1] = x_new
y_vals[i + 1] = y_new
xi_vals[i + 1] = xi_new
eta_vals[i + 1] = eta_new
Hpx_mid = 0.5 * (_H_px(xv, yv, xiv, etav) + _H_px(x_new, y_new, xi_new, eta_new))
J_vals[i + 1] = J_vals[i] + dt * (Hpx_mid @ J_vals[i])
return {'t': t, 'x': x_vals, 'y': y_vals, 'xi': xi_vals, 'eta': eta_vals, 'symbol_value': p_func(x_vals, y_vals, xi_vals, eta_vals), 'J11': J_vals[:, 0, 0], 'J12': J_vals[:, 0, 1], 'J21': J_vals[:, 1, 0], 'J22': J_vals[:, 1, 1]}
else:
raise ValueError('Invalid method for 2D flow')
# ----------------------------------------------------------------------
# 1D‑specific functions (Bohr–Sommerfeld, caustic detection)
# ----------------------------------------------------------------------
[docs]
def bohr_sommerfeld_quantization(
H: Expr,
n_max: int = 10,
x_range: Tuple[float, float] = (-10, 10),
hbar: float = 1.0,
E_range: Tuple[float, float] = (1e-06, 50.0)
) -> Dict[str, Any]:
"""
Compute Bohr-Sommerfeld quantization for 1D bound states.
Solves (1/(2π)) ∮ p dx = ℏ(n + α) with α = 1/2 (Maslov index).
Parameters
----------
H : sympy.Expr
Hamiltonian H(x,p) as a symbolic expression.
n_max : int, optional
Maximum quantum number to compute. Default is 10.
x_range : tuple of float, optional
Spatial range for finding turning points. Default is (-10, 10).
hbar : float, optional
Reduced Planck constant. Default is 1.0.
E_range : tuple of float, optional
Energy range to scan for quantized levels. Default is (1e-6, 50.0).
Returns
-------
dict
A dictionary containing:
- 'n' : numpy.ndarray (Quantum numbers)
- 'E_n' : numpy.ndarray (Quantized energy levels)
- 'actions' : numpy.ndarray (Corresponding classical actions)
- 'hbar' : float (The reduced Planck constant used)
- 'alpha' : float (The Maslov index correction used, 0.5)
"""
x, p = symbols('x p', real=True)
E_sym = symbols('E', real=True, positive=True)
solutions = solve(H - E_sym, p)
if not solutions:
raise ValueError('Cannot solve H=E for p(x,E)')
p_expr = solutions[-1]
p_func = lambdify((x, E_sym), p_expr, 'numpy')
alpha = 0.5
def action(E):
X = np.linspace(x_range[0], x_range[1], 2000)
p_vals = p_func(X, E)
p_vals = np.real_if_close(p_vals)
p_vals = np.real(p_vals)
if np.ndim(p_vals) == 0:
return 0.0
mask = p_vals >= 0
if not np.any(mask):
return 0.0
idx = np.where(mask)[0]
a, b = (X[idx[0]], X[idx[-1]])
def integrand(xv):
return p_func(xv, E)
I, _ = quad(integrand, a, b, epsabs=1e-10, epsrel=1e-10)
return I / np.pi
targets = [hbar * (n + alpha) for n in range(n_max)]
E_scan = np.linspace(E_range[0], E_range[1], 200)
I_scan = [action(E) for E in E_scan]
energies = []
actions = []
quantum_numbers = []
for n, Itarget in zip(range(n_max), targets):
found = False
for k in range(len(E_scan) - 1):
if (I_scan[k] - Itarget) * (I_scan[k + 1] - Itarget) < 0:
E_left, E_right = (E_scan[k], E_scan[k + 1])
found = True
break
if not found:
continue
def F(E):
return action(E) - Itarget
E_n = bisect(F, E_left, E_right, xtol=1e-10, rtol=1e-10, maxiter=100)
energies.append(E_n)
actions.append(Itarget)
quantum_numbers.append(n)
return {'n': np.array(quantum_numbers), 'E_n': np.array(energies), 'actions': np.array(actions), 'hbar': hbar, 'alpha': alpha}
[docs]
def find_caustics_1d(
symbol: Expr,
x_range: Tuple[float, float],
xi_range: Tuple[float, float],
resolution: int = 100
) -> Dict[str, Union[np.ndarray, float]]:
"""
Find caustics (envelope of bicharacteristics) in 1D.
Uses a simplified condition: identifies regions where d²p/dξ² ≈ 0,
which correspond to turning points in frequency.
Parameters
----------
symbol : sympy.Expr
The 1D principal symbol p(x, ξ).
x_range : tuple of float
The (min, max) range for the spatial variable x.
xi_range : tuple of float
The (min, max) range for the frequency variable ξ.
resolution : int, optional
Number of grid points per axis for the evaluation mesh. Default is 100.
Returns
-------
dict
A dictionary containing:
- 'x_grid' : numpy.ndarray (2D meshgrid of x coordinates)
- 'xi_grid' : numpy.ndarray (2D meshgrid of ξ coordinates)
- 'caustic_indicator' : numpy.ndarray (Absolute value of d²p/dξ²)
- 'threshold' : float (10th percentile of the indicator, useful for masking)
"""
x, xi = symbols('x xi', real=True)
d2p = diff(symbol, xi, 2)
func = lambdify((x, xi), d2p, 'numpy')
xv = np.linspace(x_range[0], x_range[1], resolution)
xiv = np.linspace(xi_range[0], xi_range[1], resolution)
X, XI = np.meshgrid(xv, xiv, indexing='ij')
Z = np.abs(func(X, XI))
return {'x_grid': X, 'xi_grid': XI, 'caustic_indicator': Z, 'threshold': np.percentile(Z, 10)}
[docs]
def propagate_singularity(
symbol: Expr,
initial_sing_support: List[Sequence[float]],
tspan: Tuple[float, float],
dim: Optional[int] = None,
n_samples: Optional[int] = None
) -> Dict[str, Any]:
"""
Propagate singular support along bicharacteristics.
Parameters
----------
symbol : sympy.Expr
The principal symbol.
initial_sing_support : list of tuple
List of initial phase-space points (x₀, ξ₀) for 1D or
(x₀, y₀, ξ₀, η₀) for 2D.
tspan : tuple of float
The (t_start, t_end) integration interval.
dim : int, optional
Spatial dimension (1 or 2). Inferred from `symbol` if None.
n_samples : int, optional
Currently unused. Kept for API compatibility.
Returns
-------
dict
A dictionary containing:
- 'trajectories' : list of dict (Bicharacteristic flow results)
- 'endpoints' : list of tuple (Final phase-space coordinates)
- 'initial' : list of tuple (The initial seed points provided)
"""
dim = _infer_dim(symbol, dim)
trajectories = []
for z0 in initial_sing_support:
traj = bicharacteristic_flow(symbol, z0, tspan, dim=dim, method='symplectic')
trajectories.append(traj)
endpoints = [(traj['x'][-1], traj.get('y', [None])[-1], traj['xi'][-1], traj.get('eta', [None])[-1]) for traj in trajectories]
return {'trajectories': trajectories, 'endpoints': endpoints, 'initial': initial_sing_support}
# ----------------------------------------------------------------------
# Visualisation functions (non‑WKB)
# ----------------------------------------------------------------------
[docs]
def plot_characteristic_set(
symbol: Expr,
x_range: Tuple[float, float],
xi_range: Tuple[float, float],
dim: Optional[int] = None,
resolution: int = 200,
**kwargs: Any
) -> None:
"""
Plot the characteristic variety (zero set of the principal symbol).
For 1D: plots the contour p(x,ξ)=0 in the (x,ξ) phase plane.
For 2D: plots a slice with fixed (ξ,η); you must provide `xi0` and `eta0` via kwargs.
Parameters
----------
symbol : sympy.Expr
The principal symbol.
x_range : tuple of float
Range for the first spatial variable (x).
xi_range : tuple of float
Range for the second spatial variable (y) in 2D, or frequency (ξ) in 1D.
dim : int, optional
Dimension (1 or 2). Inferred from `symbol` if None.
resolution : int, optional
Grid resolution for plotting. Default is 200.
**kwargs : dict
Additional arguments. For 2D, expects `xi0` and `eta0` (floats).
Returns
-------
None
Displays a matplotlib plot.
"""
dim = _infer_dim(symbol, dim)
if dim == 1:
x, xi = symbols('x xi', real=True)
p_func = lambdify((x, xi), symbol, 'numpy')
xv = np.linspace(x_range[0], x_range[1], resolution)
xiv = np.linspace(xi_range[0], xi_range[1], resolution)
X, XI = np.meshgrid(xv, xiv, indexing='ij')
Z = p_func(X, XI)
if np.isscalar(Z):
Z = np.full_like(X, Z)
plt.figure(figsize=(8, 6))
plt.contour(X, XI, Z, levels=[0], colors='red', linewidths=3)
plt.pcolormesh(X, XI, np.log10(np.abs(Z) + 1e-10), shading='auto', cmap='viridis', alpha=0.5)
plt.colorbar(label='log₁₀|p|')
plt.xlabel('x')
plt.ylabel('ξ')
plt.title('Characteristic variety (1D)')
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()
else:
xi0 = kwargs.get('xi0', 1.0)
eta0 = kwargs.get('eta0', 0.0)
x, y, xi, eta = symbols('x y xi eta', real=True)
p_fixed = symbol.subs({xi: xi0, eta: eta0})
p_func = lambdify((x, y), p_fixed, 'numpy')
xv = np.linspace(x_range[0], x_range[1], resolution)
yv = np.linspace(xi_range[0], xi_range[1], resolution)
X, Y = np.meshgrid(xv, yv, indexing='ij')
Z = p_func(X, Y)
if np.isscalar(Z):
Z = np.full_like(X, Z)
plt.figure(figsize=(8, 6))
plt.contour(X, Y, Z, levels=[0], colors='red', linewidths=3)
plt.pcolormesh(X, Y, np.log10(np.abs(Z) + 1e-10), shading='auto', cmap='viridis', alpha=0.5)
plt.colorbar(label='log₁₀|p|')
plt.xlabel('x')
plt.ylabel('y')
plt.title(f'Characteristic set (ξ={xi0}, η={eta0})')
plt.axis('equal')
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()
[docs]
def plot_bicharacteristics(
symbol: Expr,
initial_points: List[Sequence[float]],
tspan: Tuple[float, float],
dim: Optional[int] = None,
projection: Literal['position', 'frequency', 'mixed'] = 'position',
**kwargs: Any
) -> None:
"""
Plot bicharacteristic curves (Hamiltonian flow trajectories).
For 1D: plots trajectories in the (x,ξ) phase plane.
For 2D: projection can be 'position' (x-y), 'frequency' (ξ-η), or 'mixed' (x-ξ).
Parameters
----------
symbol : sympy.Expr
The principal symbol.
initial_points : list of tuple
List of initial conditions for the flow.
tspan : tuple of float
The (t_start, t_end) integration interval.
dim : int, optional
Dimension (1 or 2). Inferred from `initial_points` if None.
projection : str, optional
Projection plane for 2D plots. One of 'position', 'frequency', or 'mixed'.
Default is 'position'.
**kwargs : dict
Additional arguments passed to `bicharacteristic_flow`.
Returns
-------
None
Displays a matplotlib plot.
"""
dim = _infer_dim(symbol, dim)
fig, ax = plt.subplots(figsize=(10, 8))
colors = plt.cm.viridis(np.linspace(0, 1, len(initial_points)))
for idx, z0 in enumerate(initial_points):
traj = bicharacteristic_flow(symbol, z0, tspan, dim=dim, method='symplectic', n_steps=500)
if dim == 1:
ax.plot(traj['x'], traj['xi'], color=colors[idx], alpha=0.7, lw=2)
ax.plot(traj['x'][0], traj['xi'][0], 'go', ms=8)
ax.plot(traj['x'][-1], traj['xi'][-1], 'ro', ms=6)
elif projection == 'position':
ax.plot(traj['x'], traj['y'], color=colors[idx], alpha=0.7, lw=2)
ax.plot(traj['x'][0], traj['y'][0], 'go', ms=8)
ax.plot(traj['x'][-1], traj['y'][-1], 'ro', ms=6)
elif projection == 'frequency':
ax.plot(traj['xi'], traj['eta'], color=colors[idx], alpha=0.7, lw=2)
ax.plot(traj['xi'][0], traj['eta'][0], 'go', ms=8)
ax.plot(traj['xi'][-1], traj['eta'][-1], 'ro', ms=6)
elif projection == 'mixed':
ax.plot(traj['x'], traj['xi'], color=colors[idx], alpha=0.7, lw=2)
ax.plot(traj['x'][0], traj['xi'][0], 'go', ms=8)
ax.plot(traj['x'][-1], traj['xi'][-1], 'ro', ms=6)
else:
raise ValueError("projection must be 'position', 'frequency', or 'mixed'")
if dim == 1:
ax.set_xlabel('x')
ax.set_ylabel('ξ')
else:
if projection == 'position':
ax.set_xlabel('x')
ax.set_ylabel('y')
elif projection == 'frequency':
ax.set_xlabel('ξ')
ax.set_ylabel('η')
else:
ax.set_xlabel('x')
ax.set_ylabel('ξ')
ax.axis('equal')
ax.set_title('Bicharacteristics')
ax.grid(alpha=0.3)
plt.tight_layout()
plt.show()
[docs]
def plot_wavefront_set(
symbol: Expr,
initial_sing_support: List[Sequence[float]],
tspan: Tuple[float, float],
dim: Optional[int] = None,
projection: str = 'cotangent',
n_steps: int = 500,
cmap: str = 'plasma',
show_flow: bool = True,
show_endpoints: bool = True,
title: Optional[str] = None,
ax: Optional[Union[Axes, Sequence[Axes]]] = None
) -> Tuple[Figure, Union[Axes, np.ndarray]]:
"""
Plot the wavefront set WF(u) of a distribution u whose singularities
propagate along bicharacteristics of the operator with symbol p.
The wavefront set is represented as a subset of the cotangent bundle T*ℝⁿ.
Each initial point (x₀, ξ₀) seeds a bicharacteristic strip; the union of
these strips in phase space approximates WF(u) at time tspan[1].
Parameters
----------
symbol : sympy expression
Principal symbol p(x, ξ) in 1D or p(x, y, ξ, η) in 2D.
initial_sing_support : list of tuples
Seed points on the wavefront set at t=0.
1D: list of (x₀, ξ₀) pairs.
2D: list of (x₀, y₀, ξ₀, η₀) quadruples.
tspan : tuple
(t_start, t_end) for bicharacteristic integration.
dim : int, optional
Dimension (1 or 2). Inferred from seed points if None.
projection : str
Which subspace to visualise (dimension-dependent):
- 1D:
'cotangent' – (x, ξ) phase-space portrait [default]
'position' – x(t) projected onto ℝ (singular support)
- 2D:
'cotangent' – (x, ξ) slice (ignoring y, η)
'position' – (x, y) projection (singular support in ℝ²)
'frequency' – (ξ, η) projection (directions of non-smoothness)
'full' – 2×2 grid: position, frequency, (x,ξ), (y,η)
n_steps : int
Number of integration steps per bicharacteristic.
cmap : str
Matplotlib colormap used to colour individual bicharacteristics
(colour encodes the index of the seed point, i.e. which part of the
initial singular support it originated from).
show_flow : bool
If True, draw the full bicharacteristic strip (trajectory in phase
space). If False, only show the endpoint scatter.
show_endpoints : bool
If True, mark the *initial* point (green •) and *final* point (red •)
of each bicharacteristic.
title : str, optional
Figure title. A sensible default is generated if None.
ax : matplotlib Axes or array of Axes, optional
Axes to draw into. If None a new figure is created. For
projection='full' (2D) pass an array of 4 Axes or leave None.
Returns
-------
fig : matplotlib Figure
axes : Axes or array of Axes
Examples
--------
1D example – Schrödinger-type operator, horizontal line singularity::
x, xi = symbols('x xi', real=True)
p = xi**2 - (1 - x**2) # simple potential well symbol
seeds = [(xi_val, float(xi_val)) for xi_val in np.linspace(-1, 1, 12)]
fig, ax = plot_wavefront_set(p, seeds, tspan=(0, 4), dim=1)
plt.show()
2D example – wave operator, outward circular wavefront::
x, y, xi, eta = symbols('x y xi eta', real=True)
p = xi**2 + eta**2 - 1
seeds = [(np.cos(t), np.sin(t), np.cos(t), np.sin(t))
for t in np.linspace(0, 2*np.pi, 24, endpoint=False)]
fig, axes = plot_wavefront_set(p, seeds, tspan=(0, 2), dim=2,
projection='full')
plt.show()
"""
if dim is None:
dim = 2 if len(initial_sing_support[0]) == 4 else 1
n_seeds = len(initial_sing_support)
colors = plt.get_cmap(cmap)(np.linspace(0.05, 0.95, n_seeds))
trajs = []
for z0 in initial_sing_support:
traj = bicharacteristic_flow(symbol, z0, tspan, dim=dim, method='symplectic', n_steps=n_steps)
trajs.append(traj)
if projection == 'full' and dim == 2:
if ax is None:
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes = axes.flatten()
else:
axes = np.asarray(ax).flatten()
fig = axes[0].get_figure()
ax_list = [(axes[0], 'x', 'y', 'position', 'Position space (x, y)'), (axes[1], 'xi', 'eta', 'frequency', 'Frequency space (ξ, η)'), (axes[2], 'x', 'xi', 'mixed_x', 'Cotangent slice (x, ξ)'), (axes[3], 'y', 'eta', 'mixed_y', 'Cotangent slice (y, η)')]
for idx, traj in enumerate(trajs):
c = colors[idx]
x0, y0 = (traj['x'][0], traj['y'][0])
xi0, eta0 = (traj['xi'][0], traj['eta'][0])
xf, yf = (traj['x'][-1], traj['y'][-1])
xif, etaf = (traj['xi'][-1], traj['eta'][-1])
pairs = [(traj['x'], traj['y'], x0, y0, xf, yf), (traj['xi'], traj['eta'], xi0, eta0, xif, etaf), (traj['x'], traj['xi'], x0, xi0, xf, xif), (traj['y'], traj['eta'], y0, eta0, yf, etaf)]
for (ax_obj, kx, ky, proj_label, panel_title), (u, v, u0, v0, uf, vf) in zip(ax_list, pairs):
if show_flow:
ax_obj.plot(u, v, color=c, alpha=0.6, lw=1.2)
if show_endpoints:
ax_obj.plot(u0, v0, 'o', color='limegreen', ms=5, zorder=5, markeredgewidth=0)
ax_obj.plot(uf, vf, 's', color='crimson', ms=4, zorder=5, markeredgewidth=0)
for ax_obj, kx, ky, proj_label, panel_title in ax_list:
ax_obj.set_xlabel({'x': 'x', 'xi': 'ξ', 'y': 'y', 'eta': 'η'}[kx])
ax_obj.set_ylabel({'x': 'x', 'xi': 'ξ', 'y': 'y', 'eta': 'η'}[ky])
ax_obj.set_title(panel_title, fontsize=10)
ax_obj.grid(alpha=0.25)
ax_obj.set_aspect('equal', adjustable='datalim')
fig.suptitle(title or 'Wavefront set WF(u) [2D, full cotangent bundle]', fontsize=13, fontweight='bold')
plt.tight_layout()
return (fig, axes)
if ax is None:
fig, axes = plt.subplots(figsize=(8, 6))
else:
axes = ax
fig = axes.get_figure()
if dim == 1:
if projection in ('cotangent', 'phase'):
get_u = lambda t: t['x']
get_v = lambda t: t['xi']
xlabel, ylabel = ('x', 'ξ')
equal_aspect = False
elif projection == 'position':
get_u = lambda t: t['t']
get_v = lambda t: t['x']
xlabel, ylabel = ('t', 'x (singular support)')
equal_aspect = False
else:
raise ValueError(f"Unknown projection '{projection}' for dim=1. Choose 'cotangent' or 'position'.")
elif projection in ('cotangent', 'mixed_x'):
get_u = lambda t: t['x']
get_v = lambda t: t['xi']
xlabel, ylabel = ('x', 'ξ')
equal_aspect = True
elif projection == 'position':
get_u = lambda t: t['x']
get_v = lambda t: t['y']
xlabel, ylabel = ('x', 'y')
equal_aspect = True
elif projection == 'frequency':
get_u = lambda t: t['xi']
get_v = lambda t: t['eta']
xlabel, ylabel = ('ξ', 'η')
equal_aspect = True
elif projection == 'mixed_y':
get_u = lambda t: t['y']
get_v = lambda t: t['eta']
xlabel, ylabel = ('y', 'η')
equal_aspect = True
else:
raise ValueError(f"Unknown projection '{projection}' for dim=2. Choose 'cotangent', 'position', 'frequency', 'mixed_x', 'mixed_y', or 'full'.")
for idx, traj in enumerate(trajs):
c = colors[idx]
u = get_u(traj)
v = get_v(traj)
if show_flow:
axes.plot(u, v, color=c, alpha=0.65, lw=1.5)
if show_endpoints:
axes.plot(u[0], v[0], 'o', color='limegreen', ms=7, zorder=5, label='initial' if idx == 0 else '')
axes.plot(u[-1], v[-1], 's', color='crimson', ms=5, zorder=5, label='final' if idx == 0 else '')
axes.set_xlabel(xlabel, fontsize=12)
axes.set_ylabel(ylabel, fontsize=12)
if equal_aspect:
axes.set_aspect('equal', adjustable='datalim')
if title is None:
dim_str = f'{dim}D'
title = f'Wavefront set WF(u) [{dim_str}, projection: {projection}]'
axes.set_title(title, fontsize=12)
axes.grid(alpha=0.3)
if show_endpoints:
handles, labels = axes.get_legend_handles_labels()
if labels:
axes.legend(fontsize=9)
plt.tight_layout()
return (fig, axes)
def _wavefront_flow_batch(
symbol: Expr,
z0: np.ndarray,
tspan: Tuple[float, float],
dim: int,
n_steps: int,
max_points: Optional[int] = None,
scheme: str = 'auto'
) -> Dict[str, np.ndarray]:
"""Integrate many bicharacteristics at once (vectorised over rays).
The symbolic differentiation / lambdify is done once and each step updates every
ray simultaneously with NumPy arrays. The stability matrix, which plotting does
not need, is not computed.
Parameters
----------
z0 : ndarray, shape (N, 2*dim)
Initial points (x, xi) in 1D or (x, y, xi, eta) in 2D.
max_points : int, optional
Store at most about this many points per ray (every k-th step, always
including the first and the last one). The integration itself still uses
all `n_steps` steps; only the memory used by the result is reduced.
scheme : {'auto', 'euler', 'verlet'}
'euler': fixed-step symplectic Euler (first order), the scheme of
`bicharacteristic_flow(..., method='symplectic')` with the same time grid.
'verlet': velocity Verlet / Stormer scheme (second order, symplectic,
time-reversible), only valid for separable symbols p = T(xi) + V(x); its time
grid is exactly linspace(t0, t1, n_steps). 'auto' (default) uses 'verlet'
for separable symbols and 'euler' otherwise.
Returns
-------
dict
't' of shape (m,) and, for each coordinate name, an array of shape (N, m),
where m = n_steps unless `max_points` decimates the output.
"""
if dim == 1:
x, xi = symbols('x xi', real=True)
args, names = (x, xi), ['x', 'xi']
qs, ps = [x], [xi]
else:
x, y, xi, eta = symbols('x y xi eta', real=True)
args, names = (x, y, xi, eta), ['x', 'y', 'xi', 'eta']
qs, ps = [x, y], [xi, eta]
dpos = [diff(symbol, p_) for p_ in ps]
dmom = [-diff(symbol, q_) for q_ in qs]
if scheme not in ('auto', 'euler', 'verlet'):
raise ValueError("scheme must be 'auto', 'euler' or 'verlet'.")
separable = all(expand(diff(symbol, q_, p_)) == 0 for q_ in qs for p_ in ps)
if scheme == 'auto':
scheme = 'verlet' if separable else 'euler'
if scheme == 'verlet' and not separable:
raise ValueError("scheme='verlet' requires a separable symbol p = T(xi) + V(x).")
f_pos = [lambdify(args, e, 'numpy') for e in dpos]
f_mom = [lambdify(args, e, 'numpy') for e in dmom]
z0 = np.asarray(z0, dtype=float)
N = z0.shape[0]
t = np.linspace(tspan[0], tspan[1], n_steps)
if scheme == 'verlet':
dt = (tspan[1] - tspan[0]) / max(n_steps - 1, 1)
else:
dt = (tspan[1] - tspan[0]) / n_steps
keep = np.zeros(n_steps, dtype=bool)
if max_points is None or n_steps <= max_points:
keep[:] = True
else:
keep[::int(np.ceil(n_steps / max_points))] = True
keep[-1] = True
slot = np.cumsum(keep) - 1
out = np.empty((2 * dim, N, int(keep.sum())))
ones = np.ones(N)
q = [z0[:, k].copy() for k in range(dim)]
p = [z0[:, dim + k].copy() for k in range(dim)]
def _ev(fs, *a):
return [np.asarray(f(*a), dtype=float) * ones for f in fs]
out[:dim, :, 0] = np.array(q)
out[dim:, :, 0] = np.array(p)
if scheme == 'verlet':
kick = _ev(f_mom, *q, *p)
for i in range(n_steps - 1):
p = [p[k] + 0.5 * dt * kick[k] for k in range(dim)]
drift = _ev(f_pos, *q, *p)
q = [q[k] + dt * drift[k] for k in range(dim)]
kick = _ev(f_mom, *q, *p)
p = [p[k] + 0.5 * dt * kick[k] for k in range(dim)]
if keep[i + 1]:
out[:dim, :, slot[i + 1]] = np.array(q)
out[dim:, :, slot[i + 1]] = np.array(p)
else:
for i in range(n_steps - 1):
kick = _ev(f_mom, *q, *p)
p = [p[k] + dt * kick[k] for k in range(dim)]
drift = _ev(f_pos, *q, *p)
q = [q[k] + dt * drift[k] for k in range(dim)]
if keep[i + 1]:
out[:dim, :, slot[i + 1]] = np.array(q)
out[dim:, :, slot[i + 1]] = np.array(p)
res = {'t': t[keep]}
for k, name in enumerate(names):
res[name] = out[k]
return res
def _wavefront_curve_catalogue() -> Dict[str, Dict[str, Any]]:
"""Build the catalogue of ready-made curves (see WAVEFRONT_CURVES)."""
from sympy import Symbol, cos, sin, cosh, sinh, exp, pi
s = Symbol('s', real=True)
TAU = 2 * pi
c = {}
def add(name, xy, rng, note):
c[name] = {'curve': xy, 'range': (float(rng[0]), float(rng[1])), 'note': note}
add('circle', (cos(s), sin(s)), (0, TAU),
'Smooth, closed, constant curvature: the conormal rays focus at the centre (a point caustic).')
add('ellipse', (2 * cos(s), sin(s)), (0, TAU),
'Closed, curvature varies: inward conormal rays envelope the evolute (a caustic with 4 cusps).')
add('parabola', (s, s ** 2), (-2, 2),
'Open curve: inward conormal rays envelope the evolute (a semicubical parabola).')
add('hyperbola', (cosh(s), sinh(s)), (-1.5, 1.5),
'One branch of x² - y² = 1; open curve with curvature decaying at infinity.')
add('line', (s, 0 * s), (-3, 3),
'Zero curvature: a plane wave, the conormals are parallel and no caustic ever forms.')
add('sine', (s, sin(s)), (-2 * pi, 2 * pi),
'Inflection points: the curvature changes sign, so rays switch between converging and diverging.')
add('cubic', (s, s ** 3), (-1.2, 1.2),
'Flat inflection at the origin (zero curvature there).')
add('cusp', (s ** 2, s ** 3), (-1.2, 1.2),
'Semicubical parabola: a cusp at s=0 where the velocity vanishes (that seed is skipped).')
add('astroid', (cos(s) ** 3, sin(s) ** 3), (0, TAU),
'Four cusps (velocity vanishes there); strongly non-convex between them.')
add('cardioid', ((1 + cos(s)) * cos(s), (1 + cos(s)) * sin(s)), (0, TAU),
'One cusp; the curvature is large near it.')
add('nephroid', (3 * cos(s) - cos(3 * s), 3 * sin(s) - sin(3 * s)), (0, TAU),
'Two cusps; also the caustic of light rays in a circular cup.')
add('deltoid', (2 * cos(s) + cos(2 * s), 2 * sin(s) - sin(2 * s)), (0, TAU),
'Three cusps (hypocycloid).')
add('limacon_loop', ((1 + 2 * cos(s)) * cos(s), (1 + 2 * cos(s)) * sin(s)), (0, TAU),
'Limaçon with an inner loop: one self-crossing, the loop has opposite orientation.')
add('lemniscate', (cos(s) / (1 + sin(s) ** 2), sin(s) * cos(s) / (1 + sin(s) ** 2)), (0, TAU),
'Figure eight: a transversal self-crossing at the origin, where two branches of WF meet.')
add('lissajous_3_2', (sin(3 * s), sin(2 * s)), (0, TAU),
'Lissajous 3:2: several transversal self-crossings, a "braid" in the conormal bundle.')
add('trefoil', (sin(s) + 2 * sin(2 * s), cos(s) - 2 * cos(2 * s)), (0, TAU),
'Planar projection of the trefoil knot: three lobes and three crossings.')
add('archimedes_spiral', (s * cos(s), s * sin(s)), (0, 4 * pi),
'Open spiral with evenly spaced turns; the curvature decays outward.')
add('log_spiral', (exp(0.15 * s) * cos(s), exp(0.15 * s) * sin(s)), (0, 4 * pi),
'Self-similar spiral: the conormal rays make a constant angle with the radius.')
add('cycloid', (s - sin(s), 1 - cos(s)), (0, 4 * pi),
'Arches meeting in cusps at s = 2πk (velocity vanishes there).')
add('rose3', (cos(3 * s) * cos(s), cos(3 * s) * sin(s)), (0, pi),
'Three-petal rose: passes through the origin three times with different tangents.')
return c
WAVEFRONT_CURVES: Dict[str, Dict[str, Any]] = _wavefront_curve_catalogue()
"""Ready-made curves for ``animate_wavefront_set(..., seed_family='curve', curve=<name>)``.
Each entry has ``'curve'`` (a pair of sympy expressions in a parameter s),
``'range'`` (the default parameter interval) and ``'note'`` (why the curve
is interesting for wavefront-set propagation). Names: circle, ellipse,
parabola, hyperbola, line, sine, cubic, cusp, astroid, cardioid, nephroid,
deltoid, limacon_loop, lemniscate, lissajous_3_2, trefoil,
archimedes_spiral, log_spiral, cycloid, rose3.
"""
def _fin_range(arrays: Sequence[np.ndarray]) -> Tuple[float, float]:
"""(min, max) over the finite entries of several arrays ((0, 1) if there are none)."""
vals = np.concatenate([np.asarray(a, dtype=float).ravel() for a in arrays])
vals = vals[np.isfinite(vals)]
return (float(vals.min()), float(vals.max())) if vals.size else (0.0, 1.0)
def _last_finite(*arrays: np.ndarray) -> np.ndarray:
"""For each row, index of the last column where all the arrays are finite (0 if none)."""
ok = np.ones(arrays[0].shape, dtype=bool)
for a in arrays:
ok &= np.isfinite(a)
n = ok.shape[1]
idx = np.where(ok.any(axis=1), n - 1 - np.argmax(ok[:, ::-1], axis=1), 0)
return idx
def _wavefront_3d_animation(
frames: List[Tuple[Dict[str, np.ndarray], float]],
dim: int,
var_label: str,
coords3d: Optional[Sequence[str]],
color_by: Optional[str],
cmap: str,
show_flow: bool,
show_endpoints: bool,
pad: float,
xlim: Optional[Tuple[float, float]],
ylim: Optional[Tuple[float, float]],
zlim: Optional[Tuple[float, float]],
fixed_limits: bool,
figsize: Optional[Tuple[float, float]],
view: Tuple[float, float],
rotate: float,
title: Optional[str],
ax: Optional[Any],
interval: int,
save_path: Optional[str],
i0: Optional[int] = None,
ortho: bool = False
) -> FuncAnimation:
"""3D version of `animate_wavefront_set` (see its docstring)."""
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d.art3d import Line3DCollection
phase = ['x', 'y', 'xi', 'eta'] if dim == 2 else ['x', 'xi']
allowed = phase + ['t'] + (['u', 'v', 'phi', 'kappa'] if dim == 2 else ['kappa'])
def _derived(d):
"""Add the derived coordinates: covector norm 'kappa', and in 2D the sign-invariant
frequencies 'u', 'v' and the covector angle 'phi' (continuous along each ray, then shifted by a
multiple of 2π so that its value at the seed lies in [-π, π); the reversals by π at the points
where kappa = 0 are not unwrapped)."""
out = dict(d)
if dim == 2:
out['u'] = d['xi'] ** 2 - d['eta'] ** 2
out['v'] = 2 * d['xi'] * d['eta']
out['kappa'] = np.hypot(d['xi'], d['eta'])
raw = np.arctan2(d['eta'], d['xi'])
# unwrap only the 2π wraps of arctan2 (steps of about 2π); the reversals by π that happen where
# kappa = 0 (turning points of radial rays) are left as they are, instead of being accumulated
un = np.unwrap(raw, axis=1, discont=1.5 * np.pi)
ref = un[:, 0 if i0 is None else i0]
principal = (ref + np.pi) % (2 * np.pi) - np.pi
out['phi'] = un - (ref - principal)[:, None]
else:
out['kappa'] = np.abs(d['xi'])
return out
frames = [(_derived(d), v) for d, v in frames]
if coords3d is None:
coords3d = ('x', 'y', 'xi') if dim == 2 else ('x', 'xi', 't')
coords3d = tuple(coords3d)
if len(coords3d) != 3 or len(set(coords3d)) != 3 or any(c not in allowed for c in coords3d):
raise ValueError(f"coords3d must be 3 distinct names among {allowed}, got {coords3d}.")
if color_by is None:
rest = [c for c in phase if c not in coords3d]
if 'phi' in coords3d and 'kappa' not in coords3d:
color_by = 'kappa' # (x, y, φ) shows the angle; the norm is the missing information
else:
color_by = rest[0] if rest else 'index'
if color_by != 'index' and color_by not in allowed:
raise ValueError(f"color_by must be 'index' or one of {allowed}, got '{color_by}'.")
labs = {'x': 'x', 'y': 'y', 'xi': 'ξ', 'eta': 'η', 't': 't', 'u': 'ξ²−η²', 'v': '2ξη',
'phi': 'φ (covector angle)', 'kappa': '|κ| (covector norm)'}
n_pts = frames[0][0]['x'].shape[1]
sel = np.unique(np.concatenate([np.arange(0, n_pts, max(1, n_pts // 200)), [n_pts - 1], [] if i0 is None else [i0]]).astype(int)) # light-weight rendering
def _colour_values(d):
n = d['x'].shape[0]
if color_by == 'index':
return np.repeat(np.linspace(0, 1, n)[:, None], len(sel), axis=1)
return np.asarray(d[color_by])[:, sel]
cvals = [_colour_values(d) for d, _ in frames]
cmin, cmax = _fin_range(cvals)
if cmax <= cmin:
cmax = cmin + 1.0
if ax is None:
fig = plt.figure(figsize=figsize or (9, 7))
axes = fig.add_subplot(111, projection='3d')
created = True
else:
axes, fig, created = ax, ax.get_figure(), False
if not hasattr(axes, 'get_zlim'):
raise ValueError("For projection='3d', `ax` must be a 3D axes (projection='3d').")
def _pts(d):
return np.stack([np.asarray(d[c])[:, sel] for c in coords3d], axis=-1) # (n, m, 3)
def _padded(lo, hi):
span = hi - lo
m = span * pad if span > 0 else (abs(lo) * pad or 1.0)
return lo - m, hi + m
def _set_limits(subset):
P = np.concatenate([_pts(d).reshape(-1, 3) for d, _ in subset])
for k, (setter, user) in enumerate(((axes.set_xlim, xlim), (axes.set_ylim, ylim), (axes.set_zlim, zlim))):
setter(*(user or _padded(*_fin_range([P[:, k]]))))
_set_limits(frames)
axes.set_xlabel(labs[coords3d[0]], fontsize=12)
axes.set_ylabel(labs[coords3d[1]], fontsize=12)
axes.set_zlabel(labs[coords3d[2]], fontsize=12)
try: # honour zorder (otherwise markers are depth-sorted behind the semi-transparent strips)
axes.computed_zorder = False
except AttributeError:
pass
if ortho:
axes.set_proj_type('ortho')
axes.view_init(elev=view[0], azim=view[1])
base_title = title or f'Wavefront set WF(u) [{dim}D, 3D: ({", ".join(labs[c] for c in coords3d)})]'
title_artist = axes.set_title(base_title, fontsize=12)
def _segments(P):
return np.stack([P[:, :-1], P[:, 1:]], axis=2).reshape(-1, 2, 3)
def _seg_colours(C):
return (0.5 * (C[:, :-1] + C[:, 1:])).ravel()
lc = None
if show_flow:
lc = Line3DCollection(_segments(_pts(frames[0][0])), cmap=cmap, linewidths=1.4, alpha=0.8, zorder=1)
lc.set_array(_seg_colours(cvals[0]))
lc.set_clim(cmin, cmax)
axes.add_collection3d(lc, autolim=False)
if color_by != 'index':
cb = fig.colorbar(lc, ax=axes, shrink=0.6, pad=0.1)
cb.set_label(labs[color_by] + ' (colour)')
st = hd = None
if show_endpoints:
st, = axes.plot([], [], [], 'o', color='limegreen', ms=6, markeredgecolor='k', markeredgewidth=0.4,
zorder=10, label='initial')
hd, = axes.plot([], [], [], 's', color='crimson', ms=4.5, markeredgewidth=0, zorder=9, label='final')
axes.legend(fontsize=9, loc='upper left')
def update(i):
d, v = frames[i]
P = _pts(d)
if lc is not None:
lc.set_segments(_segments(P))
lc.set_array(_seg_colours(cvals[i]))
if show_endpoints:
k0 = 0 if i0 is None else int(np.where(sel == i0)[0][0])
last = _last_finite(P[..., 0], P[..., 1], P[..., 2]) # diverged rays end at their last finite point
rows = np.arange(P.shape[0])
ends = (P[rows, last][:, None] if i0 is None else np.stack([P[:, 0], P[rows, last]], axis=1)).reshape(-1, 3)
st.set_data(P[:, k0, 0], P[:, k0, 1]); st.set_3d_properties(P[:, k0, 2])
hd.set_data(ends[:, 0], ends[:, 1]); hd.set_3d_properties(ends[:, 2])
if not fixed_limits:
_set_limits([frames[i]])
if rotate:
axes.view_init(elev=view[0], azim=view[1] + rotate * i)
title_artist.set_text(f"{base_title} [{var_label}₀ = {v:.2f}]")
return [a for a in (lc, st, hd) if a is not None]
update(0)
if created:
fig.tight_layout()
anim = FuncAnimation(fig, update, frames=len(frames), interval=interval, blit=False)
if save_path:
anim.save(save_path)
if created:
plt.close(fig)
return anim
[docs]
def animate_wavefront_set(
symbol: Expr,
tspan: Tuple[float, float],
x0: Optional[float] = None,
y0: Optional[float] = None,
xi0: Optional[float] = None,
eta0: Optional[float] = None,
x_range: Optional[Tuple[float, float]] = None,
y_range: Optional[Tuple[float, float]] = None,
xi_range: Optional[Tuple[float, float]] = None,
eta_range: Optional[Tuple[float, float]] = None,
seeds: int = 100,
radius: float = 0.15,
spread: float = 2.0,
seed_family: Literal['circle', 'fiber', 'grid', 'rectangle', 'curve'] = 'circle',
half_turn: bool = True,
corner_ul: Optional[Tuple[float, float]] = None,
corner_lr: Optional[Tuple[float, float]] = None,
step: Union[float, Tuple[float, float]] = 0.5,
phase: Optional[Expr] = None,
both_normals: bool = True,
fan: int = 9,
curve: Optional[Tuple[Any, Any]] = None,
curve_range: Optional[Tuple[float, float]] = None,
covector_angle: Optional[Any] = None,
closed: Optional[bool] = None,
two_sided: Optional[bool] = None,
n_frames: int = 40,
dim: Optional[int] = None,
projection: str = 'cotangent',
n_steps: int = 500,
cmap: str = 'plasma',
show_flow: bool = True,
show_endpoints: bool = True,
pad: float = 0.25,
xlim: Optional[Union[Tuple[float, float], Sequence[Optional[Tuple[float, float]]]]] = None,
ylim: Optional[Union[Tuple[float, float], Sequence[Optional[Tuple[float, float]]]]] = None,
figsize: Optional[Tuple[float, float]] = None,
equal_aspect: Optional[bool] = None,
fixed_limits: bool = True,
fast: bool = True,
scheme: str = 'auto',
max_points: int = 1000,
energy: Optional[float] = None,
coords3d: Optional[Sequence[str]] = None,
color_by: Optional[str] = None,
zlim: Optional[Tuple[float, float]] = None,
view: Tuple[float, float] = (25.0, -60.0),
rotate: float = 0.0,
ortho: bool = False,
title: Optional[str] = None,
ax: Optional[Union[Axes, Sequence[Axes]]] = None,
interval: int = 80,
save_path: Optional[str] = None
) -> FuncAnimation:
"""
Animate the wavefront set WF(u) while one initial coordinate varies.
Each frame is the picture `visualize_wavefront_set` /
`interactive_symbol_analysis` would give for one value of the
animated variable: a family of `seeds` seed singularities is
propagated along the bicharacteristics over `tspan` and drawn
(strips, initial points in green, end points in red). The animated
variable is the coordinate that has a range; it sweeps this range
over `n_frames` frames while the other coordinates stay fixed.
The seed family is the default one of `visualize_wavefront_set`:
- 2D: a point-source circle of radius `radius` around (x0, y0), with
outward covectors of norm |(ξ0, η0)|. Varying x or y moves the
circle; varying ξ or η changes the norm of the covectors.
- 1D: a fan of `seeds` frequencies ξ ∈ [ξ0 - spread, ξ0 + spread] at
the point x0. Varying x moves the fan, varying ξ shifts it.
Möbius strip: the covector (ξ, η) and its opposite -(ξ, η) define the
same line, and since p is even they give the same geometric ray run
in opposite directions: (ξ0, η0) at time t is the same point as
-(ξ0, η0) at time -t. Taking the whole fiber over a point
(directions modulo π, times in [-T, T]) therefore gives a strip whose
two edges are glued with a flip of t, i.e. a Möbius strip. To see it,
plot quantities that are invariant under (ξ, η) -> -(ξ, η) in
projection='3d', e.g. coords3d=('u', 'v', 'y') with the doubled-angle
frequencies u = ξ² - η², v = 2ξη.
Parameters
----------
symbol : sympy expression
Principal symbol p(x, ξ) in 1D or p(x, y, ξ, η) in 2D.
tspan : tuple
(t_start, t_end) for bicharacteristic integration.
x0, y0, xi0, eta0 : float, optional
Fixed values of the coordinates (defaults 0, 0, 1, 0, as in
`visualize_wavefront_set`). The one that has a range is ignored.
x_range, y_range, xi_range, eta_range : tuple (lo, hi)
Range of the animated variable. Give exactly one (y and eta only
in 2D).
seeds : int, default=100
Number of rays in each frame.
radius : float, default=0.15
2D: radius of the circle of seed points.
spread : float, default=2.0
1D: half-width of the fan of frequencies.
seed_family : {'circle', 'fiber', 'grid', 'rectangle', 'curve'}, default='circle'
2D only. 'circle': the point-source circle described above.
'fiber': all seeds sit at the single point (x0, y0) and only the
direction of the covector varies, over a half turn (see
`half_turn`) with norm |(ξ0, η0)|; every ray is integrated in
both time directions (the seed is at t = 0). Modulo the
identification (ξ, η) ~ -(ξ, η) of a covector with its opposite,
this is a Möbius strip (see the notes below). `radius` is
ignored. If tspan[0] >= 0 the flow is run over
(-tspan[1], tspan[1]); otherwise over tspan.
'grid': one seed at every node of a rectangular grid (see
`corner_ul`, `corner_lr`, `step`), all with the same covector
(ξ0, η0), or with the covector ∇S(x, y) if a phase function `phase`
= S(x, y) is given (the initial Lagrangian manifold of a
WKB / plane wave). 'rectangle': the wavefront set of the
indicator of the rectangle: seeds along the four edges (spaced by
`step`) with the unit conormals times |(ξ0, η0)|, plus a fan of
`fan` directions at each corner. For both, `seeds` is ignored,
and (x0, y0) is an offset that shifts the whole rectangle
(default 0, 0), so x_range / y_range slide it; with 'rectangle'
xi_range / eta_range change the covector norm, with 'grid' they
change the constant covector (not allowed together with `phase`).
With many rays use show_flow=False and a 3D view.
'curve': the conormal bundle of a parametrised curve
(x(s), y(s)), s ∈ `curve_range`: `seeds` points along the curve,
each with the unit conormal (y'(s), -x'(s))/|γ'(s)| times
|(ξ0, η0)| (the right-hand normal, outward for a curve run
counter-clockwise), plus the opposite conormal if `both_normals`.
This is the wavefront set of a distribution singular along the
curve (a line, circle, ellipse, parabola ...). (x0, y0) shifts
the curve and xi_range / eta_range change the covector norm, as
for 'rectangle'. Points where γ'(s) = 0 are skipped; corners of
piecewise curves are not filled in (use 'rectangle' for that).
corner_ul, corner_lr : tuple (x, y)
'grid' / 'rectangle': upper-left and lower-right corners.
step : float or tuple (dx, dy), default=0.5
'grid' / 'rectangle': spacing along the x and y axes (the spacing
is adjusted slightly so that the far edge is hit exactly).
phase : sympy expression in x, y, optional
'grid' only: phase function S(x, y); the covector at each node is
(∂S/∂x, ∂S/∂y) evaluated at the (shifted) node.
curve : str or tuple (x(s), y(s)), optional
'curve' only: the name of a ready-made curve (a key of
`WAVEFRONT_CURVES`, e.g. 'astroid'), or two sympy expressions in
one common parameter symbol (e.g. ``(2*cos(s), sin(s))``;
constants are allowed for one component), or two vectorised
callables ``f(s) -> ndarray``.
curve_range : tuple (s0, s1), optional
'curve' only: parameter interval (default: the catalogue range
for a named curve, else (0, 2π)).
covector_angle : expression in s or callable, optional
'curve' only: direction φ(s) of the covector (cos φ, sin φ),
instead of the normal to the curve. The curve may then be a
single point, e.g. ``curve=(0, 0), covector_angle=s``. This is how
'circle' (normal of a circle) and 'fiber' (constant position,
rotating covector) are special cases of 'curve'.
two_sided : bool, optional
Integrate every ray in both time directions (seed at t = 0),
as 'fiber' does. Default: True for 'fiber', else False.
closed : bool, optional
'curve' only: if True the end point s1 is not repeated. Default:
detected automatically (curve(s0) == curve(s1)).
both_normals : bool, default=True
'rectangle' and 'curve': also seed the opposite conormals (WF is
symmetric under ξ -> -ξ for a real distribution).
fan : int, default=9
'rectangle' only: number of directions in the fan at each corner
(from one conormal to the other, and its opposite fan if
`both_normals`).
half_turn : bool, default=True
'fiber' only: directions φ ∈ [0, π] (True) or [0, 2π] (False,
which covers the Möbius strip twice). Both end points are
included, so the initial curve closes up in sign-invariant
coordinates such as ('u', 'v'); in (ξ, η) itself a half turn is
only an arc (use half_turn=False for the full circle there).
n_frames : int, default=40
Number of values of the animated variable (one frame each).
dim : int, optional
1 or 2. Inferred from the arguments (y0, eta0, y_range or
eta_range present -> 2D), else from the symbols of `symbol`.
projection, n_steps, cmap, show_flow, show_endpoints, title, ax
As in `plot_wavefront_set` (all projections, including 'full' in
2D); colour encodes the index of the ray. Additionally
projection='3d' draws the rays in a 3D plot (see `coords3d`).
pad : float, default=0.25
Margin around the rays on each side, as a fraction of the data
extent (computed over all frames). Increase it for a wider view.
xlim, ylim : tuple (lo, hi), optional
Explicit axis limits, overriding `pad`. For a single panel give
a tuple; for projection='full' (2D) give a list of 4 tuples (or
None entries) in the panel order position, frequency, (x, ξ),
(y, η). Limits stay fixed during the whole animation.
figsize : tuple (width, height) in inches, optional
Size of the figure created here (ignored if `ax` is given).
Default (12, 10) for projection='full' and (8, 6) otherwise.
equal_aspect : bool, optional
True: equal scaling on both axes of every panel. False: every
panel fills its cell. Default (None): equal scaling only for the
position panel (x, y).
fixed_limits : bool, default=True
True: the axis limits cover all frames and never change.
False: the limits are re-fitted to the current frame (ignored
for the axes with explicit `xlim`/`ylim`).
energy : float, optional
2D only. Put every seed on the energy shell p(x, y, ξ, η) = `energy`:
the direction of its covector is kept (circle: outward, curve:
conormal, grid: constant covector or ∇S, ...) and its norm is
solved for. This replaces the choice of |(ξ0, η0)|, which is then
ignored, and gives the picture of the singularities of a solution
of (P - E)u = 0. The norm is the smallest positive root of
p(x, y, κu) = E along each direction u, which assumes that p
increases with κ (as for kinetic + potential energy). Seeds in the
classically forbidden region (no root) are dropped with a warning,
and an error is raised if a frame has no seed left. With `energy`,
xi_range / eta_range cannot be the animation variable, except for
seed_family='grid' (where they change the direction of the covector).
fast : bool, default=True
True: integrate all the rays of all the frames at once with
NumPy (orders of magnitude faster than ray by ray). False: call
`bicharacteristic_flow` ray by ray (slow reference path, always
symplectic Euler).
scheme : {'auto', 'euler', 'verlet'}, default='auto'
Integration scheme of the fast path. 'euler': fixed-step symplectic
Euler (first order, as `bicharacteristic_flow`). 'verlet': velocity
Verlet (second order, symplectic, time-reversible), valid for
separable symbols p = T(ξ, η) + V(x, y) such as kinetic + potential
energy. 'auto' uses 'verlet' when the symbol is separable and
'euler' otherwise. Use scheme='euler' to compare with
``fast=False``.
max_points : int, default=1000
Maximum number of points stored (and drawn) per ray; the
integration still uses all `n_steps` steps, so a large `n_steps`
improves the accuracy without increasing the memory.
coords3d : tuple of 3 names, optional
projection='3d' only: which three coordinates are the axes, among
'x', 'y', 'xi', 'eta' (2D) or 'x', 'xi' (1D), 't' (time) and the
derived quantities 'kappa' = |(ξ, η)| (covector norm; |ξ| in 1D)
and, in 2D only, 'phi' = arg(ξ + iη) (covector angle, made
continuous along each ray and equal to a value in [-π, π) at the
seed; where the covector vanishes, e.g. at the turning points of
bound radial rays, the covector reverses and 'phi' jumps by π,
which is left as it is) and the sign-invariant frequencies
'u' = ξ²-η², 'v' = 2ξη. For example
``coords3d=('x', 'y', 'phi')`` draws the total space of the
cotangent bundle as the base plane plus a circle fiber.
Default ('x', 'y', 'xi') in 2D and ('x', 'xi', 't') in 1D.
color_by : str, optional
projection='3d' only: quantity mapped to colour -- a coordinate
name, 't' or 'index' (ray index). Default: the 4th phase-space
coordinate not used by the axes (e.g. 'eta'), so that all four
dimensions of T*ℝ² are visible; 'kappa' if 'phi' is one of the
axes; 'index' in 1D.
zlim : tuple (lo, hi), optional
projection='3d' only: explicit z limits (`xlim` and `ylim` give
the x and y limits, each a plain tuple).
view : tuple (elevation, azimuth), default (25, -60)
projection='3d' only: camera angles in degrees.
rotate : float, default=0.0
projection='3d' only: degrees the camera turns around the z axis
at each frame (e.g. 3.0 for a slowly turning view).
ortho : bool, default=False
projection='3d' only: use an orthographic camera instead of the
default perspective one, so that parallel lines stay parallel
(planes such as t = 0 then look flat from any angle).
interval : int, default=80
Delay between frames, in milliseconds.
save_path : str, optional
If given, the animation is saved to this path via `anim.save`.
Returns
-------
matplotlib.animation.FuncAnimation
The animation object (``anim._fig`` is the figure). The figure is
closed if created here; use ``HTML(anim.to_jshtml())`` in a
notebook.
Special cases (the older families written as curves; `radius`, x0, y0 ...
as above)::
# seed_family='circle'
curve=(radius*cos(s), radius*sin(s)), both_normals=False
# seed_family='fiber' (half_turn=True)
curve=(0, 0), covector_angle=s, curve_range=(0, np.pi),
both_normals=False, two_sided=True
Examples
--------
::
anim = animate_wavefront_set(p, tspan=(0, 4), x0=1, xi0=1, eta0=1,
y_range=(-5, 5), seeds=100,
projection='position')
# 3D view of the 4D phase space: axes (x, y, ξ), colour = η
anim = animate_wavefront_set(p, tspan=(0, 4), x0=1, xi0=1, eta0=1,
y_range=(-5, 5), seeds=100,
projection='3d', rotate=2.0)
# wavefront set of an ellipse: conormals along the curve
anim = animate_wavefront_set(p, tspan=(0, 2), seed_family='curve',
curve=(2*cos(s), sin(s)), seeds=60,
xi0=1, eta0=0, x_range=(-1, 1))
# Möbius strip: whole fiber over a point, both time directions
anim = animate_wavefront_set(p, tspan=(0, 3), x0=0, xi0=1, eta0=0,
y_range=(0.2, 1.0), seeds=120,
seed_family='fiber', projection='3d',
coords3d=('u', 'v', 'y'))
# 1D
anim = animate_wavefront_set(p, tspan=(0, 4), x0=1, xi_range=(-2, 2),
seeds=40)
"""
from matplotlib.animation import FuncAnimation
from matplotlib.collections import LineCollection
given = {'x': x0, 'y': y0, 'xi': xi0, 'eta': eta0}
ranges = {'x': x_range, 'y': y_range, 'xi': xi_range, 'eta': eta_range}
if dim is None:
if any(given[k] is not None or ranges[k] is not None for k in ('y', 'eta')):
dim = 2
else:
names = {s.name for s in symbol.free_symbols}
dim = 2 if ({'y', 'eta'} & names) else 1
coord_names = ['x', 'y', 'xi', 'eta'] if dim == 2 else ['x', 'xi']
extra = [k for k in ('y', 'eta') if dim == 1 and (given[k] is not None or ranges[k] is not None)]
if extra:
raise ValueError(f"{extra} given but dim=1.")
swept = [k for k in coord_names if ranges[k] is not None]
if len(swept) != 1:
raise ValueError(f"Give exactly one range among {[k + '_range' for k in coord_names]}; got {len(swept)}.")
var = swept[0]
if given[var] is not None:
raise ValueError(f"'{var}' has a range, so '{var}0' must not be given.")
if seed_family not in ('circle', 'fiber', 'grid', 'rectangle', 'curve'):
raise ValueError("seed_family must be 'circle', 'fiber', 'grid', 'rectangle' or 'curve'.")
if seed_family != 'circle' and dim != 2:
raise ValueError(f"seed_family='{seed_family}' requires dim=2.")
rect_pts, grid_pts, f_grad, curve_pts = None, None, None, None
if seed_family == 'curve':
if curve is None:
raise ValueError("`curve=(x(s), y(s))` (or a name of WAVEFRONT_CURVES) is required for seed_family='curve'.")
default_range = (0.0, 2 * np.pi)
if isinstance(curve, str):
if curve not in WAVEFRONT_CURVES:
raise ValueError(f"Unknown curve '{curve}'. Available: {sorted(WAVEFRONT_CURVES)}.")
default_range = WAVEFRONT_CURVES[curve]['range']
curve = WAVEFRONT_CURVES[curve]['curve']
s0, s1 = curve_range if curve_range is not None else default_range
if not s1 > s0:
raise ValueError("curve_range must satisfy s0 < s1.")
cxe, cye = curve
ang = covector_angle
h = 1e-6 * (s1 - s0)
def _wrap(f):
return lambda t: np.broadcast_to(np.asarray(f(t), dtype=float), np.shape(t))
if callable(cxe) and callable(cye):
_fx, _fy = _wrap(cxe), _wrap(cye)
_dfx = lambda t: (_fx(t + h) - _fx(t - h)) / (2 * h)
_dfy = lambda t: (_fy(t + h) - _fy(t - h)) / (2 * h)
_fa = _wrap(ang) if callable(ang) else None
if ang is not None and _fa is None:
raise ValueError("With callable curve components, covector_angle must be callable too.")
else:
params = set(getattr(cxe, 'free_symbols', ())) | set(getattr(cye, 'free_symbols', ())) \
| set(getattr(ang, 'free_symbols', ()))
if len(params) != 1:
raise ValueError("The curve (and covector_angle) expressions must depend on exactly one common "
f"parameter symbol (found {sorted(str(q) for q in params)}).")
sp_ = next(iter(params))
def _mk(e):
f = lambdify(sp_, e, 'numpy')
return lambda t: np.broadcast_to(np.asarray(f(t), dtype=float), np.shape(t))
_fx, _fy = _mk(cxe), _mk(cye)
_dfx, _dfy = _mk(diff(cxe, sp_)), _mk(diff(cye, sp_))
_fa = _mk(ang) if ang is not None else None
e0, e1 = np.array(s0, dtype=float), np.array(s1, dtype=float)
same_pt = np.hypot(float(_fx(e1)) - float(_fx(e0)), float(_fy(e1)) - float(_fy(e0))) < 1e-9
same_dir = True if _fa is None else bool(
np.hypot(np.cos(float(_fa(e1))) - np.cos(float(_fa(e0))),
np.sin(float(_fa(e1))) - np.sin(float(_fa(e0)))) < 1e-9)
is_closed = closed if closed is not None else bool(same_pt and same_dir)
tt = np.linspace(s0, s1, seeds, endpoint=not is_closed)
px, py = _fx(tt), _fy(tt)
if _fa is not None:
phi = _fa(tt)
nxs, nys, ok = np.cos(phi), np.sin(phi), np.ones(len(tt), dtype=bool)
else:
tx, ty = _dfx(tt), _dfy(tt)
L = np.hypot(tx, ty)
ok = L > 1e-9
if not ok.any():
raise ValueError("The curve has zero velocity everywhere (give `covector_angle` for a point).")
with np.errstate(divide='ignore', invalid='ignore'):
nxs, nys = ty / L, -tx / L
curve_pts = []
for i in np.where(ok)[0]:
curve_pts.append((float(px[i]), float(py[i]), float(nxs[i]), float(nys[i])))
if both_normals:
curve_pts.append((float(px[i]), float(py[i]), float(-nxs[i]), float(-nys[i])))
if seed_family in ('grid', 'rectangle'):
if corner_ul is None or corner_lr is None:
raise ValueError("corner_ul and corner_lr are required for 'grid' and 'rectangle'.")
(xl, yt), (xr, yb) = corner_ul, corner_lr
dx, dy = (step, step) if np.isscalar(step) else step
if not (xr > xl and yt > yb):
raise ValueError("Need corner_lr to be to the right of and below corner_ul.")
if dx <= 0 or dy <= 0:
raise ValueError("step must be positive.")
if phase is not None and seed_family != 'grid':
raise ValueError("`phase` is only used with seed_family='grid'.")
if phase is not None and (xi_range is not None or eta_range is not None):
raise ValueError("With `phase`, the covector comes from ∇S: use x_range or y_range (shift).")
xs = np.linspace(xl, xr, max(1, int(np.ceil((xr - xl) / dx - 1e-9))) + 1)
ys = np.linspace(yb, yt, max(1, int(np.ceil((yt - yb) / dy - 1e-9))) + 1)
if seed_family == 'grid':
GX, GY = np.meshgrid(xs, ys)
grid_pts = np.c_[GX.ravel(), GY.ravel()]
if phase is not None:
_x, _y = symbols('x y', real=True)
f_grad = (lambdify((_x, _y), diff(phase, _x), 'numpy'),
lambdify((_x, _y), diff(phase, _y), 'numpy'))
else:
if fan < 2:
raise ValueError("fan must be >= 2.")
rect_pts = [] # (x, y, nx, ny) with unit covector directions
def _add(px, py, nx, ny):
rect_pts.append((px, py, nx, ny))
if both_normals:
rect_pts.append((px, py, -nx, -ny))
for px in xs[1:-1]:
_add(px, yb, 0.0, -1.0); _add(px, yt, 0.0, 1.0)
for py in ys[1:-1]:
_add(xl, py, -1.0, 0.0); _add(xr, py, 1.0, 0.0)
for (cx_, cy_), (a0, a1) in (((xr, yt), (0, np.pi / 2)), ((xl, yt), (np.pi / 2, np.pi)),
((xl, yb), (np.pi, 3 * np.pi / 2)), ((xr, yb), (3 * np.pi / 2, 2 * np.pi))):
for ang in np.linspace(a0, a1, fan):
_add(cx_, cy_, float(np.cos(ang)), float(np.sin(ang)))
two_sided = (seed_family == 'fiber') if two_sided is None else bool(two_sided)
if energy is not None:
if dim != 2:
raise ValueError("`energy` requires dim=2.")
if (xi_range is not None or eta_range is not None) and seed_family != 'grid':
raise ValueError("With `energy` the covector norm is solved for, so xi_range / eta_range cannot be the "
"animation variable (use x_range or y_range).")
defaults = {'x': 0.0, 'y': 0.0, 'xi': 1.0, 'eta': 0.0}
base = {k: float(defaults[k] if given[k] is None else given[k]) for k in coord_names}
values = np.linspace(ranges[var][0], ranges[var][1], max(2, n_frames))
def _seed_family(v):
c = {**base, var: float(v)}
if dim == 1:
xi_vals = np.linspace(c['xi'] - spread, c['xi'] + spread, seeds)
nonzero = xi_vals[np.abs(xi_vals) > 1e-06]
if len(nonzero) > 0:
xi_vals = nonzero
return [(c['x'], float(xi)) for xi in xi_vals]
mag = float(np.hypot(c['xi'], c['eta'])) or 1.0
if seed_family == 'grid':
P = grid_pts + np.array([c['x'], c['y']])
if f_grad is None:
gx = np.full(len(P), c['xi']); gy = np.full(len(P), c['eta'])
else:
gx = np.broadcast_to(np.asarray(f_grad[0](P[:, 0], P[:, 1]), dtype=float), len(P))
gy = np.broadcast_to(np.asarray(f_grad[1](P[:, 0], P[:, 1]), dtype=float), len(P))
return [(float(P[i, 0]), float(P[i, 1]), float(gx[i]), float(gy[i])) for i in range(len(P))]
if seed_family in ('rectangle', 'curve'):
pts = rect_pts if seed_family == 'rectangle' else curve_pts
return [(px + c['x'], py + c['y'], mag * nx, mag * ny) for px, py, nx, ny in pts]
if seed_family == 'fiber':
phis = np.linspace(0, np.pi if half_turn else 2 * np.pi, seeds) # both end points: the initial curve closes up
return [(c['x'], c['y'], mag * np.cos(f), mag * np.sin(f)) for f in phis]
angles = np.linspace(0, 2 * np.pi, seeds, endpoint=False)
return [(c['x'] + radius * np.cos(a), c['y'] + radius * np.sin(a),
mag * np.cos(a), mag * np.sin(a)) for a in angles]
# ---- flows: frames[i] = {coord name: array (n_rays_i, n_pts), 't': ...}
n_dropped = [0]
if energy is not None:
_sx, _sy, _sxi, _seta = symbols('x y xi eta', real=True)
_fp = lambdify((_sx, _sy, _sxi, _seta), symbol, 'numpy')
def _on_shell(fam):
"""Rescale every covector (keeping its direction) so that p = energy."""
Zf = np.array(fam, dtype=float).reshape(-1, 4)
X, Y, XI, ETA = Zf.T
nrm = np.hypot(XI, ETA)
good = nrm > 1e-12
ux = np.where(good, XI / np.where(good, nrm, 1.0), 1.0)
uy = np.where(good, ETA / np.where(good, nrm, 1.0), 0.0)
g = lambda kap: np.broadcast_to(np.asarray(_fp(X, Y, kap * ux, kap * uy), dtype=float) - energy, X.shape)
lo, hi = np.zeros_like(X), np.ones_like(X)
f0 = g(lo)
ok = good & (f0 < 0) # p(x, 0) < E: a positive root can exist
for _ in range(80): # bracket the root
grow = ok & (g(hi) < 0)
if not grow.any():
break
hi = np.where(grow, 2 * hi, hi)
ok &= g(hi) >= 0
for _ in range(70): # bisection
mid = 0.5 * (lo + hi)
neg = g(mid) < 0
lo, hi = np.where(neg, mid, lo), np.where(neg, hi, mid)
kap = 0.5 * (lo + hi)
n_dropped[0] += int((~ok).sum())
return [(float(X[i]), float(Y[i]), float(kap[i] * ux[i]), float(kap[i] * uy[i])) for i in np.where(ok)[0]]
families = [_on_shell(_seed_family(v)) for v in values]
for v, fam in zip(values, families):
if len(fam) == 0:
raise ValueError(f"No seed can reach the energy shell p = {energy} at {var} = {v:.3g} "
"(classically forbidden region): restrict the range.")
if n_dropped[0]:
import warnings
warnings.warn(f"{n_dropped[0]} seed(s) were dropped: classically forbidden region (p(x, 0) >= energy) "
"or zero covector direction.")
else:
families = [_seed_family(v) for v in values]
counts = [len(fam) for fam in families]
if sum(counts) > 50000:
import warnings
warnings.warn(f"{sum(counts)} rays will be integrated and drawn; consider a larger `step`, fewer `n_frames` or show_flow=False.")
if two_sided:
T = tspan[1] if tspan[0] >= 0 else None
t_fwd = (0.0, tspan[1]) if T is None else (0.0, T)
t_bwd = (0.0, tspan[0]) if T is None else (0.0, -T)
else:
t_fwd, t_bwd = tspan, None
def _two_sided(fwd, bwd):
# backward branch reversed (without t=0), then forward branch
out = {k: np.concatenate([bwd[k][:, :0:-1], fwd[k]], axis=1) for k in coord_names}
out['t'] = np.concatenate([np.asarray(bwd['t'])[:0:-1], np.asarray(fwd['t'])])
return out
if fast:
Z = np.array([z for fam in families for z in fam])
flat = _wavefront_flow_batch(symbol, Z, t_fwd, dim, n_steps, max_points=max_points, scheme=scheme)
if two_sided:
flat = _two_sided(flat, _wavefront_flow_batch(symbol, Z, t_bwd, dim, n_steps,
max_points=max_points, scheme=scheme))
bounds = np.cumsum([0] + counts)
frames = []
for i, v in enumerate(values):
sl = slice(bounds[i], bounds[i + 1])
d = {k: flat[k][sl] for k in coord_names}
d['t'] = np.broadcast_to(flat['t'], (counts[i], len(flat['t'])))
frames.append((d, v))
else:
def _slow(fam, span):
trajs = [bicharacteristic_flow(symbol, z0, span, dim=dim, method='symplectic', n_steps=n_steps)
for z0 in fam]
n = min(len(tr['x']) for tr in trajs)
return {k: np.array([np.asarray(tr[k])[:n] for tr in trajs]) for k in coord_names + ['t']}
frames = []
for fam, v in zip(families, values):
d = _slow(fam, t_fwd)
if two_sided:
d_b = _slow(fam, t_bwd)
d_b['t'] = d_b['t'][0]
d_f = dict(d); d_f['t'] = d['t'][0]
d = _two_sided(d_f, d_b)
d['t'] = np.broadcast_to(d['t'], (len(fam), len(d['t'])))
frames.append((d, v))
i0 = (frames[0][0]['x'].shape[1] - 1) // 2 if two_sided else None # index of the seed (t = 0) when two-sided
n_bad = 0
for d, _ in frames: # diverged integrations: inf -> nan, and count them
bad_rows = np.zeros(d['x'].shape[0], dtype=bool)
for k in coord_names:
fin = np.isfinite(d[k])
bad_rows |= ~fin.all(axis=1)
d[k] = np.where(fin, d[k], np.nan)
n_bad += int(bad_rows.sum())
if n_bad:
import warnings
warnings.warn(f"{n_bad} ray(s) diverged (non-finite values) and are drawn only up to their last finite point. "
"Use a larger n_steps, a shorter tspan or a smaller range to avoid this.")
if projection == '3d':
return _wavefront_3d_animation(frames, dim, {'x': 'x', 'y': 'y', 'xi': 'ξ', 'eta': 'η'}[var],
coords3d, color_by, cmap, show_flow, show_endpoints, pad,
xlim, ylim, zlim, fixed_limits, figsize, view, rotate,
title, ax, interval, save_path, i0=i0, ortho=ortho)
n_rays = max(counts)
colors = plt.get_cmap(cmap)(np.linspace(0.05, 0.95, n_rays))
lbl = {'x': 'x', 'y': 'y', 'xi': 'ξ', 'eta': 'η'}
# ---- panels ------------------------------------------------------
labs = {'x': 'x', 'xi': 'ξ', 'y': 'y', 'eta': 'η', 't': 't'}
if projection == 'full' and dim == 2:
panel_specs = [('x', 'y', 'Position space (x, y)'),
('xi', 'eta', 'Frequency space (ξ, η)'),
('x', 'xi', 'Cotangent slice (x, ξ)'),
('y', 'eta', 'Cotangent slice (y, η)')]
if ax is None:
fig, axes = plt.subplots(2, 2, figsize=figsize or (12, 10))
created = True
else:
axes = np.asarray(ax)
fig = axes.flatten()[0].get_figure()
created = False
ax_list = list(np.asarray(axes).flatten())
else:
if dim == 1:
table = {'cotangent': ('x', 'xi'), 'phase': ('x', 'xi'), 'position': ('t', 'x')}
else:
table = {'cotangent': ('x', 'xi'), 'mixed_x': ('x', 'xi'), 'position': ('x', 'y'),
'frequency': ('xi', 'eta'), 'mixed_y': ('y', 'eta')}
if projection not in table:
raise ValueError(f"Unknown projection '{projection}' for dim={dim}. "
f"Choose from {sorted(table)}" + (" or 'full'." if dim == 2 else "."))
ku, kv = table[projection]
panel_specs = [(ku, kv, None)]
if projection == 'position' and dim == 1:
labs['x'] = 'x (singular support)'
if ax is None:
fig, axes = plt.subplots(figsize=figsize or (8, 6))
created = True
else:
axes = ax
fig = axes.get_figure()
created = False
ax_list = [axes]
def _padded(lo, hi, frac=pad):
span = hi - lo
margin = span * frac if span > 0 else (abs(lo) * frac or 1.0)
return lo - margin, hi + margin
def _user_lim(lim, p):
if lim is None:
return None
if len(lim) == 2 and np.isscalar(lim[0]):
return tuple(lim)
return lim[p]
def _set_limits(p_idx, subset):
ku, kv, _ = panel_specs[p_idx]
lo_u, hi_u = _fin_range([d[ku] for d, _ in subset])
lo_v, hi_v = _fin_range([d[kv] for d, _ in subset])
ax_obj = ax_list[p_idx]
ax_obj.set_xlim(*(_user_lim(xlim, p_idx) or _padded(lo_u, hi_u)))
ax_obj.set_ylim(*(_user_lim(ylim, p_idx) or _padded(lo_v, hi_v)))
for p_idx, (ax_obj, (ku, kv, ptitle)) in enumerate(zip(ax_list, panel_specs)):
_set_limits(p_idx, frames)
ax_obj.set_xlabel(labs[ku], fontsize=12)
ax_obj.set_ylabel(labs[kv], fontsize=12)
if ptitle:
ax_obj.set_title(ptitle, fontsize=10)
ax_obj.grid(alpha=0.3)
use_equal = equal_aspect if equal_aspect is not None else (dim == 2 and (ku, kv) == ('x', 'y'))
if use_equal:
ax_obj.set_aspect('equal', adjustable='box')
base_title = title or (
'Wavefront set WF(u) [2D, full cotangent bundle]'
if (projection == 'full' and dim == 2)
else f'Wavefront set WF(u) [{dim}D, projection: {projection}]')
title_artist = (fig.suptitle if len(ax_list) > 1 else ax_list[0].set_title)(base_title)
# ---- artists: one LineCollection + two marker lines per panel -----
strips, starts, heads = [], [], []
for p, ax_obj in enumerate(ax_list):
if show_flow:
lc = LineCollection([], linewidths=1.5, alpha=0.65, zorder=2)
ax_obj.add_collection(lc)
strips.append(lc)
if show_endpoints:
st, = ax_obj.plot([], [], 'o', color='limegreen', ms=6, zorder=5, markeredgewidth=0,
linestyle='none', label='initial' if p == 0 else '')
hd, = ax_obj.plot([], [], 's', color='crimson', ms=4.5, zorder=5, markeredgewidth=0,
linestyle='none', label='final' if p == 0 else '')
starts.append(st)
heads.append(hd)
if show_endpoints:
ax_list[0].legend(fontsize=9, loc='best')
def update(i):
d, v = frames[i]
n_i = d['x'].shape[0]
artists = []
for p, (ku, kv, _) in enumerate(panel_specs):
U, W = d[ku], d[kv]
if show_flow:
strips[p].set_segments(np.stack((U, W), axis=-1))
strips[p].set_color(colors[:n_i])
strips[p].set_alpha(0.65)
artists.append(strips[p])
if show_endpoints:
rows = np.arange(U.shape[0])
last = _last_finite(U, W) # diverged rays end at their last finite point
if i0 is None:
starts[p].set_data(U[:, 0], W[:, 0])
heads[p].set_data(U[rows, last], W[rows, last])
else:
starts[p].set_data(U[:, i0], W[:, i0])
heads[p].set_data(np.r_[U[:, 0], U[rows, last]], np.r_[W[:, 0], W[rows, last]])
artists += [starts[p], heads[p]]
if not fixed_limits:
for p_idx in range(len(ax_list)):
_set_limits(p_idx, [frames[i]])
title_artist.set_text(f"{base_title} [{lbl[var]}₀ = {v:.2f}]")
return artists
update(0)
if created:
if len(ax_list) > 1:
fig.tight_layout(rect=(0, 0, 1, 0.96))
else:
fig.tight_layout()
anim = FuncAnimation(fig, update, frames=len(frames), interval=interval, blit=False)
if save_path:
anim.save(save_path)
if created:
plt.close(fig)
return anim
[docs]
def compute_maslov_index(
traj: Dict[str, np.ndarray]
) -> int:
"""
Compute the Maslov index for a single trajectory.
Parameters
----------
traj : dict
Returned by bicharacteristic_flow. Must contain J11..J22 components.
Returns
-------
int
Maslov index (number of sign changes of det(J) or caustic crossings).
"""
dim = 2 if 'y' in traj else 1
detector = RayCausticDetector([traj], dimension=dim, det_threshold=0.05)
detector.detect()
return detector.maslov_index(0)
[docs]
def compute_caustics_2d(
p: Expr,
initial_curve: Dict[str, Sequence[float]],
tmax: float,
n_rays: Optional[int] = None,
**kwargs: Any
) -> List[Any]:
"""
Compute caustics for a 2D Hamiltonian given an initial curve.
Parameters
----------
p : sympy expression
Hamiltonian symbol p(x, y, xi, eta).
initial_curve : dict
Must contain keys 'x', 'y', 'xi', 'eta' with array values of equal length.
tmax : float
Maximum integration time.
n_rays : int, optional
Number of rays to use. If None, use all points in the initial curve.
**kwargs : additional arguments passed to bicharacteristic_flow
(e.g., method='symplectic', n_steps=200).
Returns
-------
list of CausticEvent
Each event contains position, time, momentum, Arnold type, etc.
"""
x_init = np.asarray(initial_curve['x'])
y_init = np.asarray(initial_curve['y'])
xi_init = np.asarray(initial_curve['xi'])
eta_init = np.asarray(initial_curve['eta'])
n_pts = len(x_init)
if n_rays is None:
n_rays = n_pts
if n_rays < n_pts:
indices = np.linspace(0, n_pts - 1, n_rays, dtype=int)
x_init = x_init[indices]
y_init = y_init[indices]
xi_init = xi_init[indices]
eta_init = eta_init[indices]
elif n_rays > n_pts:
pass
rays = []
default_kwargs = {'method': 'symplectic', 'n_steps': 200}
flow_kwargs = {**default_kwargs, **kwargs}
for i in range(len(x_init)):
z0 = (x_init[i], y_init[i], xi_init[i], eta_init[i])
traj = bicharacteristic_flow(p, z0, (0, tmax), dim=2, **flow_kwargs)
rays.append(traj)
from caustics import RayCausticDetector
detector = RayCausticDetector(rays, dimension=2, det_threshold=0.05)
events = detector.detect()
return events
# ======================================================================
# Plotting system moved here from psiop_ud.py to keep that module
# focused on operator algebra / numerics. See psiop_ud.py for the
# thin wrapper methods on PseudoDifferentialOperator that delegate here.
# ======================================================================
# ---- low-level rendering helpers (shared by the op-visualization and
# characteristic-set/wavefront plots below) --------------------
def _slice_grid(
op: Any,
kind: Literal['freq', 'space'],
x_grid: Optional[np.ndarray] = None,
xi_grid: Optional[np.ndarray] = None,
y_grid: Optional[np.ndarray] = None,
eta_grid: Optional[np.ndarray] = None,
x0: float = 0.0,
y0: float = 0.0,
xi0: float = 0.0,
eta0: float = 0.0
) -> Tuple[np.ndarray, np.ndarray, str, str, np.ndarray]:
"""Evaluate op.p_func over one canonical 2D slice of phase space.
kind='freq' : 1D -> vary (x, xi). 2D -> fix (x0, y0), vary (xi, eta).
(used by: fiber, characteristic set/gradient, micro-support)
kind='space' : 1D -> vary (x, xi) (1D has only one slice, same as above).
2D -> fix (xi0, eta0), vary (x, y).
(used by: symbol amplitude, phase)
Returns (axis1_vals, axis2_vals, axis1_label, axis2_label, Z).
"""
if op.dim == 1:
A, B = np.meshgrid(x_grid, xi_grid, indexing='ij')
return (x_grid, xi_grid, 'x', '$\\xi$', op.p_func(A, B))
if kind == 'space':
A, B = np.meshgrid(x_grid, y_grid, indexing='ij')
Z = op.p_func(A, B, np.full_like(A, xi0), np.full_like(B, eta0))
return (x_grid, y_grid, 'x', 'y', Z)
A, B = np.meshgrid(xi_grid, eta_grid, indexing='ij')
Z = op.p_func(x0, y0, A, B)
return (xi_grid, eta_grid, '$\\xi$', '$\\eta$', Z)
def _render_field(
ax1: np.ndarray,
ax2: np.ndarray,
Z: np.ndarray,
style: Literal['pcolormesh', 'contourf', 'contour'] = 'pcolormesh',
cmap: str = 'viridis',
cbar_label: Optional[str] = None,
xlabel: str = 'x',
ylabel: str = '$\\xi$',
title: str = '',
levels: Union[int, Sequence[float]] = 50,
contour_color: str = 'red',
grid: bool = False,
show: bool = True
) -> None:
"""
Render a 2D scalar field using pcolormesh, contourf, or contour.
Parameters
----------
ax1, ax2 : numpy.ndarray
2D coordinate arrays for the X and Y axes.
Z : numpy.ndarray
2D array of scalar values to plot.
style : str, optional
Plotting style: 'pcolormesh', 'contourf', or 'contour'. Default is 'pcolormesh'.
cmap : str, optional
Matplotlib colormap name. Default is 'viridis'.
cbar_label : str, optional
Label for the colorbar.
xlabel, ylabel : str, optional
Axis labels.
title : str, optional
Plot title.
levels : int or list, optional
Number of contour levels or specific levels. Default is 50.
contour_color : str, optional
Color for contour lines if `style='contour'`. Default is 'red'.
grid : bool, optional
Whether to display a grid. Default is False.
show : bool, optional
Whether to call `plt.show()`. Default is True.
Returns
-------
None
"""
if style == 'pcolormesh':
im_ = plt.pcolormesh(ax1, ax2, Z, shading='auto', cmap=cmap)
plt.colorbar(im_, label=cbar_label)
elif style == 'contourf':
im_ = plt.contourf(ax1, ax2, Z, levels=levels, cmap=cmap)
plt.colorbar(im_, label=cbar_label)
elif style == 'contour':
plt.contour(ax1, ax2, Z, levels=levels, colors=contour_color)
else:
raise ValueError(f'unknown style {style!r}')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
if grid:
plt.grid(True)
if show:
plt.show()
def _grad_norm(
Z: np.ndarray
) -> np.ndarray:
"""|grad Z| over a 2D array, using a consistent abs()**2 convention on
both axes (see visualize_characteristic_gradient's note: this unifies
an inconsistency that used to exist between its 1D and 2D branches)."""
gx = np.gradient(Z, axis=0)
gy = np.gradient(Z, axis=1)
return np.sqrt(np.abs(gx) ** 2 + np.abs(gy) ** 2)
def _quiver_colored(
ax: Any,
X: np.ndarray,
Y: np.ndarray,
U: np.ndarray,
V: np.ndarray,
cmap: str = 'viridis',
scale: float = 25,
width: float = 0.004,
colorbar: bool = True,
cbar_label: str = '|field|',
**quiver_kwargs: Any
) -> Any:
"""Draw a quiver field with fixed-length arrows, colored by magnitude.
Plain `plt.quiver(X, Y, U, V)` sizes each arrow by its own (U, V)
magnitude, which is what makes raw vector-field plots look like a
tangle of dark arrows of wildly different lengths whenever the field
spans more than about one order of magnitude (e.g. near a symbol's
zero set). Here every arrow is normalized to the same unit length
(direction only), and the original magnitude is instead mapped to
color via matplotlib's `quiver(X, Y, U, V, C)` form -- the same trick
used for wind/flow-field plots.
Parameters
----------
ax : matplotlib Axes or the `matplotlib.pyplot` module
Target to draw on; anything exposing `.quiver(...)` (an Axes) or
module-level `quiver(...)` (`plt` itself) works.
X, Y : ndarray
Arrow base positions (same shape as U, V).
U, V : ndarray
Raw (unnormalized) vector field components.
cmap : str, default='viridis'
Colormap used for the magnitude.
scale : float, default=25
Passed to `quiver`; larger values shrink the (now uniform) arrow
length. Tune this once the field is normalized -- it no longer
needs to be re-tuned per symbol the way a magnitude-scaled plot
would.
width : float, default=0.004
Arrow shaft width, passed to `quiver`.
colorbar : bool, default=True
If True, attach a colorbar labelled `cbar_label` showing the
magnitude scale. Skipped automatically if `ax` has no attached
figure to draw it on (e.g. `ax=plt`, when called more than once
on the same axes -- pass `colorbar=False` for background/overlay
fields to avoid stacking colorbars).
**quiver_kwargs
Extra keyword arguments forwarded to `quiver` (e.g. `alpha`).
`color` is ignored if passed here, since color is used to encode
magnitude; use `cmap` instead.
Returns
-------
matplotlib.quiver.Quiver
The artist returned by the underlying `quiver` call, so callers
can attach their own colorbar/legend if `colorbar=False`.
"""
quiver_kwargs.pop('color', None)
mag = np.hypot(np.abs(U), np.abs(V))
safe_mag = np.where(mag == 0, 1.0, mag)
Un, Vn = (np.real(U) / safe_mag, np.real(V) / safe_mag)
q = ax.quiver(X, Y, Un, Vn, mag, cmap=cmap, scale=scale, width=width, **quiver_kwargs)
if colorbar:
fig = ax.figure if hasattr(ax, 'figure') else plt.gcf()
fig.colorbar(q, ax=ax if hasattr(ax, 'figure') else plt.gca(), label=cbar_label)
return q
def _quiver_field(
op: Any,
xlim: Tuple[float, float],
klim: Tuple[float, float],
density: int,
vec_exprs_fn: Callable[[Expr, Symbol, Symbol], Tuple[Expr, Expr]],
title: str,
scale: float = 25,
width: float = 0.004,
cmap: str = 'viridis',
cbar_label: str = '|field|'
) -> None:
"""1D-only quiver plot of a vector field derived from the symbol.
Arrows are drawn at fixed length; the field's local magnitude is
encoded by color instead (see `_quiver_colored`).
vec_exprs_fn(p, x, xi) -> (U_expr, V_expr)
"""
if op.dim != 1:
raise NotImplementedError('Only 1D version implemented.')
x, = op.vars_x
xi = symbols('xi', real=True)
x_vals = np.linspace(*xlim, density)
xi_vals = np.linspace(*klim, density)
X, XI = np.meshgrid(x_vals, xi_vals, indexing='ij')
U_expr, V_expr = vec_exprs_fn(op.symbol, x, xi)
U = lambdify((x, xi), simplify(U_expr), 'numpy')(X, XI)
V = lambdify((x, xi), simplify(V_expr), 'numpy')(X, XI)
if np.isscalar(U):
U = np.full_like(X, U, dtype=float)
if np.isscalar(V):
V = np.full_like(X, V, dtype=float)
_quiver_colored(plt.gca(), X, XI, U, V, cmap=cmap, scale=scale, width=width, cbar_label=cbar_label)
plt.xlabel('x')
plt.ylabel('$\\xi$')
plt.title(title)
plt.grid(True)
plt.show()
def _make_real(
expr: Expr
) -> Expr:
"""Re(expr), fully evaluated -- used when a Hamiltonian field may come
out complex-valued from sympy but only the real part is physically
meaningful for the flow."""
return simplify(re(expr.doit(deep=True)))
# ---- PseudoDifferentialOperator visualizations, as free functions
# taking the operator `op` as the first argument (duck-typed on
# op.p_func / op.dim / op.vars_x / op.symbol / op.symplectic_flow /
# op.expr) -------------------------------------------------------
[docs]
def visualize_fiber(
op: Any,
x_grid: np.ndarray,
xi_grid: np.ndarray,
x0: float = 0.0,
y0: float = 0.0
) -> None:
"""
Plot the cotangent fiber structure at a fixed spatial point (x₀[, y₀]).
This visualization shows how the symbol p(x, ξ) behaves on the cotangent fiber
above a fixed spatial point. In microlocal analysis, this provides insight into
the frequency content of the operator at that location.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
x_grid : ndarray
Spatial grid values (1D) for evaluation in 1D case.
xi_grid : ndarray
Frequency grid values (1D) for evaluation in both 1D and 2D cases.
x0 : float, optional
Fixed x-coordinate of the base point in space (1D or 2D).
y0 : float, optional
Fixed y-coordinate of the base point in space (2D only).
Notes
-----
- In 1D: Displays |p(x, ξ)| over the (x, ξ) phase plane near the fixed point.
- In 2D: Fixes (x₀, y₀) and evaluates p(x₀, y₀, ξ, η), showing the fiber over that point.
- The color map represents the magnitude of the symbol, highlighting regions where it vanishes or becomes singular.
Raises
------
NotImplementedError
If called in 2D with missing or improperly formatted grids.
"""
a1, a2, l1, l2, Z = _slice_grid(op, 'freq', x_grid, xi_grid, eta_grid=xi_grid, x0=x0, y0=y0)
title = 'Cotangent Fiber Structure' if op.dim == 1 else f'Cotangent Fiber at x={x0}, y={y0}'
_render_field(a1, a2, np.abs(Z), style='contourf', cbar_label='|Symbol|', xlabel=l1, ylabel=l2, title=title)
[docs]
def visualize_symbol_amplitude(
op: Any,
x_grid: np.ndarray,
xi_grid: np.ndarray,
y_grid: Optional[np.ndarray] = None,
eta_grid: Optional[np.ndarray] = None,
xi0: float = 0.0,
eta0: float = 0.0
) -> None:
"""
Display the modulus |p(x, ξ)| or |p(x, y, ξ₀, η₀)| as a color map.
This method visualizes the amplitude of the pseudodifferential operator's symbol
in either 1D or 2D spatial configuration. In 2D, the frequency variables are fixed
to specified values (ξ₀, η₀) for visualization purposes.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
x_grid, y_grid : ndarray
Spatial grids over which to evaluate the symbol. y_grid is optional and used only in 2D.
xi_grid, eta_grid : ndarray
Frequency grids. In 2D, these define the domain over which the symbol is evaluated,
but the visualization fixes ξ = ξ₀ and η = η₀.
xi0, eta0 : float, optional
Fixed frequency values for slicing in 2D visualization. Defaults to zero.
Notes
-----
- In 1D: Visualizes |p(x, ξ)| over the (x, ξ) grid.
- In 2D: Visualizes |p(x, y, ξ₀, η₀)| at fixed frequencies ξ₀ and η₀.
- The color intensity represents the magnitude of the symbol, highlighting regions where the symbol is large or small.
"""
kind = 'freq' if op.dim == 1 else 'space'
a1, a2, l1, l2, Z = _slice_grid(op, kind, x_grid, xi_grid, y_grid, eta_grid, xi0=xi0, eta0=eta0)
title = 'Symbol Amplitude |p(x, ξ)|' if op.dim == 1 else f'Symbol Amplitude at ξ={xi0}, η={eta0}'
_render_field(a1, a2, np.abs(Z), style='pcolormesh', cbar_label='|Symbol|', xlabel=l1, ylabel=l2, title=title)
[docs]
def visualize_phase(
op: Any,
x_grid: np.ndarray,
xi_grid: np.ndarray,
y_grid: Optional[np.ndarray] = None,
eta_grid: Optional[np.ndarray] = None,
xi0: float = 0.0,
eta0: float = 0.0
) -> None:
"""
Plot the phase (argument) of the pseudodifferential operator's symbol p(x, ξ) or p(x, y, ξ, η).
This visualization helps in understanding the oscillatory behavior and regularity
properties of the operator in phase space. The phase is displayed modulo 2π using
a cyclic colormap ('twilight') to emphasize its periodic nature.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
x_grid : ndarray
1D array of spatial coordinates (x).
xi_grid : ndarray
1D array of frequency coordinates (ξ).
y_grid : ndarray, optional
2D spatial grid for y-coordinate (in 2D problems). Default is None.
eta_grid : ndarray, optional
2D frequency grid for η (in 2D problems). Not used directly but kept for API consistency.
xi0 : float, optional
Fixed value of ξ for slicing in 2D visualization. Default is 0.0.
eta0 : float, optional
Fixed value of η for slicing in 2D visualization. Default is 0.0.
Notes:
- In 1D: Displays arg(p(x, ξ)) over the (x, ξ) phase plane.
- In 2D: Displays arg(p(x, y, ξ₀, η₀)) for fixed frequency values (ξ₀, η₀).
- Uses plt.pcolormesh with 'twilight' colormap to represent angles from -π to π.
Raises:
- NotImplementedError: If the spatial dimension is not 1D or 2D.
"""
kind = 'freq' if op.dim == 1 else 'space'
a1, a2, l1, l2, Z = _slice_grid(op, kind, x_grid, xi_grid, y_grid, eta_grid, xi0=xi0, eta0=eta0)
title = 'Phase Portrait (arg p(x, ξ))' if op.dim == 1 else f'Phase Portrait at ξ={xi0}, η={eta0}'
_render_field(a1, a2, np.angle(Z), style='pcolormesh', cmap='twilight', cbar_label='arg(Symbol) [rad]', xlabel=l1, ylabel=l2, title=title)
[docs]
def visualize_characteristic_set(
op: Any,
x_grid: np.ndarray,
xi_grid: np.ndarray,
y_grid: Optional[np.ndarray] = None,
eta_grid: Optional[np.ndarray] = None,
y0: float = 0.0,
x0: float = 0.0,
levels: Sequence[float] = [0.1]
) -> None:
"""
Visualize the characteristic set of the pseudo-differential symbol, defined as the approximate zero set p(x, ξ) ≈ 0.
In microlocal analysis, the characteristic set is the locus of points in phase space (x, ξ) where the symbol p(x, ξ) vanishes,
playing a key role in understanding propagation of singularities.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
x_grid : ndarray
Spatial grid values (1D array) for plotting in 1D or evaluation point in 2D.
xi_grid : ndarray
Frequency variable grid values (1D array) used to construct the frequency domain.
x0 : float, optional
Fixed spatial coordinate in 2D case for evaluating the symbol at a specific x position.
y0 : float, optional
Fixed spatial coordinate in 2D case for evaluating the symbol at a specific y position.
Notes
-----
- For 1D, this method plots the contour of |p(x, ξ)| = ε with ε = 1e-5 over the (x, ξ) plane.
- For 2D, it evaluates the symbol at fixed (x₀, y₀) and plots the characteristic set in the (ξ, η) frequency plane.
- This visualization helps identify directions of degeneracy or hypoellipticity of the operator.
Raises
------
NotImplementedError
If called on a solver with dimensionality other than 1D or 2D.
Displays
------
A matplotlib contour plot showing either:
- The characteristic curve in the (x, ξ) phase plane (1D),
- The characteristic surface slice in the (ξ, η) frequency plane at (x₀, y₀) (2D).
"""
if op.dim not in (1, 2):
raise NotImplementedError('Only 1D/2D characteristic sets supported.')
if op.dim == 2 and eta_grid is None:
raise ValueError('eta_grid must be provided for 2D visualization.')
a1, a2, l1, l2, Z = _slice_grid(op, 'freq', x_grid, xi_grid, y_grid, eta_grid, x0=x0, y0=y0)
title = 'Characteristic Set (p(x, ξ) ≈ 0)' if op.dim == 1 else f'Characteristic Set at x={x0}, y={y0}'
_render_field(a1, a2, np.abs(Z), style='contour', levels=levels, xlabel=l1, ylabel=l2, title=title, grid=True)
[docs]
def visualize_characteristic_gradient(
op: Any,
x_grid: np.ndarray,
xi_grid: np.ndarray,
y_grid: Optional[np.ndarray] = None,
eta_grid: Optional[np.ndarray] = None,
y0: float = 0.0,
x0: float = 0.0
) -> None:
"""
Visualize the norm of the gradient of the symbol in phase space.
This method computes the magnitude of the gradient |∇p| of a pseudo-differential
symbol p(x, ξ) in 1D or p(x, y, ξ, η) in 2D. The resulting colormap reveals
regions where the symbol varies rapidly or remains nearly stationary,
which is particularly useful for analyzing characteristic sets.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
x_grid : numpy.ndarray
1D array of spatial coordinates for the x-direction.
xi_grid : numpy.ndarray
1D array of frequency coordinates (ξ).
y_grid : numpy.ndarray, optional
1D array of spatial coordinates for the y-direction (used in 2D mode). Default is None.
eta_grid : numpy.ndarray, optional
1D array of frequency coordinates (η) for the 2D case. Default is None.
x0 : float, optional
Fixed x-coordinate for evaluating the symbol in 2D. Default is 0.0.
y0 : float, optional
Fixed y-coordinate for evaluating the symbol in 2D. Default is 0.0.
Returns
-------
None
Displays a 2D colormap of |∇p| over the relevant phase-space domain.
Notes
-----
- In 1D, the full gradient ∇p = (∂ₓp, ∂ξp) is computed over the (x, ξ) grid.
- In 2D, the gradient ∇p = (∂ξp, ∂ηp) is computed at a fixed spatial point (x₀, y₀) over the (ξ, η) grid.
- Numerical differentiation is performed using `np.gradient`.
- High values of |∇p| indicate rapid variation of the symbol, while low values typically suggest characteristic regions.
"""
a1, a2, l1, l2, Z = _slice_grid(op, 'freq', x_grid, xi_grid, y_grid, eta_grid, x0=x0, y0=y0)
title = 'Gradient Norm (High Near Zeros)' if op.dim == 1 else f'Gradient Norm at x={x0}, y={y0}'
_render_field(a1, a2, _grad_norm(Z), style='pcolormesh', cmap='inferno', cbar_label='|∇p|', xlabel=l1, ylabel=l2, title=title, grid=True)
[docs]
def plot_hamiltonian_flow(
op: Any,
x0: float = 0.0,
xi0: float = 5.0,
y0: float = 0.0,
eta0: float = 0.0,
tmax: float = 1.0,
n_steps: int = 100,
show_field: bool = True
) -> None:
"""
Integrate and plot the Hamiltonian trajectories of the symbol in phase space.
This method numerically integrates the Hamiltonian vector field derived from
the operator's symbol to visualize how singularities propagate under the flow.
It supports both 1D and 2D problems.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
x0, xi0 : float
Initial position and frequency (momentum) in 1D.
y0, eta0 : float, optional
Initial position and frequency in 2D; defaults to zero.
tmax : float
Final integration time for the ODE solver.
n_steps : int
Number of time steps used in the integration.
Notes
-----
- The Hamiltonian vector field is obtained from the symplectic flow of the symbol.
- If the field is complex-valued, only its real part is used for integration.
- In 1D, the trajectory is plotted in (x, ξ) phase space.
- In 2D, the spatial trajectory (x(t), y(t)) is shown along with instantaneous
momentum vectors (ξ(t), η(t)) using a quiver plot.
Raises
------
NotImplementedError
If the spatial dimension is not 1D or 2D.
Displays
--------
matplotlib plot
Phase space trajectory(ies) showing the evolution of position and momentum
under the Hamiltonian dynamics.
"""
x0v = [x0] if op.dim == 1 else [x0, y0]
xi0v = [xi0] if op.dim == 1 else [xi0, eta0]
_, _, _, _, trajs = integrate_singularity(op.symbol, op.vars_x, x0=x0v, xi0=xi0v, tmax=tmax, n_frames=n_steps)
Y = trajs[0]
if op.dim == 1:
x_vals, xi_vals = Y
plt.plot(x_vals, xi_vals)
plt.xlabel('x')
plt.ylabel('ξ')
plt.title('Hamiltonian Flow in Phase Space (1D)')
plt.grid(True)
plt.show()
elif op.dim == 2:
x_vals, y_vals, xi_vals, eta_vals = Y
plt.plot(x_vals, y_vals, label='Position')
_quiver_colored(plt.gca(), x_vals, y_vals, xi_vals, eta_vals, cmap='autumn', scale=20, width=0.003, alpha=0.7, cbar_label='$|(\\xi,\\eta)|$')
if show_field:
x, y = op.vars_x
xi, eta = symbols('xi eta', real=True)
H = op.symplectic_flow()
dxdt = lambdify((x, y, xi, eta), _make_real(H['dx/dt']), 'numpy')
dydt = lambdify((x, y, xi, eta), _make_real(H['dy/dt']), 'numpy')
Xg, Yg = np.meshgrid(np.linspace(min(x_vals), max(x_vals), 20), np.linspace(min(y_vals), max(y_vals), 20))
XI, ETA = (xi0 * np.ones_like(Xg), eta0 * np.ones_like(Yg))
Ub, Vb = (dxdt(Xg, Yg, XI, ETA), dydt(Xg, Yg, XI, ETA))
mag_b = np.hypot(Ub, Vb)
safe_b = np.where(mag_b == 0, 1.0, mag_b)
plt.quiver(Xg, Yg, Ub / safe_b, Vb / safe_b, color='gray', alpha=0.25, scale=30, width=0.002)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Hamiltonian Flow in Phase Space (2D)')
plt.legend()
plt.grid(True)
plt.axis('equal')
plt.show()
[docs]
def plot_symplectic_vector_field(
op: Any,
xlim: Tuple[float, float] = (-2, 2),
klim: Tuple[float, float] = (-5, 5),
density: int = 30
) -> None:
"""
Visualize the symplectic vector field (Hamiltonian vector field) associated with the operator's symbol.
The plotted vector field corresponds to (∂_ξ p, -∂_x p), where p(x, ξ) is the principal symbol
of the pseudo-differential operator. This field governs the bicharacteristic flow in phase space.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
xlim : tuple of float
Range for spatial variable x, as (x_min, x_max).
klim : tuple of float
Range for frequency variable ξ, as (ξ_min, ξ_max).
density : int
Number of grid points per axis for the visualization grid.
Raises
------
NotImplementedError
If called on a 2D operator (currently only 1D implementation available).
Notes
-----
- Only supports one-dimensional operators.
- Uses symbolic differentiation to compute ∂_ξ p and ∂_x p.
- Numerical evaluation is done via lambdify with NumPy backend.
- Visualization uses matplotlib quiver plot to show vector directions.
"""
_quiver_field(op, xlim, klim, density, lambda p, x, xi: (diff(p, xi), -diff(p, x)), 'Symplectic Vector Field (1D)')
[docs]
def visualize_micro_support(
op: Any,
xlim: Tuple[float, float] = (-2, 2),
klim: Tuple[float, float] = (-10, 10),
threshold: float = 0.001,
density: int = 300,
xi0: float = 0.0,
eta0: float = 0.0
) -> None:
"""
Visualize the micro-support of the operator by plotting the inverse of the symbol magnitude 1 / |p(x, ξ)|.
The micro-support provides insight into the singularities of a pseudo-differential operator
in phase space (x, ξ). Regions where |p(x, ξ)| is small correspond to large values in 1/|p(x, ξ)|,
highlighting areas of significant operator influence or singularity.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
xlim : tuple
Spatial domain limits (x_min, x_max).
klim : tuple
Frequency domain limits (ξ_min, ξ_max).
threshold : float
Threshold below which |p(x, ξ)| is considered effectively zero; used for numerical stability.
density : int
Number of grid points along each axis for visualization resolution.
Raises
------
NotImplementedError
If called on a solver with dimension greater than 1 (only 1D visualization is supported).
Notes
-----
- This method evaluates the symbol p(x, ξ) over a grid and plots its reciprocal to emphasize
regions where the symbol is near zero.
- A small constant (1e-10) is added to the denominator to avoid division by zero.
- The resulting plot helps identify characteristic sets.
"""
x_grid = np.linspace(*xlim, density)
xi_grid = np.linspace(*klim, density)
if op.dim == 1:
a1, a2, l1, l2, Z = _slice_grid(op, 'freq', x_grid, xi_grid)
else:
a1, a2, l1, l2, Z = _slice_grid(op, 'space', x_grid, None, x_grid, None, xi0=xi0, eta0=eta0)
title = 'Micro-Support Estimate (1/|Symbol|)' if op.dim == 1 else f'Micro-Support Estimate at ξ={xi0}, η={eta0}'
_render_field(a1, a2, 1 / (np.abs(Z) + 1e-10), style='contourf', cmap='inferno', cbar_label='$1/|p(x,\\xi)|$', xlabel=l1, ylabel=l2, title=title)
[docs]
def group_velocity_field(
op: Any,
xlim: Tuple[float, float] = (-2, 2),
klim: Tuple[float, float] = (-10, 10),
density: int = 30
) -> None:
"""
Plot the group velocity field ∇_ξ p(x, ξ) for 1D pseudo-differential operators.
The group velocity represents the speed at which waves of different frequencies propagate
in a dispersive medium. It is defined as the gradient of the symbol p(x, ξ) with respect
to the frequency variable ξ.
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed.
Must expose attributes: `dim`, `p_func`, `vars_x`, `symbol`, and `expr`.
xlim : tuple of float
Spatial domain limits (x-axis).
klim : tuple of float
Frequency domain limits (ξ-axis).
density : int
Number of grid points per axis used for visualization.
Raises
------
NotImplementedError
If called on a 2D operator, since this visualization is only implemented for 1D.
Notes
-----
- This method visualizes the vector field (∂p/∂ξ) in phase space.
- Used for analyzing wave propagation properties and dispersion relations.
- Requires symbolic expression self.expr depending on x and ξ.
"""
_quiver_field(op, xlim, klim, density, lambda p, x, xi: (Integer(1), diff(p, xi)), 'Group Velocity Field (1D)')
def _default_wavefront_seeds(
op: Any,
x0: float = 0.0,
y0: float = 0.0,
xi0: float = 1.0,
eta0: float = 0.0,
spread: float = 2.0,
n_seeds: int = 25,
radius: float = 0.15
) -> List[Tuple[float, ...]]:
"""Auto-generate seed singularities for visualize_wavefront_set.
1D: a fan of frequencies xi in [xi0 - spread, xi0 + spread] at the fixed
point x0 (mirrors the "vertical slice" seeding used for Example 11 in
the microlocal notebook).
2D: a small circle of point-source singularities of radius `radius`
around (x0, y0), with outward covectors scaled to |(xi0, eta0)| (mirrors
the point-source seeding of Example 12).
"""
if op.dim == 1:
xi_vals = np.linspace(xi0 - spread, xi0 + spread, n_seeds)
nonzero = xi_vals[np.abs(xi_vals) > 1e-06]
if len(nonzero) > 0:
xi_vals = nonzero
return [(x0, float(xi)) for xi in xi_vals]
angles = np.linspace(0, 2 * np.pi, n_seeds, endpoint=False)
mag = float(np.hypot(xi0, eta0)) or 1.0
return [(x0 + radius * np.cos(a), y0 + radius * np.sin(a), mag * np.cos(a), mag * np.sin(a)) for a in angles]
[docs]
def visualize_wavefront_set(
op: Any,
seeds: Optional[List[Sequence[float]]] = None,
tspan: Tuple[float, float] = (0, 3.0),
projection: str = 'cotangent',
n_steps: int = 500,
cmap: str = 'plasma',
show_flow: bool = True,
show_endpoints: bool = True,
title: Optional[str] = None,
x0: float = 0.0,
y0: float = 0.0,
xi0: float = 1.0,
eta0: float = 0.0,
spread: float = 2.0,
n_seeds: int = 25,
radius: float = 0.15
) -> Tuple[Figure, Union[Axes, np.ndarray]]:
"""
Visualize the wavefront set WF(u) obtained by propagating seed singularities
along the bicharacteristics of the operator's symbol.
If `seeds` is not given, a default fan/point-source is built from
(x0, y0) and (xi0, eta0).
Parameters
----------
op : PseudoDifferentialOperator
The pseudo-differential operator.
seeds : list of tuple, optional
Seed points for the wavefront set. If None, auto-generated.
tspan : tuple of float, optional
Integration time interval (t_start, t_end). Default is (0, 3.0).
projection : str, optional
Subspace to visualize. 1D: 'cotangent', 'position'.
2D: 'cotangent', 'position', 'frequency', 'mixed_x', 'mixed_y', 'full'.
n_steps : int, optional
Number of integration steps per bicharacteristic. Default is 500.
cmap : str, optional
Matplotlib colormap. Default is 'plasma'.
show_flow : bool, optional
Whether to draw the full bicharacteristic strip. Default is True.
show_endpoints : bool, optional
Whether to mark initial and final points. Default is True.
title : str, optional
Figure title.
x0, y0 : float, optional
Base spatial coordinates for auto-generated seeds.
xi0, eta0 : float, optional
Base frequency coordinates for auto-generated seeds.
spread : float, optional
Frequency spread for 1D auto-generated seeds.
n_seeds : int, optional
Number of seed points to generate if `seeds` is None.
radius : float, optional
Radius for 2D point-source auto-generated seeds.
Returns
-------
fig : matplotlib.figure.Figure
The generated figure.
axes : matplotlib.axes.Axes or ndarray of Axes
The axes object(s).
"""
if seeds is None:
seeds = _default_wavefront_seeds(op, x0=x0, y0=y0, xi0=xi0, eta0=eta0, spread=spread, n_seeds=n_seeds, radius=radius)
return plot_wavefront_set(op.symbol, seeds, tspan, dim=op.dim, projection=projection, n_steps=n_steps, cmap=cmap, show_flow=show_flow, show_endpoints=show_endpoints, title=title)
[docs]
def animate_operator_singularity(
op: Any,
xi0: float = 5.0,
eta0: float = 0.0,
x0: float = 0.0,
y0: float = 0.0,
tmax: float = 4.0,
n_frames: int = 100,
projection: Optional[str] = None
) -> FuncAnimation:
"""Animate the propagation of a singularity under the Hamiltonian
flow. Thin delegate to the module-level `animate_singularity`
engine (previously ~130 lines of duplicated Hamiltonian/ODE setup
here, plus a near-identical copy further down the module)."""
rc('animation', html='jshtml')
x0v = x0 if op.dim == 1 else [x0, y0]
xi0v = xi0 if op.dim == 1 else [xi0, eta0]
if projection is None:
projection = 'phase' if op.dim == 1 else 'position'
return animate_singularity(op.symbol, op.vars_x, x0=x0v, xi0=xi0v, tmax=tmax, n_frames=n_frames, projection=projection)
[docs]
def interactive_symbol_analysis(
pseudo_op: Any,
xlim: Tuple[float, float] = (-2, 2),
ylim: Tuple[float, float] = (-2, 2),
xi_range: Tuple[float, float] = (0.1, 5),
eta_range: Tuple[float, float] = (-5, 5),
density: int = 50
) -> None:
"""
Launch an interactive dashboard for symbol exploration using ipywidgets.
The dashboard combines a drop-down menu (the visualization *mode*) with sliders that set the
base point of the exploration, (x₀[, y₀]) in position space and (ξ₀[, η₀]) in frequency space.
The selected visualization is redrawn each time a widget changes. It works for operators in
1D and 2D and covers pointwise maps of the symbol (amplitude, phase, micro-support),
characteristic-set diagnostics, vector fields (group velocity, Hamiltonian field) and the
propagation of singularities (Hamiltonian flow and wavefront set).
Parameters
----------
pseudo_op : PseudoDifferentialOperator
The pseudo-differential operator whose symbol is to be analyzed interactively. Only its
dimension (``pseudo_op.dim``, 1 or 2), its space variables and its symbol are used.
xlim, ylim : tuple of float, default=(-2, 2)
Spatial domain limits along x and y (``ylim`` is ignored in 1D). They define the
evaluation grids and the ranges of the x₀ and y₀ sliders.
xi_range : tuple of float, default=(0.1, 5)
eta_range : tuple of float, default=(-5, 5)
Frequency domain limits along ξ and η (``eta_range`` is ignored in 1D). They define the
evaluation grids and the ranges of the ξ₀ and η₀ sliders. In 1D, the width of
``xi_range`` is also used as the half-width of the fan of frequencies in the
'Wavefront Set' mode.
density : int, default=50
Number of points per axis of the evaluation grids (resolution of the maps and vector
fields). It is also the number of seeds (rays) of the 'Wavefront Set' mode. Large values
make the dashboard slower.
Returns
-------
None
The dashboard (mode selector, sliders and output area) is displayed in the notebook; the
figures are drawn in the output area and updated as the widgets change.
Notes
-----
- Requires a Jupyter environment with ``ipywidgets``. Each update closes all the open matplotlib
figures (``plt.close('all')``) before redrawing.
- Only 1D and 2D operators are supported.
- Sliders (step 0.1): ξ₀ ranges over ``xi_range``, η₀ over ``eta_range``, x₀ over ``xlim`` and
y₀ over ``ylim``. Only the sliders needed by the selected mode are displayed; a hidden slider
keeps its current value (initially ξ₀ = η₀ = 1 and x₀ = y₀ = 0).
Visualization modes in 1D (phase space (x, ξ)); sliders shown in brackets:
- 'Symbol Amplitude' : |p(x,ξ)| as a color map
- 'Symbol Phase' : arg p(x,ξ) (mod 2π, cyclic color map)
- 'Micro-Support (1/|p|)' : 1/|p(x,ξ)|, large where the symbol is small
- 'Cotangent Fiber' : p on the cotangent fiber above x₀
- 'Characteristic Set' : approximate zero set {p ≈ 0}
- 'Characteristic Gradient' : |∇p(x,ξ)|
- 'Group Velocity Field' : ∂_ξ p(x,ξ) (1D only)
- 'Symplectic Vector Field' : Hamiltonian vector field (∂_ξ p, -∂_x p)
- 'Hamiltonian Flow' : trajectory from (x₀, ξ₀) with the vector field [ξ₀, x₀]
- 'Wavefront Set' : propagation of a fan of seed frequencies at x₀ [ξ₀, x₀]
Visualization modes in 2D (phase space (x, y, ξ, η)):
- 'Symbol Amplitude' : |p| over (x, y) at fixed frequency (ξ₀, η₀) [ξ₀, η₀]
- 'Symbol Phase' : arg p over (x, y) at fixed frequency (ξ₀, η₀) [ξ₀, η₀]
- 'Micro-Support (1/|p|)' : 1/|p| at fixed frequency (ξ₀, η₀) [ξ₀, η₀]
- 'Cotangent Fiber' : p over the frequency plane (ξ, η) above (x₀, y₀) [x₀, y₀]
- 'Characteristic Set' : approximate zero set {p ≈ 0} above (x₀, y₀) [x₀, y₀]
- 'Characteristic Gradient' : |∇p| above (x₀, y₀) [x₀, y₀]
- 'Symplectic Vector Field' : arrows (∂_ξ p, ∂_η p) over (x, y) at fixed frequency
(ξ₀, η₀), colored by their norm [ξ₀, η₀]. This is the group
velocity field, which is why there is no separate
'Group Velocity Field' mode in 2D.
- 'Hamiltonian Flow' : trajectory from (x₀, y₀, ξ₀, η₀) [all four sliders]
- 'Wavefront Set' : propagation of a point source at (x₀, y₀) [all four sliders]
'Wavefront Set' mode. The seeds are generated by `visualize_wavefront_set` and propagated
along the bicharacteristics over t ∈ [0, 5] with 150 steps:
- 1D: ``density`` seeds at the point x₀ with frequencies in [ξ₀ - w, ξ₀ + w], where w is the
width of ``xi_range``; the figure shows the cotangent projection.
- 2D: ``density`` seeds on a circle of radius 0.15 around (x₀, y₀), with outward covectors of
norm |(ξ₀, η₀)| (a point source); the figure shows the four 2D slices of phase space
(position, frequency and the two cotangent slices).
For other settings (seed families, flow time, number of steps) or for animations, call
`visualize_wavefront_set`, `plot_wavefront_set` or `animate_wavefront_set` directly.
See Also
--------
visualize_wavefront_set : Static wavefront-set plot with default seeds.
plot_wavefront_set : Wavefront-set plot for user-supplied seeds.
animate_wavefront_set : Animation of the wavefront set while an initial coordinate sweeps a range.
plot_hamiltonian_flow : Hamiltonian trajectories in phase space.
Examples
--------
>>> x, y, xi, eta = sp.symbols('x y xi eta', real=True)
>>> symbol = (xi**2 + eta**2) / 2 - 1 / sp.sqrt(x**2 + y**2 + 0.01)
>>> op = PseudoDifferentialOperator(expr=symbol, vars_x=[x, y], mode='symbol')
>>> interactive_symbol_analysis(op, xlim=(-3, 3), ylim=(-3, 3),
... xi_range=(-3, 3), eta_range=(-3, 3), density=40)
"""
dim = pseudo_op.dim
x_vals = np.linspace(*xlim, density)
y_vals = np.linspace(*ylim, density) if dim == 2 else None
xi_lin = np.linspace(*xi_range, density)
eta_lin = np.linspace(*eta_range, density) if dim == 2 else None
if dim == 1:
modes = ['Symbol Amplitude', 'Symbol Phase', 'Micro-Support (1/|p|)',
'Cotangent Fiber', 'Characteristic Set', 'Characteristic Gradient',
'Group Velocity Field', 'Symplectic Vector Field', 'Hamiltonian Flow', 'Wavefront Set']
needs = {
'Symbol Amplitude': (), 'Symbol Phase': (), 'Micro-Support (1/|p|)': (),
'Group Velocity Field': (), 'Symplectic Vector Field': (),
'Hamiltonian Flow': ('xi', 'x'), 'Cotangent Fiber': (), 'Characteristic Set': (),
'Characteristic Gradient': (), 'Wavefront Set': ('xi', 'x')
}
mode_selector = Dropdown(options=modes, value='Symbol Amplitude', description='Mode:')
xi_slider = FloatSlider(min=xi_range[0], max=xi_range[1], step=0.1, value=1.0, description='ξ₀')
x_slider = FloatSlider(min=xlim[0], max=xlim[1], step=0.1, value=0.0, description='x₀')
all_sliders = {'xi': xi_slider, 'x': x_slider}
def render(mode, xi0, x0):
plt.close('all')
if mode != 'Wavefront Set':
plt.figure()
if mode == 'Symbol Amplitude':
visualize_symbol_amplitude(pseudo_op, x_vals, xi_lin, xi0=xi0)
elif mode == 'Symbol Phase':
visualize_phase(pseudo_op, x_vals, xi_lin, xi0=xi0)
elif mode == 'Micro-Support (1/|p|)':
visualize_micro_support(pseudo_op, xlim, xi_range, density=density)
elif mode == 'Group Velocity Field':
group_velocity_field(pseudo_op, xlim, xi_range, density=density)
elif mode == 'Symplectic Vector Field':
plot_symplectic_vector_field(pseudo_op, xlim, xi_range, density=density)
elif mode == 'Cotangent Fiber':
visualize_fiber(pseudo_op, x_vals, xi_lin, x0=x0)
elif mode == 'Characteristic Set':
visualize_characteristic_set(pseudo_op, x_vals, xi_lin, x0=x0)
elif mode == 'Characteristic Gradient':
visualize_characteristic_gradient(pseudo_op, x_vals, xi_lin, x0=x0)
elif mode == 'Hamiltonian Flow':
plot_hamiltonian_flow(pseudo_op, x0=x0, xi0=xi0)
elif mode == 'Wavefront Set':
visualize_wavefront_set(pseudo_op, x0=x0, xi0=xi0, spread=xi_range[1] - xi_range[0], tspan=(0, 5.0), n_seeds=density, n_steps=150)
plt.show()
interactive_kwargs = {'mode': mode_selector, 'xi0': xi_slider, 'x0': x_slider}
slider_order = ['xi', 'x']
else:
modes = ['Symbol Amplitude', 'Symbol Phase', 'Micro-Support (1/|p|)',
'Cotangent Fiber', 'Characteristic Set', 'Characteristic Gradient',
'Symplectic Vector Field', 'Hamiltonian Flow', 'Wavefront Set']
needs = {
'Symbol Amplitude': ('xi', 'eta'), 'Symbol Phase': ('xi', 'eta'),
'Micro-Support (1/|p|)': ('xi', 'eta'), 'Symplectic Vector Field': ('xi', 'eta'),
'Hamiltonian Flow': ('xi', 'eta', 'x', 'y'),
'Cotangent Fiber': ('x', 'y'), 'Characteristic Set': ('x', 'y'),
'Characteristic Gradient': ('x', 'y'), 'Wavefront Set': ('xi', 'eta', 'x', 'y')
}
mode_selector = Dropdown(options=modes, value='Symbol Amplitude', description='Mode:')
xi_slider = FloatSlider(min=xi_range[0], max=xi_range[1], step=0.1, value=1.0, description='ξ₀')
eta_slider = FloatSlider(min=eta_range[0], max=eta_range[1], step=0.1, value=1.0, description='η₀')
x_slider = FloatSlider(min=xlim[0], max=xlim[1], step=0.1, value=0.0, description='x₀')
y_slider = FloatSlider(min=ylim[0], max=ylim[1], step=0.1, value=0.0, description='y₀')
all_sliders = {'xi': xi_slider, 'eta': eta_slider, 'x': x_slider, 'y': y_slider}
def render(mode, xi0, eta0, x0, y0):
plt.close('all')
if mode != 'Wavefront Set':
plt.figure()
if mode == 'Symbol Amplitude':
visualize_symbol_amplitude(pseudo_op, x_vals, xi_lin, y_vals, eta_lin, xi0=xi0, eta0=eta0)
elif mode == 'Symbol Phase':
visualize_phase(pseudo_op, x_vals, xi_lin, y_vals, eta_lin, xi0=xi0, eta0=eta0)
elif mode == 'Micro-Support (1/|p|)':
visualize_micro_support(pseudo_op, xlim, xi_range, density=density, xi0=xi0, eta0=eta0)
elif mode == 'Symplectic Vector Field':
x, y = pseudo_op.vars_x
xi, eta = symbols('xi eta', real=True)
Xg, Yg = np.meshgrid(x_vals, y_vals, indexing='ij')
U, V = lambdify((x, y, xi, eta), [diff(pseudo_op.expr, xi), diff(pseudo_op.expr, eta)], 'numpy')(Xg, Yg, xi0, eta0)
_quiver_colored(plt.gca(), Xg, Yg, U, V, scale=10, width=0.004, cbar_label='|(dp/dξ, dp/dη)|')
plt.xlabel('x')
plt.ylabel('y')
plt.title(f'Symplectic Field at ξ={xi0:.2f}, η={eta0:.2f}')
elif mode == 'Cotangent Fiber':
visualize_fiber(pseudo_op, xi_lin, eta_lin, x0=x0, y0=y0)
elif mode == 'Characteristic Set':
visualize_characteristic_set(pseudo_op, x_vals, xi_lin, y_vals, eta_lin, x0=x0, y0=y0)
elif mode == 'Characteristic Gradient':
visualize_characteristic_gradient(pseudo_op, x_vals, xi_lin, y_vals, eta_lin, x0=x0, y0=y0)
elif mode == 'Hamiltonian Flow':
plot_hamiltonian_flow(pseudo_op, x0=x0, y0=y0, xi0=xi0, eta0=eta0)
elif mode == 'Wavefront Set':
visualize_wavefront_set(pseudo_op, x0=x0, y0=y0, xi0=xi0, eta0=eta0, projection='full', tspan=(0, 5.0), n_seeds=density, n_steps=150)
if mode not in ('Cotangent Fiber', 'Characteristic Set', 'Characteristic Gradient', 'Hamiltonian Flow'):
plt.show()
interactive_kwargs = {'mode': mode_selector, 'xi0': xi_slider, 'eta0': eta_slider, 'x0': x_slider, 'y0': y_slider}
slider_order = ['xi', 'eta', 'x', 'y']
controls_box = VBox([mode_selector] + list(all_sliders.values()))
def update_controls(change):
active = needs[change['new']]
controls_box.children = [mode_selector] + [all_sliders[k] for k in slider_order if k in active]
mode_selector.observe(update_controls, names='value')
update_controls({'new': mode_selector.value})
out = interactive_output(render, interactive_kwargs)
display(VBox([controls_box, out]))
[docs]
def plot_pseudospectrum(
Lambda: np.ndarray,
resolvent_norm: np.ndarray,
sigma_min_grid: np.ndarray,
epsilon_levels: Sequence[float],
eigenvalues: Optional[np.ndarray]
) -> None:
"""
Plot pseudospectrum results.
Parameters
----------
Lambda : ndarray
Complex λ grid
resolvent_norm : ndarray
Resolvent norms
sigma_min_grid : ndarray
Smallest singular values
epsilon_levels : list
Contour levels
eigenvalues : ndarray or None
Eigenvalues to overlay
"""
Lambda_re = Lambda.real
Lambda_im = Lambda.imag
plt.figure(figsize=(14, 6))
plt.subplot(1, 2, 1)
log_resolvent = np.log10(resolvent_norm + 1e-16)
levels_log = np.log10(1.0 / np.array(epsilon_levels))
valid_levels = [lv for lv in levels_log if log_resolvent.min() <= lv <= log_resolvent.max()]
if len(valid_levels) > 0:
cs = plt.contour(Lambda_re, Lambda_im, log_resolvent, levels=valid_levels, colors='blue', linewidths=1.5)
labels = [f'ε={eps:.0e}' for eps in epsilon_levels[:len(valid_levels)]]
fmt = dict(zip(cs.levels, labels))
plt.clabel(cs, inline=True, fmt=fmt, fontsize=9)
else:
print('⚠️ Warning: No contours in specified epsilon range')
cs = plt.contour(Lambda_re, Lambda_im, log_resolvent, levels=10, colors='blue', linewidths=1.5)
if eigenvalues is not None:
plt.plot(eigenvalues.real, eigenvalues.imag, 'r*', markersize=10, label='Eigenvalues', markeredgecolor='darkred')
plt.xlabel('Re(λ)', fontsize=12)
plt.ylabel('Im(λ)', fontsize=12)
plt.title('ε-Pseudospectrum: log₁₀(‖(H - λI)⁻¹‖)', fontsize=13)
plt.grid(alpha=0.3)
plt.legend(fontsize=10)
plt.axis('equal')
plt.subplot(1, 2, 2)
sigma_plot = np.where(np.isfinite(sigma_min_grid), sigma_min_grid, np.nan)
vmin = np.nanmin(sigma_plot[sigma_plot > 0]) if np.any(sigma_plot > 0) else 1e-10
vmax = np.nanmax(sigma_plot)
cs2 = plt.contourf(Lambda_re, Lambda_im, sigma_plot, levels=50, cmap='viridis', norm=LogNorm(vmin=vmin, vmax=vmax))
plt.colorbar(cs2, label='σ_min(H - λI)')
if eigenvalues is not None:
plt.plot(eigenvalues.real, eigenvalues.imag, 'r*', markersize=10, markeredgecolor='darkred')
for eps in epsilon_levels:
cs_eps = plt.contour(Lambda_re, Lambda_im, sigma_plot, levels=[eps], colors='red', linewidths=2, alpha=0.8)
plt.xlabel('Re(λ)', fontsize=12)
plt.ylabel('Im(λ)', fontsize=12)
plt.title('Smallest singular value σ_min(H - λI)', fontsize=13)
plt.grid(alpha=0.3)
plt.axis('equal')
plt.tight_layout()
plt.show()
# ---- solver-output plotting/animation (scalar & matrix fields from
# the time-stepping solvers, and bicharacteristic/singularity
# trajectory animations) ---------------------------------------
def _matrix_of(
s_expr: Union[Expr, MatrixBase, Sequence[Any]]
) -> Matrix:
"""
Coerce a symbol expression into a sympy Matrix.
If `s_expr` is already a MatrixBase, list, or tuple, it is converted
via `Matrix(s_expr)`. A bare scalar expression is wrapped into a
1×1 matrix so that downstream code can treat scalar and matrix-valued
operators uniformly.
Parameters
----------
s_expr : sympy.Expr, sympy.MatrixBase, list, or tuple
The symbol or matrix of symbols.
Returns
-------
sympy.Matrix
Square matrix of symbol expressions.
Raises
------
ValueError
If the resulting matrix is not square (checked by callers).
"""
if isinstance(s_expr, (MatrixBase, list, tuple)):
return Matrix(s_expr)
return Matrix([[s_expr]])
def _quantity_fn(
quantity: Literal['real', 'imag', 'abs']
) -> Callable[[np.ndarray], np.ndarray]:
"""
Map a string identifier to the corresponding NumPy function.
Parameters
----------
quantity : str
One of 'real', 'imag', or 'abs'.
Returns
-------
callable
The corresponding NumPy function (`np.real`, `np.imag`, or `np.abs`).
Raises
------
ValueError
If `quantity` is not one of the supported strings.
"""
try:
return {'real': np.real, 'imag': np.imag, 'abs': np.abs}[quantity]
except KeyError:
raise ValueError("quantity must be 'real', 'imag', or 'abs'")
def _finish_headless(
fig: Figure,
save_path: Optional[str] = None
) -> Figure:
"""
Finalize a matplotlib figure for headless rendering or saving.
Applies a tight layout, optionally saves the figure to disk, and closes it.
Parameters
----------
fig : matplotlib.figure.Figure
The figure to finalize.
save_path : str, optional
If provided, the path where the figure will be saved (dpi=150).
Returns
-------
matplotlib.figure.Figure
The closed figure object.
"""
fig.tight_layout()
if save_path:
fig.savefig(save_path, dpi=150)
plt.close(fig)
return fig
[docs]
def plot_scalar_1d(
t: np.ndarray,
U: np.ndarray,
x: np.ndarray,
title: str = "u(x, t)",
quantity: Literal['real', 'imag', 'abs'] = 'real',
n_snapshots: int = 6,
save_path: Optional[str] = None
) -> Figure:
"""
Plot a scalar 1D space-time solution as a combined heatmap and
snapshot overlay.
Parameters
----------
t : ndarray, shape (n_times,)
Time samples.
U : ndarray, shape (n_times, Nx)
Solution values u(x, t) sampled on the grid.
x : ndarray, shape (Nx,)
Spatial grid.
title : str, default="u(x, t)"
Base title used for the heatmap panel.
quantity : {'real', 'imag', 'abs'}, default='real'
Which part of U to plot.
n_snapshots : int, default=6
Number of time slices drawn as line overlays in the second panel.
save_path : str, optional
If given, the figure is saved to this path (dpi=150) before closing.
Returns
-------
matplotlib.figure.Figure
The completed figure (already closed via `_finish_headless`, so it
will not display inline; use `save_path` or re-show it explicitly).
"""
field = _quantity_fn(quantity)(U)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 4))
im = ax1.pcolormesh(x, t, field, shading='auto', cmap='RdBu_r')
ax1.set_xlabel('x'); ax1.set_ylabel('t')
ax1.set_title(f"{title} -- space-time ({quantity})")
fig.colorbar(im, ax=ax1)
idx = np.linspace(0, len(t) - 1, n_snapshots).astype(int)
cmap = plt.cm.viridis(np.linspace(0, 1, len(idx)))
for c, i in zip(cmap, idx):
ax2.plot(x, field[i], color=c, label=f"t={t[i]:.2f}")
ax2.set_xlabel('x'); ax2.set_ylabel(quantity)
ax2.set_title("snapshots")
ax2.legend(fontsize=8, ncol=2)
return _finish_headless(fig, save_path)
[docs]
def plot_matrix_1d(
t: np.ndarray,
U: np.ndarray,
x: np.ndarray,
labels: Optional[List[str]] = None,
quantity: Literal['real', 'imag', 'abs'] = 'real',
save_path: Optional[str] = None
) -> Figure:
"""
Plot each component of a matrix-valued 1D solution as a stacked
space-time heatmap.
Parameters
----------
t : ndarray, shape (n_times,)
Time samples.
U : ndarray, shape (n_times, size, Nx)
Diagonal (or otherwise reduced) matrix solution components, one
row of panels per index k = 0, ..., size-1.
x : ndarray, shape (Nx,)
Spatial grid.
labels : list of str, optional
One label per component; defaults to `["u_1", ..., "u_size"]`.
quantity : {'real', 'imag', 'abs'}, default='real'
Which part of U to plot.
save_path : str, optional
If given, the figure is saved to this path (dpi=150) before closing.
Returns
-------
matplotlib.figure.Figure
The completed figure (already closed via `_finish_headless`).
"""
field_fn = _quantity_fn(quantity)
size = U.shape[1]
labels = labels or [f"u_{k+1}" for k in range(size)]
fig, axes = plt.subplots(size, 1, figsize=(6, 3 * size), sharex=True)
axes = [axes] if size == 1 else axes
for k, ax in enumerate(axes):
im = ax.pcolormesh(x, t, field_fn(U[:, k, :]), shading='auto', cmap='RdBu_r')
ax.set_ylabel('t')
ax.set_title(f"{labels[k]} ({quantity})")
fig.colorbar(im, ax=ax)
axes[-1].set_xlabel('x')
return _finish_headless(fig, save_path)
[docs]
def plot_scalar_2d(
t: np.ndarray,
U: np.ndarray,
x: np.ndarray,
y: np.ndarray,
times: Optional[Sequence[int]] = None,
quantity: Literal['real', 'imag', 'abs'] = 'real',
save_path: Optional[str] = None
) -> Figure:
"""
Plot a scalar 2D solution at selected time instants as a row of
side-by-side pcolormesh panels.
Parameters
----------
t : ndarray, shape (n_times,)
Time samples.
U : ndarray, shape (n_times, Nx, Ny)
Solution values u(x, y, t) sampled on the grid.
x, y : ndarray
Spatial grids along each axis.
times : array_like of int, optional
Indices into `t` selecting which snapshots to plot. Defaults to 6
indices evenly spaced across the whole time range.
quantity : {'real', 'imag', 'abs'}, default='real'
Which part of U to plot.
save_path : str, optional
If given, the figure is saved to this path (dpi=150) before closing.
Returns
-------
matplotlib.figure.Figure
The completed figure (already closed via `_finish_headless`).
"""
field_fn = _quantity_fn(quantity)
times = np.linspace(0, len(t) - 1, 6).astype(int) if times is None else times
n = len(times)
fig, axes = plt.subplots(1, n, figsize=(3 * n, 3), sharey=True)
axes = [axes] if n == 1 else axes
im = None
for ax, i in zip(axes, times):
im = ax.pcolormesh(x, y, field_fn(U[i]).T, shading='auto', cmap='RdBu_r')
ax.set_title(f"t={t[i]:.2f}")
ax.set_xlabel('x')
axes[0].set_ylabel('y')
fig.colorbar(im, ax=axes[-1])
return _finish_headless(fig, save_path)
[docs]
def animate_scalar_1d(
t: np.ndarray,
U: np.ndarray,
x: np.ndarray,
quantity: Literal['real', 'imag', 'abs'] = 'real',
interval: int = 40,
save_path: Optional[str] = None
) -> FuncAnimation:
"""
Animate a scalar 1D solution as a line plot evolving in time.
Parameters
----------
t : ndarray, shape (n_times,)
Time samples.
U : ndarray, shape (n_times, Nx)
Solution values u(x, t) sampled on the grid.
x : ndarray, shape (Nx,)
Spatial grid.
quantity : {'real', 'imag', 'abs'}, default='real'
Which part of U to plot; also sets the fixed y-axis limits from
the min/max of that quantity over the whole trajectory.
interval : int, default=40
Delay between animation frames, in milliseconds.
save_path : str, optional
If given, the animation is saved to this path via `anim.save`.
Returns
-------
matplotlib.animation.FuncAnimation
The animation object (figure is not shown automatically).
"""
from matplotlib.animation import FuncAnimation
field = _quantity_fn(quantity)(U)
fig, ax = plt.subplots(figsize=(6, 4))
line, = ax.plot(x, field[0])
ax.set_ylim(field.min(), field.max())
ax.set_xlabel('x'); ax.set_ylabel(quantity)
title = ax.set_title(f"t={t[0]:.2f}")
def update(i):
line.set_ydata(field[i])
title.set_text(f"t={t[i]:.2f}")
return line, title
anim = FuncAnimation(fig, update, frames=len(t), interval=interval, blit=False)
if save_path:
anim.save(save_path)
plt.close(fig)
return anim
# --- New: matrix-field solvers (solve_matrix_field / solve_sylvester_field)
# had NO plotting function at all -- their output shape (n_saved, N, N,
# *grid) doesn't fit plot_matrix_1d's (n_saved, size, *grid). These fill
# that gap.
def _matrix_field_reduce(
U: np.ndarray,
component: Union[Literal['trace', 'frobenius', 'diag'], Tuple[int, int]],
quantity: Literal['real', 'imag', 'abs']
) -> Tuple[np.ndarray, Callable[[np.ndarray], np.ndarray]]:
"""Reduce a (n_saved, N, N, *grid) matrix-field array to a single
real/complex scalar field (n_saved, *grid), per `component`."""
if component == 'trace':
return np.trace(U, axis1=1, axis2=2), _quantity_fn(quantity)
if component == 'frobenius':
return np.linalg.norm(U, axis=(1, 2)), np.abs # already real, non-negative
if component == 'diag':
raise ValueError("component='diag' needs the *_1d panel-per-entry "
"helper (plot_matrix_field_1d); pick 'trace', "
"'frobenius', or an (i, j) entry here.")
i, j = component
return U[:, i, j, ...], _quantity_fn(quantity)
[docs]
def plot_matrix_field_1d(
t: np.ndarray,
U: np.ndarray,
x: np.ndarray,
quantity: Literal['real', 'imag', 'abs'] = 'abs',
component: Union[Literal['diag', 'trace', 'frobenius'], Tuple[int, int]] = 'diag',
labels: Optional[List[str]] = None,
save_path: Optional[str] = None
) -> Figure:
"""
Space-time heatmap(s) for a matrix-valued 1D solution.
Parameters
----------
t : ndarray, shape (n_times,)
Time samples.
U : ndarray, shape (n_times, N, N, Nx)
Matrix-valued solution field, as returned by `solve_matrix_field`
/ `solve_sylvester_field` in 1D.
x : ndarray, shape (Nx,)
Spatial grid.
quantity : {'real', 'imag', 'abs'}, default='abs'
Which part of the (reduced) field to plot.
component : 'diag' | 'trace' | 'frobenius' | (i, j), default='diag'
'diag' -- one panel per diagonal entry U_kk(x, t).
'trace' -- single panel, sum_k U_kk(x, t).
'frobenius' -- single panel, ||U(x, t)||_F.
(i, j) -- single panel, the (i, j) entry U_ij(x, t).
labels : list of str, optional
Panel labels used when `component='diag'`; defaults to
`["U_11", "U_22", ...]`.
save_path : str, optional
If given, the figure is saved to this path (dpi=150) before closing.
Returns
-------
matplotlib.figure.Figure
The completed figure (already closed via `_finish_headless`).
"""
if component == 'diag':
field_fn = _quantity_fn(quantity)
size = U.shape[1]
labels = labels or [f"U_{k+1}{k+1}" for k in range(size)]
fig, axes = plt.subplots(size, 1, figsize=(6, 3 * size), sharex=True)
axes = [axes] if size == 1 else axes
for k, ax in enumerate(axes):
im = ax.pcolormesh(x, t, field_fn(U[:, k, k, :]), shading='auto', cmap='RdBu_r')
ax.set_ylabel('t'); ax.set_title(f"{labels[k]} ({quantity})")
fig.colorbar(im, ax=ax)
axes[-1].set_xlabel('x')
return _finish_headless(fig, save_path)
panel, field_fn = _matrix_field_reduce(U, component, quantity)
if component in ('trace', 'frobenius'):
title = 'tr U(x, t)' if component == 'trace' else '||U(x, t)||_F'
else:
title = f"U_{component[0]+1}{component[1]+1}(x, t)"
fig, ax = plt.subplots(figsize=(6, 4))
im = ax.pcolormesh(x, t, field_fn(panel), shading='auto', cmap='RdBu_r')
ax.set_xlabel('x'); ax.set_ylabel('t'); ax.set_title(f"{title} ({quantity})")
fig.colorbar(im, ax=ax)
return _finish_headless(fig, save_path)
[docs]
def plot_matrix_field_2d(
t: np.ndarray,
U: np.ndarray,
x: np.ndarray,
y: np.ndarray,
times: Optional[Sequence[int]] = None,
quantity: Literal['real', 'imag', 'abs'] = 'abs',
component: Union[Literal['trace', 'frobenius'], Tuple[int, int]] = 'trace',
save_path: Optional[str] = None
) -> Figure:
"""
Plot snapshot panels for a matrix-valued 2D solution field.
Parameters
----------
t : numpy.ndarray
1D array of time samples, shape (n_times,).
U : numpy.ndarray
Matrix-valued solution field, shape (n_times, N, N, Nx, Ny).
x, y : numpy.ndarray
1D arrays of spatial grid coordinates.
times : array_like of int, optional
Indices into `t` selecting which snapshots to plot. Defaults to 6
evenly spaced indices.
quantity : {'real', 'imag', 'abs'}, optional
Which part of the reduced field to plot. Default is 'abs'.
component : {'trace', 'frobenius'} or tuple of int, optional
How to reduce the matrix field to a scalar field.
'trace' : sum of diagonal entries.
'frobenius' : Frobenius norm.
(i, j) : specific matrix entry.
Default is 'trace'.
save_path : str, optional
If provided, saves the figure to this path.
Returns
-------
matplotlib.figure.Figure
The completed and closed figure object.
"""
panel_all, field_fn = _matrix_field_reduce(U, component, quantity)
times = np.linspace(0, len(t) - 1, 6).astype(int) if times is None else times
n = len(times)
fig, axes = plt.subplots(1, n, figsize=(3 * n, 3), sharey=True)
axes = [axes] if n == 1 else axes
im = None
for ax, idx in zip(axes, times):
im = ax.pcolormesh(x, y, field_fn(panel_all[idx]).T, shading='auto', cmap='RdBu_r')
ax.set_title(f"t={t[idx]:.2f}"); ax.set_xlabel('x')
axes[0].set_ylabel('y')
fig.colorbar(im, ax=axes[-1])
return _finish_headless(fig, save_path)
# --- New: solve_second_order returns (U, V) but there was no combined
# view -- previously required calling plot_scalar_1d twice by hand.
[docs]
def plot_wave_solution_1d(
t: np.ndarray,
U: np.ndarray,
V: np.ndarray,
x: np.ndarray,
quantity: Literal['real', 'imag', 'abs'] = 'real',
save_path: Optional[str] = None
) -> Figure:
"""
Plot side-by-side space-time heatmaps of displacement and velocity.
Intended for the output of `solve_second_order` (scalar, 1D case).
Parameters
----------
t : numpy.ndarray
1D array of time samples, shape (n_times,).
U : numpy.ndarray
Displacement field u(x, t), shape (n_times, Nx).
V : numpy.ndarray
Velocity field ∂_t u(x, t), shape (n_times, Nx).
x : numpy.ndarray
1D array of spatial grid coordinates, shape (Nx,).
quantity : {'real', 'imag', 'abs'}, optional
Which part of the fields to plot. Default is 'real'.
save_path : str, optional
If provided, saves the figure to this path.
Returns
-------
matplotlib.figure.Figure
The completed and closed figure object.
"""
field_fn = _quantity_fn(quantity)
fig, axes = plt.subplots(1, 2, figsize=(11, 4), sharex=True, sharey=True)
for ax, field, label in zip(axes, (U, V), ('u', r'$\partial_t u$')):
im = ax.pcolormesh(x, t, field_fn(field), shading='auto', cmap='RdBu_r')
ax.set_xlabel('x'); ax.set_title(f"{label} ({quantity})")
fig.colorbar(im, ax=ax)
axes[0].set_ylabel('t')
return _finish_headless(fig, save_path)
# ----------------------------------------------------------------------
# Singularity & Ray Flow
# ----------------------------------------------------------------------
def _order_freq_vars(
freq: Sequence[Symbol],
dim: int
) -> List[Symbol]:
"""
Order a set of free frequency symbols into canonical (ξ, η) or
(ξ, η, …) sequence matching the spatial dimension.
Recognised canonical names are matched first ('xi', 'eta', 'kx',
'ky', 'k1', 'k2'); any remaining symbols are appended in
alphabetical order. This ensures a deterministic variable ordering
for differentiation and lambdification regardless of the order in
which SymPy reports free symbols.
Parameters
----------
freq : iterable of sympy.Symbol
Candidate frequency symbols extracted from an expression.
dim : int
Number of frequency variables expected (1 or 2).
Returns
-------
list of sympy.Symbol
Ordered frequency symbols of length `dim`.
"""
if dim == 1:
return list(freq)
by_name = {str(s): s for s in freq}
out = []
for canon in ('xi', 'eta', 'kx', 'ky', 'k1', 'k2'):
if canon in by_name and len(out) < dim:
out.append(by_name.pop(canon))
out += sorted(by_name.values(), key=lambda s: s.name)
return out[:dim]
[docs]
def characteristic_hamiltonians(
s_expr: Union[Expr, MatrixBase],
vars_x: Sequence[Symbol],
vars_xi: Optional[Sequence[Symbol]] = None
) -> Tuple[List[Expr], List[Symbol], List[Symbol]]:
"""
Extract the characteristic Hamiltonian functions H(x, ξ) from a
(possibly matrix-valued) operator symbol.
For a scalar symbol p(x, ξ), the single Hamiltonian is
H(x, ξ) = Re(p(x, ξ))
For a matrix symbol P(x, ξ), the eigenvalues λ_k(x, ξ) are computed
symbolically and each branch yields
H_k(x, ξ) = Re(λ_k(x, ξ))
These Hamiltonians generate the bicharacteristic (ray) flow via
Hamilton's equations:
ẋ = ∂H/∂ξ, ξ̇ = −∂H/∂x
Parameters
----------
s_expr : sympy.Expr or sympy.Matrix
Operator symbol (scalar or matrix-valued).
vars_x : list of sympy.Symbol
Spatial variables.
vars_xi : list of sympy.Symbol, optional
Frequency variables. If None, they are inferred from the free
symbols of `s_expr` that are not in `vars_x`, and ordered
canonically via `_order_freq_vars`.
Returns
-------
H_list : list of sympy.Expr
One Hamiltonian per characteristic branch (eigenvalue).
xs : list of sympy.Symbol
Canonical spatial symbols (real=True) used in H_list.
xis : list of sympy.Symbol
Canonical frequency symbols (real=True) used in H_list.
Notes
-----
The classical Hamiltonian governing bicharacteristic flow is the
(real part of the) principal symbol itself. A previous version of
this function erroneously computed ``Re(i * p)``, which vanishes
identically for any real-valued symbol and therefore produced
trivial (stationary) trajectories in the flow visualization.
The substitution to fresh canonical symbols ensures consistent
differentiation even if the input expression uses symbols with
different assumptions.
"""
S = _matrix_of(s_expr)
dim = len(vars_x)
freq = [s for s in S.free_symbols if s not in set(vars_x)]
vars_xi = list(vars_xi) if vars_xi is not None else _order_freq_vars(freq, dim)
xs = [Symbol(v.name, real=True) for v in vars_x]
xis = [Symbol(v.name, real=True) for v in vars_xi]
S = S.subs(dict(zip(list(vars_x) + list(vars_xi), xs + xis)))
eigen = [S[0, 0]] if S.shape == (1, 1) else list(S.eigenvals().keys())
H_list = []
for lam in eigen:
H_list.append(simplify(re(lam)))
return H_list, xs, xis
[docs]
def integrate_singularity(
s_expr: Union[Expr, MatrixBase],
vars_x: Sequence[Symbol],
x0: Union[float, Sequence[float]] = 0.0,
xi0: Union[float, Sequence[float]] = 5.0,
tmax: float = 4.0,
n_frames: int = 100,
vars_xi: Optional[Sequence[Symbol]] = None,
branches: Union[Literal['all'], int, Sequence[int]] = 'all',
method: str = 'RK45',
**ivp_kwargs: Any
) -> Tuple[
List[Expr],
List[Symbol],
List[Symbol],
np.ndarray,
List[np.ndarray]
]:
"""
Numerically integrate bicharacteristic (Hamiltonian ray) trajectories
from an initial phase-space point (x₀, ξ₀).
For each characteristic branch H_k, Hamilton's equations
ẋ = ∂H_k/∂ξ, ξ̇ = −∂H_k/∂x
are integrated using `scipy.integrate.solve_ivp` over [0, tmax].
Parameters
----------
s_expr : sympy.Expr or sympy.Matrix
Operator symbol from which Hamiltonians are extracted.
vars_x : list of sympy.Symbol
Spatial variables.
x0 : float or array_like, default 0.0
Initial spatial position(s). Scalar for 1D, sequence for 2D.
xi0 : float or array_like, default 5.0
Initial frequency (momentum) component(s).
tmax : float, default 4.0
Final integration time.
n_frames : int, default 100
Number of output time samples in [0, tmax].
vars_xi : list of sympy.Symbol, optional
Explicit frequency variables (inferred if None).
branches : 'all', int, or list of int, default 'all'
Which characteristic branches to integrate. 'all' integrates
every branch; an int or list selects specific ones.
method : str, default 'RK45'
ODE solver method passed to `solve_ivp`.
**ivp_kwargs
Additional keyword arguments forwarded to `solve_ivp`
(e.g. `rtol`, `atol`, `max_step`).
Returns
-------
H_list : list of sympy.Expr
Hamiltonian expressions for the integrated branches.
xs : list of sympy.Symbol
Canonical spatial symbols.
xis : list of sympy.Symbol
Canonical frequency symbols.
t_eval : ndarray, shape (n_frames,)
Time samples at which trajectories are recorded.
trajs : list of ndarray
`trajs[b]` has shape (2·dim, n_frames): the first dim rows are
position components, the last dim rows are momentum components.
Examples
--------
>>> H, xs, xis, t, trajs = integrate_singularity(xi**2 + x**2, [x],
... x0=0.0, xi0=3.0, tmax=6.0)
"""
H_all, xs, xis = characteristic_hamiltonians(s_expr, vars_x, vars_xi)
H_list = [H_all[b] for b in ([branches] if isinstance(branches, int) else branches)] if branches != 'all' else H_all
dim = len(xs)
y0 = np.concatenate([np.atleast_1d(x0), np.atleast_1d(xi0)])
t_eval = np.linspace(0.0, tmax, n_frames)
trajs = []
for H in H_list:
rhs_exprs = [diff(H, k) for k in xis] + [-diff(H, x) for x in xs]
f = lambdify(xs + xis, rhs_exprs, 'numpy')
sol = solve_ivp(lambda t, Y: f(*Y), (0.0, tmax), y0, t_eval=t_eval, method=method, **ivp_kwargs)
trajs.append(sol.y)
return H_list, xs, xis, t_eval, trajs
def _trail_animation(
coords_list: List[Dict[str, np.ndarray]],
colors: Sequence[Any],
px: str,
py: str,
pz: Optional[str] = None,
interval: int = 50,
pad_frac: float = 0.08
) -> Tuple[Figure, FuncAnimation]:
"""Shared 'growing dashed trail + moving point' animation builder for
both the 2D and 3D singularity animations -- factors out what used to
be two near-identical copies of the same figure/update-function setup.
FIX: the axes are now explicitly sized to the full trajectory range
(with a small padding margin) before the animation starts. Previously
no xlim/ylim(/zlim) were set at all: since every trail/point artist is
created empty (`ax.plot([], [])`) and only ever updated via
`set_data`/`set_3d_properties` (not `ax.relim()` + `autoscale_view()`),
matplotlib never grew the view beyond its default (0, 1) x (0, 1) box,
so the rendered animation domain was almost always far too small to
show the actual ray flow.
"""
is_3d = pz is not None
fig = plt.figure(figsize=(7.5, 5.5) if is_3d else (6, 5))
ax = fig.add_subplot(111, projection='3d') if is_3d else fig.add_subplot(111)
def _padded_range(values):
lo, hi = float(np.min(values)), float(np.max(values))
span = hi - lo
pad = span * pad_frac if span > 0 else (abs(lo) * pad_frac or 1.0)
return lo - pad, hi + pad
all_x = np.concatenate([np.atleast_1d(c[px]) for c in coords_list])
all_y = np.concatenate([np.atleast_1d(c[py]) for c in coords_list])
ax.set_xlim(*_padded_range(all_x))
ax.set_ylim(*_padded_range(all_y))
ax.set_xlabel(px)
ax.set_ylabel(py)
if is_3d:
all_z = np.concatenate([np.atleast_1d(c[pz]) for c in coords_list])
ax.set_zlim(*_padded_range(all_z))
ax.set_zlabel(pz)
else:
ax.grid(True, alpha=0.3)
trails, points = [], []
for c in colors:
if is_3d:
tr, = ax.plot([], [], [], ls='--', lw=1.3, color=c)
pt, = ax.plot([], [], [], 'o', ms=6, color=c)
else:
tr, = ax.plot([], [], ls='--', lw=1.3, alpha=0.6, color=c)
pt, = ax.plot([], [], 'o', ms=6.5, color=c)
trails.append(tr)
points.append(pt)
n_act = min(len(c[px]) for c in coords_list)
def update(i):
for b, c in enumerate(coords_list):
xa, ya = c[px][:i + 1], c[py][:i + 1]
trails[b].set_data(xa, ya)
if is_3d:
za = c[pz][:i + 1]
trails[b].set_3d_properties(za)
points[b].set_data([xa[-1]], [ya[-1]])
points[b].set_3d_properties([za[-1]])
else:
points[b].set_data([xa[-1]], [ya[-1]])
return trails + points
anim = FuncAnimation(fig, update, frames=n_act, interval=interval, blit=False)
plt.close(fig)
return fig, anim
[docs]
def animate_singularity(
s_expr: Expr,
vars_x: Sequence[Symbol],
x0: Union[float, Sequence[float]] = 0.0,
xi0: Union[float, Sequence[float]] = 5.0,
tmax: float = 4.0,
n_frames: int = 100,
projection: Optional[Literal['phase', 'position', 'frequency']] = None,
branches: Union[Literal['all'], int, Sequence[int]] = 'all',
labels: Optional[List[str]] = None,
interval: int = 50,
contours: bool = True,
solution: Optional[Any] = None,
quantity: str = 'abs',
save_path: Optional[str] = None
) -> FuncAnimation:
"""
Animate the propagation of singularities along bicharacteristic
trajectories, projected onto a 2D phase-space plane.
Internally calls `integrate_singularity` to obtain the trajectories,
then draws a growing dashed trail plus a moving point per branch via
the shared `_trail_animation` helper.
Parameters
----------
s_expr : sympy.Expr
Principal symbol used to build the Hamiltonian(s).
vars_x : list of sympy.Symbol
Spatial variables.
x0 : float or array_like, default=0.0
Initial spatial position(s).
xi0 : float or array_like, default=5.0
Initial frequency (momentum) component(s).
tmax : float, default=4.0
Final integration time.
n_frames : int, default=100
Number of time samples used for the trajectory and the animation.
projection : {'phase', 'position', 'frequency'} or None, default=None
Which plane to draw. In 1D, defaults to 'phase' (x vs xi); in 2D,
defaults to 'position' (x vs y) and ('frequency'/'phase' are not
selectable in 2D -- the projection is always (x, y)).
branches : 'all', int, or list of int, default='all'
Which characteristic branches to animate.
labels : list of str, optional
Currently unused (reserved for per-branch legend labels).
interval : int, default=50
Delay between animation frames, in milliseconds.
contours : bool, default=True
Currently unused (reserved for background contour overlays).
solution : optional
Currently unused (reserved for overlaying a PDE solution field).
quantity : str, default='abs'
Currently unused (reserved alongside `solution`).
save_path : str, optional
If given, the animation is saved to this path via `anim.save`.
Returns
-------
matplotlib.animation.FuncAnimation
The animation object.
Raises
------
ValueError
If `projection` is not one of the supported values for a 1D symbol.
"""
dim = len(vars_x)
H_list, xs, xis, t_eval, trajs = integrate_singularity(
s_expr, vars_x, x0=x0, xi0=xi0, tmax=tmax, n_frames=n_frames, branches=branches)
n_act = min(Y.shape[1] for Y in trajs)
t_eval = t_eval[:n_act]
trajs = [Y[:, :n_act] for Y in trajs]
names = [v.name for v in xs] + [v.name for v in xis]
coords = [{**dict(zip(names, Y)), 't': t_eval} for Y in trajs]
# FIX: `projection` was never defaulted when None, so in 1D the
# fallback branch picked py='y' -- a key that doesn't exist in 1D
# coords (only 'x', 'xi', 't') -- raising a KeyError as soon as the
# axis limits are computed from the data. Default explicitly, as
# documented ('phase' in 1D, 'position' in 2D), and support the same
# projections as the class method (position/frequency plotted on the
# diagonal in 1D, since there's only one spatial/frequency axis).
if projection is None:
projection = 'phase' if dim == 1 else 'position'
if dim == 1:
px, py = {'phase': ('x', 'xi'), 'position': ('x', 'x'),
'frequency': ('xi', 'xi')}.get(projection, (None, None))
if px is None:
raise ValueError(
f"Invalid projection {projection!r} for a 1D symbol; "
"use 'phase', 'position', or 'frequency'."
)
else:
px, py = 'x', 'y'
colors = [plt.cm.tab10.colors[b % 10] for b in range(len(H_list))]
_, anim = _trail_animation(coords, colors, px, py, interval=interval)
if save_path:
anim.save(save_path)
return anim
[docs]
def animate_singularity_3d(
s_expr: Expr,
vars_x: Sequence[Symbol],
x0: Union[float, Sequence[float]] = 0.0,
xi0: Union[float, Sequence[float]] = 5.0,
tmax: float = 4.0,
n_frames: int = 100,
projection: Optional[Any] = None,
branches: Union[Literal['all'], int, Sequence[int]] = 'all',
labels: Optional[List[str]] = None,
interval: int = 50,
save_path: Optional[str] = None
) -> FuncAnimation:
"""
Animate bicharacteristic trajectories in a 3D matplotlib plot.
Internally calls `integrate_singularity` to obtain the trajectories,
then draws a growing dashed trail plus a moving point per branch via
the shared `_trail_animation` helper. In 1D, the third axis is time
`t`; in 2D (or higher), the first three phase-space coordinates
`(x, y, ...)` are used directly.
Parameters
----------
s_expr : sympy.Expr
Principal symbol used to build the Hamiltonian(s).
vars_x : list of sympy.Symbol
Spatial variables.
x0 : float or array_like, default=0.0
Initial spatial position(s).
xi0 : float or array_like, default=5.0
Initial frequency (momentum) component(s).
tmax : float, default=4.0
Final integration time.
n_frames : int, default=100
Number of time samples used for the trajectory and the animation.
projection : optional
Currently unused for the 3D case (reserved for API parity with
`animate_singularity`); the axes are always chosen as described
above.
branches : 'all', int, or list of int, default='all'
Which characteristic branches to animate.
labels : list of str, optional
Currently unused (reserved for per-branch legend labels).
interval : int, default=50
Delay between animation frames, in milliseconds.
save_path : str, optional
If given, the animation is saved to this path via `anim.save`.
Returns
-------
matplotlib.animation.FuncAnimation
The animation object.
"""
H_list, xs, xis, t_eval, trajs = integrate_singularity(
s_expr, vars_x, x0=x0, xi0=xi0, tmax=tmax, n_frames=n_frames, branches=branches)
n_act = min(Y.shape[1] for Y in trajs)
t_eval, trajs = t_eval[:n_act], [Y[:, :n_act] for Y in trajs]
names = [v.name for v in xs] + [v.name for v in xis]
coords = [{**dict(zip(names, Y)), 't': t_eval} for Y in trajs]
px, py, pz = (names[0], names[1], 't') if len(vars_x) == 1 else (names[0], names[1], names[2])
colors = [plt.cm.tab10.colors[b % 10] for b in range(len(H_list))]
_, anim = _trail_animation(coords, colors, px, py, pz=pz, interval=interval)
if save_path:
anim.save(save_path)
return anim