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 const 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]','');
114 res = ((series_funcp_${N})(registered_functions()[serial].s))(${SEQ1},s,point,order);
115 } catch (do_taylor) {
116 res = basic::series(s, point, order);
120 END_OF_SERIES_SWITCH_STATEMENT
122 $register_new_implementation=generate(
123 <<'END_OF_REGISTER_NEW_IMPLEMENTATION','','');
124 unsigned function::register_new(char const * nm, eval_funcp_${N} e,
125 evalf_funcp_${N} ef, diff_funcp_${N} d, series_funcp_${N} s)
127 registered_function_info rfi={nm,${N},0,eval_funcp(e),
128 evalf_funcp(ef),diff_funcp(d),series_funcp(s)};
129 registered_functions().push_back(rfi);
130 return registered_functions().size()-1;
132 END_OF_REGISTER_NEW_IMPLEMENTATION
134 $interface=<<END_OF_INTERFACE;
135 /** \@file function.h
137 * Interface to abstract class function (new function concept). */
140 * This file was generated automatically by function.pl.
141 * Please do not modify it directly, edit the perl script instead!
142 * function.pl options: \$maxargs=${maxargs}
144 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
146 * This program is free software; you can redistribute it and/or modify
147 * it under the terms of the GNU General Public License as published by
148 * the Free Software Foundation; either version 2 of the License, or
149 * (at your option) any later version.
151 * This program is distributed in the hope that it will be useful,
152 * but WITHOUT ANY WARRANTY; without even the implied warranty of
153 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
154 * GNU General Public License for more details.
156 * You should have received a copy of the GNU General Public License
157 * along with this program; if not, write to the Free Software
158 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
161 #ifndef __GINAC_FUNCTION_H__
162 #define __GINAC_FUNCTION_H__
168 // CINT needs <algorithm> to work properly with <vector>
170 #endif // def __CINT__
172 #include <ginac/exprseq.h>
174 #ifndef NO_GINAC_NAMESPACE
176 // the following lines have been generated for max. ${maxargs} parameters
177 $declare_function_macro_namespace
178 // end of generated lines
180 #else // ndef NO_GINAC_NAMESPACE
182 // the following lines have been generated for max. ${maxargs} parameters
183 $declare_function_macro_no_namespace
184 // end of generated lines
186 #endif // ndef NO_GINAC_NAMESPACE
188 #ifndef NO_GINAC_NAMESPACE
190 #define REGISTER_FUNCTION(NAME,E,EF,D,S) \\
191 const unsigned function_index_##NAME=GiNaC::function::register_new(#NAME,E,EF,D,S);
193 #else // ndef NO_GINAC_NAMESPACE
195 #define REGISTER_FUNCTION(NAME,E,EF,D,S) \\
196 const unsigned function_index_##NAME=function::register_new(#NAME,E,EF,D,S);
198 #endif // ndef NO_GINAC_NAMESPACE
200 #define BEGIN_TYPECHECK \\
201 bool automatic_typecheck=true;
203 #define TYPECHECK(VAR,TYPE) \\
204 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
205 automatic_typecheck=false; \\
208 #ifndef NO_GINAC_NAMESPACE
210 #define TYPECHECK_INTEGER(VAR) \\
211 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
212 automatic_typecheck=false; \\
215 #else // ndef NO_GINAC_NAMESPACE
217 #define TYPECHECK_INTEGER(VAR) \\
218 if (!(VAR).info(info_flags::integer)) { \\
219 automatic_typecheck=false; \\
222 #endif // ndef NO_GINAC_NAMESPACE
224 #define END_TYPECHECK(RV) \\
226 if (!automatic_typecheck) { \\
230 #ifndef NO_GINAC_NAMESPACE
232 #endif // ndef NO_GINAC_NAMESPACE
236 typedef ex (* eval_funcp)();
237 typedef ex (* evalf_funcp)();
238 typedef ex (* diff_funcp)();
239 typedef ex (* series_funcp)();
241 // the following lines have been generated for max. ${maxargs} parameters
245 $typedef_series_funcp
246 // end of generated lines
248 struct registered_function_info {
258 /** The class function is used to implement builtin functions like sin, cos...
259 and user defined functions */
260 class function : public exprseq
262 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
264 // CINT has a linking problem
265 friend void ginsh_get_ginac_functions(void);
269 // default constructor, destructor, copy constructor assignment operator and helpers
273 function(function const & other);
274 function const & operator=(function const & other);
276 void copy(function const & other);
277 void destroy(bool call_parent);
279 // other constructors
281 function(unsigned ser);
282 // the following lines have been generated for max. ${maxargs} parameters
283 $constructors_interface
284 // end of generated lines
285 function(unsigned ser, exprseq const & es);
286 function(unsigned ser, exvector const & v, bool discardable=0);
287 function(unsigned ser, exvector * vp); // vp will be deleted
289 // functions overriding virtual functions from bases classes
291 basic * duplicate() const;
292 void printraw(ostream & os) const;
293 void print(ostream & os, unsigned upper_precedence=0) const;
294 void printtree(ostream & os, unsigned indent) const;
295 void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
296 ex expand(unsigned options=0) const;
297 ex eval(int level=0) const;
298 ex evalf(int level=0) const;
299 ex diff(symbol const & s) const;
300 ex series(symbol const & s, ex const & point, int order) const;
301 ex thisexprseq(exvector const & v) const;
302 ex thisexprseq(exvector * vp) const;
304 int compare_same_type(basic const & other) const;
305 bool is_equal_same_type(basic const & other) const;
306 unsigned return_type(void) const;
307 unsigned return_type_tinfo(void) const;
309 // new virtual functions which can be overridden by derived classes
312 // non-virtual functions in this class
314 ex pdiff(unsigned diff_param) const; // partial differentiation
315 static vector<registered_function_info> & registered_functions(void);
317 // the following lines have been generated for max. ${maxargs} parameters
318 $register_new_interface
319 // end of generated lines
320 unsigned getserial(void) const {return serial;}
330 #ifndef NO_GINAC_NAMESPACE
332 #define is_ex_the_function(OBJ, FUNCNAME) \\
333 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
335 #else // ndef NO_GINAC_NAMESPACE
337 #define is_ex_the_function(OBJ, FUNCNAME) \\
338 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
340 #endif // ndef NO_GINAC_NAMESPACE
344 extern const function some_function;
345 extern type_info const & typeid_function;
347 #ifndef NO_GINAC_NAMESPACE
349 #endif // ndef NO_GINAC_NAMESPACE
351 #endif // ndef __GINAC_FUNCTION_H__
355 $implementation=<<END_OF_IMPLEMENTATION;
356 /** \@file function.cpp
358 * Implementation of class function. */
361 * This file was generated automatically by function.pl.
362 * Please do not modify it directly, edit the perl script instead!
363 * function.pl options: \$maxargs=${maxargs}
365 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
367 * This program is free software; you can redistribute it and/or modify
368 * it under the terms of the GNU General Public License as published by
369 * the Free Software Foundation; either version 2 of the License, or
370 * (at your option) any later version.
372 * This program is distributed in the hope that it will be useful,
373 * but WITHOUT ANY WARRANTY; without even the implied warranty of
374 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
375 * GNU General Public License for more details.
377 * You should have received a copy of the GNU General Public License
378 * along with this program; if not, write to the Free Software
379 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
385 #include "function.h"
389 #include "debugmsg.h"
391 #ifndef NO_GINAC_NAMESPACE
393 #endif // ndef NO_GINAC_NAMESPACE
395 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
398 // default constructor, destructor, copy constructor assignment operator and helpers
403 function::function() : serial(0)
405 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
406 tinfo_key = TINFO_function;
409 function::~function()
411 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
415 function::function(function const & other)
417 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
421 function const & function::operator=(function const & other)
423 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
424 if (this != &other) {
433 void function::copy(function const & other)
435 exprseq::copy(other);
439 void function::destroy(bool call_parent)
441 if (call_parent) exprseq::destroy(call_parent);
445 // other constructors
450 function::function(unsigned ser) : serial(ser)
452 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
453 tinfo_key = TINFO_function;
456 // the following lines have been generated for max. ${maxargs} parameters
457 $constructors_implementation
458 // end of generated lines
460 function::function(unsigned ser, exprseq const & es) : exprseq(es), serial(ser)
462 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
463 tinfo_key = TINFO_function;
466 function::function(unsigned ser, exvector const & v, bool discardable)
467 : exprseq(v,discardable), serial(ser)
469 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
470 tinfo_key = TINFO_function;
473 function::function(unsigned ser, exvector * vp)
474 : exprseq(vp), serial(ser)
476 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
477 tinfo_key = TINFO_function;
484 /** Construct object from archive_node. */
485 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
487 debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
489 // Find serial number by function name
491 if (n.find_string("name", s)) {
492 unsigned int ser = 0;
493 vector<registered_function_info>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
501 throw (std::runtime_error("unknown function '" + s + "' in archive"));
503 throw (std::runtime_error("unnamed function in archive"));
506 /** Unarchive the object. */
507 ex function::unarchive(const archive_node &n, const lst &sym_lst)
509 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
512 /** Archive the object. */
513 void function::archive(archive_node &n) const
515 inherited::archive(n);
516 GINAC_ASSERT(serial < registered_functions().size());
517 n.add_string("name", registered_functions()[serial].name);
521 // functions overriding virtual functions from bases classes
526 basic * function::duplicate() const
528 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
529 return new function(*this);
532 void function::printraw(ostream & os) const
534 debugmsg("function printraw",LOGLEVEL_PRINT);
536 GINAC_ASSERT(serial<registered_functions().size());
538 os << "function(name=" << registered_functions()[serial].name;
539 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
546 void function::print(ostream & os, unsigned upper_precedence) const
548 debugmsg("function print",LOGLEVEL_PRINT);
550 GINAC_ASSERT(serial<registered_functions().size());
552 os << registered_functions()[serial].name;
553 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
556 void function::printtree(ostream & os, unsigned indent) const
558 debugmsg("function printtree",LOGLEVEL_PRINT);
560 GINAC_ASSERT(serial<registered_functions().size());
562 os << string(indent,' ') << "function "
563 << registered_functions()[serial].name
564 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
565 << ", flags=" << flags
566 << ", nops=" << nops() << endl;
567 for (unsigned i=0; i<nops(); ++i) {
568 seq[i].printtree(os,indent+delta_indent);
570 os << string(indent+delta_indent,' ') << "=====" << endl;
573 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
575 debugmsg("function print csrc",LOGLEVEL_PRINT);
577 GINAC_ASSERT(serial<registered_functions().size());
579 // Print function name in lowercase
581 lname=registered_functions()[serial].name;
582 for (unsigned i=0; i<lname.size(); i++)
583 lname[i] = tolower(lname[i]);
586 // Print arguments, separated by commas
587 exvector::const_iterator it = seq.begin();
588 exvector::const_iterator itend = seq.end();
589 while (it != itend) {
590 it->bp->printcsrc(os, type, 0);
598 ex function::expand(unsigned options) const
600 return this->setflag(status_flags::expanded);
603 ex function::eval(int level) const
605 GINAC_ASSERT(serial<registered_functions().size());
607 exvector eseq=evalchildren(level);
609 if (registered_functions()[serial].e==0) {
610 return function(serial,eseq).hold();
612 switch (registered_functions()[serial].nparams) {
613 // the following lines have been generated for max. ${maxargs} parameters
614 ${eval_switch_statement}
615 // end of generated lines
617 throw(std::logic_error("function::eval(): invalid nparams"));
620 ex function::evalf(int level) const
622 GINAC_ASSERT(serial<registered_functions().size());
624 exvector eseq=evalfchildren(level);
626 if (registered_functions()[serial].ef==0) {
627 return function(serial,eseq).hold();
629 switch (registered_functions()[serial].nparams) {
630 // the following lines have been generated for max. ${maxargs} parameters
631 ${evalf_switch_statement}
632 // end of generated lines
634 throw(std::logic_error("function::evalf(): invalid nparams"));
637 ex function::thisexprseq(exvector const & v) const
639 return function(serial,v);
642 ex function::thisexprseq(exvector * vp) const
644 return function(serial,vp);
647 /** Implementation of ex::series for functions.
648 * \@see ex::series */
649 ex function::series(symbol const & s, ex const & point, int order) const
651 GINAC_ASSERT(serial<registered_functions().size());
653 if (registered_functions()[serial].s==0) {
654 return basic::series(s, point, order);
657 switch (registered_functions()[serial].nparams) {
658 // the following lines have been generated for max. ${maxargs} parameters
659 ${series_switch_statement}
660 // end of generated lines
662 throw(std::logic_error("function::series(): invalid nparams"));
667 int function::compare_same_type(basic const & other) const
669 GINAC_ASSERT(is_of_type(other, function));
670 function const & o=static_cast<function &>(const_cast<basic &>(other));
672 if (serial!=o.serial) {
673 return serial < o.serial ? -1 : 1;
675 return exprseq::compare_same_type(o);
678 bool function::is_equal_same_type(basic const & other) const
680 GINAC_ASSERT(is_of_type(other, function));
681 function const & o=static_cast<function &>(const_cast<basic &>(other));
683 if (serial!=o.serial) return false;
684 return exprseq::is_equal_same_type(o);
687 unsigned function::return_type(void) const
690 return return_types::commutative;
692 return (*seq.begin()).return_type();
695 unsigned function::return_type_tinfo(void) const
700 return (*seq.begin()).return_type_tinfo();
704 // new virtual functions which can be overridden by derived classes
710 // non-virtual functions in this class
715 ex function::pdiff(unsigned diff_param) const // partial differentiation
717 GINAC_ASSERT(serial<registered_functions().size());
719 if (registered_functions()[serial].d==0) {
720 throw(std::logic_error(string("function::pdiff(") + registered_functions()[serial].name + "): no diff function defined"));
722 switch (registered_functions()[serial].nparams) {
723 // the following lines have been generated for max. ${maxargs} parameters
724 ${diff_switch_statement}
725 // end of generated lines
727 throw(std::logic_error("function::pdiff(): no diff function defined"));
730 vector<registered_function_info> & function::registered_functions(void)
732 static vector<registered_function_info> * rf=new vector<registered_function_info>;
738 // the following lines have been generated for max. ${maxargs} parameters
739 $register_new_implementation
740 // end of generated lines
743 // static member variables
752 const function some_function;
753 type_info const & typeid_function=typeid(some_function);
755 #ifndef NO_GINAC_NAMESPACE
757 #endif // ndef NO_GINAC_NAMESPACE
759 END_OF_IMPLEMENTATION
761 print "Creating interface file function.h...";
762 open OUT,">function.h" or die "cannot open function.h";
763 print OUT $interface;
767 print "Creating implementation file function.cpp...";
768 open OUT,">function.cpp" or die "cannot open function.cpp";
769 print OUT $implementation;