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::get_class_info_static().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());
261 function_options & dummy() { return *this; }
262 function_options & set_name(std::string const & n, std::string const & tn=std::string());
263 function_options & latex_name(std::string const & tn);
264 // the following lines have been generated for max. ${maxargs} parameters
266 $evalf_func_interface
267 $derivative_func_interface
268 $series_func_interface
269 $print_func_interface
270 // end of generated lines
271 function_options & eval_func(eval_funcp_exvector e);
272 function_options & evalf_func(evalf_funcp_exvector ef);
273 function_options & derivative_func(derivative_funcp_exvector d);
274 function_options & series_func(series_funcp_exvector s);
276 template <class Ctx> function_options & print_func(print_funcp_exvector p)
278 print_use_exvector_args = true;
279 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
283 function_options & set_return_type(unsigned rt, unsigned rtt=0);
284 function_options & do_not_evalf_params();
285 function_options & remember(unsigned size, unsigned assoc_size=0,
286 unsigned strategy=remember_strategies::delete_never);
287 function_options & overloaded(unsigned o);
288 function_options & set_symmetry(const symmetry & s);
290 std::string get_name() const { return name; }
291 unsigned get_nparams() const { return nparams; }
294 bool has_derivative() const { return derivative_f != NULL; }
295 void test_and_set_nparams(unsigned n);
296 void set_print_func(unsigned id, print_funcp f);
299 std::string TeX_name;
305 derivative_funcp derivative_f;
306 series_funcp series_f;
307 std::vector<print_funcp> print_dispatch_table;
309 bool evalf_params_first;
311 bool use_return_type;
312 unsigned return_type;
313 unsigned return_type_tinfo;
316 unsigned remember_size;
317 unsigned remember_assoc_size;
318 unsigned remember_strategy;
320 bool eval_use_exvector_args;
321 bool evalf_use_exvector_args;
322 bool derivative_use_exvector_args;
323 bool series_use_exvector_args;
324 bool print_use_exvector_args;
326 unsigned functions_with_same_name;
332 /** Exception class thrown by classes which provide their own series expansion
333 * to signal that ordinary Taylor expansion is safe. */
337 /** The class function is used to implement builtin functions like sin, cos...
338 and user defined functions */
339 class function : public exprseq
341 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
343 // CINT has a linking problem
345 friend void ginsh_get_ginac_functions();
346 #endif // def __MAKECINT__
348 friend class remember_table_entry;
349 // friend class remember_table_list;
350 // friend class remember_table;
354 // other constructors
356 function(unsigned ser);
357 // the following lines have been generated for max. ${maxargs} parameters
358 $constructors_interface
359 // end of generated lines
360 function(unsigned ser, const exprseq & es);
361 function(unsigned ser, const exvector & v, bool discardable = false);
362 function(unsigned ser, std::auto_ptr<exvector> vp);
364 // functions overriding virtual functions from base classes
366 void print(const print_context & c, unsigned level = 0) const;
367 unsigned precedence() const {return 70;}
368 ex expand(unsigned options=0) const;
369 ex eval(int level=0) const;
370 ex evalf(int level=0) const;
371 unsigned calchash() const;
372 ex series(const relational & r, int order, unsigned options = 0) const;
373 ex thiscontainer(const exvector & v) const;
374 ex thiscontainer(std::auto_ptr<exvector> vp) const;
376 ex derivative(const symbol & s) const;
377 bool is_equal_same_type(const basic & other) const;
378 bool match_same_type(const basic & other) const;
379 unsigned return_type() const;
380 unsigned return_type_tinfo() const;
382 // new virtual functions which can be overridden by derived classes
385 // non-virtual functions in this class
387 ex pderivative(unsigned diff_param) const; // partial differentiation
388 static std::vector<function_options> & registered_functions();
389 bool lookup_remember_table(ex & result) const;
390 void store_remember_table(ex const & result) const;
392 static unsigned register_new(function_options const & opt);
393 static unsigned current_serial;
394 static unsigned find_function(const std::string &name, unsigned nparams);
395 unsigned get_serial() const {return serial;}
396 std::string get_name() const;
404 // utility functions/macros
406 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
407 template<> inline bool is_exactly_a<function>(const basic & obj)
409 return obj.tinfo()==TINFO_function;
412 template <typename T>
413 inline bool is_the_function(const ex & x)
415 return is_exactly_a<function>(x)
416 && ex_to<function>(x).get_serial() == T::serial;
419 // Check whether OBJ is the specified symbolic function.
420 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
424 #endif // ndef __GINAC_FUNCTION_H__
428 $implementation=<<END_OF_IMPLEMENTATION;
429 /** \@file function.cpp
431 * Implementation of class of symbolic functions. */
434 * This file was generated automatically by function.pl.
435 * Please do not modify it directly, edit the perl script instead!
436 * function.pl options: \$maxargs=${maxargs}
438 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
440 * This program is free software; you can redistribute it and/or modify
441 * it under the terms of the GNU General Public License as published by
442 * the Free Software Foundation; either version 2 of the License, or
443 * (at your option) any later version.
445 * This program is distributed in the hope that it will be useful,
446 * but WITHOUT ANY WARRANTY; without even the implied warranty of
447 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
448 * GNU General Public License for more details.
450 * You should have received a copy of the GNU General Public License
451 * along with this program; if not, write to the Free Software
452 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
460 #include "function.h"
461 #include "operators.h"
462 #include "fderivative.h"
465 #include "symmetry.h"
469 #include "tostring.h"
471 #include "remember.h"
476 // helper class function_options
479 function_options::function_options()
484 function_options::function_options(std::string const & n, std::string const & tn)
490 function_options::~function_options()
492 // nothing to clean up at the moment
495 void function_options::initialize()
497 set_name("unnamed_function", "\\\\mbox{unnamed}");
499 eval_f = evalf_f = derivative_f = series_f = 0;
500 evalf_params_first = true;
501 use_return_type = false;
502 eval_use_exvector_args = false;
503 evalf_use_exvector_args = false;
504 derivative_use_exvector_args = false;
505 series_use_exvector_args = false;
506 print_use_exvector_args = false;
507 use_remember = false;
508 functions_with_same_name = 1;
512 function_options & function_options::set_name(std::string const & n,
513 std::string const & tn)
516 if (tn==std::string())
517 TeX_name = "\\\\mbox{"+name+"}";
523 function_options & function_options::latex_name(std::string const & tn)
529 // the following lines have been generated for max. ${maxargs} parameters
530 $eval_func_implementation
531 $evalf_func_implementation
532 $derivative_func_implementation
533 $series_func_implementation
534 // end of generated lines
536 function_options& function_options::eval_func(eval_funcp_exvector e)
538 eval_use_exvector_args = true;
539 eval_f = eval_funcp(e);
542 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
544 evalf_use_exvector_args = true;
545 evalf_f = evalf_funcp(ef);
548 function_options& function_options::derivative_func(derivative_funcp_exvector d)
550 derivative_use_exvector_args = true;
551 derivative_f = derivative_funcp(d);
554 function_options& function_options::series_func(series_funcp_exvector s)
556 series_use_exvector_args = true;
557 series_f = series_funcp(s);
561 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
563 use_return_type = true;
565 return_type_tinfo = rtt;
569 function_options & function_options::do_not_evalf_params()
571 evalf_params_first = false;
575 function_options & function_options::remember(unsigned size,
580 remember_size = size;
581 remember_assoc_size = assoc_size;
582 remember_strategy = strategy;
586 function_options & function_options::overloaded(unsigned o)
588 functions_with_same_name = o;
592 function_options & function_options::set_symmetry(const symmetry & s)
598 void function_options::test_and_set_nparams(unsigned n)
602 } else if (nparams!=n) {
603 // we do not throw an exception here because this code is
604 // usually executed before main(), so the exception could not
606 std::cerr << "WARNING: " << name << "(): number of parameters ("
607 << n << ") differs from number set before ("
608 << nparams << ")" << std::endl;
612 void function_options::set_print_func(unsigned id, print_funcp f)
614 if (id >= print_dispatch_table.size())
615 print_dispatch_table.resize(id + 1);
616 print_dispatch_table[id] = f;
619 /** This can be used as a hook for external applications. */
620 unsigned function::current_serial = 0;
623 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
626 // default constructor
631 function::function() : serial(0)
633 tinfo_key = TINFO_function;
637 // other constructors
642 function::function(unsigned ser) : serial(ser)
644 tinfo_key = TINFO_function;
647 // the following lines have been generated for max. ${maxargs} parameters
648 $constructors_implementation
649 // end of generated lines
651 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
653 tinfo_key = TINFO_function;
655 // Force re-evaluation even if the exprseq was already evaluated
656 // (the exprseq copy constructor copies the flags)
657 clearflag(status_flags::evaluated);
660 function::function(unsigned ser, const exvector & v, bool discardable)
661 : exprseq(v,discardable), serial(ser)
663 tinfo_key = TINFO_function;
666 function::function(unsigned ser, std::auto_ptr<exvector> vp)
667 : exprseq(vp), serial(ser)
669 tinfo_key = TINFO_function;
676 /** Construct object from archive_node. */
677 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
679 // Find serial number by function name
681 if (n.find_string("name", s)) {
682 unsigned int ser = 0;
683 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
691 throw (std::runtime_error("unknown function '" + s + "' in archive"));
693 throw (std::runtime_error("unnamed function in archive"));
696 /** Unarchive the object. */
697 ex function::unarchive(const archive_node &n, lst &sym_lst)
699 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
702 /** Archive the object. */
703 void function::archive(archive_node &n) const
705 inherited::archive(n);
706 GINAC_ASSERT(serial < registered_functions().size());
707 n.add_string("name", registered_functions()[serial].name);
711 // functions overriding virtual functions from base classes
716 void function::print(const print_context & c, unsigned level) const
718 GINAC_ASSERT(serial<registered_functions().size());
719 const function_options &opt = registered_functions()[serial];
720 const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
722 // Dynamically dispatch on print_context type
723 const print_context_class_info *pc_info = &c.get_class_info();
726 unsigned id = pc_info->options.get_id();
727 if (id >= pdt.size() || pdt[id] == NULL) {
729 // Method not found, try parent print_context class
730 const print_context_class_info *parent_pc_info = pc_info->get_parent();
731 if (parent_pc_info) {
732 pc_info = parent_pc_info;
736 // Method still not found, use default output
737 if (is_a<print_tree>(c)) {
739 c.s << std::string(level, ' ') << class_name() << " "
740 << opt.name << " @" << this
741 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
742 << ", nops=" << nops()
744 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
745 for (size_t i=0; i<seq.size(); ++i)
746 seq[i].print(c, level + delta_indent);
747 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
749 } else if (is_a<print_csrc>(c)) {
751 // Print function name in lowercase
752 std::string lname = opt.name;
753 size_t num = lname.size();
754 for (size_t i=0; i<num; i++)
755 lname[i] = tolower(lname[i]);
757 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
759 } else if (is_a<print_latex>(c)) {
761 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
764 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
769 // Method found, call it
770 current_serial = serial;
771 if (opt.print_use_exvector_args)
772 ((print_funcp_exvector)pdt[id])(seq, c);
773 else switch (opt.nparams) {
774 // the following lines have been generated for max. ${maxargs} parameters
775 ${print_switch_statement}
776 // end of generated lines
778 throw(std::logic_error("function::print(): invalid nparams"));
783 ex function::expand(unsigned options) const
785 // Only expand arguments when asked to do so
786 if (options & expand_options::expand_function_args)
787 return inherited::expand(options);
789 return (options == 0) ? setflag(status_flags::expanded) : *this;
792 ex function::eval(int level) const
795 // first evaluate children, then we will end up here again
796 return function(serial,evalchildren(level));
799 GINAC_ASSERT(serial<registered_functions().size());
800 const function_options &opt = registered_functions()[serial];
802 // Canonicalize argument order according to the symmetry properties
803 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
805 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
806 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
807 if (sig != INT_MAX) {
808 // Something has changed while sorting arguments, more evaluations later
811 return ex(sig) * thiscontainer(v);
819 bool use_remember = opt.use_remember;
821 if (use_remember && lookup_remember_table(eval_result)) {
824 current_serial = serial;
825 if (opt.eval_use_exvector_args)
826 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
828 switch (opt.nparams) {
829 // the following lines have been generated for max. ${maxargs} parameters
830 ${eval_switch_statement}
831 // end of generated lines
833 throw(std::logic_error("function::eval(): invalid nparams"));
836 store_remember_table(eval_result);
841 ex function::evalf(int level) const
843 GINAC_ASSERT(serial<registered_functions().size());
844 const function_options &opt = registered_functions()[serial];
846 // Evaluate children first
848 if (level == 1 || !(opt.evalf_params_first))
850 else if (level == -max_recursion_level)
851 throw(std::runtime_error("max recursion level reached"));
853 eseq.reserve(seq.size());
855 exvector::const_iterator it = seq.begin(), itend = seq.end();
856 while (it != itend) {
857 eseq.push_back(it->evalf(level));
862 if (opt.evalf_f==0) {
863 return function(serial,eseq).hold();
865 current_serial = serial;
866 if (opt.evalf_use_exvector_args)
867 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
868 switch (opt.nparams) {
869 // the following lines have been generated for max. ${maxargs} parameters
870 ${evalf_switch_statement}
871 // end of generated lines
873 throw(std::logic_error("function::evalf(): invalid nparams"));
876 unsigned function::calchash() const
878 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
879 for (size_t i=0; i<nops(); i++) {
881 v ^= this->op(i).gethash();
884 if (flags & status_flags::evaluated) {
885 setflag(status_flags::hash_calculated);
891 ex function::thiscontainer(const exvector & v) const
893 return function(serial, v);
896 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
898 return function(serial, vp);
901 /** Implementation of ex::series for functions.
902 * \@see ex::series */
903 ex function::series(const relational & r, int order, unsigned options) const
905 GINAC_ASSERT(serial<registered_functions().size());
906 const function_options &opt = registered_functions()[serial];
908 if (opt.series_f==0) {
909 return basic::series(r, order);
912 current_serial = serial;
913 if (opt.series_use_exvector_args) {
915 res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
916 } catch (do_taylor) {
917 res = basic::series(r, order, options);
921 switch (opt.nparams) {
922 // the following lines have been generated for max. ${maxargs} parameters
923 ${series_switch_statement}
924 // end of generated lines
926 throw(std::logic_error("function::series(): invalid nparams"));
931 /** Implementation of ex::diff() for functions. It applies the chain rule,
932 * except for the Order term function.
934 ex function::derivative(const symbol & s) const
938 if (serial == Order_SERIAL::serial) {
939 // Order Term function only differentiates the argument
940 return Order(seq[0].diff(s));
944 size_t num = seq.size();
945 for (size_t i=0; i<num; i++) {
946 arg_diff = seq[i].diff(s);
947 // We apply the chain rule only when it makes sense. This is not
948 // just for performance reasons but also to allow functions to
949 // throw when differentiated with respect to one of its arguments
950 // without running into trouble with our automatic full
952 if (!arg_diff.is_zero())
953 result += pderivative(i)*arg_diff;
959 int function::compare_same_type(const basic & other) const
961 GINAC_ASSERT(is_a<function>(other));
962 const function & o = static_cast<const function &>(other);
964 if (serial != o.serial)
965 return serial < o.serial ? -1 : 1;
967 return exprseq::compare_same_type(o);
970 bool function::is_equal_same_type(const basic & other) const
972 GINAC_ASSERT(is_a<function>(other));
973 const function & o = static_cast<const function &>(other);
975 if (serial != o.serial)
978 return exprseq::is_equal_same_type(o);
981 bool function::match_same_type(const basic & other) const
983 GINAC_ASSERT(is_a<function>(other));
984 const function & o = static_cast<const function &>(other);
986 return serial == o.serial;
989 unsigned function::return_type() const
991 GINAC_ASSERT(serial<registered_functions().size());
992 const function_options &opt = registered_functions()[serial];
994 if (opt.use_return_type) {
995 // Return type was explicitly specified
996 return opt.return_type;
998 // Default behavior is to use the return type of the first
999 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1001 return return_types::commutative;
1003 return seq.begin()->return_type();
1007 unsigned function::return_type_tinfo() const
1009 GINAC_ASSERT(serial<registered_functions().size());
1010 const function_options &opt = registered_functions()[serial];
1012 if (opt.use_return_type) {
1013 // Return type was explicitly specified
1014 return opt.return_type_tinfo;
1016 // Default behavior is to use the return type of the first
1017 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1021 return seq.begin()->return_type_tinfo();
1026 // new virtual functions which can be overridden by derived classes
1032 // non-virtual functions in this class
1037 ex function::pderivative(unsigned diff_param) const // partial differentiation
1039 GINAC_ASSERT(serial<registered_functions().size());
1040 const function_options &opt = registered_functions()[serial];
1042 // No derivative defined? Then return abstract derivative object
1043 if (opt.derivative_f == NULL)
1044 return fderivative(serial, diff_param, seq);
1046 current_serial = serial;
1047 if (opt.derivative_use_exvector_args)
1048 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1049 switch (opt.nparams) {
1050 // the following lines have been generated for max. ${maxargs} parameters
1051 ${diff_switch_statement}
1052 // end of generated lines
1054 throw(std::logic_error("function::pderivative(): no diff function defined"));
1057 std::vector<function_options> & function::registered_functions()
1059 static std::vector<function_options> * rf = new std::vector<function_options>;
1063 bool function::lookup_remember_table(ex & result) const
1065 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1068 void function::store_remember_table(ex const & result) const
1070 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1075 unsigned function::register_new(function_options const & opt)
1077 size_t same_name = 0;
1078 for (size_t i=0; i<registered_functions().size(); ++i) {
1079 if (registered_functions()[i].name==opt.name) {
1083 if (same_name>=opt.functions_with_same_name) {
1084 // we do not throw an exception here because this code is
1085 // usually executed before main(), so the exception could not
1087 std::cerr << "WARNING: function name " << opt.name
1088 << " already in use!" << std::endl;
1090 registered_functions().push_back(opt);
1091 if (opt.use_remember) {
1092 remember_table::remember_tables().
1093 push_back(remember_table(opt.remember_size,
1094 opt.remember_assoc_size,
1095 opt.remember_strategy));
1097 remember_table::remember_tables().push_back(remember_table());
1099 return registered_functions().size()-1;
1102 /** Find serial number of function by name and number of parameters.
1103 * Throws exception if function was not found. */
1104 unsigned function::find_function(const std::string &name, unsigned nparams)
1106 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1107 unsigned serial = 0;
1109 if (i->get_name() == name && i->get_nparams() == nparams)
1114 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1117 /** Return the print name of the function. */
1118 std::string function::get_name() const
1120 GINAC_ASSERT(serial<registered_functions().size());
1121 return registered_functions()[serial].name;
1124 } // namespace GiNaC
1126 END_OF_IMPLEMENTATION
1128 print "Creating interface file function.h...";
1129 open OUT,">function.h" or die "cannot open function.h";
1130 print OUT $interface;
1134 print "Creating implementation file function.cpp...";
1135 open OUT,">function.cpp" or die "cannot open function.cpp";
1136 print OUT $implementation;