! Simpla precizie
! Metoda Laguerre pentru polinoame
! Zero-uri complexe
! Cu IMSL: CALL dZPLRC (NDEG, COEFF, ROOT)
! ndeg: gradul polinomului.
! coef: vectorul coef. incepand cu gradul cel mai mare
! root: tablou complex pt. radacini
!use IMSL
!
! Polinom: p(z) =a_n*x^n +a_(n-1)*x^(n-1) + ... +a_1*z +a_0
! a_i = reali
!
! Fisier de date:
! 1) Titlul problemei: <=80 caractere.
! 2) Grad polinom: n
! 3) Coeficientii (reali): a_n, a_(n-1), ..., a_0
! Urmatoarele date sunt optionale:
! 4) Inverse: logic, pentru inversarea coeficientilor: T/F
! 5) Distance: logic, pentru calulul distanteo rad. la un punct "z": T/F
!*6) z: complex. Numai daca Distance = T.

use numerical_libraries



character(260) fname, dummy*80
real(4), allocatable :: coef(:)
complex, allocatable :: rad(:); complex polval;
logical inverse
logical distance; complex z;

integer status

!print '(a, \)', ' Input file: '
!read '(a)', fname
call GetFile(fname, status, 'Selecteaza Fisierul de Intrare'C)

open(1, file =fname, status ='old', form ='formatted')

read(1,'(a)') dummy		! Title

read (1,*) n	! gradul
allocate (coef(0:n))
allocate (rad(n))

read(1,*) (coef(i), i=n,0, -1)

read (1, '(L)', err =100, end =100) inverse
if(inverse) then
do i=0, n
coef(i) =1./coef(i)
enddo
endif

100 print *, 'Coeficienti: ', (coef(i), i=n,0,-1)


! Check distance:
read(1,'(L)', err =200, end =200) distance

if(distance) then
read (1,*) z
endif

200 write(1,*)
call ZPLRC (n, coef, rad)
!CALL ZPLRC (NDEG, COEFF, ROOT)

! Roots:
write(1,'(/, a)') ' Radacini:'
do i =1,n
write(1,*) i, rad(i)
enddo

! Polynomial values:
write(1,'(/, a)') ' Valori polinom:'
do i =1,n
call compute_polval(n, coef, rad(i), polval)
write(1,*), i, polval
enddo

! Distance:
if(distance) then
write(1, '(/, a, f8.3, f8.3)') ' Distanta la punctul:', z
do i =1,n
d = cabs(rad(i)-z)
write(1,*) i, d
enddo
endif


END
