! Inverse Power method
! A, w and z - defined in "work"

subroutine LU_Shift_Complex(iter, kodTest, kodPrint, kodLU, test_val)
use work

complex lambda0		! Previous value
integer order(n)

integer width

complex colin(n)

logical do_pvt


! LU decomposition:
kodLU =0

! Initializeaza ORDER:
do i =1, n
order(i) =i
end do

do_pvt =.true.
kodPivot =1
!call LUdecomp_z(A_z, dl, n, order, kodLU)
call LUdecomp_z(A, n, order, do_pvt, kodPivot, kodLU)

if(kodLU /=0) then
RETURN	!call EXIT
endif

! Normalize guess vector:
z0 =w0/vNorm2_z(n, w0)

! Printing:
IF(kodPrint >=2) THEN
write(1, '(/, a)') ' Guess vector:'
write(1,*) (z0(j), j=1,n)

itl =14 +(2*n-1)*15 +7
write(1, '(/, a, t14, a, t<itl>, a)') ' Iteration', 'Current_vector', 'Current_lambda'
ENDIF


lambda0 =0

! Initialize counter:
iter =0

100 iter =iter+1

! Solve Aw =z
!call LUsolve_z(A_z, n, dl, z0, z, order)
call LUsolve_z(A, z0, n, z, order)

! Calculate "lambda": Seek the largest coordinate of vector w:
imax =maxindex_z(z)
lambda =z(imax)/z0(imax)


! Normalize z:
z =z/vNorm2_z(n,z)

! Iterations printing:
IF(kodPrint >=2) THEN
iwi =width(iter) +1
write(1,'(I<iwi>, <n>(G15.7, g15.7), (g15.7,g15.7))') iter, (z(j), j=1,n), lambda
ENDIF

! Do at least 2 iterations:
if(iter ==1) then
z0 =z
lambda0 =lambda
goto 100
endif

! Test on |lambda-lambda0| or |z-z0|:
SELECT CASE(kodTest)

CASE(0)		! Test "lambda":
!test_val =abs(lambda -lambda0)
test_val =abs((1.,0.)/lambda -(1.,0.)/lambda0)

CASE DEFAULT	! Test if: z||z0
do i=1,n
if(abs(z(i)) <TOL .and. abs(z0(i)) <TOL) then
cycle

else
colin(i) =z(i)/z0(i)
endif
enddo

do i=1,n
if(abs(z(i)) <TOL .and. abs(z0(i)) <TOL) then

colin(i) =colin(imax)
endif
enddo

!*
!write(1,*) colin
!*

colin =colin -colin(imax)
test_val =vNorm2_z(n, colin)

END SELECT

if(test_val <= TOL .or. iter ==lnit) then
RETURN

else
z0 =z
lambda0 =lambda
GOTO 100
endif


500 continue

end subroutine LU_Shift_Complex
