/usr/share/octave/packages/optim-1.4.0/private/__dfdp__.m is in octave-optim 1.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | ## Copyright (C) 1992-1994 Richard Shrager
## Copyright (C) 1992-1994 Arthur Jutan
## Copyright (C) 1992-1994 Ray Muzic
## Copyright (C) 2010-2013 Olaf Till <i7tiol@t-online.de>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
function prt = __dfdp__ (p, func, hook)
## Meant to be called by interfaces "dfxpdp.m" and "dfpdp.m", see there.
if (nargin > 2 && isfield (hook, "f"))
f = hook.f;
else
f = func (p);
f = f(:);
endif
m = length (f);
n = length (p);
if (nargin > 2)
if (isfield (hook, "fixed"))
fixed = hook.fixed;
else
fixed = false (n, 1);
endif
if (isfield (hook, "diffp"))
diffp = hook.diffp;
else
diffp = .001 * ones (n, 1);
endif
if (isfield (hook, "diff_onesided"))
diff_onesided = hook.diff_onesided;
else
diff_onesided = false (n, 1);
endif
if (isfield (hook, "lbound"))
lbound = hook.lbound;
else
lbound = - Inf (n, 1);
endif
if (isfield (hook, "ubound"))
ubound = hook.ubound;
else
ubound = Inf (n, 1);
endif
if (isfield (hook, "plabels"))
plabels = hook.plabels;
else
plabels = num2cell (num2cell ((1:n).'));
endif
if (isfield (hook, "parallel_local"))
parallel_local = hook.parallel_local;
else
parallel_local = false;
endif
else
fixed = false (n, 1);
diff_onesided = fixed;
diffp = .001 * ones (n, 1);
lbound = - Inf (n, 1);
ubound = Inf (n, 1);
plabels = num2cell (num2cell ((1:n).'));
parallel_local = false;
endif
prt = zeros (m, n); # initialise Jacobian to Zero
del = diffp .* p;
idxa = p == 0;
del(idxa) = diffp(idxa);
del(diff_onesided) = - del(diff_onesided); # keep course of
# optimization of previous versions
absdel = abs (del);
idxd = ~(diff_onesided | fixed); # double sided interval
p1 = zeros (n, 1);
p2 = p1;
idxvs = false (n, 1);
idx1g2w = idxvs;
idx1le2w = idxvs;
## p may be slightly out of bounds due to inaccuracy, or exactly at
## the bound -> single sided interval
idxvl = p <= lbound;
idxvg = p >= ubound;
p1(idxvl) = min (p(idxvl, 1) + absdel(idxvl, 1), ubound(idxvl, 1));
idxd(idxvl) = false;
p1(idxvg) = max (p(idxvg, 1) - absdel(idxvg, 1), lbound(idxvg, 1));
idxd(idxvg) = false;
idxs = ~(fixed | idxd); # single sided interval
idxnv = ~(idxvl | idxvg); # current paramters within bounds
idxnvs = idxs & idxnv; # within bounds, single sided interval
idxnvd = idxd & idxnv; # within bounds, double sided interval
## remaining single sided intervals
p1(idxnvs) = p(idxnvs) + del(idxnvs); # don't take absdel, this could
# change course of optimization without
# bounds with respect to previous
# versions
## remaining single sided intervals, violating a bound -> take largest
## possible direction of single sided interval
idxvs(idxnvs) = p1(idxnvs, 1) < lbound(idxnvs, 1) | ...
p1(idxnvs, 1) > ubound(idxnvs, 1);
del1 = p(idxvs, 1) - lbound(idxvs, 1);
del2 = ubound(idxvs, 1) - p(idxvs, 1);
idx1g2 = del1 > del2;
idx1g2w(idxvs) = idx1g2;
idx1le2w(idxvs) = ~idx1g2;
p1(idx1g2w) = max (p(idx1g2w, 1) - absdel(idx1g2w, 1), ...
lbound(idx1g2w, 1));
p1(idx1le2w) = min (p(idx1le2w, 1) + absdel(idx1le2w, 1), ...
ubound(idx1le2w, 1));
## double sided interval
p1(idxnvd) = min (p(idxnvd, 1) + absdel(idxnvd, 1), ...
ubound(idxnvd, 1));
p2(idxnvd) = max (p(idxnvd, 1) - absdel(idxnvd, 1), ...
lbound(idxnvd, 1));
del(idxs) = p1(idxs) - p(idxs);
del(idxd) = p1(idxd) - p2(idxd);
info.f = f;
info.parallel = parallel_local;
if (parallel_local)
## symplicistic approach, fork for each computation and leave all
## scheduling to kernel; otherwise arguments would have to be passed
## over pipes, not sure whether this would be faster
n_ss = sum (idxs);
n_ds = sum (idxd);
n_childs = n_ss + 2 * n_ds;
child_data = zeros (n_childs, 5); # pipe desriptor for reading, pid,
# side (zero for one-sided), line
# number, parameter number
child_data(:, 4) = 1 : n_childs;
active_childs = true (n_childs, 1);
tp_ss = zeros (m, n); # results for single sided
tp_ds = zeros (m, n, 2); # results for double sided
unwind_protect
ready = false;
lerrm = lasterr ();
lasterr ("");
cid = 0;
for j = 1:n
if (! fixed(j))
cid++;
child_data(cid, 5) = j;
info.plabels = plabels(j, :);
ps = p;
ps(j) = p1(j);
[pd1, pd2, err, msg] = pipe ();
if (err)
error ("could not create pipe: %s", msg);
endif
child_data(cid, 1) = pd1;
if (idxs(j))
info.side = 0; # onesided interval
if ((pid = fork ()) == 0)
## child
pclose (pd1);
unwind_protect
tp = func (ps, info);
__bw_psend__ (pd2, tp);
unwind_protect_cleanup
pclose (pd2);
__internal_exit__ ();
end_unwind_protect
## end child
elseif (pid > 0)
child_data(cid, 2) = pid;
## child_data(cid, 3) is already 0
pclose (pd2);
else
## fork error
error ("could not fork");
endif
else
info.side = 1; # centered interval, side 1
if ((pid = fork ()) == 0)
## child
pclose (pd1);
unwind_protect
tp = func (ps, info);
__bw_psend__ (pd2, tp);
unwind_protect_cleanup
pclose (pd2);
__internal_exit__ ();
end_unwind_protect
## end child
elseif (pid > 0)
child_data(cid, 2) = pid;
child_data(cid, 3) = 1;
pclose (pd2);
else
## fork error
error ("could not fork");
endif
cid++;
child_data(cid, 5) = j;
ps(j) = p2(j);
info.side = 2; # centered interval, side 2
[pd1, pd2, err, msg] = pipe ();
if (err)
error ("could not create pipe: %s", msg);
endif
child_data(cid, 1) = pd1;
if ((pid = fork ()) == 0)
## child
pclose (pd1);
unwind_protect
tp = func (ps, info);
__bw_psend__ (pd2, tp);
unwind_protect_cleanup
pclose (pd2);
__internal_exit__ ();
end_unwind_protect
## end child
elseif (pid > 0)
child_data(cid, 2) = pid;
child_data(cid, 3) = 2;
pclose (pd2);
else
## fork error
error ("could not fork");
endif
endif
endif # (! fixed(j))
endfor
while (any (active_childs))
[~, act] = select (child_data(active_childs, 1), [], [], -1);
act_idx = child_data(active_childs, 4)(act);
for id = act_idx.'
res = __bw_prcv__ (child_data(id, 1));
if (ismatrix (res))
error ("child closed pipe without sending");
endif
res = res.psend_var;
pclose (child_data(id, 1));
child_data(id, 1) = 0;
waitpid (child_data(id, 2));
child_data(id, 2) = 0;
active_childs(id) = false;
if (child_data(id, 3)) # double sided
tp_ds(:, child_data(id, 5), child_data(id, 3)) = res;
else # single sided
tp_ss(:, child_data(id, 5)) = res;
endif
endfor
endwhile
ready = true; # try/catch would not handle ctrl-c
unwind_protect_cleanup
if (! ready)
for (id = 1 : n_childs)
if (child_data(id, 1))
pclose (child_data(id, 1));
endif
if (child_data(id, 2))
kill (child_data(id, 2), 9);
waitpid (child_data(id, 2));
endif
endfor
nerrm = lasterr ();
error ("no success, last error message: %s", nerrm);
endif
lasterr (lerrm);
end_unwind_protect
prt(:, idxs) = (tp_ss(:, idxs) - f(:, ones (1, n_ss))) ./ ...
del(idxs).'(ones (1, m), :);
prt(:, idxd) = (tp_ds(:, idxd, 1) - tp_ds(:, idxd, 2)) ./ ...
del(idxd).'(ones (1, m), :);
else # not parallel
for j = 1:n
if (~fixed(j))
info.plabels = plabels(j, :);
ps = p;
ps(j) = p1(j);
if (idxs(j))
info.side = 0; # onesided interval
tp1 = func (ps, info);
prt(:, j) = (tp1(:) - f) / del(j);
else
info.side = 1; # centered interval, side 1
tp1 = func (ps, info);
ps(j) = p2(j);
info.side = 2; # centered interval, side 2
tp2 = func (ps, info);
prt(:, j) = (tp1(:) - tp2(:)) / del(j);
endif
endif
endfor
endif
endfunction
|