Skip to content

Commit 57e3f89

Browse files
committed
make eye default type: real(dp)
1 parent 4066365 commit 57e3f89

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,28 +249,24 @@ Constructs the identity matrix.
249249

250250
- `dim1`: A scalar of type `integer`. This is an `intent(in)` argument and specifies the number of rows.
251251
- `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 `integer(int8)` by default.
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.
253253

254254
### Return value
255255

256256
Returns the identity matrix, with ones on the main diagonal and zeros elsewhere.
257257

258-
- By default, the return value is of type `integer(int8)`, which is recommended for storage efficiency.
258+
- By default, the return value is of type `real(real64)`, which is recommended for arithmetic safety.
259259
- 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
262-
263-
When using the default `integer(int8)` type, be cautious when performing arithmetic operations, as integer division may occur. For example:
261+
### Example
264262

265263
```fortran
266-
!> Caution: default type is `integer`
267-
A = eye(2,2)/2 !! A == 0.0 due to integer division
268-
!> Recommend using a non-integer type for division
269-
A = eye(2,2, mold=1.0)/2 !! 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)])
270268
```
271269

272-
### Example
273-
274270
```fortran
275271
{!example/linalg/example_eye1.f90!}
276272
```

src/stdlib_linalg.fypp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ contains
13421342

13431343
integer, intent(in) :: dim1
13441344
integer, intent(in), optional :: dim2
1345-
${t1}$, intent(in) #{if 'int8' in t1}#, optional #{endif}#:: mold
1345+
${t1}$, intent(in) #{if t1 == 'real(dp)'}#, optional #{endif}#:: mold
13461346
${t1}$, allocatable :: result(:, :)
13471347

13481348
integer :: dim2_

0 commit comments

Comments
 (0)