! Copyright (C) 2008, 2009  VZLU Prague, a.s., Czech Republic, Jaroslav Hajek <highegg@gmail.com>
! Copyright (C) 2026 Martin Köhler <koehlerm(AT)mpi-magdeburg.mpg.de>
!
! This file is part of qrupdate-ng.
!
! qrupdate is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation; either version 3 of the License, or
! (at your option) any later version.
!
! This program 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 General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this software; see the file COPYING.  If not, see
! <http://www.gnu.org/licenses/>.
!
program tch1dn
    integer n
    external stest, dtest, ctest, ztest, pstats

    write (*,*)
    write (*,*) 'testing Cholesky rank-1 downdate routines.'
    write (*,*) 'All residual errors are expected to be small.'
    write (*,*)

    n = 50
    write (*,*) 'sch1dn test:'
    call stest(n)
    write (*,*) 'dch1dn test:'
    call dtest(n)
    write (*,*) 'cch1dn test:'
    call ctest(n)
    write (*,*) 'zch1dn test:'
    call ztest(n)

    call pstats
end program

subroutine stest(n)
    use iso_fortran_env
    integer n
    real(real32) A(n,n),R(n,n),u(n),wrk(2*n)
    external srandg,scopy,schgen,sch1up,schchk,sch1dn
    integer info
    ! set up random matrix & vectors
    call srandg(n,n,A,n)
    call srandg(n,1,u,n)
    call scopy(n,u,1,wrk,1)
    ! generate A'*A and its Cholesky decomposition
    call schgen(n,A,n,R,n)
    ! update the Cholesky decomposition
    call sch1up(n,R,n,u,wrk(1+n))
    ! downdate it back
    call sch1dn(n,R,n,wrk,wrk(1+n),info)
    ! check result
    call schchk(n,A,n,R,n)

end subroutine

subroutine dtest(n)
    use iso_fortran_env
    integer n
    real(real64) A(n,n),R(n,n),u(n),wrk(2*n)
    external drandg,dcopy,dchgen,dch1up,dchchk,dch1dn
    integer info
    ! set up random matrix & vectors
    call drandg(n,n,A,n)
    call drandg(n,1,u,n)
    call dcopy(n,u,1,wrk,1)
    ! generate A'*A and its Cholesky decomposition
    call dchgen(n,A,n,R,n)
    ! update the Cholesky decomposition
    call dch1up(n,R,n,u,wrk(1+n))
    ! downdate it back
    call dch1dn(n,R,n,wrk,wrk(1+n),info)
    ! check result
    call dchchk(n,A,n,R,n)

end subroutine

subroutine ctest(n)
    use iso_fortran_env
    integer n
    complex(real32) A(n,n),R(n,n),u(n),wrk(n)
    real(real32) rwrk(n)
    external crandg,ccopy,cchgen,cch1up,cchchk,cch1dn
    integer info
    ! set up random matrix & vectors
    call crandg(n,n,A,n)
    call crandg(n,1,u,n)
    call ccopy(n,u,1,wrk,1)
    ! generate A'*A and its Cholesky decomposition
    call cchgen(n,A,n,R,n)
    ! update the Cholesky decomposition
    call cch1up(n,R,n,u,rwrk)
    ! downdate it back
    call cch1dn(n,R,n,wrk,rwrk,info)
    ! check result
    call cchchk(n,A,n,R,n)

end subroutine

subroutine ztest(n)
    use iso_fortran_env
    integer n
    complex(real64) A(n,n),R(n,n),u(n),wrk(n)
    real(real64) rwrk(n)
    external zrandg,zcopy,zchgen,zch1up,zchchk,zch1dn
    integer info
    ! set up random matrix & vectors
    call zrandg(n,n,A,n)
    call zrandg(n,1,u,n)
    call zcopy(n,u,1,wrk,1)
    ! generate A'*A and its Cholesky decomposition
    call zchgen(n,A,n,R,n)
    ! update the Cholesky decomposition
    call zch1up(n,R,n,u,rwrk)
    ! downdate it back
    call zch1dn(n,R,n,wrk,rwrk,info)
    ! check result
    call zchchk(n,A,n,R,n)

    END
