!===============================================================================
!
! cg_mbcg2_get_sxi.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2012 Yoshifumi Nakamura
!               2016 Hinnerk Stueben
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD 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.
!
! BQCD 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 BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
subroutine cg_mbcg2_get_sxi(sxi2, sxinv, sxi1, sxi0, alp0, alp, rho0, rho, shift)

  implicit none
  integer, parameter :: nr = 2
  COMPLEX, dimension(nr, nr), intent(out) :: sxi2, sxinv
  COMPLEX, dimension(nr, nr), intent(in)  :: sxi1, sxi0, alp0, alp, rho0, rho
  REAL, intent(in)                        :: shift

  COMPLEX, dimension(nr, nr) :: tmp0, tmp1, tmp2

  tmp0(1, 1) = conjg(rho0(1, 1))
  tmp0(2, 1) = conjg(rho0(1, 2))
  tmp0(1, 2) = conjg(rho0(2, 1))
  tmp0(2, 2) = conjg(rho0(2, 2))

  call STRCAT(matinv2_c,    RKIND)(tmp1, alp0)
  call STRCAT(matmul2_nn_c, RKIND)(tmp2, tmp1, tmp0)

  call STRCAT(matinv2_c,    RKIND)(tmp1, sxi0)
  call STRCAT(matmul2_nn_c, RKIND)(tmp0, sxi1, tmp1)

  tmp0 = rho0 - tmp0
  call STRCAT(matmul2_nn_c, RKIND)(tmp1, tmp0, tmp2)
  call STRCAT(matmul2_nn_c, RKIND)(tmp2, tmp1, alp)

  sxinv = shift * alp + tmp2
  sxinv(1,1) = sxinv(1,1) + ONE
  sxinv(2,2) = sxinv(2,2) + ONE
  tmp1 = sxinv
  call STRCAT(matinv2_c,    RKIND)(sxinv, tmp1)
  call STRCAT(matmul2_nn_c, RKIND)(tmp0, rho, sxinv)
  call STRCAT(matmul2_nn_c, RKIND)(sxi2, tmp0, sxi1)

end

!===============================================================================
