Skip to content

Commit 9906c93

Browse files
committed
add real and complex examples
1 parent 0be918e commit 9906c93

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

example/linalg/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ ADD_EXAMPLE(state1)
1818
ADD_EXAMPLE(state2)
1919
ADD_EXAMPLE(blas_gemv)
2020
ADD_EXAMPLE(lapack_getrf)
21+
ADD_EXAMPLE(solve1)
22+
ADD_EXAMPLE(solve2)

example/linalg/example_solve1.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
program example_solve1
2+
use stdlib_linalg_constants, only: sp
3+
use stdlib_linalg, only: solve, linalg_state_type
4+
implicit none
5+
6+
real(sp), allocatable :: A(:,:),b(:),x(:)
7+
8+
! Solve a system of 3 linear equations:
9+
! 4x + 3y + 2z = 25
10+
! -2x + 2y + 3z = -10
11+
! 3x - 5y + 2z = -4
12+
13+
! Note: Fortran is column-major! -> transpose
14+
A = transpose(reshape([ 4, 3, 2, &
15+
-2, 2, 3, &
16+
3,-5, 2], [3,3]))
17+
b = [25,-10,-4]
18+
19+
! Get coefficients of y = coef(1) + x*coef(2) + x^2*coef(3)
20+
x = solve(A,b)
21+
22+
print *, 'solution: ',x
23+
! 5.0, 3.0, -2.0
24+
25+
end program example_solve1
26+

example/linalg/example_solve2.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
program example_solve2
2+
use stdlib_linalg_constants, only: sp
3+
use stdlib_linalg, only: solve, linalg_state_type
4+
implicit none
5+
6+
complex(sp), allocatable :: A(:,:),b(:),x(:)
7+
8+
! Solve a system of 3 complex linear equations:
9+
! 2x + iy + 2z = (5-i)
10+
! -ix + (4-3i)y + 6z = i
11+
! 4x + 3y + z = 1
12+
13+
! Note: Fortran is column-major! -> transpose
14+
A = transpose(reshape([(2.0, 0.0),(0.0, 1.0),(2.0,0.0), &
15+
(0.0,-1.0),(4.0,-3.0),(6.0,0.0), &
16+
(4.0, 0.0),(3.0, 0.0),(1.0,0.0)] , [3,3]))
17+
b = [(5.0,-1.0),(0.0,1.0),(1.0,0.0)]
18+
19+
! Get coefficients of y = coef(1) + x*coef(2) + x^2*coef(3)
20+
x = solve(A,b)
21+
22+
print *, 'solution: ',x
23+
! (1.0947,0.3674) (-1.519,-0.4539) (1.1784,-0.1078)
24+
25+
end program example_solve2
26+

src/stdlib_linalg.fypp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ module stdlib_linalg
3131
public :: is_triangular
3232
public :: is_hessenberg
3333

34+
! Export linalg error handling
35+
public :: linalg_state_type, linalg_error_handling
36+
3437
interface diag
3538
!! version: experimental
3639
!!

0 commit comments

Comments
 (0)