-
Notifications
You must be signed in to change notification settings - Fork 194
feat : [linalg] add bi-conjugate gradient stabilized method #1034
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3dc4088
implement bicgstab with copilot
jalvesz 05a694b
replace logical size indicator
jalvesz a3a3bdd
add example with wilkilson matrix
jalvesz 938c0de
solve conflict with master branch
jalvesz 89425c0
Merge branch 'master' into bicgstab
jalvesz 2d08a29
revert space
jalvesz 56345f9
fix documentation links and descriptions
jalvesz ef5611a
Update example/linalg/example_solve_bicgstab_wilkinson.f90
jalvesz 8a5f861
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz 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,41 @@ | ||
program example_solve_bicgstab | ||
use stdlib_kinds, only: dp | ||
use stdlib_linalg_iterative_solvers | ||
implicit none | ||
|
||
integer, parameter :: n = 4 | ||
real(dp) :: A(n,n), b(n), x(n) | ||
integer :: i | ||
|
||
! Example matrix (same as SciPy documentation) | ||
A = reshape([4.0_dp, 2.0_dp, 0.0_dp, 1.0_dp, & | ||
3.0_dp, 0.0_dp, 0.0_dp, 2.0_dp, & | ||
0.0_dp, 1.0_dp, 1.0_dp, 1.0_dp, & | ||
0.0_dp, 2.0_dp, 1.0_dp, 0.0_dp], [n,n]) | ||
|
||
b = [-1.0_dp, -0.5_dp, -1.0_dp, 2.0_dp] | ||
|
||
x = 0.0_dp ! Initial guess | ||
|
||
print *, 'Solving Ax = b using BiCGSTAB method:' | ||
print *, 'Matrix A:' | ||
do i = 1, n | ||
print '(4F8.2)', A(i,:) | ||
end do | ||
print *, 'Right-hand side b:' | ||
print '(4F8.2)', b | ||
|
||
! Solve using BiCGSTAB | ||
call stdlib_solve_bicgstab(A, b, x, rtol=1e-10_dp, atol=1e-12_dp) | ||
|
||
print *, 'Solution x:' | ||
print '(4F10.6)', x | ||
|
||
! Verify solution | ||
print *, 'Verification A*x:' | ||
print '(4F10.6)', matmul(A, x) | ||
|
||
print *, 'Residual ||b - A*x||:' | ||
print *, norm2(b - matmul(A, x)) | ||
|
||
end program |
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,52 @@ | ||
program example_solve_bicgstab_wilkinson | ||
use stdlib_kinds, only: dp | ||
use stdlib_linalg_iterative_solvers | ||
use stdlib_sparse | ||
use stdlib_sparse_spmv | ||
implicit none | ||
|
||
integer, parameter :: n = 21 | ||
type(COO_dp_type) :: COO | ||
type(CSR_dp_type) :: A | ||
type(stdlib_solver_workspace_dp_type) :: workspace | ||
real(dp) :: b(n), x(n), norm_sq0 | ||
integer :: i | ||
|
||
! Construct the Wilkinson's matrix in COO format | ||
! https://en.wikipedia.org/wiki/Wilkinson_matrix | ||
call COO%malloc(n, n, n + 2*(n-1)) | ||
COO%data(1:n) = [( dble(abs(i)), i=-(n-1)/2, (n-1)/2)] | ||
jalvesz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
COO%data(n+1:) = 1.0_dp | ||
do i = 1, n | ||
COO%index(1:2, i) = [i,i] | ||
end do | ||
do i = 1, n-1 | ||
COO%index(1:2, n+i) = [i,i+1] | ||
COO%index(1:2, n+n-1+i) = [i+1,i] | ||
end do | ||
call coo2ordered(COO,sort_data=.true.) | ||
jalvesz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
! Convert COO to CSR format | ||
call coo2csr(COO, A) | ||
|
||
! Set up the right-hand side for the solution to be ones | ||
b = 0.0_dp | ||
x = 1.0_dp | ||
call spmv(A, x, b) ! b = A * 1 | ||
x = 0.0_dp ! initial guess | ||
|
||
! Solve the system using BiCGSTAB | ||
workspace%callback => my_logger | ||
call stdlib_solve_bicgstab(A, b, x, rtol=1e-12_dp, maxiter=100, workspace=workspace) | ||
|
||
contains | ||
|
||
subroutine my_logger(x,norm_sq,iter) | ||
real(dp), intent(in) :: x(:) | ||
real(dp), intent(in) :: norm_sq | ||
integer, intent(in) :: iter | ||
if(iter == 0) norm_sq0 = norm_sq | ||
print *, "Iteration: ", iter, " Residual: ", norm_sq, " Relative: ", norm_sq/norm_sq0 | ||
end subroutine | ||
|
||
end program example_solve_bicgstab_wilkinson |
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.
Uh oh!
There was an error while loading. Please reload this page.