1 # This perl script automatically generates function.h and function.cpp
3 # function.pl options: \$maxargs=${maxargs}
5 # GiNaC Copyright (C) 1999-2010 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)
142 END_OF_CONSTRUCTORS_IMPLEMENTATION
144 $eval_switch_statement=generate(
145 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
147 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
149 END_OF_EVAL_SWITCH_STATEMENT
151 $evalf_switch_statement=generate(
152 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
154 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
155 END_OF_EVALF_SWITCH_STATEMENT
157 $conjugate_switch_statement=generate(
158 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
160 return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
161 END_OF_DIFF_SWITCH_STATEMENT
163 $real_part_switch_statement=generate(
164 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
166 return ((real_part_funcp_${N})(opt.real_part_f))(${SEQ1});
167 END_OF_DIFF_SWITCH_STATEMENT
169 $imag_part_switch_statement=generate(
170 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
172 return ((imag_part_funcp_${N})(opt.imag_part_f))(${SEQ1});
173 END_OF_DIFF_SWITCH_STATEMENT
175 $diff_switch_statement=generate(
176 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
178 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
179 END_OF_DIFF_SWITCH_STATEMENT
181 $power_switch_statement=generate(
182 <<'END_OF_POWER_SWITCH_STATEMENT','seq[${N}-1]','','');
184 return ((power_funcp_${N})(opt.power_f))(${SEQ1},power_param);
185 END_OF_POWER_SWITCH_STATEMENT
187 $series_switch_statement=generate(
188 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
191 res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
192 } catch (do_taylor) {
193 res = basic::series(r, order, options);
196 END_OF_SERIES_SWITCH_STATEMENT
198 $print_switch_statement=generate(
199 <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
201 ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
203 END_OF_PRINT_SWITCH_STATEMENT
205 $eval_func_implementation=generate(
206 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
207 function_options & function_options::eval_func(eval_funcp_${N} e)
209 test_and_set_nparams(${N});
210 eval_f = eval_funcp(e);
213 END_OF_EVAL_FUNC_IMPLEMENTATION
215 $evalf_func_implementation=generate(
216 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
217 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
219 test_and_set_nparams(${N});
220 evalf_f = evalf_funcp(ef);
223 END_OF_EVALF_FUNC_IMPLEMENTATION
225 $conjugate_func_implementation=generate(
226 <<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
227 function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
229 test_and_set_nparams(${N});
230 conjugate_f = conjugate_funcp(c);
233 END_OF_CONJUGATE_FUNC_IMPLEMENTATION
235 $real_part_func_implementation=generate(
236 <<'END_OF_REAL_PART_FUNC_IMPLEMENTATION','','','');
237 function_options & function_options::real_part_func(real_part_funcp_${N} c)
239 test_and_set_nparams(${N});
240 real_part_f = real_part_funcp(c);
243 END_OF_REAL_PART_FUNC_IMPLEMENTATION
245 $imag_part_func_implementation=generate(
246 <<'END_OF_IMAG_PART_FUNC_IMPLEMENTATION','','','');
247 function_options & function_options::imag_part_func(imag_part_funcp_${N} c)
249 test_and_set_nparams(${N});
250 imag_part_f = imag_part_funcp(c);
253 END_OF_IMAG_PART_FUNC_IMPLEMENTATION
255 $derivative_func_implementation=generate(
256 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
257 function_options & function_options::derivative_func(derivative_funcp_${N} d)
259 test_and_set_nparams(${N});
260 derivative_f = derivative_funcp(d);
263 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
265 $power_func_implementation=generate(
266 <<'END_OF_POWER_FUNC_IMPLEMENTATION','','','');
267 function_options & function_options::power_func(power_funcp_${N} d)
269 test_and_set_nparams(${N});
270 power_f = power_funcp(d);
273 END_OF_POWER_FUNC_IMPLEMENTATION
275 $series_func_implementation=generate(
276 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
277 function_options & function_options::series_func(series_funcp_${N} s)
279 test_and_set_nparams(${N});
280 series_f = series_funcp(s);
283 END_OF_SERIES_FUNC_IMPLEMENTATION
285 $interface=<<END_OF_INTERFACE;
286 /** \@file function.h
288 * Interface to class of symbolic functions. */
291 * This file was generated automatically by function.pl.
292 * Please do not modify it directly, edit the perl script instead!
293 * function.pl options: \$maxargs=${maxargs}
295 * GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
297 * This program is free software; you can redistribute it and/or modify
298 * it under the terms of the GNU General Public License as published by
299 * the Free Software Foundation; either version 2 of the License, or
300 * (at your option) any later version.
302 * This program is distributed in the hope that it will be useful,
303 * but WITHOUT ANY WARRANTY; without even the implied warranty of
304 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
305 * GNU General Public License for more details.
307 * You should have received a copy of the GNU General Public License
308 * along with this program; if not, write to the Free Software
309 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
312 #ifndef GINAC_FUNCTION_H
313 #define GINAC_FUNCTION_H
317 // CINT needs <algorithm> to work properly with <vector>
322 // the following lines have been generated for max. ${maxargs} parameters
323 $declare_function_macro
324 // end of generated lines
326 #define REGISTER_FUNCTION(NAME,OPT) \\
327 unsigned NAME##_SERIAL::serial = \\
328 GiNaC::function::register_new(GiNaC::function_options(#NAME, NAME##_NPARAMS).OPT);
335 typedef ex (* eval_funcp)();
336 typedef ex (* evalf_funcp)();
337 typedef ex (* conjugate_funcp)();
338 typedef ex (* real_part_funcp)();
339 typedef ex (* imag_part_funcp)();
340 typedef ex (* derivative_funcp)();
341 typedef ex (* power_funcp)();
342 typedef ex (* series_funcp)();
343 typedef void (* print_funcp)();
345 // the following lines have been generated for max. ${maxargs} parameters
348 $typedef_conjugate_funcp
349 $typedef_real_part_funcp
350 $typedef_imag_part_funcp
351 $typedef_derivative_funcp
353 $typedef_series_funcp
355 // end of generated lines
357 // Alternatively, an exvector may be passed into the static function, instead
358 // of individual ex objects. Then, the number of arguments is not limited.
359 typedef ex (* eval_funcp_exvector)(const exvector &);
360 typedef ex (* evalf_funcp_exvector)(const exvector &);
361 typedef ex (* conjugate_funcp_exvector)(const exvector &);
362 typedef ex (* real_part_funcp_exvector)(const exvector &);
363 typedef ex (* imag_part_funcp_exvector)(const exvector &);
364 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
365 typedef ex (* power_funcp_exvector)(const exvector &, const ex &);
366 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
367 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
370 class function_options
372 friend class function;
373 friend class fderivative;
376 function_options(std::string const & n, std::string const & tn=std::string());
377 function_options(std::string const & n, unsigned np);
381 function_options & dummy() { return *this; }
382 function_options & set_name(std::string const & n, std::string const & tn=std::string());
383 function_options & latex_name(std::string const & tn);
384 // the following lines have been generated for max. ${maxargs} parameters
386 $evalf_func_interface
387 $conjugate_func_interface
388 $real_part_func_interface
389 $imag_part_func_interface
390 $derivative_func_interface
391 $power_func_interface
392 $series_func_interface
393 $print_func_interface
394 // end of generated lines
395 function_options & eval_func(eval_funcp_exvector e);
396 function_options & evalf_func(evalf_funcp_exvector ef);
397 function_options & conjugate_func(conjugate_funcp_exvector d);
398 function_options & real_part_func(real_part_funcp_exvector d);
399 function_options & imag_part_func(imag_part_funcp_exvector d);
400 function_options & derivative_func(derivative_funcp_exvector d);
401 function_options & power_func(power_funcp_exvector d);
402 function_options & series_func(series_funcp_exvector s);
404 template <class Ctx> function_options & print_func(print_funcp_exvector p)
406 print_use_exvector_args = true;
407 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
411 function_options & set_return_type(unsigned rt, const return_type_t* rtt = 0);
412 function_options & do_not_evalf_params();
413 function_options & remember(unsigned size, unsigned assoc_size=0,
414 unsigned strategy=remember_strategies::delete_never);
415 function_options & overloaded(unsigned o);
416 function_options & set_symmetry(const symmetry & s);
418 std::string get_name() const { return name; }
419 unsigned get_nparams() const { return nparams; }
422 bool has_derivative() const { return derivative_f != NULL; }
423 bool has_power() const { return power_f != NULL; }
424 void test_and_set_nparams(unsigned n);
425 void set_print_func(unsigned id, print_funcp f);
428 std::string TeX_name;
434 conjugate_funcp conjugate_f;
435 real_part_funcp real_part_f;
436 imag_part_funcp imag_part_f;
437 derivative_funcp derivative_f;
439 series_funcp series_f;
440 std::vector<print_funcp> print_dispatch_table;
442 bool evalf_params_first;
444 bool use_return_type;
445 unsigned return_type;
446 return_type_t return_type_tinfo;
449 unsigned remember_size;
450 unsigned remember_assoc_size;
451 unsigned remember_strategy;
453 bool eval_use_exvector_args;
454 bool evalf_use_exvector_args;
455 bool conjugate_use_exvector_args;
456 bool real_part_use_exvector_args;
457 bool imag_part_use_exvector_args;
458 bool derivative_use_exvector_args;
459 bool power_use_exvector_args;
460 bool series_use_exvector_args;
461 bool print_use_exvector_args;
463 unsigned functions_with_same_name;
469 /** Exception class thrown by classes which provide their own series expansion
470 * to signal that ordinary Taylor expansion is safe. */
474 /** The class function is used to implement builtin functions like sin, cos...
475 and user defined functions */
476 class function : public exprseq
478 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
480 // CINT has a linking problem
482 friend void ginsh_get_ginac_functions();
483 #endif // def __MAKECINT__
485 friend class remember_table_entry;
486 // friend class remember_table_list;
487 // friend class remember_table;
491 // other constructors
493 function(unsigned ser);
494 // the following lines have been generated for max. ${maxargs} parameters
495 $constructors_interface
496 // end of generated lines
497 function(unsigned ser, const exprseq & es);
498 function(unsigned ser, const exvector & v, bool discardable = false);
499 function(unsigned ser, std::auto_ptr<exvector> vp);
501 // functions overriding virtual functions from base classes
503 void print(const print_context & c, unsigned level = 0) const;
504 unsigned precedence() const {return 70;}
505 ex expand(unsigned options=0) const;
506 ex eval(int level=0) const;
507 ex evalf(int level=0) const;
508 ex eval_ncmul(const exvector & v) const;
509 unsigned calchash() const;
510 ex series(const relational & r, int order, unsigned options = 0) const;
511 ex thiscontainer(const exvector & v) const;
512 ex thiscontainer(std::auto_ptr<exvector> vp) const;
513 ex conjugate() const;
514 ex real_part() const;
515 ex imag_part() const;
516 void archive(archive_node& n) const;
517 void read_archive(const archive_node& n, lst& syms);
519 ex derivative(const symbol & s) const;
520 bool is_equal_same_type(const basic & other) const;
521 bool match_same_type(const basic & other) const;
522 unsigned return_type() const;
523 return_type_t return_type_tinfo() const;
525 // new virtual functions which can be overridden by derived classes
528 // non-virtual functions in this class
530 ex pderivative(unsigned diff_param) const; // partial differentiation
531 static std::vector<function_options> & registered_functions();
532 bool lookup_remember_table(ex & result) const;
533 void store_remember_table(ex const & result) const;
535 ex power(const ex & exp) const;
536 static unsigned register_new(function_options const & opt);
537 static unsigned current_serial;
538 static unsigned find_function(const std::string &name, unsigned nparams);
539 unsigned get_serial() const {return serial;}
540 std::string get_name() const;
547 GINAC_DECLARE_UNARCHIVER(function);
549 // utility functions/macros
551 template <typename T>
552 inline bool is_the_function(const ex & x)
554 return is_exactly_a<function>(x)
555 && ex_to<function>(x).get_serial() == T::serial;
558 // Check whether OBJ is the specified symbolic function.
559 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
563 #endif // ndef GINAC_FUNCTION_H
567 $implementation=<<END_OF_IMPLEMENTATION;
568 /** \@file function.cpp
570 * Implementation of class of symbolic functions. */
573 * This file was generated automatically by function.pl.
574 * Please do not modify it directly, edit the perl script instead!
575 * function.pl options: \$maxargs=${maxargs}
577 * GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
579 * This program is free software; you can redistribute it and/or modify
580 * it under the terms of the GNU General Public License as published by
581 * the Free Software Foundation; either version 2 of the License, or
582 * (at your option) any later version.
584 * This program is distributed in the hope that it will be useful,
585 * but WITHOUT ANY WARRANTY; without even the implied warranty of
586 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
587 * GNU General Public License for more details.
589 * You should have received a copy of the GNU General Public License
590 * along with this program; if not, write to the Free Software
591 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
594 #include "function.h"
595 #include "operators.h"
596 #include "fderivative.h"
599 #include "symmetry.h"
604 #include "tostring.h"
606 #include "hash_seed.h"
607 #include "remember.h"
618 // helper class function_options
621 function_options::function_options()
626 function_options::function_options(std::string const & n, std::string const & tn)
632 function_options::function_options(std::string const & n, unsigned np)
635 set_name(n, std::string());
639 function_options::~function_options()
641 // nothing to clean up at the moment
644 void function_options::initialize()
646 set_name("unnamed_function", "\\\\mbox{unnamed}");
648 eval_f = evalf_f = real_part_f = imag_part_f = conjugate_f = derivative_f
649 = power_f = series_f = 0;
650 evalf_params_first = true;
651 use_return_type = false;
652 eval_use_exvector_args = false;
653 evalf_use_exvector_args = false;
654 conjugate_use_exvector_args = false;
655 real_part_use_exvector_args = false;
656 imag_part_use_exvector_args = false;
657 derivative_use_exvector_args = false;
658 power_use_exvector_args = false;
659 series_use_exvector_args = false;
660 print_use_exvector_args = false;
661 use_remember = false;
662 functions_with_same_name = 1;
666 function_options & function_options::set_name(std::string const & n,
667 std::string const & tn)
670 if (tn==std::string())
671 TeX_name = "\\\\mbox{"+name+"}";
677 function_options & function_options::latex_name(std::string const & tn)
683 // the following lines have been generated for max. ${maxargs} parameters
684 $eval_func_implementation
685 $evalf_func_implementation
686 $conjugate_func_implementation
687 $real_part_func_implementation
688 $imag_part_func_implementation
689 $derivative_func_implementation
690 $power_func_implementation
691 $series_func_implementation
692 // end of generated lines
694 function_options& function_options::eval_func(eval_funcp_exvector e)
696 eval_use_exvector_args = true;
697 eval_f = eval_funcp(e);
700 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
702 evalf_use_exvector_args = true;
703 evalf_f = evalf_funcp(ef);
706 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
708 conjugate_use_exvector_args = true;
709 conjugate_f = conjugate_funcp(c);
712 function_options& function_options::real_part_func(real_part_funcp_exvector c)
714 real_part_use_exvector_args = true;
715 real_part_f = real_part_funcp(c);
718 function_options& function_options::imag_part_func(imag_part_funcp_exvector c)
720 imag_part_use_exvector_args = true;
721 imag_part_f = imag_part_funcp(c);
725 function_options& function_options::derivative_func(derivative_funcp_exvector d)
727 derivative_use_exvector_args = true;
728 derivative_f = derivative_funcp(d);
731 function_options& function_options::power_func(power_funcp_exvector d)
733 power_use_exvector_args = true;
734 power_f = power_funcp(d);
737 function_options& function_options::series_func(series_funcp_exvector s)
739 series_use_exvector_args = true;
740 series_f = series_funcp(s);
744 function_options & function_options::set_return_type(unsigned rt, const return_type_t* rtt)
746 use_return_type = true;
749 return_type_tinfo = *rtt;
751 return_type_tinfo = make_return_type_t<function>();
755 function_options & function_options::do_not_evalf_params()
757 evalf_params_first = false;
761 function_options & function_options::remember(unsigned size,
766 remember_size = size;
767 remember_assoc_size = assoc_size;
768 remember_strategy = strategy;
772 function_options & function_options::overloaded(unsigned o)
774 functions_with_same_name = o;
778 function_options & function_options::set_symmetry(const symmetry & s)
784 void function_options::test_and_set_nparams(unsigned n)
788 } else if (nparams!=n) {
789 // we do not throw an exception here because this code is
790 // usually executed before main(), so the exception could not
792 std::cerr << "WARNING: " << name << "(): number of parameters ("
793 << n << ") differs from number set before ("
794 << nparams << ")" << std::endl;
798 void function_options::set_print_func(unsigned id, print_funcp f)
800 if (id >= print_dispatch_table.size())
801 print_dispatch_table.resize(id + 1);
802 print_dispatch_table[id] = f;
805 /** This can be used as a hook for external applications. */
806 unsigned function::current_serial = 0;
809 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
812 // default constructor
817 function::function() : serial(0)
822 // other constructors
827 function::function(unsigned ser) : serial(ser)
831 // the following lines have been generated for max. ${maxargs} parameters
832 $constructors_implementation
833 // end of generated lines
835 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
838 // Force re-evaluation even if the exprseq was already evaluated
839 // (the exprseq copy constructor copies the flags)
840 clearflag(status_flags::evaluated);
843 function::function(unsigned ser, const exvector & v, bool discardable)
844 : exprseq(v,discardable), serial(ser)
846 if ( ser >= registered_functions().size() ) {
847 throw std::runtime_error("function does not exist");
851 function::function(unsigned ser, std::auto_ptr<exvector> vp)
852 : exprseq(vp), serial(ser)
860 /** Construct object from archive_node. */
861 void function::read_archive(const archive_node& n, lst& sym_lst)
863 inherited::read_archive(n, sym_lst);
864 // Find serial number by function name
866 if (n.find_string("name", s)) {
867 unsigned int ser = 0;
868 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
876 throw (std::runtime_error("unknown function '" + s + "' in archive"));
878 throw (std::runtime_error("unnamed function in archive"));
881 /** Archive the object. */
882 void function::archive(archive_node &n) const
884 inherited::archive(n);
885 GINAC_ASSERT(serial < registered_functions().size());
886 n.add_string("name", registered_functions()[serial].name);
889 GINAC_BIND_UNARCHIVER(function);
892 // functions overriding virtual functions from base classes
897 void function::print(const print_context & c, unsigned level) const
899 GINAC_ASSERT(serial<registered_functions().size());
900 const function_options &opt = registered_functions()[serial];
901 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
903 // Dynamically dispatch on print_context type
904 const print_context_class_info *pc_info = &c.get_class_info();
907 unsigned id = pc_info->options.get_id();
908 if (id >= pdt.size() || pdt[id] == NULL) {
910 // Method not found, try parent print_context class
911 const print_context_class_info *parent_pc_info = pc_info->get_parent();
912 if (parent_pc_info) {
913 pc_info = parent_pc_info;
917 // Method still not found, use default output
918 if (is_a<print_tree>(c)) {
920 c.s << std::string(level, ' ') << class_name() << " "
921 << opt.name << " @" << this
922 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
923 << ", nops=" << nops()
925 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
926 for (size_t i=0; i<seq.size(); ++i)
927 seq[i].print(c, level + delta_indent);
928 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
930 } else if (is_a<print_csrc>(c)) {
932 // Print function name in lowercase
933 std::string lname = opt.name;
934 size_t num = lname.size();
935 for (size_t i=0; i<num; i++)
936 lname[i] = tolower(lname[i]);
938 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
940 } else if (is_a<print_latex>(c)) {
942 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
945 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
950 // Method found, call it
951 current_serial = serial;
952 if (opt.print_use_exvector_args)
953 ((print_funcp_exvector)pdt[id])(seq, c);
954 else switch (opt.nparams) {
955 // the following lines have been generated for max. ${maxargs} parameters
956 ${print_switch_statement}
957 // end of generated lines
959 throw(std::logic_error("function::print(): invalid nparams"));
964 ex function::expand(unsigned options) const
966 // Only expand arguments when asked to do so
967 if (options & expand_options::expand_function_args)
968 return inherited::expand(options);
970 return (options == 0) ? setflag(status_flags::expanded) : *this;
973 ex function::eval(int level) const
976 // first evaluate children, then we will end up here again
977 return function(serial,evalchildren(level));
980 GINAC_ASSERT(serial<registered_functions().size());
981 const function_options &opt = registered_functions()[serial];
983 // Canonicalize argument order according to the symmetry properties
984 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
986 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
987 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
988 if (sig != std::numeric_limits<int>::max()) {
989 // Something has changed while sorting arguments, more evaluations later
992 return ex(sig) * thiscontainer(v);
1000 bool use_remember = opt.use_remember;
1002 if (use_remember && lookup_remember_table(eval_result)) {
1005 current_serial = serial;
1006 if (opt.eval_use_exvector_args)
1007 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
1009 switch (opt.nparams) {
1010 // the following lines have been generated for max. ${maxargs} parameters
1011 ${eval_switch_statement}
1012 // end of generated lines
1014 throw(std::logic_error("function::eval(): invalid nparams"));
1017 store_remember_table(eval_result);
1022 ex function::evalf(int level) const
1024 GINAC_ASSERT(serial<registered_functions().size());
1025 const function_options &opt = registered_functions()[serial];
1027 // Evaluate children first
1029 if (level == 1 || !(opt.evalf_params_first))
1031 else if (level == -max_recursion_level)
1032 throw(std::runtime_error("max recursion level reached"));
1034 eseq.reserve(seq.size());
1036 exvector::const_iterator it = seq.begin(), itend = seq.end();
1037 while (it != itend) {
1038 eseq.push_back(it->evalf(level));
1043 if (opt.evalf_f==0) {
1044 return function(serial,eseq).hold();
1046 current_serial = serial;
1047 if (opt.evalf_use_exvector_args)
1048 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
1049 switch (opt.nparams) {
1050 // the following lines have been generated for max. ${maxargs} parameters
1051 ${evalf_switch_statement}
1052 // end of generated lines
1054 throw(std::logic_error("function::evalf(): invalid nparams"));
1058 * This method is defined to be in line with behaviour of function::return_type()
1060 ex function::eval_ncmul(const exvector & v) const
1062 // If this function is called then the list of arguments is non-empty
1063 // and the first argument is non-commutative, see function::return_type()
1064 return seq.begin()->eval_ncmul(v);
1067 unsigned function::calchash() const
1069 unsigned v = golden_ratio_hash(make_hash_seed(typeid(*this)) ^ serial);
1070 for (size_t i=0; i<nops(); i++) {
1072 v ^= this->op(i).gethash();
1075 if (flags & status_flags::evaluated) {
1076 setflag(status_flags::hash_calculated);
1082 ex function::thiscontainer(const exvector & v) const
1084 return function(serial, v);
1087 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
1089 return function(serial, vp);
1092 /** Implementation of ex::series for functions.
1093 * \@see ex::series */
1094 ex function::series(const relational & r, int order, unsigned options) const
1096 GINAC_ASSERT(serial<registered_functions().size());
1097 const function_options &opt = registered_functions()[serial];
1099 if (opt.series_f==0) {
1100 return basic::series(r, order);
1103 current_serial = serial;
1104 if (opt.series_use_exvector_args) {
1106 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
1107 } catch (do_taylor) {
1108 res = basic::series(r, order, options);
1112 switch (opt.nparams) {
1113 // the following lines have been generated for max. ${maxargs} parameters
1114 ${series_switch_statement}
1115 // end of generated lines
1117 throw(std::logic_error("function::series(): invalid nparams"));
1120 /** Implementation of ex::conjugate for functions. */
1121 ex function::conjugate() const
1123 GINAC_ASSERT(serial<registered_functions().size());
1124 const function_options & opt = registered_functions()[serial];
1126 if (opt.conjugate_f==0) {
1127 return conjugate_function(*this).hold();
1130 if (opt.conjugate_use_exvector_args) {
1131 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
1134 switch (opt.nparams) {
1135 // the following lines have been generated for max. ${maxargs} parameters
1136 ${conjugate_switch_statement}
1137 // end of generated lines
1139 throw(std::logic_error("function::conjugate(): invalid nparams"));
1142 /** Implementation of ex::real_part for functions. */
1143 ex function::real_part() const
1145 GINAC_ASSERT(serial<registered_functions().size());
1146 const function_options & opt = registered_functions()[serial];
1148 if (opt.real_part_f==0)
1149 return basic::real_part();
1151 if (opt.real_part_use_exvector_args)
1152 return ((real_part_funcp_exvector)(opt.real_part_f))(seq);
1154 switch (opt.nparams) {
1155 // the following lines have been generated for max. ${maxargs} parameters
1156 ${real_part_switch_statement}
1157 // end of generated lines
1159 throw(std::logic_error("function::real_part(): invalid nparams"));
1162 /** Implementation of ex::imag_part for functions. */
1163 ex function::imag_part() const
1165 GINAC_ASSERT(serial<registered_functions().size());
1166 const function_options & opt = registered_functions()[serial];
1168 if (opt.imag_part_f==0)
1169 return basic::imag_part();
1171 if (opt.imag_part_use_exvector_args)
1172 return ((imag_part_funcp_exvector)(opt.imag_part_f))(seq);
1174 switch (opt.nparams) {
1175 // the following lines have been generated for max. ${maxargs} parameters
1176 ${imag_part_switch_statement}
1177 // end of generated lines
1179 throw(std::logic_error("function::imag_part(): invalid nparams"));
1184 /** Implementation of ex::diff() for functions. It applies the chain rule,
1185 * except for the Order term function.
1187 ex function::derivative(const symbol & s) const
1191 if (serial == Order_SERIAL::serial) {
1192 // Order Term function only differentiates the argument
1193 return Order(seq[0].diff(s));
1197 size_t num = seq.size();
1198 for (size_t i=0; i<num; i++) {
1199 arg_diff = seq[i].diff(s);
1200 // We apply the chain rule only when it makes sense. This is not
1201 // just for performance reasons but also to allow functions to
1202 // throw when differentiated with respect to one of its arguments
1203 // without running into trouble with our automatic full
1205 if (!arg_diff.is_zero())
1206 result += pderivative(i)*arg_diff;
1212 int function::compare_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)
1218 return serial < o.serial ? -1 : 1;
1220 return exprseq::compare_same_type(o);
1223 bool function::is_equal_same_type(const basic & other) const
1225 GINAC_ASSERT(is_a<function>(other));
1226 const function & o = static_cast<const function &>(other);
1228 if (serial != o.serial)
1231 return exprseq::is_equal_same_type(o);
1234 bool function::match_same_type(const basic & other) const
1236 GINAC_ASSERT(is_a<function>(other));
1237 const function & o = static_cast<const function &>(other);
1239 return serial == o.serial;
1242 unsigned function::return_type() const
1244 GINAC_ASSERT(serial<registered_functions().size());
1245 const function_options &opt = registered_functions()[serial];
1247 if (opt.use_return_type) {
1248 // Return type was explicitly specified
1249 return opt.return_type;
1251 // Default behavior is to use the return type of the first
1252 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1254 return return_types::commutative;
1256 return seq.begin()->return_type();
1260 return_type_t function::return_type_tinfo() const
1262 GINAC_ASSERT(serial<registered_functions().size());
1263 const function_options &opt = registered_functions()[serial];
1265 if (opt.use_return_type) {
1266 // Return type was explicitly specified
1267 return opt.return_type_tinfo;
1269 // Default behavior is to use the return type of the first
1270 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1272 return make_return_type_t<function>();
1274 return seq.begin()->return_type_tinfo();
1279 // new virtual functions which can be overridden by derived classes
1285 // non-virtual functions in this class
1290 ex function::pderivative(unsigned diff_param) const // partial differentiation
1292 GINAC_ASSERT(serial<registered_functions().size());
1293 const function_options &opt = registered_functions()[serial];
1295 // No derivative defined? Then return abstract derivative object
1296 if (opt.derivative_f == NULL)
1297 return fderivative(serial, diff_param, seq);
1299 current_serial = serial;
1300 if (opt.derivative_use_exvector_args)
1301 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1302 switch (opt.nparams) {
1303 // the following lines have been generated for max. ${maxargs} parameters
1304 ${diff_switch_statement}
1305 // end of generated lines
1307 throw(std::logic_error("function::pderivative(): no diff function defined"));
1310 ex function::power(const ex & power_param) const // power of function
1312 GINAC_ASSERT(serial<registered_functions().size());
1313 const function_options &opt = registered_functions()[serial];
1315 // No derivative defined? Then return abstract derivative object
1316 if (opt.power_f == NULL)
1317 return (new GiNaC::power(*this, power_param))->setflag(status_flags::dynallocated |
1318 status_flags::evaluated);
1320 current_serial = serial;
1321 if (opt.power_use_exvector_args)
1322 return ((power_funcp_exvector)(opt.power_f))(seq, power_param);
1323 switch (opt.nparams) {
1324 // the following lines have been generated for max. ${maxargs} parameters
1325 ${power_switch_statement}
1326 // end of generated lines
1328 throw(std::logic_error("function::power(): no power function defined"));
1331 std::vector<function_options> & function::registered_functions()
1333 static std::vector<function_options> rf = std::vector<function_options>();
1337 bool function::lookup_remember_table(ex & result) const
1339 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1342 void function::store_remember_table(ex const & result) const
1344 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1349 unsigned function::register_new(function_options const & opt)
1351 size_t same_name = 0;
1352 for (size_t i=0; i<registered_functions().size(); ++i) {
1353 if (registered_functions()[i].name==opt.name) {
1357 if (same_name>=opt.functions_with_same_name) {
1358 // we do not throw an exception here because this code is
1359 // usually executed before main(), so the exception could not
1361 std::cerr << "WARNING: function name " << opt.name
1362 << " already in use!" << std::endl;
1364 registered_functions().push_back(opt);
1365 if (opt.use_remember) {
1366 remember_table::remember_tables().
1367 push_back(remember_table(opt.remember_size,
1368 opt.remember_assoc_size,
1369 opt.remember_strategy));
1371 remember_table::remember_tables().push_back(remember_table());
1373 return registered_functions().size()-1;
1376 /** Find serial number of function by name and number of parameters.
1377 * Throws exception if function was not found. */
1378 unsigned function::find_function(const std::string &name, unsigned nparams)
1380 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1381 unsigned serial = 0;
1383 if (i->get_name() == name && i->get_nparams() == nparams)
1388 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1391 /** Return the print name of the function. */
1392 std::string function::get_name() const
1394 if ( serial >= registered_functions().size() ) {
1395 throw std::runtime_error("unknown function");
1397 return registered_functions()[serial].name;
1400 } // namespace GiNaC
1402 END_OF_IMPLEMENTATION
1404 print "Creating interface file function.h...";
1405 open OUT,">function.h" or die "cannot open function.h";
1406 print OUT $interface;
1410 print "Creating implementation file function.cpp...";
1411 open OUT,">function.cpp" or die "cannot open function.cpp";
1412 print OUT $implementation;