@@ -82,20 +82,28 @@ module stdlib_linalg
8282
8383contains
8484
85- function eye(n) result(res)
86- !! version: experimental
87- !!
88- !! Constructs the identity matrix
89- !! ([Specification](../page/specs/stdlib_linalg.html#description_1))
90- integer, intent(in) :: n
91- integer(int8) :: res(n, n)
92- integer :: i
93- res = 0
94- do i = 1, n
95- res(i, i) = 1
96- end do
97- end function eye
85+ !> Version: experimental
86+ !>
87+ !> Constructs the identity matrix.
88+ !> ([Specification](../page/specs/stdlib_linalg.html#eye-construct-the-identity-matrix))
89+ pure function eye(dim1, dim2) result(result)
90+
91+ integer, intent(in) :: dim1
92+ integer, intent(in), optional :: dim2
93+ integer, allocatable :: result(:, :)
94+
95+ integer :: dim2_
96+ integer :: i
9897
98+ dim2_ = merge(dim2, dim1, present(dim2))
99+ allocate(result(dim1, dim2_))
100+
101+ result = 0
102+ do i = 1, min(dim1, dim2_)
103+ result(i, i) = 1
104+ end do
105+
106+ end function eye
99107
100108 #:for k1, t1 in RCI_KINDS_TYPES
101109 function trace_${t1[0]}$${k1}$(A) result(res)
@@ -108,4 +116,5 @@ contains
108116 end do
109117 end function trace_${t1[0]}$${k1}$
110118 #:endfor
111- end module
119+
120+ end module stdlib_linalg
0 commit comments