#include "exoutclass.h" #include using namespace std; bool exless::operator()(const ex&x,const ex&y) { int compresult=x.compare(y); if(!compresult && (x.info(info_flags::integer)+ y.info(info_flags::integer))==1) if(x.info(info_flags::integer)) return 1; else return 0; return compresult==-1; } Restargs restargs; ex rest_eval(const ex&x) { set::iterator loc=restargs.heap.find(x); if(loc==restargs.heap.end()) restargs.heap.insert(x); else return rest(*loc).hold(); return rest(x).hold(); } REGISTER_FUNCTION(rest,eval_func(rest_eval)) splitrestrest::splitrestrest(const ex&x) { second=1; if(!is_a(x)) first=x; else first=x.map(*this); } ex splitrestrest::operator()(const ex&x) { if(!x.match(rest(wild(0)))) return x; second=x.op(0); return 1; } ex Restify::operator()(const ex&x,const symbol&s_) { s=s_; return this->operator()(x); } ex Restify::operator()(const ex&x) { if(!x.has(s)) return rest(x); if(x==s) return x; if(is_a(x)||is_a(x)) return x.map(*this); if(is_a(x)) { ex tmp=x.map(*this); ex result=1; ex restarg=1; for(int i=0;i(x)) { ex tmp=x.map(*this); ex result=0; maptermmap; typedef map::iterator exmapit; for(int i=0;ifirst)*rest(i->second); return result; } return x; } Restify restify; void exoutclass::getfunction(const symbol&s,const ex&x) { slist.push_back(substitution(s,x,substitution::getfun)); } ex exoutclass::Makerestexpr::operator()(const ex&x) { if(x.match(rest(wild(0)))) { if(is_a(x.op(0))) return x.op(0); map::iterator mapit=m.find(x.op(0)); if(mapit==m.end()) { symbol s=eoc->newvar(); m[x.op(0)]=s; return s; } else return mapit->second; } else if(x.nops()) return x.map(*this); return x; } void exoutclass::setfunction(const symbol&s,const ex&x) { Makerestexpr makerestexpr(this); for(slistit i=slist.end();i!=slist.begin();) { i--; if((i->k)==substitution::setfun) break; i->subsex=(i->subsex).subs(x==s); i->subsex=restify(i->subsex,s); i->subsex=makerestexpr(i->subsex); //if(is_a(i->subsex)) //{ for(slistit j=slist.begin();j!=i;j++) // j->subsex.subs(i->subssym==i->subsex); // i=slist.erase(i); //} } restargs.clear(); slist.push_back(substitution(s,0,substitution::setfun)); for(map::iterator i=makerestexpr.m.begin(); i!=makerestexpr.m.end();i++) slist.push_back(substitution(i->second,i->first, substitution::temp)); } void exoutclass::writeoutbase(char*name,char*ftype,const print_context&c) { c.s << "#ifndef name_" << "VAR" << endl; c.s << "#define name_" << "VAR" << endl; c.s << "class " << name << endl; c.s << "{private:typedef " << ftype << " floattype;" << endl; c.s << "public:" << name << "(){}" << endl; slistit i=--slist.end(); do { switch(i->k) { case substitution::setfun: c.s << "public:virtual void set" << ex_to(i->subssym).get_name() << "(" << ftype << ")=0;" << endl;; break; case substitution::getfun: c.s << "public: virtual " << ftype << " get" << ex_to(i->subssym).get_name() << "()=0;" << endl;; break; } } while(i==slist.begin() ? false : (i--,true)); c.s << "};" << endl; c.s << "#endif" << endl; } void exoutclass::writeouth(char*name,char*ftype,const print_context&c,char*parent) { c.s << "class " << name; if(*parent) c.s << ":public " << parent; c.s << endl; c.s << "{private:typedef " << ftype << " floattype;" << endl; c.s << "private:int functionsdone;" << endl; c.s << "public:" << name << "();" << endl; slistit i=--slist.end(); do { switch(i->k) { case substitution::setfun: c.s << "public:void set" << ex_to(i->subssym).get_name() << "(" << ftype << ");" << endl;; c.s << "private:floattype " << ex_to(i->subssym).get_name() << " ;" << endl; break; case substitution::getfun: c.s << "private:" << ftype << " " << ex_to(i->subssym).get_name() << ";" << endl;; c.s << "public:" << ftype << " get" << ex_to(i->subssym).get_name() << "();" << endl;; break; case substitution::temp: c.s << "private:floattype " << ex_to(i->subssym).get_name() << ";" << endl; } } while(i==slist.begin() ? false : (i--,true)); c.s << "};" << endl; } void exoutclass::writeoutC(char*name,char*ftype,const print_context&c) { int donecount=0; c.s << "#include " << endl; c.s << "#include \"" << name << ".h\"" << endl; c.s << "using namespace std;" << endl; c.s << name << "::" << name << "()" << endl; c.s << "{" << endl; c.s << "functionsdone=0;" << endl; slistit startsection=slist.end(); while(true) { slistit pos=startsection; while(pos!=slist.begin()&&(--pos)->k!=substitution::setfun) { c.s << ex_to(pos->subssym).get_name() << "="; (pos->subsex).print(c); c.s << ";" << endl; } c.s << "}" << endl; pos=startsection; while(pos!=slist.begin()&&(--pos)->k!=substitution::setfun) if(pos->k==substitution::getfun) { c.s << name << "::floattype "<< name << "::get" << pos->subssym << "()" << endl; c.s << "{" << endl; c.s << "if(functionsdone<" << donecount << ")" << endl; c.s << "cerr << \"Warning: function get" << pos->subssym << " called prematurely.\" << endl;" << endl; c.s << "return " << pos->subssym << ";" << endl; c.s << "}" << endl; } if(pos==slist.begin()) break; c.s << "void " << name << "::set" << pos->subssym << "(" << ftype << " " << pos->subssym << ")" << endl; c.s << "{" << endl; c.s << "if(functionsdone<" << donecount << ")" << endl; c.s << "cerr << \"Warning: function set" << pos->subssym << " called prematurely.\" << endl;" << endl; donecount++; c.s << "functionsdone=" << donecount << ";" << endl; startsection=pos; } } void exoutclass::print() { for(slistit i=slist.begin();i!=slist.end();i++) cout << i->subssym << " = " << i->subsex << endl; }