File: SylvParams.hh

package info (click to toggle)
dynare 4.6.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 74,896 kB
  • sloc: cpp: 98,057; ansic: 28,929; pascal: 13,844; sh: 5,947; objc: 4,236; yacc: 4,215; makefile: 2,583; lex: 1,534; fortran: 877; python: 647; ruby: 291; lisp: 152; xml: 22
file content (231 lines) | stat: -rw-r--r-- 6,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright © 2004-2011 Ondra Kamenik
 * Copyright © 2019 Dynare Team
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SYLV_PARAMS_H
#define SYLV_PARAMS_H

#include <string>
#include <ostream>

#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
# include <dynmex.h>
#endif

enum class status { def, changed, undef };

template<class _Type>
struct ParamItem
{
protected:
  using _Self = ParamItem<_Type>;
  status s;
  _Type value;
public:
  ParamItem()
  {
    s = status::undef;
  }
  ParamItem(_Type val)
  {
    value = val;
    s = status::def;
  }
  ParamItem(const _Self &item) = default;
  _Self &operator=(const _Self &item) = default;
  _Self &
  operator=(const _Type &val)
  {
    value = val;
    s = status::changed;
    return *this;
  }
  _Type
  operator*() const
  {
    return value;
  }
  status
  getStatus() const
  {
    return s;
  }
  void
  print(std::ostream &out, const std::string &prefix, const std::string &str) const
  {
    if (s == status::undef)
      return;
    out << prefix << str << "= " << value;
    if (s == status::def)
      out << " <default>";
    out << std::endl;
  }
};

class SylvParams
{
public:
  enum class solve_method { iter, recurse };

protected:
  class DoubleParamItem : public ParamItem<double>
  {
  public:
    DoubleParamItem() : ParamItem<double>()
    {
    }
    DoubleParamItem(double val) : ParamItem<double>(val)
    {
    }
    DoubleParamItem(const DoubleParamItem &item) = default;
    DoubleParamItem &
    operator=(const double &val)
    {
      ParamItem<double>::operator=(val); return *this;
    }
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
    mxArray *createMatlabArray() const;
#endif
  };

  class IntParamItem : public ParamItem<int>
  {
  public:
    IntParamItem() : ParamItem<int>()
    {
    }
    IntParamItem(int val) : ParamItem<int>(val)
    {
    }
    IntParamItem(const IntParamItem &item) = default;
    IntParamItem &
    operator=(const int &val)
    {
      ParamItem<int>::operator=(val); return *this;
    }
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
    mxArray *createMatlabArray() const;
#endif
  };

  class BoolParamItem : public ParamItem<bool>
  {
  public:
    BoolParamItem() : ParamItem<bool>()
    {
    }
    BoolParamItem(bool val) : ParamItem<bool>(val)
    {
    }
    BoolParamItem(const BoolParamItem &item) = default;
    BoolParamItem &
    operator=(const bool &val)
    {
      ParamItem<bool>::operator=(val); return *this;
    }
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
    mxArray *createMatlabArray() const;
#endif
  };

  class MethodParamItem : public ParamItem<solve_method>
  {
  public:
    MethodParamItem() : ParamItem<solve_method>()
    {
    }
    MethodParamItem(solve_method val) : ParamItem<solve_method>(val)
    {
    }
    MethodParamItem(const MethodParamItem &item) = default;
    MethodParamItem
    operator=(const solve_method &val)
    {
      ParamItem<solve_method>::operator=(val); return *this;
    }
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
    mxArray *createMatlabArray() const;
#endif
  };

public:
  // input parameters
  MethodParamItem method; // method of solution: iter/recurse
  DoubleParamItem convergence_tol; // norm for what we consider converged
  IntParamItem max_num_iter; // max number of iterations
  DoubleParamItem bs_norm; // Bavely Stewart log₁₀ of norm for diagonalization
  BoolParamItem want_check; // true => allocate extra space for checks
  // output parameters
  BoolParamItem converged; // true if converged
  DoubleParamItem iter_last_norm; // norm of the last iteration
  IntParamItem num_iter; // number of iterations
  DoubleParamItem f_err1; // norm 1 of diagonalization abs. error C−V·F·V⁻¹
  DoubleParamItem f_errI; // norm ∞ of diagonalization abs. error C−V·F·V⁻¹
  DoubleParamItem viv_err1; // norm 1 of error I−V·V⁻¹
  DoubleParamItem viv_errI; // norm ∞ of error I−V·V⁻¹
  DoubleParamItem ivv_err1; // norm 1 of error I−V⁻¹·V
  DoubleParamItem ivv_errI; // norm ∞ of error I−V⁻¹·V
  IntParamItem f_blocks; // number of diagonal blocks of F
  IntParamItem f_largest; // size of largest diagonal block in F
  IntParamItem f_zeros; // number of off diagonal zeros in F
  IntParamItem f_offdiag; // number of all off diagonal elements in F
  DoubleParamItem rcondA1; // reciprocal cond 1 number of A
  DoubleParamItem rcondAI; // reciprocal cond ∞ number of A
  DoubleParamItem eig_min; // minimum eigenvalue of the solved system
  DoubleParamItem mat_err1; // rel. matrix 1 norm of A·X−B·X·⊗ⁱC−D
  DoubleParamItem mat_errI; // rel. matrix ∞ norm of A·X−B·X·⊗ⁱC−D
  DoubleParamItem mat_errF; // rel. matrix Frob. norm of A·X−B·X·⊗ⁱC−D
  DoubleParamItem vec_err1; // rel. vector 1 norm of A·X−B·X·⊗ⁱC−D
  DoubleParamItem vec_errI; // rel. vector ∞ norm of A·X−B·X·⊗ⁱC−D
  DoubleParamItem cpu_time; // time of the job in CPU seconds

  SylvParams(bool wc = false)
    : method(solve_method::recurse), convergence_tol(1.e-30), max_num_iter(15),
      bs_norm(1.3), want_check(wc)
  {
  }
  SylvParams(const SylvParams &p) = default;
  SylvParams &operator=(const SylvParams &p) = default;
  ~SylvParams() = default;
  void print(const std::string &prefix) const;
  void print(std::ostream &fdesc, const std::string &prefix) const;
  void setArrayNames(int &num, const char **names) const;
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
  mxArray *createStructArray() const;
#endif
private:
  void copy(const SylvParams &p);
};

inline std::ostream &
operator<<(std::ostream &out, SylvParams::solve_method m)
{
  switch (m)
    {
    case SylvParams::solve_method::iter:
      out << "iterative";
      break;
    case SylvParams::solve_method::recurse:
      out << "recurse (a.k.a. triangular)";
      break;
    }
  return out;
}

#endif /* SYLV_PARAMS_H */