-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #806 from perazz/linalg_solve
linalg: solve
- Loading branch information
Showing
11 changed files
with
653 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
program example_solve1 | ||
use stdlib_linalg_constants, only: sp | ||
use stdlib_linalg, only: solve, linalg_state_type | ||
implicit none | ||
|
||
real(sp), allocatable :: A(:,:),b(:),x(:) | ||
|
||
! Solve a system of 3 linear equations: | ||
! 4x + 3y + 2z = 25 | ||
! -2x + 2y + 3z = -10 | ||
! 3x - 5y + 2z = -4 | ||
|
||
! Note: Fortran is column-major! -> transpose | ||
A = transpose(reshape([ 4, 3, 2, & | ||
-2, 2, 3, & | ||
3,-5, 2], [3,3])) | ||
b = [25,-10,-4] | ||
|
||
! Get coefficients of y = coef(1) + x*coef(2) + x^2*coef(3) | ||
x = solve(A,b) | ||
|
||
print *, 'solution: ',x | ||
! 5.0, 3.0, -2.0 | ||
|
||
end program example_solve1 | ||
|
This file contains 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 @@ | ||
program example_solve2 | ||
use stdlib_linalg_constants, only: sp | ||
use stdlib_linalg, only: solve, linalg_state_type | ||
implicit none | ||
|
||
complex(sp), allocatable :: A(:,:),b(:),x(:) | ||
|
||
! Solve a system of 3 complex linear equations: | ||
! 2x + iy + 2z = (5-i) | ||
! -ix + (4-3i)y + 6z = i | ||
! 4x + 3y + z = 1 | ||
|
||
! Note: Fortran is column-major! -> transpose | ||
A = transpose(reshape([(2.0, 0.0),(0.0, 1.0),(2.0,0.0), & | ||
(0.0,-1.0),(4.0,-3.0),(6.0,0.0), & | ||
(4.0, 0.0),(3.0, 0.0),(1.0,0.0)] , [3,3])) | ||
b = [(5.0,-1.0),(0.0,1.0),(1.0,0.0)] | ||
|
||
! Get coefficients of y = coef(1) + x*coef(2) + x^2*coef(3) | ||
x = solve(A,b) | ||
|
||
print *, 'solution: ',x | ||
! (1.0947,0.3674) (-1.519,-0.4539) (1.1784,-0.1078) | ||
|
||
end program example_solve2 | ||
|
This file contains 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,32 @@ | ||
program example_solve3 | ||
use stdlib_linalg_constants, only: sp,ilp | ||
use stdlib_linalg, only: solve_lu, linalg_state_type | ||
implicit none | ||
|
||
integer(ilp) :: test | ||
integer(ilp), allocatable :: pivot(:) | ||
complex(sp), allocatable :: A(:,:),b(:),x(:) | ||
|
||
! Solve a system of 3 complex linear equations: | ||
! 2x + iy + 2z = (5-i) | ||
! -ix + (4-3i)y + 6z = i | ||
! 4x + 3y + z = 1 | ||
|
||
! Note: Fortran is column-major! -> transpose | ||
A = transpose(reshape([(2.0, 0.0),(0.0, 1.0),(2.0,0.0), & | ||
(0.0,-1.0),(4.0,-3.0),(6.0,0.0), & | ||
(4.0, 0.0),(3.0, 0.0),(1.0,0.0)] , [3,3])) | ||
|
||
! Pre-allocate x | ||
allocate(b(size(A,2)),pivot(size(A,2))) | ||
allocate(x,mold=b) | ||
|
||
! Call system many times avoiding reallocation | ||
do test=1,100 | ||
b = test*[(5.0,-1.0),(0.0,1.0),(1.0,0.0)] | ||
call solve_lu(A,b,x,pivot) | ||
print "(i3,'-th solution: ',*(1x,f12.6))", test,x | ||
end do | ||
|
||
end program example_solve3 | ||
|
This file contains 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 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.