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_power_funcp=generate(
87 'typedef ex (* power_funcp_${N})(${SEQ1}, const ex &);'."\n",
90 $typedef_series_funcp=generate(
91 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
94 $typedef_print_funcp=generate(
95 'typedef void (* print_funcp_${N})(${SEQ1}, const print_context &);'."\n",
98 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
100 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
102 $conjugate_func_interface=generate(' function_options & conjugate_func(conjugate_funcp_${N} d);'."\n",'','','');
104 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
106 $power_func_interface=generate(' function_options & power_func(power_funcp_${N} d);'."\n",'','','');
108 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','','');
110 $print_func_interface=generate(
111 <<'END_OF_PRINT_FUNC_INTERFACE','','','');
112 template <class Ctx> function_options & print_func(print_funcp_${N} p)
114 test_and_set_nparams(${N});
115 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
118 END_OF_PRINT_FUNC_INTERFACE
120 $constructors_interface=generate(
121 ' function(unsigned ser, ${SEQ1});'."\n",
122 'const ex & param${N}','','');
124 $constructors_implementation=generate(
125 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
126 function::function(unsigned ser, ${SEQ1})
127 : exprseq(${SEQ2}), serial(ser)
129 tinfo_key = TINFO_function;
131 END_OF_CONSTRUCTORS_IMPLEMENTATION
133 $eval_switch_statement=generate(
134 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
136 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
138 END_OF_EVAL_SWITCH_STATEMENT
140 $evalf_switch_statement=generate(
141 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
143 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
144 END_OF_EVALF_SWITCH_STATEMENT
146 $conjugate_switch_statement=generate(
147 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
149 return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
150 END_OF_DIFF_SWITCH_STATEMENT
152 $diff_switch_statement=generate(
153 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
155 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
156 END_OF_DIFF_SWITCH_STATEMENT
158 $power_switch_statement=generate(
159 <<'END_OF_POWER_SWITCH_STATEMENT','seq[${N}-1]','','');
161 return ((power_funcp_${N})(opt.power_f))(${SEQ1},power_param);
162 END_OF_POWER_SWITCH_STATEMENT
164 $series_switch_statement=generate(
165 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
168 res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
169 } catch (do_taylor) {
170 res = basic::series(r, order, options);
173 END_OF_SERIES_SWITCH_STATEMENT
175 $print_switch_statement=generate(
176 <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
178 ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
180 END_OF_PRINT_SWITCH_STATEMENT
182 $eval_func_implementation=generate(
183 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
184 function_options & function_options::eval_func(eval_funcp_${N} e)
186 test_and_set_nparams(${N});
187 eval_f = eval_funcp(e);
190 END_OF_EVAL_FUNC_IMPLEMENTATION
192 $evalf_func_implementation=generate(
193 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
194 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
196 test_and_set_nparams(${N});
197 evalf_f = evalf_funcp(ef);
200 END_OF_EVALF_FUNC_IMPLEMENTATION
202 $conjugate_func_implementation=generate(
203 <<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
204 function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
206 test_and_set_nparams(${N});
207 conjugate_f = conjugate_funcp(c);
210 END_OF_CONJUGATE_FUNC_IMPLEMENTATION
212 $derivative_func_implementation=generate(
213 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
214 function_options & function_options::derivative_func(derivative_funcp_${N} d)
216 test_and_set_nparams(${N});
217 derivative_f = derivative_funcp(d);
220 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
222 $power_func_implementation=generate(
223 <<'END_OF_POWER_FUNC_IMPLEMENTATION','','','');
224 function_options & function_options::power_func(power_funcp_${N} d)
226 test_and_set_nparams(${N});
227 power_f = power_funcp(d);
230 END_OF_POWER_FUNC_IMPLEMENTATION
232 $series_func_implementation=generate(
233 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
234 function_options & function_options::series_func(series_funcp_${N} s)
236 test_and_set_nparams(${N});
237 series_f = series_funcp(s);
240 END_OF_SERIES_FUNC_IMPLEMENTATION
242 $interface=<<END_OF_INTERFACE;
243 /** \@file function.h
245 * Interface to class of symbolic functions. */
248 * This file was generated automatically by function.pl.
249 * Please do not modify it directly, edit the perl script instead!
250 * function.pl options: \$maxargs=${maxargs}
252 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
254 * This program is free software; you can redistribute it and/or modify
255 * it under the terms of the GNU General Public License as published by
256 * the Free Software Foundation; either version 2 of the License, or
257 * (at your option) any later version.
259 * This program is distributed in the hope that it will be useful,
260 * but WITHOUT ANY WARRANTY; without even the implied warranty of
261 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
262 * GNU General Public License for more details.
264 * You should have received a copy of the GNU General Public License
265 * along with this program; if not, write to the Free Software
266 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
269 #ifndef __GINAC_FUNCTION_H__
270 #define __GINAC_FUNCTION_H__
275 // CINT needs <algorithm> to work properly with <vector>
280 // the following lines have been generated for max. ${maxargs} parameters
281 $declare_function_macro
282 // end of generated lines
284 #define REGISTER_FUNCTION(NAME,OPT) \\
285 unsigned NAME##_SERIAL::serial = \\
286 GiNaC::function::register_new(GiNaC::function_options(#NAME, NAME##_NPARAMS).OPT);
293 typedef ex (* eval_funcp)();
294 typedef ex (* evalf_funcp)();
295 typedef ex (* conjugate_funcp)();
296 typedef ex (* derivative_funcp)();
297 typedef ex (* power_funcp)();
298 typedef ex (* series_funcp)();
299 typedef void (* print_funcp)();
301 // the following lines have been generated for max. ${maxargs} parameters
304 $typedef_conjugate_funcp
305 $typedef_derivative_funcp
307 $typedef_series_funcp
309 // end of generated lines
311 // Alternatively, an exvector may be passed into the static function, instead
312 // of individual ex objects. Then, the number of arguments is not limited.
313 typedef ex (* eval_funcp_exvector)(const exvector &);
314 typedef ex (* evalf_funcp_exvector)(const exvector &);
315 typedef ex (* conjugate_funcp_exvector)(const exvector &);
316 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
317 typedef ex (* power_funcp_exvector)(const exvector &, const ex &);
318 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
319 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
322 class function_options
324 friend class function;
325 friend class fderivative;
328 function_options(std::string const & n, std::string const & tn=std::string());
329 function_options(std::string const & n, unsigned np);
333 function_options & dummy() { return *this; }
334 function_options & set_name(std::string const & n, std::string const & tn=std::string());
335 function_options & latex_name(std::string const & tn);
336 // the following lines have been generated for max. ${maxargs} parameters
338 $evalf_func_interface
339 $conjugate_func_interface
340 $derivative_func_interface
341 $power_func_interface
342 $series_func_interface
343 $print_func_interface
344 // end of generated lines
345 function_options & eval_func(eval_funcp_exvector e);
346 function_options & evalf_func(evalf_funcp_exvector ef);
347 function_options & conjugate_func(conjugate_funcp_exvector d);
348 function_options & derivative_func(derivative_funcp_exvector d);
349 function_options & power_func(power_funcp_exvector d);
350 function_options & series_func(series_funcp_exvector s);
352 template <class Ctx> function_options & print_func(print_funcp_exvector p)
354 print_use_exvector_args = true;
355 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
359 function_options & set_return_type(unsigned rt, unsigned rtt=0);
360 function_options & do_not_evalf_params();
361 function_options & remember(unsigned size, unsigned assoc_size=0,
362 unsigned strategy=remember_strategies::delete_never);
363 function_options & overloaded(unsigned o);
364 function_options & set_symmetry(const symmetry & s);
366 std::string get_name() const { return name; }
367 unsigned get_nparams() const { return nparams; }
370 bool has_derivative() const { return derivative_f != NULL; }
371 bool has_power() const { return power_f != NULL; }
372 void test_and_set_nparams(unsigned n);
373 void set_print_func(unsigned id, print_funcp f);
376 std::string TeX_name;
382 conjugate_funcp conjugate_f;
383 derivative_funcp derivative_f;
385 series_funcp series_f;
386 std::vector<print_funcp> print_dispatch_table;
388 bool evalf_params_first;
390 bool use_return_type;
391 unsigned return_type;
392 unsigned return_type_tinfo;
395 unsigned remember_size;
396 unsigned remember_assoc_size;
397 unsigned remember_strategy;
399 bool eval_use_exvector_args;
400 bool evalf_use_exvector_args;
401 bool conjugate_use_exvector_args;
402 bool derivative_use_exvector_args;
403 bool power_use_exvector_args;
404 bool series_use_exvector_args;
405 bool print_use_exvector_args;
407 unsigned functions_with_same_name;
413 /** Exception class thrown by classes which provide their own series expansion
414 * to signal that ordinary Taylor expansion is safe. */
418 /** The class function is used to implement builtin functions like sin, cos...
419 and user defined functions */
420 class function : public exprseq
422 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
424 // CINT has a linking problem
426 friend void ginsh_get_ginac_functions();
427 #endif // def __MAKECINT__
429 friend class remember_table_entry;
430 // friend class remember_table_list;
431 // friend class remember_table;
435 // other constructors
437 function(unsigned ser);
438 // the following lines have been generated for max. ${maxargs} parameters
439 $constructors_interface
440 // end of generated lines
441 function(unsigned ser, const exprseq & es);
442 function(unsigned ser, const exvector & v, bool discardable = false);
443 function(unsigned ser, std::auto_ptr<exvector> vp);
445 // functions overriding virtual functions from base classes
447 void print(const print_context & c, unsigned level = 0) const;
448 unsigned precedence() const {return 70;}
449 ex expand(unsigned options=0) const;
450 ex eval(int level=0) const;
451 ex evalf(int level=0) const;
452 unsigned calchash() const;
453 ex series(const relational & r, int order, unsigned options = 0) const;
454 ex thiscontainer(const exvector & v) const;
455 ex thiscontainer(std::auto_ptr<exvector> vp) const;
456 ex conjugate() const;
458 ex derivative(const symbol & s) const;
459 bool is_equal_same_type(const basic & other) const;
460 bool match_same_type(const basic & other) const;
461 unsigned return_type() const;
462 unsigned return_type_tinfo() const;
464 // new virtual functions which can be overridden by derived classes
467 // non-virtual functions in this class
469 ex pderivative(unsigned diff_param) const; // partial differentiation
470 static std::vector<function_options> & registered_functions();
471 bool lookup_remember_table(ex & result) const;
472 void store_remember_table(ex const & result) const;
474 ex power(const ex & exp) const;
475 static unsigned register_new(function_options const & opt);
476 static unsigned current_serial;
477 static unsigned find_function(const std::string &name, unsigned nparams);
478 unsigned get_serial() const {return serial;}
479 std::string get_name() const;
487 // utility functions/macros
489 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
490 template<> inline bool is_exactly_a<function>(const basic & obj)
492 return obj.tinfo()==TINFO_function;
495 template <typename T>
496 inline bool is_the_function(const ex & x)
498 return is_exactly_a<function>(x)
499 && ex_to<function>(x).get_serial() == T::serial;
502 // Check whether OBJ is the specified symbolic function.
503 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
507 #endif // ndef __GINAC_FUNCTION_H__
511 $implementation=<<END_OF_IMPLEMENTATION;
512 /** \@file function.cpp
514 * Implementation of class of symbolic functions. */
517 * This file was generated automatically by function.pl.
518 * Please do not modify it directly, edit the perl script instead!
519 * function.pl options: \$maxargs=${maxargs}
521 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
523 * This program is free software; you can redistribute it and/or modify
524 * it under the terms of the GNU General Public License as published by
525 * the Free Software Foundation; either version 2 of the License, or
526 * (at your option) any later version.
528 * This program is distributed in the hope that it will be useful,
529 * but WITHOUT ANY WARRANTY; without even the implied warranty of
530 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
531 * GNU General Public License for more details.
533 * You should have received a copy of the GNU General Public License
534 * along with this program; if not, write to the Free Software
535 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
543 #include "function.h"
544 #include "operators.h"
545 #include "fderivative.h"
548 #include "symmetry.h"
553 #include "tostring.h"
555 #include "remember.h"
560 // helper class function_options
563 function_options::function_options()
568 function_options::function_options(std::string const & n, std::string const & tn)
574 function_options::function_options(std::string const & n, unsigned np)
577 set_name(n, std::string());
581 function_options::~function_options()
583 // nothing to clean up at the moment
586 void function_options::initialize()
588 set_name("unnamed_function", "\\\\mbox{unnamed}");
590 eval_f = evalf_f = conjugate_f = derivative_f = power_f = series_f = 0;
591 evalf_params_first = true;
592 use_return_type = false;
593 eval_use_exvector_args = false;
594 evalf_use_exvector_args = false;
595 conjugate_use_exvector_args = false;
596 derivative_use_exvector_args = false;
597 power_use_exvector_args = false;
598 series_use_exvector_args = false;
599 print_use_exvector_args = false;
600 use_remember = false;
601 functions_with_same_name = 1;
605 function_options & function_options::set_name(std::string const & n,
606 std::string const & tn)
609 if (tn==std::string())
610 TeX_name = "\\\\mbox{"+name+"}";
616 function_options & function_options::latex_name(std::string const & tn)
622 // the following lines have been generated for max. ${maxargs} parameters
623 $eval_func_implementation
624 $evalf_func_implementation
625 $conjugate_func_implementation
626 $derivative_func_implementation
627 $power_func_implementation
628 $series_func_implementation
629 // end of generated lines
631 function_options& function_options::eval_func(eval_funcp_exvector e)
633 eval_use_exvector_args = true;
634 eval_f = eval_funcp(e);
637 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
639 evalf_use_exvector_args = true;
640 evalf_f = evalf_funcp(ef);
643 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
645 conjugate_use_exvector_args = true;
646 conjugate_f = conjugate_funcp(c);
649 function_options& function_options::derivative_func(derivative_funcp_exvector d)
651 derivative_use_exvector_args = true;
652 derivative_f = derivative_funcp(d);
655 function_options& function_options::power_func(power_funcp_exvector d)
657 power_use_exvector_args = true;
658 power_f = power_funcp(d);
661 function_options& function_options::series_func(series_funcp_exvector s)
663 series_use_exvector_args = true;
664 series_f = series_funcp(s);
668 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
670 use_return_type = true;
672 return_type_tinfo = rtt;
676 function_options & function_options::do_not_evalf_params()
678 evalf_params_first = false;
682 function_options & function_options::remember(unsigned size,
687 remember_size = size;
688 remember_assoc_size = assoc_size;
689 remember_strategy = strategy;
693 function_options & function_options::overloaded(unsigned o)
695 functions_with_same_name = o;
699 function_options & function_options::set_symmetry(const symmetry & s)
705 void function_options::test_and_set_nparams(unsigned n)
709 } else if (nparams!=n) {
710 // we do not throw an exception here because this code is
711 // usually executed before main(), so the exception could not
713 std::cerr << "WARNING: " << name << "(): number of parameters ("
714 << n << ") differs from number set before ("
715 << nparams << ")" << std::endl;
719 void function_options::set_print_func(unsigned id, print_funcp f)
721 if (id >= print_dispatch_table.size())
722 print_dispatch_table.resize(id + 1);
723 print_dispatch_table[id] = f;
726 /** This can be used as a hook for external applications. */
727 unsigned function::current_serial = 0;
730 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
733 // default constructor
738 function::function() : serial(0)
740 tinfo_key = TINFO_function;
744 // other constructors
749 function::function(unsigned ser) : serial(ser)
751 tinfo_key = TINFO_function;
754 // the following lines have been generated for max. ${maxargs} parameters
755 $constructors_implementation
756 // end of generated lines
758 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
760 tinfo_key = TINFO_function;
762 // Force re-evaluation even if the exprseq was already evaluated
763 // (the exprseq copy constructor copies the flags)
764 clearflag(status_flags::evaluated);
767 function::function(unsigned ser, const exvector & v, bool discardable)
768 : exprseq(v,discardable), serial(ser)
770 tinfo_key = TINFO_function;
773 function::function(unsigned ser, std::auto_ptr<exvector> vp)
774 : exprseq(vp), serial(ser)
776 tinfo_key = TINFO_function;
783 /** Construct object from archive_node. */
784 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
786 // Find serial number by function name
788 if (n.find_string("name", s)) {
789 unsigned int ser = 0;
790 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
798 throw (std::runtime_error("unknown function '" + s + "' in archive"));
800 throw (std::runtime_error("unnamed function in archive"));
803 /** Unarchive the object. */
804 ex function::unarchive(const archive_node &n, lst &sym_lst)
806 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
809 /** Archive the object. */
810 void function::archive(archive_node &n) const
812 inherited::archive(n);
813 GINAC_ASSERT(serial < registered_functions().size());
814 n.add_string("name", registered_functions()[serial].name);
818 // functions overriding virtual functions from base classes
823 void function::print(const print_context & c, unsigned level) const
825 GINAC_ASSERT(serial<registered_functions().size());
826 const function_options &opt = registered_functions()[serial];
827 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
829 // Dynamically dispatch on print_context type
830 const print_context_class_info *pc_info = &c.get_class_info();
833 unsigned id = pc_info->options.get_id();
834 if (id >= pdt.size() || pdt[id] == NULL) {
836 // Method not found, try parent print_context class
837 const print_context_class_info *parent_pc_info = pc_info->get_parent();
838 if (parent_pc_info) {
839 pc_info = parent_pc_info;
843 // Method still not found, use default output
844 if (is_a<print_tree>(c)) {
846 c.s << std::string(level, ' ') << class_name() << " "
847 << opt.name << " @" << this
848 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
849 << ", nops=" << nops()
851 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
852 for (size_t i=0; i<seq.size(); ++i)
853 seq[i].print(c, level + delta_indent);
854 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
856 } else if (is_a<print_csrc>(c)) {
858 // Print function name in lowercase
859 std::string lname = opt.name;
860 size_t num = lname.size();
861 for (size_t i=0; i<num; i++)
862 lname[i] = tolower(lname[i]);
864 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
866 } else if (is_a<print_latex>(c)) {
868 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
871 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
876 // Method found, call it
877 current_serial = serial;
878 if (opt.print_use_exvector_args)
879 ((print_funcp_exvector)pdt[id])(seq, c);
880 else switch (opt.nparams) {
881 // the following lines have been generated for max. ${maxargs} parameters
882 ${print_switch_statement}
883 // end of generated lines
885 throw(std::logic_error("function::print(): invalid nparams"));
890 ex function::expand(unsigned options) const
892 // Only expand arguments when asked to do so
893 if (options & expand_options::expand_function_args)
894 return inherited::expand(options);
896 return (options == 0) ? setflag(status_flags::expanded) : *this;
899 ex function::eval(int level) const
902 // first evaluate children, then we will end up here again
903 return function(serial,evalchildren(level));
906 GINAC_ASSERT(serial<registered_functions().size());
907 const function_options &opt = registered_functions()[serial];
909 // Canonicalize argument order according to the symmetry properties
910 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
912 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
913 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
914 if (sig != INT_MAX) {
915 // Something has changed while sorting arguments, more evaluations later
918 return ex(sig) * thiscontainer(v);
926 bool use_remember = opt.use_remember;
928 if (use_remember && lookup_remember_table(eval_result)) {
931 current_serial = serial;
932 if (opt.eval_use_exvector_args)
933 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
935 switch (opt.nparams) {
936 // the following lines have been generated for max. ${maxargs} parameters
937 ${eval_switch_statement}
938 // end of generated lines
940 throw(std::logic_error("function::eval(): invalid nparams"));
943 store_remember_table(eval_result);
948 ex function::evalf(int level) const
950 GINAC_ASSERT(serial<registered_functions().size());
951 const function_options &opt = registered_functions()[serial];
953 // Evaluate children first
955 if (level == 1 || !(opt.evalf_params_first))
957 else if (level == -max_recursion_level)
958 throw(std::runtime_error("max recursion level reached"));
960 eseq.reserve(seq.size());
962 exvector::const_iterator it = seq.begin(), itend = seq.end();
963 while (it != itend) {
964 eseq.push_back(it->evalf(level));
969 if (opt.evalf_f==0) {
970 return function(serial,eseq).hold();
972 current_serial = serial;
973 if (opt.evalf_use_exvector_args)
974 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
975 switch (opt.nparams) {
976 // the following lines have been generated for max. ${maxargs} parameters
977 ${evalf_switch_statement}
978 // end of generated lines
980 throw(std::logic_error("function::evalf(): invalid nparams"));
983 unsigned function::calchash() const
985 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
986 for (size_t i=0; i<nops(); i++) {
988 v ^= this->op(i).gethash();
991 if (flags & status_flags::evaluated) {
992 setflag(status_flags::hash_calculated);
998 ex function::thiscontainer(const exvector & v) const
1000 return function(serial, v);
1003 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
1005 return function(serial, vp);
1008 /** Implementation of ex::series for functions.
1009 * \@see ex::series */
1010 ex function::series(const relational & r, int order, unsigned options) const
1012 GINAC_ASSERT(serial<registered_functions().size());
1013 const function_options &opt = registered_functions()[serial];
1015 if (opt.series_f==0) {
1016 return basic::series(r, order);
1019 current_serial = serial;
1020 if (opt.series_use_exvector_args) {
1022 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
1023 } catch (do_taylor) {
1024 res = basic::series(r, order, options);
1028 switch (opt.nparams) {
1029 // the following lines have been generated for max. ${maxargs} parameters
1030 ${series_switch_statement}
1031 // end of generated lines
1033 throw(std::logic_error("function::series(): invalid nparams"));
1036 /** Implementation of ex::conjugate for functions. */
1037 ex function::conjugate() const
1039 GINAC_ASSERT(serial<registered_functions().size());
1040 const function_options & opt = registered_functions()[serial];
1042 if (opt.conjugate_f==0) {
1043 return exprseq::conjugate();
1046 if (opt.conjugate_use_exvector_args) {
1047 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
1050 switch (opt.nparams) {
1051 // the following lines have been generated for max. ${maxargs} parameters
1052 ${conjugate_switch_statement}
1053 // end of generated lines
1055 throw(std::logic_error("function::conjugate(): invalid nparams"));
1060 /** Implementation of ex::diff() for functions. It applies the chain rule,
1061 * except for the Order term function.
1063 ex function::derivative(const symbol & s) const
1067 if (serial == Order_SERIAL::serial) {
1068 // Order Term function only differentiates the argument
1069 return Order(seq[0].diff(s));
1073 size_t num = seq.size();
1074 for (size_t i=0; i<num; i++) {
1075 arg_diff = seq[i].diff(s);
1076 // We apply the chain rule only when it makes sense. This is not
1077 // just for performance reasons but also to allow functions to
1078 // throw when differentiated with respect to one of its arguments
1079 // without running into trouble with our automatic full
1081 if (!arg_diff.is_zero())
1082 result += pderivative(i)*arg_diff;
1088 int function::compare_same_type(const basic & other) const
1090 GINAC_ASSERT(is_a<function>(other));
1091 const function & o = static_cast<const function &>(other);
1093 if (serial != o.serial)
1094 return serial < o.serial ? -1 : 1;
1096 return exprseq::compare_same_type(o);
1099 bool function::is_equal_same_type(const basic & other) const
1101 GINAC_ASSERT(is_a<function>(other));
1102 const function & o = static_cast<const function &>(other);
1104 if (serial != o.serial)
1107 return exprseq::is_equal_same_type(o);
1110 bool function::match_same_type(const basic & other) const
1112 GINAC_ASSERT(is_a<function>(other));
1113 const function & o = static_cast<const function &>(other);
1115 return serial == o.serial;
1118 unsigned function::return_type() const
1120 GINAC_ASSERT(serial<registered_functions().size());
1121 const function_options &opt = registered_functions()[serial];
1123 if (opt.use_return_type) {
1124 // Return type was explicitly specified
1125 return opt.return_type;
1127 // Default behavior is to use the return type of the first
1128 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1130 return return_types::commutative;
1132 return seq.begin()->return_type();
1136 unsigned function::return_type_tinfo() const
1138 GINAC_ASSERT(serial<registered_functions().size());
1139 const function_options &opt = registered_functions()[serial];
1141 if (opt.use_return_type) {
1142 // Return type was explicitly specified
1143 return opt.return_type_tinfo;
1145 // Default behavior is to use the return type of the first
1146 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1150 return seq.begin()->return_type_tinfo();
1155 // new virtual functions which can be overridden by derived classes
1161 // non-virtual functions in this class
1166 ex function::pderivative(unsigned diff_param) const // partial differentiation
1168 GINAC_ASSERT(serial<registered_functions().size());
1169 const function_options &opt = registered_functions()[serial];
1171 // No derivative defined? Then return abstract derivative object
1172 if (opt.derivative_f == NULL)
1173 return fderivative(serial, diff_param, seq);
1175 current_serial = serial;
1176 if (opt.derivative_use_exvector_args)
1177 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1178 switch (opt.nparams) {
1179 // the following lines have been generated for max. ${maxargs} parameters
1180 ${diff_switch_statement}
1181 // end of generated lines
1183 throw(std::logic_error("function::pderivative(): no diff function defined"));
1186 ex function::power(const ex & power_param) const // power of function
1188 GINAC_ASSERT(serial<registered_functions().size());
1189 const function_options &opt = registered_functions()[serial];
1191 // No derivative defined? Then return abstract derivative object
1192 if (opt.power_f == NULL)
1193 return (new power::power(*this, power_param))->setflag(status_flags::dynallocated |
1194 status_flags::evaluated);
1196 current_serial = serial;
1197 if (opt.power_use_exvector_args)
1198 return ((power_funcp_exvector)(opt.power_f))(seq, power_param);
1199 switch (opt.nparams) {
1200 // the following lines have been generated for max. ${maxargs} parameters
1201 ${power_switch_statement}
1202 // end of generated lines
1204 throw(std::logic_error("function::power(): no power function defined"));
1207 std::vector<function_options> & function::registered_functions()
1209 static std::vector<function_options> * rf = new std::vector<function_options>;
1213 bool function::lookup_remember_table(ex & result) const
1215 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1218 void function::store_remember_table(ex const & result) const
1220 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1225 unsigned function::register_new(function_options const & opt)
1227 size_t same_name = 0;
1228 for (size_t i=0; i<registered_functions().size(); ++i) {
1229 if (registered_functions()[i].name==opt.name) {
1233 if (same_name>=opt.functions_with_same_name) {
1234 // we do not throw an exception here because this code is
1235 // usually executed before main(), so the exception could not
1237 std::cerr << "WARNING: function name " << opt.name
1238 << " already in use!" << std::endl;
1240 registered_functions().push_back(opt);
1241 if (opt.use_remember) {
1242 remember_table::remember_tables().
1243 push_back(remember_table(opt.remember_size,
1244 opt.remember_assoc_size,
1245 opt.remember_strategy));
1247 remember_table::remember_tables().push_back(remember_table());
1249 return registered_functions().size()-1;
1252 /** Find serial number of function by name and number of parameters.
1253 * Throws exception if function was not found. */
1254 unsigned function::find_function(const std::string &name, unsigned nparams)
1256 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1257 unsigned serial = 0;
1259 if (i->get_name() == name && i->get_nparams() == nparams)
1264 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1267 /** Return the print name of the function. */
1268 std::string function::get_name() const
1270 GINAC_ASSERT(serial<registered_functions().size());
1271 return registered_functions()[serial].name;
1274 } // namespace GiNaC
1276 END_OF_IMPLEMENTATION
1278 print "Creating interface file function.h...";
1279 open OUT,">function.h" or die "cannot open function.h";
1280 print OUT $interface;
1284 print "Creating implementation file function.cpp...";
1285 open OUT,">function.cpp" or die "cannot open function.cpp";
1286 print OUT $implementation;