#!/usr/bin/env python3 """ Example 1: Solving the heat equation with Chernoff approximation. Problem: ∂u/∂t = a·∂²u/∂x² + c·u, u(x,0) = exp(-x²) The Chernoff composition [S(T/n)]^n · g approximates e^{TL} g, i.e., the evolution of initial condition g(x) under operator L = a·d²/dx² + c. For pure heat equation (a=1, b=0, c=0): Exact solution: u(x,t) = (1/√(1+4t)) · exp(-x²/(1+4t)) We verify Remizov's formula converges to the exact solution as n_steps increases. """ import numpy as np from remizov_net.core import ChernoffStep, ODECoefficients def exact_heat_solution(x: np.ndarray, t: float) -> np.ndarray: """Exact solution of heat equation with Gaussian initial data.""" return (1.0 / np.sqrt(1.0 + 4.0 * t)) * np.exp(-x**2 / (1.0 + 4.0 * t)) def run_chernoff_evolution( coeffs: ODECoefficients, x: np.ndarray, g: np.ndarray, total_time: float, n_steps: int, ) -> np.ndarray: """Compute [S(T/n)]^n · g — Chernoff approximation of e^{TL} g.""" step = ChernoffStep(coeffs, x) tau = total_time / n_steps result = g.copy() for _ in range(n_steps): result = step.apply(result, tau, use_fft=True) return result def main(): print("=" * 65) print(" Remizov Solver: Heat equation via Chernoff approximation") print("=" * 65) # --- Setup --- x = np.linspace(-4, 4, 201) g = np.exp(-x**2) # Initial condition: Gaussian coeffs = ODECoefficients(a=1.0, b=0.0, c=0.0) # Pure heat: u_t = u_xx # --- Convergence test: vary n_steps --- T = 0.5 # noqa: N806 y_exact = exact_heat_solution(x, T) print("\nHeat equation: du/dt = d²u/dx²") print("Initial condition: u(x,0) = exp(-x²)") print(f"Time: T = {T}") print("\nConvergence as n_steps increases:") print(f"{'n_steps':>10} {'L² error':>12} {'L∞ error':>12} {'Ratio':>8}") print("-" * 46) prev_err = None for n_steps in [5, 10, 20, 40, 80]: y_approx = run_chernoff_evolution(coeffs, x, g, T, n_steps) # Compute errors on interior (avoid boundary effects) interior = (x > -3) & (x < 3) l2_err = np.sqrt(np.mean((y_approx[interior] - y_exact[interior]) ** 2)) linf_err = np.max(np.abs(y_approx[interior] - y_exact[interior])) ratio = f"{prev_err / l2_err:.1f}x" if prev_err else "—" print(f"{n_steps:>10} {l2_err:>12.2e} {linf_err:>12.2e} {ratio:>8}") prev_err = l2_err # --- Evolution snapshots --- print("\nEvolution snapshots at x=0:") print(f"{'T':>6} {'Exact u(0,T)':>14} {'Chernoff':>14} {'Error':>12}") print("-" * 50) for T in [0.1, 0.25, 0.5, 1.0, 2.0]: # noqa: N806 y_exact_t = exact_heat_solution(x, T) y_approx_t = run_chernoff_evolution(coeffs, x, g, T, n_steps=40) mid = len(x) // 2 print( f"{T:>6.2f} " f"{y_exact_t[mid]:>14.6f} " f"{y_approx_t[mid]:>14.6f} " f"{abs(y_exact_t[mid] - y_approx_t[mid]):>12.2e}" ) # --- Damped equation: u_t = u_xx - 0.5*u --- print(f"\n{'=' * 65}") print(" Damped heat equation: du/dt = d²u/dx² - 0.5·u") print(f"{'=' * 65}") print(" Exact: u(x,t) = e^(-0.5t) · heat_solution(x,t)") coeffs_damped = ODECoefficients(a=1.0, b=0.0, c=-0.5) print(f"\n{'T':>6} {'Exact u(0,T)':>14} {'Chernoff':>14} {'Error':>12}") print("-" * 50) for T in [0.1, 0.5, 1.0, 2.0]: # noqa: N806 y_exact_damped = np.exp(-0.5 * T) * exact_heat_solution(x, T) y_approx_damped = run_chernoff_evolution(coeffs_damped, x, g, T, n_steps=40) mid = len(x) // 2 print( f"{T:>6.2f} " f"{y_exact_damped[mid]:>14.6f} " f"{y_approx_damped[mid]:>14.6f} " f"{abs(y_exact_damped[mid] - y_approx_damped[mid]):>12.2e}" ) # --- Variable coefficients demo --- print(f"\n{'=' * 65}") print(" Variable coefficients: a(x) = 1 + 0.5·sin(x)") print(f"{'=' * 65}") coeffs_var = ODECoefficients( a=lambda x: 1.0 + 0.5 * np.sin(x), b=0.0, c=0.0, ) print(" (No closed-form solution — showing Chernoff convergence)") print(f"\n{'n_steps':>10} {'u(0, T=0.5)':>14} {'Δ vs prev':>12}") print("-" * 40) prev_val = None for n_steps in [10, 20, 40, 80, 160]: y_var = run_chernoff_evolution(coeffs_var, x, g, 0.5, n_steps) mid = len(x) // 2 val = y_var[mid] delta = f"{abs(val - prev_val):.2e}" if prev_val is not None else "—" print(f"{n_steps:>10} {val:>14.8f} {delta:>12}") prev_val = val print("\n Successive differences shrink → convergence confirmed") print(" This is the equation Liouville said can't be solved in 1834.") print(" Remizov's formula solves it.\n") if __name__ == "__main__": main()