-
Notifications
You must be signed in to change notification settings - Fork 216
Solving a linear system with a symmetric positive definite matrix #1093
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
Open
aamrindersingh
wants to merge
7
commits into
fortran-lang:master
Choose a base branch
from
aamrindersingh:1069-solve-chol
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+984
−6
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fbfe4dc
fix: rebase onto v0.8.0, resolve merge conflicts
aamrindersingh 840d0ee
Update doc/specs/stdlib_linalg.md
aamrindersingh a90185b
test: add upper triangular tests for solve_chol
aamrindersingh 921ba4b
refactor: use uplo/optval, add error tests
aamrindersingh f006e13
Set LINALG_SUCCESS in handle_potrs_info and handle_posv_info
aamrindersingh ecb81de
Refactor Cholesky API: solve_chol (one-shot), solve_lower_chol, solve…
aamrindersingh da8c1df
Refactor: DRY driver for solve_lower/upper_chol, rename examples to m…
aamrindersingh 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
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,27 @@ | ||
| program example_solve_chol | ||
| use stdlib_linalg_constants, only: dp | ||
| use stdlib_linalg, only: solve_chol, linalg_state_type | ||
| implicit none | ||
|
|
||
| real(dp) :: A(3,3), b(3), x(3) | ||
| type(linalg_state_type) :: state | ||
|
|
||
| ! Symmetric positive definite matrix | ||
| A(1,:) = [4.0_dp, 2.0_dp, 2.0_dp] | ||
| A(2,:) = [2.0_dp, 5.0_dp, 1.0_dp] | ||
| A(3,:) = [2.0_dp, 1.0_dp, 6.0_dp] | ||
|
|
||
| ! Right-hand side | ||
| b = [1.0_dp, 2.0_dp, 3.0_dp] | ||
|
|
||
| ! One-shot Cholesky factorization and solve (A is preserved by default) | ||
| call solve_chol(A, b, x, lower=.true., err=state) | ||
| if (state%error()) error stop state%print() | ||
|
|
||
| print '("Solution: ",*(f8.4,1x))', x | ||
|
|
||
| ! For performance-critical code, use overwrite_a=.true. | ||
| ! to avoid internal allocation (but A will be destroyed) | ||
| ! call solve_chol(A, b, x, lower=.true., overwrite_a=.true., err=state) | ||
|
|
||
| end program example_solve_chol |
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,30 @@ | ||
| program example_solve_lower_chol | ||
| use stdlib_linalg_constants, only: dp | ||
| use stdlib_linalg, only: cholesky, solve_lower_chol, linalg_state_type | ||
| implicit none | ||
|
|
||
| real(dp) :: A(3,3), L(3,3), b1(3), b2(3), x(3) | ||
| type(linalg_state_type) :: state | ||
|
|
||
| ! Symmetric positive definite matrix | ||
| A(1,:) = [4.0_dp, 2.0_dp, 2.0_dp] | ||
| A(2,:) = [2.0_dp, 5.0_dp, 1.0_dp] | ||
| A(3,:) = [2.0_dp, 1.0_dp, 6.0_dp] | ||
|
|
||
| ! Compute lower Cholesky factorization once: A = L * L^T | ||
| call cholesky(A, L, lower=.true., err=state) | ||
| if (state%error()) error stop state%print() | ||
|
|
||
| ! First right-hand side | ||
| b1 = [1.0_dp, 2.0_dp, 3.0_dp] | ||
| call solve_lower_chol(L, b1, x, err=state) | ||
| if (state%error()) error stop state%print() | ||
| print '("Solution 1: ",*(f8.4,1x))', x | ||
|
|
||
| ! Second right-hand side (reusing the same factorization) | ||
| b2 = [4.0_dp, 5.0_dp, 6.0_dp] | ||
| call solve_lower_chol(L, b2, x, err=state) | ||
| if (state%error()) error stop state%print() | ||
| print '("Solution 2: ",*(f8.4,1x))', x | ||
|
|
||
| end program example_solve_lower_chol |
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,30 @@ | ||
| program example_solve_upper_chol | ||
| use stdlib_linalg_constants, only: dp | ||
| use stdlib_linalg, only: cholesky, solve_upper_chol, linalg_state_type | ||
| implicit none | ||
|
|
||
| real(dp) :: A(3,3), U(3,3), b1(3), b2(3), x(3) | ||
| type(linalg_state_type) :: state | ||
|
|
||
| ! Symmetric positive definite matrix | ||
| A(1,:) = [4.0_dp, 2.0_dp, 2.0_dp] | ||
| A(2,:) = [2.0_dp, 5.0_dp, 1.0_dp] | ||
| A(3,:) = [2.0_dp, 1.0_dp, 6.0_dp] | ||
|
|
||
| ! Compute upper Cholesky factorization once: A = U^T * U | ||
| call cholesky(A, U, lower=.false., err=state) | ||
| if (state%error()) error stop state%print() | ||
|
|
||
| ! First right-hand side | ||
| b1 = [1.0_dp, 2.0_dp, 3.0_dp] | ||
| call solve_upper_chol(U, b1, x, err=state) | ||
| if (state%error()) error stop state%print() | ||
| print '("Solution 1: ",*(f8.4,1x))', x | ||
|
|
||
| ! Second right-hand side (reusing the same factorization) | ||
| b2 = [4.0_dp, 5.0_dp, 6.0_dp] | ||
| call solve_upper_chol(U, b2, x, err=state) | ||
| if (state%error()) error stop state%print() | ||
| print '("Solution 2: ",*(f8.4,1x))', x | ||
|
|
||
| end program example_solve_upper_chol |
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.
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.
Can you rename the file to
example_solve_lower_cholto match the function's name?And I guess you would need to write another version
example_solve_upper_cholas well.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.
@loiseaujc
Done, Added a private driver that both solve_lower_chol and solve_upper_chol call with the appropriate uplo flag. Also renamed the example to example_solve_lower_chol.f90 and added example_solve_upper_chol.f90 to match.