File: example_svd.f90

package info (click to toggle)
fortran-stdlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 34,008 kB
  • sloc: f90: 24,178; ansic: 1,244; cpp: 623; python: 119; makefile: 13
file content (50 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
! Singular Value Decomposition 
program example_svd
  use stdlib_linalg_constants, only: dp
  use stdlib_linalg, only: svd
  implicit none

  real(dp), allocatable :: A(:,:),s(:),u(:,:),vt(:,:)
  character(*), parameter :: fmt = "(a,*(1x,f12.8))"

  ! We want to find the singular value decomposition of matrix: 
  ! 
  !    A = [ 3  2  2]
  !        [ 2  3 -2]  
  !   
  A = transpose(reshape([ 3, 2, 2, &
                          2, 3,-2], [3,2]))

  ! Prepare arrays
  allocate(s(2),u(2,2),vt(3,3))

  ! Get singular value decomposition
  call svd(A,s,u,vt)

  ! Singular values: [5, 3]
  print fmt, '    '
  print fmt, 'S = ',s
  print fmt, '    '
  
  ! Left vectors (may be flipped): 
  !     [2/2  2/2]
  ! U = [2/2 -2/2]
  !
  print fmt, '    '
  print fmt, 'U = ',u(1,:)
  print fmt, '    ',u(2,:)
  

  ! Right vectors (may be flipped): 
  !     [2/2    2/2      0]
  ! V = [1/18 -1/18  4/18]
  !     [ 2/3    -2/3   -1/3]
  !
  print fmt, '    '
  print fmt, '    ',vt(1,:)
  print fmt, 'VT= ',vt(2,:)
  print fmt, '    ',vt(3,:)
  print fmt, '    '
  

end program example_svd