Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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$
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 struct definition_t;
35 
36 namespace qucs {
37 
38 class strlist;
39 class dataset;
40 class range;
41 
42 namespace eqn {
43 
44 class solver;
45 class checker;
46 class constant;
47 class reference;
48 class assignment;
49 class application;
50 
51 enum NodeTag {
52  UNKNOWN = -1,
53  CONSTANT = 0, /* source code constant */
54  REFERENCE, /* variable reference */
55  APPLICATION, /* call to function */
56  ASSIGNMENT /* root of equation */
57 };
58 
59 /* The equation node base class defines and implements the basic
60  functionality of an equation node. Possible types of nodes are
61  listed in 'NodeTag'. */
62 class node
63 {
64 public:
65  node ();
66  node (int);
67  node (const node &);
68  virtual ~node ();
69  node * getNext (void) { return next; }
70  void setNext (node * n) { next = n; }
71  int count (void);
72  void append (node *);
73  void appendNodes (node *);
74  void setDependencies (strlist *);
75  strlist * getDependencies (void);
76  void setDataDependencies (strlist *);
77  strlist * getDataDependencies (void) { return dataDependencies; }
78  void setDropDependencies (strlist * deps) { dropDependencies = deps; }
79  void addDropDependencies (char *);
80  strlist * getDropDependencies (void) { return dropDependencies; }
81  void setPrepDependencies (strlist * deps) { prepDependencies = deps; }
82  void addPrepDependencies (char *);
83  void appendPrepDependencies (strlist *);
84  strlist * getPrepDependencies (void) { return prepDependencies; }
85  strlist * recurseDependencies (checker *, strlist *);
86  node * get (int);
87  constant * getResult (int);
88  int getType (void) { return type; }
89  int getTag (void) { return tag; }
90  void setType (int tag) { type = tag; }
91  constant * getResult (void) { return res; }
92  nr_double_t getResultDouble (void);
95  void setResult (constant *);
96  char * getInstance (void);
97  void setInstance (const char *);
98  void applyInstance (void);
99  constant * calculate (void);
100  strlist * collectDependencies (void);
101  strlist * collectDataDependencies (void);
102 
103  /* These functions should be overloaded by derivative classes. */
104  virtual void print (void) { }
105  virtual void addDependencies (strlist *) { }
106  virtual int evalType (void) { return type; }
107  virtual char * toString (void) { return txt; }
108  virtual constant * evaluate (void) { return res; }
109  virtual node * differentiate (char *) { return this; }
110  virtual node * recreate (void) { return new node (*this); }
111  virtual void replace (char *, char *) { }
112 
113 public:
115  int cycle;
117  int skip;
118  char * txt;
120  char * instance;
121  int output;
122  int dropdeps;
125 
126 private:
127  int type;
128  int tag;
130  strlist * dependencies;
132  strlist * dataDependencies;
133  strlist * dropDependencies;
134  strlist * prepDependencies;
135 };
136 
139  TAG_DOUBLE = 1, /* double constant */
140  TAG_COMPLEX = 2, /* complex value */
141  TAG_VECTOR = 4, /* list of complex values */
142  TAG_MATRIX = 8, /* complex matrix */
143  TAG_MATVEC = 16, /* list of complex matrices */
144  TAG_CHAR = 32, /* single character */
145  TAG_STRING = 64, /* character string */
146  TAG_RANGE = 128, /* interval specification */
147  TAG_BOOLEAN = 256, /* boolean value */
148 };
149 
150 /* This class represents any type of constant expression. */
151 class constant : public node
152 {
153 public:
154  constant ();
155  constant (int);
156  constant (const constant &);
157  ~constant ();
158  void print (void);
159  int evalType (void);
160  char * toString (void);
161  constant * evaluate (void);
162  node * differentiate (char *);
163  node * recreate (void);
164 
165 public:
166  bool dataref;
167  int type;
168  union {
169  nr_double_t d;
172  matrix * m;
173  matvec * mv;
174  char chr;
175  char * s;
176  range * r;
177  bool b;
178  };
179 };
180 
181 /* The class represents variable references. */
182 class reference : public node
183 {
184 public:
185  reference ();
186  reference (const reference &);
187  ~reference ();
188  void print (void);
189  void addDependencies (strlist *);
190  void findVariable (void);
191  int evalType (void);
192  char * toString (void);
193  constant * evaluate (void);
194  node * differentiate (char *);
195  node * recreate (void);
196  void replace (char *, char *);
197 
198 public:
199  char * n;
201 };
202 
203 /* This class represents assignments with a left hand and right hand
204  side. */
205 class assignment : public node
206 {
207 public:
208  assignment ();
209  assignment (const assignment &);
210  ~assignment ();
211  void print (void);
212  void addDependencies (strlist *);
213  int evalType (void);
214  char * toString (void);
215  constant * evaluate (void);
216  node * differentiate (char *);
217  node * recreate (void);
218  void replace (char *, char *);
219  void rename (char *);
220  void mul (assignment *);
221  void add (assignment *);
222  void mulref (assignment *);
223 
224 public:
225  char * result;
227 };
228 
229 // Type of application function.
230 typedef constant * (* evaluator_t) (constant *);
231 
232 // Type of derivative function.
233 typedef node * (* differentiator_t) (application *, char *);
234 
235 /* The application class represents any kind of operation (unary,
236  binary and n-ary ones) containing the appropriate argument list. */
237 class application : public node
238 {
239 public:
240  application ();
241  application (const application &);
242  application (const char *, int);
243  ~application ();
244  void print (void);
245  void addDependencies (strlist *);
246  int evalType (void);
247  char * toString (void);
248  constant * evaluate (void);
249  node * differentiate (char *);
250  node * recreate (void);
251  void replace (char *, char *);
252 
253 public:
254  char * n;
255  int nargs;
260 
261 private:
262  void evalTypeArgs (void);
263  char * createKey (void);
264  int evalTypeFast (void);
265  int findDifferentiator (void);
266 };
267 
268 /* This class implements the actual functionality regarding a set of
269  equations stored within a netlist. */
270 class checker
271 {
272 public:
273  checker ();
274  ~checker ();
275  void collectDependencies (void);
276  void collectDependencies (node *);
277  void setEquations (node *);
278  node * getEquations (void) { return equations; }
279  void list (void);
280  int findUndefined (int);
281  static const char * tag2key (int);
282  static int isGenerated (char *);
283  strlist * getVariables (void);
284  int findDuplicate (void);
285  static node * findEquation (node *, char *);
286  int detectCycles (void);
287  static strlist * foldDependencies (strlist *);
288  node * appendEquation (node *, node *);
289  void dropEquation (node *);
290  void reorderEquations (void);
291  static node * lastEquation (node *);
292  int applyTypes (void);
293  int checkExport (void);
294  void constants (void);
295  int check (int noundefined = 1);
296  strlist * variables (void);
297  node * addDouble (const char *, const char *, nr_double_t);
298  node * createDouble (const char *, const char *, nr_double_t);
299  node * addComplex (const char *, const char *, nr_complex_t);
300  node * createComplex (const char *, const char *, nr_complex_t);
301  node * addReference (const char *, const char *, char *);
302  node * createReference (const char *, const char *, char *);
303  void appendEquation (node *);
304  void addEquation (node *);
305  node * findEquation (char *);
306  bool containsVariable (char *);
307  nr_double_t getDouble (char *);
308  void setDouble (char *, nr_double_t);
309  qucs::vector getVector (char *);
310  void setDefinitions (struct definition_t * d) { defs = d; }
311  struct definition_t * getDefinitions (void) { return defs; }
312  node * findProperty (char *);
313 
314 public:
316 
317  private:
318  bool consts;
319  struct definition_t * defs;
320 };
321 
322 /* The solver class is finally used to solve the list of equations. */
323 class solver
324 {
325 public:
326  solver (checker *);
327  ~solver ();
328  void setEquations (node * eqn) { equations = eqn; }
329  node * getEquations (void) { return equations; }
330  void setData (dataset * d) { data = d; }
331  dataset * getDataset (void) { return data; }
332  void evaluate (void);
333  node * addEquationData (qucs::vector *, bool ref = false);
334  node * addEquationData (matvec *);
335  node * addGeneratedEquation (qucs::vector *, const char *);
337  void checkinDataset (void);
338  void checkoutDataset (void);
339  static int dataSize (constant *);
340  int getDependencySize (strlist *, int);
341  int getDataSize (char *);
342  strlist * collectDataDependencies (node *);
343  int dataSize (strlist *);
344  qucs::vector * getDataVector (char *);
346  char * isMatrixVector (char *, int&, int&);
347  int findEquationResult (node *);
348  int solve (dataset *);
349 
350 public:
352 
353 private:
354  dataset * data;
357 };
358 
359 } /* namespace eqn */
360 
361 } // namespace qucs
362 
363 #endif /* __EQUATION_H__ */
node * recreate(void)
Definition: equation.cpp:636
int applyTypes(void)
Definition: equation.cpp:1823
std::complex< nr_double_t > nr_complex_t
Definition: complex.h:31
void addPrepDependencies(char *)
Definition: equation.cpp:1302
qucs::vector * dataVector(node *)
Definition: equation.cpp:1959
void appendPrepDependencies(strlist *)
Definition: equation.cpp:1310
static int dataSize(constant *)
Definition: equation.cpp:2170
node * addGeneratedEquation(qucs::vector *, const char *)
Definition: equation.cpp:1936
void addDependencies(strlist *)
Definition: equation.cpp:755
node * addDouble(const char *, const char *, nr_double_t)
Definition: equation.cpp:2490
qucs::vector * v
Definition: equation.h:171
solver * solvee
Definition: equation.h:123
int getDataSize(char *)
Definition: equation.cpp:2189
strlist * collectDataDependencies(node *)
Definition: equation.cpp:2255
node * getNext(void)
Definition: equation.h:69
void setResult(constant *)
Definition: equation.cpp:1120
void setData(dataset *d)
Definition: equation.h:330
int check(int noundefined=1)
Definition: equation.cpp:1845
void setDependencies(strlist *)
Definition: equation.cpp:1219
solver(checker *)
Definition: equation.cpp:1862
static int isGenerated(char *)
Definition: equation.cpp:1496
bool containsVariable(char *)
Definition: equation.cpp:2433
void mulref(assignment *)
Definition: equation.cpp:535
char * toString(void)
Definition: equation.cpp:449
evaluator_t eval
Definition: equation.h:258
void setEquations(node *)
Definition: equation.cpp:1812
constant * res
Definition: equation.h:131
virtual void replace(char *, char *)
Definition: equation.h:111
int findDifferentiator(void)
Definition: equation.cpp:885
nr_double_t getResultDouble(void)
Definition: equation.cpp:1135
char * isMatrixVector(char *, int &, int &)
void findMatrixVectors(qucs::vector *)
Definition: equation.cpp:2058
n
Definition: parse_citi.y:147
node * addEquationData(qucs::vector *, bool ref=false)
Definition: equation.cpp:1920
int evalType(void)
Definition: equation.cpp:256
virtual void addDependencies(strlist *)
Definition: equation.h:105
checker * checkee
Definition: equation.h:356
void setDouble(char *, nr_double_t)
Definition: equation.cpp:2600
void evaluate(void)
Definition: equation.cpp:1882
nr_complex_t getResultComplex(void)
Definition: equation.cpp:1158
void checkoutDataset(void)
Definition: equation.cpp:2314
void setDataDependencies(strlist *)
Definition: equation.cpp:1317
void addDependencies(strlist *)
Definition: equation.cpp:459
void replace(char *, char *)
Definition: equation.cpp:298
static node * lastEquation(node *)
Definition: equation.cpp:1745
strlist * recurseDependencies(checker *, strlist *)
Definition: equation.cpp:1236
qucs::vector getVector(char *)
Definition: equation.cpp:2618
void setType(int tag)
Definition: equation.h:90
void rename(char *)
Definition: equation.cpp:429
checker * checkee
Definition: equation.h:124
strlist * dataDependencies
Definition: equation.h:132
node * differentiate(char *)
Definition: equation.cpp:488
void addEquation(node *)
Definition: equation.cpp:2509
constant * calculate(void)
Definition: equation.cpp:1324
node * createComplex(const char *, const char *, nr_complex_t)
Definition: equation.cpp:2547
void collectDependencies(void)
Definition: equation.cpp:1400
char * createKey(void)
Definition: equation.cpp:783
virtual int evalType(void)
Definition: equation.h:106
void append(node *)
Definition: equation.cpp:1093
node * getEquations(void)
Definition: equation.h:329
node * findProperty(char *)
Definition: equation.cpp:1562
dataset * data
Definition: equation.h:354
node * addComplex(const char *, const char *, nr_complex_t)
Definition: equation.cpp:2500
nr_double_t d
Definition: equation.h:169
char * getInstance(void)
Definition: equation.cpp:1064
static node * findEquation(node *, char *)
Definition: equation.cpp:1670
node * recreate(void)
Definition: equation.cpp:121
nr_complex_t * c
Definition: equation.h:170
strlist * collectDataDependencies(void)
Definition: equation.cpp:1352
int findUndefined(int)
Definition: equation.cpp:1513
void checkinDataset(void)
Definition: equation.cpp:2027
node * recreate(void)
Definition: equation.cpp:292
Dense matrix class header file.
virtual constant * evaluate(void)
Definition: equation.h:108
char * instance
Definition: equation.h:120
virtual char * toString(void)
Definition: equation.h:107
constant * evaluate(void)
Definition: equation.cpp:901
int findDuplicate(void)
Definition: equation.cpp:1633
void setDefinitions(struct definition_t *d)
Definition: equation.h:310
void setPrepDependencies(strlist *deps)
Definition: equation.h:81
virtual node * differentiate(char *)
Definition: equation.h:109
virtual void print(void)
Definition: equation.h:104
void replace(char *, char *)
Definition: equation.cpp:642
void replace(char *, char *)
Definition: equation.cpp:423
strlist * getDataDependencies(void)
Definition: equation.h:77
qucs::vector * getDataVector(char *)
Definition: equation.cpp:2228
char * toString(void)
Definition: equation.cpp:320
virtual ~node()
Definition: equation.cpp:1046
qucs::vector getResultVector(void)
Definition: equation.cpp:1181
static strlist * foldDependencies(strlist *)
Definition: equation.cpp:1719
virtual node * recreate(void)
Definition: equation.h:110
void setEquations(node *eqn)
Definition: equation.h:328
static const char * tag2key(int)
int getDependencySize(strlist *, int)
Definition: equation.cpp:2198
constant * getResult(void)
Definition: equation.h:91
void evalTypeArgs(void)
Definition: equation.cpp:766
node * recreate(void)
Definition: equation.cpp:417
void setInstance(const char *)
Definition: equation.cpp:1057
strlist * collectDependencies(void)
Definition: equation.cpp:1343
void addDropDependencies(char *)
Definition: equation.cpp:1294
void findVariable(void)
Definition: equation.cpp:335
strlist * getVariables(void)
Definition: equation.cpp:1620
node * createDouble(const char *, const char *, nr_double_t)
Definition: equation.cpp:2528
void setNext(node *n)
Definition: equation.h:70
List int
Definition: parse_citi.y:183
node * addReference(const char *, const char *, char *)
Definition: equation.cpp:2480
node * equations
Definition: equation.h:351
node * differentiate(char *)
Definition: equation.cpp:982
int solve(dataset *)
Definition: equation.cpp:2400
strlist * dependencies
Definition: equation.h:130
void reorderEquations(void)
Definition: equation.cpp:1770
range
Definition: parse_vcd.y:206
node * createReference(const char *, const char *, char *)
Definition: equation.cpp:2566
int getTag(void)
Definition: equation.h:89
void applyInstance(void)
Definition: equation.cpp:1072
strlist * dropDependencies
Definition: equation.h:133
void dropEquation(node *)
Definition: equation.cpp:1753
int getType(void)
Definition: equation.h:88
strlist * getDependencies(void)
Definition: equation.cpp:1226
char * toString(void)
Definition: equation.cpp:672
node *(* differentiator_t)(application *, char *)
strlist * getPrepDependencies(void)
Definition: equation.h:84
int checkExport(void)
Definition: equation.cpp:1420
strlist * variables(void)
Definition: equation.cpp:2422
char * toString(void)
Definition: equation.cpp:180
dataset * getDataset(void)
Definition: equation.h:331
struct definition_t * defs
Definition: equation.h:319
void constants(void)
Definition: equation.cpp:2462
constant * evaluate(void)
Definition: equation.cpp:379
node * appendEquation(node *, node *)
Definition: equation.cpp:1732
int detectCycles(void)
Definition: equation.cpp:1692
node * differentiate(char *)
Definition: equation.cpp:269
int count(void)
Definition: equation.cpp:1085
void appendNodes(node *)
Definition: equation.cpp:1103
constant *(* evaluator_t)(constant *)
Definition: equation.h:230
void mul(assignment *)
Definition: equation.cpp:506
node * differentiate(char *)
Definition: equation.cpp:391
nr_double_t getDouble(char *)
Definition: equation.cpp:2586
void add(assignment *)
Definition: equation.cpp:563
struct definition_t * getDefinitions(void)
Definition: equation.h:311
int findEquationResult(node *)
Definition: equation.cpp:2371
void addDependencies(strlist *)
Definition: equation.cpp:328
void setDropDependencies(strlist *deps)
Definition: equation.h:78
constant * evaluate(void)
Definition: equation.cpp:262
node * getEquations(void)
Definition: equation.h:278
constant * evaluate(void)
Definition: equation.cpp:473
void print(void)
Definition: equation.cpp:157
differentiator_t derive
Definition: equation.h:259
strlist * prepDependencies
Definition: equation.h:134
strlist * getDropDependencies(void)
Definition: equation.h:80