! Hessian - or its inverse - update
! We use the Murtagh-Sargent update, also called Symmetric Rank 1
!
! input:
!  - nfree: number of coordinates
!  - ds: GeomOld-Geom (expressed in the optimization coord.)
!  - dgrad: GradOld-Grad  (expressed in the optimization coord.)
!
!  input/output:
!  - Hess: old Hessian in input, replaced by updated Hessian on output
!             (or inverse Hessian)
!
!
!!!!!!!!!!!!!!!!!!!!!!
! The update formula for the Hessian is:
! (following Fletcher, approximate Hessian is denoted by B)
! B(k+1)=B+[(y-Bs){(y-Bs)T}]/[{(y-Bs)T}s]
!
! it is self dual, ie for the inverse Hessian, denote by H:
! H(k+1)=H+[(s-Hy){(s-Hy)T}]/[{(s-Hy)T}y]
!
! 
! We decompose the computation in two steps:
! first we compute the update, and then we add it to Hess to get the
! new Hessian.
! This allows us to introduce a scaling factor if needed later.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SUBROUTINE Hupdate_MS(Nfree,ds,dgrad,Hess)

  IMPLICIT NONE

  INTEGER, PARAMETER :: KINT=KIND(1)
  INTEGER, PARAMETER :: KREAL=KIND(1.D0)

  INTEGER(KINT), INTENT(IN) :: NFREE
  REAL(KREAL), INTENT(INOUT) :: Hess(Nfree,Nfree)
  REAL(KREAL), INTENT(IN) :: ds(Nfree)
  REAL(KREAL), INTENT(IN) :: dGrad(Nfree)


  REAL(KREAL), ALLOCATABLE  :: HessOld(:,:) , HupMS(:,:)
  REAL(KREAL), ALLOCATABLE  :: Hup(:,:)
  REAL(KREAL), ALLOCATABLE  :: HupInt(:)
  REAL(KREAL), ALLOCATABLE  :: HS(:), Num(:,:)
  REAL(KREAL) :: Phi, Num3, Den, Den2,NormDS
  CHARACTER(120) :: fmt,fmt2,Line
  INTEGER(KINT) :: i,j, idx

  LOGICAL :: debug

  interface
     function valid(string) result (isValid)
       logical                  :: isValid
       character(*), intent(in) :: string
     end function valid

  end interface




  debug=valid("Hupdate").OR.Valid("Hupdate_MS")

  if (debug) Call Header("Entering Hupdate_MS")

  ALLOCATE(HessOld(nfree,nfree),  &
       Hup(nfree,nfree), &
       HupInt(nfree), HS(nfree),  &
       Num(nfree,nfree))

  HessOld=Hess

  if (debug) THEN
     WRITE(*,*) 'DBG Hupdate HessOld'
     Idx=min(12,Nfree)
     DO I=1,NFree
        WRITE(*,'(12(1X,F8.4))') HessOld(I,1:Idx)
     END DO
     WRITE(*,*) 'DBG Hupdate ds'
     WRITE(*,'(12(1X,F8.4))') ds(1:Idx)
     WRITE(*,*) 'DBG Hupdate dGrad'
     WRITE(*,'(12(1X,F8.4))') dGrad(1:Idx)
  END IF

  NormDS=sqrt(DOT_PRODUCT(DS,DS))
  if (NormDS>=1e-6) THEN

     HS=0
     DO I=1,nfree
        DO J=1,nfree
           HS(I)=HS(I)+HessOld(i,j)*Ds(j)
        END DO
     END DO

     if (debug) WRITE(*,*) "HS:",HS
     HupInt=DGrad-HS

     ! WE calculate the MS update
     Num=0.
     Den=0.
     DO I=1,nfree
        DO J=1,nfree
           Num(I,J)=HUpInt(i)*HupInt(j)
        END DO
        Den=Den+Hupint(i)*Ds(i)
     END DO
     if (debug) WRITE(*,*) "Den:",Den,1./Den

     Hup=Num/Den

     Hess=HessOld+Hup
  ELSE
     Hess=HessOld
  END IF
  !     WRITE(*,*) 'New hess'
  !     DO I=1,nfree
  !        WRITE(*,fmt) Hess(i,:)
  !     END DO

  DEALLOCATE(HessOld,Hup, HupInt, HS, Num )

  if (debug) Call Header("Hupdate_MS Over")

END SUBROUTINE Hupdate_MS
