6 my ($seq_template,$n)=@_;
10 for ($N=1; $N<=$n; $N++) {
11 $res .= eval('"' . $seq_template . '"');
20 my ($template,$seq_template1,$seq_template2)=@_;
24 for ($N=1; $N<=$maxargs; $N++) {
25 $SEQ1=generate_seq($seq_template1,$N);
26 $SEQ2=generate_seq($seq_template2,$N);
27 $res .= eval('"' . $template . '"');
28 $SEQ1=''; # to avoid main::SEQ1 used only once warning
29 $SEQ2=''; # same as above
34 $declare_function_macro=generate(
35 <<'END_OF_DECLARE_FUNCTION_MACRO','ex const & p${N}','p${N}');
36 #define DECLARE_FUNCTION_${N}P(NAME) \\
37 extern unsigned function_index_##NAME; \\
38 inline function NAME(${SEQ1}) { \\
39 return function(function_index_##NAME, ${SEQ2}); \\
42 END_OF_DECLARE_FUNCTION_MACRO
44 $typedef_eval_funcp=generate(
45 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
48 $typedef_evalf_funcp=generate(
49 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
52 $typedef_diff_funcp=generate(
53 'typedef ex (* diff_funcp_${N})(${SEQ1}, unsigned);'."\n",
56 $typedef_series_funcp=generate(
57 'typedef ex (* series_funcp_${N})(${SEQ1}, symbol const &, ex const &, int);'."\n",
60 $constructors_interface=generate(
61 ' function(unsigned ser, ${SEQ1});'."\n",
62 'ex const & param${N}','');
64 $register_new_interface=generate(
65 ' static unsigned register_new(char const * nm, eval_funcp_${N} e,'."\n".
66 ' evalf_funcp_${N} ef=0, diff_funcp_${N} d=0, series_funcp_${N} s=0);'.
69 $constructors_implementation=generate(
70 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','ex const & param${N}','param${N}');
71 function::function(unsigned ser, ${SEQ1})
72 : exprseq(${SEQ2}), serial(ser)
74 debugmsg(\"function constructor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
75 tinfo_key = TINFO_FUNCTION;
77 END_OF_CONSTRUCTORS_IMPLEMENTATION
79 $eval_switch_statement=generate(
80 <<'END_OF_EVAL_SWITCH_STATEMENT','eseq[${N}-1]','');
82 return ((eval_funcp_${N})(registered_functions()[serial].e))(${SEQ1});
84 END_OF_EVAL_SWITCH_STATEMENT
86 $evalf_switch_statement=generate(
87 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
89 return ((evalf_funcp_${N})(registered_functions()[serial].ef))(${SEQ1});
91 END_OF_EVALF_SWITCH_STATEMENT
93 $diff_switch_statement=generate(
94 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
96 return ((diff_funcp_${N})(registered_functions()[serial].d))(${SEQ1},diff_param);
98 END_OF_DIFF_SWITCH_STATEMENT
100 $series_switch_statement=generate(
101 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
103 return ((series_funcp_${N})(registered_functions()[serial].s))(${SEQ1},s,point,order);
105 END_OF_SERIES_SWITCH_STATEMENT
107 $register_new_implementation=generate(
108 <<'END_OF_REGISTER_NEW_IMPLEMENTATION','','');
109 unsigned function::register_new(char const * nm, eval_funcp_${N} e,
110 evalf_funcp_${N} ef, diff_funcp_${N} d, series_funcp_${N} s)
112 registered_function_info rfi={nm,${N},0,eval_funcp(e),
113 evalf_funcp(ef),diff_funcp(d),series_funcp(s)};
114 registered_functions().push_back(rfi);
115 return registered_functions().size()-1;
117 END_OF_REGISTER_NEW_IMPLEMENTATION
119 $interface=<<END_OF_INTERFACE;
120 /** \@file function.h
122 * Interface to abstract class function (new function concept).
124 * This file was generated automatically by function.pl.
125 * Please do not modify it directly, edit the perl script instead!
126 * function.pl options: \$maxargs=${maxargs} */
139 // the following lines have been generated for max. ${maxargs} parameters
140 $declare_function_macro
141 // end of generated lines
143 #define REGISTER_FUNCTION(NAME,E,EF,D,S) \\
144 unsigned function_index_##NAME=function::register_new(#NAME,E,EF,D,S);
146 #define BEGIN_TYPECHECK \\
147 bool automatic_typecheck=true;
149 #define TYPECHECK(VAR,TYPE) \\
150 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
151 automatic_typecheck=false; \\
154 #define TYPECHECK_INTEGER(VAR) \\
155 if (!(VAR).info(info_flags::integer)) { \\
156 automatic_typecheck=false; \\
159 #define END_TYPECHECK(RV) \\
161 if (!automatic_typecheck) { \\
165 typedef ex (* eval_funcp)();
166 typedef ex (* evalf_funcp)();
167 typedef ex (* diff_funcp)();
168 typedef ex (* series_funcp)();
170 // the following lines have been generated for max. ${maxargs} parameters
174 $typedef_series_funcp
175 // end of generated lines
177 struct registered_function_info {
187 /** The class function is used to implement builtin functions like sin, cos...
188 and user defined functions */
189 class function : public exprseq
191 friend void ginsh_get_ginac_functions(void);
195 // default constructor, destructor, copy constructor assignment operator and helpers
199 function(function const & other);
200 function const & operator=(function const & other);
202 void copy(function const & other);
203 void destroy(bool call_parent);
205 // other constructors
207 function(unsigned ser);
208 // the following lines have been generated for max. ${maxargs} parameters
209 $constructors_interface
210 // end of generated lines
211 function(unsigned ser, exprseq const & es);
212 function(unsigned ser, exvector const & v, bool discardable=0);
213 function(unsigned ser, exvector * vp); // vp will be deleted
215 // functions overriding virtual functions from bases classes
217 basic * duplicate() const;
218 void printraw(ostream & os) const;
219 void print(ostream & os, unsigned upper_precedence=0) const;
220 void printtree(ostream & os, unsigned indent) const;
221 void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
222 ex expand(unsigned options=0) const;
223 ex eval(int level=0) const;
224 ex evalf(int level=0) const;
225 ex diff(symbol const & s) const;
226 ex series(symbol const & s, ex const & point, int order) const;
227 ex thisexprseq(exvector const & v) const;
228 ex thisexprseq(exvector * vp) const;
230 int compare_same_type(basic const & other) const;
231 bool is_equal_same_type(basic const & other) const;
232 unsigned return_type(void) const;
233 unsigned return_type_tinfo(void) const;
235 // new virtual functions which can be overridden by derived classes
238 // non-virtual functions in this class
240 ex pdiff(unsigned diff_param) const; // partial differentiation
241 static vector<registered_function_info> & registered_functions(void);
243 // the following lines have been generated for max. ${maxargs} parameters
244 $register_new_interface
245 // end of generated lines
246 unsigned getserial(void) const {return serial;}
256 #define is_ex_the_function(OBJ, FUNCNAME) \\
257 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
261 extern const function some_function;
262 extern type_info const & typeid_function;
264 #endif // ndef _FUNCTION_H_
268 $implementation=<<END_OF_IMPLEMENTATION;
269 /** \@file function.cpp
271 * Implementation of class function.
273 * This file was generated automatically by function.pl.
274 * Please do not modify it directly, edit the perl script instead!
275 * function.pl options: \$maxargs=${maxargs} */
283 // default constructor, destructor, copy constructor assignment operator and helpers
288 function::function() : serial(0)
290 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
291 tinfo_key = TINFO_FUNCTION;
294 function::~function()
296 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
300 function::function(function const & other)
302 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
306 function const & function::operator=(function const & other)
308 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
309 if (this != &other) {
318 void function::copy(function const & other)
320 exprseq::copy(other);
324 void function::destroy(bool call_parent)
326 if (call_parent) exprseq::destroy(call_parent);
330 // other constructors
335 function::function(unsigned ser) : serial(ser)
337 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
338 tinfo_key = TINFO_FUNCTION;
341 // the following lines have been generated for max. ${maxargs} parameters
342 $constructors_implementation
343 // end of generated lines
345 function::function(unsigned ser, exprseq const & es) : exprseq(es), serial(ser)
347 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
348 tinfo_key = TINFO_FUNCTION;
351 function::function(unsigned ser, exvector const & v, bool discardable)
352 : exprseq(v,discardable), serial(ser)
354 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
355 tinfo_key = TINFO_FUNCTION;
358 function::function(unsigned ser, exvector * vp)
359 : exprseq(vp), serial(ser)
361 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
362 tinfo_key = TINFO_FUNCTION;
366 // functions overriding virtual functions from bases classes
371 basic * function::duplicate() const
373 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
374 return new function(*this);
377 void function::printraw(ostream & os) const
379 debugmsg("function printraw",LOGLEVEL_PRINT);
381 ASSERT(serial<registered_functions().size());
383 os << "function(name=" << registered_functions()[serial].name;
384 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
391 void function::print(ostream & os, unsigned upper_precedence) const
393 debugmsg("function print",LOGLEVEL_PRINT);
395 ASSERT(serial<registered_functions().size());
397 os << registered_functions()[serial].name;
398 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
401 void function::printtree(ostream & os, unsigned indent) const
403 debugmsg("function printtree",LOGLEVEL_PRINT);
405 ASSERT(serial<registered_functions().size());
407 os << string(indent,' ') << "function "
408 << registered_functions()[serial].name
409 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
410 << ", flags=" << flags
411 << ", nops=" << nops() << endl;
412 for (int i=0; i<nops(); ++i) {
413 seq[i].printtree(os,indent+delta_indent);
415 os << string(indent+delta_indent,' ') << "=====" << endl;
418 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
420 debugmsg("function print csrc",LOGLEVEL_PRINT);
422 ASSERT(serial<registered_functions().size());
424 // Print function name in lowercase
426 lname=registered_functions()[serial].name;
427 for (unsigned i=0; i<lname.size(); i++)
428 lname[i] = tolower(lname[i]);
431 // Print arguments, separated by commas
432 exvector::const_iterator it = seq.begin();
433 exvector::const_iterator itend = seq.end();
434 while (it != itend) {
435 it->bp->printcsrc(os, type, 0);
443 ex function::expand(unsigned options) const
445 return this->setflag(status_flags::expanded);
448 ex function::eval(int level) const
450 ASSERT(serial<registered_functions().size());
452 exvector eseq=evalchildren(level);
454 if (registered_functions()[serial].e==0) {
455 return function(serial,eseq).hold();
457 switch (registered_functions()[serial].nparams) {
458 // the following lines have been generated for max. ${maxargs} parameters
459 ${eval_switch_statement}
460 // end of generated lines
462 throw(std::logic_error("function::eval(): invalid nparams"));
465 ex function::evalf(int level) const
467 ASSERT(serial<registered_functions().size());
469 exvector eseq=evalfchildren(level);
471 if (registered_functions()[serial].ef==0) {
472 return function(serial,eseq).hold();
474 switch (registered_functions()[serial].nparams) {
475 // the following lines have been generated for max. ${maxargs} parameters
476 ${evalf_switch_statement}
477 // end of generated lines
479 throw(std::logic_error("function::evalf(): invalid nparams"));
482 ex function::thisexprseq(exvector const & v) const
484 return function(serial,v);
487 ex function::thisexprseq(exvector * vp) const
489 return function(serial,vp);
492 /** Implementation of ex::series for functions.
493 * \@see ex::series */
494 ex function::series(symbol const & s, ex const & point, int order) const
496 ASSERT(serial<registered_functions().size());
498 if (registered_functions()[serial].s==0) {
499 return basic::series(s, point, order);
501 switch (registered_functions()[serial].nparams) {
502 // the following lines have been generated for max. ${maxargs} parameters
503 ${series_switch_statement}
504 // end of generated lines
506 throw(std::logic_error("function::series(): invalid nparams"));
511 int function::compare_same_type(basic const & other) const
513 ASSERT(is_of_type(other, function));
514 function const & o=static_cast<function &>(const_cast<basic &>(other));
516 if (serial!=o.serial) {
517 return serial < o.serial ? -1 : 1;
519 return exprseq::compare_same_type(o);
522 bool function::is_equal_same_type(basic const & other) const
524 ASSERT(is_of_type(other, function));
525 function const & o=static_cast<function &>(const_cast<basic &>(other));
527 if (serial!=o.serial) return false;
528 return exprseq::is_equal_same_type(o);
531 unsigned function::return_type(void) const
534 return return_types::commutative;
536 return (*seq.begin()).return_type();
539 unsigned function::return_type_tinfo(void) const
544 return (*seq.begin()).return_type_tinfo();
548 // new virtual functions which can be overridden by derived classes
554 // non-virtual functions in this class
559 ex function::pdiff(unsigned diff_param) const // partial differentiation
561 ASSERT(serial<registered_functions().size());
563 if (registered_functions()[serial].d==0) {
564 throw(std::logic_error(string("function::pdiff(") + registered_functions()[serial].name + "): no diff function defined"));
566 switch (registered_functions()[serial].nparams) {
567 // the following lines have been generated for max. ${maxargs} parameters
568 ${diff_switch_statement}
569 // end of generated lines
571 throw(std::logic_error("function::pdiff(): no diff function defined"));
574 vector<registered_function_info> & function::registered_functions(void)
576 static vector<registered_function_info> * rf=new vector<registered_function_info>;
582 // the following lines have been generated for max. ${maxargs} parameters
583 $register_new_implementation
584 // end of generated lines
587 // static member variables
596 const function some_function;
597 type_info const & typeid_function=typeid(some_function);
599 END_OF_IMPLEMENTATION
601 print "Creating interface file function.h...";
602 open OUT,">function.h" or die "cannot open function.h";
603 print OUT $interface;
607 print "Creating implementation file function.cpp...";
608 open OUT,">function.cpp" or die "cannot open function.cpp";
609 print OUT $implementation;