GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
rcond.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2013 David Bateman
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "defun.h"
28 #include "error.h"
29 #include "gripes.h"
30 #include "oct-obj.h"
31 #include "utils.h"
32 
33 DEFUN (rcond, args, ,
34  "-*- texinfo -*-\n\
35 @deftypefn {Built-in Function} {@var{c} =} rcond (@var{A})\n\
36 Compute the 1-norm estimate of the reciprocal condition number as returned\n\
37 by @sc{lapack}. If the matrix is well-conditioned then @var{c} will be near\n\
38 1 and if the matrix is poorly conditioned it will be close to zero.\n\
39 \n\
40 The matrix @var{A} must not be sparse. If the matrix is sparse then\n\
41 @code{condest (@var{A})} or @code{rcond (full (@var{A}))} should be used\n\
42 instead.\n\
43 @seealso{cond, condest}\n\
44 @end deftypefn")
45 {
46  octave_value retval;
47 
48  int nargin = args.length ();
49 
50  if (nargin != 1)
51  print_usage ();
52  else if (args(0).is_sparse_type ())
53  error ("rcond: for sparse matrices use 'rcond (full (a))' or 'condest (a)' instead");
54  else if (args(0).is_single_type ())
55  {
56  if (args(0).is_complex_type ())
57  {
58  FloatComplexMatrix m = args(0).float_complex_matrix_value ();
59  MatrixType mattyp;
60  retval = m.rcond (mattyp);
61  args(0).matrix_type (mattyp);
62  }
63  else
64  {
65  FloatMatrix m = args(0).float_matrix_value ();
66  MatrixType mattyp;
67  retval = m.rcond (mattyp);
68  args(0).matrix_type (mattyp);
69  }
70  }
71  else if (args(0).is_complex_type ())
72  {
73  ComplexMatrix m = args(0).complex_matrix_value ();
74  MatrixType mattyp;
75  retval = m.rcond (mattyp);
76  args(0).matrix_type (mattyp);
77  }
78  else
79  {
80  Matrix m = args(0).matrix_value ();
81  MatrixType mattyp;
82  retval = m.rcond (mattyp);
83  args(0).matrix_type (mattyp);
84  }
85 
86  return retval;
87 }
88 
89 /*
90 %!assert (rcond (eye (2)), 1)
91 %!assert (rcond (ones (2)), 0)
92 %!assert (rcond ([1 1; 2 1]), 1/9)
93 %!assert (rcond (magic (4)), 0, eps)
94 
95 %!shared x, sx
96 %! x = [-5.25, -2.25; -2.25, 1] * eps () + ones (2) / 2;
97 %! sx = [-5.25, -2.25; -2.25, 1] * eps ("single") + ones (2) / 2;
98 %!assert (rcond (x) < eps ());
99 %!assert (rcond (sx) < eps ('single'));
100 %!assert (rcond (x*i) < eps ());
101 %!assert (rcond (sx*i) < eps ('single'));
102 
103 */