File: example_eig_generalized.f90

package info (click to toggle)
fortran-stdlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,008 kB
  • sloc: f90: 24,178; ansic: 1,244; cpp: 623; python: 119; makefile: 13
file content (30 lines) | stat: -rw-r--r-- 886 bytes parent folder | download
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
! Eigendecomposition of a real square matrix for the generalized eigenproblem
program example_eig_generalized
  use stdlib_linalg, only: eig, eye
  implicit none

  integer :: i
  real, allocatable :: A(:,:), B(:,:)
  complex, allocatable :: lambda(:), vectors(:,:)

  ! Matrices for the generalized eigenproblem: A * v = lambda * B * v
  ! NB Fortran is column-major -> transpose input
  A = transpose(reshape([ [2, 2, 4], &
                          [1, 3, 5], &
                          [2, 3, 4] ], [3,3]))

  B = eye(3)

  ! Allocate eigenvalues and right eigenvectors
  allocate(lambda(3), vectors(3,3))

  ! Get eigenvalues and right eigenvectors for the generalized problem
  call eig(A, B, lambda, right=vectors)

  do i = 1, 3
     print *, 'Eigenvalue  ', i, ': ', lambda(i)
     print *, 'Eigenvector ', i, ': ', vectors(:,i)
  end do

end program example_eig_generalized