GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rcond.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-2024 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 
37 
38 DEFUN (rcond, args, ,
39  doc: /* -*- texinfo -*-
40 @deftypefn {} {@var{c} =} rcond (@var{A})
41 Compute the 1-norm estimate of the reciprocal condition number as returned
42 by @sc{lapack}.
43 
44 If the matrix is well-conditioned then @var{c} will be near 1 and if the
45 matrix is poorly conditioned it will be close to 0.
46 
47 The matrix @var{A} must not be sparse. If the matrix is sparse then
48 @code{condest (@var{A})} or @code{rcond (full (@var{A}))} should be used
49 instead.
50 @seealso{cond, condest}
51 @end deftypefn */)
52 {
53  if (args.length () != 1)
54  print_usage ();
55 
56  octave_value retval;
57 
58  if (args(0).issparse ())
59  error ("rcond: for sparse matrices use 'rcond (full (a))' or 'condest (a)' instead");
60 
61  if (args(0).is_single_type ())
62  {
63  if (args(0).iscomplex ())
64  {
65  FloatComplexMatrix m = args(0).float_complex_matrix_value ();
66  MatrixType mattyp;
67  retval = m.rcond (mattyp);
68  args(0).matrix_type (mattyp);
69  }
70  else
71  {
72  FloatMatrix m = args(0).float_matrix_value ();
73  MatrixType mattyp;
74  retval = m.rcond (mattyp);
75  args(0).matrix_type (mattyp);
76  }
77  }
78  else if (args(0).iscomplex ())
79  {
80  ComplexMatrix m = args(0).complex_matrix_value ();
81  MatrixType mattyp;
82  retval = m.rcond (mattyp);
83  args(0).matrix_type (mattyp);
84  }
85  else
86  {
87  Matrix m = args(0).matrix_value ();
88  MatrixType mattyp;
89  retval = m.rcond (mattyp);
90  args(0).matrix_type (mattyp);
91  }
92 
93  return retval;
94 }
95 
96 /*
97 %!assert (rcond (eye (2)), 1)
98 %!assert (rcond (ones (2)), 0)
99 %!assert (rcond ([1 1; 2 1]), 1/9)
100 %!assert (rcond (magic (4)), 0, eps)
101 
102 %!shared x, sx
103 %! x = [-5.25, -2.25; -2.25, 1] * eps () + ones (2) / 2;
104 %! sx = [-5.25, -2.25; -2.25, 1] * eps ("single") + ones (2) / 2;
105 %!assert (rcond (x) < eps ())
106 %!assert (rcond (sx) < eps ('single'))
107 %!assert (rcond (x*i) < eps ())
108 %!assert (rcond (sx*i) < eps ('single'))
109 
110 */
111 
112 OCTAVE_END_NAMESPACE(octave)
Definition: dMatrix.h:42
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage(void)
Definition: defun-int.h:72
#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:988
T octave_idx_type m
Definition: mx-inlines.cc:781