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','GiNaC::ex const & p${N}','p${N}');
36 #define DECLARE_FUNCTION_${N}P(NAME) \\
37 extern const unsigned function_index_##NAME; \\
38 inline GiNaC::function NAME(${SEQ1}) { \\
39 return GiNaC::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). */
125 * This file was generated automatically by function.pl.
126 * Please do not modify it directly, edit the perl script instead!
127 * function.pl options: \$maxargs=${maxargs}
129 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
131 * This program is free software; you can redistribute it and/or modify
132 * it under the terms of the GNU General Public License as published by
133 * the Free Software Foundation; either version 2 of the License, or
134 * (at your option) any later version.
136 * This program is distributed in the hope that it will be useful,
137 * but WITHOUT ANY WARRANTY; without even the implied warranty of
138 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139 * GNU General Public License for more details.
141 * You should have received a copy of the GNU General Public License
142 * along with this program; if not, write to the Free Software
143 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
146 #ifndef __GINAC_FUNCTION_H__
147 #define __GINAC_FUNCTION_H__
151 #include <ginac/exprseq.h>
153 // the following lines have been generated for max. ${maxargs} parameters
154 $declare_function_macro
155 // end of generated lines
157 #define REGISTER_FUNCTION(NAME,E,EF,D,S) \\
158 const unsigned function_index_##NAME=GiNaC::function::register_new(#NAME,E,EF,D,S);
160 #define BEGIN_TYPECHECK \\
161 bool automatic_typecheck=true;
163 #define TYPECHECK(VAR,TYPE) \\
164 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
165 automatic_typecheck=false; \\
168 #define TYPECHECK_INTEGER(VAR) \\
169 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
170 automatic_typecheck=false; \\
173 #define END_TYPECHECK(RV) \\
175 if (!automatic_typecheck) { \\
183 typedef ex (* eval_funcp)();
184 typedef ex (* evalf_funcp)();
185 typedef ex (* diff_funcp)();
186 typedef ex (* series_funcp)();
188 // the following lines have been generated for max. ${maxargs} parameters
192 $typedef_series_funcp
193 // end of generated lines
195 struct registered_function_info {
205 /** The class function is used to implement builtin functions like sin, cos...
206 and user defined functions */
207 class function : public exprseq
209 friend void ginsh_get_ginac_functions(void);
213 // default constructor, destructor, copy constructor assignment operator and helpers
217 function(function const & other);
218 function const & operator=(function const & other);
220 void copy(function const & other);
221 void destroy(bool call_parent);
223 // other constructors
225 function(unsigned ser);
226 // the following lines have been generated for max. ${maxargs} parameters
227 $constructors_interface
228 // end of generated lines
229 function(unsigned ser, exprseq const & es);
230 function(unsigned ser, exvector const & v, bool discardable=0);
231 function(unsigned ser, exvector * vp); // vp will be deleted
233 // functions overriding virtual functions from bases classes
235 basic * duplicate() const;
236 void printraw(ostream & os) const;
237 void print(ostream & os, unsigned upper_precedence=0) const;
238 void printtree(ostream & os, unsigned indent) const;
239 void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
240 ex expand(unsigned options=0) const;
241 ex eval(int level=0) const;
242 ex evalf(int level=0) const;
243 ex diff(symbol const & s) const;
244 ex series(symbol const & s, ex const & point, int order) const;
245 ex thisexprseq(exvector const & v) const;
246 ex thisexprseq(exvector * vp) const;
248 int compare_same_type(basic const & other) const;
249 bool is_equal_same_type(basic const & other) const;
250 unsigned return_type(void) const;
251 unsigned return_type_tinfo(void) const;
253 // new virtual functions which can be overridden by derived classes
256 // non-virtual functions in this class
258 ex pdiff(unsigned diff_param) const; // partial differentiation
259 static vector<registered_function_info> & registered_functions(void);
261 // the following lines have been generated for max. ${maxargs} parameters
262 $register_new_interface
263 // end of generated lines
264 unsigned getserial(void) const {return serial;}
274 #define is_ex_the_function(OBJ, FUNCNAME) \\
275 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
279 extern const function some_function;
280 extern type_info const & typeid_function;
284 #endif // ndef __GINAC_FUNCTION_H__
288 $implementation=<<END_OF_IMPLEMENTATION;
289 /** \@file function.cpp
291 * Implementation of class function. */
294 * This file was generated automatically by function.pl.
295 * Please do not modify it directly, edit the perl script instead!
296 * function.pl options: \$maxargs=${maxargs}
298 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
300 * This program is free software; you can redistribute it and/or modify
301 * it under the terms of the GNU General Public License as published by
302 * the Free Software Foundation; either version 2 of the License, or
303 * (at your option) any later version.
305 * This program is distributed in the hope that it will be useful,
306 * but WITHOUT ANY WARRANTY; without even the implied warranty of
307 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
308 * GNU General Public License for more details.
310 * You should have received a copy of the GNU General Public License
311 * along with this program; if not, write to the Free Software
312 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
318 #include "function.h"
320 #include "debugmsg.h"
325 // default constructor, destructor, copy constructor assignment operator and helpers
330 function::function() : serial(0)
332 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
333 tinfo_key = TINFO_function;
336 function::~function()
338 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
342 function::function(function const & other)
344 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
348 function const & function::operator=(function const & other)
350 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
351 if (this != &other) {
360 void function::copy(function const & other)
362 exprseq::copy(other);
366 void function::destroy(bool call_parent)
368 if (call_parent) exprseq::destroy(call_parent);
372 // other constructors
377 function::function(unsigned ser) : serial(ser)
379 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
380 tinfo_key = TINFO_function;
383 // the following lines have been generated for max. ${maxargs} parameters
384 $constructors_implementation
385 // end of generated lines
387 function::function(unsigned ser, exprseq const & es) : exprseq(es), serial(ser)
389 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
390 tinfo_key = TINFO_function;
393 function::function(unsigned ser, exvector const & v, bool discardable)
394 : exprseq(v,discardable), serial(ser)
396 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
397 tinfo_key = TINFO_function;
400 function::function(unsigned ser, exvector * vp)
401 : exprseq(vp), serial(ser)
403 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
404 tinfo_key = TINFO_function;
408 // functions overriding virtual functions from bases classes
413 basic * function::duplicate() const
415 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
416 return new function(*this);
419 void function::printraw(ostream & os) const
421 debugmsg("function printraw",LOGLEVEL_PRINT);
423 GINAC_ASSERT(serial<registered_functions().size());
425 os << "function(name=" << registered_functions()[serial].name;
426 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
433 void function::print(ostream & os, unsigned upper_precedence) const
435 debugmsg("function print",LOGLEVEL_PRINT);
437 GINAC_ASSERT(serial<registered_functions().size());
439 os << registered_functions()[serial].name;
440 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
443 void function::printtree(ostream & os, unsigned indent) const
445 debugmsg("function printtree",LOGLEVEL_PRINT);
447 GINAC_ASSERT(serial<registered_functions().size());
449 os << string(indent,' ') << "function "
450 << registered_functions()[serial].name
451 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
452 << ", flags=" << flags
453 << ", nops=" << nops() << endl;
454 for (int i=0; i<nops(); ++i) {
455 seq[i].printtree(os,indent+delta_indent);
457 os << string(indent+delta_indent,' ') << "=====" << endl;
460 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
462 debugmsg("function print csrc",LOGLEVEL_PRINT);
464 GINAC_ASSERT(serial<registered_functions().size());
466 // Print function name in lowercase
468 lname=registered_functions()[serial].name;
469 for (unsigned i=0; i<lname.size(); i++)
470 lname[i] = tolower(lname[i]);
473 // Print arguments, separated by commas
474 exvector::const_iterator it = seq.begin();
475 exvector::const_iterator itend = seq.end();
476 while (it != itend) {
477 it->bp->printcsrc(os, type, 0);
485 ex function::expand(unsigned options) const
487 return this->setflag(status_flags::expanded);
490 ex function::eval(int level) const
492 GINAC_ASSERT(serial<registered_functions().size());
494 exvector eseq=evalchildren(level);
496 if (registered_functions()[serial].e==0) {
497 return function(serial,eseq).hold();
499 switch (registered_functions()[serial].nparams) {
500 // the following lines have been generated for max. ${maxargs} parameters
501 ${eval_switch_statement}
502 // end of generated lines
504 throw(std::logic_error("function::eval(): invalid nparams"));
507 ex function::evalf(int level) const
509 GINAC_ASSERT(serial<registered_functions().size());
511 exvector eseq=evalfchildren(level);
513 if (registered_functions()[serial].ef==0) {
514 return function(serial,eseq).hold();
516 switch (registered_functions()[serial].nparams) {
517 // the following lines have been generated for max. ${maxargs} parameters
518 ${evalf_switch_statement}
519 // end of generated lines
521 throw(std::logic_error("function::evalf(): invalid nparams"));
524 ex function::thisexprseq(exvector const & v) const
526 return function(serial,v);
529 ex function::thisexprseq(exvector * vp) const
531 return function(serial,vp);
534 /** Implementation of ex::series for functions.
535 * \@see ex::series */
536 ex function::series(symbol const & s, ex const & point, int order) const
538 GINAC_ASSERT(serial<registered_functions().size());
540 if (registered_functions()[serial].s==0) {
541 return basic::series(s, point, order);
543 switch (registered_functions()[serial].nparams) {
544 // the following lines have been generated for max. ${maxargs} parameters
545 ${series_switch_statement}
546 // end of generated lines
548 throw(std::logic_error("function::series(): invalid nparams"));
553 int function::compare_same_type(basic const & other) const
555 GINAC_ASSERT(is_of_type(other, function));
556 function const & o=static_cast<function &>(const_cast<basic &>(other));
558 if (serial!=o.serial) {
559 return serial < o.serial ? -1 : 1;
561 return exprseq::compare_same_type(o);
564 bool function::is_equal_same_type(basic const & other) const
566 GINAC_ASSERT(is_of_type(other, function));
567 function const & o=static_cast<function &>(const_cast<basic &>(other));
569 if (serial!=o.serial) return false;
570 return exprseq::is_equal_same_type(o);
573 unsigned function::return_type(void) const
576 return return_types::commutative;
578 return (*seq.begin()).return_type();
581 unsigned function::return_type_tinfo(void) const
586 return (*seq.begin()).return_type_tinfo();
590 // new virtual functions which can be overridden by derived classes
596 // non-virtual functions in this class
601 ex function::pdiff(unsigned diff_param) const // partial differentiation
603 GINAC_ASSERT(serial<registered_functions().size());
605 if (registered_functions()[serial].d==0) {
606 throw(std::logic_error(string("function::pdiff(") + registered_functions()[serial].name + "): no diff function defined"));
608 switch (registered_functions()[serial].nparams) {
609 // the following lines have been generated for max. ${maxargs} parameters
610 ${diff_switch_statement}
611 // end of generated lines
613 throw(std::logic_error("function::pdiff(): no diff function defined"));
616 vector<registered_function_info> & function::registered_functions(void)
618 static vector<registered_function_info> * rf=new vector<registered_function_info>;
624 // the following lines have been generated for max. ${maxargs} parameters
625 $register_new_implementation
626 // end of generated lines
629 // static member variables
638 const function some_function;
639 type_info const & typeid_function=typeid(some_function);
643 END_OF_IMPLEMENTATION
645 print "Creating interface file function.h...";
646 open OUT,">function.h" or die "cannot open function.h";
647 print OUT $interface;
651 print "Creating implementation file function.cpp...";
652 open OUT,">function.cpp" or die "cannot open function.cpp";
653 print OUT $implementation;