4 my ($seq_template,$n)=@_;
8 for ($N=1; $N<=$n; $N++) {
9 $res .= eval('"' . $seq_template . '"');
17 sub generate_from_to {
18 my ($template,$seq_template1,$seq_template2,$seq_template3,$from,$to)=@_;
22 for ($N=$from; $N<=$to; $N++) {
23 $SEQ1=generate_seq($seq_template1,$N);
24 $SEQ2=generate_seq($seq_template2,$N);
25 $SEQ3=generate_seq($seq_template3,$N);
26 $res .= eval('"' . $template . '"');
27 $SEQ1=''; # to avoid main::SEQ1 used only once warning
28 $SEQ2=''; # same as above
29 $SEQ3=''; # same as above
35 my ($template,$seq_template1,$seq_template2,$seq_template3)=@_;
36 return generate_from_to($template,$seq_template1,$seq_template2,$seq_template3,1,$maxargs);
39 $declare_function_macro = generate(
40 <<'END_OF_DECLARE_FUNCTION_MACRO','typename T${N}','const T${N} & p${N}','GiNaC::ex(p${N})');
41 #define DECLARE_FUNCTION_${N}P(NAME) \\
42 class NAME##_SERIAL { public: static unsigned serial; }; \\
43 template<${SEQ1}> const GiNaC::function NAME(${SEQ2}) { \\
44 return GiNaC::function(NAME##_SERIAL::serial, ${SEQ3}); \\
47 END_OF_DECLARE_FUNCTION_MACRO
49 $typedef_eval_funcp=generate(
50 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
53 $typedef_evalf_funcp=generate(
54 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
57 $typedef_derivative_funcp=generate(
58 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
61 $typedef_series_funcp=generate(
62 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
65 $typedef_print_funcp=generate(
66 'typedef void (* print_funcp_${N})(${SEQ1}, const print_context &);'."\n",
69 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
71 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
73 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
75 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','','');
77 $print_func_interface=generate(
78 <<'END_OF_PRINT_FUNC_INTERFACE','','','');
79 template <class Ctx> function_options & print_func(print_funcp_${N} p)
81 test_and_set_nparams(${N});
82 set_print_func(Ctx::reg_info.options.get_id(), print_funcp(p));
85 END_OF_PRINT_FUNC_INTERFACE
87 $constructors_interface=generate(
88 ' function(unsigned ser, ${SEQ1});'."\n",
89 'const ex & param${N}','','');
91 $constructors_implementation=generate(
92 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
93 function::function(unsigned ser, ${SEQ1})
94 : exprseq(${SEQ2}), serial(ser)
96 tinfo_key = TINFO_function;
98 END_OF_CONSTRUCTORS_IMPLEMENTATION
100 $eval_switch_statement=generate(
101 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
103 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
105 END_OF_EVAL_SWITCH_STATEMENT
107 $evalf_switch_statement=generate(
108 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
110 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
111 END_OF_EVALF_SWITCH_STATEMENT
113 $diff_switch_statement=generate(
114 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
116 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
117 END_OF_DIFF_SWITCH_STATEMENT
119 $series_switch_statement=generate(
120 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
123 res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
124 } catch (do_taylor) {
125 res = basic::series(r, order, options);
128 END_OF_SERIES_SWITCH_STATEMENT
130 $print_switch_statement=generate(
131 <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
133 ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
135 END_OF_PRINT_SWITCH_STATEMENT
137 $eval_func_implementation=generate(
138 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
139 function_options & function_options::eval_func(eval_funcp_${N} e)
141 test_and_set_nparams(${N});
142 eval_f = eval_funcp(e);
145 END_OF_EVAL_FUNC_IMPLEMENTATION
147 $evalf_func_implementation=generate(
148 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
149 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
151 test_and_set_nparams(${N});
152 evalf_f = evalf_funcp(ef);
155 END_OF_EVALF_FUNC_IMPLEMENTATION
157 $derivative_func_implementation=generate(
158 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
159 function_options & function_options::derivative_func(derivative_funcp_${N} d)
161 test_and_set_nparams(${N});
162 derivative_f = derivative_funcp(d);
165 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
167 $series_func_implementation=generate(
168 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
169 function_options & function_options::series_func(series_funcp_${N} s)
171 test_and_set_nparams(${N});
172 series_f = series_funcp(s);
175 END_OF_SERIES_FUNC_IMPLEMENTATION
177 $interface=<<END_OF_INTERFACE;
178 /** \@file function.h
180 * Interface to class of symbolic functions. */
183 * This file was generated automatically by function.pl.
184 * Please do not modify it directly, edit the perl script instead!
185 * function.pl options: \$maxargs=${maxargs}
187 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
189 * This program is free software; you can redistribute it and/or modify
190 * it under the terms of the GNU General Public License as published by
191 * the Free Software Foundation; either version 2 of the License, or
192 * (at your option) any later version.
194 * This program is distributed in the hope that it will be useful,
195 * but WITHOUT ANY WARRANTY; without even the implied warranty of
196 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
197 * GNU General Public License for more details.
199 * You should have received a copy of the GNU General Public License
200 * along with this program; if not, write to the Free Software
201 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
204 #ifndef __GINAC_FUNCTION_H__
205 #define __GINAC_FUNCTION_H__
210 // CINT needs <algorithm> to work properly with <vector>
215 // the following lines have been generated for max. ${maxargs} parameters
216 $declare_function_macro
217 // end of generated lines
219 #define REGISTER_FUNCTION(NAME,OPT) \\
220 unsigned NAME##_SERIAL::serial = \\
221 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
228 typedef ex (* eval_funcp)();
229 typedef ex (* evalf_funcp)();
230 typedef ex (* derivative_funcp)();
231 typedef ex (* series_funcp)();
232 typedef void (* print_funcp)();
234 // the following lines have been generated for max. ${maxargs} parameters
237 $typedef_derivative_funcp
238 $typedef_series_funcp
240 // end of generated lines
242 // Alternatively, an exvector may be passed into the static function, instead
243 // of individual ex objects. Then, the number of arguments is not limited.
244 typedef ex (* eval_funcp_exvector)(const exvector &);
245 typedef ex (* evalf_funcp_exvector)(const exvector &);
246 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
247 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
248 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
251 class function_options
253 friend class function;
254 friend class fderivative;
257 function_options(std::string const & n, std::string const & tn=std::string());
260 function_options & set_name(std::string const & n, std::string const & tn=std::string());
261 function_options & latex_name(std::string const & tn);
262 // the following lines have been generated for max. ${maxargs} parameters
264 $evalf_func_interface
265 $derivative_func_interface
266 $series_func_interface
267 $print_func_interface
268 // end of generated lines
269 function_options & eval_func(eval_funcp_exvector e);
270 function_options & evalf_func(evalf_funcp_exvector ef);
271 function_options & derivative_func(derivative_funcp_exvector d);
272 function_options & series_func(series_funcp_exvector s);
274 template <class Ctx> function_options & print_func(print_funcp_exvector p)
276 print_use_exvector_args = true;
277 set_print_func(Ctx::reg_info.options.get_id(), print_funcp(p));
281 function_options & set_return_type(unsigned rt, unsigned rtt=0);
282 function_options & do_not_evalf_params();
283 function_options & remember(unsigned size, unsigned assoc_size=0,
284 unsigned strategy=remember_strategies::delete_never);
285 function_options & overloaded(unsigned o);
286 function_options & set_symmetry(const symmetry & s);
288 std::string get_name() const { return name; }
289 unsigned get_nparams() const { return nparams; }
292 bool has_derivative() const { return derivative_f != NULL; }
293 void test_and_set_nparams(unsigned n);
294 void set_print_func(unsigned id, print_funcp f);
297 std::string TeX_name;
303 derivative_funcp derivative_f;
304 series_funcp series_f;
305 std::vector<print_funcp> print_dispatch_table;
307 bool evalf_params_first;
309 bool use_return_type;
310 unsigned return_type;
311 unsigned return_type_tinfo;
314 unsigned remember_size;
315 unsigned remember_assoc_size;
316 unsigned remember_strategy;
318 bool eval_use_exvector_args;
319 bool evalf_use_exvector_args;
320 bool derivative_use_exvector_args;
321 bool series_use_exvector_args;
322 bool print_use_exvector_args;
324 unsigned functions_with_same_name;
330 /** Exception class thrown by classes which provide their own series expansion
331 * to signal that ordinary Taylor expansion is safe. */
335 /** The class function is used to implement builtin functions like sin, cos...
336 and user defined functions */
337 class function : public exprseq
339 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
341 // CINT has a linking problem
343 friend void ginsh_get_ginac_functions();
344 #endif // def __MAKECINT__
346 friend class remember_table_entry;
347 // friend class remember_table_list;
348 // friend class remember_table;
352 // other constructors
354 function(unsigned ser);
355 // the following lines have been generated for max. ${maxargs} parameters
356 $constructors_interface
357 // end of generated lines
358 function(unsigned ser, const exprseq & es);
359 function(unsigned ser, const exvector & v, bool discardable = false);
360 function(unsigned ser, exvector * vp); // vp will be deleted
362 // functions overriding virtual functions from base classes
364 void print(const print_context & c, unsigned level = 0) const;
365 unsigned precedence() const {return 70;}
366 ex expand(unsigned options=0) const;
367 ex eval(int level=0) const;
368 ex evalf(int level=0) const;
369 unsigned calchash() const;
370 ex series(const relational & r, int order, unsigned options = 0) const;
371 ex thiscontainer(const exvector & v) const;
372 ex thiscontainer(exvector * vp) const;
374 ex derivative(const symbol & s) const;
375 bool is_equal_same_type(const basic & other) const;
376 bool match_same_type(const basic & other) const;
377 unsigned return_type() const;
378 unsigned return_type_tinfo() const;
380 // new virtual functions which can be overridden by derived classes
383 // non-virtual functions in this class
385 ex pderivative(unsigned diff_param) const; // partial differentiation
386 static std::vector<function_options> & registered_functions();
387 bool lookup_remember_table(ex & result) const;
388 void store_remember_table(ex const & result) const;
390 static unsigned register_new(function_options const & opt);
391 static unsigned current_serial;
392 static unsigned find_function(const std::string &name, unsigned nparams);
393 unsigned get_serial() const {return serial;}
394 std::string get_name() const;
402 // utility functions/macros
404 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
405 template<> inline bool is_exactly_a<function>(const basic & obj)
407 return obj.tinfo()==TINFO_function;
410 template <typename T>
411 inline bool is_the_function(const ex & x)
413 return is_exactly_a<function>(x)
414 && ex_to<function>(x).get_serial() == T::serial;
417 // Check whether OBJ is the specified symbolic function.
418 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
422 #endif // ndef __GINAC_FUNCTION_H__
426 $implementation=<<END_OF_IMPLEMENTATION;
427 /** \@file function.cpp
429 * Implementation of class of symbolic functions. */
432 * This file was generated automatically by function.pl.
433 * Please do not modify it directly, edit the perl script instead!
434 * function.pl options: \$maxargs=${maxargs}
436 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
438 * This program is free software; you can redistribute it and/or modify
439 * it under the terms of the GNU General Public License as published by
440 * the Free Software Foundation; either version 2 of the License, or
441 * (at your option) any later version.
443 * This program is distributed in the hope that it will be useful,
444 * but WITHOUT ANY WARRANTY; without even the implied warranty of
445 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
446 * GNU General Public License for more details.
448 * You should have received a copy of the GNU General Public License
449 * along with this program; if not, write to the Free Software
450 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
458 #include "function.h"
459 #include "operators.h"
460 #include "fderivative.h"
463 #include "symmetry.h"
467 #include "tostring.h"
469 #include "remember.h"
474 // helper class function_options
477 function_options::function_options()
482 function_options::function_options(std::string const & n, std::string const & tn)
488 function_options::~function_options()
490 // nothing to clean up at the moment
493 void function_options::initialize()
495 set_name("unnamed_function","\\\\mbox{unnamed}");
497 eval_f = evalf_f = derivative_f = series_f = 0;
498 evalf_params_first = true;
499 use_return_type = false;
500 eval_use_exvector_args = false;
501 evalf_use_exvector_args = false;
502 derivative_use_exvector_args = false;
503 series_use_exvector_args = false;
504 print_use_exvector_args = false;
505 use_remember = false;
506 functions_with_same_name = 1;
510 function_options & function_options::set_name(std::string const & n,
511 std::string const & tn)
514 if (tn==std::string())
515 TeX_name = "\\\\mbox{"+name+"}";
521 function_options & function_options::latex_name(std::string const & tn)
527 // the following lines have been generated for max. ${maxargs} parameters
528 $eval_func_implementation
529 $evalf_func_implementation
530 $derivative_func_implementation
531 $series_func_implementation
532 // end of generated lines
534 function_options& function_options::eval_func(eval_funcp_exvector e)
536 eval_use_exvector_args = true;
537 eval_f = eval_funcp(e);
540 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
542 evalf_use_exvector_args = true;
543 evalf_f = evalf_funcp(ef);
546 function_options& function_options::derivative_func(derivative_funcp_exvector d)
548 derivative_use_exvector_args = true;
549 derivative_f = derivative_funcp(d);
552 function_options& function_options::series_func(series_funcp_exvector s)
554 series_use_exvector_args = true;
555 series_f = series_funcp(s);
559 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
561 use_return_type = true;
563 return_type_tinfo = rtt;
567 function_options & function_options::do_not_evalf_params()
569 evalf_params_first = false;
573 function_options & function_options::remember(unsigned size,
578 remember_size = size;
579 remember_assoc_size = assoc_size;
580 remember_strategy = strategy;
584 function_options & function_options::overloaded(unsigned o)
586 functions_with_same_name = o;
590 function_options & function_options::set_symmetry(const symmetry & s)
596 void function_options::test_and_set_nparams(unsigned n)
600 } else if (nparams!=n) {
601 // we do not throw an exception here because this code is
602 // usually executed before main(), so the exception could not
604 std::cerr << "WARNING: " << name << "(): number of parameters ("
605 << n << ") differs from number set before ("
606 << nparams << ")" << std::endl;
610 void function_options::set_print_func(unsigned id, print_funcp f)
612 if (id >= print_dispatch_table.size())
613 print_dispatch_table.resize(id + 1);
614 print_dispatch_table[id] = f;
617 /** This can be used as a hook for external applications. */
618 unsigned function::current_serial = 0;
621 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
624 // default constructor
629 function::function() : serial(0)
631 tinfo_key = TINFO_function;
635 // other constructors
640 function::function(unsigned ser) : serial(ser)
642 tinfo_key = TINFO_function;
645 // the following lines have been generated for max. ${maxargs} parameters
646 $constructors_implementation
647 // end of generated lines
649 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
651 tinfo_key = TINFO_function;
653 // Force re-evaluation even if the exprseq was already evaluated
654 // (the exprseq copy constructor copies the flags)
655 clearflag(status_flags::evaluated);
658 function::function(unsigned ser, const exvector & v, bool discardable)
659 : exprseq(v,discardable), serial(ser)
661 tinfo_key = TINFO_function;
664 function::function(unsigned ser, exvector * vp)
665 : exprseq(vp), serial(ser)
667 tinfo_key = TINFO_function;
674 /** Construct object from archive_node. */
675 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
677 // Find serial number by function name
679 if (n.find_string("name", s)) {
680 unsigned int ser = 0;
681 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
689 throw (std::runtime_error("unknown function '" + s + "' in archive"));
691 throw (std::runtime_error("unnamed function in archive"));
694 /** Unarchive the object. */
695 ex function::unarchive(const archive_node &n, lst &sym_lst)
697 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
700 /** Archive the object. */
701 void function::archive(archive_node &n) const
703 inherited::archive(n);
704 GINAC_ASSERT(serial < registered_functions().size());
705 n.add_string("name", registered_functions()[serial].name);
709 // functions overriding virtual functions from base classes
714 void function::print(const print_context & c, unsigned level) const
716 GINAC_ASSERT(serial<registered_functions().size());
717 const function_options &opt = registered_functions()[serial];
718 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
720 // Dynamically dispatch on print_context type
721 const print_context_class_info *pc_info = &c.get_class_info();
724 unsigned id = pc_info->options.get_id();
725 if (id >= pdt.size() || pdt[id] == NULL) {
727 // Method not found, try parent print_context class
728 const print_context_class_info *parent_pc_info = pc_info->get_parent();
729 if (parent_pc_info) {
730 pc_info = parent_pc_info;
734 // Method still not found, use default output
735 if (is_a<print_tree>(c)) {
737 c.s << std::string(level, ' ') << class_name() << " "
739 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
740 << ", nops=" << nops()
742 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
743 for (size_t i=0; i<seq.size(); ++i)
744 seq[i].print(c, level + delta_indent);
745 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
747 } else if (is_a<print_csrc>(c)) {
749 // Print function name in lowercase
750 std::string lname = opt.name;
751 size_t num = lname.size();
752 for (size_t i=0; i<num; i++)
753 lname[i] = tolower(lname[i]);
755 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
757 } else if (is_a<print_latex>(c)) {
759 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
762 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
767 // Method found, call it
768 current_serial = serial;
769 if (opt.print_use_exvector_args)
770 ((print_funcp_exvector)pdt[id])(seq, c);
771 else switch (opt.nparams) {
772 // the following lines have been generated for max. ${maxargs} parameters
773 ${print_switch_statement}
774 // end of generated lines
776 throw(std::logic_error("function::print(): invalid nparams"));
781 ex function::expand(unsigned options) const
783 // Only expand arguments when asked to do so
784 if (options & expand_options::expand_function_args)
785 return inherited::expand(options);
787 return (options == 0) ? setflag(status_flags::expanded) : *this;
790 ex function::eval(int level) const
793 // first evaluate children, then we will end up here again
794 return function(serial,evalchildren(level));
797 GINAC_ASSERT(serial<registered_functions().size());
798 const function_options &opt = registered_functions()[serial];
800 // Canonicalize argument order according to the symmetry properties
801 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
803 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
804 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
805 if (sig != INT_MAX) {
806 // Something has changed while sorting arguments, more evaluations later
809 return ex(sig) * thiscontainer(v);
817 bool use_remember = opt.use_remember;
819 if (use_remember && lookup_remember_table(eval_result)) {
822 current_serial = serial;
823 if (opt.eval_use_exvector_args)
824 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
826 switch (opt.nparams) {
827 // the following lines have been generated for max. ${maxargs} parameters
828 ${eval_switch_statement}
829 // end of generated lines
831 throw(std::logic_error("function::eval(): invalid nparams"));
834 store_remember_table(eval_result);
839 ex function::evalf(int level) const
841 GINAC_ASSERT(serial<registered_functions().size());
842 const function_options &opt = registered_functions()[serial];
844 // Evaluate children first
846 if (level == 1 || !(opt.evalf_params_first))
848 else if (level == -max_recursion_level)
849 throw(std::runtime_error("max recursion level reached"));
851 eseq.reserve(seq.size());
853 exvector::const_iterator it = seq.begin(), itend = seq.end();
854 while (it != itend) {
855 eseq.push_back(it->evalf(level));
860 if (opt.evalf_f==0) {
861 return function(serial,eseq).hold();
863 current_serial = serial;
864 if (opt.evalf_use_exvector_args)
865 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
866 switch (opt.nparams) {
867 // the following lines have been generated for max. ${maxargs} parameters
868 ${evalf_switch_statement}
869 // end of generated lines
871 throw(std::logic_error("function::evalf(): invalid nparams"));
874 unsigned function::calchash() const
876 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
877 for (size_t i=0; i<nops(); i++) {
879 v ^= this->op(i).gethash();
882 if (flags & status_flags::evaluated) {
883 setflag(status_flags::hash_calculated);
889 ex function::thiscontainer(const exvector & v) const
891 return function(serial,v);
894 ex function::thiscontainer(exvector * vp) const
896 return function(serial,vp);
899 /** Implementation of ex::series for functions.
900 * \@see ex::series */
901 ex function::series(const relational & r, int order, unsigned options) const
903 GINAC_ASSERT(serial<registered_functions().size());
904 const function_options &opt = registered_functions()[serial];
906 if (opt.series_f==0) {
907 return basic::series(r, order);
910 current_serial = serial;
911 if (opt.series_use_exvector_args) {
913 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
914 } catch (do_taylor) {
915 res = basic::series(r, order, options);
919 switch (opt.nparams) {
920 // the following lines have been generated for max. ${maxargs} parameters
921 ${series_switch_statement}
922 // end of generated lines
924 throw(std::logic_error("function::series(): invalid nparams"));
929 /** Implementation of ex::diff() for functions. It applies the chain rule,
930 * except for the Order term function.
932 ex function::derivative(const symbol & s) const
936 if (serial == Order_SERIAL::serial) {
937 // Order Term function only differentiates the argument
938 return Order(seq[0].diff(s));
942 size_t num = seq.size();
943 for (size_t i=0; i<num; i++) {
944 arg_diff = seq[i].diff(s);
945 // We apply the chain rule only when it makes sense. This is not
946 // just for performance reasons but also to allow functions to
947 // throw when differentiated with respect to one of its arguments
948 // without running into trouble with our automatic full
950 if (!arg_diff.is_zero())
951 result += pderivative(i)*arg_diff;
957 int function::compare_same_type(const basic & other) const
959 GINAC_ASSERT(is_a<function>(other));
960 const function & o = static_cast<const function &>(other);
962 if (serial != o.serial)
963 return serial < o.serial ? -1 : 1;
965 return exprseq::compare_same_type(o);
968 bool function::is_equal_same_type(const basic & other) const
970 GINAC_ASSERT(is_a<function>(other));
971 const function & o = static_cast<const function &>(other);
973 if (serial != o.serial)
976 return exprseq::is_equal_same_type(o);
979 bool function::match_same_type(const basic & other) const
981 GINAC_ASSERT(is_a<function>(other));
982 const function & o = static_cast<const function &>(other);
984 return serial == o.serial;
987 unsigned function::return_type() const
989 GINAC_ASSERT(serial<registered_functions().size());
990 const function_options &opt = registered_functions()[serial];
992 if (opt.use_return_type) {
993 // Return type was explicitly specified
994 return opt.return_type;
996 // Default behavior is to use the return type of the first
997 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
999 return return_types::commutative;
1001 return seq.begin()->return_type();
1005 unsigned function::return_type_tinfo() const
1007 GINAC_ASSERT(serial<registered_functions().size());
1008 const function_options &opt = registered_functions()[serial];
1010 if (opt.use_return_type) {
1011 // Return type was explicitly specified
1012 return opt.return_type_tinfo;
1014 // Default behavior is to use the return type of the first
1015 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1019 return seq.begin()->return_type_tinfo();
1024 // new virtual functions which can be overridden by derived classes
1030 // non-virtual functions in this class
1035 ex function::pderivative(unsigned diff_param) const // partial differentiation
1037 GINAC_ASSERT(serial<registered_functions().size());
1038 const function_options &opt = registered_functions()[serial];
1040 // No derivative defined? Then return abstract derivative object
1041 if (opt.derivative_f == NULL)
1042 return fderivative(serial, diff_param, seq);
1044 current_serial = serial;
1045 if (opt.derivative_use_exvector_args)
1046 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1047 switch (opt.nparams) {
1048 // the following lines have been generated for max. ${maxargs} parameters
1049 ${diff_switch_statement}
1050 // end of generated lines
1052 throw(std::logic_error("function::pderivative(): no diff function defined"));
1055 std::vector<function_options> & function::registered_functions()
1057 static std::vector<function_options> * rf = new std::vector<function_options>;
1061 bool function::lookup_remember_table(ex & result) const
1063 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1066 void function::store_remember_table(ex const & result) const
1068 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1073 unsigned function::register_new(function_options const & opt)
1075 size_t same_name = 0;
1076 for (size_t i=0; i<registered_functions().size(); ++i) {
1077 if (registered_functions()[i].name==opt.name) {
1081 if (same_name>=opt.functions_with_same_name) {
1082 // we do not throw an exception here because this code is
1083 // usually executed before main(), so the exception could not
1085 std::cerr << "WARNING: function name " << opt.name
1086 << " already in use!" << std::endl;
1088 registered_functions().push_back(opt);
1089 if (opt.use_remember) {
1090 remember_table::remember_tables().
1091 push_back(remember_table(opt.remember_size,
1092 opt.remember_assoc_size,
1093 opt.remember_strategy));
1095 remember_table::remember_tables().push_back(remember_table());
1097 return registered_functions().size()-1;
1100 /** Find serial number of function by name and number of parameters.
1101 * Throws exception if function was not found. */
1102 unsigned function::find_function(const std::string &name, unsigned nparams)
1104 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1105 unsigned serial = 0;
1107 if (i->get_name() == name && i->get_nparams() == nparams)
1112 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1115 /** Return the print name of the function. */
1116 std::string function::get_name() const
1118 GINAC_ASSERT(serial<registered_functions().size());
1119 return registered_functions()[serial].name;
1122 } // namespace GiNaC
1124 END_OF_IMPLEMENTATION
1126 print "Creating interface file function.h...";
1127 open OUT,">function.h" or die "cannot open function.h";
1128 print OUT $interface;
1132 print "Creating implementation file function.cpp...";
1133 open OUT,">function.cpp" or die "cannot open function.cpp";
1134 print OUT $implementation;