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','const GiNaC::ex & 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','const ex & 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_derivative_funcp=generate(
63 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
66 $typedef_series_funcp=generate(
67 'typedef ex (* series_funcp_${N})(${SEQ1}, const symbol &, const ex &, int);'."\n",
70 $constructors_interface=generate(
71 ' function(unsigned ser, ${SEQ1});'."\n",
72 'const ex & param${N}','');
74 $register_new_interface=generate(
75 ' static unsigned register_new(const char * nm, eval_funcp_${N} e,'."\n".
76 ' evalf_funcp_${N} ef=0, derivative_funcp_${N} d=0, series_funcp_${N} s=0);'.
79 $constructors_implementation=generate(
80 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & 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 ((derivative_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(const char * nm, eval_funcp_${N} e,
125 evalf_funcp_${N} ef, derivative_funcp_${N} d, series_funcp_${N} s)
127 registered_function_info rfi={nm,${N},0,eval_funcp(e),
128 evalf_funcp(ef),derivative_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__
174 #ifndef NO_NAMESPACE_GINAC
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_NAMESPACE_GINAC
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_NAMESPACE_GINAC
188 #ifndef NO_NAMESPACE_GINAC
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_NAMESPACE_GINAC
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_NAMESPACE_GINAC
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_NAMESPACE_GINAC
210 #define TYPECHECK_INTEGER(VAR) \\
211 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
212 automatic_typecheck=false; \\
215 #else // ndef NO_NAMESPACE_GINAC
217 #define TYPECHECK_INTEGER(VAR) \\
218 if (!(VAR).info(info_flags::integer)) { \\
219 automatic_typecheck=false; \\
222 #endif // ndef NO_NAMESPACE_GINAC
224 #define END_TYPECHECK(RV) \\
226 if (!automatic_typecheck) { \\
230 #ifndef NO_NAMESPACE_GINAC
232 #endif // ndef NO_NAMESPACE_GINAC
236 typedef ex (* eval_funcp)();
237 typedef ex (* evalf_funcp)();
238 typedef ex (* derivative_funcp)();
239 typedef ex (* series_funcp)();
241 // the following lines have been generated for max. ${maxargs} parameters
244 $typedef_derivative_funcp
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(const function & other);
274 const function & operator=(const function & other);
276 void copy(const function & 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, const exprseq & es);
286 function(unsigned ser, const exvector & 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 series(const symbol & s, const ex & point, int order) const;
300 ex thisexprseq(const exvector & v) const;
301 ex thisexprseq(exvector * vp) const;
303 ex derivative(const symbol & s) const;
304 int compare_same_type(const basic & other) const;
305 bool is_equal_same_type(const basic & 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 pderivative(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_NAMESPACE_GINAC
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_NAMESPACE_GINAC
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_NAMESPACE_GINAC
344 extern const function some_function;
345 extern const type_info & typeid_function;
347 #ifndef NO_NAMESPACE_GINAC
349 #endif // ndef NO_NAMESPACE_GINAC
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"
390 #include "debugmsg.h"
392 #ifndef NO_NAMESPACE_GINAC
394 #endif // ndef NO_NAMESPACE_GINAC
396 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
399 // default constructor, destructor, copy constructor assignment operator and helpers
404 function::function() : serial(0)
406 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
407 tinfo_key = TINFO_function;
410 function::~function()
412 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
416 function::function(const function & other)
418 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
422 const function & function::operator=(const function & other)
424 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
425 if (this != &other) {
434 void function::copy(const function & other)
436 exprseq::copy(other);
440 void function::destroy(bool call_parent)
442 if (call_parent) exprseq::destroy(call_parent);
446 // other constructors
451 function::function(unsigned ser) : serial(ser)
453 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
454 tinfo_key = TINFO_function;
457 // the following lines have been generated for max. ${maxargs} parameters
458 $constructors_implementation
459 // end of generated lines
461 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
463 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
464 tinfo_key = TINFO_function;
467 function::function(unsigned ser, const exvector & v, bool discardable)
468 : exprseq(v,discardable), serial(ser)
470 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
471 tinfo_key = TINFO_function;
474 function::function(unsigned ser, exvector * vp)
475 : exprseq(vp), serial(ser)
477 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
478 tinfo_key = TINFO_function;
485 /** Construct object from archive_node. */
486 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
488 debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
490 // Find serial number by function name
492 if (n.find_string("name", s)) {
493 unsigned int ser = 0;
494 vector<registered_function_info>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
502 throw (std::runtime_error("unknown function '" + s + "' in archive"));
504 throw (std::runtime_error("unnamed function in archive"));
507 /** Unarchive the object. */
508 ex function::unarchive(const archive_node &n, const lst &sym_lst)
510 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
513 /** Archive the object. */
514 void function::archive(archive_node &n) const
516 inherited::archive(n);
517 GINAC_ASSERT(serial < registered_functions().size());
518 n.add_string("name", registered_functions()[serial].name);
522 // functions overriding virtual functions from bases classes
527 basic * function::duplicate() const
529 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
530 return new function(*this);
533 void function::printraw(ostream & os) const
535 debugmsg("function printraw",LOGLEVEL_PRINT);
537 GINAC_ASSERT(serial<registered_functions().size());
539 os << "function(name=" << registered_functions()[serial].name;
540 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
547 void function::print(ostream & os, unsigned upper_precedence) const
549 debugmsg("function print",LOGLEVEL_PRINT);
551 GINAC_ASSERT(serial<registered_functions().size());
553 os << registered_functions()[serial].name;
554 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
557 void function::printtree(ostream & os, unsigned indent) const
559 debugmsg("function printtree",LOGLEVEL_PRINT);
561 GINAC_ASSERT(serial<registered_functions().size());
563 os << string(indent,' ') << "function "
564 << registered_functions()[serial].name
565 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
566 << ", flags=" << flags
567 << ", nops=" << nops() << endl;
568 for (unsigned i=0; i<nops(); ++i) {
569 seq[i].printtree(os,indent+delta_indent);
571 os << string(indent+delta_indent,' ') << "=====" << endl;
574 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
576 debugmsg("function print csrc",LOGLEVEL_PRINT);
578 GINAC_ASSERT(serial<registered_functions().size());
580 // Print function name in lowercase
582 lname=registered_functions()[serial].name;
583 for (unsigned i=0; i<lname.size(); i++)
584 lname[i] = tolower(lname[i]);
587 // Print arguments, separated by commas
588 exvector::const_iterator it = seq.begin();
589 exvector::const_iterator itend = seq.end();
590 while (it != itend) {
591 it->bp->printcsrc(os, type, 0);
599 ex function::expand(unsigned options) const
601 return this->setflag(status_flags::expanded);
604 ex function::eval(int level) const
606 GINAC_ASSERT(serial<registered_functions().size());
608 exvector eseq=evalchildren(level);
610 if (registered_functions()[serial].e==0) {
611 return function(serial,eseq).hold();
613 switch (registered_functions()[serial].nparams) {
614 // the following lines have been generated for max. ${maxargs} parameters
615 ${eval_switch_statement}
616 // end of generated lines
618 throw(std::logic_error("function::eval(): invalid nparams"));
621 ex function::evalf(int level) const
623 GINAC_ASSERT(serial<registered_functions().size());
625 exvector eseq=evalfchildren(level);
627 if (registered_functions()[serial].ef==0) {
628 return function(serial,eseq).hold();
630 switch (registered_functions()[serial].nparams) {
631 // the following lines have been generated for max. ${maxargs} parameters
632 ${evalf_switch_statement}
633 // end of generated lines
635 throw(std::logic_error("function::evalf(): invalid nparams"));
638 ex function::thisexprseq(const exvector & v) const
640 return function(serial,v);
643 ex function::thisexprseq(exvector * vp) const
645 return function(serial,vp);
648 /** Implementation of ex::series for functions.
649 * \@see ex::series */
650 ex function::series(const symbol & s, const ex & point, int order) const
652 GINAC_ASSERT(serial<registered_functions().size());
654 if (registered_functions()[serial].s==0) {
655 return basic::series(s, point, order);
658 switch (registered_functions()[serial].nparams) {
659 // the following lines have been generated for max. ${maxargs} parameters
660 ${series_switch_statement}
661 // end of generated lines
663 throw(std::logic_error("function::series(): invalid nparams"));
669 /** Implementation of ex::diff() for functions. It applies the chain rule,
670 * except for the Order term function.
672 ex function::derivative(const symbol & s) const
676 if (serial==function_index_Order) {
677 // Order Term function only differentiates the argument
678 return Order(seq[0].diff(s));
682 for (unsigned i=0; i!=seq.size(); i++) {
683 arg_diff = seq[i].diff(s);
684 // We apply the chain rule only when it makes sense. This is not
685 // just for performance reasons but also to allow functions to
686 // throw when differentiated with respect to one of its arguments
687 // without running into trouble with our automatic full
689 if (!arg_diff.is_zero())
690 result += pderivative(i)*arg_diff;
696 int function::compare_same_type(const basic & other) const
698 GINAC_ASSERT(is_of_type(other, function));
699 const function & o=static_cast<function &>(const_cast<basic &>(other));
701 if (serial!=o.serial) {
702 return serial < o.serial ? -1 : 1;
704 return exprseq::compare_same_type(o);
707 bool function::is_equal_same_type(const basic & other) const
709 GINAC_ASSERT(is_of_type(other, function));
710 const function & o=static_cast<function &>(const_cast<basic &>(other));
712 if (serial!=o.serial) return false;
713 return exprseq::is_equal_same_type(o);
716 unsigned function::return_type(void) const
719 return return_types::commutative;
721 return (*seq.begin()).return_type();
724 unsigned function::return_type_tinfo(void) const
729 return (*seq.begin()).return_type_tinfo();
733 // new virtual functions which can be overridden by derived classes
739 // non-virtual functions in this class
744 ex function::pderivative(unsigned diff_param) const // partial differentiation
746 GINAC_ASSERT(serial<registered_functions().size());
748 if (registered_functions()[serial].d==0) {
749 throw(std::logic_error(string("function::pderivative(") + registered_functions()[serial].name + "): no diff function defined"));
751 switch (registered_functions()[serial].nparams) {
752 // the following lines have been generated for max. ${maxargs} parameters
753 ${diff_switch_statement}
754 // end of generated lines
756 throw(std::logic_error("function::pderivative(): no diff function defined"));
759 vector<registered_function_info> & function::registered_functions(void)
761 static vector<registered_function_info> * rf=new vector<registered_function_info>;
767 // the following lines have been generated for max. ${maxargs} parameters
768 $register_new_implementation
769 // end of generated lines
772 // static member variables
781 const function some_function;
782 const type_info & typeid_function=typeid(some_function);
784 #ifndef NO_NAMESPACE_GINAC
786 #endif // ndef NO_NAMESPACE_GINAC
788 END_OF_IMPLEMENTATION
790 print "Creating interface file function.h...";
791 open OUT,">function.h" or die "cannot open function.h";
792 print OUT $interface;
796 print "Creating implementation file function.cpp...";
797 open OUT,">function.cpp" or die "cannot open function.cpp";
798 print OUT $implementation;