1 # This perl script automatically generates function.h and function.cpp
3 # function.pl options: \$maxargs=${maxargs}
5 # GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 my ($seq_template,$n)=@_;
28 for ($N=1; $N<=$n; $N++) {
29 $res .= eval('"' . $seq_template . '"');
37 sub generate_from_to {
38 my ($template,$seq_template1,$seq_template2,$seq_template3,$from,$to)=@_;
42 for ($N=$from; $N<=$to; $N++) {
43 $SEQ1=generate_seq($seq_template1,$N);
44 $SEQ2=generate_seq($seq_template2,$N);
45 $SEQ3=generate_seq($seq_template3,$N);
46 $res .= eval('"' . $template . '"');
47 $SEQ1=''; # to avoid main::SEQ1 used only once warning
48 $SEQ2=''; # same as above
49 $SEQ3=''; # same as above
55 my ($template,$seq_template1,$seq_template2,$seq_template3)=@_;
56 return generate_from_to($template,$seq_template1,$seq_template2,$seq_template3,1,$maxargs);
59 $declare_function_macro = generate(
60 <<'END_OF_DECLARE_FUNCTION_MACRO','typename T${N}','const T${N} & p${N}','GiNaC::ex(p${N})');
61 #define DECLARE_FUNCTION_${N}P(NAME) \\
62 class NAME##_SERIAL { public: static unsigned serial; }; \\
63 template<${SEQ1}> const GiNaC::function NAME(${SEQ2}) { \\
64 return GiNaC::function(NAME##_SERIAL::serial, ${SEQ3}); \\
67 END_OF_DECLARE_FUNCTION_MACRO
69 $typedef_eval_funcp=generate(
70 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
73 $typedef_evalf_funcp=generate(
74 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
77 $typedef_conjugate_funcp=generate(
78 'typedef ex (* conjugate_funcp_${N})(${SEQ1});'."\n",
81 $typedef_derivative_funcp=generate(
82 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
85 $typedef_series_funcp=generate(
86 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
89 $typedef_print_funcp=generate(
90 'typedef void (* print_funcp_${N})(${SEQ1}, const print_context &);'."\n",
93 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
95 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
97 $conjugate_func_interface=generate(' function_options & conjugate_func(conjugate_funcp_${N} d);'."\n",'','','');
99 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
101 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','','');
103 $print_func_interface=generate(
104 <<'END_OF_PRINT_FUNC_INTERFACE','','','');
105 template <class Ctx> function_options & print_func(print_funcp_${N} p)
107 test_and_set_nparams(${N});
108 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
111 END_OF_PRINT_FUNC_INTERFACE
113 $constructors_interface=generate(
114 ' function(unsigned ser, ${SEQ1});'."\n",
115 'const ex & param${N}','','');
117 $constructors_implementation=generate(
118 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
119 function::function(unsigned ser, ${SEQ1})
120 : exprseq(${SEQ2}), serial(ser)
122 tinfo_key = TINFO_function;
124 END_OF_CONSTRUCTORS_IMPLEMENTATION
126 $eval_switch_statement=generate(
127 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
129 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
131 END_OF_EVAL_SWITCH_STATEMENT
133 $evalf_switch_statement=generate(
134 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
136 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
137 END_OF_EVALF_SWITCH_STATEMENT
139 $conjugate_switch_statement=generate(
140 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
142 return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
143 END_OF_DIFF_SWITCH_STATEMENT
145 $diff_switch_statement=generate(
146 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
148 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
149 END_OF_DIFF_SWITCH_STATEMENT
151 $series_switch_statement=generate(
152 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
155 res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
156 } catch (do_taylor) {
157 res = basic::series(r, order, options);
160 END_OF_SERIES_SWITCH_STATEMENT
162 $print_switch_statement=generate(
163 <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
165 ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
167 END_OF_PRINT_SWITCH_STATEMENT
169 $eval_func_implementation=generate(
170 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
171 function_options & function_options::eval_func(eval_funcp_${N} e)
173 test_and_set_nparams(${N});
174 eval_f = eval_funcp(e);
177 END_OF_EVAL_FUNC_IMPLEMENTATION
179 $evalf_func_implementation=generate(
180 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
181 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
183 test_and_set_nparams(${N});
184 evalf_f = evalf_funcp(ef);
187 END_OF_EVALF_FUNC_IMPLEMENTATION
189 $conjugate_func_implementation=generate(
190 <<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
191 function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
193 test_and_set_nparams(${N});
194 conjugate_f = conjugate_funcp(c);
197 END_OF_CONJUGATE_FUNC_IMPLEMENTATION
199 $derivative_func_implementation=generate(
200 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
201 function_options & function_options::derivative_func(derivative_funcp_${N} d)
203 test_and_set_nparams(${N});
204 derivative_f = derivative_funcp(d);
207 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
209 $series_func_implementation=generate(
210 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
211 function_options & function_options::series_func(series_funcp_${N} s)
213 test_and_set_nparams(${N});
214 series_f = series_funcp(s);
217 END_OF_SERIES_FUNC_IMPLEMENTATION
219 $interface=<<END_OF_INTERFACE;
220 /** \@file function.h
222 * Interface to class of symbolic functions. */
225 * This file was generated automatically by function.pl.
226 * Please do not modify it directly, edit the perl script instead!
227 * function.pl options: \$maxargs=${maxargs}
229 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
231 * This program is free software; you can redistribute it and/or modify
232 * it under the terms of the GNU General Public License as published by
233 * the Free Software Foundation; either version 2 of the License, or
234 * (at your option) any later version.
236 * This program is distributed in the hope that it will be useful,
237 * but WITHOUT ANY WARRANTY; without even the implied warranty of
238 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
239 * GNU General Public License for more details.
241 * You should have received a copy of the GNU General Public License
242 * along with this program; if not, write to the Free Software
243 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
246 #ifndef __GINAC_FUNCTION_H__
247 #define __GINAC_FUNCTION_H__
252 // CINT needs <algorithm> to work properly with <vector>
257 // the following lines have been generated for max. ${maxargs} parameters
258 $declare_function_macro
259 // end of generated lines
261 #define REGISTER_FUNCTION(NAME,OPT) \\
262 unsigned NAME##_SERIAL::serial = \\
263 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
270 typedef ex (* eval_funcp)();
271 typedef ex (* evalf_funcp)();
272 typedef ex (* conjugate_funcp)();
273 typedef ex (* derivative_funcp)();
274 typedef ex (* series_funcp)();
275 typedef void (* print_funcp)();
277 // the following lines have been generated for max. ${maxargs} parameters
280 $typedef_conjugate_funcp
281 $typedef_derivative_funcp
282 $typedef_series_funcp
284 // end of generated lines
286 // Alternatively, an exvector may be passed into the static function, instead
287 // of individual ex objects. Then, the number of arguments is not limited.
288 typedef ex (* eval_funcp_exvector)(const exvector &);
289 typedef ex (* evalf_funcp_exvector)(const exvector &);
290 typedef ex (* conjugate_funcp_exvector)(const exvector &);
291 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
292 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
293 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
296 class function_options
298 friend class function;
299 friend class fderivative;
302 function_options(std::string const & n, std::string const & tn=std::string());
306 function_options & dummy() { return *this; }
307 function_options & set_name(std::string const & n, std::string const & tn=std::string());
308 function_options & latex_name(std::string const & tn);
309 // the following lines have been generated for max. ${maxargs} parameters
311 $evalf_func_interface
312 $conjugate_func_interface
313 $derivative_func_interface
314 $series_func_interface
315 $print_func_interface
316 // end of generated lines
317 function_options & eval_func(eval_funcp_exvector e);
318 function_options & evalf_func(evalf_funcp_exvector ef);
319 function_options & conjugate_func(conjugate_funcp_exvector d);
320 function_options & derivative_func(derivative_funcp_exvector d);
321 function_options & series_func(series_funcp_exvector s);
323 template <class Ctx> function_options & print_func(print_funcp_exvector p)
325 print_use_exvector_args = true;
326 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
330 function_options & set_return_type(unsigned rt, unsigned rtt=0);
331 function_options & do_not_evalf_params();
332 function_options & remember(unsigned size, unsigned assoc_size=0,
333 unsigned strategy=remember_strategies::delete_never);
334 function_options & overloaded(unsigned o);
335 function_options & set_symmetry(const symmetry & s);
337 std::string get_name() const { return name; }
338 unsigned get_nparams() const { return nparams; }
341 bool has_derivative() const { return derivative_f != NULL; }
342 void test_and_set_nparams(unsigned n);
343 void set_print_func(unsigned id, print_funcp f);
346 std::string TeX_name;
352 conjugate_funcp conjugate_f;
353 derivative_funcp derivative_f;
354 series_funcp series_f;
355 std::vector<print_funcp> print_dispatch_table;
357 bool evalf_params_first;
359 bool use_return_type;
360 unsigned return_type;
361 unsigned return_type_tinfo;
364 unsigned remember_size;
365 unsigned remember_assoc_size;
366 unsigned remember_strategy;
368 bool eval_use_exvector_args;
369 bool evalf_use_exvector_args;
370 bool conjugate_use_exvector_args;
371 bool derivative_use_exvector_args;
372 bool series_use_exvector_args;
373 bool print_use_exvector_args;
375 unsigned functions_with_same_name;
381 /** Exception class thrown by classes which provide their own series expansion
382 * to signal that ordinary Taylor expansion is safe. */
386 /** The class function is used to implement builtin functions like sin, cos...
387 and user defined functions */
388 class function : public exprseq
390 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
392 // CINT has a linking problem
394 friend void ginsh_get_ginac_functions();
395 #endif // def __MAKECINT__
397 friend class remember_table_entry;
398 // friend class remember_table_list;
399 // friend class remember_table;
403 // other constructors
405 function(unsigned ser);
406 // the following lines have been generated for max. ${maxargs} parameters
407 $constructors_interface
408 // end of generated lines
409 function(unsigned ser, const exprseq & es);
410 function(unsigned ser, const exvector & v, bool discardable = false);
411 function(unsigned ser, std::auto_ptr<exvector> vp);
413 // functions overriding virtual functions from base classes
415 void print(const print_context & c, unsigned level = 0) const;
416 unsigned precedence() const {return 70;}
417 ex expand(unsigned options=0) const;
418 ex eval(int level=0) const;
419 ex evalf(int level=0) const;
420 unsigned calchash() const;
421 ex series(const relational & r, int order, unsigned options = 0) const;
422 ex thiscontainer(const exvector & v) const;
423 ex thiscontainer(std::auto_ptr<exvector> vp) const;
424 ex conjugate() const;
426 ex derivative(const symbol & s) const;
427 bool is_equal_same_type(const basic & other) const;
428 bool match_same_type(const basic & other) const;
429 unsigned return_type() const;
430 unsigned return_type_tinfo() const;
432 // new virtual functions which can be overridden by derived classes
435 // non-virtual functions in this class
437 ex pderivative(unsigned diff_param) const; // partial differentiation
438 static std::vector<function_options> & registered_functions();
439 bool lookup_remember_table(ex & result) const;
440 void store_remember_table(ex const & result) const;
442 static unsigned register_new(function_options const & opt);
443 static unsigned current_serial;
444 static unsigned find_function(const std::string &name, unsigned nparams);
445 unsigned get_serial() const {return serial;}
446 std::string get_name() const;
454 // utility functions/macros
456 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
457 template<> inline bool is_exactly_a<function>(const basic & obj)
459 return obj.tinfo()==TINFO_function;
462 template <typename T>
463 inline bool is_the_function(const ex & x)
465 return is_exactly_a<function>(x)
466 && ex_to<function>(x).get_serial() == T::serial;
469 // Check whether OBJ is the specified symbolic function.
470 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
474 #endif // ndef __GINAC_FUNCTION_H__
478 $implementation=<<END_OF_IMPLEMENTATION;
479 /** \@file function.cpp
481 * Implementation of class of symbolic functions. */
484 * This file was generated automatically by function.pl.
485 * Please do not modify it directly, edit the perl script instead!
486 * function.pl options: \$maxargs=${maxargs}
488 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
490 * This program is free software; you can redistribute it and/or modify
491 * it under the terms of the GNU General Public License as published by
492 * the Free Software Foundation; either version 2 of the License, or
493 * (at your option) any later version.
495 * This program is distributed in the hope that it will be useful,
496 * but WITHOUT ANY WARRANTY; without even the implied warranty of
497 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
498 * GNU General Public License for more details.
500 * You should have received a copy of the GNU General Public License
501 * along with this program; if not, write to the Free Software
502 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
510 #include "function.h"
511 #include "operators.h"
512 #include "fderivative.h"
515 #include "symmetry.h"
519 #include "tostring.h"
521 #include "remember.h"
526 // helper class function_options
529 function_options::function_options()
534 function_options::function_options(std::string const & n, std::string const & tn)
540 function_options::~function_options()
542 // nothing to clean up at the moment
545 void function_options::initialize()
547 set_name("unnamed_function", "\\\\mbox{unnamed}");
549 eval_f = evalf_f = conjugate_f = derivative_f = series_f = 0;
550 evalf_params_first = true;
551 use_return_type = false;
552 eval_use_exvector_args = false;
553 evalf_use_exvector_args = false;
554 conjugate_use_exvector_args = false;
555 derivative_use_exvector_args = false;
556 series_use_exvector_args = false;
557 print_use_exvector_args = false;
558 use_remember = false;
559 functions_with_same_name = 1;
562 function_options & function_options::set_name(std::string const & n,
563 std::string const & tn)
566 if (tn==std::string())
567 TeX_name = "\\\\mbox{"+name+"}";
573 function_options & function_options::latex_name(std::string const & tn)
579 // the following lines have been generated for max. ${maxargs} parameters
580 $eval_func_implementation
581 $evalf_func_implementation
582 $conjugate_func_implementation
583 $derivative_func_implementation
584 $series_func_implementation
585 // end of generated lines
587 function_options& function_options::eval_func(eval_funcp_exvector e)
589 eval_use_exvector_args = true;
590 eval_f = eval_funcp(e);
593 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
595 evalf_use_exvector_args = true;
596 evalf_f = evalf_funcp(ef);
599 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
601 conjugate_use_exvector_args = true;
602 conjugate_f = conjugate_funcp(c);
605 function_options& function_options::derivative_func(derivative_funcp_exvector d)
607 derivative_use_exvector_args = true;
608 derivative_f = derivative_funcp(d);
611 function_options& function_options::series_func(series_funcp_exvector s)
613 series_use_exvector_args = true;
614 series_f = series_funcp(s);
618 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
620 use_return_type = true;
622 return_type_tinfo = rtt;
626 function_options & function_options::do_not_evalf_params()
628 evalf_params_first = false;
632 function_options & function_options::remember(unsigned size,
637 remember_size = size;
638 remember_assoc_size = assoc_size;
639 remember_strategy = strategy;
643 function_options & function_options::overloaded(unsigned o)
645 functions_with_same_name = o;
649 function_options & function_options::set_symmetry(const symmetry & s)
655 void function_options::test_and_set_nparams(unsigned n)
659 } else if (nparams!=n) {
660 // we do not throw an exception here because this code is
661 // usually executed before main(), so the exception could not
663 std::cerr << "WARNING: " << name << "(): number of parameters ("
664 << n << ") differs from number set before ("
665 << nparams << ")" << std::endl;
669 void function_options::set_print_func(unsigned id, print_funcp f)
671 if (id >= print_dispatch_table.size())
672 print_dispatch_table.resize(id + 1);
673 print_dispatch_table[id] = f;
676 /** This can be used as a hook for external applications. */
677 unsigned function::current_serial = 0;
680 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
683 // default constructor
688 function::function() : serial(0)
690 tinfo_key = TINFO_function;
694 // other constructors
699 function::function(unsigned ser) : serial(ser)
701 tinfo_key = TINFO_function;
704 // the following lines have been generated for max. ${maxargs} parameters
705 $constructors_implementation
706 // end of generated lines
708 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
710 tinfo_key = TINFO_function;
712 // Force re-evaluation even if the exprseq was already evaluated
713 // (the exprseq copy constructor copies the flags)
714 clearflag(status_flags::evaluated);
717 function::function(unsigned ser, const exvector & v, bool discardable)
718 : exprseq(v,discardable), serial(ser)
720 tinfo_key = TINFO_function;
723 function::function(unsigned ser, std::auto_ptr<exvector> vp)
724 : exprseq(vp), serial(ser)
726 tinfo_key = TINFO_function;
733 /** Construct object from archive_node. */
734 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
736 // Find serial number by function name
738 if (n.find_string("name", s)) {
739 unsigned int ser = 0;
740 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
748 throw (std::runtime_error("unknown function '" + s + "' in archive"));
750 throw (std::runtime_error("unnamed function in archive"));
753 /** Unarchive the object. */
754 ex function::unarchive(const archive_node &n, lst &sym_lst)
756 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
759 /** Archive the object. */
760 void function::archive(archive_node &n) const
762 inherited::archive(n);
763 GINAC_ASSERT(serial < registered_functions().size());
764 n.add_string("name", registered_functions()[serial].name);
768 // functions overriding virtual functions from base classes
773 void function::print(const print_context & c, unsigned level) const
775 GINAC_ASSERT(serial<registered_functions().size());
776 const function_options &opt = registered_functions()[serial];
777 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
779 // Dynamically dispatch on print_context type
780 const print_context_class_info *pc_info = &c.get_class_info();
783 unsigned id = pc_info->options.get_id();
784 if (id >= pdt.size() || pdt[id] == NULL) {
786 // Method not found, try parent print_context class
787 const print_context_class_info *parent_pc_info = pc_info->get_parent();
788 if (parent_pc_info) {
789 pc_info = parent_pc_info;
793 // Method still not found, use default output
794 if (is_a<print_tree>(c)) {
796 c.s << std::string(level, ' ') << class_name() << " "
797 << opt.name << " @" << this
798 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
799 << ", nops=" << nops()
801 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
802 for (size_t i=0; i<seq.size(); ++i)
803 seq[i].print(c, level + delta_indent);
804 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
806 } else if (is_a<print_csrc>(c)) {
808 // Print function name in lowercase
809 std::string lname = opt.name;
810 size_t num = lname.size();
811 for (size_t i=0; i<num; i++)
812 lname[i] = tolower(lname[i]);
814 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
816 } else if (is_a<print_latex>(c)) {
818 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
821 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
826 // Method found, call it
827 current_serial = serial;
828 if (opt.print_use_exvector_args)
829 ((print_funcp_exvector)pdt[id])(seq, c);
830 else switch (opt.nparams) {
831 // the following lines have been generated for max. ${maxargs} parameters
832 ${print_switch_statement}
833 // end of generated lines
835 throw(std::logic_error("function::print(): invalid nparams"));
840 ex function::expand(unsigned options) const
842 // Only expand arguments when asked to do so
843 if (options & expand_options::expand_function_args)
844 return inherited::expand(options);
846 return (options == 0) ? setflag(status_flags::expanded) : *this;
849 ex function::eval(int level) const
852 // first evaluate children, then we will end up here again
853 return function(serial,evalchildren(level));
856 GINAC_ASSERT(serial<registered_functions().size());
857 const function_options &opt = registered_functions()[serial];
859 // Canonicalize argument order according to the symmetry properties
860 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
862 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
863 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
864 if (sig != INT_MAX) {
865 // Something has changed while sorting arguments, more evaluations later
868 return ex(sig) * thiscontainer(v);
876 bool use_remember = opt.use_remember;
878 if (use_remember && lookup_remember_table(eval_result)) {
881 current_serial = serial;
882 if (opt.eval_use_exvector_args)
883 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
885 switch (opt.nparams) {
886 // the following lines have been generated for max. ${maxargs} parameters
887 ${eval_switch_statement}
888 // end of generated lines
890 throw(std::logic_error("function::eval(): invalid nparams"));
893 store_remember_table(eval_result);
898 ex function::evalf(int level) const
900 GINAC_ASSERT(serial<registered_functions().size());
901 const function_options &opt = registered_functions()[serial];
903 // Evaluate children first
905 if (level == 1 || !(opt.evalf_params_first))
907 else if (level == -max_recursion_level)
908 throw(std::runtime_error("max recursion level reached"));
910 eseq.reserve(seq.size());
912 exvector::const_iterator it = seq.begin(), itend = seq.end();
913 while (it != itend) {
914 eseq.push_back(it->evalf(level));
919 if (opt.evalf_f==0) {
920 return function(serial,eseq).hold();
922 current_serial = serial;
923 if (opt.evalf_use_exvector_args)
924 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
925 switch (opt.nparams) {
926 // the following lines have been generated for max. ${maxargs} parameters
927 ${evalf_switch_statement}
928 // end of generated lines
930 throw(std::logic_error("function::evalf(): invalid nparams"));
933 unsigned function::calchash() const
935 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
936 for (size_t i=0; i<nops(); i++) {
938 v ^= this->op(i).gethash();
941 if (flags & status_flags::evaluated) {
942 setflag(status_flags::hash_calculated);
948 ex function::thiscontainer(const exvector & v) const
950 return function(serial, v);
953 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
955 return function(serial, vp);
958 /** Implementation of ex::series for functions.
959 * \@see ex::series */
960 ex function::series(const relational & r, int order, unsigned options) const
962 GINAC_ASSERT(serial<registered_functions().size());
963 const function_options &opt = registered_functions()[serial];
965 if (opt.series_f==0) {
966 return basic::series(r, order);
969 current_serial = serial;
970 if (opt.series_use_exvector_args) {
972 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
973 } catch (do_taylor) {
974 res = basic::series(r, order, options);
978 switch (opt.nparams) {
979 // the following lines have been generated for max. ${maxargs} parameters
980 ${series_switch_statement}
981 // end of generated lines
983 throw(std::logic_error("function::series(): invalid nparams"));
986 /** Implementation of ex::conjugate for functions. */
987 ex function::conjugate() const
989 GINAC_ASSERT(serial<registered_functions().size());
990 const function_options & opt = registered_functions()[serial];
992 if (opt.conjugate_f==0) {
993 return exprseq::conjugate();
996 if (opt.conjugate_use_exvector_args) {
997 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
1000 switch (opt.nparams) {
1001 // the following lines have been generated for max. ${maxargs} parameters
1002 ${conjugate_switch_statement}
1003 // end of generated lines
1005 throw(std::logic_error("function::conjugate(): invalid nparams"));
1010 /** Implementation of ex::diff() for functions. It applies the chain rule,
1011 * except for the Order term function.
1013 ex function::derivative(const symbol & s) const
1017 if (serial == Order_SERIAL::serial) {
1018 // Order Term function only differentiates the argument
1019 return Order(seq[0].diff(s));
1023 size_t num = seq.size();
1024 for (size_t i=0; i<num; i++) {
1025 arg_diff = seq[i].diff(s);
1026 // We apply the chain rule only when it makes sense. This is not
1027 // just for performance reasons but also to allow functions to
1028 // throw when differentiated with respect to one of its arguments
1029 // without running into trouble with our automatic full
1031 if (!arg_diff.is_zero())
1032 result += pderivative(i)*arg_diff;
1038 int function::compare_same_type(const basic & other) const
1040 GINAC_ASSERT(is_a<function>(other));
1041 const function & o = static_cast<const function &>(other);
1043 if (serial != o.serial)
1044 return serial < o.serial ? -1 : 1;
1046 return exprseq::compare_same_type(o);
1049 bool function::is_equal_same_type(const basic & other) const
1051 GINAC_ASSERT(is_a<function>(other));
1052 const function & o = static_cast<const function &>(other);
1054 if (serial != o.serial)
1057 return exprseq::is_equal_same_type(o);
1060 bool function::match_same_type(const basic & other) const
1062 GINAC_ASSERT(is_a<function>(other));
1063 const function & o = static_cast<const function &>(other);
1065 return serial == o.serial;
1068 unsigned function::return_type() const
1070 GINAC_ASSERT(serial<registered_functions().size());
1071 const function_options &opt = registered_functions()[serial];
1073 if (opt.use_return_type) {
1074 // Return type was explicitly specified
1075 return opt.return_type;
1077 // Default behavior is to use the return type of the first
1078 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1080 return return_types::commutative;
1082 return seq.begin()->return_type();
1086 unsigned function::return_type_tinfo() const
1088 GINAC_ASSERT(serial<registered_functions().size());
1089 const function_options &opt = registered_functions()[serial];
1091 if (opt.use_return_type) {
1092 // Return type was explicitly specified
1093 return opt.return_type_tinfo;
1095 // Default behavior is to use the return type of the first
1096 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1100 return seq.begin()->return_type_tinfo();
1105 // new virtual functions which can be overridden by derived classes
1111 // non-virtual functions in this class
1116 ex function::pderivative(unsigned diff_param) const // partial differentiation
1118 GINAC_ASSERT(serial<registered_functions().size());
1119 const function_options &opt = registered_functions()[serial];
1121 // No derivative defined? Then return abstract derivative object
1122 if (opt.derivative_f == NULL)
1123 return fderivative(serial, diff_param, seq);
1125 current_serial = serial;
1126 if (opt.derivative_use_exvector_args)
1127 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1128 switch (opt.nparams) {
1129 // the following lines have been generated for max. ${maxargs} parameters
1130 ${diff_switch_statement}
1131 // end of generated lines
1133 throw(std::logic_error("function::pderivative(): no diff function defined"));
1136 std::vector<function_options> & function::registered_functions()
1138 static std::vector<function_options> * rf = new std::vector<function_options>;
1142 bool function::lookup_remember_table(ex & result) const
1144 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1147 void function::store_remember_table(ex const & result) const
1149 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1154 unsigned function::register_new(function_options const & opt)
1156 size_t same_name = 0;
1157 for (size_t i=0; i<registered_functions().size(); ++i) {
1158 if (registered_functions()[i].name==opt.name) {
1162 if (same_name>=opt.functions_with_same_name) {
1163 // we do not throw an exception here because this code is
1164 // usually executed before main(), so the exception could not
1166 std::cerr << "WARNING: function name " << opt.name
1167 << " already in use!" << std::endl;
1169 registered_functions().push_back(opt);
1170 if (opt.use_remember) {
1171 remember_table::remember_tables().
1172 push_back(remember_table(opt.remember_size,
1173 opt.remember_assoc_size,
1174 opt.remember_strategy));
1176 remember_table::remember_tables().push_back(remember_table());
1178 return registered_functions().size()-1;
1181 /** Find serial number of function by name and number of parameters.
1182 * Throws exception if function was not found. */
1183 unsigned function::find_function(const std::string &name, unsigned nparams)
1185 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1186 unsigned serial = 0;
1188 if (i->get_name() == name && i->get_nparams() == nparams)
1193 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1196 /** Return the print name of the function. */
1197 std::string function::get_name() const
1199 GINAC_ASSERT(serial<registered_functions().size());
1200 return registered_functions()[serial].name;
1203 } // namespace GiNaC
1205 END_OF_IMPLEMENTATION
1207 print "Creating interface file function.h...";
1208 open OUT,">function.h" or die "cannot open function.h";
1209 print OUT $interface;
1213 print "Creating implementation file function.cpp...";
1214 open OUT,">function.cpp" or die "cannot open function.cpp";
1215 print OUT $implementation;