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_namespace=generate(
35 <<'END_OF_DECLARE_FUNCTION_MACRO_NAMESPACE','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_NAMESPACE
44 $declare_function_macro_no_namespace=generate(
45 <<'END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE','ex const & p${N}','p${N}');
46 #define DECLARE_FUNCTION_${N}P(NAME) \\
47 extern unsigned function_index_##NAME; \\
48 inline function NAME(${SEQ1}) { \\
49 return function(function_index_##NAME, ${SEQ2}); \\
52 END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE
54 $typedef_eval_funcp=generate(
55 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
58 $typedef_evalf_funcp=generate(
59 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
62 $typedef_diff_funcp=generate(
63 'typedef ex (* diff_funcp_${N})(${SEQ1}, unsigned);'."\n",
66 $typedef_series_funcp=generate(
67 'typedef ex (* series_funcp_${N})(${SEQ1}, symbol const &, ex const &, int);'."\n",
70 $constructors_interface=generate(
71 ' function(unsigned ser, ${SEQ1});'."\n",
72 'ex const & param${N}','');
74 $register_new_interface=generate(
75 ' static unsigned register_new(char const * nm, eval_funcp_${N} e,'."\n".
76 ' evalf_funcp_${N} ef=0, diff_funcp_${N} d=0, series_funcp_${N} s=0);'.
79 $constructors_implementation=generate(
80 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','ex const & param${N}','param${N}');
81 function::function(unsigned ser, ${SEQ1})
82 : exprseq(${SEQ2}), serial(ser)
84 debugmsg(\"function constructor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
85 tinfo_key = TINFO_function;
87 END_OF_CONSTRUCTORS_IMPLEMENTATION
89 $eval_switch_statement=generate(
90 <<'END_OF_EVAL_SWITCH_STATEMENT','eseq[${N}-1]','');
92 return ((eval_funcp_${N})(registered_functions()[serial].e))(${SEQ1});
94 END_OF_EVAL_SWITCH_STATEMENT
96 $evalf_switch_statement=generate(
97 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
99 return ((evalf_funcp_${N})(registered_functions()[serial].ef))(${SEQ1});
101 END_OF_EVALF_SWITCH_STATEMENT
103 $diff_switch_statement=generate(
104 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
106 return ((diff_funcp_${N})(registered_functions()[serial].d))(${SEQ1},diff_param);
108 END_OF_DIFF_SWITCH_STATEMENT
110 $series_switch_statement=generate(
111 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
113 return ((series_funcp_${N})(registered_functions()[serial].s))(${SEQ1},s,point,order);
115 END_OF_SERIES_SWITCH_STATEMENT
117 $register_new_implementation=generate(
118 <<'END_OF_REGISTER_NEW_IMPLEMENTATION','','');
119 unsigned function::register_new(char const * nm, eval_funcp_${N} e,
120 evalf_funcp_${N} ef, diff_funcp_${N} d, series_funcp_${N} s)
122 registered_function_info rfi={nm,${N},0,eval_funcp(e),
123 evalf_funcp(ef),diff_funcp(d),series_funcp(s)};
124 registered_functions().push_back(rfi);
125 return registered_functions().size()-1;
127 END_OF_REGISTER_NEW_IMPLEMENTATION
129 $interface=<<END_OF_INTERFACE;
130 /** \@file function.h
132 * Interface to abstract class function (new function concept). */
135 * This file was generated automatically by function.pl.
136 * Please do not modify it directly, edit the perl script instead!
137 * function.pl options: \$maxargs=${maxargs}
139 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
141 * This program is free software; you can redistribute it and/or modify
142 * it under the terms of the GNU General Public License as published by
143 * the Free Software Foundation; either version 2 of the License, or
144 * (at your option) any later version.
146 * This program is distributed in the hope that it will be useful,
147 * but WITHOUT ANY WARRANTY; without even the implied warranty of
148 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
149 * GNU General Public License for more details.
151 * You should have received a copy of the GNU General Public License
152 * along with this program; if not, write to the Free Software
153 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
156 #ifndef __GINAC_FUNCTION_H__
157 #define __GINAC_FUNCTION_H__
163 // CINT needs <algorithm> to work properly with <vector>
165 #endif // def __CINT__
167 #include <ginac/exprseq.h>
169 #ifndef NO_GINAC_NAMESPACE
171 // the following lines have been generated for max. ${maxargs} parameters
172 $declare_function_macro_namespace
173 // end of generated lines
175 #else // ndef NO_GINAC_NAMESPACE
177 // the following lines have been generated for max. ${maxargs} parameters
178 $declare_function_macro_no_namespace
179 // end of generated lines
181 #endif // ndef NO_GINAC_NAMESPACE
183 #ifndef NO_GINAC_NAMESPACE
185 #define REGISTER_FUNCTION(NAME,E,EF,D,S) \\
187 const unsigned function_index_##NAME=GiNaC::function::register_new(#NAME,E,EF,D,S);
189 #else // ndef NO_GINAC_NAMESPACE
191 const unsigned function_index_##NAME=function::register_new(#NAME,E,EF,D,S);
193 #endif // ndef NO_GINAC_NAMESPACE
195 #define BEGIN_TYPECHECK \\
196 bool automatic_typecheck=true;
198 #define TYPECHECK(VAR,TYPE) \\
199 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
200 automatic_typecheck=false; \\
203 #ifndef NO_GINAC_NAMESPACE
205 #define TYPECHECK_INTEGER(VAR) \\
206 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
207 automatic_typecheck=false; \\
210 #else // ndef NO_GINAC_NAMESPACE
212 #define TYPECHECK_INTEGER(VAR) \\
213 if (!(VAR).info(info_flags::integer)) { \\
214 automatic_typecheck=false; \\
217 #endif // ndef NO_GINAC_NAMESPACE
219 #define END_TYPECHECK(RV) \\
221 if (!automatic_typecheck) { \\
225 #ifndef NO_GINAC_NAMESPACE
227 #endif // ndef NO_GINAC_NAMESPACE
231 typedef ex (* eval_funcp)();
232 typedef ex (* evalf_funcp)();
233 typedef ex (* diff_funcp)();
234 typedef ex (* series_funcp)();
236 // the following lines have been generated for max. ${maxargs} parameters
240 $typedef_series_funcp
241 // end of generated lines
243 struct registered_function_info {
253 /** The class function is used to implement builtin functions like sin, cos...
254 and user defined functions */
255 class function : public exprseq
257 // CINT has a linking problem
258 friend void ginsh_get_ginac_functions(void);
262 // default constructor, destructor, copy constructor assignment operator and helpers
266 function(function const & other);
267 function const & operator=(function const & other);
269 void copy(function const & other);
270 void destroy(bool call_parent);
272 // other constructors
274 function(unsigned ser);
275 // the following lines have been generated for max. ${maxargs} parameters
276 $constructors_interface
277 // end of generated lines
278 function(unsigned ser, exprseq const & es);
279 function(unsigned ser, exvector const & v, bool discardable=0);
280 function(unsigned ser, exvector * vp); // vp will be deleted
282 // functions overriding virtual functions from bases classes
284 basic * duplicate() const;
285 void printraw(ostream & os) const;
286 void print(ostream & os, unsigned upper_precedence=0) const;
287 void printtree(ostream & os, unsigned indent) const;
288 void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
289 ex expand(unsigned options=0) const;
290 ex eval(int level=0) const;
291 ex evalf(int level=0) const;
292 ex diff(symbol const & s) const;
293 ex series(symbol const & s, ex const & point, int order) const;
294 ex thisexprseq(exvector const & v) const;
295 ex thisexprseq(exvector * vp) const;
297 int compare_same_type(basic const & other) const;
298 bool is_equal_same_type(basic const & other) const;
299 unsigned return_type(void) const;
300 unsigned return_type_tinfo(void) const;
302 // new virtual functions which can be overridden by derived classes
305 // non-virtual functions in this class
307 ex pdiff(unsigned diff_param) const; // partial differentiation
308 static vector<registered_function_info> & registered_functions(void);
310 // the following lines have been generated for max. ${maxargs} parameters
311 $register_new_interface
312 // end of generated lines
313 unsigned getserial(void) const {return serial;}
323 #ifndef NO_GINAC_NAMESPACE
325 #define is_ex_the_function(OBJ, FUNCNAME) \\
326 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
328 #else // ndef NO_GINAC_NAMESPACE
330 #define is_ex_the_function(OBJ, FUNCNAME) \\
331 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
333 #endif // ndef NO_GINAC_NAMESPACE
337 extern const function some_function;
338 extern type_info const & typeid_function;
340 #ifndef NO_GINAC_NAMESPACE
342 #endif // ndef NO_GINAC_NAMESPACE
344 #endif // ndef __GINAC_FUNCTION_H__
348 $implementation=<<END_OF_IMPLEMENTATION;
349 /** \@file function.cpp
351 * Implementation of class function. */
354 * This file was generated automatically by function.pl.
355 * Please do not modify it directly, edit the perl script instead!
356 * function.pl options: \$maxargs=${maxargs}
358 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
360 * This program is free software; you can redistribute it and/or modify
361 * it under the terms of the GNU General Public License as published by
362 * the Free Software Foundation; either version 2 of the License, or
363 * (at your option) any later version.
365 * This program is distributed in the hope that it will be useful,
366 * but WITHOUT ANY WARRANTY; without even the implied warranty of
367 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
368 * GNU General Public License for more details.
370 * You should have received a copy of the GNU General Public License
371 * along with this program; if not, write to the Free Software
372 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
378 #include "function.h"
380 #include "debugmsg.h"
382 #ifndef NO_GINAC_NAMESPACE
384 #endif // ndef NO_GINAC_NAMESPACE
387 // default constructor, destructor, copy constructor assignment operator and helpers
392 function::function() : serial(0)
394 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
395 tinfo_key = TINFO_function;
398 function::~function()
400 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
404 function::function(function const & other)
406 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
410 function const & function::operator=(function const & other)
412 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
413 if (this != &other) {
422 void function::copy(function const & other)
424 exprseq::copy(other);
428 void function::destroy(bool call_parent)
430 if (call_parent) exprseq::destroy(call_parent);
434 // other constructors
439 function::function(unsigned ser) : serial(ser)
441 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
442 tinfo_key = TINFO_function;
445 // the following lines have been generated for max. ${maxargs} parameters
446 $constructors_implementation
447 // end of generated lines
449 function::function(unsigned ser, exprseq const & es) : exprseq(es), serial(ser)
451 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
452 tinfo_key = TINFO_function;
455 function::function(unsigned ser, exvector const & v, bool discardable)
456 : exprseq(v,discardable), serial(ser)
458 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
459 tinfo_key = TINFO_function;
462 function::function(unsigned ser, exvector * vp)
463 : exprseq(vp), serial(ser)
465 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
466 tinfo_key = TINFO_function;
470 // functions overriding virtual functions from bases classes
475 basic * function::duplicate() const
477 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
478 return new function(*this);
481 void function::printraw(ostream & os) const
483 debugmsg("function printraw",LOGLEVEL_PRINT);
485 GINAC_ASSERT(serial<registered_functions().size());
487 os << "function(name=" << registered_functions()[serial].name;
488 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
495 void function::print(ostream & os, unsigned upper_precedence) const
497 debugmsg("function print",LOGLEVEL_PRINT);
499 GINAC_ASSERT(serial<registered_functions().size());
501 os << registered_functions()[serial].name;
502 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
505 void function::printtree(ostream & os, unsigned indent) const
507 debugmsg("function printtree",LOGLEVEL_PRINT);
509 GINAC_ASSERT(serial<registered_functions().size());
511 os << string(indent,' ') << "function "
512 << registered_functions()[serial].name
513 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
514 << ", flags=" << flags
515 << ", nops=" << nops() << endl;
516 for (int i=0; i<nops(); ++i) {
517 seq[i].printtree(os,indent+delta_indent);
519 os << string(indent+delta_indent,' ') << "=====" << endl;
522 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
524 debugmsg("function print csrc",LOGLEVEL_PRINT);
526 GINAC_ASSERT(serial<registered_functions().size());
528 // Print function name in lowercase
530 lname=registered_functions()[serial].name;
531 for (unsigned i=0; i<lname.size(); i++)
532 lname[i] = tolower(lname[i]);
535 // Print arguments, separated by commas
536 exvector::const_iterator it = seq.begin();
537 exvector::const_iterator itend = seq.end();
538 while (it != itend) {
539 it->bp->printcsrc(os, type, 0);
547 ex function::expand(unsigned options) const
549 return this->setflag(status_flags::expanded);
552 ex function::eval(int level) const
554 GINAC_ASSERT(serial<registered_functions().size());
556 exvector eseq=evalchildren(level);
558 if (registered_functions()[serial].e==0) {
559 return function(serial,eseq).hold();
561 switch (registered_functions()[serial].nparams) {
562 // the following lines have been generated for max. ${maxargs} parameters
563 ${eval_switch_statement}
564 // end of generated lines
566 throw(std::logic_error("function::eval(): invalid nparams"));
569 ex function::evalf(int level) const
571 GINAC_ASSERT(serial<registered_functions().size());
573 exvector eseq=evalfchildren(level);
575 if (registered_functions()[serial].ef==0) {
576 return function(serial,eseq).hold();
578 switch (registered_functions()[serial].nparams) {
579 // the following lines have been generated for max. ${maxargs} parameters
580 ${evalf_switch_statement}
581 // end of generated lines
583 throw(std::logic_error("function::evalf(): invalid nparams"));
586 ex function::thisexprseq(exvector const & v) const
588 return function(serial,v);
591 ex function::thisexprseq(exvector * vp) const
593 return function(serial,vp);
596 /** Implementation of ex::series for functions.
597 * \@see ex::series */
598 ex function::series(symbol const & s, ex const & point, int order) const
600 GINAC_ASSERT(serial<registered_functions().size());
602 if (registered_functions()[serial].s==0) {
603 return basic::series(s, point, order);
605 switch (registered_functions()[serial].nparams) {
606 // the following lines have been generated for max. ${maxargs} parameters
607 ${series_switch_statement}
608 // end of generated lines
610 throw(std::logic_error("function::series(): invalid nparams"));
615 int function::compare_same_type(basic const & other) const
617 GINAC_ASSERT(is_of_type(other, function));
618 function const & o=static_cast<function &>(const_cast<basic &>(other));
620 if (serial!=o.serial) {
621 return serial < o.serial ? -1 : 1;
623 return exprseq::compare_same_type(o);
626 bool function::is_equal_same_type(basic const & other) const
628 GINAC_ASSERT(is_of_type(other, function));
629 function const & o=static_cast<function &>(const_cast<basic &>(other));
631 if (serial!=o.serial) return false;
632 return exprseq::is_equal_same_type(o);
635 unsigned function::return_type(void) const
638 return return_types::commutative;
640 return (*seq.begin()).return_type();
643 unsigned function::return_type_tinfo(void) const
648 return (*seq.begin()).return_type_tinfo();
652 // new virtual functions which can be overridden by derived classes
658 // non-virtual functions in this class
663 ex function::pdiff(unsigned diff_param) const // partial differentiation
665 GINAC_ASSERT(serial<registered_functions().size());
667 if (registered_functions()[serial].d==0) {
668 throw(std::logic_error(string("function::pdiff(") + registered_functions()[serial].name + "): no diff function defined"));
670 switch (registered_functions()[serial].nparams) {
671 // the following lines have been generated for max. ${maxargs} parameters
672 ${diff_switch_statement}
673 // end of generated lines
675 throw(std::logic_error("function::pdiff(): no diff function defined"));
678 vector<registered_function_info> & function::registered_functions(void)
680 static vector<registered_function_info> * rf=new vector<registered_function_info>;
686 // the following lines have been generated for max. ${maxargs} parameters
687 $register_new_implementation
688 // end of generated lines
691 // static member variables
700 const function some_function;
701 type_info const & typeid_function=typeid(some_function);
703 #ifndef NO_GINAC_NAMESPACE
705 #endif // ndef NO_GINAC_NAMESPACE
707 END_OF_IMPLEMENTATION
709 print "Creating interface file function.h...";
710 open OUT,">function.h" or die "cannot open function.h";
711 print OUT $interface;
715 print "Creating implementation file function.cpp...";
716 open OUT,">function.cpp" or die "cannot open function.cpp";
717 print OUT $implementation;