Files
remizov-net/tests/test_core.py
T
anorandClaude Opus 4.6 24c31fcce5 feat: initial implementation of RemizovNet
Реализация формулы Ремизова (аппроксимации Чернова) для решения
линейных ОДУ второго порядка с переменными коэффициентами
a(x)y'' + b(x)y' + c(x)y = g(x).

Ядро (NumPy):
- ChernoffStep: S₂ (гауссова свёртка + дрейф) и S₃ (потенциал)
- RemizovSolver: полная формула с интегралом Лапласа

Нейросеть (PyTorch):
- RemizovLayer: один шаг Чернова как дифференцируемый слой
- RemizovNet: n слоёв = формула Ремизова, веса = коэффициенты ОДУ
- EquationDiscoverer: обнаружение уравнения из данных

Примеры:
- example_solver: уравнение теплопроводности, точность до 1e-15
- example_discover: восстановление коэффициентов, 1% ошибка предсказания

19 тестов, ruff clean.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 13:47:35 +10:00

102 lines
3.7 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."""
f = np.exp(-self.x**2)
# For constant coefficients, FFT should be close to direct
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], atol=0.15
)
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