PyTorch _apply_s2 имел ту же ошибку что и core.py: - отсутствовал completion-of-square factor exp(b²τ/(4a)) - отсутствовал drift phase shift exp(2πi·k·b_mean·τ) Это приводило к неправильным результатам RemizovNet и EquationDiscoverer для уравнений с b≠0. Добавлены тесты: - test_s2_fft_matches_direct_with_drift (core, b=1.0) - test_s2_with_drift (PyTorch layer) - ужесточён tolerance: atol=0.15 → rtol=0.05 21 тестов проходят. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
113 lines
4.2 KiB
Python
113 lines
4.2 KiB
Python
"""Tests for the core Chernoff approximation solver."""
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from remizov_net.core import ChernoffStep, ODECoefficients, RemizovSolver
|
|
|
|
|
|
class TestChernoffStep:
|
|
"""Test individual Chernoff step operations."""
|
|
|
|
def setup_method(self):
|
|
self.x = np.linspace(-5, 5, 101)
|
|
self.coeffs = ODECoefficients(a=1.0, b=0.0, c=-1.0)
|
|
self.step = ChernoffStep(self.coeffs, self.x)
|
|
|
|
def test_s3_identity_at_zero_tau(self):
|
|
"""S₃(0) should be identity."""
|
|
f = np.sin(self.x)
|
|
result = self.step.apply_s3(f, tau=0.0)
|
|
np.testing.assert_allclose(result, f, atol=1e-12)
|
|
|
|
def test_s3_exponential_scaling(self):
|
|
"""S₃(τ) should multiply by exp(τ·V(x))."""
|
|
f = np.ones_like(self.x)
|
|
tau = 0.1
|
|
result = self.step.apply_s3(f, tau)
|
|
expected = np.exp(tau * self.step.V)
|
|
np.testing.assert_allclose(result, expected, atol=1e-12)
|
|
|
|
def test_s2_preserves_constant(self):
|
|
"""Gaussian convolution of a constant should return ~constant."""
|
|
f = np.ones_like(self.x) * 3.0
|
|
result = self.step.apply_s2(f, tau=0.1)
|
|
# Interior points should be close to 3.0 (boundary effects at edges)
|
|
interior = slice(20, 80)
|
|
np.testing.assert_allclose(result[interior], 3.0, atol=0.1)
|
|
|
|
def test_s2_fft_matches_direct(self):
|
|
"""FFT-based S₂ should approximate direct computation (b=0)."""
|
|
f = np.exp(-self.x**2)
|
|
result_direct = self.step.apply_s2(f, tau=0.05)
|
|
result_fft = self.step.apply_s2_fft(f, tau=0.05)
|
|
interior = slice(20, 80)
|
|
np.testing.assert_allclose(
|
|
result_fft[interior], result_direct[interior], rtol=0.05,
|
|
)
|
|
|
|
def test_s2_fft_matches_direct_with_drift(self):
|
|
"""FFT-based S₂ should match direct computation when b != 0."""
|
|
coeffs = ODECoefficients(a=0.5, b=1.0, c=0.0)
|
|
step = ChernoffStep(coeffs, self.x)
|
|
f = np.exp(-self.x**2)
|
|
result_direct = step.apply_s2(f, tau=0.05)
|
|
result_fft = step.apply_s2_fft(f, tau=0.05)
|
|
interior = slice(20, 80)
|
|
np.testing.assert_allclose(
|
|
result_fft[interior], result_direct[interior], rtol=0.05,
|
|
)
|
|
|
|
def test_potential_computation(self):
|
|
"""V(x) = c(x) - b(x)^2 / (4*a(x))."""
|
|
coeffs = ODECoefficients(a=2.0, b=1.0, c=3.0)
|
|
step = ChernoffStep(coeffs, self.x)
|
|
expected_potential = 3.0 - 1.0**2 / (4.0 * 2.0) # = 2.875
|
|
np.testing.assert_allclose(step.V, expected_potential, atol=1e-12)
|
|
|
|
|
|
class TestODECoefficients:
|
|
"""Test coefficient evaluation."""
|
|
|
|
def test_constant_coefficients(self):
|
|
x = np.array([1.0, 2.0, 3.0])
|
|
coeffs = ODECoefficients(a=2.0, b=3.0, c=4.0)
|
|
np.testing.assert_allclose(coeffs.eval_a(x), [2.0, 2.0, 2.0])
|
|
np.testing.assert_allclose(coeffs.eval_b(x), [3.0, 3.0, 3.0])
|
|
np.testing.assert_allclose(coeffs.eval_c(x), [4.0, 4.0, 4.0])
|
|
|
|
def test_function_coefficients(self):
|
|
x = np.array([0.0, 1.0, 2.0])
|
|
coeffs = ODECoefficients(
|
|
a=lambda x: x**2 + 1,
|
|
b=np.sin,
|
|
c=lambda x: np.full_like(x, -1.0),
|
|
)
|
|
np.testing.assert_allclose(coeffs.eval_a(x), [1.0, 2.0, 5.0])
|
|
np.testing.assert_allclose(coeffs.eval_b(x), np.sin(x))
|
|
np.testing.assert_allclose(coeffs.eval_c(x), [-1.0, -1.0, -1.0])
|
|
|
|
|
|
class TestRemizovSolver:
|
|
"""Test the full solver on known equations."""
|
|
|
|
def test_lambda_validation(self):
|
|
"""Solver should reject λ ≤ sup c(x)."""
|
|
x = np.linspace(-1, 1, 51)
|
|
coeffs = ODECoefficients(a=1.0, b=0.0, c=2.0)
|
|
with pytest.raises(ValueError, match="must be >"):
|
|
RemizovSolver(coeffs, x, lam=1.0)
|
|
|
|
def test_constant_coeff_basic(self):
|
|
"""Basic smoke test: solver runs without error on y'' - y = g."""
|
|
x = np.linspace(-3, 3, 61)
|
|
coeffs = ODECoefficients(a=1.0, b=0.0, c=-1.0)
|
|
solver = RemizovSolver(coeffs, x, n_steps=10, lam=2.0)
|
|
|
|
g = np.exp(-x**2)
|
|
result = solver.solve(g, t_max=5.0, quad_points=20)
|
|
|
|
# Solution should be finite and smooth
|
|
assert np.all(np.isfinite(result))
|
|
assert result.shape == x.shape
|