]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/gjp.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / gjp.m
1 %% Copyright (C) 2010, 2011 Olaf Till <i7tiol@t-online.de>
2 %%
3 %% This program is free software; you can redistribute it and/or modify it under
4 %% the terms of the GNU General Public License as published by the Free Software
5 %% Foundation; either version 3 of the License, or (at your option) any later
6 %% version.
7 %%
8 %% This program is distributed in the hope that it will be useful, but WITHOUT
9 %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 %% details.
12 %%
13 %% You should have received a copy of the GNU General Public License along with
14 %% this program; if not, see <http://www.gnu.org/licenses/>.
15
16 %% m = gjp (m, k[, l])
17 %%
18 %% m: matrix; k, l: row- and column-index of pivot, l defaults to k.
19 %%
20 %% Gauss-Jordon pivot as defined in Bard, Y.: Nonlinear Parameter
21 %% Estimation, p. 296, Academic Press, New York and London 1974. In
22 %% the pivot column, this seems not quite the same as the usual
23 %% Gauss-Jordan(-Clasen) pivot. Bard gives Beaton, A. E., 'The use of
24 %% special matrix operators in statistical calculus' Research Bulletin
25 %% RB-64-51 (1964), Educational Testing Service, Princeton, New Jersey
26 %% as a reference, but this article is not easily accessible. Another
27 %% reference, whose definition of gjp differs from Bards by some
28 %% signs, is Clarke, R. B., 'Algorithm AS 178: The Gauss-Jordan sweep
29 %% operator with detection of collinearity', Journal of the Royal
30 %% Statistical Society, Series C (Applied Statistics) (1982), 31(2),
31 %% 166--168.
32
33 function m = gjp (m, k, l)
34
35   if (nargin < 3)
36     l = k;
37   end
38
39   p = m(k, l);
40
41   if (p == 0)
42     error ('pivot is zero');
43   end
44
45   %% This is a case where I really hate to remain Matlab compatible,
46   %% giving so many indices twice.
47   m(k, [1:l-1, l+1:end]) = m(k, [1:l-1, l+1:end]) / p; % pivot row
48   m([1:k-1, k+1:end], [1:l-1, l+1:end]) = ... % except pivot row and col
49       m([1:k-1, k+1:end], [1:l-1, l+1:end]) - ...
50       m([1:k-1, k+1:end], l) * m(k, [1:l-1, l+1:end]);
51   m([1:k-1, k+1:end], l) = - m([1:k-1, k+1:end], l) / p; % pivot column
52   m(k, l) = 1 / p;