Skip to content

Commit fcb5a50

Browse files
authored
linalg eye: allow generalized return type and kind (#902)
2 parents 35e7146 + 3d00b1d commit fcb5a50

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -239,37 +239,34 @@ Pure function.
239239

240240
### Description
241241

242-
Construct the identity matrix.
242+
Constructs the identity matrix.
243243

244244
### Syntax
245245

246-
`I = ` [[stdlib_linalg(module):eye(function)]] `(dim1 [, dim2])`
246+
`I = ` [[stdlib_linalg(module):eye(function)]] `(dim1 [, dim2] [, mold])`
247247

248248
### Arguments
249249

250-
`dim1`: Shall be a scalar of default type `integer`.
251-
This is an `intent(in)` argument.
252-
253-
`dim2`: Shall be a scalar of default type `integer`.
254-
This is an `intent(in)` and `optional` argument.
250+
- `dim1`: A scalar of type `integer`. This is an `intent(in)` argument and specifies the number of rows.
251+
- `dim2`: A scalar of type `integer`. This is an optional `intent(in)` argument specifying the number of columns. If not provided, the matrix is square (`dim1 = dim2`).
252+
- `mold`: A scalar of any supported `integer`, `real`, or `complex` type. This is an optional `intent(in)` argument. If provided, the returned identity matrix will have the same type and kind as `mold`. If not provided, the matrix will be of type `real(real64)` by default.
255253

256254
### Return value
257255

258-
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)`.
259-
The use of `int8` was suggested to save storage.
256+
Returns the identity matrix, with ones on the main diagonal and zeros elsewhere.
257+
258+
- By default, the return value is of type `real(real64)`, which is recommended for arithmetic safety.
259+
- If the `mold` argument is provided, the return value will match the type and kind of `mold`, allowing for arbitrary `integer`, `real`, or `complex` return types.
260260

261-
#### Warning
261+
### Example
262262

263-
Since the result of `eye` is of `integer(int8)` type, one should be careful about using it in arithmetic expressions. For example:
264263
```fortran
265-
!> Be careful
266-
A = eye(2,2)/2 !! A == 0.0
267-
!> Recommend
268-
A = eye(2,2)/2.0 !! A == diag([0.5, 0.5])
264+
!> Return default type (real64)
265+
A = eye(2,2)/2 !! A == diag([0.5_dp, 0.5_dp])
266+
!> Return 32-bit complex
267+
A = eye(2,2, mold=(0.0,0.0))/2 !! A == diag([(0.5,0.5), (0.5,0.5)])
269268
```
270269

271-
### Example
272-
273270
```fortran
274271
{!example/linalg/example_eye1.f90!}
275272
```

example/linalg/example_eye1.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ program example_eye1
55
real :: a(3, 3)
66
real :: b(2, 3) !! Matrix is non-square.
77
complex :: c(2, 2)
8-
I = eye(2) !! [1,0; 0,1]
9-
A = eye(3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0]
10-
A = eye(3, 3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0]
11-
B = eye(2, 3) !! [1.0,0.0,0.0; 0.0,1.0,0.0]
12-
C = eye(2, 2) !! [(1.0,0.0),(0.0,0.0); (0.0,0.0),(1.0,0.0)]
8+
I = eye(2) !! [1,0; 0,1]
9+
A = eye(3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0]
10+
A = eye(3, 3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0]
11+
B = eye(2, 3) !! [1.0,0.0,0.0; 0.0,1.0,0.0]
12+
C = eye(2, 2) !! [(1.0,0.0),(0.0,0.0); (0.0,0.0),(1.0,0.0)]
1313
C = (1.0, 1.0)*eye(2, 2) !! [(1.0,1.0),(0.0,0.0); (0.0,0.0),(1.0,1.0)]
1414
end program example_eye1

src/stdlib_linalg.fypp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ module stdlib_linalg
189189
#:endfor
190190
end interface
191191

192+
! Identity matrix
193+
interface eye
194+
!! version: experimental
195+
!!
196+
!! Constructs the identity matrix
197+
!! ([Specification](../page/specs/stdlib_linalg.html#eye-construct-the-identity-matrix))
198+
#:for k1, t1 in RCI_KINDS_TYPES
199+
module procedure eye_${t1[0]}$${k1}$
200+
#:endfor
201+
end interface eye
192202

193203
! Outer product (of two vectors)
194204
interface outer_product
@@ -1411,24 +1421,27 @@ contains
14111421
!>
14121422
!> Constructs the identity matrix.
14131423
!> ([Specification](../page/specs/stdlib_linalg.html#eye-construct-the-identity-matrix))
1414-
pure function eye(dim1, dim2) result(result)
1424+
#:for k1, t1 in RCI_KINDS_TYPES
1425+
pure function eye_${t1[0]}$${k1}$(dim1, dim2, mold) result(result)
14151426

14161427
integer, intent(in) :: dim1
14171428
integer, intent(in), optional :: dim2
1418-
integer(int8), allocatable :: result(:, :)
1429+
${t1}$, intent(in) #{if t1 == 'real(dp)'}#, optional #{endif}#:: mold
1430+
${t1}$, allocatable :: result(:, :)
14191431

14201432
integer :: dim2_
14211433
integer :: i
14221434

14231435
dim2_ = optval(dim2, dim1)
14241436
allocate(result(dim1, dim2_))
14251437

1426-
result = 0_int8
1438+
result = 0
14271439
do i = 1, min(dim1, dim2_)
1428-
result(i, i) = 1_int8
1440+
result(i, i) = 1
14291441
end do
14301442

1431-
end function eye
1443+
end function eye_${t1[0]}$${k1}$
1444+
#:endfor
14321445

14331446
#:for k1, t1 in RCI_KINDS_TYPES
14341447
function trace_${t1[0]}$${k1}$(A) result(res)

0 commit comments

Comments
 (0)