Skip to content

fix: dispatch PyLinearSolver::solve to the Python solve method - #95

Merged
oberbichler merged 1 commit into
masterfrom
fix/linear-solver-trampoline
Jul 26, 2026
Merged

fix: dispatch PyLinearSolver::solve to the Python solve method#95
oberbichler merged 1 commit into
masterfrom
fix/linear-solver-trampoline

Conversation

@oberbichler

Copy link
Copy Markdown
Owner

Problem

The solve override in the PyLinearSolver trampoline dispatched to the wrong Python method:

virtual bool solve(...) override {
    pybind11::gil_scoped_acquire acquire;
    PYBIND11_OVERRIDE_PURE(bool, T, factorize, ia, ja, a, b, x);  // names 'factorize', not 'solve'
}

When Problem::hm_inv_v calls solve(ia, ja, a, b, x) on a LinearSolver subclassed in Python, the trampoline invoked the Python factorize method (with 5 arguments) instead of the user's solve. A Python-defined linear solver therefore never had its solve executed.

Fix

Name solve in the macro, so the override dispatches to the Python solve.

Test

New tests/test_linear_solver_trampoline.py subclasses LinearSolver in Python (recording how it is called and applying a trivial x = b + 1), assigns it as problem.linear_solver, and calls problem.hm_inv_v. It asserts that solve is dispatched exactly once with the correct right-hand side, that factorize is only ever called with its 3-argument signature, and that the x written by solve flows back to the caller.

  • Before the fix: solve is never called (factorize is invoked in its place); test fails.
  • After the fix: passes; full suite 44 passed.

The solve override in the PyLinearSolver trampoline used
PYBIND11_OVERRIDE_PURE(bool, T, factorize, ia, ja, a, b, x), naming
'factorize' instead of 'solve'. When Problem::hm_inv_v called solve on a
LinearSolver subclassed in Python, the trampoline invoked the Python
'factorize' method (with 5 arguments) and never the user's 'solve', so
Python-defined linear solvers did not work.

Dispatch to 'solve'. Add a regression test that subclasses LinearSolver
in Python and asserts, via Problem.hm_inv_v, that solve is invoked with
the correct right-hand side and its result flows back.
@oberbichler
oberbichler merged commit b5c6f97 into master Jul 26, 2026
10 checks passed
@oberbichler
oberbichler deleted the fix/linear-solver-trampoline branch July 26, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant