Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/rref.m @ 13485:59d266f7ed89
Removed plotter tab.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Wed, 27 Apr 2011 14:21:28 +0200 |
parents | 7ef7e20057fa |
children | c5c94b63931f |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Paul Kienzle |
5827 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5827 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5827 | 18 |
19 ## -*- texinfo -*- | |
12584
7ef7e20057fa
Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {} rref (@var{A}) |
7ef7e20057fa
Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {} rref (@var{A}, @var{tol}) |
7ef7e20057fa
Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{r}, @var{k}] =} rref (@dots{}) |
7ef7e20057fa
Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
23 ## Return the reduced row echelon form of @var{A}. @var{tol} defaults |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
24 ## to @code{eps * max (size (@var{A})) * norm (@var{A}, inf)}. |
5827 | 25 ## |
26 ## Called with two return arguments, @var{k} returns the vector of | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
27 ## "bound variables", which are those columns on which elimination |
5827 | 28 ## has been performed. |
29 ## | |
30 ## @end deftypefn | |
31 | |
32 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
33 ## (based on an anonymous source from the public domain) |
5827 | 34 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
35 function [A, k] = rref (A, tol) |
5827 | 36 |
37 if (nargin < 1 || nargin > 2) | |
38 print_usage (); | |
39 endif | |
40 | |
41 if (ndims (A) > 2) | |
42 error ("rref: expecting matrix argument"); | |
43 endif | |
44 | |
45 [rows, cols] = size (A); | |
46 | |
47 if (nargin < 2) | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
48 if (isa (A, "single")) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
49 tol = eps ("single") * max (rows, cols) * norm (A, inf ("single")); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
50 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
51 tol = eps * max (rows, cols) * norm (A, inf); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
52 endif |
5827 | 53 endif |
54 | |
55 used = zeros (1, cols); | |
56 r = 1; | |
57 for c = 1:cols | |
58 ## Find the pivot row | |
59 [m, pivot] = max (abs (A(r:rows,c))); | |
60 pivot = r + pivot - 1; | |
61 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
62 if (m <= tol) |
5827 | 63 ## Skip column c, making sure the approximately zero terms are |
64 ## actually zero. | |
65 A (r:rows, c) = zeros (rows-r+1, 1); | |
66 else | |
67 ## keep track of bound variables | |
68 used (1, c) = 1; | |
69 | |
70 ## Swap current row and pivot row | |
71 A ([pivot, r], c:cols) = A ([r, pivot], c:cols); | |
72 | |
73 ## Normalize pivot row | |
74 A (r, c:cols) = A (r, c:cols) / A (r, c); | |
75 | |
76 ## Eliminate the current column | |
77 ridx = [1:r-1, r+1:rows]; | |
78 A (ridx, c:cols) = A (ridx, c:cols) - A (ridx, c) * A(r, c:cols); | |
79 | |
80 ## Check if done | |
81 if (r++ == rows) | |
10549 | 82 break; |
5827 | 83 endif |
84 endif | |
85 endfor | |
86 k = find (used); | |
87 | |
88 endfunction |