Skip to main content

omnibias.core.tanh_method

0.5.0rc1Release candidatePython >=3.10Apache-2.0

Coefficient recurrences, activation contracts, jet algebra, and verified arithmetic shared by the numerical backends.

import omnibias.core.tanh_method
Module 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.

CONSTANT

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, name
python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g2_balance_degree -q
Regression 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])), name
python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g1_symbolic_exactness -q
Read the implementation
G1_NAMES: tuple[str, ...] = (
    "kdv",
    "mkdv",
    "burgers",
    "sine_gordon",
    "fkpp",
    "fisher",
    "boussinesq",
    "kuramoto_sivashinsky",
    "klein_gordon",
    "camassa_holm_adjacent",
)
CLASS

PDESpec​

omnibias.core.tanh_method.PDESpecSource ↗
class PDESpec

Dataclass 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

NameType & defaultContract
namedataclass fieldstrRequiredSee the operation contract and implementation below.
termsdataclass fieldtuple[PDETerm, ...]RequiredSee the operation contract and implementation below.
specialdataclass fieldLiteral['', '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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
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 -q

Fields & methods

namefield
name: str

Declared 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: ''.

View source ↗
residual_polymethod
residual_poly(self, jets: Mapping[str, TPoly]) -> TPoly

See the class contract above and the exact method implementation below.

Parameters

NameType & defaultContract
jetspositional-or-keywordMapping[str, TPoly]RequiredSee 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method 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 acc
View source ↗
Read 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 acc
CLASS

PDETerm​

omnibias.core.tanh_method.PDETermSource ↗
class PDETerm

Dataclass 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

NameType & defaultContract
kinddataclass fieldTermKindRequiredSee the operation contract and implementation below.
coeffdataclass fieldFractionRequiredSee 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 -q

Fields & methods

kindfield
kind: TermKind

Declared TermKind field on PDETerm. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.

View source ↗
coefffield
coeff: Fraction

Declared 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: Fraction
CLASS

TPoly​

omnibias.core.tanh_method.TPolySource ↗
class TPoly

Dataclass 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

NameType & defaultContract
coeffsdataclass fieldtuple[Fraction, ...]RequiredSee 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q

Fields & 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) -> None

See 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def __post_init__(self) -> None:
        object.__setattr__(self, "coeffs", _trim(tuple(_c(a) for a in self.coeffs)))
View source ↗
constantmethod
constant(cls, a: int | float | Fraction) -> TPoly

See the class contract above and the exact method implementation below.

Parameters

NameType & defaultContract
apositional-or-keywordint | float | FractionRequiredSee 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def constant(cls, a: int | float | Fraction) -> TPoly:
        return cls((_c(a),))
View source ↗
tmethod
t(cls) -> TPoly

See the class contract above and the exact method implementation below.

Returns: TPoly

Method implementation
    def t(cls) -> TPoly:
        return cls((Fraction(0), Fraction(1)))
View source ↗
degreeproperty
degree(self) -> int

See 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def degree(self) -> int:
        return len(self.coeffs) - 1
View source ↗
__add__method
__add__(self, other: TPoly) -> TPoly

See the class contract above and the exact method implementation below.

Parameters

NameType & defaultContract
otherpositional-or-keywordTPolyRequiredSee 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method 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))
View source ↗
__sub__method
__sub__(self, other: TPoly) -> TPoly

See the class contract above and the exact method implementation below.

Parameters

NameType & defaultContract
otherpositional-or-keywordTPolyRequiredSee 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def __sub__(self, other: TPoly) -> TPoly:
        return self + other.scale(-1)
View source ↗
scalemethod
scale(self, s: int | float | Fraction) -> TPoly

See the class contract above and the exact method implementation below.

Parameters

NameType & defaultContract
spositional-or-keywordint | float | FractionRequiredSee 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def scale(self, s: int | float | Fraction) -> TPoly:
        k = _c(s)
        return TPoly(tuple(k * a for a in self.coeffs))
View source ↗
__mul__method
__mul__(self, other: TPoly) -> TPoly

See the class contract above and the exact method implementation below.

Parameters

NameType & defaultContract
otherpositional-or-keywordTPolyRequiredSee 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method 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))
View source ↗
__pow__method
__pow__(self, n: int) -> TPoly

See the class contract above and the exact method implementation below.

Parameters

NameType & defaultContract
npositional-or-keywordintRequiredSee the operation contract and implementation below.

Returns: TPoly

Validation & failure cases
  • When n < 0, raises ValueError('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 acc
View source ↗
deriv_Tmethod
deriv_T(self) -> TPoly

See 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method 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))
View source ↗
shiftmethod
shift(self, n: int) -> TPoly

Multiply by T^n.

Parameters

NameType & defaultContract
npositional-or-keywordintRequiredSee the operation contract and implementation below.

Returns: TPoly

