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 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
67 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
69 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
71 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','','');
73 $constructors_interface=generate(
74 ' function(unsigned ser, ${SEQ1});'."\n",
75 'const ex & param${N}','','');
77 $constructors_implementation=generate(
78 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
79 function::function(unsigned ser, ${SEQ1})
80 : exprseq(${SEQ2}), serial(ser)
82 tinfo_key = TINFO_function;
84 END_OF_CONSTRUCTORS_IMPLEMENTATION
86 $eval_switch_statement=generate(
87 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
89 eval_result = ((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
91 END_OF_EVAL_SWITCH_STATEMENT
93 $evalf_switch_statement=generate(
94 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
96 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
97 END_OF_EVALF_SWITCH_STATEMENT
99 $diff_switch_statement=generate(
100 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
102 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
103 END_OF_DIFF_SWITCH_STATEMENT
105 $series_switch_statement=generate(
106 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
109 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
110 } catch (do_taylor) {
111 res = basic::series(r, order, options);
114 END_OF_SERIES_SWITCH_STATEMENT
116 $eval_func_implementation=generate(
117 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
118 function_options & function_options::eval_func(eval_funcp_${N} e)
120 test_and_set_nparams(${N});
121 eval_f = eval_funcp(e);
124 END_OF_EVAL_FUNC_IMPLEMENTATION
126 $evalf_func_implementation=generate(
127 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
128 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
130 test_and_set_nparams(${N});
131 evalf_f = evalf_funcp(ef);
134 END_OF_EVALF_FUNC_IMPLEMENTATION
136 $derivative_func_implementation=generate(
137 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
138 function_options & function_options::derivative_func(derivative_funcp_${N} d)
140 test_and_set_nparams(${N});
141 derivative_f = derivative_funcp(d);
144 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
146 $series_func_implementation=generate(
147 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
148 function_options & function_options::series_func(series_funcp_${N} s)
150 test_and_set_nparams(${N});
151 series_f = series_funcp(s);
154 END_OF_SERIES_FUNC_IMPLEMENTATION
156 $interface=<<END_OF_INTERFACE;
157 /** \@file function.h
159 * Interface to class of symbolic functions. */
162 * This file was generated automatically by function.pl.
163 * Please do not modify it directly, edit the perl script instead!
164 * function.pl options: \$maxargs=${maxargs}
166 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
168 * This program is free software; you can redistribute it and/or modify
169 * it under the terms of the GNU General Public License as published by
170 * the Free Software Foundation; either version 2 of the License, or
171 * (at your option) any later version.
173 * This program is distributed in the hope that it will be useful,
174 * but WITHOUT ANY WARRANTY; without even the implied warranty of
175 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
176 * GNU General Public License for more details.
178 * You should have received a copy of the GNU General Public License
179 * along with this program; if not, write to the Free Software
180 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
183 #ifndef __GINAC_FUNCTION_H__
184 #define __GINAC_FUNCTION_H__
189 // CINT needs <algorithm> to work properly with <vector>
194 // the following lines have been generated for max. ${maxargs} parameters
195 $declare_function_macro
196 // end of generated lines
198 #define REGISTER_FUNCTION(NAME,OPT) \\
199 unsigned NAME##_SERIAL::serial = \\
200 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
207 typedef ex (* eval_funcp)();
208 typedef ex (* evalf_funcp)();
209 typedef ex (* derivative_funcp)();
210 typedef ex (* series_funcp)();
212 // the following lines have been generated for max. ${maxargs} parameters
215 $typedef_derivative_funcp
216 $typedef_series_funcp
217 // end of generated lines
219 // Alternatively, an exvector may be passed into the static function, instead
220 // of individual ex objects. Then, the number of arguments is not limited.
221 typedef ex (* eval_funcp_exvector)(const exvector &);
222 typedef ex (* evalf_funcp_exvector)(const exvector &);
223 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
224 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
227 class function_options
229 friend class function;
230 friend class fderivative;
233 function_options(std::string const & n, std::string const & tn=std::string());
236 function_options & set_name(std::string const & n, std::string const & tn=std::string());
237 function_options & latex_name(std::string const & tn);
238 // the following lines have been generated for max. ${maxargs} parameters
240 $evalf_func_interface
241 $derivative_func_interface
242 $series_func_interface
243 // end of generated lines
244 function_options & eval_func(eval_funcp_exvector e);
245 function_options & evalf_func(evalf_funcp_exvector ef);
246 function_options & derivative_func(derivative_funcp_exvector d);
247 function_options & series_func(series_funcp_exvector s);
249 function_options & set_return_type(unsigned rt, unsigned rtt=0);
250 function_options & do_not_evalf_params();
251 function_options & remember(unsigned size, unsigned assoc_size=0,
252 unsigned strategy=remember_strategies::delete_never);
253 function_options & overloaded(unsigned o);
254 function_options & set_symmetry(const symmetry & s);
255 void test_and_set_nparams(unsigned n);
256 std::string get_name() const { return name; }
257 unsigned get_nparams() const { return nparams; }
258 bool has_derivative() const { return derivative_f != NULL; }
262 std::string TeX_name;
268 derivative_funcp derivative_f;
269 series_funcp series_f;
271 bool evalf_params_first;
273 bool use_return_type;
274 unsigned return_type;
275 unsigned return_type_tinfo;
278 unsigned remember_size;
279 unsigned remember_assoc_size;
280 unsigned remember_strategy;
282 bool eval_use_exvector_args;
283 bool evalf_use_exvector_args;
284 bool derivative_use_exvector_args;
285 bool series_use_exvector_args;
287 unsigned functions_with_same_name;
293 /** Exception class thrown by classes which provide their own series expansion
294 * to signal that ordinary Taylor expansion is safe. */
298 /** The class function is used to implement builtin functions like sin, cos...
299 and user defined functions */
300 class function : public exprseq
302 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
304 // CINT has a linking problem
306 friend void ginsh_get_ginac_functions();
307 #endif // def __MAKECINT__
309 friend class remember_table_entry;
310 // friend class remember_table_list;
311 // friend class remember_table;
315 // other constructors
317 function(unsigned ser);
318 // the following lines have been generated for max. ${maxargs} parameters
319 $constructors_interface
320 // end of generated lines
321 function(unsigned ser, const exprseq & es);
322 function(unsigned ser, const exvector & v, bool discardable = false);
323 function(unsigned ser, exvector * vp); // vp will be deleted
325 // functions overriding virtual functions from base classes
327 void print(const print_context & c, unsigned level = 0) const;
328 unsigned precedence() const {return 70;}
329 ex expand(unsigned options=0) const;
330 ex eval(int level=0) const;
331 ex evalf(int level=0) const;
332 unsigned calchash() const;
333 ex series(const relational & r, int order, unsigned options = 0) const;
334 ex thiscontainer(const exvector & v) const;
335 ex thiscontainer(exvector * vp) const;
337 ex derivative(const symbol & s) const;
338 bool is_equal_same_type(const basic & other) const;
339 bool match_same_type(const basic & other) const;
340 unsigned return_type() const;
341 unsigned return_type_tinfo() const;
343 // new virtual functions which can be overridden by derived classes
346 // non-virtual functions in this class
348 ex pderivative(unsigned diff_param) const; // partial differentiation
349 static std::vector<function_options> & registered_functions();
350 bool lookup_remember_table(ex & result) const;
351 void store_remember_table(ex const & result) const;
353 static unsigned register_new(function_options const & opt);
354 static unsigned current_serial;
355 static unsigned find_function(const std::string &name, unsigned nparams);
356 unsigned get_serial() const {return serial;}
357 std::string get_name() const;
365 // utility functions/macros
367 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
368 template<> inline bool is_exactly_a<function>(const basic & obj)
370 return obj.tinfo()==TINFO_function;
373 template <typename T>
374 inline bool is_the_function(const ex & x)
376 return is_exactly_a<function>(x)
377 && ex_to<function>(x).get_serial() == T::serial;
380 // Check whether OBJ is the specified symbolic function.
381 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
385 #endif // ndef __GINAC_FUNCTION_H__
389 $implementation=<<END_OF_IMPLEMENTATION;
390 /** \@file function.cpp
392 * Implementation of class of symbolic functions. */
395 * This file was generated automatically by function.pl.
396 * Please do not modify it directly, edit the perl script instead!
397 * function.pl options: \$maxargs=${maxargs}
399 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
401 * This program is free software; you can redistribute it and/or modify
402 * it under the terms of the GNU General Public License as published by
403 * the Free Software Foundation; either version 2 of the License, or
404 * (at your option) any later version.
406 * This program is distributed in the hope that it will be useful,
407 * but WITHOUT ANY WARRANTY; without even the implied warranty of
408 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
409 * GNU General Public License for more details.
411 * You should have received a copy of the GNU General Public License
412 * along with this program; if not, write to the Free Software
413 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
421 #include "function.h"
422 #include "operators.h"
423 #include "fderivative.h"
426 #include "symmetry.h"
430 #include "tostring.h"
432 #include "remember.h"
437 // helper class function_options
440 function_options::function_options()
445 function_options::function_options(std::string const & n, std::string const & tn)
451 function_options::~function_options()
453 // nothing to clean up at the moment
456 void function_options::initialize()
458 set_name("unnamed_function","\\\\mbox{unnamed}");
460 eval_f = evalf_f = derivative_f = series_f = 0;
461 evalf_params_first = true;
462 use_return_type = false;
463 eval_use_exvector_args = false;
464 evalf_use_exvector_args = false;
465 derivative_use_exvector_args = false;
466 series_use_exvector_args = false;
467 use_remember = false;
468 functions_with_same_name = 1;
472 function_options & function_options::set_name(std::string const & n,
473 std::string const & tn)
476 if (tn==std::string())
477 TeX_name = "\\\\mbox{"+name+"}";
483 function_options & function_options::latex_name(std::string const & tn)
489 // the following lines have been generated for max. ${maxargs} parameters
490 $eval_func_implementation
491 $evalf_func_implementation
492 $derivative_func_implementation
493 $series_func_implementation
494 // end of generated lines
496 function_options& function_options::eval_func(eval_funcp_exvector e)
498 eval_use_exvector_args = true;
499 eval_f = eval_funcp(e);
502 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
504 evalf_use_exvector_args = true;
505 evalf_f = evalf_funcp(ef);
508 function_options& function_options::derivative_func(derivative_funcp_exvector d)
510 derivative_use_exvector_args = true;
511 derivative_f = derivative_funcp(d);
514 function_options& function_options::series_func(series_funcp_exvector s)
516 series_use_exvector_args = true;
517 series_f = series_funcp(s);
521 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
523 use_return_type = true;
525 return_type_tinfo = rtt;
529 function_options & function_options::do_not_evalf_params()
531 evalf_params_first = false;
535 function_options & function_options::remember(unsigned size,
540 remember_size = size;
541 remember_assoc_size = assoc_size;
542 remember_strategy = strategy;
546 function_options & function_options::overloaded(unsigned o)
548 functions_with_same_name = o;
552 function_options & function_options::set_symmetry(const symmetry & s)
558 void function_options::test_and_set_nparams(unsigned n)
562 } else if (nparams!=n) {
563 // we do not throw an exception here because this code is
564 // usually executed before main(), so the exception could not
566 std::cerr << "WARNING: " << name << "(): number of parameters ("
567 << n << ") differs from number set before ("
568 << nparams << ")" << std::endl;
572 /** This can be used as a hook for external applications. */
573 unsigned function::current_serial = 0;
576 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
579 // default constructor
584 function::function() : serial(0)
586 tinfo_key = TINFO_function;
590 // other constructors
595 function::function(unsigned ser) : serial(ser)
597 tinfo_key = TINFO_function;
600 // the following lines have been generated for max. ${maxargs} parameters
601 $constructors_implementation
602 // end of generated lines
604 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
606 tinfo_key = TINFO_function;
608 // Force re-evaluation even if the exprseq was already evaluated
609 // (the exprseq copy constructor copies the flags)
610 clearflag(status_flags::evaluated);
613 function::function(unsigned ser, const exvector & v, bool discardable)
614 : exprseq(v,discardable), serial(ser)
616 tinfo_key = TINFO_function;
619 function::function(unsigned ser, exvector * vp)
620 : exprseq(vp), serial(ser)
622 tinfo_key = TINFO_function;
629 /** Construct object from archive_node. */
630 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
632 // Find serial number by function name
634 if (n.find_string("name", s)) {
635 unsigned int ser = 0;
636 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
644 throw (std::runtime_error("unknown function '" + s + "' in archive"));
646 throw (std::runtime_error("unnamed function in archive"));
649 /** Unarchive the object. */
650 ex function::unarchive(const archive_node &n, lst &sym_lst)
652 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
655 /** Archive the object. */
656 void function::archive(archive_node &n) const
658 inherited::archive(n);
659 GINAC_ASSERT(serial < registered_functions().size());
660 n.add_string("name", registered_functions()[serial].name);
664 // functions overriding virtual functions from base classes
669 void function::print(const print_context & c, unsigned level) const
671 GINAC_ASSERT(serial<registered_functions().size());
673 if (is_a<print_tree>(c)) {
675 c.s << std::string(level, ' ') << class_name() << " "
676 << registered_functions()[serial].name
677 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
678 << ", nops=" << nops()
680 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
681 for (size_t i=0; i<seq.size(); ++i)
682 seq[i].print(c, level + delta_indent);
683 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
685 } else if (is_a<print_csrc>(c)) {
687 // Print function name in lowercase
688 std::string lname = registered_functions()[serial].name;
689 size_t num = lname.size();
690 for (size_t i=0; i<num; i++)
691 lname[i] = tolower(lname[i]);
694 // Print arguments, separated by commas
695 exvector::const_iterator it = seq.begin(), itend = seq.end();
696 while (it != itend) {
704 } else if (is_a<print_latex>(c)) {
705 c.s << registered_functions()[serial].TeX_name;
706 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
708 c.s << registered_functions()[serial].name;
709 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
713 ex function::expand(unsigned options) const
715 // Only expand arguments when asked to do so
716 if (options & expand_options::expand_function_args)
717 return inherited::expand(options);
719 return (options == 0) ? setflag(status_flags::expanded) : *this;
722 ex function::eval(int level) const
724 GINAC_ASSERT(serial<registered_functions().size());
727 // first evaluate children, then we will end up here again
728 return function(serial,evalchildren(level));
731 const function_options &opt = registered_functions()[serial];
733 // Canonicalize argument order according to the symmetry properties
734 if (seq.size() > 1 && !(opt.symtree.is_zero())) {
736 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
737 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
738 if (sig != INT_MAX) {
739 // Something has changed while sorting arguments, more evaluations later
742 return ex(sig) * thiscontainer(v);
750 bool use_remember = opt.use_remember;
752 if (use_remember && lookup_remember_table(eval_result)) {
755 current_serial = serial;
756 if (registered_functions()[serial].eval_use_exvector_args)
757 eval_result = ((eval_funcp_exvector)(registered_functions()[serial].eval_f))(seq);
759 switch (opt.nparams) {
760 // the following lines have been generated for max. ${maxargs} parameters
761 ${eval_switch_statement}
762 // end of generated lines
764 throw(std::logic_error("function::eval(): invalid nparams"));
767 store_remember_table(eval_result);
772 ex function::evalf(int level) const
774 GINAC_ASSERT(serial<registered_functions().size());
776 const function_options &opt = registered_functions()[serial];
778 // Evaluate children first
780 if (level == 1 || !(opt.evalf_params_first))
782 else if (level == -max_recursion_level)
783 throw(std::runtime_error("max recursion level reached"));
785 eseq.reserve(seq.size());
787 exvector::const_iterator it = seq.begin(), itend = seq.end();
788 while (it != itend) {
789 eseq.push_back(it->evalf(level));
794 if (opt.evalf_f==0) {
795 return function(serial,eseq).hold();
797 current_serial = serial;
798 if (opt.evalf_use_exvector_args)
799 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
800 switch (opt.nparams) {
801 // the following lines have been generated for max. ${maxargs} parameters
802 ${evalf_switch_statement}
803 // end of generated lines
805 throw(std::logic_error("function::evalf(): invalid nparams"));
808 unsigned function::calchash() const
810 unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
811 for (size_t i=0; i<nops(); i++) {
813 v ^= this->op(i).gethash();
816 if (flags & status_flags::evaluated) {
817 setflag(status_flags::hash_calculated);
823 ex function::thiscontainer(const exvector & v) const
825 return function(serial,v);
828 ex function::thiscontainer(exvector * vp) const
830 return function(serial,vp);
833 /** Implementation of ex::series for functions.
834 * \@see ex::series */
835 ex function::series(const relational & r, int order, unsigned options) const
837 GINAC_ASSERT(serial<registered_functions().size());
839 if (registered_functions()[serial].series_f==0) {
840 return basic::series(r, order);
843 current_serial = serial;
844 if (registered_functions()[serial].series_use_exvector_args) {
846 res = ((series_funcp_exvector)(registered_functions()[serial].series_f))(seq, r, order, options);
847 } catch (do_taylor) {
848 res = basic::series(r, order, options);
852 switch (registered_functions()[serial].nparams) {
853 // the following lines have been generated for max. ${maxargs} parameters
854 ${series_switch_statement}
855 // end of generated lines
857 throw(std::logic_error("function::series(): invalid nparams"));
862 /** Implementation of ex::diff() for functions. It applies the chain rule,
863 * except for the Order term function.
865 ex function::derivative(const symbol & s) const
869 if (serial == Order_SERIAL::serial) {
870 // Order Term function only differentiates the argument
871 return Order(seq[0].diff(s));
875 size_t num = seq.size();
876 for (size_t i=0; i<num; i++) {
877 arg_diff = seq[i].diff(s);
878 // We apply the chain rule only when it makes sense. This is not
879 // just for performance reasons but also to allow functions to
880 // throw when differentiated with respect to one of its arguments
881 // without running into trouble with our automatic full
883 if (!arg_diff.is_zero())
884 result += pderivative(i)*arg_diff;
890 int function::compare_same_type(const basic & other) const
892 GINAC_ASSERT(is_a<function>(other));
893 const function & o = static_cast<const function &>(other);
895 if (serial != o.serial)
896 return serial < o.serial ? -1 : 1;
898 return exprseq::compare_same_type(o);
901 bool function::is_equal_same_type(const basic & other) const
903 GINAC_ASSERT(is_a<function>(other));
904 const function & o = static_cast<const function &>(other);
906 if (serial != o.serial)
909 return exprseq::is_equal_same_type(o);
912 bool function::match_same_type(const basic & other) const
914 GINAC_ASSERT(is_a<function>(other));
915 const function & o = static_cast<const function &>(other);
917 return serial == o.serial;
920 unsigned function::return_type() const
922 const function_options &opt = registered_functions()[serial];
924 if (opt.use_return_type) {
925 // Return type was explicitly specified
926 return opt.return_type;
928 // Default behavior is to use the return type of the first
929 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
931 return return_types::commutative;
933 return seq.begin()->return_type();
937 unsigned function::return_type_tinfo() const
939 const function_options &opt = registered_functions()[serial];
941 if (opt.use_return_type) {
942 // Return type was explicitly specified
943 return opt.return_type_tinfo;
945 // Default behavior is to use the return type of the first
946 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
950 return seq.begin()->return_type_tinfo();
955 // new virtual functions which can be overridden by derived classes
961 // non-virtual functions in this class
966 ex function::pderivative(unsigned diff_param) const // partial differentiation
968 GINAC_ASSERT(serial<registered_functions().size());
970 // No derivative defined? Then return abstract derivative object
971 if (registered_functions()[serial].derivative_f == NULL)
972 return fderivative(serial, diff_param, seq);
974 current_serial = serial;
975 if (registered_functions()[serial].derivative_use_exvector_args)
976 return ((derivative_funcp_exvector)(registered_functions()[serial].derivative_f))(seq, diff_param);
977 switch (registered_functions()[serial].nparams) {
978 // the following lines have been generated for max. ${maxargs} parameters
979 ${diff_switch_statement}
980 // end of generated lines
982 throw(std::logic_error("function::pderivative(): no diff function defined"));
985 std::vector<function_options> & function::registered_functions()
987 static std::vector<function_options> * rf = new std::vector<function_options>;
991 bool function::lookup_remember_table(ex & result) const
993 return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
996 void function::store_remember_table(ex const & result) const
998 remember_table::remember_tables()[this->serial].add_entry(*this,result);
1003 unsigned function::register_new(function_options const & opt)
1005 size_t same_name = 0;
1006 for (size_t i=0; i<registered_functions().size(); ++i) {
1007 if (registered_functions()[i].name==opt.name) {
1011 if (same_name>=opt.functions_with_same_name) {
1012 // we do not throw an exception here because this code is
1013 // usually executed before main(), so the exception could not
1015 std::cerr << "WARNING: function name " << opt.name
1016 << " already in use!" << std::endl;
1018 registered_functions().push_back(opt);
1019 if (opt.use_remember) {
1020 remember_table::remember_tables().
1021 push_back(remember_table(opt.remember_size,
1022 opt.remember_assoc_size,
1023 opt.remember_strategy));
1025 remember_table::remember_tables().push_back(remember_table());
1027 return registered_functions().size()-1;
1030 /** Find serial number of function by name and number of parameters.
1031 * Throws exception if function was not found. */
1032 unsigned function::find_function(const std::string &name, unsigned nparams)
1034 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1035 unsigned serial = 0;
1037 if (i->get_name() == name && i->get_nparams() == nparams)
1042 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1045 /** Return the print name of the function. */
1046 std::string function::get_name() const
1048 GINAC_ASSERT(serial<registered_functions().size());
1049 return registered_functions()[serial].name;
1052 } // namespace GiNaC
1054 END_OF_IMPLEMENTATION
1056 print "Creating interface file function.h...";
1057 open OUT,">function.h" or die "cannot open function.h";
1058 print OUT $interface;
1062 print "Creating implementation file function.cpp...";
1063 open OUT,">function.cpp" or die "cannot open function.cpp";
1064 print OUT $implementation;