!Performs the check A*z =lambda*z
subroutine Gen_Eigen_Files(fname, n, lambda_z, vec_z, lambda, vec, real_val)
!use work
use dflib


logical real_val

real(8) lambda(n), vec(n,n); complex(8) lambda_z(n), vec_z(n,n);

complex(8) az, lz, dif_z
real(8) ay, ly, dif_y, dif_r, dif_max

integer width

CHARACTER(3)   drive
CHARACTER(256) dir
CHARACTER(100) name
CHARACTER(4)   ext

character($maxpath) fname, val_file, vec_file



! File Names:
call splitpathqq(fname, drive, dir, name, ext)
val_file =trim(name)//'-VAL.dat'
vec_file =trim(name)//'-VEC.dat'

open(3, file =VAL_file, status ='unknown', form ='formatted')
open(4, file =VEC_file, status ='unknown', form ='formatted')

write(3,'(a)') ' EIGEN_VALUES'
write(4,'(a,\)') ' EIGEN_VECTORS'



IVP_GEN: &
DO IVP =1,n
iwi =width(ivp)


RealVal_If: &
IF(real_val) THEN

write(3, '(1x, I<iwi>, 1pg24.16)') ivp, lambda(ivp)

! Vectors:
write(4, '(/, I8)') ivp

do l =1,n
write(4, '(g24.16)') VEC(l, ivp)
enddo

ELSE	! complex
if(imag(lambda_z(ivp)) /=0) then
write(3, '(1x, I<iwi>, 1pg24.16, 1pg24.16)') ivp, lambda_z(ivp)

else
write(3, '(1x, I<iwi>, 1pg24.16, 1pg24.16)') ivp, real(lambda_z(ivp))
endif

! Vectors:
write(4, '(/, I8)') ivp

do l=1,n
	if(imag(vec_z(l, ivp)) /=0) then
	write(4,'(g24.16, g24.16)') vec_z(l, ivp)

	else
	write(4,'(g24.16)') real(vec_z(l, ivp))
	endif
enddo

ENDIF RealVal_If

ENDDO IVP_GEN


!500 continue
end subroutine Gen_Eigen_Files
