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,$seq_template3,$from,$to)=@_;
22 for ($N=$from; $N<=$to; $N++) {
23 $SEQ1=generate_seq($seq_template1,$N);
24 $SEQ2=generate_seq($seq_template2,$N);
25 $SEQ3=generate_seq($seq_template3,$N);
26 $res .= eval('"' . $template . '"');
27 $SEQ1=''; # to avoid main::SEQ1 used only once warning
28 $SEQ2=''; # same as above
29 $SEQ3=''; # same as above
35 my ($template,$seq_template1,$seq_template2,$seq_template3)=@_;
36 return generate_from_to($template,$seq_template1,$seq_template2,$seq_template3,1,$maxargs);
39 $declare_function_macro = generate(
40 <<'END_OF_DECLARE_FUNCTION_MACRO','typename T${N}','const T${N} & p${N}','GiNaC::ex(p${N})');
41 #define DECLARE_FUNCTION_${N}P(NAME) \\
42 extern const unsigned function_index_##NAME; \\
44 inline const GiNaC::function NAME(${SEQ2}) { \\
45 return GiNaC::function(function_index_##NAME, ${SEQ3}); \\
48 END_OF_DECLARE_FUNCTION_MACRO
50 $typedef_eval_funcp=generate(
51 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
54 $typedef_evalf_funcp=generate(
55 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
58 $typedef_derivative_funcp=generate(
59 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
62 $typedef_series_funcp=generate(
63 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
66 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
68 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
70 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
72 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','','');
74 $constructors_interface=generate(
75 ' function(unsigned ser, ${SEQ1});'."\n",
76 'const ex & param${N}','','');
78 $constructors_implementation=generate(
79 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
80 function::function(unsigned ser, ${SEQ1})
81 : exprseq(${SEQ2}), serial(ser)
83 tinfo_key = TINFO_function;
85 END_OF_CONSTRUCTORS_IMPLEMENTATION
87 $eval_switch_statement=generate(
88 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
90 eval_result = ((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
92 END_OF_EVAL_SWITCH_STATEMENT
94 $evalf_switch_statement=generate(
95 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
97 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
98 END_OF_EVALF_SWITCH_STATEMENT
100 $diff_switch_statement=generate(
101 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
103 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
104 END_OF_DIFF_SWITCH_STATEMENT
106 $series_switch_statement=generate(
107 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
110 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
111 } catch (do_taylor) {
112 res = basic::series(r, order, options);
115 END_OF_SERIES_SWITCH_STATEMENT
117 $eval_func_implementation=generate(
118 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
119 function_options & function_options::eval_func(eval_funcp_${N} e)
121 test_and_set_nparams(${N});
122 eval_f = eval_funcp(e);
125 END_OF_EVAL_FUNC_IMPLEMENTATION
127 $evalf_func_implementation=generate(
128 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
129 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
131 test_and_set_nparams(${N});
132 evalf_f = evalf_funcp(ef);
135 END_OF_EVALF_FUNC_IMPLEMENTATION
137 $derivative_func_implementation=generate(
138 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
139 function_options & function_options::derivative_func(derivative_funcp_${N} d)
141 test_and_set_nparams(${N});
142 derivative_f = derivative_funcp(d);
145 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
147 $series_func_implementation=generate(
148 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
149 function_options & function_options::series_func(series_funcp_${N} s)
151 test_and_set_nparams(${N});
152 series_f = series_funcp(s);
155 END_OF_SERIES_FUNC_IMPLEMENTATION
157 $interface=<<END_OF_INTERFACE;
158 /** \@file function.h
160 * Interface to class of symbolic functions. */
163 * This file was generated automatically by function.pl.
164 * Please do not modify it directly, edit the perl script instead!
165 * function.pl options: \$maxargs=${maxargs}
167 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
169 * This program is free software; you can redistribute it and/or modify
170 * it under the terms of the GNU General Public License as published by
171 * the Free Software Foundation; either version 2 of the License, or
172 * (at your option) any later version.
174 * This program is distributed in the hope that it will be useful,
175 * but WITHOUT ANY WARRANTY; without even the implied warranty of
176 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
177 * GNU General Public License for more details.
179 * You should have received a copy of the GNU General Public License
180 * along with this program; if not, write to the Free Software
181 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
184 #ifndef __GINAC_FUNCTION_H__
185 #define __GINAC_FUNCTION_H__
190 // CINT needs <algorithm> to work properly with <vector>
195 // the following lines have been generated for max. ${maxargs} parameters
196 $declare_function_macro
197 // end of generated lines
199 #define REGISTER_FUNCTION(NAME,OPT) \\
200 const unsigned function_index_##NAME= \\
201 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
208 typedef ex (* eval_funcp)();
209 typedef ex (* evalf_funcp)();
210 typedef ex (* derivative_funcp)();
211 typedef ex (* series_funcp)();
213 // the following lines have been generated for max. ${maxargs} parameters
216 $typedef_derivative_funcp
217 $typedef_series_funcp
218 // end of generated lines
220 // Alternatively, an exvector may be passed into the static function, instead
221 // of individual ex objects. Then, the number of arguments is not limited.
222 typedef ex (* eval_funcp_exvector)(const exvector &);
223 typedef ex (* evalf_funcp_exvector)(const exvector &);
224 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
225 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
227 class function_options
229 friend class function;
230 friend class fderivative;
233 function_options(std::string const & n, std::string const & tn=std::string());
235 void initialize(void);
236 function_options & set_name(std::string const & n, std::string const & tn=std::string());
237 function_options & latex_name(std::string const & tn);
238 // the following lines have been generated for max. ${maxargs} parameters
240 $evalf_func_interface
241 $derivative_func_interface
242 $series_func_interface
243 // end of generated lines
244 function_options & eval_func(eval_funcp_exvector e);
245 function_options & evalf_func(evalf_funcp_exvector ef);
246 function_options & derivative_func(derivative_funcp_exvector d);
247 function_options & series_func(series_funcp_exvector s);
249 function_options & set_return_type(unsigned rt, unsigned rtt=0);
250 function_options & do_not_evalf_params(void);
251 function_options & remember(unsigned size, unsigned assoc_size=0,
252 unsigned strategy=remember_strategies::delete_never);
253 function_options & overloaded(unsigned o);
254 function_options & set_symmetry(const symmetry & s);
255 void test_and_set_nparams(unsigned n);
256 std::string get_name(void) const { return name; }
257 unsigned get_nparams(void) const { return nparams; }
258 bool has_derivative(void) const { return derivative_f != NULL; }
262 std::string TeX_name;
268 derivative_funcp derivative_f;
269 series_funcp series_f;
271 bool evalf_params_first;
273 bool use_return_type;
274 unsigned return_type;
275 unsigned return_type_tinfo;
278 unsigned remember_size;
279 unsigned remember_assoc_size;
280 unsigned remember_strategy;
282 bool eval_use_exvector_args;
283 bool evalf_use_exvector_args;
284 bool derivative_use_exvector_args;
285 bool series_use_exvector_args;
287 unsigned functions_with_same_name;
292 /** The class function is used to implement builtin functions like sin, cos...
293 and user defined functions */
294 class function : public exprseq
296 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
298 // CINT has a linking problem
300 friend void ginsh_get_ginac_functions(void);
301 #endif // def __MAKECINT__
303 friend class remember_table_entry;
304 // friend class remember_table_list;
305 // friend class remember_table;
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 base 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 current_serial;
352 static unsigned find_function(const std::string &name, unsigned nparams);
353 unsigned get_serial(void) const {return serial;}
354 std::string get_name(void) const;
362 // utility functions/macros
364 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
365 template<> inline bool is_exactly_a<function>(const basic & obj)
367 return obj.tinfo()==TINFO_function;
370 #define is_ex_the_function(OBJ, FUNCNAME) \\
371 (is_exactly_a<GiNaC::function>(OBJ) && ex_to<GiNaC::function>(OBJ).get_serial() == function_index_##FUNCNAME)
375 #endif // ndef __GINAC_FUNCTION_H__
379 $implementation=<<END_OF_IMPLEMENTATION;
380 /** \@file function.cpp
382 * Implementation of class of symbolic functions. */
385 * This file was generated automatically by function.pl.
386 * Please do not modify it directly, edit the perl script instead!
387 * function.pl options: \$maxargs=${maxargs}
389 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
391 * This program is free software; you can redistribute it and/or modify
392 * it under the terms of the GNU General Public License as published by
393 * the Free Software Foundation; either version 2 of the License, or
394 * (at your option) any later version.
396 * This program is distributed in the hope that it will be useful,
397 * but WITHOUT ANY WARRANTY; without even the implied warranty of
398 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
399 * GNU General Public License for more details.
401 * You should have received a copy of the GNU General Public License
402 * along with this program; if not, write to the Free Software
403 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
411 #include "function.h"
412 #include "fderivative.h"
415 #include "symmetry.h"
419 #include "tostring.h"
421 #include "remember.h"
426 // helper class function_options
429 function_options::function_options()
434 function_options::function_options(std::string const & n, std::string const & tn)
440 function_options::~function_options()
442 // nothing to clean up at the moment
445 void function_options::initialize(void)
447 set_name("unnamed_function","\\\\mbox{unnamed}");
449 eval_f = evalf_f = derivative_f = series_f = 0;
450 evalf_params_first = true;
451 use_return_type = false;
452 eval_use_exvector_args = false;
453 evalf_use_exvector_args = false;
454 derivative_use_exvector_args = false;
455 series_use_exvector_args = false;
456 use_remember = false;
457 functions_with_same_name = 1;
461 function_options & function_options::set_name(std::string const & n,
462 std::string const & tn)
465 if (tn==std::string())
466 TeX_name = "\\\\mbox{"+name+"}";
472 function_options & function_options::latex_name(std::string const & tn)
478 // the following lines have been generated for max. ${maxargs} parameters
479 $eval_func_implementation
480 $evalf_func_implementation
481 $derivative_func_implementation
482 $series_func_implementation
483 // end of generated lines
485 function_options& function_options::eval_func(eval_funcp_exvector e)
487 eval_use_exvector_args = true;
488 eval_f = eval_funcp(e);
491 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
493 evalf_use_exvector_args = true;
494 evalf_f = evalf_funcp(ef);
497 function_options& function_options::derivative_func(derivative_funcp_exvector d)
499 derivative_use_exvector_args = true;
500 derivative_f = derivative_funcp(d);
503 function_options& function_options::series_func(series_funcp_exvector s)
505 series_use_exvector_args = true;
506 series_f = series_funcp(s);
511 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
513 use_return_type = true;
515 return_type_tinfo = rtt;
519 function_options & function_options::do_not_evalf_params(void)
521 evalf_params_first = false;
525 function_options & function_options::remember(unsigned size,
530 remember_size = size;
531 remember_assoc_size = assoc_size;
532 remember_strategy = strategy;
536 function_options & function_options::overloaded(unsigned o)
538 functions_with_same_name = o;
542 function_options & function_options::set_symmetry(const symmetry & s)
548 void function_options::test_and_set_nparams(unsigned n)
552 } else if (nparams!=n) {
553 // we do not throw an exception here because this code is
554 // usually executed before main(), so the exception could not
556 std::cerr << "WARNING: number of parameters ("
557 << n << ") differs from number set before ("
558 << nparams << ")" << std::endl;
562 /** This can be used as a hook for external applications. */
563 unsigned function::current_serial = 0;
566 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
569 // default ctor, dtor, copy ctor, assignment operator and helpers
574 function::function() : serial(0)
576 tinfo_key = TINFO_function;
581 void function::copy(const function & other)
583 inherited::copy(other);
584 serial = other.serial;
587 void function::destroy(bool call_parent)
590 inherited::destroy(call_parent);
599 function::function(unsigned ser) : serial(ser)
601 tinfo_key = TINFO_function;
604 // the following lines have been generated for max. ${maxargs} parameters
605 $constructors_implementation
606 // end of generated lines
608 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
610 tinfo_key = TINFO_function;
613 function::function(unsigned ser, const exvector & v, bool discardable)
614 : exprseq(v,discardable), serial(ser)
616 tinfo_key = TINFO_function;
619 function::function(unsigned ser, exvector * vp)
620 : exprseq(vp), serial(ser)
622 tinfo_key = TINFO_function;
629 /** Construct object from archive_node. */
630 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
632 // Find serial number by function name
634 if (n.find_string("name", s)) {
635 unsigned int ser = 0;
636 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
644 throw (std::runtime_error("unknown function '" + s + "' in archive"));
646 throw (std::runtime_error("unnamed function in archive"));
649 /** Unarchive the object. */
650 ex function::unarchive(const archive_node &n, const lst &sym_lst)
652 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
655 /** Archive the object. */
656 void function::archive(archive_node &n) const
658 inherited::archive(n);
659 GINAC_ASSERT(serial < registered_functions().size());
660 n.add_string("name", registered_functions()[serial].name);
664 // functions overriding virtual functions from base classes
669 void function::print(const print_context & c, unsigned level) const
671 GINAC_ASSERT(serial<registered_functions().size());
673 if (is_of_type(c, print_tree)) {
675 c.s << std::string(level, ' ') << class_name() << " "
676 << registered_functions()[serial].name
677 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
678 << ", nops=" << nops()
680 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
681 for (unsigned i=0; i<seq.size(); ++i)
682 seq[i].print(c, level + delta_indent);
683 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
685 } else if (is_of_type(c, print_csrc)) {
687 // Print function name in lowercase
688 std::string lname = registered_functions()[serial].name;
689 unsigned num = lname.size();
690 for (unsigned i=0; i<num; i++)
691 lname[i] = tolower(lname[i]);
694 // Print arguments, separated by commas
695 exvector::const_iterator it = seq.begin(), itend = seq.end();
696 while (it != itend) {
704 } else if (is_of_type(c, print_latex)) {
705 c.s << registered_functions()[serial].TeX_name;
706 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
708 c.s << registered_functions()[serial].name;
709 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
713 ex function::expand(unsigned options) const
715 // Only expand arguments when asked to do so
716 if (options & expand_options::expand_function_args)
717 return inherited::expand(options);
719 return (options == 0) ? setflag(status_flags::expanded) : *this;
722 int function::degree(const ex & s) const
724 return is_equal(ex_to<basic>(s)) ? 1 : 0;
727 int function::ldegree(const ex & s) const
729 return is_equal(ex_to<basic>(s)) ? 1 : 0;
732 ex function::coeff(const ex & s, int n) const
734 if (is_equal(ex_to<basic>(s)))
735 return n==1 ? _ex1 : _ex0;
737 return n==0 ? ex(*this) : _ex0;
740 ex function::eval(int level) const
742 GINAC_ASSERT(serial<registered_functions().size());
745 // first evaluate children, then we will end up here again
746 return function(serial,evalchildren(level));
749 const function_options &opt = registered_functions()[serial];
751 // Canonicalize argument order according to the symmetry properties
752 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
754 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
755 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
756 if (sig != INT_MAX) {
757 // Something has changed while sorting arguments, more evaluations later
760 return ex(sig) * thisexprseq(v);
768 bool use_remember = opt.use_remember;
770 if (use_remember && lookup_remember_table(eval_result)) {
773 current_serial = serial;
774 if (registered_functions()[serial].eval_use_exvector_args)
775 eval_result = ((eval_funcp_exvector)(registered_functions()[serial].eval_f))(seq);
777 switch (opt.nparams) {
778 // the following lines have been generated for max. ${maxargs} parameters
779 ${eval_switch_statement}
780 // end of generated lines
782 throw(std::logic_error("function::eval(): invalid nparams"));
785 store_remember_table(eval_result);
790 ex function::evalf(int level) const
792 GINAC_ASSERT(serial<registered_functions().size());
794 // Evaluate children first
798 else if (level == -max_recursion_level)
799 throw(std::runtime_error("max recursion level reached"));
801 eseq.reserve(seq.size());
803 exvector::const_iterator it = seq.begin(), itend = seq.end();
804 while (it != itend) {
805 eseq.push_back(it->evalf(level));
809 if (registered_functions()[serial].evalf_f==0) {
810 return function(serial,eseq).hold();
812 current_serial = serial;
813 if (registered_functions()[serial].evalf_use_exvector_args)
814 return ((evalf_funcp_exvector)(registered_functions()[serial].evalf_f))(seq);
815 switch (registered_functions()[serial].nparams) {
816 // the following lines have been generated for max. ${maxargs} parameters
817 ${evalf_switch_statement}
818 // end of generated lines
820 throw(std::logic_error("function::evalf(): invalid nparams"));
823 unsigned function::calchash(void) const
825 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
826 for (unsigned i=0; i<nops(); i++) {
827 v = rotate_left_31(v);
828 v ^= this->op(i).gethash();
831 if (flags & status_flags::evaluated) {
832 setflag(status_flags::hash_calculated);
838 ex function::thisexprseq(const exvector & v) const
840 return function(serial,v);
843 ex function::thisexprseq(exvector * vp) const
845 return function(serial,vp);
848 /** Implementation of ex::series for functions.
849 * \@see ex::series */
850 ex function::series(const relational & r, int order, unsigned options) const
852 GINAC_ASSERT(serial<registered_functions().size());
854 if (registered_functions()[serial].series_f==0) {
855 return basic::series(r, order);
858 current_serial = serial;
859 if (registered_functions()[serial].series_use_exvector_args) {
861 res = ((series_funcp_exvector)(registered_functions()[serial].series_f))(seq, r, order, options);
862 } catch (do_taylor) {
863 res = basic::series(r, order, options);
867 switch (registered_functions()[serial].nparams) {
868 // the following lines have been generated for max. ${maxargs} parameters
869 ${series_switch_statement}
870 // end of generated lines
872 throw(std::logic_error("function::series(): invalid nparams"));
877 /** Implementation of ex::diff() for functions. It applies the chain rule,
878 * except for the Order term function.
880 ex function::derivative(const symbol & s) const
884 if (serial == function_index_Order) {
885 // Order Term function only differentiates the argument
886 return Order(seq[0].diff(s));
890 unsigned num = seq.size();
891 for (unsigned i=0; i<num; i++) {
892 arg_diff = seq[i].diff(s);
893 // We apply the chain rule only when it makes sense. This is not
894 // just for performance reasons but also to allow functions to
895 // throw when differentiated with respect to one of its arguments
896 // without running into trouble with our automatic full
898 if (!arg_diff.is_zero())
899 result += pderivative(i)*arg_diff;
905 int function::compare_same_type(const basic & other) const
907 GINAC_ASSERT(is_of_type(other, function));
908 const function & o = static_cast<const function &>(other);
910 if (serial != o.serial)
911 return serial < o.serial ? -1 : 1;
913 return exprseq::compare_same_type(o);
916 bool function::is_equal_same_type(const basic & other) const
918 GINAC_ASSERT(is_of_type(other, function));
919 const function & o = static_cast<const function &>(other);
921 if (serial != o.serial)
924 return exprseq::is_equal_same_type(o);
927 bool function::match_same_type(const basic & other) const
929 GINAC_ASSERT(is_of_type(other, function));
930 const function & o = static_cast<const function &>(other);
932 return serial == o.serial;
935 unsigned function::return_type(void) const
938 return return_types::commutative;
940 return seq.begin()->return_type();
943 unsigned function::return_type_tinfo(void) const
948 return seq.begin()->return_type_tinfo();
952 // new virtual functions which can be overridden by derived classes
958 // non-virtual functions in this class
963 ex function::pderivative(unsigned diff_param) const // partial differentiation
965 GINAC_ASSERT(serial<registered_functions().size());
967 // No derivative defined? Then return abstract derivative object
968 if (registered_functions()[serial].derivative_f == NULL)
969 return fderivative(serial, diff_param, seq);
971 current_serial = serial;
972 if (registered_functions()[serial].derivative_use_exvector_args)
973 return ((derivative_funcp_exvector)(registered_functions()[serial].derivative_f))(seq, diff_param);
974 switch (registered_functions()[serial].nparams) {
975 // the following lines have been generated for max. ${maxargs} parameters
976 ${diff_switch_statement}
977 // end of generated lines
979 throw(std::logic_error("function::pderivative(): no diff function defined"));
982 std::vector<function_options> & function::registered_functions(void)
984 static std::vector<function_options> * rf = new std::vector<function_options>;
988 bool function::lookup_remember_table(ex & result) const
990 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
993 void function::store_remember_table(ex const & result) const
995 remember_table::remember_tables()[serial].add_entry(*this,result);
1000 unsigned function::register_new(function_options const & opt)
1002 unsigned same_name = 0;
1003 for (unsigned i=0; i<registered_functions().size(); ++i) {
1004 if (registered_functions()[i].name==opt.name) {
1008 if (same_name>=opt.functions_with_same_name) {
1009 // we do not throw an exception here because this code is
1010 // usually executed before main(), so the exception could not
1012 std::cerr << "WARNING: function name " << opt.name
1013 << " already in use!" << std::endl;
1015 registered_functions().push_back(opt);
1016 if (opt.use_remember) {
1017 remember_table::remember_tables().
1018 push_back(remember_table(opt.remember_size,
1019 opt.remember_assoc_size,
1020 opt.remember_strategy));
1022 remember_table::remember_tables().push_back(remember_table());
1024 return registered_functions().size()-1;
1027 /** Find serial number of function by name and number of parameters.
1028 * Throws exception if function was not found. */
1029 unsigned function::find_function(const std::string &name, unsigned nparams)
1031 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1032 unsigned serial = 0;
1034 if (i->get_name() == name && i->get_nparams() == nparams)
1039 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1042 /** Return the print name of the function. */
1043 std::string function::get_name(void) const
1045 GINAC_ASSERT(serial<registered_functions().size());
1046 return registered_functions()[serial].name;
1049 } // namespace GiNaC
1051 END_OF_IMPLEMENTATION
1053 print "Creating interface file function.h...";
1054 open OUT,">function.h" or die "cannot open function.h";
1055 print OUT $interface;
1059 print "Creating implementation file function.cpp...";
1060 open OUT,">function.cpp" or die "cannot open function.cpp";
1061 print OUT $implementation;