-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add LU decomposition with partial pivoting #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9965d48
Initial plan
Copilot f6f3c44
Add LU decomposition implementation with comprehensive tests
Copilot a47d439
Address code review comments: fix documentation and remove debug output
Copilot c68aa1e
Merge branch 'main' into copilot/add-lu-decomposition-factorization
Quafadas c81540c
Merge branch 'main' into copilot/add-lu-decomposition-factorization
Quafadas 46fc641
Merge branch 'main' into copilot/add-lu-decomposition-factorization
Quafadas b29537e
Apply suggestions from code review
Quafadas 87a235b
Merge branch 'main' into copilot/add-lu-decomposition-factorization
Quafadas 6129e88
.
Quafadas f39d49b
.
Quafadas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package vecxt | ||
|
|
||
| import vecxt.matrix.Matrix | ||
|
|
||
| /** LU decomposition placeholder for JS and Native platforms. | ||
| * | ||
| * Decomposes a matrix A into the product P*A = L*U where: | ||
| * - P is a permutation matrix | ||
| * - L is a lower triangular matrix with unit diagonal | ||
| * - U is an upper triangular matrix | ||
| */ | ||
| object LU: | ||
|
|
||
| /** Computes the LU decomposition with partial pivoting of a matrix. | ||
| * | ||
| * @param m | ||
| * The input matrix to decompose. | ||
| * @return | ||
| * A named tuple containing: | ||
| * - L: Lower triangular matrix with unit diagonal | ||
| * - U: Upper triangular matrix | ||
| * - P: Permutation array | ||
| */ | ||
| inline def lu(m: Matrix[Double]): (L: Matrix[Double], U: Matrix[Double], P: Array[Int]) = ??? | ||
|
|
||
| end LU | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package vecxt | ||
|
|
||
| import dev.ludovic.netlib.lapack.JavaLAPACK | ||
| import org.netlib.util.intW | ||
| import vecxt.matrix.Matrix | ||
| import vecxt.MatrixInstance.* | ||
| import vecxt.MatrixHelper.zeros | ||
| import vecxt.BoundsCheck.BoundsCheck | ||
|
|
||
| /** LU decomposition with partial pivoting using LAPACK. | ||
| * | ||
| * Decomposes a matrix A into the product P*A = L*U where: | ||
| * - P is a permutation matrix | ||
| * - L is a lower triangular matrix with unit diagonal | ||
| * - U is an upper triangular matrix | ||
| */ | ||
| object LU: | ||
| private lazy final val lapack = JavaLAPACK.getInstance() | ||
|
|
||
| /** Computes the LU decomposition with partial pivoting of a matrix. | ||
| * | ||
| * Uses LAPACK's dgetrf routine to compute the factorization P*A = L*U where: | ||
| * - P is a permutation matrix (represented as a pivot array) | ||
| * - L is a lower triangular matrix with ones on the diagonal | ||
| * - U is an upper triangular matrix | ||
| * | ||
| * The decomposition allows efficient solving of linear systems and computation of determinants. | ||
| * | ||
| * Note: The factorization is computed even for singular matrices. In such cases, at least one diagonal element of U | ||
| * will be zero (or near zero), indicating the matrix is rank-deficient. | ||
| * | ||
| * @param m | ||
| * The input matrix to decompose. Can be square or rectangular (m x n). | ||
| * @return | ||
| * A named tuple containing: | ||
| * - L: Lower triangular matrix with unit diagonal (rows × min(rows,cols)) | ||
| * - U: Upper triangular matrix (min(rows,cols) × cols) | ||
| * - P: Permutation array representing successive row interchanges. At step i, row i was swapped with row P(i). The | ||
| * permutation matrix can be reconstructed by applying these swaps in order. | ||
| * @throws IllegalArgumentException | ||
| * if matrix is empty or if an argument to LAPACK is invalid | ||
| */ | ||
| inline def lu(m: Matrix[Double])(using | ||
| inline bc: BoundsCheck | ||
| ): (L: Matrix[Double], U: Matrix[Double], P: Array[Int]) = | ||
| nonEmptyMatCheck(m) | ||
|
|
||
| val rows = m.rows | ||
| val cols = m.cols | ||
| val minDim = math.min(rows, cols) | ||
|
|
||
| // Copy input matrix as LAPACK will overwrite it | ||
| val A = m.deepCopy | ||
|
|
||
| // Allocate pivot array - LAPACK uses 1-based indexing | ||
| val ipiv = Array.ofDim[Int](minDim) | ||
|
|
||
| val info = new intW(0) | ||
|
|
||
| // Compute LU factorization | ||
| lapack.dgetrf( | ||
| rows, // M: number of rows | ||
| cols, // N: number of columns | ||
| A.raw, // A: matrix to factor (overwritten with L and U) | ||
| math.max(1, rows), // LDA: leading dimension | ||
| ipiv, // IPIV: pivot indices | ||
| info // INFO: status | ||
| ) | ||
|
|
||
| // Check for errors | ||
| if info.`val` < 0 then | ||
| throw new IllegalArgumentException(s"LU decomposition failed: argument ${-info.`val`} had an illegal value") | ||
| end if | ||
|
|
||
| // Note: info.val > 0 means U(info.val, info.val) is exactly zero | ||
| // The factorization has been completed, but U is singular | ||
| // We allow this and let the caller handle it | ||
|
|
||
| // Extract L and U from the factored matrix | ||
| // LAPACK stores L and U in the same matrix: | ||
| // - U is in the upper triangle (including diagonal) | ||
| // - L is in the lower triangle (diagonal is implicitly 1) | ||
|
|
||
| val L = Matrix.zeros[Double](rows, minDim) | ||
| val U = Matrix.zeros[Double](minDim, cols) | ||
|
|
||
| // Extract L (lower triangle with unit diagonal) | ||
| var i = 0 | ||
| while i < rows do | ||
| var j = 0 | ||
| while j < minDim do | ||
| if i > j then | ||
| // Below diagonal: copy from A | ||
| L(i, j) = A(i, j) | ||
| else if i == j then | ||
| // On diagonal: set to 1 | ||
| L(i, j) = 1.0 | ||
| // else: above diagonal is 0 (already initialized) | ||
| end if | ||
| j += 1 | ||
| end while | ||
| i += 1 | ||
| end while | ||
|
|
||
| // Extract U (upper triangle) | ||
| i = 0 | ||
| while i < minDim do | ||
| var j = 0 | ||
| while j < cols do | ||
| if i <= j then | ||
| // On or above diagonal: copy from A | ||
| U(i, j) = A(i, j) | ||
| // else: below diagonal is 0 (already initialized) | ||
| end if | ||
| j += 1 | ||
| end while | ||
| i += 1 | ||
| end while | ||
|
|
||
| // Convert LAPACK's 1-based pivot indices to 0-based | ||
| val pivotArray = Array.ofDim[Int](minDim) | ||
| i = 0 | ||
| while i < minDim do | ||
| pivotArray(i) = ipiv(i) - 1 // Convert from 1-based to 0-based | ||
| i += 1 | ||
| end while | ||
|
|
||
| (L = L, U = U, P = pivotArray) | ||
| end lu | ||
|
|
||
| end LU |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The placeholder signature is missing the
BoundsCheckparameter that the JVM implementation has. To maintain consistency with other decomposition functions (e.g.,eig,solve), add the bounds check parameter: