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 #define DECLARE_FUNCTION_1P(NAME) \
39 extern const unsigned function_index_##NAME; \
40 inline GiNaC::function NAME(const GiNaC::ex & p1) { \
41 return GiNaC::function(function_index_##NAME, p1); \
43 #define DECLARE_FUNCTION_2P(NAME) \
44 extern const unsigned function_index_##NAME; \
45 inline GiNaC::function NAME(const GiNaC::ex & p1, const GiNaC::ex & p2) { \
46 return GiNaC::function(function_index_##NAME, p1, p2); \
49 END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NAMESPACE
51 $declare_function_macro_namespace .= generate_from_to(
52 <<'END_OF_DECLARE_FUNCTION_MACRO_NAMESPACE','const GiNaC::ex & p${N}','p${N}',3,$maxargs);
53 #define DECLARE_FUNCTION_${N}P(NAME) \\
54 extern const unsigned function_index_##NAME; \\
55 inline GiNaC::function NAME(${SEQ1}) { \\
56 return GiNaC::function(function_index_##NAME, ${SEQ2}); \\
59 END_OF_DECLARE_FUNCTION_MACRO_NAMESPACE
61 $declare_function_macro_no_namespace = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NO_NAMESPACE';
62 #define DECLARE_FUNCTION_1P(NAME) \
63 extern const unsigned function_index_##NAME; \
64 inline function NAME(const ex & p1) { \
65 return function(function_index_##NAME, p1); \
67 #define DECLARE_FUNCTION_2P(NAME) \
68 extern const unsigned function_index_##NAME; \
69 inline function NAME(const ex & p1, const ex & p2) { \
70 return function(function_index_##NAME, p1, p2); \
73 END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NO_NAMESPACE
75 $declare_function_macro_no_namespace .= generate_from_to(
76 <<'END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE','const ex & p${N}','p${N}',3,$maxargs);
77 #define DECLARE_FUNCTION_${N}P(NAME) \\
78 extern const unsigned function_index_##NAME; \\
79 inline function NAME(${SEQ1}) { \\
80 return function(function_index_##NAME, ${SEQ2}); \\
83 END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE
85 $typedef_eval_funcp=generate(
86 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
89 $typedef_evalf_funcp=generate(
90 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
93 $typedef_derivative_funcp=generate(
94 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
97 $typedef_series_funcp=generate(
98 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int);'."\n",
101 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','');
103 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','');
105 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','');
107 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','');
109 $constructors_interface=generate(
110 ' function(unsigned ser, ${SEQ1});'."\n",
111 'const ex & param${N}','');
113 $constructors_implementation=generate(
114 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}');
115 function::function(unsigned ser, ${SEQ1})
116 : exprseq(${SEQ2}), serial(ser)
118 debugmsg(\"function constructor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
119 tinfo_key = TINFO_function;
121 END_OF_CONSTRUCTORS_IMPLEMENTATION
123 $eval_switch_statement=generate(
124 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
126 eval_result=((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
128 END_OF_EVAL_SWITCH_STATEMENT
130 $evalf_switch_statement=generate(
131 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
133 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
135 END_OF_EVALF_SWITCH_STATEMENT
137 $diff_switch_statement=generate(
138 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
140 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
142 END_OF_DIFF_SWITCH_STATEMENT
144 $series_switch_statement=generate(
145 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
148 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order);
149 } catch (do_taylor) {
150 res = basic::series(r, order);
154 END_OF_SERIES_SWITCH_STATEMENT
156 $eval_func_implementation=generate(
157 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','');
158 function_options & function_options::eval_func(eval_funcp_${N} e)
160 test_and_set_nparams(${N});
161 eval_f=eval_funcp(e);
164 END_OF_EVAL_FUNC_IMPLEMENTATION
166 $evalf_func_implementation=generate(
167 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','');
168 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
170 test_and_set_nparams(${N});
171 evalf_f=evalf_funcp(ef);
174 END_OF_EVALF_FUNC_IMPLEMENTATION
176 $derivative_func_implementation=generate(
177 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','');
178 function_options & function_options::derivative_func(derivative_funcp_${N} d)
180 test_and_set_nparams(${N});
181 derivative_f=derivative_funcp(d);
184 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
186 $series_func_implementation=generate(
187 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','');
188 function_options & function_options::series_func(series_funcp_${N} s)
190 test_and_set_nparams(${N});
191 series_f=series_funcp(s);
194 END_OF_SERIES_FUNC_IMPLEMENTATION
196 $interface=<<END_OF_INTERFACE;
197 /** \@file function.h
199 * Interface to abstract class function (new function concept). */
202 * This file was generated automatically by function.pl.
203 * Please do not modify it directly, edit the perl script instead!
204 * function.pl options: \$maxargs=${maxargs}
206 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
208 * This program is free software; you can redistribute it and/or modify
209 * it under the terms of the GNU General Public License as published by
210 * the Free Software Foundation; either version 2 of the License, or
211 * (at your option) any later version.
213 * This program is distributed in the hope that it will be useful,
214 * but WITHOUT ANY WARRANTY; without even the implied warranty of
215 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
216 * GNU General Public License for more details.
218 * You should have received a copy of the GNU General Public License
219 * along with this program; if not, write to the Free Software
220 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
223 #ifndef __GINAC_FUNCTION_H__
224 #define __GINAC_FUNCTION_H__
230 // CINT needs <algorithm> to work properly with <vector>
232 #endif // def __CINT__
236 #ifndef NO_NAMESPACE_GINAC
238 // the following lines have been generated for max. ${maxargs} parameters
239 $declare_function_macro_namespace
240 // end of generated lines
242 #else // ndef NO_NAMESPACE_GINAC
244 // the following lines have been generated for max. ${maxargs} parameters
245 $declare_function_macro_no_namespace
246 // end of generated lines
248 #endif // ndef NO_NAMESPACE_GINAC
250 #ifndef NO_NAMESPACE_GINAC
252 #define REGISTER_FUNCTION(NAME,OPT) \\
253 const unsigned function_index_##NAME= \\
254 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
256 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
257 const unsigned function_index_##NAME= \\
258 GiNaC::function::register_new(GiNaC::function_options(#NAME). \\
261 derivative_func(D). \\
264 #else // ndef NO_NAMESPACE_GINAC
266 #define REGISTER_FUNCTION(NAME,OPT) \\
267 const unsigned function_index_##NAME= \\
268 function::register_new(function_options(#NAME).OPT);
270 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
271 const unsigned function_index_##NAME= \\
272 function::register_new(function_options(#NAME). \\
275 derivative_func(D). \\
278 #endif // ndef NO_NAMESPACE_GINAC
280 #define BEGIN_TYPECHECK \\
281 bool automatic_typecheck=true;
283 #define TYPECHECK(VAR,TYPE) \\
284 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
285 automatic_typecheck=false; \\
288 #ifndef NO_NAMESPACE_GINAC
290 #define TYPECHECK_INTEGER(VAR) \\
291 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
292 automatic_typecheck=false; \\
295 #else // ndef NO_NAMESPACE_GINAC
297 #define TYPECHECK_INTEGER(VAR) \\
298 if (!(VAR).info(info_flags::integer)) { \\
299 automatic_typecheck=false; \\
302 #endif // ndef NO_NAMESPACE_GINAC
304 #define END_TYPECHECK(RV) \\
306 if (!automatic_typecheck) { \\
310 #ifndef NO_NAMESPACE_GINAC
312 #endif // ndef NO_NAMESPACE_GINAC
316 typedef ex (* eval_funcp)();
317 typedef ex (* evalf_funcp)();
318 typedef ex (* derivative_funcp)();
319 typedef ex (* series_funcp)();
321 // the following lines have been generated for max. ${maxargs} parameters
324 $typedef_derivative_funcp
325 $typedef_series_funcp
326 // end of generated lines
328 class function_options
330 friend class function;
333 function_options(string const & n, string const & tn=string());
335 void initialize(void);
336 function_options & set_name(string const & n, string const & tn=string());
337 // the following lines have been generated for max. ${maxargs} parameters
339 $evalf_func_interface
340 $derivative_func_interface
341 $series_func_interface
342 // end of generated lines
343 function_options & set_return_type(unsigned rt, unsigned rtt=0);
344 function_options & do_not_evalf_params(void);
345 function_options & remember(unsigned size, unsigned assoc_size=0,
346 unsigned strategy=remember_strategies::delete_never);
347 function_options & overloaded(unsigned o);
348 void test_and_set_nparams(unsigned n);
349 string get_name(void) const { return name; }
350 unsigned get_nparams(void) const { return nparams; }
360 derivative_funcp derivative_f;
361 series_funcp series_f;
363 bool evalf_params_first;
365 bool use_return_type;
366 unsigned return_type;
367 unsigned return_type_tinfo;
370 unsigned remember_size;
371 unsigned remember_assoc_size;
372 unsigned remember_strategy;
374 unsigned functions_with_same_name;
377 /** The class function is used to implement builtin functions like sin, cos...
378 and user defined functions */
379 class function : public exprseq
381 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
383 // CINT has a linking problem
385 friend void ginsh_get_ginac_functions(void);
386 #endif // def __MAKECINT__
388 friend class remember_table_entry;
389 // friend class remember_table_list;
390 // friend class remember_table;
394 // default constructor, destructor, copy constructor assignment operator and helpers
398 function(const function & other);
399 const function & operator=(const function & other);
401 void copy(const function & other);
402 void destroy(bool call_parent);
404 // other constructors
406 function(unsigned ser);
407 // the following lines have been generated for max. ${maxargs} parameters
408 $constructors_interface
409 // end of generated lines
410 function(unsigned ser, const exprseq & es);
411 function(unsigned ser, const exvector & v, bool discardable=0);
412 function(unsigned ser, exvector * vp); // vp will be deleted
414 // functions overriding virtual functions from bases classes
416 basic * duplicate() const;
417 void printraw(ostream & os) const;
418 void print(ostream & os, unsigned upper_precedence=0) const;
419 void printtree(ostream & os, unsigned indent) const;
420 void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
421 ex expand(unsigned options=0) const;
422 ex eval(int level=0) const;
423 ex evalf(int level=0) const;
424 ex series(const relational & r, int order) const;
425 ex thisexprseq(const exvector & v) const;
426 ex thisexprseq(exvector * vp) const;
428 ex derivative(const symbol & s) const;
429 int compare_same_type(const basic & other) const;
430 bool is_equal_same_type(const basic & other) const;
431 unsigned return_type(void) const;
432 unsigned return_type_tinfo(void) const;
434 // new virtual functions which can be overridden by derived classes
437 // non-virtual functions in this class
439 ex pderivative(unsigned diff_param) const; // partial differentiation
440 static vector<function_options> & registered_functions(void);
441 bool lookup_remember_table(ex & result) const;
442 void store_remember_table(ex const & result) const;
444 static unsigned register_new(function_options const & opt);
445 static unsigned find_function(const string &name, unsigned nparams);
446 unsigned getserial(void) const {return serial;}
454 // utility functions/macros
455 inline const function &ex_to_function(const ex &e)
457 return static_cast<const function &>(*e.bp);
460 #ifndef NO_NAMESPACE_GINAC
462 #define is_ex_the_function(OBJ, FUNCNAME) \\
463 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
465 #else // ndef NO_NAMESPACE_GINAC
467 #define is_ex_the_function(OBJ, FUNCNAME) \\
468 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
470 #endif // ndef NO_NAMESPACE_GINAC
474 extern const function some_function;
475 extern const type_info & typeid_function;
477 #ifndef NO_NAMESPACE_GINAC
479 #endif // ndef NO_NAMESPACE_GINAC
481 #endif // ndef __GINAC_FUNCTION_H__
485 $implementation=<<END_OF_IMPLEMENTATION;
486 /** \@file function.cpp
488 * Implementation of class function. */
491 * This file was generated automatically by function.pl.
492 * Please do not modify it directly, edit the perl script instead!
493 * function.pl options: \$maxargs=${maxargs}
495 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
497 * This program is free software; you can redistribute it and/or modify
498 * it under the terms of the GNU General Public License as published by
499 * the Free Software Foundation; either version 2 of the License, or
500 * (at your option) any later version.
502 * This program is distributed in the hope that it will be useful,
503 * but WITHOUT ANY WARRANTY; without even the implied warranty of
504 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
505 * GNU General Public License for more details.
507 * You should have received a copy of the GNU General Public License
508 * along with this program; if not, write to the Free Software
509 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
516 #include "function.h"
522 #include "debugmsg.h"
523 #include "remember.h"
525 #ifndef NO_NAMESPACE_GINAC
527 #endif // ndef NO_NAMESPACE_GINAC
530 // helper class function_options
533 function_options::function_options()
538 function_options::function_options(string const & n, string const & tn)
544 function_options::~function_options()
546 // nothing to clean up at the moment
549 void function_options::initialize(void)
551 set_name("unnamed_function","\\\\operatorname{unnamed}");
553 eval_f=evalf_f=derivative_f=series_f=0;
554 evalf_params_first=true;
555 use_return_type=false;
557 functions_with_same_name=1;
560 function_options & function_options::set_name(string const & n,
565 TeX_name="\\\\operatorname{"+name+"}";
572 // the following lines have been generated for max. ${maxargs} parameters
573 $eval_func_implementation
574 $evalf_func_implementation
575 $derivative_func_implementation
576 $series_func_implementation
577 // end of generated lines
579 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
581 use_return_type=true;
583 return_type_tinfo=rtt;
587 function_options & function_options::do_not_evalf_params(void)
589 evalf_params_first=false;
593 function_options & function_options::remember(unsigned size,
599 remember_assoc_size=assoc_size;
600 remember_strategy=strategy;
604 function_options & function_options::overloaded(unsigned o)
606 functions_with_same_name=o;
610 void function_options::test_and_set_nparams(unsigned n)
614 } else if (nparams!=n) {
615 // we do not throw an exception here because this code is
616 // usually executed before main(), so the exception could not
618 cerr << "WARNING: number of parameters ("
619 << n << ") differs from number set before ("
620 << nparams << ")" << endl;
624 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
627 // default constructor, destructor, copy constructor assignment operator and helpers
632 function::function() : serial(0)
634 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
635 tinfo_key = TINFO_function;
638 function::~function()
640 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
644 function::function(const function & other)
646 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
650 const function & function::operator=(const function & other)
652 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
653 if (this != &other) {
662 void function::copy(const function & other)
664 exprseq::copy(other);
668 void function::destroy(bool call_parent)
670 if (call_parent) exprseq::destroy(call_parent);
674 // other constructors
679 function::function(unsigned ser) : serial(ser)
681 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
682 tinfo_key = TINFO_function;
685 // the following lines have been generated for max. ${maxargs} parameters
686 $constructors_implementation
687 // end of generated lines
689 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
691 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
692 tinfo_key = TINFO_function;
695 function::function(unsigned ser, const exvector & v, bool discardable)
696 : exprseq(v,discardable), serial(ser)
698 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
699 tinfo_key = TINFO_function;
702 function::function(unsigned ser, exvector * vp)
703 : exprseq(vp), serial(ser)
705 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
706 tinfo_key = TINFO_function;
713 /** Construct object from archive_node. */
714 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
716 debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
718 // Find serial number by function name
720 if (n.find_string("name", s)) {
721 unsigned int ser = 0;
722 vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
730 throw (std::runtime_error("unknown function '" + s + "' in archive"));
732 throw (std::runtime_error("unnamed function in archive"));
735 /** Unarchive the object. */
736 ex function::unarchive(const archive_node &n, const lst &sym_lst)
738 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
741 /** Archive the object. */
742 void function::archive(archive_node &n) const
744 inherited::archive(n);
745 GINAC_ASSERT(serial < registered_functions().size());
746 n.add_string("name", registered_functions()[serial].name);
750 // functions overriding virtual functions from bases classes
755 basic * function::duplicate() const
757 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
758 return new function(*this);
761 void function::printraw(ostream & os) const
763 debugmsg("function printraw",LOGLEVEL_PRINT);
765 GINAC_ASSERT(serial<registered_functions().size());
767 os << "function(name=" << registered_functions()[serial].name;
768 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
775 void function::print(ostream & os, unsigned upper_precedence) const
777 debugmsg("function print",LOGLEVEL_PRINT);
779 GINAC_ASSERT(serial<registered_functions().size());
781 os << registered_functions()[serial].name;
782 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
785 void function::printtree(ostream & os, unsigned indent) const
787 debugmsg("function printtree",LOGLEVEL_PRINT);
789 GINAC_ASSERT(serial<registered_functions().size());
791 os << string(indent,' ') << "function "
792 << registered_functions()[serial].name
793 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
794 << ", flags=" << flags
795 << ", nops=" << nops() << endl;
796 for (unsigned i=0; i<nops(); ++i) {
797 seq[i].printtree(os,indent+delta_indent);
799 os << string(indent+delta_indent,' ') << "=====" << endl;
802 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
804 debugmsg("function print csrc",LOGLEVEL_PRINT);
806 GINAC_ASSERT(serial<registered_functions().size());
808 // Print function name in lowercase
810 lname=registered_functions()[serial].name;
811 for (unsigned i=0; i<lname.size(); i++)
812 lname[i] = tolower(lname[i]);
815 // Print arguments, separated by commas
816 exvector::const_iterator it = seq.begin();
817 exvector::const_iterator itend = seq.end();
818 while (it != itend) {
819 it->bp->printcsrc(os, type, 0);
827 ex function::expand(unsigned options) const
829 return this->setflag(status_flags::expanded);
832 ex function::eval(int level) const
834 GINAC_ASSERT(serial<registered_functions().size());
837 // first evaluate children, then we will end up here again
838 return function(serial,evalchildren(level));
841 if (registered_functions()[serial].eval_f==0) {
845 bool use_remember=registered_functions()[serial].use_remember;
847 if (use_remember && lookup_remember_table(eval_result)) {
851 switch (registered_functions()[serial].nparams) {
852 // the following lines have been generated for max. ${maxargs} parameters
853 ${eval_switch_statement}
854 // end of generated lines
856 throw(std::logic_error("function::eval(): invalid nparams"));
859 store_remember_table(eval_result);
864 ex function::evalf(int level) const
866 GINAC_ASSERT(serial<registered_functions().size());
868 exvector eseq=evalfchildren(level);
870 if (registered_functions()[serial].evalf_f==0) {
871 return function(serial,eseq).hold();
873 switch (registered_functions()[serial].nparams) {
874 // the following lines have been generated for max. ${maxargs} parameters
875 ${evalf_switch_statement}
876 // end of generated lines
878 throw(std::logic_error("function::evalf(): invalid nparams"));
881 ex function::thisexprseq(const exvector & v) const
883 return function(serial,v);
886 ex function::thisexprseq(exvector * vp) const
888 return function(serial,vp);
891 /** Implementation of ex::series for functions.
892 * \@see ex::series */
893 ex function::series(const relational & r, int order) const
895 GINAC_ASSERT(serial<registered_functions().size());
897 if (registered_functions()[serial].series_f==0) {
898 return basic::series(r, order);
901 switch (registered_functions()[serial].nparams) {
902 // the following lines have been generated for max. ${maxargs} parameters
903 ${series_switch_statement}
904 // end of generated lines
906 throw(std::logic_error("function::series(): invalid nparams"));
912 /** Implementation of ex::diff() for functions. It applies the chain rule,
913 * except for the Order term function.
915 ex function::derivative(const symbol & s) const
919 if (serial == function_index_Order) {
920 // Order Term function only differentiates the argument
921 return Order(seq[0].diff(s));
922 } else if (serial == function_index_Derivative) {
923 // Inert derivative performs chain rule on the first argument only, and
924 // adds differentiation parameter to list (second argument)
925 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
926 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
929 for (unsigned i=0; i!=fcn.nops(); i++) {
930 arg_diff = fcn.op(i).diff(s);
931 if (!arg_diff.is_zero()) {
932 lst new_lst = ex_to_lst(seq[1]);
934 result += arg_diff * Derivative(fcn, new_lst);
940 for (unsigned i=0; i!=seq.size(); i++) {
941 arg_diff = seq[i].diff(s);
942 // We apply the chain rule only when it makes sense. This is not
943 // just for performance reasons but also to allow functions to
944 // throw when differentiated with respect to one of its arguments
945 // without running into trouble with our automatic full
947 if (!arg_diff.is_zero())
948 result += pderivative(i)*arg_diff;
954 int function::compare_same_type(const basic & other) const
956 GINAC_ASSERT(is_of_type(other, function));
957 const function & o=static_cast<function &>(const_cast<basic &>(other));
959 if (serial!=o.serial) {
960 return serial < o.serial ? -1 : 1;
962 return exprseq::compare_same_type(o);
965 bool function::is_equal_same_type(const basic & other) const
967 GINAC_ASSERT(is_of_type(other, function));
968 const function & o=static_cast<function &>(const_cast<basic &>(other));
970 if (serial!=o.serial) return false;
971 return exprseq::is_equal_same_type(o);
974 unsigned function::return_type(void) const
977 return return_types::commutative;
979 return (*seq.begin()).return_type();
982 unsigned function::return_type_tinfo(void) const
987 return (*seq.begin()).return_type_tinfo();
991 // new virtual functions which can be overridden by derived classes
997 // non-virtual functions in this class
1002 ex function::pderivative(unsigned diff_param) const // partial differentiation
1004 GINAC_ASSERT(serial<registered_functions().size());
1006 if (registered_functions()[serial].derivative_f==0) {
1007 return Derivative(*this, lst(diff_param));
1009 switch (registered_functions()[serial].nparams) {
1010 // the following lines have been generated for max. ${maxargs} parameters
1011 ${diff_switch_statement}
1012 // end of generated lines
1014 throw(std::logic_error("function::pderivative(): no diff function defined"));
1017 vector<function_options> & function::registered_functions(void)
1019 static vector<function_options> * rf=new vector<function_options>;
1023 bool function::lookup_remember_table(ex & result) const
1025 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
1028 void function::store_remember_table(ex const & result) const
1030 remember_table::remember_tables()[serial].add_entry(*this,result);
1035 unsigned function::register_new(function_options const & opt)
1037 unsigned same_name=0;
1038 for (unsigned i=0; i<registered_functions().size(); ++i) {
1039 if (registered_functions()[i].name==opt.name) {
1043 if (same_name>=opt.functions_with_same_name) {
1044 // we do not throw an exception here because this code is
1045 // usually executed before main(), so the exception could not
1047 cerr << "WARNING: function name " << opt.name
1048 << " already in use!" << endl;
1050 registered_functions().push_back(opt);
1051 if (opt.use_remember) {
1052 remember_table::remember_tables().
1053 push_back(remember_table(opt.remember_size,
1054 opt.remember_assoc_size,
1055 opt.remember_strategy));
1057 remember_table::remember_tables().push_back(remember_table());
1059 return registered_functions().size()-1;
1062 /** Find serial number of function by name and number of parameters.
1063 * Throws exception if function was not found. */
1064 unsigned function::find_function(const string &name, unsigned nparams)
1066 vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1067 unsigned serial = 0;
1069 if (i->get_name() == name && i->get_nparams() == nparams)
1074 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1078 // static member variables
1087 const function some_function;
1088 const type_info & typeid_function=typeid(some_function);
1090 #ifndef NO_NAMESPACE_GINAC
1091 } // namespace GiNaC
1092 #endif // ndef NO_NAMESPACE_GINAC
1094 END_OF_IMPLEMENTATION
1096 print "Creating interface file function.h...";
1097 open OUT,">function.h" or die "cannot open function.h";
1098 print OUT $interface;
1102 print "Creating implementation file function.cpp...";
1103 open OUT,">function.cpp" or die "cannot open function.cpp";
1104 print OUT $implementation;