4 my ($seq_template,$n)=@_;
8 for ($N=1; $N<=$n; $N++) {
9 $res .= eval('"' . $seq_template . '"');
17 sub generate_from_to {
18 my ($template,$seq_template1,$seq_template2,$from,$to)=@_;
22 for ($N=$from; $N<=$to; $N++) {
23 $SEQ1=generate_seq($seq_template1,$N);
24 $SEQ2=generate_seq($seq_template2,$N);
25 $res .= eval('"' . $template . '"');
26 $SEQ1=''; # to avoid main::SEQ1 used only once warning
27 $SEQ2=''; # same as above
33 my ($template,$seq_template1,$seq_template2)=@_;
34 return generate_from_to($template,$seq_template1,$seq_template2,1,$maxargs);
37 $declare_function_macro_namespace = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NAMESPACE';
38 #ifdef CINT_CONVERSION_WORKAROUND
40 #define DECLARE_FUNCTION_1P(NAME) \
41 extern const unsigned function_index_##NAME; \
42 inline GiNaC::function NAME(const GiNaC::ex & p1) { \
43 return GiNaC::function(function_index_##NAME, p1); \
45 inline GiNaC::function NAME(const GiNaC::basic & p1) { \
46 return GiNaC::function(function_index_##NAME, GiNaC::ex(p1)); \
48 #define DECLARE_FUNCTION_2P(NAME) \
49 extern const unsigned function_index_##NAME; \
50 inline GiNaC::function NAME(const GiNaC::ex & p1, const GiNaC::ex & p2) { \
51 return GiNaC::function(function_index_##NAME, p1, p2); \
53 inline GiNaC::function NAME(const GiNaC::basic & p1, const GiNaC::ex & p2) { \
54 return GiNaC::function(function_index_##NAME, GiNaC::ex(p1), p2); \
56 inline GiNaC::function NAME(const GiNaC::ex & p1, const GiNaC::basic & p2) { \
57 return GiNaC::function(function_index_##NAME, p1, GiNaC::ex(p2)); \
59 inline GiNaC::function NAME(const GiNaC::basic & p1, const GiNaC::basic & p2) { \
60 return GiNaC::function(function_index_##NAME, GiNaC::ex(p1), GiNaC::ex(p2)); \
63 #else // def CINT_CONVERSION_WORKAROUND
65 #define DECLARE_FUNCTION_1P(NAME) \
66 extern const unsigned function_index_##NAME; \
67 inline GiNaC::function NAME(const GiNaC::ex & p1) { \
68 return GiNaC::function(function_index_##NAME, p1); \
70 #define DECLARE_FUNCTION_2P(NAME) \
71 extern const unsigned function_index_##NAME; \
72 inline GiNaC::function NAME(const GiNaC::ex & p1, const GiNaC::ex & p2) { \
73 return GiNaC::function(function_index_##NAME, p1, p2); \
76 #endif // def CINT_CONVERSION_WORKAROUND
78 END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NAMESPACE
80 $declare_function_macro_namespace .= generate_from_to(
81 <<'END_OF_DECLARE_FUNCTION_MACRO_NAMESPACE','const GiNaC::ex & p${N}','p${N}',3,$maxargs);
82 #define DECLARE_FUNCTION_${N}P(NAME) \\
83 extern const unsigned function_index_##NAME; \\
84 inline GiNaC::function NAME(${SEQ1}) { \\
85 return GiNaC::function(function_index_##NAME, ${SEQ2}); \\
88 END_OF_DECLARE_FUNCTION_MACRO_NAMESPACE
90 $declare_function_macro_no_namespace = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NO_NAMESPACE';
91 #ifdef CINT_CONVERSION_WORKAROUND
93 #define DECLARE_FUNCTION_1P(NAME) \
94 extern const unsigned function_index_##NAME; \
95 inline function NAME(const ex & p1) { \
96 return function(function_index_##NAME, p1); \
98 inline function NAME(const basic & p1) { \
99 return function(function_index_##NAME, ex(p1)); \
101 #define DECLARE_FUNCTION_2P(NAME) \
102 extern const unsigned function_index_##NAME; \
103 inline function NAME(const ex & p1, const ex & p2) { \
104 return function(function_index_##NAME, p1, p2); \
106 inline function NAME(const basic & p1, const ex & p2) { \
107 return function(function_index_##NAME, ex(p1), p2); \
109 inline function NAME(const ex & p1, const basic & p2) { \
110 return function(function_index_##NAME, p1, ex(p2)); \
112 inline function NAME(const basic & p1, const basic & p2) { \
113 return function(function_index_##NAME, ex(p1), ex(p2)); \
116 #else // def CINT_CONVERSION_WORKAROUND
118 #define DECLARE_FUNCTION_1P(NAME) \
119 extern const unsigned function_index_##NAME; \
120 inline function NAME(const ex & p1) { \
121 return function(function_index_##NAME, p1); \
123 #define DECLARE_FUNCTION_2P(NAME) \
124 extern const unsigned function_index_##NAME; \
125 inline function NAME(const ex & p1, const ex & p2) { \
126 return function(function_index_##NAME, p1, p2); \
129 #endif // def CINT_CONVERSION_WORKAROUND
131 END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NO_NAMESPACE
133 $declare_function_macro_no_namespace .= generate_from_to(
134 <<'END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE','const ex & p${N}','p${N}',3,$maxargs);
135 #define DECLARE_FUNCTION_${N}P(NAME) \\
136 extern const unsigned function_index_##NAME; \\
137 inline function NAME(${SEQ1}) { \\
138 return function(function_index_##NAME, ${SEQ2}); \\
141 END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE
143 $typedef_eval_funcp=generate(
144 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
147 $typedef_evalf_funcp=generate(
148 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
151 $typedef_derivative_funcp=generate(
152 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
155 $typedef_series_funcp=generate(
156 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int);'."\n",
159 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','');
161 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','');
163 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','');
165 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','');
167 $constructors_interface=generate(
168 ' function(unsigned ser, ${SEQ1});'."\n",
169 'const ex & param${N}','');
171 $constructors_implementation=generate(
172 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}');
173 function::function(unsigned ser, ${SEQ1})
174 : exprseq(${SEQ2}), serial(ser)
176 debugmsg(\"function constructor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
177 tinfo_key = TINFO_function;
179 END_OF_CONSTRUCTORS_IMPLEMENTATION
181 $eval_switch_statement=generate(
182 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
184 eval_result=((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
186 END_OF_EVAL_SWITCH_STATEMENT
188 $evalf_switch_statement=generate(
189 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
191 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
193 END_OF_EVALF_SWITCH_STATEMENT
195 $diff_switch_statement=generate(
196 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
198 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
200 END_OF_DIFF_SWITCH_STATEMENT
202 $series_switch_statement=generate(
203 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
206 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order);
207 } catch (do_taylor) {
208 res = basic::series(r, order);
212 END_OF_SERIES_SWITCH_STATEMENT
214 $eval_func_implementation=generate(
215 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','');
216 function_options & function_options::eval_func(eval_funcp_${N} e)
218 test_and_set_nparams(${N});
219 eval_f=eval_funcp(e);
222 END_OF_EVAL_FUNC_IMPLEMENTATION
224 $evalf_func_implementation=generate(
225 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','');
226 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
228 test_and_set_nparams(${N});
229 evalf_f=evalf_funcp(ef);
232 END_OF_EVALF_FUNC_IMPLEMENTATION
234 $derivative_func_implementation=generate(
235 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','');
236 function_options & function_options::derivative_func(derivative_funcp_${N} d)
238 test_and_set_nparams(${N});
239 derivative_f=derivative_funcp(d);
242 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
244 $series_func_implementation=generate(
245 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','');
246 function_options & function_options::series_func(series_funcp_${N} s)
248 test_and_set_nparams(${N});
249 series_f=series_funcp(s);
252 END_OF_SERIES_FUNC_IMPLEMENTATION
254 $interface=<<END_OF_INTERFACE;
255 /** \@file function.h
257 * Interface to abstract class function (new function concept). */
260 * This file was generated automatically by function.pl.
261 * Please do not modify it directly, edit the perl script instead!
262 * function.pl options: \$maxargs=${maxargs}
264 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
266 * This program is free software; you can redistribute it and/or modify
267 * it under the terms of the GNU General Public License as published by
268 * the Free Software Foundation; either version 2 of the License, or
269 * (at your option) any later version.
271 * This program is distributed in the hope that it will be useful,
272 * but WITHOUT ANY WARRANTY; without even the implied warranty of
273 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
274 * GNU General Public License for more details.
276 * You should have received a copy of the GNU General Public License
277 * along with this program; if not, write to the Free Software
278 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
281 #ifndef __GINAC_FUNCTION_H__
282 #define __GINAC_FUNCTION_H__
288 // CINT needs <algorithm> to work properly with <vector>
290 #endif // def __CINT__
294 #ifndef NO_NAMESPACE_GINAC
296 // the following lines have been generated for max. ${maxargs} parameters
297 $declare_function_macro_namespace
298 // end of generated lines
300 #else // ndef NO_NAMESPACE_GINAC
302 // the following lines have been generated for max. ${maxargs} parameters
303 $declare_function_macro_no_namespace
304 // end of generated lines
306 #endif // ndef NO_NAMESPACE_GINAC
308 #ifndef NO_NAMESPACE_GINAC
310 #define REGISTER_FUNCTION(NAME,OPT) \\
311 const unsigned function_index_##NAME= \\
312 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
314 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
315 const unsigned function_index_##NAME= \\
316 GiNaC::function::register_new(GiNaC::function_options(#NAME). \\
319 derivative_func(D). \\
322 #else // ndef NO_NAMESPACE_GINAC
324 #define REGISTER_FUNCTION(NAME,OPT) \\
325 const unsigned function_index_##NAME= \\
326 function::register_new(function_options(#NAME).OPT);
328 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
329 const unsigned function_index_##NAME= \\
330 function::register_new(function_options(#NAME). \\
333 derivative_func(D). \\
336 #endif // ndef NO_NAMESPACE_GINAC
338 #define BEGIN_TYPECHECK \\
339 bool automatic_typecheck=true;
341 #define TYPECHECK(VAR,TYPE) \\
342 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
343 automatic_typecheck=false; \\
346 #ifndef NO_NAMESPACE_GINAC
348 #define TYPECHECK_INTEGER(VAR) \\
349 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
350 automatic_typecheck=false; \\
353 #else // ndef NO_NAMESPACE_GINAC
355 #define TYPECHECK_INTEGER(VAR) \\
356 if (!(VAR).info(info_flags::integer)) { \\
357 automatic_typecheck=false; \\
360 #endif // ndef NO_NAMESPACE_GINAC
362 #define END_TYPECHECK(RV) \\
364 if (!automatic_typecheck) { \\
368 #ifndef NO_NAMESPACE_GINAC
370 #endif // ndef NO_NAMESPACE_GINAC
374 typedef ex (* eval_funcp)();
375 typedef ex (* evalf_funcp)();
376 typedef ex (* derivative_funcp)();
377 typedef ex (* series_funcp)();
379 // the following lines have been generated for max. ${maxargs} parameters
382 $typedef_derivative_funcp
383 $typedef_series_funcp
384 // end of generated lines
386 class function_options
388 friend class function;
391 function_options(string const & n, string const & tn=string());
393 void initialize(void);
394 function_options & set_name(string const & n, string const & tn=string());
395 // the following lines have been generated for max. ${maxargs} parameters
397 $evalf_func_interface
398 $derivative_func_interface
399 $series_func_interface
400 // end of generated lines
401 function_options & set_return_type(unsigned rt, unsigned rtt=0);
402 function_options & do_not_evalf_params(void);
403 function_options & remember(unsigned size, unsigned assoc_size=0,
404 unsigned strategy=remember_strategies::delete_never);
405 function_options & overloaded(unsigned o);
406 void test_and_set_nparams(unsigned n);
407 string get_name(void) const { return name; }
408 unsigned get_nparams(void) const { return nparams; }
418 derivative_funcp derivative_f;
419 series_funcp series_f;
421 bool evalf_params_first;
423 bool use_return_type;
424 unsigned return_type;
425 unsigned return_type_tinfo;
428 unsigned remember_size;
429 unsigned remember_assoc_size;
430 unsigned remember_strategy;
432 unsigned functions_with_same_name;
435 /** The class function is used to implement builtin functions like sin, cos...
436 and user defined functions */
437 class function : public exprseq
439 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
441 // CINT has a linking problem
442 friend void ginsh_get_ginac_functions(void);
444 friend class remember_table_entry;
445 // friend class remember_table_list;
446 // friend class remember_table;
450 // default constructor, destructor, copy constructor assignment operator and helpers
454 function(const function & other);
455 const function & operator=(const function & other);
457 void copy(const function & other);
458 void destroy(bool call_parent);
460 // other constructors
462 function(unsigned ser);
463 // the following lines have been generated for max. ${maxargs} parameters
464 $constructors_interface
465 // end of generated lines
466 function(unsigned ser, const exprseq & es);
467 function(unsigned ser, const exvector & v, bool discardable=0);
468 function(unsigned ser, exvector * vp); // vp will be deleted
470 // functions overriding virtual functions from bases classes
472 basic * duplicate() const;
473 void printraw(ostream & os) const;
474 void print(ostream & os, unsigned upper_precedence=0) const;
475 void printtree(ostream & os, unsigned indent) const;
476 void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
477 ex expand(unsigned options=0) const;
478 ex eval(int level=0) const;
479 ex evalf(int level=0) const;
480 ex series(const relational & r, int order) const;
481 ex thisexprseq(const exvector & v) const;
482 ex thisexprseq(exvector * vp) const;
484 ex derivative(const symbol & s) const;
485 int compare_same_type(const basic & other) const;
486 bool is_equal_same_type(const basic & other) const;
487 unsigned return_type(void) const;
488 unsigned return_type_tinfo(void) const;
490 // new virtual functions which can be overridden by derived classes
493 // non-virtual functions in this class
495 ex pderivative(unsigned diff_param) const; // partial differentiation
496 static vector<function_options> & registered_functions(void);
497 bool lookup_remember_table(ex & result) const;
498 void store_remember_table(ex const & result) const;
500 static unsigned register_new(function_options const & opt);
501 unsigned getserial(void) const {return serial;}
509 // utility functions/macros
510 inline const function &ex_to_function(const ex &e)
512 return static_cast<const function &>(*e.bp);
515 #ifndef NO_NAMESPACE_GINAC
517 #define is_ex_the_function(OBJ, FUNCNAME) \\
518 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
520 #else // ndef NO_NAMESPACE_GINAC
522 #define is_ex_the_function(OBJ, FUNCNAME) \\
523 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
525 #endif // ndef NO_NAMESPACE_GINAC
529 extern const function some_function;
530 extern const type_info & typeid_function;
532 #ifndef NO_NAMESPACE_GINAC
534 #endif // ndef NO_NAMESPACE_GINAC
536 #endif // ndef __GINAC_FUNCTION_H__
540 $implementation=<<END_OF_IMPLEMENTATION;
541 /** \@file function.cpp
543 * Implementation of class function. */
546 * This file was generated automatically by function.pl.
547 * Please do not modify it directly, edit the perl script instead!
548 * function.pl options: \$maxargs=${maxargs}
550 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
552 * This program is free software; you can redistribute it and/or modify
553 * it under the terms of the GNU General Public License as published by
554 * the Free Software Foundation; either version 2 of the License, or
555 * (at your option) any later version.
557 * This program is distributed in the hope that it will be useful,
558 * but WITHOUT ANY WARRANTY; without even the implied warranty of
559 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
560 * GNU General Public License for more details.
562 * You should have received a copy of the GNU General Public License
563 * along with this program; if not, write to the Free Software
564 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
571 #include "function.h"
577 #include "debugmsg.h"
578 #include "remember.h"
580 #ifndef NO_NAMESPACE_GINAC
582 #endif // ndef NO_NAMESPACE_GINAC
585 // helper class function_options
588 function_options::function_options()
593 function_options::function_options(string const & n, string const & tn)
599 function_options::~function_options()
601 // nothing to clean up at the moment
604 void function_options::initialize(void)
606 set_name("unnamed_function","\\\\operatorname{unnamed}");
608 eval_f=evalf_f=derivative_f=series_f=0;
609 evalf_params_first=true;
610 use_return_type=false;
612 functions_with_same_name=1;
615 function_options & function_options::set_name(string const & n,
620 TeX_name="\\\\operatorname{"+name+"}";
627 // the following lines have been generated for max. ${maxargs} parameters
628 $eval_func_implementation
629 $evalf_func_implementation
630 $derivative_func_implementation
631 $series_func_implementation
632 // end of generated lines
634 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
636 use_return_type=true;
638 return_type_tinfo=rtt;
642 function_options & function_options::do_not_evalf_params(void)
644 evalf_params_first=false;
648 function_options & function_options::remember(unsigned size,
654 remember_assoc_size=assoc_size;
655 remember_strategy=strategy;
659 function_options & function_options::overloaded(unsigned o)
661 functions_with_same_name=o;
665 void function_options::test_and_set_nparams(unsigned n)
669 } else if (nparams!=n) {
670 // we do not throw an exception here because this code is
671 // usually executed before main(), so the exception could not
673 cerr << "WARNING: number of parameters ("
674 << n << ") differs from number set before ("
675 << nparams << ")" << endl;
679 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
682 // default constructor, destructor, copy constructor assignment operator and helpers
687 function::function() : serial(0)
689 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
690 tinfo_key = TINFO_function;
693 function::~function()
695 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
699 function::function(const function & other)
701 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
705 const function & function::operator=(const function & other)
707 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
708 if (this != &other) {
717 void function::copy(const function & other)
719 exprseq::copy(other);
723 void function::destroy(bool call_parent)
725 if (call_parent) exprseq::destroy(call_parent);
729 // other constructors
734 function::function(unsigned ser) : serial(ser)
736 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
737 tinfo_key = TINFO_function;
740 // the following lines have been generated for max. ${maxargs} parameters
741 $constructors_implementation
742 // end of generated lines
744 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
746 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
747 tinfo_key = TINFO_function;
750 function::function(unsigned ser, const exvector & v, bool discardable)
751 : exprseq(v,discardable), serial(ser)
753 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
754 tinfo_key = TINFO_function;
757 function::function(unsigned ser, exvector * vp)
758 : exprseq(vp), serial(ser)
760 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
761 tinfo_key = TINFO_function;
768 /** Construct object from archive_node. */
769 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
771 debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
773 // Find serial number by function name
775 if (n.find_string("name", s)) {
776 unsigned int ser = 0;
777 vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
785 throw (std::runtime_error("unknown function '" + s + "' in archive"));
787 throw (std::runtime_error("unnamed function in archive"));
790 /** Unarchive the object. */
791 ex function::unarchive(const archive_node &n, const lst &sym_lst)
793 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
796 /** Archive the object. */
797 void function::archive(archive_node &n) const
799 inherited::archive(n);
800 GINAC_ASSERT(serial < registered_functions().size());
801 n.add_string("name", registered_functions()[serial].name);
805 // functions overriding virtual functions from bases classes
810 basic * function::duplicate() const
812 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
813 return new function(*this);
816 void function::printraw(ostream & os) const
818 debugmsg("function printraw",LOGLEVEL_PRINT);
820 GINAC_ASSERT(serial<registered_functions().size());
822 os << "function(name=" << registered_functions()[serial].name;
823 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
830 void function::print(ostream & os, unsigned upper_precedence) const
832 debugmsg("function print",LOGLEVEL_PRINT);
834 GINAC_ASSERT(serial<registered_functions().size());
836 os << registered_functions()[serial].name;
837 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
840 void function::printtree(ostream & os, unsigned indent) const
842 debugmsg("function printtree",LOGLEVEL_PRINT);
844 GINAC_ASSERT(serial<registered_functions().size());
846 os << string(indent,' ') << "function "
847 << registered_functions()[serial].name
848 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
849 << ", flags=" << flags
850 << ", nops=" << nops() << endl;
851 for (unsigned i=0; i<nops(); ++i) {
852 seq[i].printtree(os,indent+delta_indent);
854 os << string(indent+delta_indent,' ') << "=====" << endl;
857 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
859 debugmsg("function print csrc",LOGLEVEL_PRINT);
861 GINAC_ASSERT(serial<registered_functions().size());
863 // Print function name in lowercase
865 lname=registered_functions()[serial].name;
866 for (unsigned i=0; i<lname.size(); i++)
867 lname[i] = tolower(lname[i]);
870 // Print arguments, separated by commas
871 exvector::const_iterator it = seq.begin();
872 exvector::const_iterator itend = seq.end();
873 while (it != itend) {
874 it->bp->printcsrc(os, type, 0);
882 ex function::expand(unsigned options) const
884 return this->setflag(status_flags::expanded);
887 ex function::eval(int level) const
889 GINAC_ASSERT(serial<registered_functions().size());
892 // first evaluate children, then we will end up here again
893 return function(serial,evalchildren(level));
896 if (registered_functions()[serial].eval_f==0) {
900 bool use_remember=registered_functions()[serial].use_remember;
902 if (use_remember && lookup_remember_table(eval_result)) {
906 switch (registered_functions()[serial].nparams) {
907 // the following lines have been generated for max. ${maxargs} parameters
908 ${eval_switch_statement}
909 // end of generated lines
911 throw(std::logic_error("function::eval(): invalid nparams"));
914 store_remember_table(eval_result);
919 ex function::evalf(int level) const
921 GINAC_ASSERT(serial<registered_functions().size());
923 exvector eseq=evalfchildren(level);
925 if (registered_functions()[serial].evalf_f==0) {
926 return function(serial,eseq).hold();
928 switch (registered_functions()[serial].nparams) {
929 // the following lines have been generated for max. ${maxargs} parameters
930 ${evalf_switch_statement}
931 // end of generated lines
933 throw(std::logic_error("function::evalf(): invalid nparams"));
936 ex function::thisexprseq(const exvector & v) const
938 return function(serial,v);
941 ex function::thisexprseq(exvector * vp) const
943 return function(serial,vp);
946 /** Implementation of ex::series for functions.
947 * \@see ex::series */
948 ex function::series(const relational & r, int order) const
950 GINAC_ASSERT(serial<registered_functions().size());
952 if (registered_functions()[serial].series_f==0) {
953 return basic::series(r, order);
956 switch (registered_functions()[serial].nparams) {
957 // the following lines have been generated for max. ${maxargs} parameters
958 ${series_switch_statement}
959 // end of generated lines
961 throw(std::logic_error("function::series(): invalid nparams"));
967 /** Implementation of ex::diff() for functions. It applies the chain rule,
968 * except for the Order term function.
970 ex function::derivative(const symbol & s) const
974 if (serial == function_index_Order) {
975 // Order Term function only differentiates the argument
976 return Order(seq[0].diff(s));
977 } else if (serial == function_index_Derivative) {
978 // Inert derivative performs chain rule on the first argument only, and
979 // adds differentiation parameter to list (second argument)
980 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
981 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
984 for (unsigned i=0; i!=fcn.nops(); i++) {
985 arg_diff = fcn.op(i).diff(s);
986 if (!arg_diff.is_zero()) {
987 lst new_lst = ex_to_lst(seq[1]);
989 result += arg_diff * Derivative(fcn, new_lst);
995 for (unsigned i=0; i!=seq.size(); i++) {
996 arg_diff = seq[i].diff(s);
997 // We apply the chain rule only when it makes sense. This is not
998 // just for performance reasons but also to allow functions to
999 // throw when differentiated with respect to one of its arguments
1000 // without running into trouble with our automatic full
1002 if (!arg_diff.is_zero())
1003 result += pderivative(i)*arg_diff;
1009 int function::compare_same_type(const basic & other) const
1011 GINAC_ASSERT(is_of_type(other, function));
1012 const function & o=static_cast<function &>(const_cast<basic &>(other));
1014 if (serial!=o.serial) {
1015 return serial < o.serial ? -1 : 1;
1017 return exprseq::compare_same_type(o);
1020 bool function::is_equal_same_type(const basic & other) const
1022 GINAC_ASSERT(is_of_type(other, function));
1023 const function & o=static_cast<function &>(const_cast<basic &>(other));
1025 if (serial!=o.serial) return false;
1026 return exprseq::is_equal_same_type(o);
1029 unsigned function::return_type(void) const
1031 if (seq.size()==0) {
1032 return return_types::commutative;
1034 return (*seq.begin()).return_type();
1037 unsigned function::return_type_tinfo(void) const
1039 if (seq.size()==0) {
1042 return (*seq.begin()).return_type_tinfo();
1046 // new virtual functions which can be overridden by derived classes
1052 // non-virtual functions in this class
1057 ex function::pderivative(unsigned diff_param) const // partial differentiation
1059 GINAC_ASSERT(serial<registered_functions().size());
1061 if (registered_functions()[serial].derivative_f==0) {
1062 return Derivative(*this, lst(diff_param));
1064 switch (registered_functions()[serial].nparams) {
1065 // the following lines have been generated for max. ${maxargs} parameters
1066 ${diff_switch_statement}
1067 // end of generated lines
1069 throw(std::logic_error("function::pderivative(): no diff function defined"));
1072 vector<function_options> & function::registered_functions(void)
1074 static vector<function_options> * rf=new vector<function_options>;
1078 bool function::lookup_remember_table(ex & result) const
1080 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
1083 void function::store_remember_table(ex const & result) const
1085 remember_table::remember_tables()[serial].add_entry(*this,result);
1090 unsigned function::register_new(function_options const & opt)
1092 unsigned same_name=0;
1093 for (unsigned i=0; i<registered_functions().size(); ++i) {
1094 if (registered_functions()[i].name==opt.name) {
1098 if (same_name>=opt.functions_with_same_name) {
1099 // we do not throw an exception here because this code is
1100 // usually executed before main(), so the exception could not
1102 cerr << "WARNING: function name " << opt.name
1103 << " already in use!" << endl;
1105 registered_functions().push_back(opt);
1106 if (opt.use_remember) {
1107 remember_table::remember_tables().
1108 push_back(remember_table(opt.remember_size,
1109 opt.remember_assoc_size,
1110 opt.remember_strategy));
1112 remember_table::remember_tables().push_back(remember_table());
1114 return registered_functions().size()-1;
1118 // static member variables
1127 const function some_function;
1128 const type_info & typeid_function=typeid(some_function);
1130 #ifndef NO_NAMESPACE_GINAC
1131 } // namespace GiNaC
1132 #endif // ndef NO_NAMESPACE_GINAC
1134 END_OF_IMPLEMENTATION
1136 print "Creating interface file function.h...";
1137 open OUT,">function.h" or die "cannot open function.h";
1138 print OUT $interface;
1142 print "Creating implementation file function.cpp...";
1143 open OUT,">function.cpp" or die "cannot open function.cpp";
1144 print OUT $implementation;