-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_expm.m
75 lines (65 loc) · 1.94 KB
/
spm_expm.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function [x] = spm_expm(J,x)
% approximate matrix exponential using a Taylor expansion
% FORMAT [y] = spm_expm(J,x)
% FORMAT [y] = spm_expm(J)
% y = expm(J)*x:
% y = expm(J);
%
% This routine covers and extends expm functionality by using a
% comoutationally expedient approximation that can handle sparse
% matrices when dealing with the special case of expm(J)*x, where x
% is a vector, in an efficient fashion
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_expm.m 5691 2013-10-11 16:53:00Z karl $
# SPDX-License-Identifier: GPL-2.0
% % standard [eigen]solution
% %--------------------------------------------------------------------------
% [V,D] = eig(full(J));
% E = V*diag(exp(diag(D)))/V;
%
% % Multiply by x if necessary
% %--------------------------------------------------------------------------
% if nargin > 1, x = E*x; else, x = E; end
% expm(J) use Pade approximation
%======================================================================
% ensure norm is < 1/2 by scaling by power of 2
%----------------------------------------------------------------------
I = speye(size(J));
[f,e] = log2(norm(J,'inf'));
s = max(0,e + 1);
J = J/2^s;
X = J;
c = 1/2;
E = I + c*J;
D = I - c*J;
q = 6;
p = 1;
for k = 2:q
c = c*(q - k + 1)/(k*(2*q - k + 1));
X = J*X;
cX = c*X;
E = E + cX;
if p
D = D + cX;
else
D = D - cX;
end
p = ~p;
end
% E = inv(D)*E
%--------------------------------------------------------------------------
E = D\E;
% Undo scaling by repeated squaring E = E^(2^s)
%--------------------------------------------------------------------------
for k = 1:s
E = E*E;
end
% Multiply by x if necessary
%--------------------------------------------------------------------------
if nargin > 1
x = E*x;
else
x = E;
end