Mercurial > hg > octave-lyh
comparison scripts/linear-algebra/rref.m @ 11469:c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 09 Jan 2011 12:41:21 -0800 |
parents | 95c3e38098bf |
children | fd0a3ac60b0e |
comparison
equal
deleted
inserted
replaced
11468:e1edf0ba3bcb | 11469:c776f063fefe |
---|---|
15 ## You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {[@var{r}, @var{k}] =} rref (@var{a}, @var{tol}) | 20 ## @deftypefn {Function File} {[@var{r}, @var{k}] =} rref (@var{A}, @var{tol}) |
21 ## | 21 ## |
22 ## Returns the reduced row echelon form of @var{a}. @var{tol} defaults | 22 ## Returns the reduced row echelon form of @var{A}. @var{tol} defaults |
23 ## to @code{eps * max (size (@var{a})) * norm (@var{a}, inf)}. | 23 ## to @code{eps * max (size (@var{A})) * norm (@var{A}, inf)}. |
24 ## | 24 ## |
25 ## Called with two return arguments, @var{k} returns the vector of | 25 ## Called with two return arguments, @var{k} returns the vector of |
26 ## "bound variables", which are those columns on which elimination | 26 ## "bound variables", which are those columns on which elimination |
27 ## has been performed. | 27 ## has been performed. |
28 ## | 28 ## |
29 ## @end deftypefn | 29 ## @end deftypefn |
30 | 30 |
31 ## Author: Paul Kienzle <pkienzle@users.sf.net> | 31 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
32 ## (based on a anonymous source from the public domain) | 32 ## (based on an anonymous source from the public domain) |
33 | 33 |
34 function [A, k] = rref (A, tolerance) | 34 function [A, k] = rref (A, tol) |
35 | 35 |
36 if (nargin < 1 || nargin > 2) | 36 if (nargin < 1 || nargin > 2) |
37 print_usage (); | 37 print_usage (); |
38 endif | 38 endif |
39 | 39 |
43 | 43 |
44 [rows, cols] = size (A); | 44 [rows, cols] = size (A); |
45 | 45 |
46 if (nargin < 2) | 46 if (nargin < 2) |
47 if (isa (A, "single")) | 47 if (isa (A, "single")) |
48 tolerance = eps ("single") * max (rows, cols) * norm (A, inf ("single")); | 48 tol = eps ("single") * max (rows, cols) * norm (A, inf ("single")); |
49 else | 49 else |
50 tolerance = eps * max (rows, cols) * norm (A, inf); | 50 tol = eps * max (rows, cols) * norm (A, inf); |
51 endif | 51 endif |
52 endif | 52 endif |
53 | 53 |
54 used = zeros (1, cols); | 54 used = zeros (1, cols); |
55 r = 1; | 55 r = 1; |
56 for c = 1:cols | 56 for c = 1:cols |
57 ## Find the pivot row | 57 ## Find the pivot row |
58 [m, pivot] = max (abs (A(r:rows,c))); | 58 [m, pivot] = max (abs (A(r:rows,c))); |
59 pivot = r + pivot - 1; | 59 pivot = r + pivot - 1; |
60 | 60 |
61 if (m <= tolerance) | 61 if (m <= tol) |
62 ## Skip column c, making sure the approximately zero terms are | 62 ## Skip column c, making sure the approximately zero terms are |
63 ## actually zero. | 63 ## actually zero. |
64 A (r:rows, c) = zeros (rows-r+1, 1); | 64 A (r:rows, c) = zeros (rows-r+1, 1); |
65 else | 65 else |
66 ## keep track of bound variables | 66 ## keep track of bound variables |