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 = generate_from_to(
38 <<'END_OF_DECLARE_FUNCTION_MACRO','const GiNaC::ex & p${N}','p${N}',1,$maxargs);
39 #define DECLARE_FUNCTION_${N}P(NAME) \\
40 extern const unsigned function_index_##NAME; \\
41 inline const GiNaC::function NAME(${SEQ1}) { \\
42 return GiNaC::function(function_index_##NAME, ${SEQ2}); \\
45 END_OF_DECLARE_FUNCTION_MACRO
47 $typedef_eval_funcp=generate(
48 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
51 $typedef_evalf_funcp=generate(
52 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
55 $typedef_derivative_funcp=generate(
56 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
59 $typedef_series_funcp=generate(
60 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
63 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','');
65 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','');
67 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','');
69 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','');
71 $constructors_interface=generate(
72 ' function(unsigned ser, ${SEQ1});'."\n",
73 'const ex & param${N}','');
75 $constructors_implementation=generate(
76 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}');
77 function::function(unsigned ser, ${SEQ1})
78 : exprseq(${SEQ2}), serial(ser)
80 debugmsg(\"function ctor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
81 tinfo_key = TINFO_function;
83 END_OF_CONSTRUCTORS_IMPLEMENTATION
85 $eval_switch_statement=generate(
86 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
88 eval_result = ((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
90 END_OF_EVAL_SWITCH_STATEMENT
92 $evalf_switch_statement=generate(
93 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
95 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
96 END_OF_EVALF_SWITCH_STATEMENT
98 $diff_switch_statement=generate(
99 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
101 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
102 END_OF_DIFF_SWITCH_STATEMENT
104 $series_switch_statement=generate(
105 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
108 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
109 } catch (do_taylor) {
110 res = basic::series(r, order, options);
113 END_OF_SERIES_SWITCH_STATEMENT
115 $eval_func_implementation=generate(
116 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','');
117 function_options & function_options::eval_func(eval_funcp_${N} e)
119 test_and_set_nparams(${N});
120 eval_f = eval_funcp(e);
123 END_OF_EVAL_FUNC_IMPLEMENTATION
125 $evalf_func_implementation=generate(
126 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','');
127 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
129 test_and_set_nparams(${N});
130 evalf_f = evalf_funcp(ef);
133 END_OF_EVALF_FUNC_IMPLEMENTATION
135 $derivative_func_implementation=generate(
136 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','');
137 function_options & function_options::derivative_func(derivative_funcp_${N} d)
139 test_and_set_nparams(${N});
140 derivative_f = derivative_funcp(d);
143 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
145 $series_func_implementation=generate(
146 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','');
147 function_options & function_options::series_func(series_funcp_${N} s)
149 test_and_set_nparams(${N});
150 series_f = series_funcp(s);
153 END_OF_SERIES_FUNC_IMPLEMENTATION
155 $interface=<<END_OF_INTERFACE;
156 /** \@file function.h
158 * Interface to abstract class function (new function concept). */
161 * This file was generated automatically by function.pl.
162 * Please do not modify it directly, edit the perl script instead!
163 * function.pl options: \$maxargs=${maxargs}
165 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
167 * This program is free software; you can redistribute it and/or modify
168 * it under the terms of the GNU General Public License as published by
169 * the Free Software Foundation; either version 2 of the License, or
170 * (at your option) any later version.
172 * This program is distributed in the hope that it will be useful,
173 * but WITHOUT ANY WARRANTY; without even the implied warranty of
174 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175 * GNU General Public License for more details.
177 * You should have received a copy of the GNU General Public License
178 * along with this program; if not, write to the Free Software
179 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
182 #ifndef __GINAC_FUNCTION_H__
183 #define __GINAC_FUNCTION_H__
188 // CINT needs <algorithm> to work properly with <vector>
193 // the following lines have been generated for max. ${maxargs} parameters
194 $declare_function_macro
195 // end of generated lines
197 #define REGISTER_FUNCTION(NAME,OPT) \\
198 const unsigned function_index_##NAME= \\
199 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
201 #define BEGIN_TYPECHECK \\
202 bool automatic_typecheck=true;
204 #define TYPECHECK(VAR,TYPE) \\
205 if (!is_exactly_a<TYPE>(VAR)) { \\
206 automatic_typecheck=false; \\
209 #define TYPECHECK_INTEGER(VAR) \\
210 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
211 automatic_typecheck=false; \\
214 #define END_TYPECHECK(RV) \\
216 if (!automatic_typecheck) { \\
225 typedef ex (* eval_funcp)();
226 typedef ex (* evalf_funcp)();
227 typedef ex (* derivative_funcp)();
228 typedef ex (* series_funcp)();
230 // the following lines have been generated for max. ${maxargs} parameters
233 $typedef_derivative_funcp
234 $typedef_series_funcp
235 // end of generated lines
237 class function_options
239 friend class function;
242 function_options(std::string const & n, std::string const & tn=std::string());
244 void initialize(void);
245 function_options & set_name(std::string const & n, std::string const & tn=std::string());
246 function_options & latex_name(std::string const & tn);
247 // the following lines have been generated for max. ${maxargs} parameters
249 $evalf_func_interface
250 $derivative_func_interface
251 $series_func_interface
252 // end of generated lines
253 function_options & set_return_type(unsigned rt, unsigned rtt=0);
254 function_options & do_not_evalf_params(void);
255 function_options & remember(unsigned size, unsigned assoc_size=0,
256 unsigned strategy=remember_strategies::delete_never);
257 function_options & overloaded(unsigned o);
258 function_options & set_symmetry(const symmetry & s);
259 void test_and_set_nparams(unsigned n);
260 std::string get_name(void) const { return name; }
261 unsigned get_nparams(void) const { return nparams; }
262 bool has_derivative(void) const { return derivative_f != NULL; }
266 std::string TeX_name;
272 derivative_funcp derivative_f;
273 series_funcp series_f;
275 bool evalf_params_first;
277 bool use_return_type;
278 unsigned return_type;
279 unsigned return_type_tinfo;
282 unsigned remember_size;
283 unsigned remember_assoc_size;
284 unsigned remember_strategy;
286 unsigned functions_with_same_name;
291 /** The class function is used to implement builtin functions like sin, cos...
292 and user defined functions */
293 class function : public exprseq
295 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
297 // CINT has a linking problem
299 friend void ginsh_get_ginac_functions(void);
300 #endif // def __MAKECINT__
302 friend class remember_table_entry;
303 // friend class remember_table_list;
304 // friend class remember_table;
305 friend ex Derivative_eval(const ex &, const ex &);
311 function(unsigned ser);
312 // the following lines have been generated for max. ${maxargs} parameters
313 $constructors_interface
314 // end of generated lines
315 function(unsigned ser, const exprseq & es);
316 function(unsigned ser, const exvector & v, bool discardable = false);
317 function(unsigned ser, exvector * vp); // vp will be deleted
319 // functions overriding virtual functions from bases classes
321 void print(const print_context & c, unsigned level = 0) const;
322 unsigned precedence(void) const {return 70;}
323 int degree(const ex & s) const;
324 int ldegree(const ex & s) const;
325 ex coeff(const ex & s, int n = 1) const;
326 ex expand(unsigned options=0) const;
327 ex eval(int level=0) const;
328 ex evalf(int level=0) const;
329 unsigned calchash(void) const;
330 ex series(const relational & r, int order, unsigned options = 0) const;
331 ex thisexprseq(const exvector & v) const;
332 ex thisexprseq(exvector * vp) const;
334 ex derivative(const symbol & s) const;
335 bool is_equal_same_type(const basic & other) const;
336 bool match_same_type(const basic & other) const;
337 unsigned return_type(void) const;
338 unsigned return_type_tinfo(void) const;
340 // new virtual functions which can be overridden by derived classes
343 // non-virtual functions in this class
345 ex pderivative(unsigned diff_param) const; // partial differentiation
346 static std::vector<function_options> & registered_functions(void);
347 bool lookup_remember_table(ex & result) const;
348 void store_remember_table(ex const & result) const;
350 static unsigned register_new(function_options const & opt);
351 static unsigned find_function(const std::string &name, unsigned nparams);
352 unsigned get_serial(void) const {return serial;}
353 std::string get_name(void) const;
361 // utility functions/macros
362 /** Return the object of type function handled by an ex.
363 * This is unsafe: you need to check the type first. */
364 inline const function &ex_to_function(const ex &e)
366 return static_cast<const function &>(*e.bp);
369 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
370 template<> inline bool is_exactly_a<function>(const basic & obj)
372 return obj.tinfo()==TINFO_function;
375 #define is_ex_the_function(OBJ, FUNCNAME) \\
376 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->get_serial() == function_index_##FUNCNAME)
380 #endif // ndef __GINAC_FUNCTION_H__
384 $implementation=<<END_OF_IMPLEMENTATION;
385 /** \@file function.cpp
387 * Implementation of class function. */
390 * This file was generated automatically by function.pl.
391 * Please do not modify it directly, edit the perl script instead!
392 * function.pl options: \$maxargs=${maxargs}
394 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
396 * This program is free software; you can redistribute it and/or modify
397 * it under the terms of the GNU General Public License as published by
398 * the Free Software Foundation; either version 2 of the License, or
399 * (at your option) any later version.
401 * This program is distributed in the hope that it will be useful,
402 * but WITHOUT ANY WARRANTY; without even the implied warranty of
403 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
404 * GNU General Public License for more details.
406 * You should have received a copy of the GNU General Public License
407 * along with this program; if not, write to the Free Software
408 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
415 #include "function.h"
418 #include "symmetry.h"
423 #include "debugmsg.h"
424 #include "remember.h"
429 // helper class function_options
432 function_options::function_options()
437 function_options::function_options(std::string const & n, std::string const & tn)
443 function_options::~function_options()
445 // nothing to clean up at the moment
448 void function_options::initialize(void)
450 set_name("unnamed_function","\\\\mbox{unnamed}");
452 eval_f = evalf_f = derivative_f = series_f = 0;
453 evalf_params_first = true;
454 use_return_type = false;
455 use_remember = false;
456 functions_with_same_name = 1;
460 function_options & function_options::set_name(std::string const & n,
461 std::string const & tn)
464 if (tn==std::string())
465 TeX_name = "\\\\mbox{"+name+"}";
471 function_options & function_options::latex_name(std::string const & tn)
477 // the following lines have been generated for max. ${maxargs} parameters
478 $eval_func_implementation
479 $evalf_func_implementation
480 $derivative_func_implementation
481 $series_func_implementation
482 // end of generated lines
484 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
486 use_return_type = true;
488 return_type_tinfo = rtt;
492 function_options & function_options::do_not_evalf_params(void)
494 evalf_params_first = false;
498 function_options & function_options::remember(unsigned size,
503 remember_size = size;
504 remember_assoc_size = assoc_size;
505 remember_strategy = strategy;
509 function_options & function_options::overloaded(unsigned o)
511 functions_with_same_name = o;
515 function_options & function_options::set_symmetry(const symmetry & s)
521 void function_options::test_and_set_nparams(unsigned n)
525 } else if (nparams!=n) {
526 // we do not throw an exception here because this code is
527 // usually executed before main(), so the exception could not
529 std::cerr << "WARNING: number of parameters ("
530 << n << ") differs from number set before ("
531 << nparams << ")" << std::endl;
535 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
538 // default ctor, dtor, copy ctor assignment operator and helpers
543 function::function() : serial(0)
545 debugmsg("function default ctor",LOGLEVEL_CONSTRUCT);
546 tinfo_key = TINFO_function;
551 void function::copy(const function & other)
553 inherited::copy(other);
554 serial = other.serial;
557 void function::destroy(bool call_parent)
560 inherited::destroy(call_parent);
569 function::function(unsigned ser) : serial(ser)
571 debugmsg("function ctor from unsigned",LOGLEVEL_CONSTRUCT);
572 tinfo_key = TINFO_function;
575 // the following lines have been generated for max. ${maxargs} parameters
576 $constructors_implementation
577 // end of generated lines
579 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
581 debugmsg("function ctor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
582 tinfo_key = TINFO_function;
585 function::function(unsigned ser, const exvector & v, bool discardable)
586 : exprseq(v,discardable), serial(ser)
588 debugmsg("function ctor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
589 tinfo_key = TINFO_function;
592 function::function(unsigned ser, exvector * vp)
593 : exprseq(vp), serial(ser)
595 debugmsg("function ctor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
596 tinfo_key = TINFO_function;
603 /** Construct object from archive_node. */
604 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
606 debugmsg("function ctor from archive_node", LOGLEVEL_CONSTRUCT);
608 // Find serial number by function name
610 if (n.find_string("name", s)) {
611 unsigned int ser = 0;
612 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
620 throw (std::runtime_error("unknown function '" + s + "' in archive"));
622 throw (std::runtime_error("unnamed function in archive"));
625 /** Unarchive the object. */
626 ex function::unarchive(const archive_node &n, const lst &sym_lst)
628 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
631 /** Archive the object. */
632 void function::archive(archive_node &n) const
634 inherited::archive(n);
635 GINAC_ASSERT(serial < registered_functions().size());
636 n.add_string("name", registered_functions()[serial].name);
640 // functions overriding virtual functions from bases classes
645 void function::print(const print_context & c, unsigned level) const
647 debugmsg("function print", LOGLEVEL_PRINT);
649 GINAC_ASSERT(serial<registered_functions().size());
651 if (is_of_type(c, print_tree)) {
653 c.s << std::string(level, ' ') << class_name() << " "
654 << registered_functions()[serial].name
655 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
656 << ", nops=" << nops()
658 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
659 for (unsigned i=0; i<seq.size(); ++i)
660 seq[i].print(c, level + delta_indent);
661 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
663 } else if (is_of_type(c, print_csrc)) {
665 // Print function name in lowercase
666 std::string lname = registered_functions()[serial].name;
667 unsigned num = lname.size();
668 for (unsigned i=0; i<num; i++)
669 lname[i] = tolower(lname[i]);
672 // Print arguments, separated by commas
673 exvector::const_iterator it = seq.begin(), itend = seq.end();
674 while (it != itend) {
682 } else if (is_of_type(c, print_latex)) {
683 c.s << registered_functions()[serial].TeX_name;
684 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
686 c.s << registered_functions()[serial].name;
687 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
691 ex function::expand(unsigned options) const
693 // Only expand arguments when asked to do so
694 if (options & expand_options::expand_function_args)
695 return inherited::expand(options);
697 return (options == 0) ? setflag(status_flags::expanded) : *this;
700 int function::degree(const ex & s) const
702 return is_equal(*s.bp) ? 1 : 0;
705 int function::ldegree(const ex & s) const
707 return is_equal(*s.bp) ? 1 : 0;
710 ex function::coeff(const ex & s, int n) const
713 return n==1 ? _ex1() : _ex0();
715 return n==0 ? ex(*this) : _ex0();
718 ex function::eval(int level) const
720 GINAC_ASSERT(serial<registered_functions().size());
723 // first evaluate children, then we will end up here again
724 return function(serial,evalchildren(level));
727 const function_options &opt = registered_functions()[serial];
729 // Canonicalize argument order according to the symmetry properties
730 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
732 GINAC_ASSERT(is_ex_exactly_of_type(opt.symtree, symmetry));
733 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
734 if (sig != INT_MAX) {
735 // Something has changed while sorting arguments, more evaluations later
738 return ex(sig) * thisexprseq(v);
746 bool use_remember = opt.use_remember;
748 if (use_remember && lookup_remember_table(eval_result)) {
752 switch (opt.nparams) {
753 // the following lines have been generated for max. ${maxargs} parameters
754 ${eval_switch_statement}
755 // end of generated lines
757 throw(std::logic_error("function::eval(): invalid nparams"));
760 store_remember_table(eval_result);
765 ex function::evalf(int level) const
767 GINAC_ASSERT(serial<registered_functions().size());
769 // Evaluate children first
773 else if (level == -max_recursion_level)
774 throw(std::runtime_error("max recursion level reached"));
776 eseq.reserve(seq.size());
778 exvector::const_iterator it = seq.begin(), itend = seq.end();
779 while (it != itend) {
780 eseq.push_back(it->evalf(level));
784 if (registered_functions()[serial].evalf_f==0) {
785 return function(serial,eseq).hold();
787 switch (registered_functions()[serial].nparams) {
788 // the following lines have been generated for max. ${maxargs} parameters
789 ${evalf_switch_statement}
790 // end of generated lines
792 throw(std::logic_error("function::evalf(): invalid nparams"));
795 unsigned function::calchash(void) const
797 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
798 for (unsigned i=0; i<nops(); i++) {
799 v = rotate_left_31(v);
800 v ^= this->op(i).gethash();
803 if (flags & status_flags::evaluated) {
804 setflag(status_flags::hash_calculated);
810 ex function::thisexprseq(const exvector & v) const
812 return function(serial,v);
815 ex function::thisexprseq(exvector * vp) const
817 return function(serial,vp);
820 /** Implementation of ex::series for functions.
821 * \@see ex::series */
822 ex function::series(const relational & r, int order, unsigned options) const
824 GINAC_ASSERT(serial<registered_functions().size());
826 if (registered_functions()[serial].series_f==0) {
827 return basic::series(r, order);
830 switch (registered_functions()[serial].nparams) {
831 // the following lines have been generated for max. ${maxargs} parameters
832 ${series_switch_statement}
833 // end of generated lines
835 throw(std::logic_error("function::series(): invalid nparams"));
840 /** Implementation of ex::diff() for functions. It applies the chain rule,
841 * except for the Order term function.
843 ex function::derivative(const symbol & s) const
847 if (serial == function_index_Order) {
848 // Order Term function only differentiates the argument
849 return Order(seq[0].diff(s));
850 } else if (serial == function_index_Derivative) {
851 // Inert derivative performs chain rule on the first argument only, and
852 // adds differentiation parameter to list (second argument)
853 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
854 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
857 for (unsigned i=0; i!=fcn.nops(); i++) {
858 arg_diff = fcn.op(i).diff(s);
859 if (!arg_diff.is_zero()) {
860 lst new_lst = ex_to<lst>(seq[1]);
862 result += arg_diff * Derivative(fcn, new_lst);
868 unsigned num = seq.size();
869 for (unsigned i=0; i<num; i++) {
870 arg_diff = seq[i].diff(s);
871 // We apply the chain rule only when it makes sense. This is not
872 // just for performance reasons but also to allow functions to
873 // throw when differentiated with respect to one of its arguments
874 // without running into trouble with our automatic full
876 if (!arg_diff.is_zero())
877 result += pderivative(i)*arg_diff;
883 int function::compare_same_type(const basic & other) const
885 GINAC_ASSERT(is_of_type(other, function));
886 const function & o = static_cast<const function &>(other);
888 if (serial != o.serial)
889 return serial < o.serial ? -1 : 1;
891 return exprseq::compare_same_type(o);
894 bool function::is_equal_same_type(const basic & other) const
896 GINAC_ASSERT(is_of_type(other, function));
897 const function & o = static_cast<const function &>(other);
899 if (serial != o.serial)
902 return exprseq::is_equal_same_type(o);
905 bool function::match_same_type(const basic & other) const
907 GINAC_ASSERT(is_of_type(other, function));
908 const function & o = static_cast<const function &>(other);
910 return serial == o.serial;
913 unsigned function::return_type(void) const
916 return return_types::commutative;
918 return seq.begin()->return_type();
921 unsigned function::return_type_tinfo(void) const
926 return seq.begin()->return_type_tinfo();
930 // new virtual functions which can be overridden by derived classes
936 // non-virtual functions in this class
941 ex function::pderivative(unsigned diff_param) const // partial differentiation
943 GINAC_ASSERT(serial<registered_functions().size());
945 if (registered_functions()[serial].derivative_f==0) {
946 return Derivative(*this, lst(ex(diff_param)));
948 switch (registered_functions()[serial].nparams) {
949 // the following lines have been generated for max. ${maxargs} parameters
950 ${diff_switch_statement}
951 // end of generated lines
953 throw(std::logic_error("function::pderivative(): no diff function defined"));
956 std::vector<function_options> & function::registered_functions(void)
958 static std::vector<function_options> * rf = new std::vector<function_options>;
962 bool function::lookup_remember_table(ex & result) const
964 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
967 void function::store_remember_table(ex const & result) const
969 remember_table::remember_tables()[serial].add_entry(*this,result);
974 unsigned function::register_new(function_options const & opt)
976 unsigned same_name = 0;
977 for (unsigned i=0; i<registered_functions().size(); ++i) {
978 if (registered_functions()[i].name==opt.name) {
982 if (same_name>=opt.functions_with_same_name) {
983 // we do not throw an exception here because this code is
984 // usually executed before main(), so the exception could not
986 std::cerr << "WARNING: function name " << opt.name
987 << " already in use!" << std::endl;
989 registered_functions().push_back(opt);
990 if (opt.use_remember) {
991 remember_table::remember_tables().
992 push_back(remember_table(opt.remember_size,
993 opt.remember_assoc_size,
994 opt.remember_strategy));
996 remember_table::remember_tables().push_back(remember_table());
998 return registered_functions().size()-1;
1001 /** Find serial number of function by name and number of parameters.
1002 * Throws exception if function was not found. */
1003 unsigned function::find_function(const std::string &name, unsigned nparams)
1005 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1006 unsigned serial = 0;
1008 if (i->get_name() == name && i->get_nparams() == nparams)
1013 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1016 /** Return the print name of the function. */
1017 std::string function::get_name(void) const
1019 GINAC_ASSERT(serial<registered_functions().size());
1020 return registered_functions()[serial].name;
1023 } // namespace GiNaC
1025 END_OF_IMPLEMENTATION
1027 print "Creating interface file function.h...";
1028 open OUT,">function.h" or die "cannot open function.h";
1029 print OUT $interface;
1033 print "Creating implementation file function.cpp...";
1034 open OUT,">function.cpp" or die "cannot open function.cpp";
1035 print OUT $implementation;