55 #define A(a) ((assignment *) (a))
56 #define N(n) ((node *) (n))
57 #define C(c) ((constant *) (c))
58 #define R(r) ((reference *) (r))
153 static char str[256];
154 if (
imag (c) == 0.0) {
155 sprintf (str,
"%g", (
double)
real (c));
158 sprintf (str,
"(%g%cj%g)", (
double )
real (c),
159 imag (c) >= 0.0 ?
'+' :
'-', (
double) fabs (
imag (c)));
168 if (
txt != NULL) free (
txt);
171 sprintf (str,
"%d",
b ? 1 : 0);
175 sprintf (str,
"%g", (
double)
d);
179 txt = strdup (Cplx2String (*c));
183 int pos = 1, len = 3 +
v->
getSize () - 1;
184 txt = (
char *) malloc (len);
187 char *
s = Cplx2String (
v->
get (
i));
188 txt = (
char *) realloc (txt, len += strlen (s));
189 strcpy (&txt[pos], s); pos += strlen (s);
190 if (
i !=
v->
getSize () - 1) strcpy (&txt[pos++],
";");
192 strcpy (&
txt[pos],
"]");
198 txt = (
char *) malloc (len);
201 for (
int c = 0; c <
m->
getCols (); c++) {
202 char *
s = Cplx2String (
m->
get (
r, c));
203 txt = (
char *) realloc (txt, len += strlen (s));
205 if (c !=
m->
getCols () - 1) strcat (txt,
",");
213 sprintf (str,
"[%dx%d](%d)",
218 sprintf (str,
"'%c'",
chr);
222 sprintf (str,
"'%s'",
s);
229 txt = strdup (
"(no such type)");
262 n = o.
n ? strdup (o.
n) : NULL;
273 if (!strcmp (src,
n)) {
275 n = dst ? strdup (dst) : NULL;
309 if (!strcmp (
n,
A(eqn)->result)) {
317 if (!strcmp (
n,
A(eqn)->result)) {
349 if (
n != NULL && !strcmp (
n, derivative))
382 result = n ? strdup (n) : NULL;
400 txt = (
char *) malloc (strlen (
result) + strlen (str) + 4);
432 char *
txt = (
char *) malloc (strlen (
result) + strlen (derivative) + 4);
433 sprintf (txt,
"d%s_d%s",
result, derivative);
441 #define D(con) (C(con)->d)
442 #define isConst(n) ((n)->getTag()==CONSTANT && C(n)->getType()==TAG_DOUBLE)
443 #define isZero(n) (isConst(n) && D(n) == 0.0)
444 #define isOne(n) (isConst(n) && D(n) == 1.0)
445 #define defCon(res,val) res = new constant (TAG_DOUBLE); C(res)->d = val;
451 delete body;
delete factor;
456 }
else if (
isOne (factor)) {
477 }
else if (
isOne (factor)) {
491 delete body;
delete factor;
496 }
else if (
isZero (factor)) {
520 n = func ? strdup (func) : NULL;
531 n = o.n ? strdup (o.n) : NULL;
533 if (o.args != NULL) {
535 args = arg->recreate ();
554 arg->replace (src, dst);
562 for (
node * arg =
args; arg != NULL; arg = next) {
566 if ((res =
getResult ()) != NULL)
delete res;
580 if ((!strcmp (
n,
"+") || !strcmp (
n,
"-") || !strcmp (
n,
"*") ||
581 !strcmp (
n,
"/") || !strcmp (
n,
"^") || !strcmp (
n,
"%") ||
582 !strcmp (
n,
"<") || !strcmp (
n,
">") || !strcmp (
n,
"<=") ||
583 !strcmp (
n,
">=") || !strcmp (
n,
"&&") || !strcmp (
n,
"||") ||
584 !strcmp (
n,
"==") || !strcmp (
n,
"!="))
588 txt = (
char *) malloc (strlen (
n) + strlen (arg1) + strlen (arg2) + 3);
589 sprintf (
txt,
"(%s%s%s)", arg1,
n, arg2);
592 else if (!strcmp (
n,
"?:")) {
596 txt = (
char *) malloc (strlen (arg3) + strlen (arg1) + strlen (arg2) + 5);
597 sprintf (
txt,
"(%s?%s:%s)", arg1, arg2, arg3);
600 else if (!strcmp (
n,
"array")) {
602 txt = (
char *) malloc (len);
605 char * str = arg->toString ();
606 txt = (
char *) realloc (txt, len += strlen (str));
608 if (arg->
getNext ()) strcat (txt,
",");
613 else if (!strcmp (
n,
"vector") || !strcmp (
n,
"matrix")) {
614 int len = 3 +
nargs - 1;
615 txt = (
char *) malloc (len);
619 txt = (
char *) realloc (
txt, len++);
622 char * str = arg->toString ();
623 txt = (
char *) realloc (
txt, len += strlen (str));
633 int len = strlen (
n) + 3 +
nargs - 1;
634 txt = (
char *) malloc (len);
635 sprintf (
txt,
"%s(",
n);
637 char * str = arg->toString ();
638 txt = (
char *) realloc (
txt, len += strlen (str));
651 arg->addDependencies (depends);
657 void application::evalTypeArgs (
void) {
672 char * application::createKey (
void) {
673 char * key = (
char *) calloc (1, strlen (
n) +
nargs * 3 + 5);
684 int application::evalTypeFast (
void) {
685 char * key = createKey ();
699 #define isDDX() (nargs == 2 && !strcmp (n, "ddx") && \
700 args->getNext()->getTag () == REFERENCE)
716 findDifferentiator ();
726 if (app->
nargs >= 0) {
736 if (!(arg->getType () & app->
args[nr])) { nr = -1;
break; }
738 if (nr == -1)
continue;
741 if (app->
eval == NULL)
continue;
758 int application::findDifferentiator (
void) {
785 if (arg->evaluated == 0 || 1) {
788 if (arg->getResult () == NULL) {
791 "`%s'\n", arg->toString ());
795 "`%s'\n", arg->toString ());
801 if (arg->getResult()->dropdeps) {
802 strlist * preps = arg->getResult()->getPrepDependencies ();
818 if ((res =
getResult ()) != NULL)
delete res;
844 return derive (
this, derivative);
854 dataDependencies = NULL;
855 dropDependencies = NULL;
856 prepDependencies = NULL;
870 dataDependencies = NULL;
871 dropDependencies = NULL;
872 prepDependencies = NULL;
887 dataDependencies = NULL;
888 dropDependencies = NULL;
889 prepDependencies = NULL;
899 if (dependencies)
delete dependencies;
900 if (dataDependencies)
delete dataDependencies;
901 if (dropDependencies)
delete dropDependencies;
902 if (prepDependencies)
delete prepDependencies;
924 if (
n->getInstance () == NULL)
934 for (
node *
n =
this;
n != NULL;
n =
n->getNext ()) c++;
958 for (
int i = 0;
i < pos && n != NULL; n = n->
getNext (),
i++) ;
970 for (
int i = 0;
i < pos && n != NULL; n = n->
getNext (),
i++) ;
983 return real (*(c->
c));
break;
985 return c->
b ? 1.0 : 0.0;
break;
998 return rect (c->
d, 0.0);
break;
1000 return *(c->
c);
break;
1002 return c->
b ? 1.0 : 0.0;
break;
1019 for (co = 0; co < c->
m->
getCols (); co++)
1020 for (ro = 0; ro < c->
m->
getRows (); ro++)
1021 v (n++) = c->
m->
get (ro, co);
1027 v =
vector (1); v (0) = c->
d;
break;
1029 v =
vector (1); v (0) = *(c->
c);
break;
1031 v =
vector (1); v (0) = c->
b ? 1.0 : 0.0;
break;
1039 if (dependencies)
delete dependencies;
1040 dependencies = depends;
1045 return dependencies;
1065 for (
int i = 0;
i < deps->
length ();
i++) {
1066 char * var = deps->
get (
i);
1069 if (child != NULL) {
1070 if (child->
cycle == 0) {
1073 if (cdeps->
length () > 0) {
1075 if (sub)
delete sub; sub = res;
1095 if (sub)
delete (sub);
1103 if (dropDependencies == NULL) dropDependencies =
new strlist ();
1104 dropDependencies->
add (dep);
1110 if (prepDependencies == NULL) prepDependencies =
new strlist ();
1111 prepDependencies->
add (dep);
1117 if (prepDependencies == NULL) prepDependencies =
new strlist ();
1118 prepDependencies->
append (deps);
1123 if (dataDependencies != NULL)
delete dataDependencies;
1124 dataDependencies = deps ?
new strlist (*deps) : NULL;
1133 if (deps)
delete deps;
1181 next =
eqn->getNext ();
1187 #define foreach_equation(eqn) \
1188 for (assignment * (eqn) = A (equations); \
1189 (eqn) != NULL; (eqn) = A ((eqn)->getNext ()))
1217 if (!strcmp (eqn->result,
"Export")) {
1220 (strcmp (
R (eqn->body)->n,
"yes") &&
1221 strcmp (
R (eqn->body)->n,
"no"))) {
1223 "are `yes' or `no'\n", eqn->result);
1227 int flag = !strcmp (
R (eqn->body)->n,
"yes") ? 1 : 0;
1232 if (!strcmp (res->getInstance (),
i))
1234 if (!strcmp (res->result,
"Export") &&
1235 !strcmp (res->getInstance (),
i)) {
1242 "occurred %dx in `Eqn:%s'\n", eqn->result, found, i);
1277 int len = strlen (var);
1279 if (isdigit (var[len-1]) && isdigit (var[len-2]) &&
1280 isdigit (var[len-3]) && isdigit (var[len-4]) &&
1281 var[len-5] ==
'.') {
1296 for (
int i = 0;
i < depends->
length ();
i++) {
1297 char * var = depends->
get (
i);
1313 "equation `%s'\n", var, eqn->result);
1319 "equation `%s' not yet defined\n", var, eqn->result);
1337 char * ret, * inst, * prop;
1338 if ((ret =
strchr (var,
'.')) != NULL) {
1339 int len = ret - var;
1340 inst = (
char *) calloc (1, len + 1);
1341 memcpy (inst, var, len);
1342 prop = &var[len + 1];
1348 if (!strcmp (def->instance, inst)) {
1350 if (!strcmp (
pair->key, prop)) {
1352 if (
pair->value->ident != NULL) {
1367 "%dx, is not unique'\n", var, found);
1368 delete eqn; eqn = NULL;
1370 else if (found == 1)
1381 idents->
add (eqn->result);
1398 dups->
add (eqn->result);
1406 if (eqndups->duplicate > 1) {
1408 eqndups->result, eqndups->duplicate);
1421 for (
node * eqn = root; eqn != NULL; eqn = eqn->
getNext ()) {
1422 if (!strcmp (
A(eqn)->result, n))
return eqn;
1431 if (!strcmp (
A(eqn)->result, n))
return eqn;
1445 "`%s' involves: `%s'\n", eqn->result, deps->
toString ());
1463 for (
int i = 0; deps &&
i < deps->
length ();
i++) {
1464 char * var = deps->
get (
i);
1467 if (deps)
delete deps;
1485 for (eqn = root; eqn && eqn->
getNext () != NULL; eqn = eqn->
getNext ()) ;
1505 node * root = NULL, * next, * last;
1514 for (found = gens = i = 0; i < deps->
length (); i++) {
1515 char * var = deps->
get (i);
1520 if (found == (deps->
length () - gens)) {
1554 "undefined\n", eqn->result);
1619 #if TESTING_DERIVATIVE || 0
1649 char * str = (
char *) malloc (strlen (n) + 6);
1650 sprintf (str,
"%s.%04d", n, ++generated);
1694 for (
int r = 0; r < mv->
getRows (); r++) {
1695 for (
int c = 0; c < mv->
getCols (); c++) {
1708 for (
int r = 0; r < m->
getRows (); r++) {
1709 for (
int c = 0; c < m->
getCols (); c++) {
1729 if (
data == NULL)
return;
1758 int s, r,
c, a,
b, n = 1;
1761 for (vec = v; vec != NULL; vec = (
vector *) vec->
getNext ())
1766 r = c = s = -1; cand = NULL; deps = NULL;
1768 for (vec = v; vec != NULL; vec = (
vector *) vec->
getNext ()) {
1775 if (!strcmp (p, cand) && s == vec->
getSize ()) {
1803 for (vec = v; vec != NULL; vec = (
vector *) vec->
getNext ()) {
1807 mv->
set (*vec, a, b);
1824 free (cand); cand = NULL;
1828 }
while (cand != NULL);
1850 switch (eqn->getType ()) {
1852 size = eqn->getResult()->v->getSize ();
1855 size = eqn->getResult()->mv->getSize ();
1875 if (deps == NULL)
return 1;
1876 for (
int i = 0;
i < deps->
length () - idx;
i++) {
1887 for (
int i = 0; deps != NULL &&
i < deps->
length ();
i++) {
1888 char * str = deps->
get (
i);
1903 if ((var =
data->findVariable (str)) != NULL)
1905 if ((var =
data->findDependency (str)) != NULL)
1923 strlist * sub = NULL, * datadeps = NULL;
1928 datadeps = datadeps ?
new strlist (*datadeps) : NULL;
1930 for (
int i = 0; deps &&
i < deps->
length ();
i++) {
1931 char * var = deps->
get (
i);
1935 if (n == NULL && eqn->
solvee != NULL)
1948 if (datadeps)
delete datadeps;
1955 if (preps) datadeps->
add (preps);
1958 if (preps) datadeps->add (preps);
1965 if (datadeps->length () == 0) {
1975 if (
data == NULL)
return;
1980 if (!eqn->output)
continue;
1985 if (v == NULL)
continue;
1997 if (datadeps && datadeps->
length () > 0) {
2000 data->applyDependencies (v);
2001 data->addVariables (v);
2004 data->addVariable (v);
2010 data->addDependencies (v);
2012 data->addDependency (v);
2026 for (
int r = 0; r < mv->
getRows (); r++) {
2027 for (
int c = 0; c < mv->
getCols (); c++) {
2029 if (
data->findDependency (str) ||
data->findVariable (str))
2036 char * str =
A(eqn)->result;
2037 if (
data->findDependency (str) ||
data->findVariable (str))
2053 if (checkee->
check (data ? 1 : 0) != 0) {
2069 idents->
add (eqn->result);
2077 if (!strcmp (ident, eqn->result))
2090 static struct pconstant pconstants[] = {
2106 for (
int i = 0; pconstants[
i].
ident != NULL;
i++) {
2126 nr_double_t
value) {
2160 nr_double_t
value) {
2168 a->
result = strdup (ident);
2186 a->
result = strdup (ident);
2200 r->
n = strdup (value);
2204 a->
result = strdup (ident);
2216 if (!strcmp (ident, eqn->result)) {
2227 if (!strcmp (ident, eqn->result)) {
2241 if (!strcmp (ident, eqn->result)) {