Skip to content

Commit cf40f75

Browse files
committed
add example
1 parent 9de160b commit cf40f75

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

example/linalg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ ADD_EXAMPLE(blas_gemv)
2020
ADD_EXAMPLE(lapack_getrf)
2121
ADD_EXAMPLE(lstsq1)
2222
ADD_EXAMPLE(lstsq2)
23+
ADD_EXAMPLE(svd1)
2324
ADD_EXAMPLE(determinant)
2425
ADD_EXAMPLE(determinant2)

example/linalg/example_svd1.f90

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
! Singular Value Decomposition
2+
program example_svd1
3+
use stdlib_linalg_constants, only: dp
4+
use stdlib_linalg, only: svd
5+
implicit none
6+
7+
real(dp), allocatable :: A(:,:),s(:),u(:,:),vt(:,:)
8+
9+
! We want to find the singular value decomposition of matrix:
10+
!
11+
! A = [ 3 2 2]
12+
! [ 2 3 -2]
13+
!
14+
A = transpose(reshape([ 3, 2, 2, &
15+
2, 3,-2], [3,2]))
16+
17+
! Prepare arrays
18+
allocate(s(2),u(2,2),vt(3,3))
19+
20+
! Get singular value decomposition
21+
call svd(A,s,u,vt)
22+
23+
! Singular values: [5, 3]
24+
print 1, ' '
25+
print 1, 'S = ',s
26+
print 1, ' '
27+
28+
! Left vectors (may be flipped):
29+
! [Ã2/2 Ã2/2]
30+
! U = [Ã2/2 -Ã2/2]
31+
!
32+
print 1, ' '
33+
print 1, 'U = ',u(1,:)
34+
print 1, ' ',u(2,:)
35+
36+
37+
! Right vectors (may be flipped):
38+
! [Ã2/2 Ã2/2 0]
39+
! V = [1/Ã18 -1/Ã18 4/Ã18]
40+
! [ 2/3 -2/3 -1/3]
41+
!
42+
print 1, ' '
43+
print 1, ' ',vt(1,:)
44+
print 1, 'VT= ',vt(2,:)
45+
print 1, ' ',vt(3,:)
46+
print 1, ' '
47+
48+
1 format(a,*(1x,f12.8))
49+
50+
end program example_svd1

0 commit comments

Comments
 (0)