+ static map<node,ex> catalog; // lookup table for already evaluated subnodes
+ static unsigned prev_grad = 0; // grad argument that the catalog has been build for
+ if (grad>prev_grad) {
+ // We haven't computed far enough last time. Our catalog cannot cope with
+ // the demands for this value of grad so let's clear it.
+ catalog.clear();
+ prev_grad = grad;
+ }
+ ex product = 1; // accumulator for all the children
+ for (multiset<child>::const_iterator i=children.begin(); i!=children.end(); ++i) {
+ map<node,ex>::iterator pos = catalog.find(i->first);
+ if (pos==catalog.end()) {
+ pos = catalog.insert(map<node,ex>::value_type(i->first,i->first.evaluate(x,grad).series(x==0,grad).expand())).first;
+ if (grad<prev_grad) {
+ // We have just spoiled the catalog by inserting a series computed
+ // to lower grad than the others in it. So let's make sure next time
+ // we don't use one of the newly inserted ones by bumping prev_grad
+ // down to the current value of grad.
+ prev_grad = grad;
+ }
+ }