File: dynamic_atoms.h

package info (click to toggle)
dynare 4.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,356 kB
  • ctags: 15,842
  • sloc: cpp: 77,029; ansic: 29,056; pascal: 13,241; sh: 4,811; objc: 3,061; yacc: 3,013; makefile: 1,479; lex: 1,258; python: 162; lisp: 54; xml: 8
file content (403 lines) | stat: -rw-r--r-- 16,488 bytes parent folder | download | duplicates (4)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (C) 2005, Ondra Kamenik

// $Id: dynamic_atoms.h 2269 2008-11-23 14:33:22Z michel $

#ifndef OGP_DYNAMIC_ATOMS_H
#define OGP_DYNAMIC_ATOMS_H

#include "formula_parser.h"

#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>

namespace ogp {
	using std::vector;
	using std::map;
	using std::set;
	using std::string;

	struct ltstr {
		bool operator()(const char* a1, const char* a2) const
			{ return strcmp(a1, a2) < 0; }
	};

	/** Class storing names. We will keep names of variables in
	 * various places, and all these pointers will point to one
	 * storage, which will be responsible for allocation and
	 * deallocation. The main function of the class is to allocate
	 * space for names, and return a pointer of the stored name if
	 * required. */
	class NameStorage {
	protected:
		/** Vector of names allocated, this is the storage. */
		vector<char*> name_store;
		/** Map useful to quickly decide if the name is already
		 * allocated or not. */
		set<const char*, ltstr> name_set;
	public:
		NameStorage() {}
		NameStorage(const NameStorage& stor);
		virtual ~NameStorage();
		/** Query for the name. If the name has been stored, it
		 * returns its address, otherwise 0. */
		const char* query(const char* name) const;
		/** Insert the name if it has not been inserted yet, and
		 * return its new or old allocation. */
		const char* insert(const char* name);
		int num() const
			{return (int)name_store.size();}
		const char* get_name(int i) const
			{return name_store[i];}
		/** Debug print. */
		void print() const;
	};

	class Constants : public AtomValues {
	public:
		/** Type for a map mapping tree indices to double values. */
		typedef map<int,double> Tconstantmap;
		typedef map<int,int> Tintintmap;
	protected:
		/** Map mapping a tree index of a constant to its double value. */ 
		Tconstantmap cmap;
	public:
		Constants() {}
		/** Copy constructor. */
		Constants(const Constants& c)
			: cmap(c.cmap), cinvmap(c.cinvmap) {}
		/** Copy constructor registering the constants in the given
		 * tree. The mapping from old tree indices to new ones is
		 * traced in tmap. */
		Constants(const Constants& c, OperationTree& otree, Tintintmap& tmap)
			{import_constants(c, otree, tmap);}
		/** Import constants registering their tree indices in the
		 * given tree. The mapping form old tree indices to new ones
		 * is traced in tmap. */
		void import_constants(const Constants& c, OperationTree& otree, Tintintmap& tmap);
		/** Implements AtomValues interface. This sets the values to
		 * the evaluation tree EvalTree. */
		void setValues(EvalTree& et) const;
		/** This adds a constant with the given tree index. The
		 * constant must be checked previously and asserted that it
		 * does not exist. */
		void add_constant(int t, double val);
		/** Returns true if the tree index is either an hardwired
		 * constant (initial number OperationTree:num_constants in
		 * OperationTree) or the tree index is a registered constant
		 * by add_constant method. */
		bool is_constant(int t) const;
		double get_constant_value(int t) const;
		/** Return -1 if the given string representation of a constant
		 * is not among the constants (double represenations). If it
		 * is, its tree index is returned. */ 
		int check(const char* str) const;
		/** Debug print. */
		void print() const;
		const Tconstantmap& get_constantmap() const
			{return cmap;}
	private:
		/** Inverse map to Tconstantmap. */
		typedef map<double,int> Tconstantinvmap;
		/** This is an inverse map to cmap. This is only used for fast
		 * queries for the existing double constants in check
		 * method and add_constant. */
		Tconstantinvmap cinvmap;
	};

