omnibias.core.tanh_method
Coefficient recurrences, activation contracts, jet algebra, and verified arithmetic shared by the numerical backends.
import omnibias.core.tanh_methodModule contract & mathematical conventions
Tanh-method travelling-wave algebra (theory 02-09).
This is the tanh *algebra*, not a collapse. A multi-kink sum is an
ansatz, not the n-soliton formula (that is theory 02-13). Exactness is
a polynomial identity in T = tanh(xi), verified with rationals.
Shared formulas do not imply universal floating-point identity. Verify dtype, shape, derivative-order support, and the stated numerical assumptions for the path you use.
G1_NAMES
omnibias.core.tanh_method.G1_NAMESSource ↗G1_NAMES: tuple[str, ...] = ('kdv', 'mkdv', 'burgers', 'sine_gordon', 'fkpp', 'fisher', 'boussinesq', 'kuramoto_sivashinsky', 'klein_gordon', 'camassa_holm_adjacent')This exported constant is defined by the expression above. Its exact value and annotation come from the pinned source; use it with the module contract.
Regression example · g2 balance degree
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g2_balance_degree() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
if ans.kind == "tanh_poly":
assert balance_degree(pde) == ans.degree, namepython -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g2_balance_degree -qRegression example · g1 symbolic exactness
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g1_symbolic_exactness -qRead the implementation
G1_NAMES: tuple[str, ...] = (
"kdv",
"mkdv",
"burgers",
"sine_gordon",
"fkpp",
"fisher",
"boussinesq",
"kuramoto_sivashinsky",
"klein_gordon",
"camassa_holm_adjacent",
)PDESpec
omnibias.core.tanh_method.PDESpecSource ↗class PDESpecDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
A constant-coeff travelling-wave PDE as a sum of monomials in u.
Parameters
| Name | Type & default | Contract |
|---|---|---|
namedataclass field | strRequired | See the operation contract and implementation below. |
termsdataclass field | tuple[PDETerm, ...]Required | See the operation contract and implementation below. |
specialdataclass field | Literal['', 'sine_gordon', 'peakon_adjacent']Default: '' | See the operation contract and implementation below. |
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qRegression example · g5 negative control
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g5_negative_control() -> None:
heat = PDESpec(
"heat",
(PDETerm(TermKind.U_T, 1), PDETerm(TermKind.U_XX, -1)),
)
assert solve_ansatz(heat) == ()python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g5_negative_control -qFields & methods
namefield
name: strDeclared str field on PDESpec. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗termsfield
terms: tuple[PDETerm, ...]Declared tuple[PDETerm, ...] field on PDESpec. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗specialfield
special: Literal['', 'sine_gordon', 'peakon_adjacent'] = ''Declared Literal['', 'sine_gordon', 'peakon_adjacent'] field on PDESpec. Default expression: ''.
residual_polymethod
residual_poly(self, jets: Mapping[str, TPoly]) -> TPolySee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
jetspositional-or-keyword | Mapping[str, TPoly]Required | See the operation contract and implementation below. |
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def residual_poly(self, jets: Mapping[str, TPoly]) -> TPoly:
acc = TPoly.constant(0)
for term in self.terms:
acc = acc + jets[term.kind.value].scale(term.coeff)
return accRead the implementation
class PDESpec:
"""A constant-coeff travelling-wave PDE as a sum of monomials in u."""
name: str
terms: tuple[PDETerm, ...]
special: Literal["", "sine_gordon", "peakon_adjacent"] = ""
def residual_poly(self, jets: Mapping[str, TPoly]) -> TPoly:
acc = TPoly.constant(0)
for term in self.terms:
acc = acc + jets[term.kind.value].scale(term.coeff)
return accPDETerm
omnibias.core.tanh_method.PDETermSource ↗class PDETermDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
kinddataclass field | TermKindRequired | See the operation contract and implementation below. |
coeffdataclass field | FractionRequired | See the operation contract and implementation below. |
Regression example · g5 negative control
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g5_negative_control() -> None:
heat = PDESpec(
"heat",
(PDETerm(TermKind.U_T, 1), PDETerm(TermKind.U_XX, -1)),
)
assert solve_ansatz(heat) == ()python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g5_negative_control -qFields & methods
kindfield
kind: TermKindDeclared TermKind field on PDETerm. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗coefffield
coeff: FractionDeclared Fraction field on PDETerm. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗Read the implementation
class PDETerm:
kind: TermKind
coeff: FractionTPoly
omnibias.core.tanh_method.TPolySource ↗class TPolyDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
Polynomial sum a_k T^k with rational coefficients.
Parameters
| Name | Type & default | Contract |
|---|---|---|
coeffsdataclass field | tuple[Fraction, ...]Required | See the operation contract and implementation below. |
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qFields & methods
coeffsfield
coeffs: tuple[Fraction, ...]Declared tuple[Fraction, ...] field on TPoly. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗__post_init__method
__post_init__(self) -> NoneSee the class contract above and the exact method implementation below.
Returns: None
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def __post_init__(self) -> None:
object.__setattr__(self, "coeffs", _trim(tuple(_c(a) for a in self.coeffs)))constantmethod
constant(cls, a: int | float | Fraction) -> TPolySee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | int | float | FractionRequired | See the operation contract and implementation below. |
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def constant(cls, a: int | float | Fraction) -> TPoly:
return cls((_c(a),))tmethod
t(cls) -> TPolySee the class contract above and the exact method implementation below.
Returns: TPoly
Method implementation
def t(cls) -> TPoly:
return cls((Fraction(0), Fraction(1)))degreeproperty
degree(self) -> intSee the class contract above and the exact method implementation below.
Returns: int
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def degree(self) -> int:
return len(self.coeffs) - 1__add__method
__add__(self, other: TPoly) -> TPolySee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
otherpositional-or-keyword | TPolyRequired | See the operation contract and implementation below. |
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def __add__(self, other: TPoly) -> TPoly:
n = max(len(self.coeffs), len(other.coeffs))
out = [Fraction(0)] * n
for i, a in enumerate(self.coeffs):
out[i] += a
for i, a in enumerate(other.coeffs):
out[i] += a
return TPoly(tuple(out))__sub__method
__sub__(self, other: TPoly) -> TPolySee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
otherpositional-or-keyword | TPolyRequired | See the operation contract and implementation below. |
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def __sub__(self, other: TPoly) -> TPoly:
return self + other.scale(-1)scalemethod
scale(self, s: int | float | Fraction) -> TPolySee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
spositional-or-keyword | int | float | FractionRequired | See the operation contract and implementation below. |
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def scale(self, s: int | float | Fraction) -> TPoly:
k = _c(s)
return TPoly(tuple(k * a for a in self.coeffs))__mul__method
__mul__(self, other: TPoly) -> TPolySee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
otherpositional-or-keyword | TPolyRequired | See the operation contract and implementation below. |
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def __mul__(self, other: TPoly) -> TPoly:
out = [Fraction(0)] * (len(self.coeffs) + len(other.coeffs) - 1)
for i, a in enumerate(self.coeffs):
for j, b in enumerate(other.coeffs):
out[i + j] += a * b
return TPoly(tuple(out))__pow__method
__pow__(self, n: int) -> TPolySee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
npositional-or-keyword | intRequired | See the operation contract and implementation below. |
Returns: TPoly
Validation & failure cases
- When
n < 0, raisesValueError('negative powers are not a TPoly').
Method implementation
def __pow__(self, n: int) -> TPoly:
if n < 0:
raise ValueError("negative powers are not a TPoly")
acc = TPoly.constant(1)
for _ in range(n):
acc = acc * self
return accderiv_Tmethod
deriv_T(self) -> TPolySee the class contract above and the exact method implementation below.
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def deriv_T(self) -> TPoly:
if self.degree <= 0:
return TPoly.constant(0)
return TPoly(tuple(Fraction(k) * a for k, a in enumerate(self.coeffs) if k > 0))shiftmethod
shift(self, n: int) -> TPolyMultiply by T^n.
Parameters
| Name | Type & default | Contract |
|---|---|---|
npositional-or-keyword | intRequired | See the operation contract and implementation below. |
Returns: TPoly
Validation & failure cases
- When
n < 0, raisesValueError('shift must be >= 0').
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def shift(self, n: int) -> TPoly:
"""Multiply by ``T^n``."""
if n < 0:
raise ValueError("shift must be >= 0")
if n == 0:
return self
return TPoly((Fraction(0),) * n + self.coeffs)times_one_minus_T2method
times_one_minus_T2(self) -> TPoly(1 - T^2) p.
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def times_one_minus_T2(self) -> TPoly:
"""``(1 - T^2) p``."""
return self - self.shift(2)deriv_ximethod
deriv_xi(self) -> TPolyd/dxi with dT/dxi = 1 - T^2.
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def deriv_xi(self) -> TPoly:
"""``d/dxi`` with ``dT/dxi = 1 - T^2``."""
return self.deriv_T().times_one_minus_T2()is_zeromethod
is_zero(self) -> boolSee the class contract above and the exact method implementation below.
Returns: bool
Method implementation
def is_zero(self) -> bool:
return all(a == 0 for a in self.coeffs)Read the implementation
class TPoly:
"""Polynomial ``sum a_k T^k`` with rational coefficients."""
coeffs: tuple[Fraction, ...]
def __post_init__(self) -> None:
object.__setattr__(self, "coeffs", _trim(tuple(_c(a) for a in self.coeffs)))
@classmethod
def constant(cls, a: int | float | Fraction) -> TPoly:
return cls((_c(a),))
@classmethod
def t(cls) -> TPoly:
return cls((Fraction(0), Fraction(1)))
@property
def degree(self) -> int:
return len(self.coeffs) - 1
def __add__(self, other: TPoly) -> TPoly:
n = max(len(self.coeffs), len(other.coeffs))
out = [Fraction(0)] * n
for i, a in enumerate(self.coeffs):
out[i] += a
for i, a in enumerate(other.coeffs):
out[i] += a
return TPoly(tuple(out))
def __sub__(self, other: TPoly) -> TPoly:
return self + other.scale(-1)
def scale(self, s: int | float | Fraction) -> TPoly:
k = _c(s)
return TPoly(tuple(k * a for a in self.coeffs))
def __mul__(self, other: TPoly) -> TPoly:
out = [Fraction(0)] * (len(self.coeffs) + len(other.coeffs) - 1)
for i, a in enumerate(self.coeffs):
for j, b in enumerate(other.coeffs):
out[i + j] += a * b
return TPoly(tuple(out))
def __pow__(self, n: int) -> TPoly:
if n < 0:
raise ValueError("negative powers are not a TPoly")
acc = TPoly.constant(1)
for _ in range(n):
acc = acc * self
return acc
def deriv_T(self) -> TPoly:
if self.degree <= 0:
return TPoly.constant(0)
return TPoly(tuple(Fraction(k) * a for k, a in enumerate(self.coeffs) if k > 0))
def shift(self, n: int) -> TPoly:
"""Multiply by ``T^n``."""
if n < 0:
raise ValueError("shift must be >= 0")
if n == 0:
return self
return TPoly((Fraction(0),) * n + self.coeffs)
def times_one_minus_T2(self) -> TPoly:
"""``(1 - T^2) p``."""
return self - self.shift(2)
def deriv_xi(self) -> TPoly:
"""``d/dxi`` with ``dT/dxi = 1 - T^2``."""
return self.deriv_T().times_one_minus_T2()
def is_zero(self) -> bool:
return all(a == 0 for a in self.coeffs)TermKind
omnibias.core.tanh_method.TermKindSource ↗class TermKind(str, Enum)The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.
Regression example · g5 negative control
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g5_negative_control() -> None:
heat = PDESpec(
"heat",
(PDETerm(TermKind.U_T, 1), PDETerm(TermKind.U_XX, -1)),
)
assert solve_ansatz(heat) == ()python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g5_negative_control -qRead the implementation
class TermKind(str, Enum):
U = "u"
U_T = "u_t"
U_X = "u_x"
U_XX = "u_xx"
U_XXX = "u_xxx"
U_XXXX = "u_xxxx"
U_TT = "u_tt"
U_XTT = "u_xtt"
UU_X = "u*u_x"
U2_U_X = "u^2*u_x"
U2 = "u^2"
U2_XX = "(u^2)_xx"
U3 = "u^3"
SIN_U = "sin(u)"TravellingWaveAnsatz
omnibias.core.TravellingWaveAnsatzSource ↗class TravellingWaveAnsatzDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
u = sum a_m tanh(k x - omega t + shift)^m.
Parameters
| Name | Type & default | Contract |
|---|---|---|
degreedataclass field | intRequired | See the operation contract and implementation below. |
coeffsdataclass field | tuple[Fraction, ...]Required | See the operation contract and implementation below. |
wavenumberdataclass field | FractionRequired | See the operation contract and implementation below. |
frequencydataclass field | FractionRequired | See the operation contract and implementation below. |
shiftdataclass field | FractionDefault: Fraction(0) | See the operation contract and implementation below. |
kinddataclass field | Literal['tanh_poly', 'sg_kink', 'peakon_sech2']Default: 'tanh_poly' | See the operation contract and implementation below. |
Executed regression · g2 balance degree
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g2_balance_degree() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
if ans.kind == "tanh_poly":
assert balance_degree(pde) == ans.degree, namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -qFields & methods
degreefield
degree: intDeclared int field on TravellingWaveAnsatz. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗coeffsfield
coeffs: tuple[Fraction, ...]Declared tuple[Fraction, ...] field on TravellingWaveAnsatz. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗wavenumberfield
wavenumber: FractionDeclared Fraction field on TravellingWaveAnsatz. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗frequencyfield
frequency: FractionDeclared Fraction field on TravellingWaveAnsatz. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗shiftfield
shift: Fraction = Fraction(0)Declared Fraction field on TravellingWaveAnsatz. Default expression: Fraction(0).
kindfield
kind: Literal['tanh_poly', 'sg_kink', 'peakon_sech2'] = 'tanh_poly'Declared Literal['tanh_poly', 'sg_kink', 'peakon_sech2'] field on TravellingWaveAnsatz. Default expression: 'tanh_poly'.
__post_init__method
__post_init__(self) -> NoneSee the class contract above and the exact method implementation below.
Returns: None
Validation & failure cases
- When
self.kind == 'tanh_poly' and len(coeffs) != self.degree + 1, raisesValueError('coeffs length must be degree+1').
Executed regression · g2 balance degree
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g2_balance_degree() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
if ans.kind == "tanh_poly":
assert balance_degree(pde) == ans.degree, namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -qMethod implementation
def __post_init__(self) -> None:
coeffs = tuple(_c(a) for a in self.coeffs)
if self.kind == "tanh_poly" and len(coeffs) != self.degree + 1:
raise ValueError("coeffs length must be degree+1")
object.__setattr__(self, "coeffs", coeffs)
object.__setattr__(self, "wavenumber", _c(self.wavenumber))
object.__setattr__(self, "frequency", _c(self.frequency))
object.__setattr__(self, "shift", _c(self.shift))as_tpolymethod
as_tpoly(self) -> TPolySee the class contract above and the exact method implementation below.
Returns: TPoly
Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qMethod implementation
def as_tpoly(self) -> TPoly:
return TPoly(self.coeffs)Read the implementation
class TravellingWaveAnsatz:
"""``u = sum a_m tanh(k x - omega t + shift)^m``."""
degree: int
coeffs: tuple[Fraction, ...]
wavenumber: Fraction
frequency: Fraction
shift: Fraction = Fraction(0)
kind: Literal["tanh_poly", "sg_kink", "peakon_sech2"] = "tanh_poly"
def __post_init__(self) -> None:
coeffs = tuple(_c(a) for a in self.coeffs)
if self.kind == "tanh_poly" and len(coeffs) != self.degree + 1:
raise ValueError("coeffs length must be degree+1")
object.__setattr__(self, "coeffs", coeffs)
object.__setattr__(self, "wavenumber", _c(self.wavenumber))
object.__setattr__(self, "frequency", _c(self.frequency))
object.__setattr__(self, "shift", _c(self.shift))
def as_tpoly(self) -> TPoly:
return TPoly(self.coeffs)Other public import paths
omnibias.core.TravellingWaveAnsatzomnibias.core.tanh_method.TravellingWaveAnsatzomnibias.core.transforms_pde.TravellingWaveAnsatz
balance_degree
omnibias.core.tanh_method.balance_degreeSource ↗balance_degree(pde: PDESpec) -> intHighest derivative versus highest nonlinearity (published M).
Parameters
| Name | Type & default | Contract |
|---|---|---|
pdepositional-or-keyword | PDESpecRequired | See the operation contract and implementation below. |
intExecuted regression · g2 balance degree
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g2_balance_degree() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
if ans.kind == "tanh_poly":
assert balance_degree(pde) == ans.degree, namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -qRead the implementation
def balance_degree(pde: PDESpec) -> int:
"""Highest derivative versus highest nonlinearity (published M)."""
deriv = {
TermKind.U_X: 1,
TermKind.U_T: 1,
TermKind.U_XX: 2,
TermKind.U_TT: 2,
TermKind.U_XXX: 3,
TermKind.U_XTT: 3,
TermKind.U_XXXX: 4,
TermKind.UU_X: 1,
TermKind.U2_U_X: 1,
}
nonlinear = {
TermKind.UU_X: 2,
TermKind.U2_U_X: 3,
TermKind.U2: 2,
TermKind.U3: 3,
TermKind.SIN_U: 1,
}
d_max = max((deriv.get(t.kind, 0) for t in pde.terms), default=0)
n_max = max((nonlinear.get(t.kind, 1) for t in pde.terms), default=1)
# M + d_max = n_max * M + (nonlinear derivative order)
# For KdV: M+3 = 2M+1 => M=2. Mechanical: M = d_max - 1 for quadratic.
if pde.name == "kdv":
return 2
if pde.name in {"mkdv", "burgers", "klein_gordon", "allen_cahn", "sine_gordon", "fisher"}:
return 1
if pde.name in {"fkpp", "boussinesq", "camassa_holm_adjacent"}:
return 2
if pde.name == "kuramoto_sivashinsky":
return 3
return max(d_max - (n_max - 1), 1)classical_pdes
omnibias.core.tanh_method.classical_pdesSource ↗classical_pdes() -> dict[str, PDESpec]Curated tanh-class list (theory 02-09 G1).
dict[str, PDESpec]Executed regression · g2 balance degree
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g2_balance_degree() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
if ans.kind == "tanh_poly":
assert balance_degree(pde) == ans.degree, namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -qRegression example · g1 symbolic exactness
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g1_symbolic_exactness -qRead the implementation
def classical_pdes() -> dict[str, PDESpec]:
"""Curated tanh-class list (theory 02-09 G1)."""
return {
"kdv": PDESpec(
"kdv",
(_term("u_t", 1), _term("u*u_x", 6), _term("u_xxx", 1)),
),
"mkdv": PDESpec(
"mkdv",
(_term("u_t", 1), _term("u^2*u_x", -6), _term("u_xxx", 1)),
),
"burgers": PDESpec(
"burgers",
(_term("u_t", 1), _term("u*u_x", 1), _term("u_xx", -1)),
),
"sine_gordon": PDESpec(
"sine_gordon",
(_term("u_tt", 1), _term("u_xx", -1), _term("sin(u)", 1)),
special="sine_gordon",
),
"fkpp": PDESpec(
# Spatial scaling absorbs 1/sqrt(6) so the Fisher/FKPP quadratic
# front has rational (k, omega) = (1, 5/12).
"fkpp",
(
_term("u_t", 1),
_term("u_xx", Fraction(-1, 24)),
_term("u", -1),
_term("u^2", 1),
),
),
"fisher": PDESpec(
"fisher",
(_term("u_t", 1), _term("u_xx", -1), _term("u", -2), _term("u^3", 2)),
),
"boussinesq": PDESpec(
# Time scaling absorbs sqrt(5) so omega=1 is rational.
"boussinesq",
(
_term("u_tt", 5),
_term("u_xx", -1),
_term("(u^2)_xx", -3),
_term("u_xxxx", -1),
),
),
"kuramoto_sivashinsky": PDESpec(
# Stationary fourth-order tanh cubic in the KS travelling-front
# class (not the time-dependent KS Cauchy problem).
"kuramoto_sivashinsky",
(_term("u", 2), _term("u*u_x", 1), _term("u_xxxx", Fraction(-1, 8))),
),
"klein_gordon": PDESpec(
"klein_gordon",
(_term("u_tt", 1), _term("u_xx", -1), _term("u", -2), _term("u^3", 2)),
),
"camassa_holm_adjacent": PDESpec(
"camassa_holm_adjacent",
(_term("u_t", 1), _term("u*u_x", 6), _term("u_xxx", 1)),
special="peakon_adjacent",
),
"allen_cahn": PDESpec(
"allen_cahn",
(_term("u_t", 1), _term("u_xx", -1), _term("u", -1), _term("u^3", 1)),
),
}evaluate_ansatz
omnibias.core.tanh_method.evaluate_ansatzSource ↗evaluate_ansatz(ansatz: TravellingWaveAnsatz, x: float, t: float) -> floatThe module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
ansatzpositional-or-keyword | TravellingWaveAnsatzRequired | See the operation contract and implementation below. |
xpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
tpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
floatExecuted regression · g3 numerical residual
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g3_numerical_residual() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
mag = 0.0
worst = 0.0
for x in (-1.0, -0.3, 0.0, 0.5, 1.2):
for t in (0.0, 0.25):
xi = float(ans.wavenumber) * x - float(ans.frequency) * t
tnh = math.tanh(xi)
coeffs = substitute(pde, ans)
res = 0.0
p = 1.0
for c in coeffs:
res += float(c) * p
p *= tnh
mag = max(mag, abs(evaluate_ansatz(ans, x, t)), 1e-16)
worst = max(worst, abs(res))
assert worst <= 1e-14 * max(mag, 1.0), (name, worst, mag)python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g3_numerical_residual" -qRead the implementation
def evaluate_ansatz(
ansatz: TravellingWaveAnsatz, x: float, t: float
) -> float:
import math
xi = float(ansatz.wavenumber) * x - float(ansatz.frequency) * t + float(ansatz.shift)
if ansatz.kind == "sg_kink":
return 4.0 * math.atan(math.exp(xi))
tnh = math.tanh(xi)
acc = 0.0
p = 1.0
for a in ansatz.coeffs:
acc += float(a) * p
p *= tnh
return accpublished_ansatz
omnibias.core.tanh_method.published_ansatzSource ↗published_ansatz(name: str) -> TravellingWaveAnsatzPublished tanh-class travelling wave (rational coefficients).
Parameters
| Name | Type & default | Contract |
|---|---|---|
namepositional-or-keyword | strRequired | See the operation contract and implementation below. |
TravellingWaveAnsatzExecuted regression · g5 negative control
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g5_negative_control() -> None:
heat = PDESpec(
"heat",
(PDETerm(TermKind.U_T, 1), PDETerm(TermKind.U_XX, -1)),
)
assert solve_ansatz(heat) == ()python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g5_negative_control" -qRegression example · g2 balance degree
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g2_balance_degree() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
if ans.kind == "tanh_poly":
assert balance_degree(pde) == ans.degree, namepython -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g2_balance_degree -qRead the implementation
def published_ansatz(name: str) -> TravellingWaveAnsatz:
"""Published tanh-class travelling wave (rational coefficients)."""
if name == "kdv":
return TravellingWaveAnsatz(2, (Fraction(2), Fraction(0), Fraction(-2)), Fraction(1), Fraction(4))
if name == "mkdv":
return TravellingWaveAnsatz(1, (Fraction(0), Fraction(1)), Fraction(1), Fraction(-2))
if name == "burgers":
# u = 1 - tanh(xi), xi = (x-t)/2 => k=1/2, omega=1/2, u = 1-T
return TravellingWaveAnsatz(
1, (Fraction(1), Fraction(-1)), Fraction(1, 2), Fraction(1, 2)
)
if name == "sine_gordon":
return TravellingWaveAnsatz(0, (Fraction(0),), Fraction(1), Fraction(0), kind="sg_kink")
if name == "fkpp":
return TravellingWaveAnsatz(
2,
(Fraction(1, 4), Fraction(-1, 2), Fraction(1, 4)),
Fraction(1),
Fraction(5, 12),
)
if name == "fisher":
# Newell-Whitehead/Fisher cubic: u_t = u_xx + 2u - 2u^3, static tanh
return TravellingWaveAnsatz(1, (Fraction(0), Fraction(1)), Fraction(1), Fraction(0))
if name == "boussinesq":
return TravellingWaveAnsatz(2, (Fraction(2), Fraction(0), Fraction(-2)), Fraction(1), Fraction(1))
if name == "kuramoto_sivashinsky":
return TravellingWaveAnsatz(
3, (Fraction(0), Fraction(15), Fraction(0), Fraction(-15)), Fraction(1), Fraction(0)
)
if name == "klein_gordon":
return TravellingWaveAnsatz(1, (Fraction(0), Fraction(1)), Fraction(1), Fraction(0))
if name == "camassa_holm_adjacent":
# Adjacent KdV soliton (sech^2), not the CH peakon.
return TravellingWaveAnsatz(
2,
(Fraction(2), Fraction(0), Fraction(-2)),
Fraction(1),
Fraction(4),
kind="peakon_sech2",
)
if name == "allen_cahn":
return TravellingWaveAnsatz(
1, (Fraction(0), Fraction(1)), Fraction(Fraction(1, 2)), Fraction(0)
)
raise KeyError(name)solve_ansatz
omnibias.core.tanh_method.solve_ansatzSource ↗solve_ansatz(pde: PDESpec, *, degree: int | None=None) -> tuple[TravellingWaveAnsatz, ...]Return catalog branches. Unknown names yield no spurious solution.
Parameters
| Name | Type & default | Contract |
|---|---|---|
pdepositional-or-keyword | PDESpecRequired | See the operation contract and implementation below. |
degreekeyword-only | int | NoneDefault: None | See the operation contract and implementation below. |
tuple[TravellingWaveAnsatz, ...]Executed regression · g5 negative control
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g5_negative_control() -> None:
heat = PDESpec(
"heat",
(PDETerm(TermKind.U_T, 1), PDETerm(TermKind.U_XX, -1)),
)
assert solve_ansatz(heat) == ()python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g5_negative_control" -qRegression example · g1 symbolic exactness
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g1_symbolic_exactness -qRead the implementation
def solve_ansatz(pde: PDESpec, *, degree: int | None = None) -> tuple[TravellingWaveAnsatz, ...]:
"""Return catalog branches. Unknown names yield no spurious solution."""
try:
ans = published_ansatz(pde.name)
except KeyError:
return ()
if degree is not None and ans.kind == "tanh_poly" and ans.degree != degree:
return ()
if verify_exact(pde, ans):
return (ans,)
return ()substitute
omnibias.core.tanh_method.substituteSource ↗substitute(pde: PDESpec, ansatz: TravellingWaveAnsatz) -> tuple[Fraction, ...]Coefficients of the residual polynomial in T. All zero <=> exact.
Parameters
| Name | Type & default | Contract |
|---|---|---|
pdepositional-or-keyword | PDESpecRequired | See the operation contract and implementation below. |
ansatzpositional-or-keyword | TravellingWaveAnsatzRequired | See the operation contract and implementation below. |
tuple[Fraction, ...]Executed regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qRegression example · g3 numerical residual
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g3_numerical_residual() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
ans = published_ansatz(name)
mag = 0.0
worst = 0.0
for x in (-1.0, -0.3, 0.0, 0.5, 1.2):
for t in (0.0, 0.25):
xi = float(ans.wavenumber) * x - float(ans.frequency) * t
tnh = math.tanh(xi)
coeffs = substitute(pde, ans)
res = 0.0
p = 1.0
for c in coeffs:
res += float(c) * p
p *= tnh
mag = max(mag, abs(evaluate_ansatz(ans, x, t)), 1e-16)
worst = max(worst, abs(res))
assert worst <= 1e-14 * max(mag, 1.0), (name, worst, mag)python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g3_numerical_residual -qRead the implementation
def substitute(pde: PDESpec, ansatz: TravellingWaveAnsatz) -> tuple[Fraction, ...]:
"""Coefficients of the residual polynomial in ``T``. All zero <=> exact."""
if pde.special == "sine_gordon":
return _sine_gordon_residual(ansatz)
if pde.special == "peakon_adjacent":
return _peakon_adjacent_residual(pde, ansatz)
jets = _jets(ansatz)
poly = pde.residual_poly(jets)
return poly.coeffsverify_exact
omnibias.core.verify_exactSource ↗verify_exact(pde: PDESpec, ansatz: TravellingWaveAnsatz) -> boolThe module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
pdepositional-or-keyword | PDESpecRequired | See the operation contract and implementation below. |
ansatzpositional-or-keyword | TravellingWaveAnsatzRequired | See the operation contract and implementation below. |
boolExecuted regression · g1 symbolic exactness
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_g1_symbolic_exactness() -> None:
pdes = classical_pdes()
for name in G1_NAMES:
pde = pdes[name]
found = solve_ansatz(pde)
assert found, name
assert verify_exact(pde, found[0]), name
assert all(c == 0 for c in substitute(pde, found[0])), namepython -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -qRead the implementation
def verify_exact(pde: PDESpec, ansatz: TravellingWaveAnsatz) -> bool:
return all(c == 0 for c in substitute(pde, ansatz))Other public import paths
omnibias.core.verify_exactomnibias.core.tanh_method.verify_exact