-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_diff.m
192 lines (162 loc) · 5.32 KB
/
spm_diff.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
function [varargout] = spm_diff(varargin)
% Matrix high-order numerical differentiation
% FORMAT [dfdx] = spm_diff(f,x,...,n)
% FORMAT [dfdx] = spm_diff(f,x,...,n,V)
% FORMAT [dfdx] = spm_diff(f,x,...,n,'q')
%
% f - [name or handle] function f(x{1},...)
% x - input argument[s]
% n - arguments to differentiate w.r.t.
%
% V - cell array of matrices that allow for differentiation w.r.t.
% to a linear transformation of the parameters: i.e., returns
%
% df/dy{i}; x = V{i}y{i}; V = dx(i)/dy(i)
%
% q - (char) flag to preclude default concatenation of dfdx
%
% dfdx - df/dx{i} ; n = i
% dfdx{p}...{q} - df/dx{i}dx{j}(q)...dx{k}(p) ; n = [i j ... k]
%
%
% This routine has the same functionality as spm_ddiff, however it uses one
% sample point to approximate gradients with numerical (finite)
% differences:
%
% dfdx = (f(x + dx)- f(x))/dx
%__________________________________________________________________________
% Copyright (C) 2003-2020 Wellcome Centre for Human Neuroimaging
% Karl Friston
% $Id: spm_diff.m 7977 2020-10-07 15:29:54Z guillaume $
# SPDX-License-Identifier: GPL-2.0
% step size for numerical derivatives
%--------------------------------------------------------------------------
global GLOBAL_DX
if ~isempty(GLOBAL_DX)
dx = GLOBAL_DX;
else
dx = exp(-8);
end
% create function handle
%--------------------------------------------------------------------------
f = spm_funcheck(varargin{1});
% parse input arguments
%--------------------------------------------------------------------------
if iscell(varargin{end})
x = varargin(2:(end - 2));
n = varargin{end - 1};
V = varargin{end};
q = true;
elseif isnumeric(varargin{end})
x = varargin(2:(end - 1));
n = varargin{end};
V = cell(1,length(x));
q = true;
elseif ischar(varargin{end})
x = varargin(2:(end - 2));
n = varargin{end - 1};
V = cell(1,length(x));
q = false;
else
error('Improper call.')
end
% check transform matrices V = dxdy
%--------------------------------------------------------------------------
for i = 1:length(x)
try
V{i};
catch
V{i} = [];
end
if isempty(V{i}) && any(n == i)
V{i} = speye(spm_length(x{i}));
end
end
% initialise
%--------------------------------------------------------------------------
m = n(end);
xm = spm_vec(x{m});
J = cell(1,size(V{m},2));
% proceed to derivatives
%==========================================================================
if length(n) == 1
% dfdx
%----------------------------------------------------------------------
f0 = f(x{:});
for i = 1:length(J)
xi = x;
xi{m} = spm_unvec(xm + V{m}(:,i)*dx,x{m});
J{i} = spm_dfdx(f(xi{:}),f0,dx);
end
% return numeric array for first-order derivatives
%======================================================================
% vectorise f
%----------------------------------------------------------------------
f = spm_vec(f0);
% if there are no arguments to differentiate w.r.t. ...
%----------------------------------------------------------------------
if isempty(xm)
J = sparse(length(f),0);
% or there are no arguments to differentiate
%----------------------------------------------------------------------
elseif isempty(f)
J = sparse(0,length(xm));
end
% differentiation of a scalar or vector
%----------------------------------------------------------------------
if isnumeric(f0) && iscell(J) && q
J = spm_dfdx_cat(J);
end
% assign output argument and return
%----------------------------------------------------------------------
varargout = {J, f0};
else
% dfdxdxdx....
%----------------------------------------------------------------------
f0 = cell(1,length(n));
[f0{:}] = spm_diff(f,x{:},n(1:end - 1),V);
p = true;
for i = 1:length(J)
xi = x;
xmi = xm + V{m}(:,i)*dx;
xi{m} = spm_unvec(xmi,x{m});
fi = spm_diff(f,xi{:},n(1:end - 1),V);
J{i} = spm_dfdx(fi,f0{1},dx);
p = p & isnumeric(J{i});
end
% or differentiation of a scalar or vector
%----------------------------------------------------------------------
if p && q
J = spm_dfdx_cat(J);
end
% assign output argument and return
%----------------------------------------------------------------------
varargout = [{J}, f0];
end
%==========================================================================
% function dfdx = spm_dfdx(f,f0,dx)
%==========================================================================
function dfdx = spm_dfdx(f,f0,dx)
% numerical differences
if iscell(f)
dfdx = f;
for i = 1:length(f(:))
dfdx{i} = spm_dfdx(f{i},f0{i},dx);
end
elseif isstruct(f)
dfdx = (spm_vec(f) - spm_vec(f0))/dx;
else
dfdx = (f - f0)/dx;
end
%==========================================================================
% function J = spm_dfdx_cat(J)
%==========================================================================
function J = spm_dfdx_cat(J)
% concatenate into a matrix
if isvector(J{1})
if size(J{1},2) == 1
J = spm_cat(J);
else
J = spm_cat(J')';
end
end