    /** This class is a parent to Atoms classes which distinguish between
	 * constants (numerical literals), and variables with lags and
	 * leads. This abstraction does not distinguish between a parameter
	 * and a variable without lag or lead. In this sense, everything is a
	 * variable.*/
	class DynamicAtoms : public Atoms, public Constants {
	public:
		/** Definition of a type mapping lags to the indices of the variables. */
		typedef map<int,int> Tlagmap;
	protected:
		/** Definition of a type mapping names of the atoms to Tlagmap. */
		typedef map<const char*, Tlagmap, ltstr> Tvarmap;
		/** Definition of a type mapping indices of variables to the variable names. */
		typedef map<int, const char*> Tindexmap;
		/** This is just a storage for variable names, since all other
		 * instances of a variable name just point to the memory
		 * allocated by this object. */
		NameStorage varnames;
		/** This is the map for variables. Each variable name is
		 * mapped to the Tlagmap, which maps lags/leads to the nulary
		 * term indices in the tree. */
		Tvarmap vars;
		/** This is almost inverse map to the vars. It maps variable
		 * indices to the names. A returned name can be in turn used
		 * as a key in vars. */
		Tindexmap indices;

		/** Number of variables. */
		int nv;
		/** Minimum lag, if there is at least one lag, than this is a negative number. */
		int minlag;
		/** Maximum lead, if there is at least one lead, than this is a positive number. */
		int maxlead;
	public:
		/** Construct empty DynamicAtoms. */
		DynamicAtoms();
		DynamicAtoms(const DynamicAtoms& da);
		virtual ~DynamicAtoms() {}
		/** Check the nulary term identified by its string
		 * representation. The nulary term can be either a constant or
		 * a variable. If constant, -1 is returned so that it could be
		 * assigned regardless if the same constant has already
		 * appeared or not. If variable, then -1 is returned only if
		 * the variable has not been assigned an index, otherwise the
		 * assigned index is returned. */ 
		int check(const char* name) const;
		/** Assign the nulary term identified by its string
		 * representation. This method should be called when check()
		 * returns -1. */
		void assign(const char* name, int t);
		/** Return a number of all variables. */
		int nvar() const
			{ return nv; }
		/** Return the vector of variable indices. */
		vector<int> variables() const;
		/** Return max lead and min lag for a variable given by the
		 * index. If a variable cannot be found, the method retursn
		 * the smallest integer as maxlead and the largest integer as
		 * minlag. */
		void varspan(int t, int& mlead, int& mlag) const;
		/** Return max lead and min lag for a variable given by the
		 * name (without lead, lag). The same is valid if the variable
		 * name cannot be found. */
		void varspan(const char* name, int& mlead, int& mlag) const;
		/** Return max lead and min lag for a vector of variables given by the names. */
		void varspan(const vector<const char*>& names, int& mlead, int& mlag) const;
		/** Return true for all tree indices corresponding to a
		 * variable in the sense of this class. (This is parameters,
		 * exo and endo). Since the semantics of 'variable' will be
		 * changed in subclasses, we use name 'named atom'. These are
		 * all atoms but constants. */
		bool is_named_atom(int t) const;
		/** Return index of the variable described by the variable
		 * name and lag/lead. If it doesn't exist, return -1. */
		int index(const char* name, int ll) const;
		/** Return the lag map for the variable name. */
		const Tlagmap& lagmap(const char* name) const;
		/** Return the variable name for the tree index. It throws an
		 * exception if the tree index t is not a named atom. */
		const char* name(int t) const;
		/** Return the lead/lag for the tree index. It throws an
		 * exception if the tree index t is not a named atom. */
		int lead(int t) const;
		/** Return maximum lead. */
		int get_maxlead() const
			{return maxlead;}
		/** Return minimum lag. */
		int get_minlag() const
			{return minlag;}
		/** Return the name storage to allow querying to other
		 * classes. */
		const NameStorage& get_name_storage() const
			{return varnames;}
		/** Assign the variable with a given lead. The varname must be
		 * from the varnames storage. The method checks if the
		 * variable iwht the given lead/lag is not assigned. If so, an
		 * exception is thrown. */
		void assign_variable(const char* varname, int ll, int t);
		/** Unassign the variable with a given lead and given tree
		 * index. The tree index is only provided as a check. An
		 * exception is thrown if the name, ll, and the tree index t
		 * are not consistent. The method also updates nv, indices,
		 * maxlead and minlag. The varname must be from the varnames
		 * storage. */
		void unassign_variable(const char* varname, int ll, int t);
		/** Debug print. */
		void print() const;
	protected:
		/** Do the check for the variable. A subclass may need to
		 * reimplement this so that it could raise an error if the
		 * variable is not among a given list. */
		virtual int check_variable(const char* name) const;
		/** Assign the constant. */
		void assign_constant(const char* name, int t);
		/** Assign the variable. */
		void assign_variable(const char* name, int t);
		/** The method just updates minlag or/and maxlead. Note that
		 * when assigning variables, the update is done when inserting
		 * to the maps, however, if removing a variable, we need to
		 * call this method. */
		void update_minmaxll();
		/** The method parses the string to recover a variable name
		 * and lag/lead ll. The variable name doesn't contain a lead/lag. */
		virtual void parse_variable(const char* in, string& out, int& ll) const = 0;
	public:
		/** Return true if the str represents a double.*/ 
		static bool is_string_constant(const char* str);
	};


