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 = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO';
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
51 $declare_function_macro .= generate_from_to(
52 <<'END_OF_DECLARE_FUNCTION_MACRO','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
61 $typedef_eval_funcp=generate(
62 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
65 $typedef_evalf_funcp=generate(
66 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
69 $typedef_derivative_funcp=generate(
70 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
73 $typedef_series_funcp=generate(
74 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
77 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','');
79 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','');
81 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','');
83 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','');
85 $constructors_interface=generate(
86 ' function(unsigned ser, ${SEQ1});'."\n",
87 'const ex & param${N}','');
89 $constructors_implementation=generate(
90 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}');
91 function::function(unsigned ser, ${SEQ1})
92 : exprseq(${SEQ2}), serial(ser)
94 debugmsg(\"function ctor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
95 tinfo_key = TINFO_function;
97 END_OF_CONSTRUCTORS_IMPLEMENTATION
99 $eval_switch_statement=generate(
100 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
102 eval_result = ((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
104 END_OF_EVAL_SWITCH_STATEMENT
106 $evalf_switch_statement=generate(
107 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
109 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
110 END_OF_EVALF_SWITCH_STATEMENT
112 $diff_switch_statement=generate(
113 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
115 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
116 END_OF_DIFF_SWITCH_STATEMENT
118 $series_switch_statement=generate(
119 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
122 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
123 } catch (do_taylor) {
124 res = basic::series(r, order, options);
127 END_OF_SERIES_SWITCH_STATEMENT
129 $eval_func_implementation=generate(
130 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','');
131 function_options & function_options::eval_func(eval_funcp_${N} e)
133 test_and_set_nparams(${N});
134 eval_f = eval_funcp(e);
137 END_OF_EVAL_FUNC_IMPLEMENTATION
139 $evalf_func_implementation=generate(
140 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','');
141 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
143 test_and_set_nparams(${N});
144 evalf_f = evalf_funcp(ef);
147 END_OF_EVALF_FUNC_IMPLEMENTATION
149 $derivative_func_implementation=generate(
150 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','');
151 function_options & function_options::derivative_func(derivative_funcp_${N} d)
153 test_and_set_nparams(${N});
154 derivative_f = derivative_funcp(d);
157 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
159 $series_func_implementation=generate(
160 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','');
161 function_options & function_options::series_func(series_funcp_${N} s)
163 test_and_set_nparams(${N});
164 series_f = series_funcp(s);
167 END_OF_SERIES_FUNC_IMPLEMENTATION
169 $interface=<<END_OF_INTERFACE;
170 /** \@file function.h
172 * Interface to abstract class function (new function concept). */
175 * This file was generated automatically by function.pl.
176 * Please do not modify it directly, edit the perl script instead!
177 * function.pl options: \$maxargs=${maxargs}
179 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
181 * This program is free software; you can redistribute it and/or modify
182 * it under the terms of the GNU General Public License as published by
183 * the Free Software Foundation; either version 2 of the License, or
184 * (at your option) any later version.
186 * This program is distributed in the hope that it will be useful,
187 * but WITHOUT ANY WARRANTY; without even the implied warranty of
188 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
189 * GNU General Public License for more details.
191 * You should have received a copy of the GNU General Public License
192 * along with this program; if not, write to the Free Software
193 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
196 #ifndef __GINAC_FUNCTION_H__
197 #define __GINAC_FUNCTION_H__
202 // CINT needs <algorithm> to work properly with <vector>
207 // the following lines have been generated for max. ${maxargs} parameters
208 $declare_function_macro
209 // end of generated lines
211 #define REGISTER_FUNCTION(NAME,OPT) \\
212 const unsigned function_index_##NAME= \\
213 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
215 #define BEGIN_TYPECHECK \\
216 bool automatic_typecheck=true;
218 #define TYPECHECK(VAR,TYPE) \\
219 if (!is_exactly_a<TYPE>(VAR)) { \\
220 automatic_typecheck=false; \\
223 #define TYPECHECK_INTEGER(VAR) \\
224 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
225 automatic_typecheck=false; \\
228 #define END_TYPECHECK(RV) \\
230 if (!automatic_typecheck) { \\
239 typedef ex (* eval_funcp)();
240 typedef ex (* evalf_funcp)();
241 typedef ex (* derivative_funcp)();
242 typedef ex (* series_funcp)();
244 // the following lines have been generated for max. ${maxargs} parameters
247 $typedef_derivative_funcp
248 $typedef_series_funcp
249 // end of generated lines
251 class function_options
253 friend class function;
256 function_options(std::string const & n, std::string const & tn=std::string());
258 void initialize(void);
259 function_options & set_name(std::string const & n, std::string const & tn=std::string());
260 function_options & latex_name(std::string const & tn);
261 // the following lines have been generated for max. ${maxargs} parameters
263 $evalf_func_interface
264 $derivative_func_interface
265 $series_func_interface
266 // end of generated lines
267 function_options & set_return_type(unsigned rt, unsigned rtt=0);
268 function_options & do_not_evalf_params(void);
269 function_options & remember(unsigned size, unsigned assoc_size=0,
270 unsigned strategy=remember_strategies::delete_never);
271 function_options & overloaded(unsigned o);
272 function_options & set_symmetry(const symmetry & s);
273 void test_and_set_nparams(unsigned n);
274 std::string get_name(void) const { return name; }
275 unsigned get_nparams(void) const { return nparams; }
276 bool has_derivative(void) const { return derivative_f != NULL; }
280 std::string TeX_name;
286 derivative_funcp derivative_f;
287 series_funcp series_f;
289 bool evalf_params_first;
291 bool use_return_type;
292 unsigned return_type;
293 unsigned return_type_tinfo;
296 unsigned remember_size;
297 unsigned remember_assoc_size;
298 unsigned remember_strategy;
300 unsigned functions_with_same_name;
305 /** The class function is used to implement builtin functions like sin, cos...
306 and user defined functions */
307 class function : public exprseq
309 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
311 // CINT has a linking problem
313 friend void ginsh_get_ginac_functions(void);
314 #endif // def __MAKECINT__
316 friend class remember_table_entry;
317 // friend class remember_table_list;
318 // friend class remember_table;
319 friend ex Derivative_eval(const ex &, const ex &);
325 function(unsigned ser);
326 // the following lines have been generated for max. ${maxargs} parameters
327 $constructors_interface
328 // end of generated lines
329 function(unsigned ser, const exprseq & es);
330 function(unsigned ser, const exvector & v, bool discardable = false);
331 function(unsigned ser, exvector * vp); // vp will be deleted
333 // functions overriding virtual functions from bases classes
335 void print(const print_context & c, unsigned level = 0) const;
336 unsigned precedence(void) const {return 70;}
337 int degree(const ex & s) const;
338 int ldegree(const ex & s) const;
339 ex coeff(const ex & s, int n = 1) const;
340 ex expand(unsigned options=0) const;
341 ex eval(int level=0) const;
342 ex evalf(int level=0) const;
343 unsigned calchash(void) const;
344 ex series(const relational & r, int order, unsigned options = 0) const;
345 ex thisexprseq(const exvector & v) const;
346 ex thisexprseq(exvector * vp) const;
348 ex derivative(const symbol & s) const;
349 bool is_equal_same_type(const basic & other) const;
350 bool match_same_type(const basic & other) const;
351 unsigned return_type(void) const;
352 unsigned return_type_tinfo(void) const;
354 // new virtual functions which can be overridden by derived classes
357 // non-virtual functions in this class
359 ex pderivative(unsigned diff_param) const; // partial differentiation
360 static std::vector<function_options> & registered_functions(void);
361 bool lookup_remember_table(ex & result) const;
362 void store_remember_table(ex const & result) const;
364 static unsigned register_new(function_options const & opt);
365 static unsigned find_function(const std::string &name, unsigned nparams);
366 unsigned get_serial(void) const {return serial;}
367 std::string get_name(void) const;
375 // utility functions/macros
376 /** Return the object of type function handled by an ex.
377 * This is unsafe: you need to check the type first. */
378 inline const function &ex_to_function(const ex &e)
380 return static_cast<const function &>(*e.bp);
383 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
384 template<> inline bool is_exactly_a<function>(const basic & obj)
386 return obj.tinfo()==TINFO_function;
389 #define is_ex_the_function(OBJ, FUNCNAME) \\
390 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->get_serial() == function_index_##FUNCNAME)
394 #endif // ndef __GINAC_FUNCTION_H__
398 $implementation=<<END_OF_IMPLEMENTATION;
399 /** \@file function.cpp
401 * Implementation of class function. */
404 * This file was generated automatically by function.pl.
405 * Please do not modify it directly, edit the perl script instead!
406 * function.pl options: \$maxargs=${maxargs}
408 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
410 * This program is free software; you can redistribute it and/or modify
411 * it under the terms of the GNU General Public License as published by
412 * the Free Software Foundation; either version 2 of the License, or
413 * (at your option) any later version.
415 * This program is distributed in the hope that it will be useful,
416 * but WITHOUT ANY WARRANTY; without even the implied warranty of
417 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
418 * GNU General Public License for more details.
420 * You should have received a copy of the GNU General Public License
421 * along with this program; if not, write to the Free Software
422 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
429 #include "function.h"
432 #include "symmetry.h"
437 #include "debugmsg.h"
438 #include "remember.h"
443 // helper class function_options
446 function_options::function_options()
451 function_options::function_options(std::string const & n, std::string const & tn)
457 function_options::~function_options()
459 // nothing to clean up at the moment
462 void function_options::initialize(void)
464 set_name("unnamed_function","\\\\mbox{unnamed}");
466 eval_f = evalf_f = derivative_f = series_f = 0;
467 evalf_params_first = true;
468 use_return_type = false;
469 use_remember = false;
470 functions_with_same_name = 1;
474 function_options & function_options::set_name(std::string const & n,
475 std::string const & tn)
478 if (tn==std::string())
479 TeX_name = "\\\\mbox{"+name+"}";
485 function_options & function_options::latex_name(std::string const & tn)
491 // the following lines have been generated for max. ${maxargs} parameters
492 $eval_func_implementation
493 $evalf_func_implementation
494 $derivative_func_implementation
495 $series_func_implementation
496 // end of generated lines
498 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
500 use_return_type = true;
502 return_type_tinfo = rtt;
506 function_options & function_options::do_not_evalf_params(void)
508 evalf_params_first = false;
512 function_options & function_options::remember(unsigned size,
517 remember_size = size;
518 remember_assoc_size = assoc_size;
519 remember_strategy = strategy;
523 function_options & function_options::overloaded(unsigned o)
525 functions_with_same_name = o;
529 function_options & function_options::set_symmetry(const symmetry & s)
535 void function_options::test_and_set_nparams(unsigned n)
539 } else if (nparams!=n) {
540 // we do not throw an exception here because this code is
541 // usually executed before main(), so the exception could not
543 std::cerr << "WARNING: number of parameters ("
544 << n << ") differs from number set before ("
545 << nparams << ")" << std::endl;
549 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
552 // default ctor, dtor, copy ctor assignment operator and helpers
557 function::function() : serial(0)
559 debugmsg("function default ctor",LOGLEVEL_CONSTRUCT);
560 tinfo_key = TINFO_function;
565 void function::copy(const function & other)
567 inherited::copy(other);
568 serial = other.serial;
571 void function::destroy(bool call_parent)
574 inherited::destroy(call_parent);
583 function::function(unsigned ser) : serial(ser)
585 debugmsg("function ctor from unsigned",LOGLEVEL_CONSTRUCT);
586 tinfo_key = TINFO_function;
589 // the following lines have been generated for max. ${maxargs} parameters
590 $constructors_implementation
591 // end of generated lines
593 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
595 debugmsg("function ctor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
596 tinfo_key = TINFO_function;
599 function::function(unsigned ser, const exvector & v, bool discardable)
600 : exprseq(v,discardable), serial(ser)
602 debugmsg("function ctor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
603 tinfo_key = TINFO_function;
606 function::function(unsigned ser, exvector * vp)
607 : exprseq(vp), serial(ser)
609 debugmsg("function ctor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
610 tinfo_key = TINFO_function;
617 /** Construct object from archive_node. */
618 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
620 debugmsg("function ctor from archive_node", LOGLEVEL_CONSTRUCT);
622 // Find serial number by function name
624 if (n.find_string("name", s)) {
625 unsigned int ser = 0;
626 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
634 throw (std::runtime_error("unknown function '" + s + "' in archive"));
636 throw (std::runtime_error("unnamed function in archive"));
639 /** Unarchive the object. */
640 ex function::unarchive(const archive_node &n, const lst &sym_lst)
642 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
645 /** Archive the object. */
646 void function::archive(archive_node &n) const
648 inherited::archive(n);
649 GINAC_ASSERT(serial < registered_functions().size());
650 n.add_string("name", registered_functions()[serial].name);
654 // functions overriding virtual functions from bases classes
659 void function::print(const print_context & c, unsigned level) const
661 debugmsg("function print", LOGLEVEL_PRINT);
663 GINAC_ASSERT(serial<registered_functions().size());
665 if (is_of_type(c, print_tree)) {
667 c.s << std::string(level, ' ') << class_name() << " "
668 << registered_functions()[serial].name
669 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
670 << ", nops=" << nops()
672 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
673 for (unsigned i=0; i<seq.size(); ++i)
674 seq[i].print(c, level + delta_indent);
675 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
677 } else if (is_of_type(c, print_csrc)) {
679 // Print function name in lowercase
680 std::string lname = registered_functions()[serial].name;
681 unsigned num = lname.size();
682 for (unsigned i=0; i<num; i++)
683 lname[i] = tolower(lname[i]);
686 // Print arguments, separated by commas
687 exvector::const_iterator it = seq.begin(), itend = seq.end();
688 while (it != itend) {
696 } else if (is_of_type(c, print_latex)) {
697 c.s << registered_functions()[serial].TeX_name;
698 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
700 c.s << registered_functions()[serial].name;
701 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
705 ex function::expand(unsigned options) const
707 // Only expand arguments when asked to do so
708 if (options & expand_options::expand_function_args)
709 return inherited::expand(options);
711 return (options == 0) ? setflag(status_flags::expanded) : *this;
714 int function::degree(const ex & s) const
716 return is_equal(*s.bp) ? 1 : 0;
719 int function::ldegree(const ex & s) const
721 return is_equal(*s.bp) ? 1 : 0;
724 ex function::coeff(const ex & s, int n) const
727 return n==1 ? _ex1() : _ex0();
729 return n==0 ? ex(*this) : _ex0();
732 ex function::eval(int level) const
734 GINAC_ASSERT(serial<registered_functions().size());
737 // first evaluate children, then we will end up here again
738 return function(serial,evalchildren(level));
741 const function_options &opt = registered_functions()[serial];
743 // Canonicalize argument order according to the symmetry properties
744 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
746 GINAC_ASSERT(is_ex_exactly_of_type(opt.symtree, symmetry));
747 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
748 if (sig != INT_MAX) {
749 // Something has changed while sorting arguments, more evaluations later
752 return ex(sig) * thisexprseq(v);
760 bool use_remember = opt.use_remember;
762 if (use_remember && lookup_remember_table(eval_result)) {
766 switch (opt.nparams) {
767 // the following lines have been generated for max. ${maxargs} parameters
768 ${eval_switch_statement}
769 // end of generated lines
771 throw(std::logic_error("function::eval(): invalid nparams"));
774 store_remember_table(eval_result);
779 ex function::evalf(int level) const
781 GINAC_ASSERT(serial<registered_functions().size());
783 // Evaluate children first
787 else if (level == -max_recursion_level)
788 throw(std::runtime_error("max recursion level reached"));
790 eseq.reserve(seq.size());
792 exvector::const_iterator it = seq.begin(), itend = seq.end();
793 while (it != itend) {
794 eseq.push_back((*it).evalf(level));
798 if (registered_functions()[serial].evalf_f==0) {
799 return function(serial,eseq).hold();
801 switch (registered_functions()[serial].nparams) {
802 // the following lines have been generated for max. ${maxargs} parameters
803 ${evalf_switch_statement}
804 // end of generated lines
806 throw(std::logic_error("function::evalf(): invalid nparams"));
809 unsigned function::calchash(void) const
811 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
812 for (unsigned i=0; i<nops(); i++) {
813 v = rotate_left_31(v);
814 v ^= this->op(i).gethash();
817 if (flags & status_flags::evaluated) {
818 setflag(status_flags::hash_calculated);
824 ex function::thisexprseq(const exvector & v) const
826 return function(serial,v);
829 ex function::thisexprseq(exvector * vp) const
831 return function(serial,vp);
834 /** Implementation of ex::series for functions.
835 * \@see ex::series */
836 ex function::series(const relational & r, int order, unsigned options) const
838 GINAC_ASSERT(serial<registered_functions().size());
840 if (registered_functions()[serial].series_f==0) {
841 return basic::series(r, order);
844 switch (registered_functions()[serial].nparams) {
845 // the following lines have been generated for max. ${maxargs} parameters
846 ${series_switch_statement}
847 // end of generated lines
849 throw(std::logic_error("function::series(): invalid nparams"));
854 /** Implementation of ex::diff() for functions. It applies the chain rule,
855 * except for the Order term function.
857 ex function::derivative(const symbol & s) const
861 if (serial == function_index_Order) {
862 // Order Term function only differentiates the argument
863 return Order(seq[0].diff(s));
864 } else if (serial == function_index_Derivative) {
865 // Inert derivative performs chain rule on the first argument only, and
866 // adds differentiation parameter to list (second argument)
867 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
868 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
871 for (unsigned i=0; i!=fcn.nops(); i++) {
872 arg_diff = fcn.op(i).diff(s);
873 if (!arg_diff.is_zero()) {
874 lst new_lst = ex_to<lst>(seq[1]);
876 result += arg_diff * Derivative(fcn, new_lst);
882 unsigned num = seq.size();
883 for (unsigned i=0; i<num; i++) {
884 arg_diff = seq[i].diff(s);
885 // We apply the chain rule only when it makes sense. This is not
886 // just for performance reasons but also to allow functions to
887 // throw when differentiated with respect to one of its arguments
888 // without running into trouble with our automatic full
890 if (!arg_diff.is_zero())
891 result += pderivative(i)*arg_diff;
897 int function::compare_same_type(const basic & other) const
899 GINAC_ASSERT(is_of_type(other, function));
900 const function & o = static_cast<const function &>(other);
902 if (serial != o.serial)
903 return serial < o.serial ? -1 : 1;
905 return exprseq::compare_same_type(o);
908 bool function::is_equal_same_type(const basic & other) const
910 GINAC_ASSERT(is_of_type(other, function));
911 const function & o = static_cast<const function &>(other);
913 if (serial != o.serial)
916 return exprseq::is_equal_same_type(o);
919 bool function::match_same_type(const basic & other) const
921 GINAC_ASSERT(is_of_type(other, function));
922 const function & o = static_cast<const function &>(other);
924 return serial == o.serial;
927 unsigned function::return_type(void) const
930 return return_types::commutative;
932 return seq.begin()->return_type();
935 unsigned function::return_type_tinfo(void) const
940 return seq.begin()->return_type_tinfo();
944 // new virtual functions which can be overridden by derived classes
950 // non-virtual functions in this class
955 ex function::pderivative(unsigned diff_param) const // partial differentiation
957 GINAC_ASSERT(serial<registered_functions().size());
959 if (registered_functions()[serial].derivative_f==0) {
960 return Derivative(*this, lst(ex(diff_param)));
962 switch (registered_functions()[serial].nparams) {
963 // the following lines have been generated for max. ${maxargs} parameters
964 ${diff_switch_statement}
965 // end of generated lines
967 throw(std::logic_error("function::pderivative(): no diff function defined"));
970 std::vector<function_options> & function::registered_functions(void)
972 static std::vector<function_options> * rf = new std::vector<function_options>;
976 bool function::lookup_remember_table(ex & result) const
978 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
981 void function::store_remember_table(ex const & result) const
983 remember_table::remember_tables()[serial].add_entry(*this,result);
988 unsigned function::register_new(function_options const & opt)
990 unsigned same_name = 0;
991 for (unsigned i=0; i<registered_functions().size(); ++i) {
992 if (registered_functions()[i].name==opt.name) {
996 if (same_name>=opt.functions_with_same_name) {
997 // we do not throw an exception here because this code is
998 // usually executed before main(), so the exception could not
1000 std::cerr << "WARNING: function name " << opt.name
1001 << " already in use!" << std::endl;
1003 registered_functions().push_back(opt);
1004 if (opt.use_remember) {
1005 remember_table::remember_tables().
1006 push_back(remember_table(opt.remember_size,
1007 opt.remember_assoc_size,
1008 opt.remember_strategy));
1010 remember_table::remember_tables().push_back(remember_table());
1012 return registered_functions().size()-1;
1015 /** Find serial number of function by name and number of parameters.
1016 * Throws exception if function was not found. */
1017 unsigned function::find_function(const std::string &name, unsigned nparams)
1019 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1020 unsigned serial = 0;
1022 if (i->get_name() == name && i->get_nparams() == nparams)
1027 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1030 /** Return the print name of the function. */
1031 std::string function::get_name(void) const
1033 GINAC_ASSERT(serial<registered_functions().size());
1034 return registered_functions()[serial].name;
1037 } // namespace GiNaC
1039 END_OF_IMPLEMENTATION
1041 print "Creating interface file function.h...";
1042 open OUT,">function.h" or die "cannot open function.h";
1043 print OUT $interface;
1047 print "Creating implementation file function.cpp...";
1048 open OUT,">function.cpp" or die "cannot open function.cpp";
1049 print OUT $implementation;