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