SUBROUTINE Hupdate_MS(Nfree,ds,dgrad,Hess)

! 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.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


!----------------------------------------------------------------------
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
!  Centre National de la Recherche Scientifique,
!  Université Claude Bernard Lyon 1. All rights reserved.
!
!  This work is registered with the Agency for the Protection of Programs 
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
!
!  Authors: P. Fleurat-Lessard, P. Dayal
!  Contact: optnpath@gmail.com
!
! This file is part of "Opt'n Path".
!
!  "Opt'n Path" is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Affero General Public License as
!  published by the Free Software Foundation, either version 3 of the License,
!  or (at your option) any later version.
!
!  "Opt'n Path" is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU Affero General Public License for more details.
!
!  You should have received a copy of the GNU Affero General Public License
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
!
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
! for commercial licensing opportunities.
!----------------------------------------------------------------------

  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(:, :) 
  REAL(KREAL), ALLOCATABLE  :: Hup(:,:)
  REAL(KREAL), ALLOCATABLE  :: HupInt(:)
  REAL(KREAL), ALLOCATABLE  :: HS(:), Num(:,:)
  REAL(KREAL) :: Den, NormDS
  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
