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,$from,$to)=@_;
22 for ($N=$from; $N<=$to; $N++) {
23 $SEQ1=generate_seq($seq_template1,$N);
24 $SEQ2=generate_seq($seq_template2,$N);
25 $res .= eval('"' . $template . '"');
26 $SEQ1=''; # to avoid main::SEQ1 used only once warning
27 $SEQ2=''; # same as above
33 my ($template,$seq_template1,$seq_template2)=@_;
34 return generate_from_to($template,$seq_template1,$seq_template2,1,$maxargs);
37 $declare_function_macro_namespace = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NAMESPACE';
38 #define DECLARE_FUNCTION_1P(NAME) \
39 extern const unsigned function_index_##NAME; \
40 inline GiNaC::function NAME(const GiNaC::ex & p1) { \
41 return GiNaC::function(function_index_##NAME, p1); \
43 #define DECLARE_FUNCTION_2P(NAME) \
44 extern const unsigned function_index_##NAME; \
45 inline GiNaC::function NAME(const GiNaC::ex & p1, const GiNaC::ex & p2) { \
46 return GiNaC::function(function_index_##NAME, p1, p2); \
49 END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NAMESPACE
51 $declare_function_macro_namespace .= generate_from_to(
52 <<'END_OF_DECLARE_FUNCTION_MACRO_NAMESPACE','const GiNaC::ex & p${N}','p${N}',3,$maxargs);
53 #define DECLARE_FUNCTION_${N}P(NAME) \\
54 extern const unsigned function_index_##NAME; \\
55 inline GiNaC::function NAME(${SEQ1}) { \\
56 return GiNaC::function(function_index_##NAME, ${SEQ2}); \\
59 END_OF_DECLARE_FUNCTION_MACRO_NAMESPACE
61 $declare_function_macro_no_namespace = <<'END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NO_NAMESPACE';
62 #define DECLARE_FUNCTION_1P(NAME) \
63 extern const unsigned function_index_##NAME; \
64 inline function NAME(const ex & p1) { \
65 return function(function_index_##NAME, p1); \
67 #define DECLARE_FUNCTION_2P(NAME) \
68 extern const unsigned function_index_##NAME; \
69 inline function NAME(const ex & p1, const ex & p2) { \
70 return function(function_index_##NAME, p1, p2); \
73 END_OF_DECLARE_FUNCTION_1_AND_2P_MACRO_NO_NAMESPACE
75 $declare_function_macro_no_namespace .= generate_from_to(
76 <<'END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE','const ex & p${N}','p${N}',3,$maxargs);
77 #define DECLARE_FUNCTION_${N}P(NAME) \\
78 extern const unsigned function_index_##NAME; \\
79 inline function NAME(${SEQ1}) { \\
80 return function(function_index_##NAME, ${SEQ2}); \\
83 END_OF_DECLARE_FUNCTION_MACRO_NO_NAMESPACE
85 $typedef_eval_funcp=generate(
86 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
89 $typedef_evalf_funcp=generate(
90 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
93 $typedef_derivative_funcp=generate(
94 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
97 $typedef_series_funcp=generate(
98 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
101 $eval_func_interface=generate(' function_options & eval_func(eval_funcp_${N} e);'."\n",'','');
103 $evalf_func_interface=generate(' function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','');
105 $derivative_func_interface=generate(' function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','');
107 $series_func_interface=generate(' function_options & series_func(series_funcp_${N} s);'."\n",'','');
109 $constructors_interface=generate(
110 ' function(unsigned ser, ${SEQ1});'."\n",
111 'const ex & param${N}','');
113 $constructors_implementation=generate(
114 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}');
115 function::function(unsigned ser, ${SEQ1})
116 : exprseq(${SEQ2}), serial(ser)
118 debugmsg(\"function constructor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
119 tinfo_key = TINFO_function;
121 END_OF_CONSTRUCTORS_IMPLEMENTATION
123 $eval_switch_statement=generate(
124 <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
126 eval_result=((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
128 END_OF_EVAL_SWITCH_STATEMENT
130 $evalf_switch_statement=generate(
131 <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
133 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
134 END_OF_EVALF_SWITCH_STATEMENT
136 $diff_switch_statement=generate(
137 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
139 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
140 END_OF_DIFF_SWITCH_STATEMENT
142 $series_switch_statement=generate(
143 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
146 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
147 } catch (do_taylor) {
148 res = basic::series(r, order, options);
151 END_OF_SERIES_SWITCH_STATEMENT
153 $eval_func_implementation=generate(
154 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','');
155 function_options & function_options::eval_func(eval_funcp_${N} e)
157 test_and_set_nparams(${N});
158 eval_f=eval_funcp(e);
161 END_OF_EVAL_FUNC_IMPLEMENTATION
163 $evalf_func_implementation=generate(
164 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','');
165 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
167 test_and_set_nparams(${N});
168 evalf_f=evalf_funcp(ef);
171 END_OF_EVALF_FUNC_IMPLEMENTATION
173 $derivative_func_implementation=generate(
174 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','');
175 function_options & function_options::derivative_func(derivative_funcp_${N} d)
177 test_and_set_nparams(${N});
178 derivative_f=derivative_funcp(d);
181 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
183 $series_func_implementation=generate(
184 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','');
185 function_options & function_options::series_func(series_funcp_${N} s)
187 test_and_set_nparams(${N});
188 series_f=series_funcp(s);
191 END_OF_SERIES_FUNC_IMPLEMENTATION
193 $interface=<<END_OF_INTERFACE;
194 /** \@file function.h
196 * Interface to abstract class function (new function concept). */
199 * This file was generated automatically by function.pl.
200 * Please do not modify it directly, edit the perl script instead!
201 * function.pl options: \$maxargs=${maxargs}
203 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
205 * This program is free software; you can redistribute it and/or modify
206 * it under the terms of the GNU General Public License as published by
207 * the Free Software Foundation; either version 2 of the License, or
208 * (at your option) any later version.
210 * This program is distributed in the hope that it will be useful,
211 * but WITHOUT ANY WARRANTY; without even the implied warranty of
212 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
213 * GNU General Public License for more details.
215 * You should have received a copy of the GNU General Public License
216 * along with this program; if not, write to the Free Software
217 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
220 #ifndef __GINAC_FUNCTION_H__
221 #define __GINAC_FUNCTION_H__
226 // CINT needs <algorithm> to work properly with <vector>
231 #ifndef NO_NAMESPACE_GINAC
233 // the following lines have been generated for max. ${maxargs} parameters
234 $declare_function_macro_namespace
235 // end of generated lines
237 #else // ndef NO_NAMESPACE_GINAC
239 // the following lines have been generated for max. ${maxargs} parameters
240 $declare_function_macro_no_namespace
241 // end of generated lines
243 #endif // ndef NO_NAMESPACE_GINAC
245 #ifndef NO_NAMESPACE_GINAC
247 #define REGISTER_FUNCTION(NAME,OPT) \\
248 const unsigned function_index_##NAME= \\
249 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
251 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
252 const unsigned function_index_##NAME= \\
253 GiNaC::function::register_new(GiNaC::function_options(#NAME). \\
256 derivative_func(D). \\
259 #else // ndef NO_NAMESPACE_GINAC
261 #define REGISTER_FUNCTION(NAME,OPT) \\
262 const unsigned function_index_##NAME= \\
263 function::register_new(function_options(#NAME).OPT);
265 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
266 const unsigned function_index_##NAME= \\
267 function::register_new(function_options(#NAME). \\
270 derivative_func(D). \\
273 #endif // ndef NO_NAMESPACE_GINAC
275 #define BEGIN_TYPECHECK \\
276 bool automatic_typecheck=true;
278 #define TYPECHECK(VAR,TYPE) \\
279 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
280 automatic_typecheck=false; \\
283 #ifndef NO_NAMESPACE_GINAC
285 #define TYPECHECK_INTEGER(VAR) \\
286 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
287 automatic_typecheck=false; \\
290 #else // ndef NO_NAMESPACE_GINAC
292 #define TYPECHECK_INTEGER(VAR) \\
293 if (!(VAR).info(info_flags::integer)) { \\
294 automatic_typecheck=false; \\
297 #endif // ndef NO_NAMESPACE_GINAC
299 #define END_TYPECHECK(RV) \\
301 if (!automatic_typecheck) { \\
305 #ifndef NO_NAMESPACE_GINAC
307 #endif // ndef NO_NAMESPACE_GINAC
311 typedef ex (* eval_funcp)();
312 typedef ex (* evalf_funcp)();
313 typedef ex (* derivative_funcp)();
314 typedef ex (* series_funcp)();
316 // the following lines have been generated for max. ${maxargs} parameters
319 $typedef_derivative_funcp
320 $typedef_series_funcp
321 // end of generated lines
323 class function_options
325 friend class function;
328 function_options(std::string const & n, std::string const & tn=std::string());
330 void initialize(void);
331 function_options & set_name(std::string const & n, std::string const & tn=std::string());
332 // the following lines have been generated for max. ${maxargs} parameters
334 $evalf_func_interface
335 $derivative_func_interface
336 $series_func_interface
337 // end of generated lines
338 function_options & set_return_type(unsigned rt, unsigned rtt=0);
339 function_options & do_not_evalf_params(void);
340 function_options & remember(unsigned size, unsigned assoc_size=0,
341 unsigned strategy=remember_strategies::delete_never);
342 function_options & overloaded(unsigned o);
343 void test_and_set_nparams(unsigned n);
344 std::string get_name(void) const { return name; }
345 unsigned get_nparams(void) const { return nparams; }
349 std::string TeX_name;
355 derivative_funcp derivative_f;
356 series_funcp series_f;
358 bool evalf_params_first;
360 bool use_return_type;
361 unsigned return_type;
362 unsigned return_type_tinfo;
365 unsigned remember_size;
366 unsigned remember_assoc_size;
367 unsigned remember_strategy;
369 unsigned functions_with_same_name;
372 /** The class function is used to implement builtin functions like sin, cos...
373 and user defined functions */
374 class function : public exprseq
376 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
378 // CINT has a linking problem
380 friend void ginsh_get_ginac_functions(void);
381 #endif // def __MAKECINT__
383 friend class remember_table_entry;
384 // friend class remember_table_list;
385 // friend class remember_table;
389 // other constructors
391 function(unsigned ser);
392 // the following lines have been generated for max. ${maxargs} parameters
393 $constructors_interface
394 // end of generated lines
395 function(unsigned ser, const exprseq & es);
396 function(unsigned ser, const exvector & v, bool discardable=0);
397 function(unsigned ser, exvector * vp); // vp will be deleted
399 // functions overriding virtual functions from bases classes
401 void printraw(std::ostream & os) const;
402 void print(std::ostream & os, unsigned upper_precedence=0) const;
403 void printtree(std::ostream & os, unsigned indent) const;
404 void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence=0) const;
405 ex expand(unsigned options=0) const;
406 ex eval(int level=0) const;
407 ex evalf(int level=0) const;
408 ex series(const relational & r, int order, unsigned options = 0) const;
409 ex thisexprseq(const exvector & v) const;
410 ex thisexprseq(exvector * vp) const;
412 ex derivative(const symbol & s) const;
413 bool is_equal_same_type(const basic & other) const;
414 unsigned return_type(void) const;
415 unsigned return_type_tinfo(void) const;
417 // new virtual functions which can be overridden by derived classes
420 // non-virtual functions in this class
422 ex pderivative(unsigned diff_param) const; // partial differentiation
423 static std::vector<function_options> & registered_functions(void);
424 bool lookup_remember_table(ex & result) const;
425 void store_remember_table(ex const & result) const;
427 static unsigned register_new(function_options const & opt);
428 static unsigned find_function(const std::string &name, unsigned nparams);
429 unsigned getserial(void) const {return serial;}
437 // utility functions/macros
438 inline const function &ex_to_function(const ex &e)
440 return static_cast<const function &>(*e.bp);
443 #ifndef NO_NAMESPACE_GINAC
445 #define is_ex_the_function(OBJ, FUNCNAME) \\
446 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
448 #else // ndef NO_NAMESPACE_GINAC
450 #define is_ex_the_function(OBJ, FUNCNAME) \\
451 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
453 #endif // ndef NO_NAMESPACE_GINAC
457 extern const function some_function;
458 extern const std::type_info & typeid_function;
460 #ifndef NO_NAMESPACE_GINAC
462 #endif // ndef NO_NAMESPACE_GINAC
464 #endif // ndef __GINAC_FUNCTION_H__
468 $implementation=<<END_OF_IMPLEMENTATION;
469 /** \@file function.cpp
471 * Implementation of class function. */
474 * This file was generated automatically by function.pl.
475 * Please do not modify it directly, edit the perl script instead!
476 * function.pl options: \$maxargs=${maxargs}
478 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
480 * This program is free software; you can redistribute it and/or modify
481 * it under the terms of the GNU General Public License as published by
482 * the Free Software Foundation; either version 2 of the License, or
483 * (at your option) any later version.
485 * This program is distributed in the hope that it will be useful,
486 * but WITHOUT ANY WARRANTY; without even the implied warranty of
487 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
488 * GNU General Public License for more details.
490 * You should have received a copy of the GNU General Public License
491 * along with this program; if not, write to the Free Software
492 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
499 #include "function.h"
505 #include "debugmsg.h"
506 #include "remember.h"
508 #ifndef NO_NAMESPACE_GINAC
510 #endif // ndef NO_NAMESPACE_GINAC
513 // helper class function_options
516 function_options::function_options()
521 function_options::function_options(std::string const & n, std::string const & tn)
527 function_options::~function_options()
529 // nothing to clean up at the moment
532 void function_options::initialize(void)
534 set_name("unnamed_function","\\\\operatorname{unnamed}");
536 eval_f=evalf_f=derivative_f=series_f=0;
537 evalf_params_first=true;
538 use_return_type=false;
540 functions_with_same_name=1;
543 function_options & function_options::set_name(std::string const & n,
544 std::string const & tn)
547 if (tn==std::string()) {
548 TeX_name="\\\\operatorname{"+name+"}";
555 // the following lines have been generated for max. ${maxargs} parameters
556 $eval_func_implementation
557 $evalf_func_implementation
558 $derivative_func_implementation
559 $series_func_implementation
560 // end of generated lines
562 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
564 use_return_type=true;
566 return_type_tinfo=rtt;
570 function_options & function_options::do_not_evalf_params(void)
572 evalf_params_first=false;
576 function_options & function_options::remember(unsigned size,
582 remember_assoc_size=assoc_size;
583 remember_strategy=strategy;
587 function_options & function_options::overloaded(unsigned o)
589 functions_with_same_name=o;
593 void function_options::test_and_set_nparams(unsigned n)
597 } else if (nparams!=n) {
598 // we do not throw an exception here because this code is
599 // usually executed before main(), so the exception could not
601 std::cerr << "WARNING: number of parameters ("
602 << n << ") differs from number set before ("
603 << nparams << ")" << std::endl;
607 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
610 // default constructor, destructor, copy constructor assignment operator and helpers
615 function::function() : serial(0)
617 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
618 tinfo_key = TINFO_function;
623 void function::copy(const function & other)
625 exprseq::copy(other);
629 void function::destroy(bool call_parent)
631 if (call_parent) exprseq::destroy(call_parent);
635 // other constructors
640 function::function(unsigned ser) : serial(ser)
642 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
643 tinfo_key = TINFO_function;
646 // the following lines have been generated for max. ${maxargs} parameters
647 $constructors_implementation
648 // end of generated lines
650 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
652 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
653 tinfo_key = TINFO_function;
656 function::function(unsigned ser, const exvector & v, bool discardable)
657 : exprseq(v,discardable), serial(ser)
659 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
660 tinfo_key = TINFO_function;
663 function::function(unsigned ser, exvector * vp)
664 : exprseq(vp), serial(ser)
666 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
667 tinfo_key = TINFO_function;
674 /** Construct object from archive_node. */
675 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
677 debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
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, const 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 bases classes
716 void function::printraw(std::ostream & os) const
718 debugmsg("function printraw",LOGLEVEL_PRINT);
720 GINAC_ASSERT(serial<registered_functions().size());
722 os << "function(name=" << registered_functions()[serial].name;
723 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
730 void function::print(std::ostream & os, unsigned upper_precedence) const
732 debugmsg("function print",LOGLEVEL_PRINT);
734 GINAC_ASSERT(serial<registered_functions().size());
736 os << registered_functions()[serial].name;
737 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
740 void function::printtree(std::ostream & os, unsigned indent) const
742 debugmsg("function printtree",LOGLEVEL_PRINT);
744 GINAC_ASSERT(serial<registered_functions().size());
746 os << std::string(indent,' ') << "function "
747 << registered_functions()[serial].name
748 << ", hash=" << hashvalue
749 << " (0x" << std::hex << hashvalue << std::dec << ")"
750 << ", flags=" << flags
751 << ", nops=" << nops() << std::endl;
752 for (unsigned i=0; i<nops(); ++i) {
753 seq[i].printtree(os,indent+delta_indent);
755 os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
758 void function::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
760 debugmsg("function print csrc",LOGLEVEL_PRINT);
762 GINAC_ASSERT(serial<registered_functions().size());
764 // Print function name in lowercase
766 lname=registered_functions()[serial].name;
767 for (unsigned i=0; i<lname.size(); i++)
768 lname[i] = tolower(lname[i]);
771 // Print arguments, separated by commas
772 exvector::const_iterator it = seq.begin();
773 exvector::const_iterator itend = seq.end();
774 while (it != itend) {
775 it->bp->printcsrc(os, type, 0);
783 ex function::expand(unsigned options) const
785 return this->setflag(status_flags::expanded);
788 ex function::eval(int level) const
790 GINAC_ASSERT(serial<registered_functions().size());
793 // first evaluate children, then we will end up here again
794 return function(serial,evalchildren(level));
797 if (registered_functions()[serial].eval_f==0) {
801 bool use_remember=registered_functions()[serial].use_remember;
803 if (use_remember && lookup_remember_table(eval_result)) {
807 switch (registered_functions()[serial].nparams) {
808 // the following lines have been generated for max. ${maxargs} parameters
809 ${eval_switch_statement}
810 // end of generated lines
812 throw(std::logic_error("function::eval(): invalid nparams"));
815 store_remember_table(eval_result);
820 ex function::evalf(int level) const
822 GINAC_ASSERT(serial<registered_functions().size());
824 exvector eseq=evalfchildren(level);
826 if (registered_functions()[serial].evalf_f==0) {
827 return function(serial,eseq).hold();
829 switch (registered_functions()[serial].nparams) {
830 // the following lines have been generated for max. ${maxargs} parameters
831 ${evalf_switch_statement}
832 // end of generated lines
834 throw(std::logic_error("function::evalf(): invalid nparams"));
837 ex function::thisexprseq(const exvector & v) const
839 return function(serial,v);
842 ex function::thisexprseq(exvector * vp) const
844 return function(serial,vp);
847 /** Implementation of ex::series for functions.
848 * \@see ex::series */
849 ex function::series(const relational & r, int order, unsigned options) const
851 GINAC_ASSERT(serial<registered_functions().size());
853 if (registered_functions()[serial].series_f==0) {
854 return basic::series(r, order);
857 switch (registered_functions()[serial].nparams) {
858 // the following lines have been generated for max. ${maxargs} parameters
859 ${series_switch_statement}
860 // end of generated lines
862 throw(std::logic_error("function::series(): invalid nparams"));
868 /** Implementation of ex::diff() for functions. It applies the chain rule,
869 * except for the Order term function.
871 ex function::derivative(const symbol & s) const
875 if (serial == function_index_Order) {
876 // Order Term function only differentiates the argument
877 return Order(seq[0].diff(s));
878 } else if (serial == function_index_Derivative) {
879 // Inert derivative performs chain rule on the first argument only, and
880 // adds differentiation parameter to list (second argument)
881 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
882 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
885 for (unsigned i=0; i!=fcn.nops(); i++) {
886 arg_diff = fcn.op(i).diff(s);
887 if (!arg_diff.is_zero()) {
888 lst new_lst = ex_to_lst(seq[1]);
890 result += arg_diff * Derivative(fcn, new_lst);
896 for (unsigned i=0; i!=seq.size(); i++) {
897 arg_diff = seq[i].diff(s);
898 // We apply the chain rule only when it makes sense. This is not
899 // just for performance reasons but also to allow functions to
900 // throw when differentiated with respect to one of its arguments
901 // without running into trouble with our automatic full
903 if (!arg_diff.is_zero())
904 result += pderivative(i)*arg_diff;
910 int function::compare_same_type(const basic & other) const
912 GINAC_ASSERT(is_of_type(other, function));
913 const function & o=static_cast<function &>(const_cast<basic &>(other));
915 if (serial!=o.serial) {
916 return serial < o.serial ? -1 : 1;
918 return exprseq::compare_same_type(o);
921 bool function::is_equal_same_type(const basic & other) const
923 GINAC_ASSERT(is_of_type(other, function));
924 const function & o=static_cast<function &>(const_cast<basic &>(other));
926 if (serial!=o.serial) return false;
927 return exprseq::is_equal_same_type(o);
930 unsigned function::return_type(void) const
933 return return_types::commutative;
935 return (*seq.begin()).return_type();
938 unsigned function::return_type_tinfo(void) const
943 return (*seq.begin()).return_type_tinfo();
947 // new virtual functions which can be overridden by derived classes
953 // non-virtual functions in this class
958 ex function::pderivative(unsigned diff_param) const // partial differentiation
960 GINAC_ASSERT(serial<registered_functions().size());
962 if (registered_functions()[serial].derivative_f==0) {
963 return Derivative(*this, lst(ex(diff_param)));
965 switch (registered_functions()[serial].nparams) {
966 // the following lines have been generated for max. ${maxargs} parameters
967 ${diff_switch_statement}
968 // end of generated lines
970 throw(std::logic_error("function::pderivative(): no diff function defined"));
973 std::vector<function_options> & function::registered_functions(void)
975 static std::vector<function_options> * rf = new std::vector<function_options>;
979 bool function::lookup_remember_table(ex & result) const
981 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
984 void function::store_remember_table(ex const & result) const
986 remember_table::remember_tables()[serial].add_entry(*this,result);
991 unsigned function::register_new(function_options const & opt)
993 unsigned same_name=0;
994 for (unsigned i=0; i<registered_functions().size(); ++i) {
995 if (registered_functions()[i].name==opt.name) {
999 if (same_name>=opt.functions_with_same_name) {
1000 // we do not throw an exception here because this code is
1001 // usually executed before main(), so the exception could not
1003 std::cerr << "WARNING: function name " << opt.name
1004 << " already in use!" << std::endl;
1006 registered_functions().push_back(opt);
1007 if (opt.use_remember) {
1008 remember_table::remember_tables().
1009 push_back(remember_table(opt.remember_size,
1010 opt.remember_assoc_size,
1011 opt.remember_strategy));
1013 remember_table::remember_tables().push_back(remember_table());
1015 return registered_functions().size()-1;
1018 /** Find serial number of function by name and number of parameters.
1019 * Throws exception if function was not found. */
1020 unsigned function::find_function(const std::string &name, unsigned nparams)
1022 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1023 unsigned serial = 0;
1025 if (i->get_name() == name && i->get_nparams() == nparams)
1030 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1034 // static member variables
1043 const function some_function;
1044 const std::type_info & typeid_function=typeid(some_function);
1046 #ifndef NO_NAMESPACE_GINAC
1047 } // namespace GiNaC
1048 #endif // ndef NO_NAMESPACE_GINAC
1050 END_OF_IMPLEMENTATION
1052 print "Creating interface file function.h...";
1053 open OUT,">function.h" or die "cannot open function.h";
1054 print OUT $interface;
1058 print "Creating implementation file function.cpp...";
1059 open OUT,">function.cpp" or die "cannot open function.cpp";
1060 print OUT $implementation;