eye Function

public pure function eye(dim1, dim2) result(result)

Constructs the identity matrix. (Specification)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: dim1
integer, intent(in), optional :: dim2

Return Value integer(kind=int8), allocatable, (:,:)


Source Code

    pure function eye(dim1, dim2) result(result)

        integer, intent(in) :: dim1
        integer, intent(in), optional :: dim2
        integer(int8), allocatable :: result(:, :)

        integer :: dim2_
        integer :: i

        dim2_ = optval(dim2, dim1)
        allocate(result(dim1, dim2_))
        
        result = 0_int8
        do i = 1, min(dim1, dim2_)
            result(i, i) = 1_int8
        end do

    end function eye