GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
spparms.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-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 "ov.h"
32 #include "pager.h"
33 #include "error.h"
34 #include "errwarn.h"
35 
36 #include "oct-spparms.h"
37 
38 DEFUN (spparms, args, nargout,
39  doc: /* -*- texinfo -*-
40 @deftypefn {} { } spparms ()
41 @deftypefnx {} {@var{vals} =} spparms ()
42 @deftypefnx {} {[@var{keys}, @var{vals}] =} spparms ()
43 @deftypefnx {} {@var{val} =} spparms (@var{key})
44 @deftypefnx {} { } spparms (@var{vals})
45 @deftypefnx {} { } spparms ("default")
46 @deftypefnx {} { } spparms ("tight")
47 @deftypefnx {} { } spparms (@var{key}, @var{val})
48 Query or set the parameters used by the sparse solvers and factorization
49 functions.
50 
51 The first four calls above get information about the current settings, while
52 the others change the current settings. The parameters are stored as pairs
53 of keys and values, where the values are all floats and the keys are one of
54 the following strings:
55 
56 @table @samp
57 @item spumoni
58 Printing level of debugging information of the solvers (default 0)
59 
60 @item ths_rel
61 Included for compatibility. Not used. (default 1)
62 
63 @item ths_abs
64 Included for compatibility. Not used. (default 1)
65 
66 @item exact_d
67 Included for compatibility. Not used. (default 0)
68 
69 @item supernd
70 Included for compatibility. Not used. (default 3)
71 
72 @item rreduce
73 Included for compatibility. Not used. (default 3)
74 
75 @item wh_frac
76 Included for compatibility. Not used. (default 0.5)
77 
78 @item autommd
79 Flag whether the LU/QR and the '\' and '/' operators will automatically
80 use the sparsity preserving mmd functions (default 1)
81 
82 @item autoamd
83 Flag whether the LU and the '\' and '/' operators will automatically
84 use the sparsity preserving amd functions (default 1)
85 
86 @item piv_tol
87 The pivot tolerance of the @sc{umfpack} solvers (default 0.1)
88 
89 @item sym_tol
90 The pivot tolerance of the @sc{umfpack} symmetric solvers (default 0.001)
91 
92 @item bandden
93 The density of nonzero elements in a banded matrix before it is treated
94 by the @sc{lapack} banded solvers (default 0.5)
95 
96 @item umfpack
97 Flag whether the @sc{umfpack} or mmd solvers are used for the LU, '\' and
98 '/' operations (default 1)
99 @end table
100 
101 The value of individual keys can be set with
102 @code{spparms (@var{key}, @var{val})}.
103 The default values can be restored with the special keyword
104 @qcode{"default"}. The special keyword @qcode{"tight"} can be used to
105 set the mmd solvers to attempt a sparser solution at the potential cost of
106 longer running time.
107 @seealso{chol, colamd, lu, qr, symamd}
108 @end deftypefn */)
109 {
111  int nargin = args.length ();
112 
113  if (nargin == 0)
114  {
115  if (nargout == 0)
117  else if (nargout == 1)
119  else if (nargout == 2)
122  else
123  error ("spparms: too many output arguments");
124  }
125  else if (nargin == 1)
126  {
127  if (args(0).is_string ())
128  {
129  std::string str = args(0).string_value ();
130  int len = str.length ();
131  for (int i = 0; i < len; i++)
132  str[i] = tolower (str[i]);
133 
134  if (str == "default")
136  else if (str == "tight")
138  else
139  {
140  double val = octave_sparse_params::get_key (str);
141  if (octave::math::isnan (val))
142  error ("spparms: KEY not recognized");
143 
144  retval = ovl (val);
145  }
146  }
147  else
148  {
149  NDArray vals = args(0).xarray_value ("spparms: input must be a string or a vector");
150  if (vals.numel () > OCTAVE_SPARSE_CONTROLS_SIZE)
151  error ("spparms: too many elements in vector VALS");
152 
154  }
155  }
156  else if (nargin == 2)
157  {
158  std::string str = args(0).xstring_value ("spparms: first argument must be a string");
159 
160  double val = args(1).xdouble_value ("spparms: second argument must be a real scalar");
161 
162  if (str == "umfpack")
163  warning ("spparms: request to disable umfpack solvers ignored");
164  else if (! octave_sparse_params::set_key (str, val))
165  error ("spparms: KEY not found");
166  }
167  else
168  error ("spparms: too many input arguments");
169 
170  return retval;
171 }
172 
173 /*
174 %!test
175 %! old_vals = spparms (); # save state
176 %! spparms ("default");
177 %! vals = spparms ();
178 %! assert (vals, [0 1 1 0 3 3 0.5 1.0 1.0 0.1 0.5 1.0 0.001]');
179 %! [keys, vals] = spparms ();
180 %! assert (rows (keys), 13);
181 %! assert (keys(2,:), "ths_rel");
182 %! assert (vals, [0 1 1 0 3 3 0.5 1.0 1.0 0.1 0.5 1.0 0.001]');
183 %! spparms ([3 2 1]);
184 %! assert (spparms ()(1:3), [3, 2, 1]');
185 %! assert (spparms ("ths_rel"), 2);
186 %! spparms ("exact_d", 5);
187 %! assert (spparms ("exact_d"), 5);
188 %! spparms (old_vals); # restore state
189 
190 ## Test input validation
191 %!error <too many input arguments> spparms (1, 2, 3)
192 %!error <too many output arguments> [x, y, z] = spparms ()
193 %!error <KEY not recognized> spparms ("UNKNOWN_KEY")
194 %!#error <input must be a string> spparms ({1, 2, 3})
195 %!error spparms ({1, 2, 3})
196 %!error <too many elements in vector VALS> spparms (ones (14, 1))
197 %!error <first argument must be a string> spparms (1, 1)
198 %!#error <second argument must be a real scalar> spparms ("ths_rel", "hello")
199 %!error spparms ("ths_rel", "hello")
200 %!error <KEY not found> spparms ("UNKNOWN_KEY", 1)
201 */
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:377
static string_vector get_keys(void)
Definition: oct-spparms.cc:69
static void print_info(std::ostream &os, const std::string &prefix)
Definition: oct-spparms.cc:106
static ColumnVector get_vals(void)
Definition: oct-spparms.cc:75
static bool set_key(const std::string &key, const double &val)
Definition: oct-spparms.cc:87
static double get_key(const std::string &key)
Definition: oct-spparms.cc:93
static bool set_vals(const NDArray &vals)
Definition: oct-spparms.cc:81
static void tight(void)
Definition: oct-spparms.cc:62
static void defaults(void)
Definition: oct-spparms.cc:55
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void warning(const char *fmt,...)
Definition: error.cc:1050
void error(const char *fmt,...)
Definition: error.cc:968
bool isnan(bool)
Definition: lo-mappers.h:178
#define OCTAVE_SPARSE_CONTROLS_SIZE
Definition: oct-spparms.h:39
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
#define octave_stdout
Definition: pager.h:313
F77_RET_T len
Definition: xerbla.cc:61