@@ -101,30 +101,58 @@ end program demo_diag5
101101
102102Experimental
103103
104+ ### Class
105+
106+ Pure function.
107+
104108### Description
105109
106- Construct the identity matrix
110+ Construct the identity matrix.
107111
108112### Syntax
109113
110- ` I = [[stdlib_linalg(module):eye(function)]](n ) `
114+ ` I = [[stdlib_linalg(module):eye(function)]](dim1 [, dim2] ) `
111115
112116### Arguments
113117
114- ` n ` : Shall be a scalar of default type ` integer ` .
118+ ` dim1 ` : Shall be a scalar of default type ` integer ` .
119+ This is an ` intent(in) ` argument.
120+
121+ ` dim2 ` : Shall be a scalar of default type ` integer ` .
122+ This is an ` intent(in) ` and ` optional ` argument.
115123
116124### Return value
117125
118- Returns the identity matrix, i.e. a square matrix with ones on the main diagonal and zeros elsewhere. The return value is of type ` integer(int8) ` .
126+ Return the identity matrix, i.e. a matrix with ones on the main diagonal and zeros elsewhere. The return value is of type ` integer(int8) ` .
127+ The use of ` int8 ` was suggested to save storage.
128+
129+ #### Warning
130+
131+ Since the result of ` eye ` is of ` integer(int8) ` type, one should be careful about using it in arithmetic expressions. For example:
132+ ``` fortran
133+ real :: A(:,:)
134+ !> Be careful
135+ A = eye(2,2)/2 !! A == 0.0
136+ !> Recommend
137+ A = eye(2,2)/2.0 !! A == diag([0.5, 0.5])
138+ ```
119139
120140### Example
121141
122142``` fortran
123143program demo_eye1
124144 use stdlib_linalg, only: eye
125145 implicit none
146+ integer :: i(2,2)
126147 real :: a(3,3)
127- A = eye(3)
148+ real :: b(2,3) !! Matrix is non-square.
149+ complex :: c(2,2)
150+ I = eye(2) !! [1,0; 0,1]
151+ A = eye(3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0]
152+ A = eye(3,3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0]
153+ B = eye(2,3) !! [1.0,0.0,0.0; 0.0,1.0,0.0]
154+ C = eye(2,2) !! [(1.0,0.0),(0.0,0.0); (0.0,0.0),(1.0,0.0)]
155+ C = (1.0,1.0)*eye(2,2) !! [(1.0,1.0),(0.0,0.0); (0.0,0.0),(1.0,1.0)]
128156end program demo_eye1
129157```
130158
0 commit comments