!  Program principal pt. ELIM2
!  Matricea HILBERT: generata
!
character*128 fname, charn*2
real, allocatable :: A(:,:), B(:,:)
integer width

real, allocatable :: A0(:,:), C(:,:)
logical do_check

print '(a, \)', ' Inversa matricii HILBERT de ordinul: '
read *, n

print '(a, \)', ' Verificare (T/F): '
read(*,'(L)') do_check

iwn =width(n)
write(charn, '(I<iwn>)') n

fname ='Inv-H'//trim(charn)//'.rez'
open(1, file =fname, status ='unknown')

allocate (A(n, n), B(n,n))

! Genereaza matricea Hilbert:
ido: do i =1,n
jdo: do j =1,n
a(i,j) =1./(i+j-1)
enddo jdo
enddo ido

if(do_check) then
allocate (A0(n, n), C(n,n))
A0 =A; C=0.;
endif

! Genereaza termenii liberi:
b =0.
do i =1, n
b(i,i) =1.
enddo

nb =n
prag =1E-10

call ELIM2 (n, nb, A, B, prag, kod)

! Rezultate:
!iwn =width(n)
write(1,'(a, I<iwn>)') 'Inversa (GAUSS) a matricii HILBERT de ordinul ', n

if (kod <0) then
write(1,'(/, a)') '** Pivot < prag: Matrice aproape singulara.'
goto 500
endif

nn =min(8,n)

do i =1, n
write(1,*)
write(1, '(<nn>(1pg16.7))') b(1:n,i)
enddo

! Proba:
if(do_check) then
C =matmul(A0,B)

write(1, '(/, a)') ' Proba:'
do i =1, n
write(1, '(<nn>(1pg16.7))') C(i, 1:n)
enddo

endif

500 deallocate(A, B)
if(allocated(A0)) deallocate(A0)
if(allocated(C)) deallocate(C)

write(*,'(/, a, a )') '* Fisier de iesire: ', trim(fname)

END

