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 <ostream>
31 
32 #include "dNDArray.h"
33 #include "lo-error.h"
34 #include "lo-ieee.h"
35 #include "oct-spparms.h"
36 #include "singleton-cleanup.h"
37 
38 octave_sparse_params *octave_sparse_params::instance = nullptr;
39 
40 bool
instance_ok(void)41 octave_sparse_params::instance_ok (void)
42 {
43   bool retval = true;
44 
45   if (! instance)
46     {
47       instance = new octave_sparse_params ();
48       singleton_cleanup_list::add (cleanup_instance);
49     }
50 
51   return retval;
52 }
53 
54 void
defaults(void)55 octave_sparse_params::defaults (void)
56 {
57   if (instance_ok ())
58     instance->do_defaults ();
59 }
60 
61 void
tight(void)62 octave_sparse_params::tight (void)
63 {
64   if (instance_ok ())
65     instance->do_tight ();
66 }
67 
68 string_vector
get_keys(void)69 octave_sparse_params::get_keys (void)
70 {
71   return instance_ok () ? instance->do_get_keys () : string_vector ();
72 }
73 
74 ColumnVector
get_vals(void)75 octave_sparse_params::get_vals (void)
76 {
77   return instance_ok () ? instance->do_get_vals () : ColumnVector ();
78 }
79 
80 bool
set_vals(const NDArray & vals)81 octave_sparse_params::set_vals (const NDArray& vals)
82 {
83   return instance_ok () ? instance->do_set_vals (vals) : false;
84 }
85 
86 bool
set_key(const std::string & key,const double & val)87 octave_sparse_params::set_key (const std::string& key, const double& val)
88 {
89   return instance_ok () ? instance->do_set_key (key, val) : false;
90 }
91 
92 double
get_key(const std::string & key)93 octave_sparse_params::get_key (const std::string& key)
94 {
95   return instance_ok () ? instance->do_get_key (key)
96                         : octave::numeric_limits<double>::NaN ();
97 }
98 
99 double
get_bandden(void)100 octave_sparse_params::get_bandden (void)
101 {
102   return instance_ok () ? instance->do_get_bandden () : 0.0;
103 }
104 
105 void
print_info(std::ostream & os,const std::string & prefix)106 octave_sparse_params::print_info (std::ostream& os, const std::string& prefix)
107 {
108   if (instance_ok ())
109     instance->do_print_info (os, prefix);
110 }
111 
112 void
do_defaults(void)113 octave_sparse_params::do_defaults (void)
114 {
115   params(0) = 0;      // spumoni
116   params(1) = 1;      // ths_rel
117   params(2) = 1;      // ths_abs
118   params(3) = 0;      // exact_d
119   params(4) = 3;      // supernd
120   params(5) = 3;      // rreduce
121   params(6) = 0.5;    // wh_frac
122   params(7) = 1;      // autommd
123   params(8) = 1;      // autoamd
124   params(9) = 0.1;    // piv_tol
125   params(10) = 0.5;   // bandden
126   params(11) = 1;     // umfpack
127   params(12) = 0.001; // sym_tol
128 }
129 
130 void
do_tight(void)131 octave_sparse_params::do_tight (void)
132 {
133   params(0) = 0;      // spumoni
134   params(1) = 1;      // ths_rel
135   params(2) = 0;      // ths_abs
136   params(3) = 1;      // exact_d
137   params(4) = 1;      // supernd
138   params(5) = 1;      // rreduce
139   params(6) = 0.5;    // wh_frac
140   params(7) = 1;      // autommd
141   params(8) = 1;      // autoamd
142   params(9) = 0.1;    // piv_tol
143   params(10) = 0.5;   // bandden
144   params(11) = 1;     // umfpack
145   params(12) = 0.001; // sym_tol
146 }
147 
148 void
init_keys(void)149 octave_sparse_params::init_keys (void)
150 {
151   keys(0) = "spumoni";
152   keys(1) = "ths_rel";
153   keys(2) = "ths_abs";
154   keys(3) = "exact_d";
155   keys(4) = "supernd";
156   keys(5) = "rreduce";
157   keys(6) = "wh_frac";
158   keys(7) = "autommd";
159   keys(8) = "autoamd";
160   keys(9) = "piv_tol";
161   keys(10) = "bandden";
162   keys(11) = "umfpack";
163   keys(12) = "sym_tol";
164 }
165 
166 double
do_get_bandden(void)167 octave_sparse_params::do_get_bandden (void)
168 {
169   return params(10);
170 }
171 
172 bool
do_set_vals(const NDArray & vals)173 octave_sparse_params::do_set_vals (const NDArray& vals)
174 {
175   octave_idx_type len = vals.numel ();
176 
177   if (len > OCTAVE_SPARSE_CONTROLS_SIZE)
178     (*current_liboctave_error_handler)
179       ("octave_sparse_params::do_set_vals: too many values");
180 
181   for (int i = 0; i < len; i++)
182     params(i) = vals(i);
183 
184   return true;
185 }
186 
187 bool
do_set_key(const std::string & key,const double & val)188 octave_sparse_params::do_set_key (const std::string& key, const double& val)
189 {
190   for (int i = 0; i < OCTAVE_SPARSE_CONTROLS_SIZE; i++)
191     {
192       if (keys (i) == key)
193         {
194           params(i) = val;
195           return true;
196         }
197     }
198 
199   return false;
200 }
201 
202 double
do_get_key(const std::string & key)203 octave_sparse_params::do_get_key (const std::string& key)
204 {
205   for (int i = 0; i < OCTAVE_SPARSE_CONTROLS_SIZE; i++)
206     {
207       if (keys (i) == key)
208         return params(i);
209     }
210 
211   return octave::numeric_limits<double>::NaN ();
212 }
213 
214 void
do_print_info(std::ostream & os,const std::string & prefix) const215 octave_sparse_params::do_print_info (std::ostream& os,
216                                      const std::string& prefix) const
217 {
218   for (int i = 0; i < OCTAVE_SPARSE_CONTROLS_SIZE; i++)
219     os << prefix << keys(i) << ": " << params(i) << "\n";
220 }
221