Compute the cofactors matrix (the transpose of the adjugate matrix).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | n |
size of |
||
real(kind=wp), | intent(in), | dimension(n,n) | :: | a |
the matrix |
the cofactors of a
matrix
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