@@ -101,30 +101,58 @@ end program demo_diag5
101
101
102
102
Experimental
103
103
104
+ ### Class
105
+
106
+ Pure function.
107
+
104
108
### Description
105
109
106
- Construct the identity matrix
110
+ Construct the identity matrix.
107
111
108
112
### Syntax
109
113
110
- ` I = [[stdlib_linalg(module):eye(function)]](n ) `
114
+ ` I = [[stdlib_linalg(module):eye(function)]](dim1 [, dim2] ) `
111
115
112
116
### Arguments
113
117
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.
115
123
116
124
### Return value
117
125
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
+ ```
119
139
120
140
### Example
121
141
122
142
``` fortran
123
143
program demo_eye1
124
144
use stdlib_linalg, only: eye
125
145
implicit none
146
+ integer :: i(2,2)
126
147
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)]
128
156
end program demo_eye1
129
157
```
130
158
0 commit comments