GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
rcond.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "defun.h"
31 #include "error.h"
32 #include "errwarn.h"
33 #include "ovl.h"
34 #include "utils.h"
35 
36 DEFUN (rcond, args, ,
37  doc: /* -*- texinfo -*-
38 @deftypefn {} {@var{c} =} rcond (@var{A})
39 Compute the 1-norm estimate of the reciprocal condition number as returned
40 by @sc{lapack}.
41 
42 If the matrix is well-conditioned then @var{c} will be near 1 and if the
43 matrix is poorly conditioned it will be close to 0.
44 
45 The matrix @var{A} must not be sparse. If the matrix is sparse then
46 @code{condest (@var{A})} or @code{rcond (full (@var{A}))} should be used
47 instead.
48 @seealso{cond, condest}
49 @end deftypefn */)
50 {
51  if (args.length () != 1)
52  print_usage ();
53 
55 
56  if (args(0).issparse ())
57  error ("rcond: for sparse matrices use 'rcond (full (a))' or 'condest (a)' instead");
58 
59  if (args(0).is_single_type ())
60  {
61  if (args(0).iscomplex ())
62  {
63  FloatComplexMatrix m = args(0).float_complex_matrix_value ();
64  MatrixType mattyp;
65  retval = m.rcond (mattyp);
66  args(0).matrix_type (mattyp);
67  }
68  else
69  {
70  FloatMatrix m = args(0).float_matrix_value ();
71  MatrixType mattyp;
72  retval = m.rcond (mattyp);
73  args(0).matrix_type (mattyp);
74  }
75  }
76  else if (args(0).iscomplex ())
77  {
78  ComplexMatrix m = args(0).complex_matrix_value ();
79  MatrixType mattyp;
80  retval = m.rcond (mattyp);
81  args(0).matrix_type (mattyp);
82  }
83  else
84  {
85  Matrix m = args(0).matrix_value ();
86  MatrixType mattyp;
87  retval = m.rcond (mattyp);
88  args(0).matrix_type (mattyp);
89  }
90 
91  return retval;
92 }
93 
94 /*
95 %!assert (rcond (eye (2)), 1)
96 %!assert (rcond (ones (2)), 0)
97 %!assert (rcond ([1 1; 2 1]), 1/9)
98 %!assert (rcond (magic (4)), 0, eps)
99 
100 %!shared x, sx
101 %! x = [-5.25, -2.25; -2.25, 1] * eps () + ones (2) / 2;
102 %! sx = [-5.25, -2.25; -2.25, 1] * eps ("single") + ones (2) / 2;
103 %!assert (rcond (x) < eps ())
104 %!assert (rcond (sx) < eps ('single'))
105 %!assert (rcond (x*i) < eps ())
106 %!assert (rcond (sx*i) < eps ('single'))
107 
108 */
Definition: dMatrix.h:42
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:968
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811