1 # This perl script automatically generates function.h and function.cpp
3 # function.pl options: \$maxargs=${maxargs}
5 # GiNaC Copyright (C) 1999-2006 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 = &function::tinfo_static;
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);
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 const basic* 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 const basic* 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 template <typename T>
490 inline bool is_the_function(const ex & x)
492 return is_exactly_a<function>(x)
493 && ex_to<function>(x).get_serial() == T::serial;
496 // Check whether OBJ is the specified symbolic function.
497 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
501 #endif // ndef __GINAC_FUNCTION_H__
505 $implementation=<<END_OF_IMPLEMENTATION;
506 /** \@file function.cpp
508 * Implementation of class of symbolic functions. */
511 * This file was generated automatically by function.pl.
512 * Please do not modify it directly, edit the perl script instead!
513 * function.pl options: \$maxargs=${maxargs}
515 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
517 * This program is free software; you can redistribute it and/or modify
518 * it under the terms of the GNU General Public License as published by
519 * the Free Software Foundation; either version 2 of the License, or
520 * (at your option) any later version.
522 * This program is distributed in the hope that it will be useful,
523 * but WITHOUT ANY WARRANTY; without even the implied warranty of
524 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
525 * GNU General Public License for more details.
527 * You should have received a copy of the GNU General Public License
528 * along with this program; if not, write to the Free Software
529 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
537 #include "function.h"
538 #include "operators.h"
539 #include "fderivative.h"
542 #include "symmetry.h"
547 #include "tostring.h"
549 #include "remember.h"
554 // helper class function_options
557 function_options::function_options()
562 function_options::function_options(std::string const & n, std::string const & tn)
568 function_options::function_options(std::string const & n, unsigned np)
571 set_name(n, std::string());
575 function_options::~function_options()
577 // nothing to clean up at the moment
580 void function_options::initialize()
582 set_name("unnamed_function", "\\\\mbox{unnamed}");
584 eval_f = evalf_f = conjugate_f = derivative_f = power_f = series_f = 0;
585 evalf_params_first = true;
586 use_return_type = false;
587 eval_use_exvector_args = false;
588 evalf_use_exvector_args = false;
589 conjugate_use_exvector_args = false;
590 derivative_use_exvector_args = false;
591 power_use_exvector_args = false;
592 series_use_exvector_args = false;
593 print_use_exvector_args = false;
594 use_remember = false;
595 functions_with_same_name = 1;
599 function_options & function_options::set_name(std::string const & n,
600 std::string const & tn)
603 if (tn==std::string())
604 TeX_name = "\\\\mbox{"+name+"}";
610 function_options & function_options::latex_name(std::string const & tn)
616 // the following lines have been generated for max. ${maxargs} parameters
617 $eval_func_implementation
618 $evalf_func_implementation
619 $conjugate_func_implementation
620 $derivative_func_implementation
621 $power_func_implementation
622 $series_func_implementation
623 // end of generated lines
625 function_options& function_options::eval_func(eval_funcp_exvector e)
627 eval_use_exvector_args = true;
628 eval_f = eval_funcp(e);
631 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
633 evalf_use_exvector_args = true;
634 evalf_f = evalf_funcp(ef);
637 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
639 conjugate_use_exvector_args = true;
640 conjugate_f = conjugate_funcp(c);
643 function_options& function_options::derivative_func(derivative_funcp_exvector d)
645 derivative_use_exvector_args = true;
646 derivative_f = derivative_funcp(d);
649 function_options& function_options::power_func(power_funcp_exvector d)
651 power_use_exvector_args = true;
652 power_f = power_funcp(d);
655 function_options& function_options::series_func(series_funcp_exvector s)
657 series_use_exvector_args = true;
658 series_f = series_funcp(s);
662 function_options & function_options::set_return_type(unsigned rt)
664 use_return_type = true;
669 function_options & function_options::do_not_evalf_params()
671 evalf_params_first = false;
675 function_options & function_options::remember(unsigned size,
680 remember_size = size;
681 remember_assoc_size = assoc_size;
682 remember_strategy = strategy;
686 function_options & function_options::overloaded(unsigned o)
688 functions_with_same_name = o;
692 function_options & function_options::set_symmetry(const symmetry & s)
698 void function_options::test_and_set_nparams(unsigned n)
702 } else if (nparams!=n) {
703 // we do not throw an exception here because this code is
704 // usually executed before main(), so the exception could not
706 std::cerr << "WARNING: " << name << "(): number of parameters ("
707 << n << ") differs from number set before ("
708 << nparams << ")" << std::endl;
712 void function_options::set_print_func(unsigned id, print_funcp f)
714 if (id >= print_dispatch_table.size())
715 print_dispatch_table.resize(id + 1);
716 print_dispatch_table[id] = f;
719 /** This can be used as a hook for external applications. */
720 unsigned function::current_serial = 0;
723 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
726 // default constructor
731 function::function() : serial(0)
733 tinfo_key = &function::tinfo_static;
737 // other constructors
742 function::function(unsigned ser) : serial(ser)
744 tinfo_key = &function::tinfo_static;
747 // the following lines have been generated for max. ${maxargs} parameters
748 $constructors_implementation
749 // end of generated lines
751 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
753 tinfo_key = &function::tinfo_static;
755 // Force re-evaluation even if the exprseq was already evaluated
756 // (the exprseq copy constructor copies the flags)
757 clearflag(status_flags::evaluated);
760 function::function(unsigned ser, const exvector & v, bool discardable)
761 : exprseq(v,discardable), serial(ser)
763 tinfo_key = &function::tinfo_static;
766 function::function(unsigned ser, std::auto_ptr<exvector> vp)
767 : exprseq(vp), serial(ser)
769 tinfo_key = &function::tinfo_static;
776 /** Construct object from archive_node. */
777 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
779 // Find serial number by function name
781 if (n.find_string("name", s)) {
782 unsigned int ser = 0;
783 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
791 throw (std::runtime_error("unknown function '" + s + "' in archive"));
793 throw (std::runtime_error("unnamed function in archive"));
796 /** Unarchive the object. */
797 ex function::unarchive(const archive_node &n, lst &sym_lst)
799 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
802 /** Archive the object. */
803 void function::archive(archive_node &n) const
805 inherited::archive(n);
806 GINAC_ASSERT(serial < registered_functions().size());
807 n.add_string("name", registered_functions()[serial].name);
811 // functions overriding virtual functions from base classes
816 void function::print(const print_context & c, unsigned level) const
818 GINAC_ASSERT(serial<registered_functions().size());
819 const function_options &opt = registered_functions()[serial];
820 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
822 // Dynamically dispatch on print_context type
823 const print_context_class_info *pc_info = &c.get_class_info();
826 unsigned id = pc_info->options.get_id();
827 if (id >= pdt.size() || pdt[id] == NULL) {
829 // Method not found, try parent print_context class
830 const print_context_class_info *parent_pc_info = pc_info->get_parent();
831 if (parent_pc_info) {
832 pc_info = parent_pc_info;
836 // Method still not found, use default output
837 if (is_a<print_tree>(c)) {
839 c.s << std::string(level, ' ') << class_name() << " "
840 << opt.name << " @" << this
841 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
842 << ", nops=" << nops()
844 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
845 for (size_t i=0; i<seq.size(); ++i)
846 seq[i].print(c, level + delta_indent);
847 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
849 } else if (is_a<print_csrc>(c)) {
851 // Print function name in lowercase
852 std::string lname = opt.name;
853 size_t num = lname.size();
854 for (size_t i=0; i<num; i++)
855 lname[i] = tolower(lname[i]);
857 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
859 } else if (is_a<print_latex>(c)) {
861 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
864 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
869 // Method found, call it
870 current_serial = serial;
871 if (opt.print_use_exvector_args)
872 ((print_funcp_exvector)pdt[id])(seq, c);
873 else switch (opt.nparams) {
874 // the following lines have been generated for max. ${maxargs} parameters
875 ${print_switch_statement}
876 // end of generated lines
878 throw(std::logic_error("function::print(): invalid nparams"));
883 ex function::expand(unsigned options) const
885 // Only expand arguments when asked to do so
886 if (options & expand_options::expand_function_args)
887 return inherited::expand(options);
889 return (options == 0) ? setflag(status_flags::expanded) : *this;
892 ex function::eval(int level) const
895 // first evaluate children, then we will end up here again
896 return function(serial,evalchildren(level));
899 GINAC_ASSERT(serial<registered_functions().size());
900 const function_options &opt = registered_functions()[serial];
902 // Canonicalize argument order according to the symmetry properties
903 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
905 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
906 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
907 if (sig != INT_MAX) {
908 // Something has changed while sorting arguments, more evaluations later
911 return ex(sig) * thiscontainer(v);
919 bool use_remember = opt.use_remember;
921 if (use_remember && lookup_remember_table(eval_result)) {
924 current_serial = serial;
925 if (opt.eval_use_exvector_args)
926 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
928 switch (opt.nparams) {
929 // the following lines have been generated for max. ${maxargs} parameters
930 ${eval_switch_statement}
931 // end of generated lines
933 throw(std::logic_error("function::eval(): invalid nparams"));
936 store_remember_table(eval_result);
941 ex function::evalf(int level) const
943 GINAC_ASSERT(serial<registered_functions().size());
944 const function_options &opt = registered_functions()[serial];
946 // Evaluate children first
948 if (level == 1 || !(opt.evalf_params_first))
950 else if (level == -max_recursion_level)
951 throw(std::runtime_error("max recursion level reached"));
953 eseq.reserve(seq.size());
955 exvector::const_iterator it = seq.begin(), itend = seq.end();
956 while (it != itend) {
957 eseq.push_back(it->evalf(level));
962 if (opt.evalf_f==0) {
963 return function(serial,eseq).hold();
965 current_serial = serial;
966 if (opt.evalf_use_exvector_args)
967 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
968 switch (opt.nparams) {
969 // the following lines have been generated for max. ${maxargs} parameters
970 ${evalf_switch_statement}
971 // end of generated lines
973 throw(std::logic_error("function::evalf(): invalid nparams"));
976 unsigned function::calchash() const
978 unsigned v = golden_ratio_hash(golden_ratio_hash((unsigned)tinfo()) ^ serial);
979 for (size_t i=0; i<nops(); i++) {
981 v ^= this->op(i).gethash();
984 if (flags & status_flags::evaluated) {
985 setflag(status_flags::hash_calculated);
991 ex function::thiscontainer(const exvector & v) const
993 return function(serial, v);
996 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
998 return function(serial, vp);
1001 /** Implementation of ex::series for functions.
1002 * \@see ex::series */
1003 ex function::series(const relational & r, int order, unsigned options) const
1005 GINAC_ASSERT(serial<registered_functions().size());
1006 const function_options &opt = registered_functions()[serial];
1008 if (opt.series_f==0) {
1009 return basic::series(r, order);
1012 current_serial = serial;
1013 if (opt.series_use_exvector_args) {
1015 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
1016 } catch (do_taylor) {
1017 res = basic::series(r, order, options);
1021 switch (opt.nparams) {
1022 // the following lines have been generated for max. ${maxargs} parameters
1023 ${series_switch_statement}
1024 // end of generated lines
1026 throw(std::logic_error("function::series(): invalid nparams"));
1029 /** Implementation of ex::conjugate for functions. */
1030 ex function::conjugate() const
1032 GINAC_ASSERT(serial<registered_functions().size());
1033 const function_options & opt = registered_functions()[serial];
1035 if (opt.conjugate_f==0) {
1036 return exprseq::conjugate();
1039 if (opt.conjugate_use_exvector_args) {
1040 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
1043 switch (opt.nparams) {
1044 // the following lines have been generated for max. ${maxargs} parameters
1045 ${conjugate_switch_statement}
1046 // end of generated lines
1048 throw(std::logic_error("function::conjugate(): invalid nparams"));
1053 /** Implementation of ex::diff() for functions. It applies the chain rule,
1054 * except for the Order term function.
1056 ex function::derivative(const symbol & s) const
1060 if (serial == Order_SERIAL::serial) {
1061 // Order Term function only differentiates the argument
1062 return Order(seq[0].diff(s));
1066 size_t num = seq.size();
1067 for (size_t i=0; i<num; i++) {
1068 arg_diff = seq[i].diff(s);
1069 // We apply the chain rule only when it makes sense. This is not
1070 // just for performance reasons but also to allow functions to
1071 // throw when differentiated with respect to one of its arguments
1072 // without running into trouble with our automatic full
1074 if (!arg_diff.is_zero())
1075 result += pderivative(i)*arg_diff;
1081 int function::compare_same_type(const basic & other) const
1083 GINAC_ASSERT(is_a<function>(other));
1084 const function & o = static_cast<const function &>(other);
1086 if (serial != o.serial)
1087 return serial < o.serial ? -1 : 1;
1089 return exprseq::compare_same_type(o);
1092 bool function::is_equal_same_type(const basic & other) const
1094 GINAC_ASSERT(is_a<function>(other));
1095 const function & o = static_cast<const function &>(other);
1097 if (serial != o.serial)
1100 return exprseq::is_equal_same_type(o);
1103 bool function::match_same_type(const basic & other) const
1105 GINAC_ASSERT(is_a<function>(other));
1106 const function & o = static_cast<const function &>(other);
1108 return serial == o.serial;
1111 unsigned function::return_type() const
1113 GINAC_ASSERT(serial<registered_functions().size());
1114 const function_options &opt = registered_functions()[serial];
1116 if (opt.use_return_type) {
1117 // Return type was explicitly specified
1118 return opt.return_type;
1120 // Default behavior is to use the return type of the first
1121 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1123 return return_types::commutative;
1125 return seq.begin()->return_type();
1129 const basic* function::return_type_tinfo() const
1131 GINAC_ASSERT(serial<registered_functions().size());
1132 const function_options &opt = registered_functions()[serial];
1134 if (opt.use_return_type) {
1135 // Return type was explicitly specified
1138 // Default behavior is to use the return type of the first
1139 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1143 return seq.begin()->return_type_tinfo();
1148 // new virtual functions which can be overridden by derived classes
1154 // non-virtual functions in this class
1159 ex function::pderivative(unsigned diff_param) const // partial differentiation
1161 GINAC_ASSERT(serial<registered_functions().size());
1162 const function_options &opt = registered_functions()[serial];
1164 // No derivative defined? Then return abstract derivative object
1165 if (opt.derivative_f == NULL)
1166 return fderivative(serial, diff_param, seq);
1168 current_serial = serial;
1169 if (opt.derivative_use_exvector_args)
1170 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1171 switch (opt.nparams) {
1172 // the following lines have been generated for max. ${maxargs} parameters
1173 ${diff_switch_statement}
1174 // end of generated lines
1176 throw(std::logic_error("function::pderivative(): no diff function defined"));
1179 ex function::power(const ex & power_param) const // power of function
1181 GINAC_ASSERT(serial<registered_functions().size());
1182 const function_options &opt = registered_functions()[serial];
1184 // No derivative defined? Then return abstract derivative object
1185 if (opt.power_f == NULL)
1186 return (new power::power(*this, power_param))->setflag(status_flags::dynallocated |
1187 status_flags::evaluated);
1189 current_serial = serial;
1190 if (opt.power_use_exvector_args)
1191 return ((power_funcp_exvector)(opt.power_f))(seq, power_param);
1192 switch (opt.nparams) {
1193 // the following lines have been generated for max. ${maxargs} parameters
1194 ${power_switch_statement}
1195 // end of generated lines
1197 throw(std::logic_error("function::power(): no power function defined"));
1200 std::vector<function_options> & function::registered_functions()
1202 static std::vector<function_options> * rf = new std::vector<function_options>;
1206 bool function::lookup_remember_table(ex & result) const
1208 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1211 void function::store_remember_table(ex const & result) const
1213 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1218 unsigned function::register_new(function_options const & opt)
1220 size_t same_name = 0;
1221 for (size_t i=0; i<registered_functions().size(); ++i) {
1222 if (registered_functions()[i].name==opt.name) {
1226 if (same_name>=opt.functions_with_same_name) {
1227 // we do not throw an exception here because this code is
1228 // usually executed before main(), so the exception could not
1230 std::cerr << "WARNING: function name " << opt.name
1231 << " already in use!" << std::endl;
1233 registered_functions().push_back(opt);
1234 if (opt.use_remember) {
1235 remember_table::remember_tables().
1236 push_back(remember_table(opt.remember_size,
1237 opt.remember_assoc_size,
1238 opt.remember_strategy));
1240 remember_table::remember_tables().push_back(remember_table());
1242 return registered_functions().size()-1;
1245 /** Find serial number of function by name and number of parameters.
1246 * Throws exception if function was not found. */
1247 unsigned function::find_function(const std::string &name, unsigned nparams)
1249 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1250 unsigned serial = 0;
1252 if (i->get_name() == name && i->get_nparams() == nparams)
1257 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1260 /** Return the print name of the function. */
1261 std::string function::get_name() const
1263 GINAC_ASSERT(serial<registered_functions().size());
1264 return registered_functions()[serial].name;
1267 } // namespace GiNaC
1269 END_OF_IMPLEMENTATION
1271 print "Creating interface file function.h...";
1272 open OUT,">function.h" or die "cannot open function.h";
1273 print OUT $interface;
1277 print "Creating implementation file function.cpp...";
1278 open OUT,">function.cpp" or die "cannot open function.cpp";
1279 print OUT $implementation;