File: gjp.m

package info (click to toggle)
octave-optim 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 960 kB
  • sloc: cpp: 1,125; makefile: 66
file content (52 lines) | stat: -rw-r--r-- 2,139 bytes parent folder | download
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
%% Copyright (C) 2010, 2011 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/>.

%% m = gjp (m, k[, l])
%%
%% m: matrix; k, l: row- and column-index of pivot, l defaults to k.
%%
%% Gauss-Jordon pivot as defined in Bard, Y.: Nonlinear Parameter
%% Estimation, p. 296, Academic Press, New York and London 1974. In
%% the pivot column, this seems not quite the same as the usual
%% Gauss-Jordan(-Clasen) pivot. Bard gives Beaton, A. E., 'The use of
%% special matrix operators in statistical calculus' Research Bulletin
%% RB-64-51 (1964), Educational Testing Service, Princeton, New Jersey
%% as a reference, but this article is not easily accessible. Another
%% reference, whose definition of gjp differs from Bards by some
%% signs, is Clarke, R. B., 'Algorithm AS 178: The Gauss-Jordan sweep
%% operator with detection of collinearity', Journal of the Royal
%% Statistical Society, Series C (Applied Statistics) (1982), 31(2),
%% 166--168.

function m = gjp (m, k, l)

  if (nargin < 3)
    l = k;
  end

  p = m(k, l);

  if (p == 0)
    error ('pivot is zero');
  end

  %% This is a case where I really hate to remain Matlab compatible,
  %% giving so many indices twice.
  m(k, [1:l-1, l+1:end]) = m(k, [1:l-1, l+1:end]) / p; % pivot row
  m([1:k-1, k+1:end], [1:l-1, l+1:end]) = ... % except pivot row and col
      m([1:k-1, k+1:end], [1:l-1, l+1:end]) - ...
      m([1:k-1, k+1:end], l) * m(k, [1:l-1, l+1:end]);
  m([1:k-1, k+1:end], l) = - m([1:k-1, k+1:end], l) / p; % pivot column
  m(k, l) = 1 / p;