program Pol_Direct_q
! Metoda NEWTON pt. polinoame: numar de 'NR' radacini.
! Prin iterare in polinomul original
! Coeficientii: a_n, ..., a_1, a_0
! Derivata: f1 = catul q(xn,xn)
!
! FISIER DE DATE (intrare/iesire): nume dat de utilizator.
! 0) Titlul problemei (80 caractere)
! 1) n, nr, ktip:
!	n =grad polinom
!	nr =numar radacini 0<= nr <=n; nr =0: programul pune nr=n
!	ktip =cod tiparire la display: 0 / #0
! 2) Coeficientii polinomului: a_n, a_n-1, ..., a_1, a_0
! 3) EPS, ITMAX: Toleranta; Numarul maxim de iteratii
! 4) x(k),k=1,nr: Aproximatiile radacinilor (in numar de "nr")
!
! Rezultatele se scriu in fisier, dupa datele de intrare.

use dfwin

real, allocatable ::A(:), X(:), p(:)
integer, allocatable:: IE(:), nit(:)
character(260) fname, dummy*80
character(100) text(-2:2)
logical printed_code(-2:2)

integer, allocatable:: no_div(:)
integer status
character(80) w_titlu


text(-2)='** Numitor nul'
text(-1)='** Divergenta'

text(0) ='* Toleranta EPS atinsa'
text(1) ='** Numar de iteratii depasit'
text(2) ='** Proces stationar'

!write(*,'(a\)') ' Fisier date: '
!read(*,'(a)') fname

!call GetFile(fname, status, 'Select Data File'C)
w_titlu ='Select Data File'C
call GetFile(fname, status, w_titlu)

if(status ==0) then
print '(a, \)', 'Input File: '
read *, fname
endif

OPEN(UNIT=1, FILE=fname, STATUS='OLD')

read(1,'(a80)') dummy
READ(1,*) N, NR, ktip
if(nr.eq.0) nr=n
!
allocate (a(0:n),x(1:nr),ie(1:nr),nit(1:nr), &
p(1:nr),STAT=ierr)

!
read(1,*) (A(I), I=N,0,-1)
read(1,*) EPS, ITMAX0
READ(1,*) (X(K), K=1,NR)

allocate (no_div(nr))

write(*,'(a1, a80)') ' ', dummy

DO K=1,NR
X0=X(K)
itmax =itmax0
ier =ktip

if(ier /=0) then
write(1, '(//, a, 1pg15.7)') ' Aproximatia:', x0
endif

CALL POL_direct(N, A, X0, EPS, ITMAX, RAD, B, IER, n_div)
    X(K) =RAD
    ie(k) =ier
    nit(k) =itmax
	p(k) =b
	no_div(k) =n_div
ENDDO


! Explore divergence:
nd =maxval(no_div)

IF(nd /=0) THEN
write(1,'(//, a)') ' Radacina	    Nr. de divergente locale'

do k=1,nr

if(no_div(k) /=0) then
write(1, *) x(k), no_div(k)
endif

enddo
ENDIF

! Write roots:
write(*, '(/, a)') ' ** Radacinile:'

write(1,*)
write(1, '(/, a)') '  ** Radacinile:'
write(1,'(a)') ' Radacina				 Cod	Iteratii	p(Radacina)'

DO k=1,nr
write(*,*) X(K), IE(K), nit(k), '  ', p(k)
write(1,*) X(K), IE(K), nit(k), '  ', p(k)
ENDDO

printed_code =.false.
write(1, '(/, a, a)') ' Cod   Semnificatie'

do k=1,nr
iek =ie(k)
if(printed_code(iek)) cycle
write(1,'(I4, 3x, a)') iek, trim(text(iek))
printed_code(iek) =.true.
enddo

lf =lstrlen(fname)
write(*, '(/, a, a)') '* Fisier de iesire: ', fname(1:lf)
END


