Skip to content

Make LuFactorization a stateful class outside detail#815

Draft
tigercosmos wants to merge 1 commit into
solvcon:masterfrom
tigercosmos:issue809
Draft

Make LuFactorization a stateful class outside detail#815
tigercosmos wants to merge 1 commit into
solvcon:masterfrom
tigercosmos:issue809

Conversation

@tigercosmos
Copy link
Copy Markdown
Collaborator

@tigercosmos tigercosmos commented May 23, 2026

This refactors the LU-with-partial-pivoting helper from a namespace of static methods (modmesh::detail::Lu) into a stateful class modmesh::LuFactorization. The class computes the factorization once in its constructor and caches the resulting LU matrix and pivot vector as members; solve() and inv() are const methods that reuse those cached factors instead of re-running the O(n^3) algorithm. Owners of an LuFactorization instance decide how the cached data is used.

The existing free functions lu_factorization(), lu_solve(), and lu_inv() are preserved as thin wrappers that construct a temporary LuFactorization internally, so callers of the original API keep working unchanged. The class itself is exposed to Python as LuFactorizationFloat32/Float64/Complex64/Complex128 via a new wrap_LuFactorization.cpp, mirroring the EigenSystem binding pattern.

Tests in tests/test_linalg.py exercise the class through its lu, piv, n, solve, and inv members. Two of them mutate the cached lu between calls and assert the result changes, which would catch a hidden re-factorization in solve() or inv(). The same shape, rank, dimension, and singularity error paths that the free functions cover are re-covered through the class API.

Related to #809.

Pull the LU-with-partial-pivoting helper out of modmesh::detail and
turn it from a namespace of static methods into a stateful class
modmesh::LuFactorization<T>. The constructor runs the factorization
once and caches the lu matrix and pivot vector; solve() and inv() are
const methods that read those cached members. Free functions
lu_factorization(), lu_solve(), and lu_inv() remain as thin wrappers
that construct a temporary LuFactorization<T>, so the existing API
keeps working.

The class is exposed to Python as LuFactorizationFloat32/Float64/
Complex64/Complex128 via a new wrap_LuFactorization.cpp, modelled on
wrap_EigenSystem.cpp.

Related to solvcon#809.
@yungyuc
Copy link
Copy Markdown
Member

yungyuc commented May 24, 2026

Determinant may be a side product of the LU decomposition. Consider to add it along with the refactor work, or leave room to extend to it.

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.

2 participants