|
| 1 | +! Eigendecomposition of a real symmetric matrix |
| 2 | +program example_eigh |
| 3 | + use stdlib_linalg_constants, only: sp |
| 4 | + use stdlib_linalg, only: eigh |
| 5 | + implicit none |
| 6 | + |
| 7 | + integer :: i |
| 8 | + real(sp), allocatable :: A(:,:),lambda(:),v(:,:) |
| 9 | + complex(sp), allocatable :: cA(:,:),cv(:,:) |
| 10 | + |
| 11 | + ! Decomposition of this symmetric matrix |
| 12 | + ! NB Fortran is column-major -> transpose input |
| 13 | + A = transpose(reshape( [ [2, 1, 4], & |
| 14 | + [1, 3, 5], & |
| 15 | + [4, 5, 4] ], [3,3] )) |
| 16 | + |
| 17 | + ! Note: real symmetric matrices have real (orthogonal) eigenvalues and eigenvectors |
| 18 | + allocate(lambda(3),v(3,3)) |
| 19 | + call eigh(A, lambda, vectors=v) |
| 20 | + |
| 21 | + print *, 'Real matrix' |
| 22 | + do i=1,3 |
| 23 | + print *, 'eigenvalue ',i,': ',lambda(i) |
| 24 | + print *, 'eigenvector ',i,': ',v(:,i) |
| 25 | + end do |
| 26 | + |
| 27 | + ! Complex hermitian matrices have real (orthogonal) eigenvalues and complex eigenvectors |
| 28 | + cA = A |
| 29 | + |
| 30 | + allocate(cv(3,3)) |
| 31 | + call eigh(cA, lambda, vectors=cv) |
| 32 | + |
| 33 | + print *, 'Complex matrix' |
| 34 | + do i=1,3 |
| 35 | + print *, 'eigenvalue ',i,': ',lambda(i) |
| 36 | + print *, 'eigenvector ',i,': ',cv(:,i) |
| 37 | + end do |
| 38 | + |
| 39 | +end program example_eigh |
0 commit comments