My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
equation.h
Go to the documentation of this file.
1 /*
2  * equation.h - checker definitions for Qucs equations
3  *
4  * Copyright (C) 2004-2009 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id: equation.h 1825 2011-03-11 20:42:14Z ela $
22  *
23  */
24 
25 #ifndef __EQUATION_H__
26 #define __EQUATION_H__
27 
28 #include "object.h"
29 #include "complex.h"
30 #include "vector.h"
31 #include "matrix.h"
32 #include "matvec.h"
33 
34 class strlist;
35 class dataset;
36 class range;
37 
38 namespace eqn {
39 
40 class solver;
41 class checker;
42 class constant;
43 class reference;
44 class assignment;
45 class application;
46 
47 enum NodeTag {
48  UNKNOWN = -1,
49  CONSTANT = 0, /* source code constant */
50  REFERENCE, /* variable reference */
51  APPLICATION, /* call to function */
52  ASSIGNMENT /* root of equation */
53 };
54 
55 /* The equation node base class defines and implements the basic
56  functionality of an equation node. Possible types of nodes are
57  listed in 'NodeTag'. */
58 class node
59 {
60 public:
61  node ();
62  node (int);
63  node (const node &);
64  virtual ~node ();
65  node * getNext (void) { return next; }
66  void setNext (node * n) { next = n; }
67  int count (void);
68  void append (node *);
69  void appendNodes (node *);
70  void setDependencies (strlist *);
71  strlist * getDependencies (void);
73  strlist * getDataDependencies (void) { return dataDependencies; }
74  void setDropDependencies (strlist * deps) { dropDependencies = deps; }
75  void addDropDependencies (char *);
76  strlist * getDropDependencies (void) { return dropDependencies; }
77  void setPrepDependencies (strlist * deps) { prepDependencies = deps; }
78  void addPrepDependencies (char *);
80  strlist * getPrepDependencies (void) { return prepDependencies; }
82  node * get (int);
83  constant * getResult (int);
84  int getType (void) { return type; }
85  int getTag (void) { return tag; }
86  void setType (int tag) { type = tag; }
87  constant * getResult (void) { return res; }
88  nr_double_t getResultDouble (void);
90  vector getResultVector (void);
91  void setResult (constant *);
92  char * getInstance (void);
93  void setInstance (const char *);
94  void applyInstance (void);
95  constant * calculate (void);
98 
99  /* These functions should be overloaded by derivative classes. */
100  virtual void print (void) { }
101  virtual void addDependencies (strlist *) { }
102  virtual int evalType (void) { return type; }
103  virtual char * toString (void) { return txt; }
104  virtual constant * evaluate (void) { return res; }
105  virtual node * differentiate (char *) { return this; }
106  virtual node * recreate (void) { return new node (*this); }
107  virtual void replace (char *, char *) { }
108 
109 public:
111  int cycle;
113  int skip;
114  char * txt;
116  char * instance;
117  int output;
118  int dropdeps;
121 
122 private:
123  int type;
124  int tag;
125  node * next;
126  strlist * dependencies;
127  constant * res;
128  strlist * dataDependencies;
129  strlist * dropDependencies;
130  strlist * prepDependencies;
131 };
132 
135  TAG_DOUBLE = 1, /* double constant */
136  TAG_COMPLEX = 2, /* complex value */
137  TAG_VECTOR = 4, /* list of complex values */
138  TAG_MATRIX = 8, /* complex matrix */
139  TAG_MATVEC = 16, /* list of complex matrices */
140  TAG_CHAR = 32, /* single character */
141  TAG_STRING = 64, /* character string */
142  TAG_RANGE = 128, /* interval specification */
143  TAG_BOOLEAN = 256, /* boolean value */
144 };
145 
146 /* This class represents any type of constant expression. */
147 class constant : public node
148 {
149 public:
150  constant ();
151  constant (int);
152  constant (const constant &);
153  ~constant ();
154  void print (void);
155  int evalType (void);
156  char * toString (void);
157  constant * evaluate (void);
158  node * differentiate (char *);
159  node * recreate (void);
160 
161 public:
162  bool dataref;
163  int type;
164  union {
165  nr_double_t d;
170  char chr;
171  char * s;
172  range * r;
173  bool b;
174  };
175 };
176 
177 /* The class represents variable references. */
178 class reference : public node
179 {
180 public:
181  reference ();
182  reference (const reference &);
183  ~reference ();
184  void print (void);
185  void addDependencies (strlist *);
186  void findVariable (void);
187  int evalType (void);
188  char * toString (void);
189  constant * evaluate (void);
190  node * differentiate (char *);
191  node * recreate (void);
192  void replace (char *, char *);
193 
194 public:
195  char * n;
197 };
198 
199 /* This class represents assignments with a left hand and right hand
200  side. */
201 class assignment : public node
202 {
203 public:
204  assignment ();
205  assignment (const assignment &);
206  ~assignment ();
207  void print (void);
208  void addDependencies (strlist *);
209  int evalType (void);
210  char * toString (void);
211  constant * evaluate (void);
212  node * differentiate (char *);
213  node * recreate (void);
214  void replace (char *, char *);
215  void rename (char *);
216  void mul (assignment *);
217  void add (assignment *);
218  void mulref (assignment *);
219 
220 public:
221  char * result;
223 };
224 
225 // Type of application function.
226 typedef constant * (* evaluator_t) (constant *);
227 
228 // Type of derivative function.
229 typedef node * (* differentiator_t) (application *, char *);
230 
231 /* The application class represents any kind of operation (unary,
232  binary and n-ary ones) containing the appropriate argument list. */
233 class application : public node
234 {
235 public:
236  application ();
237  application (const application &);
238  application (const char *, int);
239  ~application ();
240  void print (void);
241  void addDependencies (strlist *);
242  int evalType (void);
243  char * toString (void);
244  constant * evaluate (void);
245  node * differentiate (char *);
246  node * recreate (void);
247  void replace (char *, char *);
248 
249 public:
250  char * n;
251  int nargs;
256 
257 private:
258  void evalTypeArgs (void);
259  char * createKey (void);
260  int evalTypeFast (void);
261  int findDifferentiator (void);
262 };
263 
264 /* This class implements the actual functionality regarding a set of
265  equations stored within a netlist. */
266 class checker
267 {
268 public:
269  checker ();
270  ~checker ();
271  void collectDependencies (void);
272  void collectDependencies (node *);
273  void setEquations (node *);
274  node * getEquations (void) { return equations; }
275  void list (void);
276  int findUndefined (int);
277  static const char * tag2key (int);
278  static int isGenerated (char *);
279  strlist * getVariables (void);
280  int findDuplicate (void);
281  static node * findEquation (node *, char *);
282  int detectCycles (void);
283  static strlist * foldDependencies (strlist *);
284  node * appendEquation (node *, node *);
285  void dropEquation (node *);
286  void reorderEquations (void);
287  static node * lastEquation (node *);
288  int applyTypes (void);
289  int checkExport (void);
290  void constants (void);
291  int check (int noundefined = 1);
292  strlist * variables (void);
293  node * addDouble (const char *, const char *, nr_double_t);
294  node * createDouble (const char *, const char *, nr_double_t);
295  node * addComplex (const char *, const char *, nr_complex_t);
296  node * createComplex (const char *, const char *, nr_complex_t);
297  node * addReference (const char *, const char *, char *);
298  node * createReference (const char *, const char *, char *);
299  void appendEquation (node *);
300  void addEquation (node *);
301  node * findEquation (char *);
302  bool containsVariable (char *);
303  nr_double_t getDouble (char *);
304  void setDouble (char *, nr_double_t);
305  vector getVector (char *);
306  void setDefinitions (struct definition_t * d) { defs = d; }
307  struct definition_t * getDefinitions (void) { return defs; }
308  node * findProperty (char *);
309 
310 public:
312 
313  private:
314  bool consts;
315  struct definition_t * defs;
316 };
317 
318 /* The solver class is finally used to solve the list of equations. */
319 class solver
320 {
321 public:
322  solver (checker *);
323  ~solver ();
324  void setEquations (node * eqn) { equations = eqn; }
325  node * getEquations (void) { return equations; }
326  void setData (dataset * d) { data = d; }
327  dataset * getDataset (void) { return data; }
328  void evaluate (void);
329  node * addEquationData (vector *, bool ref = false);
331  node * addGeneratedEquation (vector *, const char *);
332  vector * dataVector (node *);
333  void checkinDataset (void);
334  void checkoutDataset (void);
335  static int dataSize (constant *);
336  int getDependencySize (strlist *, int);
337  int getDataSize (char *);
339  int dataSize (strlist *);
340  vector * getDataVector (char *);
341  void findMatrixVectors (vector *);
342  char * isMatrixVector (char *, int&, int&);
343  int findEquationResult (node *);
344  int solve (dataset *);
345 
346 public:
348 
349 private:
350  dataset * data;
351  int generated;
352  checker * checkee;
353 };
354 
355 } /* namespace */
356 
357 #endif /* __EQUATION_H__ */