! Main for Power method
! Complex matrix A
!
! Input File:
! - Title
! - n, kodA, kodw0
! - Matrix A (on rows)
! - Vector w0
! - TOL, lnit
! - kodTest, ikod
! - kodNorm
! - Check
!
! n =matrix order
! kodA =0: read A; #0: generate A, by subroutine Agen(A)
! kodw0 =0: read w0; #0: generate w0, by subruutine W0gen(w0)
!
! TOL = Tolerance on "lambda" - or for z||z0 
! lnit = Limit number of iterations
!
! kodTest  <= 0: Test on z||z0
!			= 1: Test on |lambda -lambda0|
!          >= 2:  Test on ||A*z-lambda*z||
! ikod =0/#0: Do/Do not - Print iterations
!
! kodNorm: <=1: Infinity_Norm; >=2: 2_Norm (Euclidean)
!
! Check: text <=10 chars; the first char is significant:
!		 "C"; "c"; "P"; "p" / other chars = Do /Do not - check
!
Program Power_ComplexMethod
use work

character(255) fname, titlu *80
integer width
character(10) ch; logical do_check;
! For check:
complex az, lz
complex dif, dif_i


print '(a, \)', 'Input File: '
read *, fname
open(1, file =fname, status ='old', form ='formatted', iostat =io)

Err_If: if(io /=0) then
write(*, '(/,a,/)') ' Error in opening Input File !'
goto 600
endif Err_If


read(1, '(a)') titlu
read(1,*) n, kodA, kodw0

! Allocate:
allocate(A(n,n), w0(n), z0(n), z(n))

A =0.
w0 =(1.,0)
z0 =(0.,0.); z =(0.,0.);

! Read/Generate:
IF(kodA ==0) THEN
do i =1,n
read(1,*) (a(i,j), j =1,n)
enddo

ELSE	! Generate:
call agen(A)
ENDIF

IF(kodw0 ==0) THEN
read(1,*) (w0(i), i =1,n)

ELSE	! Generate:
call w0gen(w0)
ENDIF

read(1,*) TOL, lnit
read(1,*) kodTest, ikod
read(1,*) kodNorm

read(1,'(a)') ch
do_check =ch(1:1) =='C' .or. ch(1:1) =='c' .or. ch(1:1) =='P' .or. ch(1:1) =='p'

call Power_Complex(iter, kodTest, ikod, kodNorm, iter_flag, test_val, test_val_min)

iw =width(iter)

IF(iter_flag ==0) THEN
	select case(kodTest)
	case(:0)
	write(1, '(/, a, I<iw>, a)') ' Iteration No. ', iter, ': Tolerance on "z" met.'

	case(1)
	write(1, '(/, a, I<iw>, a)') ' Iteration No. ', iter, ': Tolerance on "lambda" met.'

	case(2:)
	write(1, '(/, a, I<iw>, a)') ' Iteration No. ', iter, ': Tolerance on "A*z-lambda*z" met.'
	end select

write(1, '(a, 1pg15.7)') ' Current Test_value is:', test_val

if(test_val_min /= test_val) then
write(1, '(a, 1pg15.7)') '   Minimum Test_value was:', test_val_min
endif

ELSE
	select case(kodTest)
	case(:0)
	write(1,'(/,a, I<iw>, a)') ' * Tolerance on "z" not met in ', iter, ' iterations'

	case(1)
	write(1,'(/,a, I<iw>, a)') ' * Tolerance on "lambda" not met in ', iter, ' iterations'

	case(2:)
	write(1,'(/,a, I<iw>, a)') ' * Tolerance on "A*z-lambda*z" not met in ', iter, ' iterations'
	end select

write(1, '(a, 1pg15.7)') '   Current Test_value is:', test_val
write(1, '(a, 1pg15.7)') '   Minimum Test_value was:', test_val_min
ENDIF


write(1, '(/, a, g15.7, g15.7)') ' Lambda-1 (dominant eigenvalue):', lambda

write(1, '(/, a)') ' Eigenvector #1:'

do i=1,n
iwi =width(i)
write(1,'(a, I<iwi>, a, g15.7, g15.7)') ' (', i, ')', z(i)
enddo

IF(do_check) THEN
write(1,'(/, a)') ' Check: A*z -lambda*z'

dif =(0.,0.)
idif =0

do i=1,n
az =dot_product(conjg(a(i,:)), z)
lz =lambda*z(i)
dif_i =az -lz

write(1,*) dif_i

if(abs(dif_i) >abs(dif)) then
dif =dif_i
idif =i
endif

enddo


write(1, '(/, a, (1pg10.3, 3x, 1pg10.3))') ' Maximum difference (in modulus) =', dif

if(idif >0) then
iwi =width(idif)
write(1, '(a, I<iwi>)') ' Coordinate # ', idif
endif

ENDIF

600 continue

end program Power_ComplexMethod



