1 # This perl script automatically generates function.h and function.cpp
3 # function.pl options: \$maxargs=${maxargs}
5 # GiNaC Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 const unsigned NAME##_NPARAMS = ${N}; \\
64 template<${SEQ1}> const GiNaC::function NAME(${SEQ2}) { \\
65 return GiNaC::function(NAME##_SERIAL::serial, ${SEQ3}); \\
68 END_OF_DECLARE_FUNCTION_MACRO
70 $typedef_eval_funcp=generate(
71 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
74 $typedef_evalf_funcp=generate(
75 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
78 $typedef_conjugate_funcp=generate(
79 'typedef ex (* conjugate_funcp_${N})(${SEQ1});'."\n",
82 $typedef_derivative_funcp=generate(
83 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
86 $typedef_series_funcp=generate(
87 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
90 $typedef_print_funcp=generate(
91 'typedef void (* print_funcp_${N})(${SEQ1}, const print_context &);'."\n",
94 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
96 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
98 $conjugate_func_interface=generate(' function_options & conjugate_func(conjugate_funcp_${N} d);'."\n",'','','');
100 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
102 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','','');
104 $print_func_interface=generate(
105 <<'END_OF_PRINT_FUNC_INTERFACE','','','');
106 template <class Ctx> function_options & print_func(print_funcp_${N} p)
108 test_and_set_nparams(${N});
109 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
112 END_OF_PRINT_FUNC_INTERFACE
114 $constructors_interface=generate(
115 ' function(unsigned ser, ${SEQ1});'."\n",
116 'const ex & param${N}','','');
118 $constructors_implementation=generate(
119 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
120 function::function(unsigned ser, ${SEQ1})
121 : exprseq(${SEQ2}), serial(ser)
123 tinfo_key = TINFO_function;
125 END_OF_CONSTRUCTORS_IMPLEMENTATION
127 $eval_switch_statement=generate(
128 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
130 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
132 END_OF_EVAL_SWITCH_STATEMENT
134 $evalf_switch_statement=generate(
135 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
137 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
138 END_OF_EVALF_SWITCH_STATEMENT
140 $conjugate_switch_statement=generate(
141 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
143 return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
144 END_OF_DIFF_SWITCH_STATEMENT
146 $diff_switch_statement=generate(
147 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
149 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
150 END_OF_DIFF_SWITCH_STATEMENT
152 $series_switch_statement=generate(
153 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
156 res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
157 } catch (do_taylor) {
158 res = basic::series(r, order, options);
161 END_OF_SERIES_SWITCH_STATEMENT
163 $print_switch_statement=generate(
164 <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
166 ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
168 END_OF_PRINT_SWITCH_STATEMENT
170 $eval_func_implementation=generate(
171 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
172 function_options & function_options::eval_func(eval_funcp_${N} e)
174 test_and_set_nparams(${N});
175 eval_f = eval_funcp(e);
178 END_OF_EVAL_FUNC_IMPLEMENTATION
180 $evalf_func_implementation=generate(
181 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
182 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
184 test_and_set_nparams(${N});
185 evalf_f = evalf_funcp(ef);
188 END_OF_EVALF_FUNC_IMPLEMENTATION
190 $conjugate_func_implementation=generate(
191 <<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
192 function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
194 test_and_set_nparams(${N});
195 conjugate_f = conjugate_funcp(c);
198 END_OF_CONJUGATE_FUNC_IMPLEMENTATION
200 $derivative_func_implementation=generate(
201 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
202 function_options & function_options::derivative_func(derivative_funcp_${N} d)
204 test_and_set_nparams(${N});
205 derivative_f = derivative_funcp(d);
208 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
210 $series_func_implementation=generate(
211 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
212 function_options & function_options::series_func(series_funcp_${N} s)
214 test_and_set_nparams(${N});
215 series_f = series_funcp(s);
218 END_OF_SERIES_FUNC_IMPLEMENTATION
220 $interface=<<END_OF_INTERFACE;
221 /** \@file function.h
223 * Interface to class of symbolic functions. */
226 * This file was generated automatically by function.pl.
227 * Please do not modify it directly, edit the perl script instead!
228 * function.pl options: \$maxargs=${maxargs}
230 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
232 * This program is free software; you can redistribute it and/or modify
233 * it under the terms of the GNU General Public License as published by
234 * the Free Software Foundation; either version 2 of the License, or
235 * (at your option) any later version.
237 * This program is distributed in the hope that it will be useful,
238 * but WITHOUT ANY WARRANTY; without even the implied warranty of
239 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
240 * GNU General Public License for more details.
242 * You should have received a copy of the GNU General Public License
243 * along with this program; if not, write to the Free Software
244 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
247 #ifndef __GINAC_FUNCTION_H__
248 #define __GINAC_FUNCTION_H__
253 // CINT needs <algorithm> to work properly with <vector>
258 // the following lines have been generated for max. ${maxargs} parameters
259 $declare_function_macro
260 // end of generated lines
262 #define REGISTER_FUNCTION(NAME,OPT) \\
263 unsigned NAME##_SERIAL::serial = \\
264 GiNaC::function::register_new(GiNaC::function_options(#NAME, NAME##_NPARAMS).OPT);
271 typedef ex (* eval_funcp)();
272 typedef ex (* evalf_funcp)();
273 typedef ex (* conjugate_funcp)();
274 typedef ex (* derivative_funcp)();
275 typedef ex (* series_funcp)();
276 typedef void (* print_funcp)();
278 // the following lines have been generated for max. ${maxargs} parameters
281 $typedef_conjugate_funcp
282 $typedef_derivative_funcp
283 $typedef_series_funcp
285 // end of generated lines
287 // Alternatively, an exvector may be passed into the static function, instead
288 // of individual ex objects. Then, the number of arguments is not limited.
289 typedef ex (* eval_funcp_exvector)(const exvector &);
290 typedef ex (* evalf_funcp_exvector)(const exvector &);
291 typedef ex (* conjugate_funcp_exvector)(const exvector &);
292 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
293 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
294 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
297 class function_options
299 friend class function;
300 friend class fderivative;
303 function_options(std::string const & n, std::string const & tn=std::string());
304 function_options(std::string const & n, unsigned np);
308 function_options & dummy() { return *this; }
309 function_options & set_name(std::string const & n, std::string const & tn=std::string());
310 function_options & latex_name(std::string const & tn);
311 // the following lines have been generated for max. ${maxargs} parameters
313 $evalf_func_interface
314 $conjugate_func_interface
315 $derivative_func_interface
316 $series_func_interface
317 $print_func_interface
318 // end of generated lines
319 function_options & eval_func(eval_funcp_exvector e);
320 function_options & evalf_func(evalf_funcp_exvector ef);
321 function_options & conjugate_func(conjugate_funcp_exvector d);
322 function_options & derivative_func(derivative_funcp_exvector d);
323 function_options & series_func(series_funcp_exvector s);
325 template <class Ctx> function_options & print_func(print_funcp_exvector p)
327 print_use_exvector_args = true;
328 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
332 function_options & set_return_type(unsigned rt, unsigned rtt=0);
333 function_options & do_not_evalf_params();
334 function_options & remember(unsigned size, unsigned assoc_size=0,
335 unsigned strategy=remember_strategies::delete_never);
336 function_options & overloaded(unsigned o);
337 function_options & set_symmetry(const symmetry & s);
339 std::string get_name() const { return name; }
340 unsigned get_nparams() const { return nparams; }
343 bool has_derivative() const { return derivative_f != NULL; }
344 void test_and_set_nparams(unsigned n);
345 void set_print_func(unsigned id, print_funcp f);
348 std::string TeX_name;
354 conjugate_funcp conjugate_f;
355 derivative_funcp derivative_f;
356 series_funcp series_f;
357 std::vector<print_funcp> print_dispatch_table;
359 bool evalf_params_first;
361 bool use_return_type;
362 unsigned return_type;
363 unsigned return_type_tinfo;
366 unsigned remember_size;
367 unsigned remember_assoc_size;
368 unsigned remember_strategy;
370 bool eval_use_exvector_args;
371 bool evalf_use_exvector_args;
372 bool conjugate_use_exvector_args;
373 bool derivative_use_exvector_args;
374 bool series_use_exvector_args;
375 bool print_use_exvector_args;
377 unsigned functions_with_same_name;
383 /** Exception class thrown by classes which provide their own series expansion
384 * to signal that ordinary Taylor expansion is safe. */
388 /** The class function is used to implement builtin functions like sin, cos...
389 and user defined functions */
390 class function : public exprseq
392 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
394 // CINT has a linking problem
396 friend void ginsh_get_ginac_functions();
397 #endif // def __MAKECINT__
399 friend class remember_table_entry;
400 // friend class remember_table_list;
401 // friend class remember_table;
405 // other constructors
407 function(unsigned ser);
408 // the following lines have been generated for max. ${maxargs} parameters
409 $constructors_interface
410 // end of generated lines
411 function(unsigned ser, const exprseq & es);
412 function(unsigned ser, const exvector & v, bool discardable = false);
413 function(unsigned ser, std::auto_ptr<exvector> vp);
415 // functions overriding virtual functions from base classes
417 void print(const print_context & c, unsigned level = 0) const;
418 unsigned precedence() const {return 70;}
419 ex expand(unsigned options=0) const;
420 ex eval(int level=0) const;
421 ex evalf(int level=0) const;
422 unsigned calchash() const;
423 ex series(const relational & r, int order, unsigned options = 0) const;
424 ex thiscontainer(const exvector & v) const;
425 ex thiscontainer(std::auto_ptr<exvector> vp) const;
426 ex conjugate() const;
428 ex derivative(const symbol & s) const;
429 bool is_equal_same_type(const basic & other) const;
430 bool match_same_type(const basic & other) const;
431 unsigned return_type() const;
432 unsigned return_type_tinfo() const;
434 // new virtual functions which can be overridden by derived classes
437 // non-virtual functions in this class
439 ex pderivative(unsigned diff_param) const; // partial differentiation
440 static std::vector<function_options> & registered_functions();
441 bool lookup_remember_table(ex & result) const;
442 void store_remember_table(ex const & result) const;
444 static unsigned register_new(function_options const & opt);
445 static unsigned current_serial;
446 static unsigned find_function(const std::string &name, unsigned nparams);
447 unsigned get_serial() const {return serial;}
448 std::string get_name() const;
456 // utility functions/macros
458 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
459 template<> inline bool is_exactly_a<function>(const basic & obj)
461 return obj.tinfo()==TINFO_function;
464 template <typename T>
465 inline bool is_the_function(const ex & x)
467 return is_exactly_a<function>(x)
468 && ex_to<function>(x).get_serial() == T::serial;
471 // Check whether OBJ is the specified symbolic function.
472 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
476 #endif // ndef __GINAC_FUNCTION_H__
480 $implementation=<<END_OF_IMPLEMENTATION;
481 /** \@file function.cpp
483 * Implementation of class of symbolic functions. */
486 * This file was generated automatically by function.pl.
487 * Please do not modify it directly, edit the perl script instead!
488 * function.pl options: \$maxargs=${maxargs}
490 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
492 * This program is free software; you can redistribute it and/or modify
493 * it under the terms of the GNU General Public License as published by
494 * the Free Software Foundation; either version 2 of the License, or
495 * (at your option) any later version.
497 * This program is distributed in the hope that it will be useful,
498 * but WITHOUT ANY WARRANTY; without even the implied warranty of
499 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
500 * GNU General Public License for more details.
502 * You should have received a copy of the GNU General Public License
503 * along with this program; if not, write to the Free Software
504 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
512 #include "function.h"
513 #include "operators.h"
514 #include "fderivative.h"
517 #include "symmetry.h"
521 #include "tostring.h"
523 #include "remember.h"
528 // helper class function_options
531 function_options::function_options()
536 function_options::function_options(std::string const & n, std::string const & tn)
542 function_options::function_options(std::string const & n, unsigned np)
545 set_name(n, std::string());
549 function_options::~function_options()
551 // nothing to clean up at the moment
554 void function_options::initialize()
556 set_name("unnamed_function", "\\\\mbox{unnamed}");
558 eval_f = evalf_f = conjugate_f = derivative_f = series_f = 0;
559 evalf_params_first = true;
560 use_return_type = false;
561 eval_use_exvector_args = false;
562 evalf_use_exvector_args = false;
563 conjugate_use_exvector_args = false;
564 derivative_use_exvector_args = false;
565 series_use_exvector_args = false;
566 print_use_exvector_args = false;
567 use_remember = false;
568 functions_with_same_name = 1;
572 function_options & function_options::set_name(std::string const & n,
573 std::string const & tn)
576 if (tn==std::string())
577 TeX_name = "\\\\mbox{"+name+"}";
583 function_options & function_options::latex_name(std::string const & tn)
589 // the following lines have been generated for max. ${maxargs} parameters
590 $eval_func_implementation
591 $evalf_func_implementation
592 $conjugate_func_implementation
593 $derivative_func_implementation
594 $series_func_implementation
595 // end of generated lines
597 function_options& function_options::eval_func(eval_funcp_exvector e)
599 eval_use_exvector_args = true;
600 eval_f = eval_funcp(e);
603 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
605 evalf_use_exvector_args = true;
606 evalf_f = evalf_funcp(ef);
609 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
611 conjugate_use_exvector_args = true;
612 conjugate_f = conjugate_funcp(c);
615 function_options& function_options::derivative_func(derivative_funcp_exvector d)
617 derivative_use_exvector_args = true;
618 derivative_f = derivative_funcp(d);
621 function_options& function_options::series_func(series_funcp_exvector s)
623 series_use_exvector_args = true;
624 series_f = series_funcp(s);
628 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
630 use_return_type = true;
632 return_type_tinfo = rtt;
636 function_options & function_options::do_not_evalf_params()
638 evalf_params_first = false;
642 function_options & function_options::remember(unsigned size,
647 remember_size = size;
648 remember_assoc_size = assoc_size;
649 remember_strategy = strategy;
653 function_options & function_options::overloaded(unsigned o)
655 functions_with_same_name = o;
659 function_options & function_options::set_symmetry(const symmetry & s)
665 void function_options::test_and_set_nparams(unsigned n)
669 } else if (nparams!=n) {
670 // we do not throw an exception here because this code is
671 // usually executed before main(), so the exception could not
673 std::cerr << "WARNING: " << name << "(): number of parameters ("
674 << n << ") differs from number set before ("
675 << nparams << ")" << std::endl;
679 void function_options::set_print_func(unsigned id, print_funcp f)
681 if (id >= print_dispatch_table.size())
682 print_dispatch_table.resize(id + 1);
683 print_dispatch_table[id] = f;
686 /** This can be used as a hook for external applications. */
687 unsigned function::current_serial = 0;
690 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
693 // default constructor
698 function::function() : serial(0)
700 tinfo_key = TINFO_function;
704 // other constructors
709 function::function(unsigned ser) : serial(ser)
711 tinfo_key = TINFO_function;
714 // the following lines have been generated for max. ${maxargs} parameters
715 $constructors_implementation
716 // end of generated lines
718 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
720 tinfo_key = TINFO_function;
722 // Force re-evaluation even if the exprseq was already evaluated
723 // (the exprseq copy constructor copies the flags)
724 clearflag(status_flags::evaluated);
727 function::function(unsigned ser, const exvector & v, bool discardable)
728 : exprseq(v,discardable), serial(ser)
730 tinfo_key = TINFO_function;
733 function::function(unsigned ser, std::auto_ptr<exvector> vp)
734 : exprseq(vp), serial(ser)
736 tinfo_key = TINFO_function;
743 /** Construct object from archive_node. */
744 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
746 // Find serial number by function name
748 if (n.find_string("name", s)) {
749 unsigned int ser = 0;
750 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
758 throw (std::runtime_error("unknown function '" + s + "' in archive"));
760 throw (std::runtime_error("unnamed function in archive"));
763 /** Unarchive the object. */
764 ex function::unarchive(const archive_node &n, lst &sym_lst)
766 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
769 /** Archive the object. */
770 void function::archive(archive_node &n) const
772 inherited::archive(n);
773 GINAC_ASSERT(serial < registered_functions().size());
774 n.add_string("name", registered_functions()[serial].name);
778 // functions overriding virtual functions from base classes
783 void function::print(const print_context & c, unsigned level) const
785 GINAC_ASSERT(serial<registered_functions().size());
786 const function_options &opt = registered_functions()[serial];
787 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
789 // Dynamically dispatch on print_context type
790 const print_context_class_info *pc_info = &c.get_class_info();
793 unsigned id = pc_info->options.get_id();
794 if (id >= pdt.size() || pdt[id] == NULL) {
796 // Method not found, try parent print_context class
797 const print_context_class_info *parent_pc_info = pc_info->get_parent();
798 if (parent_pc_info) {
799 pc_info = parent_pc_info;
803 // Method still not found, use default output
804 if (is_a<print_tree>(c)) {
806 c.s << std::string(level, ' ') << class_name() << " "
807 << opt.name << " @" << this
808 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
809 << ", nops=" << nops()
811 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
812 for (size_t i=0; i<seq.size(); ++i)
813 seq[i].print(c, level + delta_indent);
814 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
816 } else if (is_a<print_csrc>(c)) {
818 // Print function name in lowercase
819 std::string lname = opt.name;
820 size_t num = lname.size();
821 for (size_t i=0; i<num; i++)
822 lname[i] = tolower(lname[i]);
824 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
826 } else if (is_a<print_latex>(c)) {
828 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
831 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
836 // Method found, call it
837 current_serial = serial;
838 if (opt.print_use_exvector_args)
839 ((print_funcp_exvector)pdt[id])(seq, c);
840 else switch (opt.nparams) {
841 // the following lines have been generated for max. ${maxargs} parameters
842 ${print_switch_statement}
843 // end of generated lines
845 throw(std::logic_error("function::print(): invalid nparams"));
850 ex function::expand(unsigned options) const
852 // Only expand arguments when asked to do so
853 if (options & expand_options::expand_function_args)
854 return inherited::expand(options);
856 return (options == 0) ? setflag(status_flags::expanded) : *this;
859 ex function::eval(int level) const
862 // first evaluate children, then we will end up here again
863 return function(serial,evalchildren(level));
866 GINAC_ASSERT(serial<registered_functions().size());
867 const function_options &opt = registered_functions()[serial];
869 // Canonicalize argument order according to the symmetry properties
870 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
872 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
873 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
874 if (sig != INT_MAX) {
875 // Something has changed while sorting arguments, more evaluations later
878 return ex(sig) * thiscontainer(v);
886 bool use_remember = opt.use_remember;
888 if (use_remember && lookup_remember_table(eval_result)) {
891 current_serial = serial;
892 if (opt.eval_use_exvector_args)
893 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
895 switch (opt.nparams) {
896 // the following lines have been generated for max. ${maxargs} parameters
897 ${eval_switch_statement}
898 // end of generated lines
900 throw(std::logic_error("function::eval(): invalid nparams"));
903 store_remember_table(eval_result);
908 ex function::evalf(int level) const
910 GINAC_ASSERT(serial<registered_functions().size());
911 const function_options &opt = registered_functions()[serial];
913 // Evaluate children first
915 if (level == 1 || !(opt.evalf_params_first))
917 else if (level == -max_recursion_level)
918 throw(std::runtime_error("max recursion level reached"));
920 eseq.reserve(seq.size());
922 exvector::const_iterator it = seq.begin(), itend = seq.end();
923 while (it != itend) {
924 eseq.push_back(it->evalf(level));
929 if (opt.evalf_f==0) {
930 return function(serial,eseq).hold();
932 current_serial = serial;
933 if (opt.evalf_use_exvector_args)
934 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
935 switch (opt.nparams) {
936 // the following lines have been generated for max. ${maxargs} parameters
937 ${evalf_switch_statement}
938 // end of generated lines
940 throw(std::logic_error("function::evalf(): invalid nparams"));
943 unsigned function::calchash() const
945 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
946 for (size_t i=0; i<nops(); i++) {
948 v ^= this->op(i).gethash();
951 if (flags & status_flags::evaluated) {
952 setflag(status_flags::hash_calculated);
958 ex function::thiscontainer(const exvector & v) const
960 return function(serial, v);
963 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
965 return function(serial, vp);
968 /** Implementation of ex::series for functions.
969 * \@see ex::series */
970 ex function::series(const relational & r, int order, unsigned options) const
972 GINAC_ASSERT(serial<registered_functions().size());
973 const function_options &opt = registered_functions()[serial];
975 if (opt.series_f==0) {
976 return basic::series(r, order);
979 current_serial = serial;
980 if (opt.series_use_exvector_args) {
982 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
983 } catch (do_taylor) {
984 res = basic::series(r, order, options);
988 switch (opt.nparams) {
989 // the following lines have been generated for max. ${maxargs} parameters
990 ${series_switch_statement}
991 // end of generated lines
993 throw(std::logic_error("function::series(): invalid nparams"));
996 /** Implementation of ex::conjugate for functions. */
997 ex function::conjugate() const
999 GINAC_ASSERT(serial<registered_functions().size());
1000 const function_options & opt = registered_functions()[serial];
1002 if (opt.conjugate_f==0) {
1003 return exprseq::conjugate();
1006 if (opt.conjugate_use_exvector_args) {
1007 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
1010 switch (opt.nparams) {
1011 // the following lines have been generated for max. ${maxargs} parameters
1012 ${conjugate_switch_statement}
1013 // end of generated lines
1015 throw(std::logic_error("function::conjugate(): invalid nparams"));
1020 /** Implementation of ex::diff() for functions. It applies the chain rule,
1021 * except for the Order term function.
1023 ex function::derivative(const symbol & s) const
1027 if (serial == Order_SERIAL::serial) {
1028 // Order Term function only differentiates the argument
1029 return Order(seq[0].diff(s));
1033 size_t num = seq.size();
1034 for (size_t i=0; i<num; i++) {
1035 arg_diff = seq[i].diff(s);
1036 // We apply the chain rule only when it makes sense. This is not
1037 // just for performance reasons but also to allow functions to
1038 // throw when differentiated with respect to one of its arguments
1039 // without running into trouble with our automatic full
1041 if (!arg_diff.is_zero())
1042 result += pderivative(i)*arg_diff;
1048 int function::compare_same_type(const basic & other) const
1050 GINAC_ASSERT(is_a<function>(other));
1051 const function & o = static_cast<const function &>(other);
1053 if (serial != o.serial)
1054 return serial < o.serial ? -1 : 1;
1056 return exprseq::compare_same_type(o);
1059 bool function::is_equal_same_type(const basic & other) const
1061 GINAC_ASSERT(is_a<function>(other));
1062 const function & o = static_cast<const function &>(other);
1064 if (serial != o.serial)
1067 return exprseq::is_equal_same_type(o);
1070 bool function::match_same_type(const basic & other) const
1072 GINAC_ASSERT(is_a<function>(other));
1073 const function & o = static_cast<const function &>(other);
1075 return serial == o.serial;
1078 unsigned function::return_type() const
1080 GINAC_ASSERT(serial<registered_functions().size());
1081 const function_options &opt = registered_functions()[serial];
1083 if (opt.use_return_type) {
1084 // Return type was explicitly specified
1085 return opt.return_type;
1087 // Default behavior is to use the return type of the first
1088 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1090 return return_types::commutative;
1092 return seq.begin()->return_type();
1096 unsigned function::return_type_tinfo() const
1098 GINAC_ASSERT(serial<registered_functions().size());
1099 const function_options &opt = registered_functions()[serial];
1101 if (opt.use_return_type) {
1102 // Return type was explicitly specified
1103 return opt.return_type_tinfo;
1105 // Default behavior is to use the return type of the first
1106 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1110 return seq.begin()->return_type_tinfo();
1115 // new virtual functions which can be overridden by derived classes
1121 // non-virtual functions in this class
1126 ex function::pderivative(unsigned diff_param) const // partial differentiation
1128 GINAC_ASSERT(serial<registered_functions().size());
1129 const function_options &opt = registered_functions()[serial];
1131 // No derivative defined? Then return abstract derivative object
1132 if (opt.derivative_f == NULL)
1133 return fderivative(serial, diff_param, seq);
1135 current_serial = serial;
1136 if (opt.derivative_use_exvector_args)
1137 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1138 switch (opt.nparams) {
1139 // the following lines have been generated for max. ${maxargs} parameters
1140 ${diff_switch_statement}
1141 // end of generated lines
1143 throw(std::logic_error("function::pderivative(): no diff function defined"));
1146 std::vector<function_options> & function::registered_functions()
1148 static std::vector<function_options> * rf = new std::vector<function_options>;
1152 bool function::lookup_remember_table(ex & result) const
1154 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1157 void function::store_remember_table(ex const & result) const
1159 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1164 unsigned function::register_new(function_options const & opt)
1166 size_t same_name = 0;
1167 for (size_t i=0; i<registered_functions().size(); ++i) {
1168 if (registered_functions()[i].name==opt.name) {
1172 if (same_name>=opt.functions_with_same_name) {
1173 // we do not throw an exception here because this code is
1174 // usually executed before main(), so the exception could not
1176 std::cerr << "WARNING: function name " << opt.name
1177 << " already in use!" << std::endl;
1179 registered_functions().push_back(opt);
1180 if (opt.use_remember) {
1181 remember_table::remember_tables().
1182 push_back(remember_table(opt.remember_size,
1183 opt.remember_assoc_size,
1184 opt.remember_strategy));
1186 remember_table::remember_tables().push_back(remember_table());
1188 return registered_functions().size()-1;
1191 /** Find serial number of function by name and number of parameters.
1192 * Throws exception if function was not found. */
1193 unsigned function::find_function(const std::string &name, unsigned nparams)
1195 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1196 unsigned serial = 0;
1198 if (i->get_name() == name && i->get_nparams() == nparams)
1203 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1206 /** Return the print name of the function. */
1207 std::string function::get_name() const
1209 GINAC_ASSERT(serial<registered_functions().size());
1210 return registered_functions()[serial].name;
1213 } // namespace GiNaC
1215 END_OF_IMPLEMENTATION
1217 print "Creating interface file function.h...";
1218 open OUT,">function.h" or die "cannot open function.h";
1219 print OUT $interface;
1223 print "Creating implementation file function.cpp...";
1224 open OUT,">function.cpp" or die "cannot open function.cpp";
1225 print OUT $implementation;