Validation & failure cases
  • When n < 0, raises ValueError('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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method 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)
View source ↗
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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def times_one_minus_T2(self) -> TPoly:
        """``(1 - T^2) p``."""
        return self - self.shift(2)
View source ↗
deriv_ximethod
deriv_xi(self) -> TPoly

d/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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def deriv_xi(self) -> TPoly:
        """``d/dxi`` with ``dT/dxi = 1 - T^2``."""
        return self.deriv_T().times_one_minus_T2()
View source ↗
is_zeromethod
is_zero(self) -> bool

See 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)
View source ↗
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)
CLASS

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 -q
Read 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)"
CLASS

TravellingWaveAnsatz​

omnibias.core.TravellingWaveAnsatzSource ↗
class TravellingWaveAnsatz

Dataclass 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

NameType & defaultContract
degreedataclass fieldintRequiredSee the operation contract and implementation below.
coeffsdataclass fieldtuple[Fraction, ...]RequiredSee the operation contract and implementation below.
wavenumberdataclass fieldFractionRequiredSee the operation contract and implementation below.
frequencydataclass fieldFractionRequiredSee the operation contract and implementation below.
shiftdataclass fieldFractionDefault: Fraction(0)See the operation contract and implementation below.
kinddataclass fieldLiteral['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, name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -q

Fields & methods

degreefield
degree: int

Declared 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: Fraction

Declared Fraction field on TravellingWaveAnsatz. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.

View source ↗
frequencyfield
frequency: Fraction

Declared 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).

View source ↗
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'.

View source ↗
__post_init__method
__post_init__(self) -> None

See 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, raises ValueError('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, name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -q
Method 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))
View source ↗
as_tpolymethod
as_tpoly(self) -> TPoly

See 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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Method implementation
    def as_tpoly(self) -> TPoly:
        return TPoly(self.coeffs)
View source ↗
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.TravellingWaveAnsatz
  • omnibias.core.tanh_method.TravellingWaveAnsatz
  • omnibias.core.transforms_pde.TravellingWaveAnsatz
FUNCTION

balance_degree​

omnibias.core.tanh_method.balance_degreeSource ↗
balance_degree(pde: PDESpec) -> int

Highest derivative versus highest nonlinearity (published M).

Parameters

NameType & defaultContract
pdepositional-or-keywordPDESpecRequiredSee the operation contract and implementation below.
Returnsint
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, name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -q
Read 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)
FUNCTION

classical_pdes​

omnibias.core.tanh_method.classical_pdesSource ↗
classical_pdes() -> dict[str, PDESpec]

Curated tanh-class list (theory 02-09 G1).

Returnsdict[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, name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g2_balance_degree" -q
Regression 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])), name
python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g1_symbolic_exactness -q
Read 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)),
        ),
    }
FUNCTION

evaluate_ansatz​

omnibias.core.tanh_method.evaluate_ansatzSource ↗
evaluate_ansatz(ansatz: TravellingWaveAnsatz, x: float, t: float) -> float

The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.

Parameters

NameType & defaultContract
ansatzpositional-or-keywordTravellingWaveAnsatzRequiredSee the operation contract and implementation below.
xpositional-or-keywordfloatRequiredSee the operation contract and implementation below.
tpositional-or-keywordfloatRequiredSee the operation contract and implementation below.
Returnsfloat
Executed 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" -q
Read 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 acc
FUNCTION

published_ansatz​

omnibias.core.tanh_method.published_ansatzSource ↗
published_ansatz(name: str) -> TravellingWaveAnsatz

Published tanh-class travelling wave (rational coefficients).

Parameters

NameType & defaultContract
namepositional-or-keywordstrRequiredSee the operation contract and implementation below.
ReturnsTravellingWaveAnsatz
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" -q
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, name
python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g2_balance_degree -q
Read 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)
FUNCTION

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

NameType & defaultContract
pdepositional-or-keywordPDESpecRequiredSee the operation contract and implementation below.
degreekeyword-onlyint | NoneDefault: NoneSee the operation contract and implementation below.
Returnstuple[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" -q
Regression 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])), name
python -m pytest packages/omnibias-core/tests/test_tanh_method.py -k test_g1_symbolic_exactness -q
Read 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 ()
FUNCTION

substitute​

omnibias.core.tanh_method.substituteSource ↗
substitute(pde: PDESpec, ansatz: TravellingWaveAnsatz) -> tuple[Fraction, ...]

Coefficients of the residual polynomial in T. All zero <=> exact.

Parameters

NameType & defaultContract
pdepositional-or-keywordPDESpecRequiredSee the operation contract and implementation below.
ansatzpositional-or-keywordTravellingWaveAnsatzRequiredSee the operation contract and implementation below.
Returnstuple[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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Regression 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 -q
Read 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.coeffs
FUNCTION

verify_exact​

omnibias.core.verify_exactSource ↗
verify_exact(pde: PDESpec, ansatz: TravellingWaveAnsatz) -> bool

The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.

Parameters

NameType & defaultContract
pdepositional-or-keywordPDESpecRequiredSee the operation contract and implementation below.
ansatzpositional-or-keywordTravellingWaveAnsatzRequiredSee the operation contract and implementation below.
Returnsbool
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])), name
python -m pytest "packages/omnibias-core/tests/test_tanh_method.py::test_g1_symbolic_exactness" -q
Read 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_exact
  • omnibias.core.tanh_method.verify_exact