	/** This class is a parent of all orderings of the dynamic atoms
	 * of variables which can appear before t, at t, or after t. It
	 * encapsulates the ordering, and the information about the number
	 * of static (appearing only at time t) predetermined (appearing
	 * before t and possibly at t), both (appearing before t and after
	 * t and possibly at t) and forward looking (appearing after t and
	 * possibly at t).
	 *
	 * The constructor takes a list of variable names. The class also
	 * provides mapping from the ordering of the variables in the list
	 * (outer) to the new ordering (at time t) and back.
	 *
	 * The user of the subclass must call do_ordering() after
	 * initialization.
	 *
	 * The class contains a few preimplemented methods for
	 * ordering. The class is used in this way: Make a subclass, and
	 * implement pure virtual do_ordering() by just plugging a
	 * preimplemented method, or plugging your own implementation. The
	 * method do_ordering() is called by the user after the constructor.
	 */
	class VarOrdering {
	protected:
		/** Number of static variables. */
		int n_stat;
		/** Number of predetermined variables. */
		int n_pred;
		/** Number of both variables. */
		int n_both;
		/** Number of forward looking variables. */
		int n_forw;
		/** This is a set of tree indices corresponding to the
		 * variables at all times as they occur in the formulas. In
		 * fact, since this is used only for derivatives, the ordering
		 * of this vector is only important for ordering of the
		 * derivatives, in other contexts the ordering is not
		 * important, so it is rather a set of indices.*/
		vector<int> der_atoms;
		/** This maps tree index of the variable to the position in
		 * the row of the ordering. One should be careful with making
		 * space in the positions for variables not appearing at time
		 * t. For instance in the pred(t-1), both(t-1), stat(t),
		 * pred(t), both(t), forw(t), both(t+1), forw(t+1) ordering,
		 * the variables x(t-1), y(t-1), x(t+1), z(t-1), z(t), and
		 * z(t+1) having tree indices 6,5,4,3,2,1 will be ordered as
		 * follows: y(t-1), x(t-1), z(t-1), [y(t)], [x(t)], z(t),
		 * x(t+1), where a bracketed expresion means non-existent by
		 * occupying a space. The map thus will look as follows:
		 * {5->0, 6->1, 3->2, 2->5, 3->6}. Note that nothing is mapped
		 * to positions 3 and 4. */ 
		map<int,int> positions;
		/** This maps an ordering of the list of variables in
		 * constructor to the new ordering (at time t). The length is
		 * the number of variables. */
		vector<int> outer2y;
		/** This maps a new ordering to the ordering of the list of
		 * variables in constructor (at time t). The length is the
		 * number of variables. */
		vector<int> y2outer;
		/** This is just a reference for variable names to keep it
		 * from constructor to do_ordering() implementations. */
		const vector<const char*>& varnames;
		/** This is just a reference to atoms to keep it from
		 * constructor to do_ordering() implementations. */
		const DynamicAtoms& atoms;
	public:
		/** This is an enum type for an ordering type implemented by
		 * do_general. */
		enum ord_type {pbspbfbf, bfspbfpb};
		/** Construct the ordering of the variables given by the names
		 * with their dynamic occurrences defined by the atoms. It
		 * calls the virtual method do_ordering which can be
		 * reimplemented. */
		VarOrdering(const vector<const char*>& vnames, const DynamicAtoms& a)
			: n_stat(0), n_pred(0), n_both(0), n_forw(0), varnames(vnames), atoms(a)
			{}
		VarOrdering(const VarOrdering& vo, const vector<const char*>& vnames,
					const DynamicAtoms& a);
		virtual VarOrdering* clone(const vector<const char*>& vnames,
								   const DynamicAtoms& a) const = 0;
		/** Destructor does nothing here. */
		virtual ~VarOrdering() {}
		/** This is the method setting the ordering and the map. A
		 * subclass must reimplement it, possibly using a
		 * preimplemented ordering. This method must be called by the
		 * user after the class has been created. */
		virtual void do_ordering() = 0;
		/** Return number of static. */
		int nstat() const
			{return n_stat;}
		/** Return number of predetermined. */
		int npred() const
			{return n_pred;}
		/** Return number of both. */
		int nboth() const
			{return n_both;}
		/** Return number of forward looking. */
		int nforw() const
			{return n_forw;}
		/** Return the set of tree indices for derivatives. */
		const vector<int>& get_der_atoms() const
			{return der_atoms;}
		/** Return the y2outer. */
		const vector<int>& get_y2outer() const
			{return y2outer;}
		/** Return the outer2y. */
		const vector<int>& get_outer2y() const
			{return outer2y;}
		/** Query the atom given by the tree index. True is returned
		 * if the atom is one of the variables in the object. */
		bool check(int t) const;
		/** Return the position of the atom (nulary term) given by a
		 * tree index. It is a lookup to the map. If the atom cannot
		 * be found, the exception is raised. */
		int get_pos_of(int t) const;
		/** This returns a length of ordered row of atoms. In all
		 * cases so far, it does not depend on the ordering and it is
		 * as follows. */
		int length() const
			{return n_stat+2*n_pred+3*n_both+2*n_forw;}
		/** Debug print. */
		void print() const;
	protected:
		/** This is a general ordering method which orders the
		 * variables by the given ordering ord_type. See documentation
		 * for respective do_ methods. */
		void do_general(ord_type ordering);
		/** This is a preimplemented ordering for do_ordering()
		 * method. It assumes that the variables appear only at time
		 * t-1, t, t+1. It orders the atoms as pred(t-1), both(t-1),
		 * stat(t), pred(t), both(t), forw(t), both(t+1),
		 * forw(t+1). It builds the der_atoms, the map of positions,
		 * as well as y2outer and outer2y. */
		void do_pbspbfbf()
			{do_general(pbspbfbf);}
		/** This is a preimplemented ordering for do_ordering()
		 * method. It assumes that the variables appear only at time
		 * t-1, t, t+1. It orders the atoms as both(t+1), forw(t+1),
		 * stat(t), pred(t), both(t), forw(t), pred(t-1),
		 * both(t-1). It builds the der_atoms, the map of positions,
		 * as well as y2outer and outer2y. */
		void do_bfspbfpb()
			{do_general(bfspbfpb);}
		/** This is a preimplemented ordering for do_ordering()
		 * method. It makes no assumptions about occurences of
		 * variables at different times. It orders the atoms with
		 * increasing time keeping the given ordering within one
		 * time. This implies that y2outer and outer2y will be
		 * identities. The der_atoms will be just a sequence of atoms
		 * from the least to the most time preserving the order of atoms
		 * within one time. */
		void do_increasing_time();
	private:
		/** Declare this copy constructor as private to hide it. */
		VarOrdering(const VarOrdering& vo);
	};

};

#endif

// Local Variables:
// mode:C++
// End: