1 # This perl script automatically generates function.h and function.cpp
3 # function.pl options: \$maxargs=${maxargs}
5 # GiNaC Copyright (C) 1999-2007 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_real_part_funcp=generate(
83 'typedef ex (* real_part_funcp_${N})(${SEQ1});'."\n",
86 $typedef_imag_part_funcp=generate(
87 'typedef ex (* imag_part_funcp_${N})(${SEQ1});'."\n",
90 $typedef_derivative_funcp=generate(
91 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
94 $typedef_power_funcp=generate(
95 'typedef ex (* power_funcp_${N})(${SEQ1}, const ex &);'."\n",
98 $typedef_series_funcp=generate(
99 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
102 $typedef_print_funcp=generate(
103 'typedef void (* print_funcp_${N})(${SEQ1}, const print_context &);'."\n",
106 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
108 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
110 $conjugate_func_interface=generate(' function_options & conjugate_func(conjugate_funcp_${N} d);'."\n",'','','');
112 $real_part_func_interface=generate(' function_options & real_part_func(real_part_funcp_${N} d);'."\n",'','','');
114 $imag_part_func_interface=generate(' function_options & imag_part_func(imag_part_funcp_${N} d);'."\n",'','','');
116 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
118 $power_func_interface=generate(' function_options & power_func(power_funcp_${N} d);'."\n",'','','');
120 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','','');
122 $print_func_interface=generate(
123 <<'END_OF_PRINT_FUNC_INTERFACE','','','');
124 template <class Ctx> function_options & print_func(print_funcp_${N} p)
126 test_and_set_nparams(${N});
127 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
130 END_OF_PRINT_FUNC_INTERFACE
132 $constructors_interface=generate(
133 ' function(unsigned ser, ${SEQ1});'."\n",
134 'const ex & param${N}','','');
136 $constructors_implementation=generate(
137 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
138 function::function(unsigned ser, ${SEQ1})
139 : exprseq(${SEQ2}), serial(ser)
141 tinfo_key = &function::tinfo_static;
143 END_OF_CONSTRUCTORS_IMPLEMENTATION
145 $eval_switch_statement=generate(
146 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
148 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
150 END_OF_EVAL_SWITCH_STATEMENT
152 $evalf_switch_statement=generate(
153 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
155 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
156 END_OF_EVALF_SWITCH_STATEMENT
158 $conjugate_switch_statement=generate(
159 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
161 return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
162 END_OF_DIFF_SWITCH_STATEMENT
164 $real_part_switch_statement=generate(
165 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
167 return ((real_part_funcp_${N})(opt.real_part_f))(${SEQ1});
168 END_OF_DIFF_SWITCH_STATEMENT
170 $imag_part_switch_statement=generate(
171 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
173 return ((imag_part_funcp_${N})(opt.imag_part_f))(${SEQ1});
174 END_OF_DIFF_SWITCH_STATEMENT
176 $diff_switch_statement=generate(
177 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
179 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
180 END_OF_DIFF_SWITCH_STATEMENT
182 $power_switch_statement=generate(
183 <<'END_OF_POWER_SWITCH_STATEMENT','seq[${N}-1]','','');
185 return ((power_funcp_${N})(opt.power_f))(${SEQ1},power_param);
186 END_OF_POWER_SWITCH_STATEMENT
188 $series_switch_statement=generate(
189 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
192 res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
193 } catch (do_taylor) {
194 res = basic::series(r, order, options);
197 END_OF_SERIES_SWITCH_STATEMENT
199 $print_switch_statement=generate(
200 <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
202 ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
204 END_OF_PRINT_SWITCH_STATEMENT
206 $eval_func_implementation=generate(
207 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
208 function_options & function_options::eval_func(eval_funcp_${N} e)
210 test_and_set_nparams(${N});
211 eval_f = eval_funcp(e);
214 END_OF_EVAL_FUNC_IMPLEMENTATION
216 $evalf_func_implementation=generate(
217 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
218 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
220 test_and_set_nparams(${N});
221 evalf_f = evalf_funcp(ef);
224 END_OF_EVALF_FUNC_IMPLEMENTATION
226 $conjugate_func_implementation=generate(
227 <<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
228 function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
230 test_and_set_nparams(${N});
231 conjugate_f = conjugate_funcp(c);
234 END_OF_CONJUGATE_FUNC_IMPLEMENTATION
236 $real_part_func_implementation=generate(
237 <<'END_OF_REAL_PART_FUNC_IMPLEMENTATION','','','');
238 function_options & function_options::real_part_func(real_part_funcp_${N} c)
240 test_and_set_nparams(${N});
241 real_part_f = real_part_funcp(c);
244 END_OF_REAL_PART_FUNC_IMPLEMENTATION
246 $imag_part_func_implementation=generate(
247 <<'END_OF_IMAG_PART_FUNC_IMPLEMENTATION','','','');
248 function_options & function_options::imag_part_func(imag_part_funcp_${N} c)
250 test_and_set_nparams(${N});
251 imag_part_f = imag_part_funcp(c);
254 END_OF_IMAG_PART_FUNC_IMPLEMENTATION
256 $derivative_func_implementation=generate(
257 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
258 function_options & function_options::derivative_func(derivative_funcp_${N} d)
260 test_and_set_nparams(${N});
261 derivative_f = derivative_funcp(d);
264 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
266 $power_func_implementation=generate(
267 <<'END_OF_POWER_FUNC_IMPLEMENTATION','','','');
268 function_options & function_options::power_func(power_funcp_${N} d)
270 test_and_set_nparams(${N});
271 power_f = power_funcp(d);
274 END_OF_POWER_FUNC_IMPLEMENTATION
276 $series_func_implementation=generate(
277 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
278 function_options & function_options::series_func(series_funcp_${N} s)
280 test_and_set_nparams(${N});
281 series_f = series_funcp(s);
284 END_OF_SERIES_FUNC_IMPLEMENTATION
286 $interface=<<END_OF_INTERFACE;
287 /** \@file function.h
289 * Interface to class of symbolic functions. */
292 * This file was generated automatically by function.pl.
293 * Please do not modify it directly, edit the perl script instead!
294 * function.pl options: \$maxargs=${maxargs}
296 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
298 * This program is free software; you can redistribute it and/or modify
299 * it under the terms of the GNU General Public License as published by
300 * the Free Software Foundation; either version 2 of the License, or
301 * (at your option) any later version.
303 * This program is distributed in the hope that it will be useful,
304 * but WITHOUT ANY WARRANTY; without even the implied warranty of
305 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
306 * GNU General Public License for more details.
308 * You should have received a copy of the GNU General Public License
309 * along with this program; if not, write to the Free Software
310 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
313 #ifndef __GINAC_FUNCTION_H__
314 #define __GINAC_FUNCTION_H__
319 // CINT needs <algorithm> to work properly with <vector>
324 // the following lines have been generated for max. ${maxargs} parameters
325 $declare_function_macro
326 // end of generated lines
328 #define REGISTER_FUNCTION(NAME,OPT) \\
329 unsigned NAME##_SERIAL::serial = \\
330 GiNaC::function::register_new(GiNaC::function_options(#NAME, NAME##_NPARAMS).OPT);
337 typedef ex (* eval_funcp)();
338 typedef ex (* evalf_funcp)();
339 typedef ex (* conjugate_funcp)();
340 typedef ex (* real_part_funcp)();
341 typedef ex (* imag_part_funcp)();
342 typedef ex (* derivative_funcp)();
343 typedef ex (* power_funcp)();
344 typedef ex (* series_funcp)();
345 typedef void (* print_funcp)();
347 // the following lines have been generated for max. ${maxargs} parameters
350 $typedef_conjugate_funcp
351 $typedef_real_part_funcp
352 $typedef_imag_part_funcp
353 $typedef_derivative_funcp
355 $typedef_series_funcp
357 // end of generated lines
359 // Alternatively, an exvector may be passed into the static function, instead
360 // of individual ex objects. Then, the number of arguments is not limited.
361 typedef ex (* eval_funcp_exvector)(const exvector &);
362 typedef ex (* evalf_funcp_exvector)(const exvector &);
363 typedef ex (* conjugate_funcp_exvector)(const exvector &);
364 typedef ex (* real_part_funcp_exvector)(const exvector &);
365 typedef ex (* imag_part_funcp_exvector)(const exvector &);
366 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
367 typedef ex (* power_funcp_exvector)(const exvector &, const ex &);
368 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
369 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
372 class function_options
374 friend class function;
375 friend class fderivative;
378 function_options(std::string const & n, std::string const & tn=std::string());
379 function_options(std::string const & n, unsigned np);
383 function_options & dummy() { return *this; }
384 function_options & set_name(std::string const & n, std::string const & tn=std::string());
385 function_options & latex_name(std::string const & tn);
386 // the following lines have been generated for max. ${maxargs} parameters
388 $evalf_func_interface
389 $conjugate_func_interface
390 $real_part_func_interface
391 $imag_part_func_interface
392 $derivative_func_interface
393 $power_func_interface
394 $series_func_interface
395 $print_func_interface
396 // end of generated lines
397 function_options & eval_func(eval_funcp_exvector e);
398 function_options & evalf_func(evalf_funcp_exvector ef);
399 function_options & conjugate_func(conjugate_funcp_exvector d);
400 function_options & real_part_func(real_part_funcp_exvector d);
401 function_options & imag_part_func(imag_part_funcp_exvector d);
402 function_options & derivative_func(derivative_funcp_exvector d);
403 function_options & power_func(power_funcp_exvector d);
404 function_options & series_func(series_funcp_exvector s);
406 template <class Ctx> function_options & print_func(print_funcp_exvector p)
408 print_use_exvector_args = true;
409 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
413 function_options & set_return_type(unsigned rt, tinfo_t rtt=NULL);
414 function_options & do_not_evalf_params();
415 function_options & remember(unsigned size, unsigned assoc_size=0,
416 unsigned strategy=remember_strategies::delete_never);
417 function_options & overloaded(unsigned o);
418 function_options & set_symmetry(const symmetry & s);
420 std::string get_name() const { return name; }
421 unsigned get_nparams() const { return nparams; }
424 bool has_derivative() const { return derivative_f != NULL; }
425 bool has_power() const { return power_f != NULL; }
426 void test_and_set_nparams(unsigned n);
427 void set_print_func(unsigned id, print_funcp f);
430 std::string TeX_name;
436 conjugate_funcp conjugate_f;
437 real_part_funcp real_part_f;
438 imag_part_funcp imag_part_f;
439 derivative_funcp derivative_f;
441 series_funcp series_f;
442 std::vector<print_funcp> print_dispatch_table;
444 bool evalf_params_first;
446 bool use_return_type;
447 unsigned return_type;
448 tinfo_t return_type_tinfo;
451 unsigned remember_size;
452 unsigned remember_assoc_size;
453 unsigned remember_strategy;
455 bool eval_use_exvector_args;
456 bool evalf_use_exvector_args;
457 bool conjugate_use_exvector_args;
458 bool real_part_use_exvector_args;
459 bool imag_part_use_exvector_args;
460 bool derivative_use_exvector_args;
461 bool power_use_exvector_args;
462 bool series_use_exvector_args;
463 bool print_use_exvector_args;
465 unsigned functions_with_same_name;
471 /** Exception class thrown by classes which provide their own series expansion
472 * to signal that ordinary Taylor expansion is safe. */
476 /** The class function is used to implement builtin functions like sin, cos...
477 and user defined functions */
478 class function : public exprseq
480 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
482 // CINT has a linking problem
484 friend void ginsh_get_ginac_functions();
485 #endif // def __MAKECINT__
487 friend class remember_table_entry;
488 // friend class remember_table_list;
489 // friend class remember_table;
493 // other constructors
495 function(unsigned ser);
496 // the following lines have been generated for max. ${maxargs} parameters
497 $constructors_interface
498 // end of generated lines
499 function(unsigned ser, const exprseq & es);
500 function(unsigned ser, const exvector & v, bool discardable = false);
501 function(unsigned ser, std::auto_ptr<exvector> vp);
503 // functions overriding virtual functions from base classes
505 void print(const print_context & c, unsigned level = 0) const;
506 unsigned precedence() const {return 70;}
507 ex expand(unsigned options=0) const;
508 ex eval(int level=0) const;
509 ex evalf(int level=0) const;
510 unsigned calchash() const;
511 ex series(const relational & r, int order, unsigned options = 0) const;
512 ex thiscontainer(const exvector & v) const;
513 ex thiscontainer(std::auto_ptr<exvector> vp) const;
514 ex conjugate() const;
515 ex real_part() const;
516 ex imag_part() const;
518 ex derivative(const symbol & s) const;
519 bool is_equal_same_type(const basic & other) const;
520 bool match_same_type(const basic & other) const;
521 unsigned return_type() const;
522 tinfo_t return_type_tinfo() const;
524 // new virtual functions which can be overridden by derived classes
527 // non-virtual functions in this class
529 ex pderivative(unsigned diff_param) const; // partial differentiation
530 static std::vector<function_options> & registered_functions();
531 bool lookup_remember_table(ex & result) const;
532 void store_remember_table(ex const & result) const;
534 ex power(const ex & exp) const;
535 static unsigned register_new(function_options const & opt);
536 static unsigned current_serial;
537 static unsigned find_function(const std::string &name, unsigned nparams);
538 unsigned get_serial() const {return serial;}
539 std::string get_name() const;
547 // utility functions/macros
549 template <typename T>
550 inline bool is_the_function(const ex & x)
552 return is_exactly_a<function>(x)
553 && ex_to<function>(x).get_serial() == T::serial;
556 // Check whether OBJ is the specified symbolic function.
557 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
561 #endif // ndef __GINAC_FUNCTION_H__
565 $implementation=<<END_OF_IMPLEMENTATION;
566 /** \@file function.cpp
568 * Implementation of class of symbolic functions. */
571 * This file was generated automatically by function.pl.
572 * Please do not modify it directly, edit the perl script instead!
573 * function.pl options: \$maxargs=${maxargs}
575 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
577 * This program is free software; you can redistribute it and/or modify
578 * it under the terms of the GNU General Public License as published by
579 * the Free Software Foundation; either version 2 of the License, or
580 * (at your option) any later version.
582 * This program is distributed in the hope that it will be useful,
583 * but WITHOUT ANY WARRANTY; without even the implied warranty of
584 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
585 * GNU General Public License for more details.
587 * You should have received a copy of the GNU General Public License
588 * along with this program; if not, write to the Free Software
589 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
598 #include "function.h"
599 #include "operators.h"
600 #include "fderivative.h"
603 #include "symmetry.h"
608 #include "tostring.h"
610 #include "remember.h"
615 // helper class function_options
618 function_options::function_options()
623 function_options::function_options(std::string const & n, std::string const & tn)
629 function_options::function_options(std::string const & n, unsigned np)
632 set_name(n, std::string());
636 function_options::~function_options()
638 // nothing to clean up at the moment
641 void function_options::initialize()
643 set_name("unnamed_function", "\\\\mbox{unnamed}");
645 eval_f = evalf_f = real_part_f = imag_part_f = conjugate_f = derivative_f
646 = power_f = series_f = 0;
647 evalf_params_first = true;
648 use_return_type = false;
649 eval_use_exvector_args = false;
650 evalf_use_exvector_args = false;
651 conjugate_use_exvector_args = false;
652 real_part_use_exvector_args = false;
653 imag_part_use_exvector_args = false;
654 derivative_use_exvector_args = false;
655 power_use_exvector_args = false;
656 series_use_exvector_args = false;
657 print_use_exvector_args = false;
658 use_remember = false;
659 functions_with_same_name = 1;
663 function_options & function_options::set_name(std::string const & n,
664 std::string const & tn)
667 if (tn==std::string())
668 TeX_name = "\\\\mbox{"+name+"}";
674 function_options & function_options::latex_name(std::string const & tn)
680 // the following lines have been generated for max. ${maxargs} parameters
681 $eval_func_implementation
682 $evalf_func_implementation
683 $conjugate_func_implementation
684 $real_part_func_implementation
685 $imag_part_func_implementation
686 $derivative_func_implementation
687 $power_func_implementation
688 $series_func_implementation
689 // end of generated lines
691 function_options& function_options::eval_func(eval_funcp_exvector e)
693 eval_use_exvector_args = true;
694 eval_f = eval_funcp(e);
697 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
699 evalf_use_exvector_args = true;
700 evalf_f = evalf_funcp(ef);
703 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
705 conjugate_use_exvector_args = true;
706 conjugate_f = conjugate_funcp(c);
709 function_options& function_options::real_part_func(real_part_funcp_exvector c)
711 real_part_use_exvector_args = true;
712 real_part_f = real_part_funcp(c);
715 function_options& function_options::imag_part_func(imag_part_funcp_exvector c)
717 imag_part_use_exvector_args = true;
718 imag_part_f = imag_part_funcp(c);
722 function_options& function_options::derivative_func(derivative_funcp_exvector d)
724 derivative_use_exvector_args = true;
725 derivative_f = derivative_funcp(d);
728 function_options& function_options::power_func(power_funcp_exvector d)
730 power_use_exvector_args = true;
731 power_f = power_funcp(d);
734 function_options& function_options::series_func(series_funcp_exvector s)
736 series_use_exvector_args = true;
737 series_f = series_funcp(s);
741 function_options & function_options::set_return_type(unsigned rt, tinfo_t rtt)
743 use_return_type = true;
745 return_type_tinfo = rtt;
749 function_options & function_options::do_not_evalf_params()
751 evalf_params_first = false;
755 function_options & function_options::remember(unsigned size,
760 remember_size = size;
761 remember_assoc_size = assoc_size;
762 remember_strategy = strategy;
766 function_options & function_options::overloaded(unsigned o)
768 functions_with_same_name = o;
772 function_options & function_options::set_symmetry(const symmetry & s)
778 void function_options::test_and_set_nparams(unsigned n)
782 } else if (nparams!=n) {
783 // we do not throw an exception here because this code is
784 // usually executed before main(), so the exception could not
786 std::cerr << "WARNING: " << name << "(): number of parameters ("
787 << n << ") differs from number set before ("
788 << nparams << ")" << std::endl;
792 void function_options::set_print_func(unsigned id, print_funcp f)
794 if (id >= print_dispatch_table.size())
795 print_dispatch_table.resize(id + 1);
796 print_dispatch_table[id] = f;
799 /** This can be used as a hook for external applications. */
800 unsigned function::current_serial = 0;
803 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
806 // default constructor
811 function::function() : serial(0)
813 tinfo_key = &function::tinfo_static;
817 // other constructors
822 function::function(unsigned ser) : serial(ser)
824 tinfo_key = &function::tinfo_static;
827 // the following lines have been generated for max. ${maxargs} parameters
828 $constructors_implementation
829 // end of generated lines
831 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
833 tinfo_key = &function::tinfo_static;
835 // Force re-evaluation even if the exprseq was already evaluated
836 // (the exprseq copy constructor copies the flags)
837 clearflag(status_flags::evaluated);
840 function::function(unsigned ser, const exvector & v, bool discardable)
841 : exprseq(v,discardable), serial(ser)
843 tinfo_key = &function::tinfo_static;
846 function::function(unsigned ser, std::auto_ptr<exvector> vp)
847 : exprseq(vp), serial(ser)
849 tinfo_key = &function::tinfo_static;
856 /** Construct object from archive_node. */
857 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
859 // Find serial number by function name
861 if (n.find_string("name", s)) {
862 unsigned int ser = 0;
863 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
871 throw (std::runtime_error("unknown function '" + s + "' in archive"));
873 throw (std::runtime_error("unnamed function in archive"));
876 /** Unarchive the object. */
877 ex function::unarchive(const archive_node &n, lst &sym_lst)
879 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
882 /** Archive the object. */
883 void function::archive(archive_node &n) const
885 inherited::archive(n);
886 GINAC_ASSERT(serial < registered_functions().size());
887 n.add_string("name", registered_functions()[serial].name);
891 // functions overriding virtual functions from base classes
896 void function::print(const print_context & c, unsigned level) const
898 GINAC_ASSERT(serial<registered_functions().size());
899 const function_options &opt = registered_functions()[serial];
900 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
902 // Dynamically dispatch on print_context type
903 const print_context_class_info *pc_info = &c.get_class_info();
906 unsigned id = pc_info->options.get_id();
907 if (id >= pdt.size() || pdt[id] == NULL) {
909 // Method not found, try parent print_context class
910 const print_context_class_info *parent_pc_info = pc_info->get_parent();
911 if (parent_pc_info) {
912 pc_info = parent_pc_info;
916 // Method still not found, use default output
917 if (is_a<print_tree>(c)) {
919 c.s << std::string(level, ' ') << class_name() << " "
920 << opt.name << " @" << this
921 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
922 << ", nops=" << nops()
924 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
925 for (size_t i=0; i<seq.size(); ++i)
926 seq[i].print(c, level + delta_indent);
927 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
929 } else if (is_a<print_csrc>(c)) {
931 // Print function name in lowercase
932 std::string lname = opt.name;
933 size_t num = lname.size();
934 for (size_t i=0; i<num; i++)
935 lname[i] = tolower(lname[i]);
937 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
939 } else if (is_a<print_latex>(c)) {
941 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
944 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
949 // Method found, call it
950 current_serial = serial;
951 if (opt.print_use_exvector_args)
952 ((print_funcp_exvector)pdt[id])(seq, c);
953 else switch (opt.nparams) {
954 // the following lines have been generated for max. ${maxargs} parameters
955 ${print_switch_statement}
956 // end of generated lines
958 throw(std::logic_error("function::print(): invalid nparams"));
963 ex function::expand(unsigned options) const
965 // Only expand arguments when asked to do so
966 if (options & expand_options::expand_function_args)
967 return inherited::expand(options);
969 return (options == 0) ? setflag(status_flags::expanded) : *this;
972 ex function::eval(int level) const
975 // first evaluate children, then we will end up here again
976 return function(serial,evalchildren(level));
979 GINAC_ASSERT(serial<registered_functions().size());
980 const function_options &opt = registered_functions()[serial];
982 // Canonicalize argument order according to the symmetry properties
983 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
985 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
986 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
987 if (sig != std::numeric_limits<int>::max()) {
988 // Something has changed while sorting arguments, more evaluations later
991 return ex(sig) * thiscontainer(v);
999 bool use_remember = opt.use_remember;
1001 if (use_remember && lookup_remember_table(eval_result)) {
1004 current_serial = serial;
1005 if (opt.eval_use_exvector_args)
1006 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
1008 switch (opt.nparams) {
1009 // the following lines have been generated for max. ${maxargs} parameters
1010 ${eval_switch_statement}
1011 // end of generated lines
1013 throw(std::logic_error("function::eval(): invalid nparams"));
1016 store_remember_table(eval_result);
1021 ex function::evalf(int level) const
1023 GINAC_ASSERT(serial<registered_functions().size());
1024 const function_options &opt = registered_functions()[serial];
1026 // Evaluate children first
1028 if (level == 1 || !(opt.evalf_params_first))
1030 else if (level == -max_recursion_level)
1031 throw(std::runtime_error("max recursion level reached"));
1033 eseq.reserve(seq.size());
1035 exvector::const_iterator it = seq.begin(), itend = seq.end();
1036 while (it != itend) {
1037 eseq.push_back(it->evalf(level));
1042 if (opt.evalf_f==0) {
1043 return function(serial,eseq).hold();
1045 current_serial = serial;
1046 if (opt.evalf_use_exvector_args)
1047 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
1048 switch (opt.nparams) {
1049 // the following lines have been generated for max. ${maxargs} parameters
1050 ${evalf_switch_statement}
1051 // end of generated lines
1053 throw(std::logic_error("function::evalf(): invalid nparams"));
1056 unsigned function::calchash() const
1058 unsigned v = golden_ratio_hash(golden_ratio_hash((p_int)tinfo()) ^ serial);
1059 for (size_t i=0; i<nops(); i++) {
1061 v ^= this->op(i).gethash();
1064 if (flags & status_flags::evaluated) {
1065 setflag(status_flags::hash_calculated);
1071 ex function::thiscontainer(const exvector & v) const
1073 return function(serial, v);
1076 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
1078 return function(serial, vp);
1081 /** Implementation of ex::series for functions.
1082 * \@see ex::series */
1083 ex function::series(const relational & r, int order, unsigned options) const
1085 GINAC_ASSERT(serial<registered_functions().size());
1086 const function_options &opt = registered_functions()[serial];
1088 if (opt.series_f==0) {
1089 return basic::series(r, order);
1092 current_serial = serial;
1093 if (opt.series_use_exvector_args) {
1095 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
1096 } catch (do_taylor) {
1097 res = basic::series(r, order, options);
1101 switch (opt.nparams) {
1102 // the following lines have been generated for max. ${maxargs} parameters
1103 ${series_switch_statement}
1104 // end of generated lines
1106 throw(std::logic_error("function::series(): invalid nparams"));
1109 /** Implementation of ex::conjugate for functions. */
1110 ex function::conjugate() const
1112 GINAC_ASSERT(serial<registered_functions().size());
1113 const function_options & opt = registered_functions()[serial];
1115 if (opt.conjugate_f==0) {
1116 return exprseq::conjugate();
1119 if (opt.conjugate_use_exvector_args) {
1120 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
1123 switch (opt.nparams) {
1124 // the following lines have been generated for max. ${maxargs} parameters
1125 ${conjugate_switch_statement}
1126 // end of generated lines
1128 throw(std::logic_error("function::conjugate(): invalid nparams"));
1131 /** Implementation of ex::real_part for functions. */
1132 ex function::real_part() const
1134 GINAC_ASSERT(serial<registered_functions().size());
1135 const function_options & opt = registered_functions()[serial];
1137 if (opt.real_part_f==0)
1138 return basic::real_part();
1140 if (opt.real_part_use_exvector_args)
1141 return ((real_part_funcp_exvector)(opt.real_part_f))(seq);
1143 switch (opt.nparams) {
1144 // the following lines have been generated for max. ${maxargs} parameters
1145 ${real_part_switch_statement}
1146 // end of generated lines
1148 throw(std::logic_error("function::real_part(): invalid nparams"));
1151 /** Implementation of ex::imag_part for functions. */
1152 ex function::imag_part() const
1154 GINAC_ASSERT(serial<registered_functions().size());
1155 const function_options & opt = registered_functions()[serial];
1157 if (opt.imag_part_f==0)
1158 return basic::imag_part();
1160 if (opt.imag_part_use_exvector_args)
1161 return ((imag_part_funcp_exvector)(opt.imag_part_f))(seq);
1163 switch (opt.nparams) {
1164 // the following lines have been generated for max. ${maxargs} parameters
1165 ${imag_part_switch_statement}
1166 // end of generated lines
1168 throw(std::logic_error("function::imag_part(): invalid nparams"));
1173 /** Implementation of ex::diff() for functions. It applies the chain rule,
1174 * except for the Order term function.
1176 ex function::derivative(const symbol & s) const
1180 if (serial == Order_SERIAL::serial) {
1181 // Order Term function only differentiates the argument
1182 return Order(seq[0].diff(s));
1186 size_t num = seq.size();
1187 for (size_t i=0; i<num; i++) {
1188 arg_diff = seq[i].diff(s);
1189 // We apply the chain rule only when it makes sense. This is not
1190 // just for performance reasons but also to allow functions to
1191 // throw when differentiated with respect to one of its arguments
1192 // without running into trouble with our automatic full
1194 if (!arg_diff.is_zero())
1195 result += pderivative(i)*arg_diff;
1201 int function::compare_same_type(const basic & other) const
1203 GINAC_ASSERT(is_a<function>(other));
1204 const function & o = static_cast<const function &>(other);
1206 if (serial != o.serial)
1207 return serial < o.serial ? -1 : 1;
1209 return exprseq::compare_same_type(o);
1212 bool function::is_equal_same_type(const basic & other) const
1214 GINAC_ASSERT(is_a<function>(other));
1215 const function & o = static_cast<const function &>(other);
1217 if (serial != o.serial)
1220 return exprseq::is_equal_same_type(o);
1223 bool function::match_same_type(const basic & other) const
1225 GINAC_ASSERT(is_a<function>(other));
1226 const function & o = static_cast<const function &>(other);
1228 return serial == o.serial;
1231 unsigned function::return_type() const
1233 GINAC_ASSERT(serial<registered_functions().size());
1234 const function_options &opt = registered_functions()[serial];
1236 if (opt.use_return_type) {
1237 // Return type was explicitly specified
1238 return opt.return_type;
1240 // Default behavior is to use the return type of the first
1241 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1243 return return_types::commutative;
1245 return seq.begin()->return_type();
1249 tinfo_t function::return_type_tinfo() const
1251 GINAC_ASSERT(serial<registered_functions().size());
1252 const function_options &opt = registered_functions()[serial];
1254 if (opt.use_return_type) {
1255 // Return type was explicitly specified
1256 return opt.return_type_tinfo;
1258 // Default behavior is to use the return type of the first
1259 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1263 return seq.begin()->return_type_tinfo();
1268 // new virtual functions which can be overridden by derived classes
1274 // non-virtual functions in this class
1279 ex function::pderivative(unsigned diff_param) const // partial differentiation
1281 GINAC_ASSERT(serial<registered_functions().size());
1282 const function_options &opt = registered_functions()[serial];
1284 // No derivative defined? Then return abstract derivative object
1285 if (opt.derivative_f == NULL)
1286 return fderivative(serial, diff_param, seq);
1288 current_serial = serial;
1289 if (opt.derivative_use_exvector_args)
1290 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1291 switch (opt.nparams) {
1292 // the following lines have been generated for max. ${maxargs} parameters
1293 ${diff_switch_statement}
1294 // end of generated lines
1296 throw(std::logic_error("function::pderivative(): no diff function defined"));
1299 ex function::power(const ex & power_param) const // power of function
1301 GINAC_ASSERT(serial<registered_functions().size());
1302 const function_options &opt = registered_functions()[serial];
1304 // No derivative defined? Then return abstract derivative object
1305 if (opt.power_f == NULL)
1306 return (new power::power(*this, power_param))->setflag(status_flags::dynallocated |
1307 status_flags::evaluated);
1309 current_serial = serial;
1310 if (opt.power_use_exvector_args)
1311 return ((power_funcp_exvector)(opt.power_f))(seq, power_param);
1312 switch (opt.nparams) {
1313 // the following lines have been generated for max. ${maxargs} parameters
1314 ${power_switch_statement}
1315 // end of generated lines
1317 throw(std::logic_error("function::power(): no power function defined"));
1320 std::vector<function_options> & function::registered_functions()
1322 static std::vector<function_options> * rf = new std::vector<function_options>;
1326 bool function::lookup_remember_table(ex & result) const
1328 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1331 void function::store_remember_table(ex const & result) const
1333 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1338 unsigned function::register_new(function_options const & opt)
1340 size_t same_name = 0;
1341 for (size_t i=0; i<registered_functions().size(); ++i) {
1342 if (registered_functions()[i].name==opt.name) {
1346 if (same_name>=opt.functions_with_same_name) {
1347 // we do not throw an exception here because this code is
1348 // usually executed before main(), so the exception could not
1350 std::cerr << "WARNING: function name " << opt.name
1351 << " already in use!" << std::endl;
1353 registered_functions().push_back(opt);
1354 if (opt.use_remember) {
1355 remember_table::remember_tables().
1356 push_back(remember_table(opt.remember_size,
1357 opt.remember_assoc_size,
1358 opt.remember_strategy));
1360 remember_table::remember_tables().push_back(remember_table());
1362 return registered_functions().size()-1;
1365 /** Find serial number of function by name and number of parameters.
1366 * Throws exception if function was not found. */
1367 unsigned function::find_function(const std::string &name, unsigned nparams)
1369 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1370 unsigned serial = 0;
1372 if (i->get_name() == name && i->get_nparams() == nparams)
1377 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1380 /** Return the print name of the function. */
1381 std::string function::get_name() const
1383 GINAC_ASSERT(serial<registered_functions().size());
1384 return registered_functions()[serial].name;
1387 } // namespace GiNaC
1389 END_OF_IMPLEMENTATION
1391 print "Creating interface file function.h...";
1392 open OUT,">function.h" or die "cannot open function.h";
1393 print OUT $interface;
1397 print "Creating implementation file function.cpp...";
1398 open OUT,">function.cpp" or die "cannot open function.cpp";
1399 print OUT $implementation;