      subroutine inverse(u,n)
      implicit real*8 (a-h,o-x,z),logical*1(y)
      dimension u(n,n)
      double precision,dimension(:,:),allocatable :: vp
      double precision,dimension(:),allocatable :: v
      allocate (vp(n,n))
      allocate (v(n))
      call hdiag(u,vp,n,n)
      do k=1,n
         v(k)=u(k,k)
      enddo
      v=1.d0/v
      do k=1,n
         u(:,k)=u(:,k)*v(k)
      enddo
      u=matmul(u,transpose(vp))
      deallocate (v)
      deallocate (vp)
      return
      end

