Skip to content

Commit d0af9be

Browse files
committed
operator(.inv.): return NaNs on exceptions
1 parent 505f30a commit d0af9be

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,8 @@ This interface is equivalent to the function version of [[stdlib_linalg(module)
10151015

10161016
Returns a rank-2 square array with the same type, kind and rank as `a`, that contains the inverse of `a`.
10171017

1018-
Exceptions always trigger an `error stop`.
1018+
If an exception occurred on input errors, or singular matrix, NaNs will be returned.
1019+
10191020

10201021
### Example
10211022

src/stdlib_linalg.fypp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ module stdlib_linalg
655655
!!### Description
656656
!!
657657
!! This operator interface provides a convenient way to compute the inverse of a matrix.
658-
!! Supported data types include `real` and `complex`.
658+
!! Supported data types include `real` and `complex`. On input errors or singular matrix,
659+
!! NaNs will be returned.
659660
!!
660661
!!@note The provided functions are intended for square matrices.
661662
!!@note BLAS/LAPACK backends do not currently support extended precision (``xdp``).

src/stdlib_linalg_inverse.fypp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ submodule (stdlib_linalg) stdlib_linalg_inverse
66
use stdlib_linalg_lapack, only: getri,getrf,stdlib_ilaenv
77
use stdlib_linalg_state, only: linalg_state_type, linalg_error_handling, LINALG_ERROR, &
88
LINALG_INTERNAL_ERROR, LINALG_VALUE_ERROR
9+
use ieee_arithmetic, only: ieee_value, ieee_quiet_nan
910
implicit none(type,external)
1011

1112
character(*), parameter :: this = 'inverse'
@@ -153,9 +154,17 @@ submodule (stdlib_linalg) stdlib_linalg_inverse
153154
${rt}$, intent(in) :: a(:,:)
154155
!> Result matrix
155156
${rt}$, allocatable :: inva(:,:)
157+
158+
type(linalg_state_type) :: err
156159

157-
! Do not provide an error handler (error stop on issues)
158-
inva = stdlib_linalg_inverse_${ri}$(a)
160+
! Provide an error handler to return NaNs on issues
161+
inva = stdlib_linalg_inverse_${ri}$(a,err=err)
162+
163+
! Return NaN on issues
164+
if (err%error()) then
165+
if (allocated(inva)) deallocate(inva)
166+
allocate(inva(size(a,1,kind=ilp),size(a,2,kind=ilp)),source=ieee_value(1.0_${rk}$,ieee_quiet_nan))
167+
endif
159168

160169
end function stdlib_linalg_inverse_${ri}$_operator
161170

0 commit comments

Comments
 (0)