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);
203 // The TYPECHECK-macros were used inside the _evalf() functions. They are
204 // considered obsolete now: (FIXME: remove them)
206 #define BEGIN_TYPECHECK \\
207 bool automatic_typecheck=true;
209 #define TYPECHECK(VAR,TYPE) \\
210 if (!is_exactly_a<TYPE>(VAR)) { \\
211 automatic_typecheck=false; \\
214 #define TYPECHECK_INTEGER(VAR) \\
215 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
216 automatic_typecheck=false; \\
219 #define END_TYPECHECK(RV) \\
221 if (!automatic_typecheck) { \\
230 typedef ex (* eval_funcp)();
231 typedef ex (* evalf_funcp)();
232 typedef ex (* derivative_funcp)();
233 typedef ex (* series_funcp)();
235 // the following lines have been generated for max. ${maxargs} parameters
238 $typedef_derivative_funcp
239 $typedef_series_funcp
240 // end of generated lines
242 // Alternatively, an exvector may be passed into the static function, instead
243 // of individual ex objects. Then, the number of arguments is not limited.
244 typedef ex (* eval_funcp_exvector)(const exvector &);
245 typedef ex (* evalf_funcp_exvector)(const exvector &);
246 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
247 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
249 class function_options
251 friend class function;
252 friend class fderivative;
255 function_options(std::string const & n, std::string const & tn=std::string());
257 void initialize(void);
258 function_options & set_name(std::string const & n, std::string const & tn=std::string());
259 function_options & latex_name(std::string const & tn);
260 // the following lines have been generated for max. ${maxargs} parameters
262 $evalf_func_interface
263 $derivative_func_interface
264 $series_func_interface
265 // end of generated lines
266 function_options & eval_func(eval_funcp_exvector e);
267 function_options & evalf_func(evalf_funcp_exvector ef);
268 function_options & derivative_func(derivative_funcp_exvector d);
269 function_options & series_func(series_funcp_exvector s);
271 function_options & set_return_type(unsigned rt, unsigned rtt=0);
272 function_options & do_not_evalf_params(void);
273 function_options & remember(unsigned size, unsigned assoc_size=0,
274 unsigned strategy=remember_strategies::delete_never);
275 function_options & overloaded(unsigned o);
276 function_options & set_symmetry(const symmetry & s);
277 void test_and_set_nparams(unsigned n);
278 std::string get_name(void) const { return name; }
279 unsigned get_nparams(void) const { return nparams; }
280 bool has_derivative(void) const { return derivative_f != NULL; }
284 std::string TeX_name;
290 derivative_funcp derivative_f;
291 series_funcp series_f;
293 bool evalf_params_first;
295 bool use_return_type;
296 unsigned return_type;
297 unsigned return_type_tinfo;
300 unsigned remember_size;
301 unsigned remember_assoc_size;
302 unsigned remember_strategy;
304 bool eval_use_exvector_args;
305 bool evalf_use_exvector_args;
306 bool derivative_use_exvector_args;
307 bool series_use_exvector_args;
309 unsigned functions_with_same_name;
314 /** The class function is used to implement builtin functions like sin, cos...
315 and user defined functions */
316 class function : public exprseq
318 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
320 // CINT has a linking problem
322 friend void ginsh_get_ginac_functions(void);
323 #endif // def __MAKECINT__
325 friend class remember_table_entry;
326 // friend class remember_table_list;
327 // friend class remember_table;
333 function(unsigned ser);
334 // the following lines have been generated for max. ${maxargs} parameters
335 $constructors_interface
336 // end of generated lines
337 function(unsigned ser, const exprseq & es);
338 function(unsigned ser, const exvector & v, bool discardable = false);
339 function(unsigned ser, exvector * vp); // vp will be deleted
341 // functions overriding virtual functions from base classes
343 void print(const print_context & c, unsigned level = 0) const;
344 unsigned precedence(void) const {return 70;}
345 int degree(const ex & s) const;
346 int ldegree(const ex & s) const;
347 ex coeff(const ex & s, int n = 1) const;
348 ex expand(unsigned options=0) const;
349 ex eval(int level=0) const;
350 ex evalf(int level=0) const;
351 unsigned calchash(void) const;
352 ex series(const relational & r, int order, unsigned options = 0) const;
353 ex thisexprseq(const exvector & v) const;
354 ex thisexprseq(exvector * vp) const;
356 ex derivative(const symbol & s) const;
357 bool is_equal_same_type(const basic & other) const;
358 bool match_same_type(const basic & other) const;
359 unsigned return_type(void) const;
360 unsigned return_type_tinfo(void) const;
362 // new virtual functions which can be overridden by derived classes
365 // non-virtual functions in this class
367 ex pderivative(unsigned diff_param) const; // partial differentiation
368 static std::vector<function_options> & registered_functions(void);
369 bool lookup_remember_table(ex & result) const;
370 void store_remember_table(ex const & result) const;
372 static unsigned register_new(function_options const & opt);
373 static unsigned current_serial;
374 static unsigned find_function(const std::string &name, unsigned nparams);
375 unsigned get_serial(void) const {return serial;}
376 std::string get_name(void) const;
384 // utility functions/macros
386 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
387 template<> inline bool is_exactly_a<function>(const basic & obj)
389 return obj.tinfo()==TINFO_function;
392 #define is_ex_the_function(OBJ, FUNCNAME) \\
393 (is_exactly_a<GiNaC::function>(OBJ) && ex_to<GiNaC::function>(OBJ).get_serial() == function_index_##FUNCNAME)
397 #endif // ndef __GINAC_FUNCTION_H__
401 $implementation=<<END_OF_IMPLEMENTATION;
402 /** \@file function.cpp
404 * Implementation of class of symbolic functions. */
407 * This file was generated automatically by function.pl.
408 * Please do not modify it directly, edit the perl script instead!
409 * function.pl options: \$maxargs=${maxargs}
411 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
413 * This program is free software; you can redistribute it and/or modify
414 * it under the terms of the GNU General Public License as published by
415 * the Free Software Foundation; either version 2 of the License, or
416 * (at your option) any later version.
418 * This program is distributed in the hope that it will be useful,
419 * but WITHOUT ANY WARRANTY; without even the implied warranty of
420 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
421 * GNU General Public License for more details.
423 * You should have received a copy of the GNU General Public License
424 * along with this program; if not, write to the Free Software
425 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
433 #include "function.h"
434 #include "fderivative.h"
437 #include "symmetry.h"
441 #include "tostring.h"
443 #include "remember.h"
448 // helper class function_options
451 function_options::function_options()
456 function_options::function_options(std::string const & n, std::string const & tn)
462 function_options::~function_options()
464 // nothing to clean up at the moment
467 void function_options::initialize(void)
469 set_name("unnamed_function","\\\\mbox{unnamed}");
471 eval_f = evalf_f = derivative_f = series_f = 0;
472 evalf_params_first = true;
473 use_return_type = false;
474 eval_use_exvector_args = false;
475 evalf_use_exvector_args = false;
476 derivative_use_exvector_args = false;
477 series_use_exvector_args = false;
478 use_remember = false;
479 functions_with_same_name = 1;
483 function_options & function_options::set_name(std::string const & n,
484 std::string const & tn)
487 if (tn==std::string())
488 TeX_name = "\\\\mbox{"+name+"}";
494 function_options & function_options::latex_name(std::string const & tn)
500 // the following lines have been generated for max. ${maxargs} parameters
501 $eval_func_implementation
502 $evalf_func_implementation
503 $derivative_func_implementation
504 $series_func_implementation
505 // end of generated lines
507 function_options& function_options::eval_func(eval_funcp_exvector e)
509 eval_use_exvector_args = true;
510 eval_f = eval_funcp(e);
513 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
515 evalf_use_exvector_args = true;
516 evalf_f = evalf_funcp(ef);
519 function_options& function_options::derivative_func(derivative_funcp_exvector d)
521 derivative_use_exvector_args = true;
522 derivative_f = derivative_funcp(d);
525 function_options& function_options::series_func(series_funcp_exvector s)
527 series_use_exvector_args = true;
528 series_f = series_funcp(s);
533 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
535 use_return_type = true;
537 return_type_tinfo = rtt;
541 function_options & function_options::do_not_evalf_params(void)
543 evalf_params_first = false;
547 function_options & function_options::remember(unsigned size,
552 remember_size = size;
553 remember_assoc_size = assoc_size;
554 remember_strategy = strategy;
558 function_options & function_options::overloaded(unsigned o)
560 functions_with_same_name = o;
564 function_options & function_options::set_symmetry(const symmetry & s)
570 void function_options::test_and_set_nparams(unsigned n)
574 } else if (nparams!=n) {
575 // we do not throw an exception here because this code is
576 // usually executed before main(), so the exception could not
578 std::cerr << "WARNING: number of parameters ("
579 << n << ") differs from number set before ("
580 << nparams << ")" << std::endl;
584 /** This can be used as a hook for external applications. */
585 unsigned function::current_serial = 0;
588 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
591 // default ctor, dtor, copy ctor, assignment operator and helpers
596 function::function() : serial(0)
598 tinfo_key = TINFO_function;
603 void function::copy(const function & other)
605 inherited::copy(other);
606 serial = other.serial;
609 void function::destroy(bool call_parent)
612 inherited::destroy(call_parent);
621 function::function(unsigned ser) : serial(ser)
623 tinfo_key = TINFO_function;
626 // the following lines have been generated for max. ${maxargs} parameters
627 $constructors_implementation
628 // end of generated lines
630 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
632 tinfo_key = TINFO_function;
635 function::function(unsigned ser, const exvector & v, bool discardable)
636 : exprseq(v,discardable), serial(ser)
638 tinfo_key = TINFO_function;
641 function::function(unsigned ser, exvector * vp)
642 : exprseq(vp), serial(ser)
644 tinfo_key = TINFO_function;
651 /** Construct object from archive_node. */
652 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
654 // Find serial number by function name
656 if (n.find_string("name", s)) {
657 unsigned int ser = 0;
658 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
666 throw (std::runtime_error("unknown function '" + s + "' in archive"));
668 throw (std::runtime_error("unnamed function in archive"));
671 /** Unarchive the object. */
672 ex function::unarchive(const archive_node &n, const lst &sym_lst)
674 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
677 /** Archive the object. */
678 void function::archive(archive_node &n) const
680 inherited::archive(n);
681 GINAC_ASSERT(serial < registered_functions().size());
682 n.add_string("name", registered_functions()[serial].name);
686 // functions overriding virtual functions from base classes
691 void function::print(const print_context & c, unsigned level) const
693 GINAC_ASSERT(serial<registered_functions().size());
695 if (is_of_type(c, print_tree)) {
697 c.s << std::string(level, ' ') << class_name() << " "
698 << registered_functions()[serial].name
699 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
700 << ", nops=" << nops()
702 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
703 for (unsigned i=0; i<seq.size(); ++i)
704 seq[i].print(c, level + delta_indent);
705 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
707 } else if (is_of_type(c, print_csrc)) {
709 // Print function name in lowercase
710 std::string lname = registered_functions()[serial].name;
711 unsigned num = lname.size();
712 for (unsigned i=0; i<num; i++)
713 lname[i] = tolower(lname[i]);
716 // Print arguments, separated by commas
717 exvector::const_iterator it = seq.begin(), itend = seq.end();
718 while (it != itend) {
726 } else if (is_of_type(c, print_latex)) {
727 c.s << registered_functions()[serial].TeX_name;
728 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
730 c.s << registered_functions()[serial].name;
731 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
735 ex function::expand(unsigned options) const
737 // Only expand arguments when asked to do so
738 if (options & expand_options::expand_function_args)
739 return inherited::expand(options);
741 return (options == 0) ? setflag(status_flags::expanded) : *this;
744 int function::degree(const ex & s) const
746 return is_equal(ex_to<basic>(s)) ? 1 : 0;
749 int function::ldegree(const ex & s) const
751 return is_equal(ex_to<basic>(s)) ? 1 : 0;
754 ex function::coeff(const ex & s, int n) const
756 if (is_equal(ex_to<basic>(s)))
757 return n==1 ? _ex1 : _ex0;
759 return n==0 ? ex(*this) : _ex0;
762 ex function::eval(int level) const
764 GINAC_ASSERT(serial<registered_functions().size());
767 // first evaluate children, then we will end up here again
768 return function(serial,evalchildren(level));
771 const function_options &opt = registered_functions()[serial];
773 // Canonicalize argument order according to the symmetry properties
774 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
776 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
777 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
778 if (sig != INT_MAX) {
779 // Something has changed while sorting arguments, more evaluations later
782 return ex(sig) * thisexprseq(v);
790 bool use_remember = opt.use_remember;
792 if (use_remember && lookup_remember_table(eval_result)) {
795 current_serial = serial;
796 if (registered_functions()[serial].eval_use_exvector_args)
797 eval_result = ((eval_funcp_exvector)(registered_functions()[serial].eval_f))(seq);
799 switch (opt.nparams) {
800 // the following lines have been generated for max. ${maxargs} parameters
801 ${eval_switch_statement}
802 // end of generated lines
804 throw(std::logic_error("function::eval(): invalid nparams"));
807 store_remember_table(eval_result);
812 ex function::evalf(int level) const
814 GINAC_ASSERT(serial<registered_functions().size());
816 // Evaluate children first
820 else if (level == -max_recursion_level)
821 throw(std::runtime_error("max recursion level reached"));
823 eseq.reserve(seq.size());
825 exvector::const_iterator it = seq.begin(), itend = seq.end();
826 while (it != itend) {
827 eseq.push_back(it->evalf(level));
831 if (registered_functions()[serial].evalf_f==0) {
832 return function(serial,eseq).hold();
834 current_serial = serial;
835 if (registered_functions()[serial].evalf_use_exvector_args)
836 return ((evalf_funcp_exvector)(registered_functions()[serial].evalf_f))(seq);
837 switch (registered_functions()[serial].nparams) {
838 // the following lines have been generated for max. ${maxargs} parameters
839 ${evalf_switch_statement}
840 // end of generated lines
842 throw(std::logic_error("function::evalf(): invalid nparams"));
845 unsigned function::calchash(void) const
847 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
848 for (unsigned i=0; i<nops(); i++) {
849 v = rotate_left_31(v);
850 v ^= this->op(i).gethash();
853 if (flags & status_flags::evaluated) {
854 setflag(status_flags::hash_calculated);
860 ex function::thisexprseq(const exvector & v) const
862 return function(serial,v);
865 ex function::thisexprseq(exvector * vp) const
867 return function(serial,vp);
870 /** Implementation of ex::series for functions.
871 * \@see ex::series */
872 ex function::series(const relational & r, int order, unsigned options) const
874 GINAC_ASSERT(serial<registered_functions().size());
876 if (registered_functions()[serial].series_f==0) {
877 return basic::series(r, order);
880 current_serial = serial;
881 if (registered_functions()[serial].series_use_exvector_args) {
883 res = ((series_funcp_exvector)(registered_functions()[serial].series_f))(seq, r, order, options);
884 } catch (do_taylor) {
885 res = basic::series(r, order, options);
889 switch (registered_functions()[serial].nparams) {
890 // the following lines have been generated for max. ${maxargs} parameters
891 ${series_switch_statement}
892 // end of generated lines
894 throw(std::logic_error("function::series(): invalid nparams"));
899 /** Implementation of ex::diff() for functions. It applies the chain rule,
900 * except for the Order term function.
902 ex function::derivative(const symbol & s) const
906 if (serial == function_index_Order) {
907 // Order Term function only differentiates the argument
908 return Order(seq[0].diff(s));
912 unsigned num = seq.size();
913 for (unsigned i=0; i<num; i++) {
914 arg_diff = seq[i].diff(s);
915 // We apply the chain rule only when it makes sense. This is not
916 // just for performance reasons but also to allow functions to
917 // throw when differentiated with respect to one of its arguments
918 // without running into trouble with our automatic full
920 if (!arg_diff.is_zero())
921 result += pderivative(i)*arg_diff;
927 int function::compare_same_type(const basic & other) const
929 GINAC_ASSERT(is_of_type(other, function));
930 const function & o = static_cast<const function &>(other);
932 if (serial != o.serial)
933 return serial < o.serial ? -1 : 1;
935 return exprseq::compare_same_type(o);
938 bool function::is_equal_same_type(const basic & other) const
940 GINAC_ASSERT(is_of_type(other, function));
941 const function & o = static_cast<const function &>(other);
943 if (serial != o.serial)
946 return exprseq::is_equal_same_type(o);
949 bool function::match_same_type(const basic & other) const
951 GINAC_ASSERT(is_of_type(other, function));
952 const function & o = static_cast<const function &>(other);
954 return serial == o.serial;
957 unsigned function::return_type(void) const
960 return return_types::commutative;
962 return seq.begin()->return_type();
965 unsigned function::return_type_tinfo(void) const
970 return seq.begin()->return_type_tinfo();
974 // new virtual functions which can be overridden by derived classes
980 // non-virtual functions in this class
985 ex function::pderivative(unsigned diff_param) const // partial differentiation
987 GINAC_ASSERT(serial<registered_functions().size());
989 // No derivative defined? Then return abstract derivative object
990 if (registered_functions()[serial].derivative_f == NULL)
991 return fderivative(serial, diff_param, seq);
993 current_serial = serial;
994 if (registered_functions()[serial].derivative_use_exvector_args)
995 return ((derivative_funcp_exvector)(registered_functions()[serial].derivative_f))(seq, diff_param);
996 switch (registered_functions()[serial].nparams) {
997 // the following lines have been generated for max. ${maxargs} parameters
998 ${diff_switch_statement}
999 // end of generated lines
1001 throw(std::logic_error("function::pderivative(): no diff function defined"));
1004 std::vector<function_options> & function::registered_functions(void)
1006 static std::vector<function_options> * rf = new std::vector<function_options>;
1010 bool function::lookup_remember_table(ex & result) const
1012 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
1015 void function::store_remember_table(ex const & result) const
1017 remember_table::remember_tables()[serial].add_entry(*this,result);
1022 unsigned function::register_new(function_options const & opt)
1024 unsigned same_name = 0;
1025 for (unsigned i=0; i<registered_functions().size(); ++i) {
1026 if (registered_functions()[i].name==opt.name) {
1030 if (same_name>=opt.functions_with_same_name) {
1031 // we do not throw an exception here because this code is
1032 // usually executed before main(), so the exception could not
1034 std::cerr << "WARNING: function name " << opt.name
1035 << " already in use!" << std::endl;
1037 registered_functions().push_back(opt);
1038 if (opt.use_remember) {
1039 remember_table::remember_tables().
1040 push_back(remember_table(opt.remember_size,
1041 opt.remember_assoc_size,
1042 opt.remember_strategy));
1044 remember_table::remember_tables().push_back(remember_table());
1046 return registered_functions().size()-1;
1049 /** Find serial number of function by name and number of parameters.
1050 * Throws exception if function was not found. */
1051 unsigned function::find_function(const std::string &name, unsigned nparams)
1053 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1054 unsigned serial = 0;
1056 if (i->get_name() == name && i->get_nparams() == nparams)
1061 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1064 /** Return the print name of the function. */
1065 std::string function::get_name(void) const
1067 GINAC_ASSERT(serial<registered_functions().size());
1068 return registered_functions()[serial].name;
1071 } // namespace GiNaC
1073 END_OF_IMPLEMENTATION
1075 print "Creating interface file function.h...";
1076 open OUT,">function.h" or die "cannot open function.h";
1077 print OUT $interface;
1081 print "Creating implementation file function.cpp...";
1082 open OUT,">function.cpp" or die "cannot open function.cpp";
1083 print OUT $implementation;