57 #define A(a) ((assignment *) (a))
58 #define N(n) ((node *) (n))
59 #define C(c) ((constant *) (c))
60 #define R(r) ((reference *) (r))
165 static char str[256];
168 sprintf (str,
"%g", (
double)
real (c));
172 sprintf (str,
"(%g%cj%g)", (
double )
real (c),
173 imag (c) >= 0.0 ?
'+' :
'-', (
double) fabs (
imag (c)));
187 sprintf (str,
"%d",
b ? 1 : 0);
191 sprintf (str,
"%g", (
double)
d);
199 int pos = 1, len = 3 +
v->
getSize () - 1;
200 txt = (
char *) malloc (len);
205 txt = (
char *) realloc (txt, len += strlen (s));
206 strcpy (&txt[pos], s);
208 if (
i !=
v->
getSize () - 1) strcpy (&txt[pos++],
";");
210 strcpy (&
txt[pos],
"]");
215 int len = 3 + (
m->getRows () - 1) *
m->getCols () + (
m->getCols () - 1);
216 txt = (
char *) malloc (len);
218 for (
int r = 0;
r <
m->getRows ();
r++)
220 for (
int c = 0;
c <
m->getCols ();
c++)
223 txt = (
char *) realloc (txt, len += strlen (s));
225 if (
c !=
m->getCols () - 1) strcat (txt,
",");
227 if (
r !=
m->getRows () - 1) strcat (
txt,
";");
233 sprintf (str,
"[%dx%d](%d)",
234 mv->getRows (),
mv->getCols (),
mv->getSize ());
238 sprintf (str,
"'%c'",
chr);
242 sprintf (str,
"'%s'",
s);
246 txt = strdup (
r->toString ());
249 txt = strdup (
"(no such type)");
287 n = o.
n ? strdup (o.
n) : NULL;
300 if (!strcmp (src,
n))
303 n = dst ? strdup (dst) : NULL;
394 if (
n != NULL && !strcmp (
n, derivative))
432 result = n ? strdup (n) : NULL;
453 txt = (
char *) malloc (strlen (
result) + strlen (str) + 4);
490 char *
txt = (
char *) malloc (strlen (
result) + strlen (derivative) + 4);
491 sprintf (txt,
"d%s_d%s",
result, derivative);
499 #define D(con) (C(con)->d)
500 #define isConst(n) ((n)->getTag()==CONSTANT && C(n)->getType()==TAG_DOUBLE)
501 #define isZero(n) (isConst(n) && D(n) == 0.0)
502 #define isOne(n) (isConst(n) && D(n) == 1.0)
503 #define defCon(res,val) res = new constant (TAG_DOUBLE); C(res)->d = val;
520 else if (
isOne (factor))
549 else if (
isOne (factor))
606 n = func ? strdup (func) : NULL;
618 n = o.
n ? strdup (o.
n) : NULL;
646 arg->replace (src, dst);
657 next =
arg->getNext ();
676 if ((!strcmp (
n,
"+") || !strcmp (
n,
"-") || !strcmp (
n,
"*") ||
677 !strcmp (
n,
"/") || !strcmp (
n,
"^") || !strcmp (
n,
"%") ||
678 !strcmp (
n,
"<") || !strcmp (
n,
">") || !strcmp (
n,
"<=") ||
679 !strcmp (
n,
">=") || !strcmp (
n,
"&&") || !strcmp (
n,
"||") ||
680 !strcmp (
n,
"==") || !strcmp (
n,
"!="))
685 txt = (
char *) malloc (strlen (
n) + strlen (arg1) + strlen (arg2) + 3);
686 sprintf (
txt,
"(%s%s%s)", arg1,
n, arg2);
689 else if (!strcmp (
n,
"?:"))
694 txt = (
char *) malloc (strlen (arg3) + strlen (arg1) + strlen (arg2) + 5);
695 sprintf (
txt,
"(%s?%s:%s)", arg1, arg2, arg3);
698 else if (!strcmp (
n,
"array"))
701 txt = (
char *) malloc (len);
705 char * str =
arg->toString ();
706 txt = (
char *) realloc (txt, len += strlen (str));
708 if (
arg->getNext ()) strcat (txt,
",");
713 else if (!strcmp (
n,
"vector") || !strcmp (
n,
"matrix"))
715 int len = 3 +
nargs - 1;
716 txt = (
char *) malloc (len);
722 txt = (
char *) realloc (
txt, len++);
727 char * str =
arg->toString ();
728 txt = (
char *) realloc (
txt, len += strlen (str));
739 int len = strlen (
n) + 3 +
nargs - 1;
740 txt = (
char *) malloc (len);
741 sprintf (
txt,
"%s(",
n);
744 char * str =
arg->toString ();
745 txt = (
char *) realloc (
txt, len += strlen (str));
747 if (
arg->getNext ()) strcat (
txt,
",");
760 arg->addDependencies (depends);
779 #include "gperfapphash.cpp"
785 char *
key = (
char *) calloc (1, strlen (
n) +
nargs * 3 + 5);
800 struct appindex * idx = gperfapphash::get (key, strlen (key));
815 #define isDDX() (nargs == 2 && !strcmp (n, "ddx") && \
816 args->getNext()->getTag () == REFERENCE)
858 if (!(
arg->getType () & app->
args[nr]))
864 if (nr == -1)
continue;
867 if (app->
eval == NULL)
continue;
912 strlist * apreps =
new strlist ();
918 if (
arg->evaluated == 0 || 1)
922 if (
arg->getResult () == NULL)
927 "`%s'\n",
arg->toString ());
932 "`%s'\n",
arg->toString ());
939 if (
arg->getResult()->dropdeps)
941 strlist * preps =
arg->getResult()->getPrepDependencies ();
943 if (preps && (preps->length () > apreps->length ()))
946 apreps =
new strlist (*preps);
989 return derive (
this, derivative);
1077 if (
n->getInstance () == NULL)
1088 for (
node *
n =
this;
n != NULL;
n =
n->getNext ()) c++;
1115 for (
int i = 0;
i < pos && n != NULL; n = n->
getNext (),
i++) ;
1129 for (
int i = 0;
i < pos && n != NULL; n = n->
getNext (),
i++) ;
1146 return real (*(c->
c));
1149 return c->
b ? 1.0 : 0.0;
1172 return c->
b ? 1.0 : 0.0;
1193 for (co = 0; co < c->
m->getCols (); co++)
1194 for (ro = 0; ro < c->
m->getRows (); ro++)
1195 v (n++) = c->
m->get (ro, co);
1211 v (0) = c->
b ? 1.0 : 0.0;
1239 strlist *
res, * sub = NULL;
1242 if (deps->contains (
A(
this)->
result))
1244 res =
new strlist (*deps);
1250 for (
int i = 0;
i < deps->length ();
i++)
1252 char *
var = deps->get (
i);
1257 if (child->
cycle == 0)
1261 if (cdeps->length () > 0)
1264 if (sub)
delete sub;
1278 if (
cycle && sub && sub->length () > 0)
1287 if (sub)
delete (sub);
1331 if (deps)
delete deps;
1336 e->
setText (
"evaluator exception");
1345 strlist * depends =
new strlist ();
1388 next =
eqn->getNext ();
1394 #define foreach_equation(eqn) \
1395 for (assignment * (eqn) = A (equations); \
1396 (eqn) != NULL; (eqn) = A ((eqn)->getNext ()))
1411 strlist * depends =
new strlist ();
1427 next =
A (
eqn->getNext ());
1429 if (!strcmp (
eqn->result,
"Export"))
1433 (strcmp (
R (
eqn->body)->n,
"yes") &&
1434 strcmp (
R (
eqn->body)->n,
"no")))
1437 "are `yes' or `no'\n",
eqn->result);
1442 int flag = !strcmp (
R (
eqn->body)->n,
"yes") ? 1 : 0;
1443 char *
i =
eqn->getInstance ();
1448 if (!strcmp (res->getInstance (),
i))
1450 if (!strcmp (res->result,
"Export") &&
1451 !strcmp (res->getInstance (),
i))
1460 "occurred %dx in `Eqn:%s'\n",
eqn->result, found, i);
1498 int len = strlen (var);
1501 if (isdigit (var[len-1]) && isdigit (var[len-2]) &&
1502 isdigit (var[len-3]) && isdigit (var[len-4]) &&
1520 strlist * depends =
eqn->getDependencies ();
1521 for (
int i = 0;
i < depends->length ();
i++)
1523 char *
var = depends->get (
i);
1524 if (idents->contains (var) <= 0)
1532 idents->append (var);
1543 "equation `%s'\n", var,
eqn->result);
1550 "equation `%s' not yet defined\n", var,
eqn->result);
1569 char * ret, * inst, * prop;
1570 if ((ret =
strchr (var,
'.')) != NULL)
1572 int len = ret -
var;
1573 inst = (
char *) calloc (1, len + 1);
1574 memcpy (inst, var, len);
1575 prop = &var[len + 1];
1582 if (!strcmp (def->instance, inst))
1584 for (
struct pair_t * pair = def->pairs; pair; pair = pair->
next)
1586 if (!strcmp (pair->key, prop))
1590 if (pair->value->ident != NULL)
1598 eqn =
createDouble (
"#property", var, pair->value->value);
1608 "%dx, is not unique'\n", var, found);
1612 else if (found == 1)
1622 strlist * idents =
new strlist ();
1625 idents->add (
eqn->result);
1637 strlist * dups =
new strlist ();
1642 if (!
eqn->duplicate && dups->contains (
eqn->result) == 0)
1644 eqn->duplicate = idents->contains (
eqn->result);
1645 dups->add (
eqn->result);
1655 if (eqndups->duplicate > 1)
1658 eqndups->result, eqndups->duplicate);
1698 strlist * deps =
eqn->recurseDependencies (
this,
eqn->getDependencies ());
1699 if (deps->contains (
eqn->result) ||
eqn->cycle)
1702 "`%s' involves: `%s'\n",
eqn->result, deps->toString ());
1710 eqn->setDependencies (deps);
1721 strlist * res =
new strlist ();
1722 for (
int i = 0; deps &&
i < deps->length ();
i++)
1724 char *
var = deps->get (
i);
1725 if (!res->contains (var)) res->append (var);
1727 if (deps)
delete deps;
1748 for (eqn = root; eqn && eqn->
getNext () != NULL; eqn = eqn->
getNext ()) ;
1772 node * root = NULL, *
next, * last;
1777 strlist * deps =
eqn->getDependencies ();
1779 next =
eqn->getNext ();
1782 for (found = gens = i = 0; i < deps->length (); i++)
1784 char *
var = deps->get (i);
1789 if (found == (deps->length () - gens))
1795 eqn->evalPossible = 1;
1817 eqn->checkee =
this;
1828 if (
eqn->evalPossible)
1833 "undefined\n",
eqn->result);
1876 next =
eqn->getNext ();
1887 if (
eqn->evalPossible && !
eqn->skip )
1906 eqn->getResult () ?
eqn->getResult()->toString () :
"error");
1907 #if TESTING_DERIVATIVE || 0
1939 char * str = (
char *) malloc (strlen (n) + 6);
1940 sprintf (str,
"%s.%04d", n, ++
generated);
1987 for (
int r = 0;
r < mv->getRows ();
r++)
1989 for (
int c = 0;
c < mv->getCols ();
c++)
2004 for (
int r = 0;
r < m->getRows ();
r++)
2006 for (
int c = 0;
c < m->getCols ();
c++)
2010 t->
add (m->get (
r,
c));
2029 if (
data == NULL)
return;
2038 strlist * deps =
new strlist ();
2063 int s,
r,
c, a, b,
n = 1;
2086 if (!strcmp (p, cand) && s == vec->
getSize ())
2114 matvec * mv =
new matvec (s, r + 1, c + 1);
2123 mv->set (*vec, a, b);
2134 strlist * deps =
new strlist ();
2135 deps->add (mv->getName ());
2149 while (cand != NULL);
2160 assign->
result = strdup (mv->getName ());
2201 if (deps == NULL)
return 1;
2202 for (
int i = 0;
i < deps->length () - idx;
i++)
2215 for (
int i = 0; deps != NULL &&
i < deps->length ();
i++)
2217 char * str = deps->get (
i);
2234 if ((var =
data->findVariable (str)) != NULL)
2236 if ((var =
data->findDependency (str)) != NULL)
2257 strlist * sub = NULL, * datadeps = NULL;
2263 datadeps = datadeps ?
new strlist (*datadeps) : NULL;
2265 for (
int i = 0; deps &&
i < deps->length ();
i++)
2267 char *
var = deps->get (
i);
2271 if (n == NULL && eqn->
solvee != NULL)
2285 if (datadeps)
delete datadeps;
2293 if (preps) datadeps->add (preps);
2297 datadeps =
new strlist ();
2298 if (preps) datadeps->add (preps);
2305 if (datadeps->length () == 0)
2317 if (
data == NULL)
return;
2323 if (!
eqn->output)
continue;
2329 if (v == NULL)
continue;
2342 if (datadeps && datadeps->length () > 0)
2347 data->applyDependencies (v);
2348 data->addVariables (v);
2352 data->addVariable (v);
2359 data->addDependencies (v);
2361 data->addDependency (v);
2377 for (
int r = 0;
r < mv->getRows ();
r++)
2379 for (
int c = 0;
c < mv->getCols ();
c++)
2382 if (
data->findDependency (str) ||
data->findVariable (str))
2390 char * str =
A(eqn)->result;
2391 if (
data->findDependency (str) ||
data->findVariable (str))
2424 strlist * idents =
new strlist ();
2427 idents->add (
eqn->result);
2437 if (!strcmp (ident,
eqn->result))
2538 a->
result = strdup (ident);
2557 a->
result = strdup (ident);
2572 r->
n = strdup (value);
2576 a->
result = strdup (ident);
2590 if (!strcmp (ident,
eqn->result))
2592 return eqn->getResultDouble ();
2604 if (!strcmp (ident,
eqn->result))
2622 if (!strcmp (ident,
eqn->result))
2624 return eqn->getResultVector ();
#define M_E
Euler's constant ( )
std::complex< nr_double_t > nr_complex_t
void addPrepDependencies(char *)
qucs::vector * dataVector(node *)
matrix real(matrix a)
Real part matrix.
void appendPrepDependencies(strlist *)
static int dataSize(constant *)
node * addGeneratedEquation(qucs::vector *, const char *)
void addDependencies(strlist *)
node * addDouble(const char *, const char *, nr_double_t)
struct application_t applications[]
constant * getResult(int)
strlist * collectDataDependencies(node *)
void setResult(constant *)
int check(int noundefined=1)
void setDependencies(strlist *)
static int isGenerated(char *)
bool containsVariable(char *)
void mulref(assignment *)
void setEquations(node *)
Global physical constants header file.
virtual void replace(char *, char *)
int findDifferentiator(void)
nr_double_t getResultDouble(void)
#define catch_exception()
void findMatrixVectors(qucs::vector *)
node * addEquationData(qucs::vector *, bool ref=false)
virtual void addDependencies(strlist *)
static strlist * join(strlist *, strlist *)
void setDouble(char *, nr_double_t)
nr_complex_t getResultComplex(void)
void checkoutDataset(void)
void setDataDependencies(strlist *)
void addDependencies(strlist *)
void replace(char *, char *)
static node * lastEquation(node *)
#define throw_exception(e)
strlist * recurseDependencies(checker *, strlist *)
qucs::vector getVector(char *)
void setDependencies(strlist *)
strlist * dataDependencies
matrix imag(matrix a)
Imaginary part matrix.
node * differentiate(char *)
#define Q_e
Elementary charge ( )
void setName(const char *)
constant * calculate(void)
static struct pconstant pconstants[]
node * createComplex(const char *, const char *, nr_complex_t)
void collectDependencies(void)
virtual int evalType(void)
node * getEquations(void)
node * findProperty(char *)
node * addComplex(const char *, const char *, nr_complex_t)
#define kB
Boltzmann constant ( )
struct differentiation_t differentiations[]
static node * findEquation(node *, char *)
static char * Cplx2String(nr_complex_t c)
strlist * collectDataDependencies(void)
void print(const char *prefix=NULL)
#define M_PI
Archimedes' constant ( )
void checkinDataset(void)
Dense matrix class header file.
virtual constant * evaluate(void)
virtual char * toString(void)
constant * evaluate(void)
void setPrepDependencies(strlist *deps)
virtual node * differentiate(char *)
void replace(char *, char *)
void replace(char *, char *)
strlist * getDataDependencies(void)
qucs::vector * getDataVector(char *)
qucs::vector getResultVector(void)
static strlist * foldDependencies(strlist *)
virtual node * recreate(void)
static const char * tag2key(int)
int getDependencySize(strlist *, int)
constant * getResult(void)
void setInstance(const char *)
strlist * collectDependencies(void)
void addDropDependencies(char *)
strlist * getVariables(void)
node * createDouble(const char *, const char *, nr_double_t)
node * addReference(const char *, const char *, char *)
node * differentiate(char *)
void reorderEquations(void)
node * createReference(const char *, const char *, char *)
void setText(const char *,...)
strlist * dropDependencies
void dropEquation(node *)
strlist * getDependencies(void)
strlist * getPrepDependencies(void)
strlist * variables(void)
struct definition_t * defs
#define foreach_equation(eqn)
constant * evaluate(void)
node * appendEquation(node *, node *)
node * differentiate(char *)
void logprint(int level, const char *format,...)
struct definition_t * next
node * differentiate(char *)
nr_double_t getDouble(char *)
strlist * getDependencies(void)
int findEquationResult(node *)
void addDependencies(strlist *)
constant * evaluate(void)
node * getEquations(void)
constant * evaluate(void)
matrix arg(matrix a)
Computes the argument of each matrix element.
static char * isMatrixVector(char *, int &, int &)
strlist * prepDependencies
strlist * getDropDependencies(void)
static char * createMatrixString(const char *, int, int)