-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_inv.m
30 lines (27 loc) · 944 Bytes
/
spm_inv.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function X = spm_inv(A,TOL)
% inverse for ill-conditioned matrices
% FORMAT X = spm_inv(A,TOL)
%
% A - matrix
% X - inverse
%
% TOL - tolerance: default = max(eps(norm(A,'inf'))*max(m,n),exp(-32))
%
% This routine simply adds a small diagonal matrix to A and calls inv.m
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_inv.m 7143 2017-07-29 18:50:38Z karl $
# SPDX-License-Identifier: GPL-2.0
% check A
%--------------------------------------------------------------------------
[m,n] = size(A);
if isempty(A), X = sparse(n,m); return, end
% tolerance
%--------------------------------------------------------------------------
if nargin == 1
TOL = max(eps(norm(A,'inf'))*max(m,n),exp(-32));
end
% inverse
%--------------------------------------------------------------------------
X = inv(A + speye(m,n)*TOL);