Skip to content

Commit 79be1e5

Browse files
committed
Merge branch 'sparse_sellc' of https://github.com/jalvesz/stdlib into sparse_sellc
2 parents 1d25a61 + 3463c43 commit 79be1e5

12 files changed

+841
-34
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 181 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
```
@@ -509,6 +506,37 @@ Returns a `logical` scalar that is `.true.` if the input matrix is skew-symmetri
509506
{!example/linalg/example_is_skew_symmetric.f90!}
510507
```
511508

509+
## `hermitian` - Compute the Hermitian version of a rank-2 matrix
510+
511+
### Status
512+
513+
Experimental
514+
515+
### Description
516+
517+
Compute the Hermitian version of a rank-2 matrix.
518+
For `complex` matrices, the function returns the conjugate transpose (`conjg(transpose(a))`).
519+
For `real` or `integer` matrices, the function returns the transpose (`transpose(a)`).
520+
521+
### Syntax
522+
523+
`h = ` [[stdlib_linalg(module):hermitian(interface)]] `(a)`
524+
525+
### Arguments
526+
527+
`a`: Shall be a rank-2 array of type `integer`, `real`, or `complex`. The input matrix `a` is not modified.
528+
529+
### Return value
530+
531+
Returns a rank-2 array of the same shape and type as `a`. If `a` is of type `complex`, the Hermitian matrix is computed as `conjg(transpose(a))`.
532+
For `real` or `integer` types, it is equivalent to the intrinsic `transpose(a)`.
533+
534+
### Example
535+
536+
```fortran
537+
{!example/linalg/example_hermitian.f90!}
538+
```
539+
512540
## `is_hermitian` - Checks if a matrix is Hermitian
513541

