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}
128 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
130 * This program is free software; you can redistribute it and/or modify
131 * it under the terms of the GNU General Public License as published by
132 * the Free Software Foundation; either version 2 of the License, or
133 * (at your option) any later version.
135 * This program is distributed in the hope that it will be useful,
136 * but WITHOUT ANY WARRANTY; without even the implied warranty of
137 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138 * GNU General Public License for more details.
140 * You should have received a copy of the GNU General Public License
141 * along with this program; if not, write to the Free Software
142 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
145 #ifndef __GINAC_FUNCTION_H__
146 #define __GINAC_FUNCTION_H__
150 #include <ginac/exprseq.h>
154 // the following lines have been generated for max. ${maxargs} parameters
155 $declare_function_macro
156 // end of generated lines
158 #define REGISTER_FUNCTION(NAME,E,EF,D,S) \\
159 unsigned function_index_##NAME=function::register_new(#NAME,E,EF,D,S);
161 #define BEGIN_TYPECHECK \\
162 bool automatic_typecheck=true;
164 #define TYPECHECK(VAR,TYPE) \\
165 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
166 automatic_typecheck=false; \\
169 #define TYPECHECK_INTEGER(VAR) \\
170 if (!(VAR).info(info_flags::integer)) { \\
171 automatic_typecheck=false; \\
174 #define END_TYPECHECK(RV) \\
176 if (!automatic_typecheck) { \\
180 typedef ex (* eval_funcp)();
181 typedef ex (* evalf_funcp)();
182 typedef ex (* diff_funcp)();
183 typedef ex (* series_funcp)();
185 // the following lines have been generated for max. ${maxargs} parameters
189 $typedef_series_funcp
190 // end of generated lines
192 struct registered_function_info {
202 /** The class function is used to implement builtin functions like sin, cos...
203 and user defined functions */
204 class function : public exprseq
206 friend void ginsh_get_ginac_functions(void);
210 // default constructor, destructor, copy constructor assignment operator and helpers
214 function(function const & other);
215 function const & operator=(function const & other);
217 void copy(function const & other);
218 void destroy(bool call_parent);
220 // other constructors
222 function(unsigned ser);
223 // the following lines have been generated for max. ${maxargs} parameters
224 $constructors_interface
225 // end of generated lines
226 function(unsigned ser, exprseq const & es);
227 function(unsigned ser, exvector const & v, bool discardable=0);
228 function(unsigned ser, exvector * vp); // vp will be deleted
230 // functions overriding virtual functions from bases classes
232 basic * duplicate() const;
233 void printraw(ostream & os) const;
234 void print(ostream & os, unsigned upper_precedence=0) const;
235 void printtree(ostream & os, unsigned indent) const;
236 void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
237 ex expand(unsigned options=0) const;
238 ex eval(int level=0) const;
239 ex evalf(int level=0) const;
240 ex diff(symbol const & s) const;
241 ex series(symbol const & s, ex const & point, int order) const;
242 ex thisexprseq(exvector const & v) const;
243 ex thisexprseq(exvector * vp) const;
245 int compare_same_type(basic const & other) const;
246 bool is_equal_same_type(basic const & other) const;
247 unsigned return_type(void) const;
248 unsigned return_type_tinfo(void) const;
250 // new virtual functions which can be overridden by derived classes
253 // non-virtual functions in this class
255 ex pdiff(unsigned diff_param) const; // partial differentiation
256 static vector<registered_function_info> & registered_functions(void);
258 // the following lines have been generated for max. ${maxargs} parameters
259 $register_new_interface
260 // end of generated lines
261 unsigned getserial(void) const {return serial;}
271 #define is_ex_the_function(OBJ, FUNCNAME) \\
272 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
276 extern const function some_function;
277 extern type_info const & typeid_function;
279 #endif // ndef __GINAC_FUNCTION_H__
283 $implementation=<<END_OF_IMPLEMENTATION;
284 /** \@file function.cpp
286 * Implementation of class function.
288 * This file was generated automatically by function.pl.
289 * Please do not modify it directly, edit the perl script instead!
290 * function.pl options: \$maxargs=${maxargs}
292 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
294 * This program is free software; you can redistribute it and/or modify
295 * it under the terms of the GNU General Public License as published by
296 * the Free Software Foundation; either version 2 of the License, or
297 * (at your option) any later version.
299 * This program is distributed in the hope that it will be useful,
300 * but WITHOUT ANY WARRANTY; without even the implied warranty of
301 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
302 * GNU General Public License for more details.
304 * You should have received a copy of the GNU General Public License
305 * along with this program; if not, write to the Free Software
306 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
312 #include "function.h"
316 // default constructor, destructor, copy constructor assignment operator and helpers
321 function::function() : serial(0)
323 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
324 tinfo_key = TINFO_function;
327 function::~function()
329 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
333 function::function(function const & other)
335 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
339 function const & function::operator=(function const & other)
341 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
342 if (this != &other) {
351 void function::copy(function const & other)
353 exprseq::copy(other);
357 void function::destroy(bool call_parent)
359 if (call_parent) exprseq::destroy(call_parent);
363 // other constructors
368 function::function(unsigned ser) : serial(ser)
370 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
371 tinfo_key = TINFO_function;
374 // the following lines have been generated for max. ${maxargs} parameters
375 $constructors_implementation
376 // end of generated lines
378 function::function(unsigned ser, exprseq const & es) : exprseq(es), serial(ser)
380 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
381 tinfo_key = TINFO_function;
384 function::function(unsigned ser, exvector const & v, bool discardable)
385 : exprseq(v,discardable), serial(ser)
387 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
388 tinfo_key = TINFO_function;
391 function::function(unsigned ser, exvector * vp)
392 : exprseq(vp), serial(ser)
394 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
395 tinfo_key = TINFO_function;
399 // functions overriding virtual functions from bases classes
404 basic * function::duplicate() const
406 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
407 return new function(*this);
410 void function::printraw(ostream & os) const
412 debugmsg("function printraw",LOGLEVEL_PRINT);
414 ASSERT(serial<registered_functions().size());
416 os << "function(name=" << registered_functions()[serial].name;
417 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
424 void function::print(ostream & os, unsigned upper_precedence) const
426 debugmsg("function print",LOGLEVEL_PRINT);
428 ASSERT(serial<registered_functions().size());
430 os << registered_functions()[serial].name;
431 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
434 void function::printtree(ostream & os, unsigned indent) const
436 debugmsg("function printtree",LOGLEVEL_PRINT);
438 ASSERT(serial<registered_functions().size());
440 os << string(indent,' ') << "function "
441 << registered_functions()[serial].name
442 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
443 << ", flags=" << flags
444 << ", nops=" << nops() << endl;
445 for (int i=0; i<nops(); ++i) {
446 seq[i].printtree(os,indent+delta_indent);
448 os << string(indent+delta_indent,' ') << "=====" << endl;
451 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
453 debugmsg("function print csrc",LOGLEVEL_PRINT);
455 ASSERT(serial<registered_functions().size());
457 // Print function name in lowercase
459 lname=registered_functions()[serial].name;
460 for (unsigned i=0; i<lname.size(); i++)
461 lname[i] = tolower(lname[i]);
464 // Print arguments, separated by commas
465 exvector::const_iterator it = seq.begin();
466 exvector::const_iterator itend = seq.end();
467 while (it != itend) {
468 it->bp->printcsrc(os, type, 0);
476 ex function::expand(unsigned options) const
478 return this->setflag(status_flags::expanded);
481 ex function::eval(int level) const
483 ASSERT(serial<registered_functions().size());
485 exvector eseq=evalchildren(level);
487 if (registered_functions()[serial].e==0) {
488 return function(serial,eseq).hold();
490 switch (registered_functions()[serial].nparams) {
491 // the following lines have been generated for max. ${maxargs} parameters
492 ${eval_switch_statement}
493 // end of generated lines
495 throw(std::logic_error("function::eval(): invalid nparams"));
498 ex function::evalf(int level) const
500 ASSERT(serial<registered_functions().size());
502 exvector eseq=evalfchildren(level);
504 if (registered_functions()[serial].ef==0) {
505 return function(serial,eseq).hold();
507 switch (registered_functions()[serial].nparams) {
508 // the following lines have been generated for max. ${maxargs} parameters
509 ${evalf_switch_statement}
510 // end of generated lines
512 throw(std::logic_error("function::evalf(): invalid nparams"));
515 ex function::thisexprseq(exvector const & v) const
517 return function(serial,v);
520 ex function::thisexprseq(exvector * vp) const
522 return function(serial,vp);
525 /** Implementation of ex::series for functions.
526 * \@see ex::series */
527 ex function::series(symbol const & s, ex const & point, int order) const
529 ASSERT(serial<registered_functions().size());
531 if (registered_functions()[serial].s==0) {
532 return basic::series(s, point, order);
534 switch (registered_functions()[serial].nparams) {
535 // the following lines have been generated for max. ${maxargs} parameters
536 ${series_switch_statement}
537 // end of generated lines
539 throw(std::logic_error("function::series(): invalid nparams"));
544 int function::compare_same_type(basic const & other) const
546 ASSERT(is_of_type(other, function));
547 function const & o=static_cast<function &>(const_cast<basic &>(other));
549 if (serial!=o.serial) {
550 return serial < o.serial ? -1 : 1;
552 return exprseq::compare_same_type(o);
555 bool function::is_equal_same_type(basic const & other) const
557 ASSERT(is_of_type(other, function));
558 function const & o=static_cast<function &>(const_cast<basic &>(other));
560 if (serial!=o.serial) return false;
561 return exprseq::is_equal_same_type(o);
564 unsigned function::return_type(void) const
567 return return_types::commutative;
569 return (*seq.begin()).return_type();
572 unsigned function::return_type_tinfo(void) const
577 return (*seq.begin()).return_type_tinfo();
581 // new virtual functions which can be overridden by derived classes
587 // non-virtual functions in this class
592 ex function::pdiff(unsigned diff_param) const // partial differentiation
594 ASSERT(serial<registered_functions().size());
596 if (registered_functions()[serial].d==0) {
597 throw(std::logic_error(string("function::pdiff(") + registered_functions()[serial].name + "): no diff function defined"));
599 switch (registered_functions()[serial].nparams) {
600 // the following lines have been generated for max. ${maxargs} parameters
601 ${diff_switch_statement}
602 // end of generated lines
604 throw(std::logic_error("function::pdiff(): no diff function defined"));
607 vector<registered_function_info> & function::registered_functions(void)
609 static vector<registered_function_info> * rf=new vector<registered_function_info>;
615 // the following lines have been generated for max. ${maxargs} parameters
616 $register_new_implementation
617 // end of generated lines
620 // static member variables
629 const function some_function;
630 type_info const & typeid_function=typeid(some_function);
632 END_OF_IMPLEMENTATION
634 print "Creating interface file function.h...";
635 open OUT,">function.h" or die "cannot open function.h";
636 print OUT $interface;
640 print "Creating implementation file function.cpp...";
641 open OUT,">function.cpp" or die "cannot open function.cpp";
642 print OUT $implementation;