matrix_cofactor Function

public pure function matrix_cofactor(n, a) result(c)

Compute the cofactors matrix (the transpose of the adjugate matrix).

References

  • https://warwick.ac.uk/fac/sci/physics/research/condensedmatt/imr_cdt/students/david_goodwin/teaching/cis008-2/determinant_algorithm_cis008-2_lec_21.pdf
  • https://groups.google.com/forum/#!topic/comp.lang.fortran/Y6jCv-QdDhc

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n

size of a matrix

real(kind=wp), intent(in), dimension(n,n) :: a

the matrix

Return Value real(kind=wp), dimension(n,n)

the cofactors of a matrix


Calls

proc~~matrix_cofactor~~CallsGraph proc~matrix_cofactor matrix_module::matrix_cofactor proc~matrix_determinant matrix_module::matrix_determinant proc~matrix_cofactor->proc~matrix_determinant proc~matrix_determinant->proc~matrix_determinant

Source Code

    pure function matrix_cofactor (n,a) result(c)

    implicit none

    integer,intent(in)                  :: n   !! size of `a` matrix
    real(wp),dimension(n,n),intent(in)  :: a   !! the matrix
    real(wp),dimension(n,n)             :: c   !! the cofactors of `a` matrix

    integer :: i !! counter
    integer :: j !! counter
    real(wp),dimension(n-1,n-1) :: c_temp  !! temp matrix
    logical,dimension(n,n) :: m  !! mask for row/col removal

    if (n==1) then
        c(1,1) = one
    else
        do i=1,n
            do j=1,n
                ! remove the ith row and jth col from a:
                m = .true.
                m(:,j) = .false.
                m(i,:) = .false.
                c_temp = reshape(pack(a, m),[n-1,n-1])
                c(i,j) = ( (-1)**(i+j) ) * matrix_determinant(n-1,c_temp)
            end do
        end do
    end if

    end function matrix_cofactor