514542
### Status
@@ -1459,6 +1487,142 @@ If `err` is not present, exceptions trigger an `error stop`.
14591487
{!example/linalg/example_inverse_function.f90!}
14601488
```
14611489

1490+
## `pinv` - Moore-Penrose pseudo-inverse of a matrix
1491+
1492+
### Status
1493+
1494+
Experimental
1495+
1496+
### Description
1497+
1498+
This function computes the Moore-Penrose pseudo-inverse of a `real` or `complex` matrix.
1499+
The pseudo-inverse, \( A^{+} \), generalizes the matrix inverse and satisfies the conditions:
1500+
- \( A \cdot A^{+} \cdot A = A \)
1501+
- \( A^{+} \cdot A \cdot A^{+} = A^{+} \)
1502+
- \( (A \cdot A^{+})^T = A \cdot A^{+} \)
1503+
- \( (A^{+} \cdot A)^T = A^{+} \cdot A \)
1504+
1505+
The computation is based on singular value decomposition (SVD). Singular values below a relative
1506+
tolerance threshold \( \text{rtol} \cdot \sigma_{\max} \), where \( \sigma_{\max} \) is the largest
1507+
singular value, are treated as zero.
1508+
1509+
### Syntax
1510+
1511+
`b =` [[stdlib_linalg(module):pinv(interface)]] `(a, [, rtol, err])`
1512+
1513+
### Arguments
1514+
1515+
`a`: Shall be a rank-2, `real` or `complex` array of shape `[m, n]` containing the coefficient matrix.
1516+
It is an `intent(in)` argument.
1517+
1518+
`rtol` (optional): Shall be a scalar `real` value specifying the relative tolerance for singular value cutoff.
1519+
If `rtol` is not provided, the default relative tolerance is \( \text{rtol} = \text{max}(m, n) \cdot \epsilon \),
1520+
where \( \epsilon \) is the machine precision for the element type of `a`. It is an `intent(in)` argument.
1521+
1522+
`err` (optional): Shall be a `type(linalg_state_type)` value. It is an `intent(out)` argument.
1523+
1524+
### Return value
1525+
1526+
Returns an array value of the same type, kind, and rank as `a` with shape `[n, m]`, that contains the pseudo-inverse matrix \( A^{+} \).
1527+
1528+
Raises `LINALG_ERROR` if the underlying SVD did not converge.
1529+
Raises `LINALG_VALUE_ERROR` if `a` has invalid size.
1530+
If `err` is not present, exceptions trigger an `error stop`.
1531+
1532+
### Example
1533+
1534+
```fortran
1535+
{!example/linalg/example_pseudoinverse.f90!}
1536+
```
1537+
1538+
## `pseudoinvert` - Moore-Penrose pseudo-inverse of a matrix
1539+
1540+
### Status
1541+
1542+
Experimental
1543+
1544+
### Description
1545+
1546+
This subroutine computes the Moore-Penrose pseudo-inverse of a `real` or `complex` matrix.
1547+
The pseudo-inverse \( A^{+} \) is a generalization of the matrix inverse and satisfies the following properties:
1548+
- \( A \cdot A^{+} \cdot A = A \)
1549+
- \( A^{+} \cdot A \cdot A^{+} = A^{+} \)
1550+
- \( (A \cdot A^{+})^T = A \cdot A^{+} \)
1551+
- \( (A^{+} \cdot A)^T = A^{+} \cdot A \)
1552+
1553+
The computation is based on singular value decomposition (SVD). Singular values below a relative
1554+
tolerance threshold \( \text{rtol} \cdot \sigma_{\max} \), where \( \sigma_{\max} \) is the largest
1555+
singular value, are treated as zero.
1556+
1557+
On return, matrix `pinva` `[n, m]` will store the pseudo-inverse of `a` `[m, n]`.
1558+
1559+
### Syntax
1560+
1561+
`call ` [[stdlib_linalg(module):pseudoinvert(interface)]] `(a, pinva [, rtol] [, err])`
1562+
1563+
### Arguments
1564+
1565+
`a`: Shall be a rank-2, `real` or `complex` array containing the coefficient matrix.
1566+
It is an `intent(in)` argument.
1567+
1568+
`pinva`: Shall be a rank-2 array of the same kind as `a`, and size equal to that of `transpose(a)`.
1569+
On output, it contains the Moore-Penrose pseudo-inverse of `a`.
1570+
1571+
`rtol` (optional): Shall be a scalar `real` value specifying the relative tolerance for singular value cutoff.
1572+
If not provided, the default threshold is \( \text{max}(m, n) \cdot \epsilon \), where \( \epsilon \) is the
1573+
machine precision for the element type of `a`.
1574+
1575+
`err` (optional): Shall be a `type(linalg_state_type)` value. It is an `intent(out)` argument.
1576+
1577+
### Return value
1578+
1579+
Computes the Moore-Penrose pseudo-inverse of the matrix \( A \), \( A^{+} \), and returns it in matrix `pinva`.
1580+
1581+
Raises `LINALG_ERROR` if the underlying SVD did not converge.
1582+
Raises `LINALG_VALUE_ERROR` if `pinva` and `a` have degenerate or incompatible sizes.
1583+
If `err` is not present, exceptions trigger an `error stop`.
1584+
1585+
### Example
1586+
1587+
```fortran
1588+
{!example/linalg/example_pseudoinverse.f90!}
1589+
```
1590+
1591+
## `.pinv.` - Moore-Penrose Pseudo-Inverse operator
1592+
1593+
### Status
1594+
1595+
Experimental
1596+
1597+
### Description
1598+
1599+
This operator returns the Moore-Penrose pseudo-inverse of a `real` or `complex` matrix \( A \).
1600+
The pseudo-inverse \( A^{+} \) is computed using Singular Value Decomposition (SVD), and singular values
1601+
below a given threshold are treated as zero.
1602+
1603+
This interface is equivalent to the function [[stdlib_linalg(module):pinv(interface)]].
1604+
1605+
### Syntax
1606+
1607+
`b = ` [[stdlib_linalg(module):operator(.pinv.)(interface)]] `a`
1608+
1609+
### Arguments
1610+
1611+
`a`: Shall be a rank-2 array of any `real` or `complex` kinds, with arbitrary dimensions \( m \times n \). It is an `intent(in)` argument.
1612+
1613+
### Return value
1614+
1615+
Returns a rank-2 array with the same type, kind, and rank as `a`, that contains the Moore-Penrose pseudo-inverse of `a`.
1616+
1617+
If an exception occurs, or if the input matrix is degenerate (e.g., rank-deficient), the returned matrix will contain `NaN`s.
1618+
For more detailed error handling, it is recommended to use the `subroutine` or `function` interfaces.
1619+
1620+
### Example
1621+
1622+
```fortran
1623+
{!example/linalg/example_pseudoinverse.f90!}
1624+
```
1625+
14621626
## `get_norm` - Computes the vector norm of a generic-rank array.
14631627

14641628
### Status

example/linalg/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ADD_EXAMPLE(diag5)
66
ADD_EXAMPLE(eye1)
77
ADD_EXAMPLE(eye2)
88
ADD_EXAMPLE(is_diagonal)
9+
ADD_EXAMPLE(hermitian)
910
ADD_EXAMPLE(is_hermitian)
1011
ADD_EXAMPLE(is_hessenberg)
1112
ADD_EXAMPLE(is_skew_symmetric)
@@ -16,6 +17,7 @@ ADD_EXAMPLE(inverse_operator)
1617
ADD_EXAMPLE(inverse_function)
1718
ADD_EXAMPLE(inverse_inplace)
1819
ADD_EXAMPLE(inverse_subroutine)
20+
ADD_EXAMPLE(pseudoinverse)
1921
ADD_EXAMPLE(outer_product)
2022
ADD_EXAMPLE(eig)
2123
ADD_EXAMPLE(eigh)

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

example/linalg/example_hermitian.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! Example program demonstrating the usage of hermitian
2+
program example_hermitian
3+
use stdlib_linalg, only: hermitian
4+
implicit none
5+
6+
complex, dimension(2, 2) :: A, AT
7+
8+
! Define input matrices
9+
A = reshape([(1,2),(3,4),(5,6),(7,8)],[2,2])
10+
11+
! Compute Hermitian matrices
12+
AT = hermitian(A)
13+
14+
print *, "Original Complex Matrix:"
15+
print *, A
16+
print *, "Hermitian Complex Matrix:"
17+
print *, AT
18+
19+
end program example_hermitian
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
! Matrix pseudo-inversion example: function, subroutine, and operator interfaces
2+
program example_pseudoinverse
3+
use stdlib_linalg, only: pinv, pseudoinvert, operator(.pinv.), linalg_state_type
4+
implicit none(type,external)
5+
6+
real :: A(15,5), Am1(5,15)
7+
type(linalg_state_type) :: state
8+
integer :: i, j
9+
real, parameter :: tol = sqrt(epsilon(0.0))
10+
11+
! Generate random matrix A (15x15)
12+
call random_number(A)
13+
14+
! Pseudo-inverse: Function interfcae
15+
Am1 = pinv(A, err=state)
16+
print *, 'Max error (function) : ', maxval(abs(A-matmul(A, matmul(Am1,A))))
17+
18+
! User threshold
19+
Am1 = pinv(A, rtol=0.001, err=state)
20+
print *, 'Max error (rtol=0.001): ', maxval(abs(A-matmul(A, matmul(Am1,A))))
21+
22+
! Pseudo-inverse: Subroutine interface
23+
call pseudoinvert(A, Am1, err=state)
24+
25+
print *, 'Max error (subroutine): ', maxval(abs(A-matmul(A, matmul(Am1,A))))
26+
27+
! Operator interface
28+
Am1 = .pinv.A
29+
30+
print *, 'Max error (operator) : ', maxval(abs(A-matmul(A, matmul(Am1,A))))
31+
32+
end program example_pseudoinverse

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(fppFiles
3232
stdlib_linalg_determinant.fypp
3333
stdlib_linalg_qr.fypp
3434
stdlib_linalg_inverse.fypp
35+
stdlib_linalg_pinv.fypp
3536
stdlib_linalg_norms.fypp
3637
stdlib_linalg_state.fypp
3738
stdlib_linalg_svd.fypp

src/stdlib_io.fypp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ contains
167167
read (s,*,iostat=ios,iomsg=iomsg) d(i, :)
168168

169169
if (ios/=0) then
170-
write(msgout,1) trim(iomsg),i,trim(filename)
170+
write(msgout,1) trim(iomsg),size(d,2),i,trim(filename)
171171
call error_stop(msg=trim(msgout))
172172
end if
173173

@@ -178,7 +178,7 @@ contains
178178
read (s,fmt_,iostat=ios,iomsg=iomsg) d(i, :)
179179

180180
if (ios/=0) then
181-
write(msgout,1) trim(iomsg),i,trim(filename)
181+
write(msgout,1) trim(iomsg),size(d,2),i,trim(filename)
182182
call error_stop(msg=trim(msgout))
183183
end if
184184

@@ -187,7 +187,7 @@ contains
187187

188188
close(s)
189189

190-
1 format('loadtxt: error <',a,'> reading line ',i0,' of ',a,'.')
190+
1 format('loadtxt: error <',a,'> reading ',i0,' values from line ',i0,' of ',a,'.')
191191

192192
end subroutine loadtxt_${t1[0]}$${k1}$
193193
#:endfor
@@ -230,14 +230,14 @@ contains
230230
iostat=ios,iomsg=iomsg) d(i, :)
231231

232232
if (ios/=0) then
233-
write(msgout,1) trim(iomsg),i,trim(filename)
233+
write(msgout,1) trim(iomsg),size(d,2),i,trim(filename)
234234
call error_stop(msg=trim(msgout))
235235
end if
236236

237237
end do
238238
close(s)
239239

240-
1 format('savetxt: error <',a,'> writing line ',i0,' of ',a,'.')
240+
1 format('savetxt: error <',a,'> writing ',i0,' values to line ',i0,' of ',a,'.')
241241

242242
end subroutine savetxt_${t1[0]}$${k1}$
243243
#:endfor

0 commit comments

Comments
 (0)