!===============================================================================
!
! matinv.F90 - inverse of small matrices
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 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/>.
!
!-------------------------------------------------------------------------------

FOR type in real complex
SET _t = $(ifthenelse '[[ type = real ]]' '_r' '_c')
FOR rkind in 4 8

!------------------------------------------------------------------------------
subroutine matinv2%%_t%%rkind(out, in)

  implicit none
  type(rkind), intent(out) :: out(2, 2)
  type(rkind), intent(in) :: in(2, 2)
  real(rkind) :: det

  det = 1.0_%%rkind / (in(1, 1) * in(2, 2) - in(1, 2) * in(2, 1))

  out(1, 1) =  in(2, 2) * det
  out(2, 1) = -in(2, 1) * det
  out(1, 2) = -in(1, 2) * det
  out(2, 2) =  in(1, 1) * det
end

ENDFOR
ENDSET
ENDFOR
!==============================================================================
