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});
135 END_OF_EVALF_SWITCH_STATEMENT
137 $diff_switch_statement=generate(
138 <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
140 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
142 END_OF_DIFF_SWITCH_STATEMENT
144 $series_switch_statement=generate(
145 <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
148 res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
149 } catch (do_taylor) {
150 res = basic::series(r, order, options);
154 END_OF_SERIES_SWITCH_STATEMENT
156 $eval_func_implementation=generate(
157 <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','');
158 function_options & function_options::eval_func(eval_funcp_${N} e)
160 test_and_set_nparams(${N});
161 eval_f=eval_funcp(e);
164 END_OF_EVAL_FUNC_IMPLEMENTATION
166 $evalf_func_implementation=generate(
167 <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','');
168 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
170 test_and_set_nparams(${N});
171 evalf_f=evalf_funcp(ef);
174 END_OF_EVALF_FUNC_IMPLEMENTATION
176 $derivative_func_implementation=generate(
177 <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','');
178 function_options & function_options::derivative_func(derivative_funcp_${N} d)
180 test_and_set_nparams(${N});
181 derivative_f=derivative_funcp(d);
184 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
186 $series_func_implementation=generate(
187 <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','');
188 function_options & function_options::series_func(series_funcp_${N} s)
190 test_and_set_nparams(${N});
191 series_f=series_funcp(s);
194 END_OF_SERIES_FUNC_IMPLEMENTATION
196 $interface=<<END_OF_INTERFACE;
197 /** \@file function.h
199 * Interface to abstract class function (new function concept). */
202 * This file was generated automatically by function.pl.
203 * Please do not modify it directly, edit the perl script instead!
204 * function.pl options: \$maxargs=${maxargs}
206 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
208 * This program is free software; you can redistribute it and/or modify
209 * it under the terms of the GNU General Public License as published by
210 * the Free Software Foundation; either version 2 of the License, or
211 * (at your option) any later version.
213 * This program is distributed in the hope that it will be useful,
214 * but WITHOUT ANY WARRANTY; without even the implied warranty of
215 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
216 * GNU General Public License for more details.
218 * You should have received a copy of the GNU General Public License
219 * along with this program; if not, write to the Free Software
220 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
223 #ifndef __GINAC_FUNCTION_H__
224 #define __GINAC_FUNCTION_H__
229 // CINT needs <algorithm> to work properly with <vector>
234 #ifndef NO_NAMESPACE_GINAC
236 // the following lines have been generated for max. ${maxargs} parameters
237 $declare_function_macro_namespace
238 // end of generated lines
240 #else // ndef NO_NAMESPACE_GINAC
242 // the following lines have been generated for max. ${maxargs} parameters
243 $declare_function_macro_no_namespace
244 // end of generated lines
246 #endif // ndef NO_NAMESPACE_GINAC
248 #ifndef NO_NAMESPACE_GINAC
250 #define REGISTER_FUNCTION(NAME,OPT) \\
251 const unsigned function_index_##NAME= \\
252 GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
254 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
255 const unsigned function_index_##NAME= \\
256 GiNaC::function::register_new(GiNaC::function_options(#NAME). \\
259 derivative_func(D). \\
262 #else // ndef NO_NAMESPACE_GINAC
264 #define REGISTER_FUNCTION(NAME,OPT) \\
265 const unsigned function_index_##NAME= \\
266 function::register_new(function_options(#NAME).OPT);
268 #define REGISTER_FUNCTION_OLD(NAME,E,EF,D,S) \\
269 const unsigned function_index_##NAME= \\
270 function::register_new(function_options(#NAME). \\
273 derivative_func(D). \\
276 #endif // ndef NO_NAMESPACE_GINAC
278 #define BEGIN_TYPECHECK \\
279 bool automatic_typecheck=true;
281 #define TYPECHECK(VAR,TYPE) \\
282 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
283 automatic_typecheck=false; \\
286 #ifndef NO_NAMESPACE_GINAC
288 #define TYPECHECK_INTEGER(VAR) \\
289 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
290 automatic_typecheck=false; \\
293 #else // ndef NO_NAMESPACE_GINAC
295 #define TYPECHECK_INTEGER(VAR) \\
296 if (!(VAR).info(info_flags::integer)) { \\
297 automatic_typecheck=false; \\
300 #endif // ndef NO_NAMESPACE_GINAC
302 #define END_TYPECHECK(RV) \\
304 if (!automatic_typecheck) { \\
308 #ifndef NO_NAMESPACE_GINAC
310 #endif // ndef NO_NAMESPACE_GINAC
314 typedef ex (* eval_funcp)();
315 typedef ex (* evalf_funcp)();
316 typedef ex (* derivative_funcp)();
317 typedef ex (* series_funcp)();
319 // the following lines have been generated for max. ${maxargs} parameters
322 $typedef_derivative_funcp
323 $typedef_series_funcp
324 // end of generated lines
326 class function_options
328 friend class function;
331 function_options(std::string const & n, std::string const & tn=std::string());
333 void initialize(void);
334 function_options & set_name(std::string const & n, std::string const & tn=std::string());
335 // the following lines have been generated for max. ${maxargs} parameters
337 $evalf_func_interface
338 $derivative_func_interface
339 $series_func_interface
340 // end of generated lines
341 function_options & set_return_type(unsigned rt, unsigned rtt=0);
342 function_options & do_not_evalf_params(void);
343 function_options & remember(unsigned size, unsigned assoc_size=0,
344 unsigned strategy=remember_strategies::delete_never);
345 function_options & overloaded(unsigned o);
346 void test_and_set_nparams(unsigned n);
347 std::string get_name(void) const { return name; }
348 unsigned get_nparams(void) const { return nparams; }
352 std::string TeX_name;
358 derivative_funcp derivative_f;
359 series_funcp series_f;
361 bool evalf_params_first;
363 bool use_return_type;
364 unsigned return_type;
365 unsigned return_type_tinfo;
368 unsigned remember_size;
369 unsigned remember_assoc_size;
370 unsigned remember_strategy;
372 unsigned functions_with_same_name;
375 /** The class function is used to implement builtin functions like sin, cos...
376 and user defined functions */
377 class function : public exprseq
379 GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
381 // CINT has a linking problem
383 friend void ginsh_get_ginac_functions(void);
384 #endif // def __MAKECINT__
386 friend class remember_table_entry;
387 // friend class remember_table_list;
388 // friend class remember_table;
392 // default constructor, destructor, copy constructor assignment operator and helpers
396 function(const function & other);
397 const function & operator=(const function & other);
399 void copy(const function & other);
400 void destroy(bool call_parent);
402 // other constructors
404 function(unsigned ser);
405 // the following lines have been generated for max. ${maxargs} parameters
406 $constructors_interface
407 // end of generated lines
408 function(unsigned ser, const exprseq & es);
409 function(unsigned ser, const exvector & v, bool discardable=0);
410 function(unsigned ser, exvector * vp); // vp will be deleted
412 // functions overriding virtual functions from bases classes
414 basic * duplicate() const;
415 void printraw(std::ostream & os) const;
416 void print(std::ostream & os, unsigned upper_precedence=0) const;
417 void printtree(std::ostream & os, unsigned indent) const;
418 void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence=0) const;
419 ex expand(unsigned options=0) const;
420 ex eval(int level=0) const;
421 ex evalf(int level=0) const;
422 ex series(const relational & r, int order, unsigned options = 0) const;
423 ex thisexprseq(const exvector & v) const;
424 ex thisexprseq(exvector * vp) const;
426 ex derivative(const symbol & s) const;
427 int compare_same_type(const basic & other) const;
428 bool is_equal_same_type(const basic & other) const;
429 unsigned return_type(void) const;
430 unsigned return_type_tinfo(void) const;
432 // new virtual functions which can be overridden by derived classes
435 // non-virtual functions in this class
437 ex pderivative(unsigned diff_param) const; // partial differentiation
438 static std::vector<function_options> & registered_functions(void);
439 bool lookup_remember_table(ex & result) const;
440 void store_remember_table(ex const & result) const;
442 static unsigned register_new(function_options const & opt);
443 static unsigned find_function(const std::string &name, unsigned nparams);
444 unsigned getserial(void) const {return serial;}
452 // utility functions/macros
453 inline const function &ex_to_function(const ex &e)
455 return static_cast<const function &>(*e.bp);
458 #ifndef NO_NAMESPACE_GINAC
460 #define is_ex_the_function(OBJ, FUNCNAME) \\
461 (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
463 #else // ndef NO_NAMESPACE_GINAC
465 #define is_ex_the_function(OBJ, FUNCNAME) \\
466 (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
468 #endif // ndef NO_NAMESPACE_GINAC
472 extern const function some_function;
473 extern const type_info & typeid_function;
475 #ifndef NO_NAMESPACE_GINAC
477 #endif // ndef NO_NAMESPACE_GINAC
479 #endif // ndef __GINAC_FUNCTION_H__
483 $implementation=<<END_OF_IMPLEMENTATION;
484 /** \@file function.cpp
486 * Implementation of class function. */
489 * This file was generated automatically by function.pl.
490 * Please do not modify it directly, edit the perl script instead!
491 * function.pl options: \$maxargs=${maxargs}
493 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
495 * This program is free software; you can redistribute it and/or modify
496 * it under the terms of the GNU General Public License as published by
497 * the Free Software Foundation; either version 2 of the License, or
498 * (at your option) any later version.
500 * This program is distributed in the hope that it will be useful,
501 * but WITHOUT ANY WARRANTY; without even the implied warranty of
502 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
503 * GNU General Public License for more details.
505 * You should have received a copy of the GNU General Public License
506 * along with this program; if not, write to the Free Software
507 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
514 #include "function.h"
520 #include "debugmsg.h"
521 #include "remember.h"
523 #ifndef NO_NAMESPACE_GINAC
525 #endif // ndef NO_NAMESPACE_GINAC
528 // helper class function_options
531 function_options::function_options()
536 function_options::function_options(std::string const & n, std::string const & tn)
542 function_options::~function_options()
544 // nothing to clean up at the moment
547 void function_options::initialize(void)
549 set_name("unnamed_function","\\\\operatorname{unnamed}");
551 eval_f=evalf_f=derivative_f=series_f=0;
552 evalf_params_first=true;
553 use_return_type=false;
555 functions_with_same_name=1;
558 function_options & function_options::set_name(std::string const & n,
559 std::string const & tn)
562 if (tn==std::string()) {
563 TeX_name="\\\\operatorname{"+name+"}";
570 // the following lines have been generated for max. ${maxargs} parameters
571 $eval_func_implementation
572 $evalf_func_implementation
573 $derivative_func_implementation
574 $series_func_implementation
575 // end of generated lines
577 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
579 use_return_type=true;
581 return_type_tinfo=rtt;
585 function_options & function_options::do_not_evalf_params(void)
587 evalf_params_first=false;
591 function_options & function_options::remember(unsigned size,
597 remember_assoc_size=assoc_size;
598 remember_strategy=strategy;
602 function_options & function_options::overloaded(unsigned o)
604 functions_with_same_name=o;
608 void function_options::test_and_set_nparams(unsigned n)
612 } else if (nparams!=n) {
613 // we do not throw an exception here because this code is
614 // usually executed before main(), so the exception could not
616 std::cerr << "WARNING: number of parameters ("
617 << n << ") differs from number set before ("
618 << nparams << ")" << std::endl;
622 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
625 // default constructor, destructor, copy constructor assignment operator and helpers
630 function::function() : serial(0)
632 debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
633 tinfo_key = TINFO_function;
636 function::~function()
638 debugmsg("function destructor",LOGLEVEL_DESTRUCT);
642 function::function(const function & other)
644 debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
648 const function & function::operator=(const function & other)
650 debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
651 if (this != &other) {
660 void function::copy(const function & other)
662 exprseq::copy(other);
666 void function::destroy(bool call_parent)
668 if (call_parent) exprseq::destroy(call_parent);
672 // other constructors
677 function::function(unsigned ser) : serial(ser)
679 debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
680 tinfo_key = TINFO_function;
683 // the following lines have been generated for max. ${maxargs} parameters
684 $constructors_implementation
685 // end of generated lines
687 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
689 debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
690 tinfo_key = TINFO_function;
693 function::function(unsigned ser, const exvector & v, bool discardable)
694 : exprseq(v,discardable), serial(ser)
696 debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
697 tinfo_key = TINFO_function;
700 function::function(unsigned ser, exvector * vp)
701 : exprseq(vp), serial(ser)
703 debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
704 tinfo_key = TINFO_function;
711 /** Construct object from archive_node. */
712 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
714 debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
716 // Find serial number by function name
718 if (n.find_string("name", s)) {
719 unsigned int ser = 0;
720 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
728 throw (std::runtime_error("unknown function '" + s + "' in archive"));
730 throw (std::runtime_error("unnamed function in archive"));
733 /** Unarchive the object. */
734 ex function::unarchive(const archive_node &n, const lst &sym_lst)
736 return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
739 /** Archive the object. */
740 void function::archive(archive_node &n) const
742 inherited::archive(n);
743 GINAC_ASSERT(serial < registered_functions().size());
744 n.add_string("name", registered_functions()[serial].name);
748 // functions overriding virtual functions from bases classes
753 basic * function::duplicate() const
755 debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
756 return new function(*this);
759 void function::printraw(std::ostream & os) const
761 debugmsg("function printraw",LOGLEVEL_PRINT);
763 GINAC_ASSERT(serial<registered_functions().size());
765 os << "function(name=" << registered_functions()[serial].name;
766 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
773 void function::print(std::ostream & os, unsigned upper_precedence) const
775 debugmsg("function print",LOGLEVEL_PRINT);
777 GINAC_ASSERT(serial<registered_functions().size());
779 os << registered_functions()[serial].name;
780 printseq(os,'(',',',')',exprseq::precedence,function::precedence);
783 void function::printtree(std::ostream & os, unsigned indent) const
785 debugmsg("function printtree",LOGLEVEL_PRINT);
787 GINAC_ASSERT(serial<registered_functions().size());
789 os << std::string(indent,' ') << "function "
790 << registered_functions()[serial].name
791 << ", hash=" << hashvalue
792 << " (0x" << std::hex << hashvalue << std::dec << ")"
793 << ", flags=" << flags
794 << ", nops=" << nops() << std::endl;
795 for (unsigned i=0; i<nops(); ++i) {
796 seq[i].printtree(os,indent+delta_indent);
798 os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
801 void function::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
803 debugmsg("function print csrc",LOGLEVEL_PRINT);
805 GINAC_ASSERT(serial<registered_functions().size());
807 // Print function name in lowercase
809 lname=registered_functions()[serial].name;
810 for (unsigned i=0; i<lname.size(); i++)
811 lname[i] = tolower(lname[i]);
814 // Print arguments, separated by commas
815 exvector::const_iterator it = seq.begin();
816 exvector::const_iterator itend = seq.end();
817 while (it != itend) {
818 it->bp->printcsrc(os, type, 0);
826 ex function::expand(unsigned options) const
828 return this->setflag(status_flags::expanded);
831 ex function::eval(int level) const
833 GINAC_ASSERT(serial<registered_functions().size());
836 // first evaluate children, then we will end up here again
837 return function(serial,evalchildren(level));
840 if (registered_functions()[serial].eval_f==0) {
844 bool use_remember=registered_functions()[serial].use_remember;
846 if (use_remember && lookup_remember_table(eval_result)) {
850 switch (registered_functions()[serial].nparams) {
851 // the following lines have been generated for max. ${maxargs} parameters
852 ${eval_switch_statement}
853 // end of generated lines
855 throw(std::logic_error("function::eval(): invalid nparams"));
858 store_remember_table(eval_result);
863 ex function::evalf(int level) const
865 GINAC_ASSERT(serial<registered_functions().size());
867 exvector eseq=evalfchildren(level);
869 if (registered_functions()[serial].evalf_f==0) {
870 return function(serial,eseq).hold();
872 switch (registered_functions()[serial].nparams) {
873 // the following lines have been generated for max. ${maxargs} parameters
874 ${evalf_switch_statement}
875 // end of generated lines
877 throw(std::logic_error("function::evalf(): invalid nparams"));
880 ex function::thisexprseq(const exvector & v) const
882 return function(serial,v);
885 ex function::thisexprseq(exvector * vp) const
887 return function(serial,vp);
890 /** Implementation of ex::series for functions.
891 * \@see ex::series */
892 ex function::series(const relational & r, int order, unsigned options = 0) const
894 GINAC_ASSERT(serial<registered_functions().size());
896 if (registered_functions()[serial].series_f==0) {
897 return basic::series(r, order);
900 switch (registered_functions()[serial].nparams) {
901 // the following lines have been generated for max. ${maxargs} parameters
902 ${series_switch_statement}
903 // end of generated lines
905 throw(std::logic_error("function::series(): invalid nparams"));
911 /** Implementation of ex::diff() for functions. It applies the chain rule,
912 * except for the Order term function.
914 ex function::derivative(const symbol & s) const
918 if (serial == function_index_Order) {
919 // Order Term function only differentiates the argument
920 return Order(seq[0].diff(s));
921 } else if (serial == function_index_Derivative) {
922 // Inert derivative performs chain rule on the first argument only, and
923 // adds differentiation parameter to list (second argument)
924 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
925 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
928 for (unsigned i=0; i!=fcn.nops(); i++) {
929 arg_diff = fcn.op(i).diff(s);
930 if (!arg_diff.is_zero()) {
931 lst new_lst = ex_to_lst(seq[1]);
933 result += arg_diff * Derivative(fcn, new_lst);
939 for (unsigned i=0; i!=seq.size(); i++) {
940 arg_diff = seq[i].diff(s);
941 // We apply the chain rule only when it makes sense. This is not
942 // just for performance reasons but also to allow functions to
943 // throw when differentiated with respect to one of its arguments
944 // without running into trouble with our automatic full
946 if (!arg_diff.is_zero())
947 result += pderivative(i)*arg_diff;
953 int function::compare_same_type(const basic & other) const
955 GINAC_ASSERT(is_of_type(other, function));
956 const function & o=static_cast<function &>(const_cast<basic &>(other));
958 if (serial!=o.serial) {
959 return serial < o.serial ? -1 : 1;
961 return exprseq::compare_same_type(o);
964 bool function::is_equal_same_type(const basic & other) const
966 GINAC_ASSERT(is_of_type(other, function));
967 const function & o=static_cast<function &>(const_cast<basic &>(other));
969 if (serial!=o.serial) return false;
970 return exprseq::is_equal_same_type(o);
973 unsigned function::return_type(void) const
976 return return_types::commutative;
978 return (*seq.begin()).return_type();
981 unsigned function::return_type_tinfo(void) const
986 return (*seq.begin()).return_type_tinfo();
990 // new virtual functions which can be overridden by derived classes
996 // non-virtual functions in this class
1001 ex function::pderivative(unsigned diff_param) const // partial differentiation
1003 GINAC_ASSERT(serial<registered_functions().size());
1005 if (registered_functions()[serial].derivative_f==0) {
1006 return Derivative(*this, lst(diff_param));
1008 switch (registered_functions()[serial].nparams) {
1009 // the following lines have been generated for max. ${maxargs} parameters
1010 ${diff_switch_statement}
1011 // end of generated lines
1013 throw(std::logic_error("function::pderivative(): no diff function defined"));
1016 std::vector<function_options> & function::registered_functions(void)
1018 static std::vector<function_options> * rf = new std::vector<function_options>;
1022 bool function::lookup_remember_table(ex & result) const
1024 return remember_table::remember_tables()[serial].lookup_entry(*this,result);
1027 void function::store_remember_table(ex const & result) const
1029 remember_table::remember_tables()[serial].add_entry(*this,result);
1034 unsigned function::register_new(function_options const & opt)
1036 unsigned same_name=0;
1037 for (unsigned i=0; i<registered_functions().size(); ++i) {
1038 if (registered_functions()[i].name==opt.name) {
1042 if (same_name>=opt.functions_with_same_name) {
1043 // we do not throw an exception here because this code is
1044 // usually executed before main(), so the exception could not
1046 std::cerr << "WARNING: function name " << opt.name
1047 << " already in use!" << std::endl;
1049 registered_functions().push_back(opt);
1050 if (opt.use_remember) {
1051 remember_table::remember_tables().
1052 push_back(remember_table(opt.remember_size,
1053 opt.remember_assoc_size,
1054 opt.remember_strategy));
1056 remember_table::remember_tables().push_back(remember_table());
1058 return registered_functions().size()-1;
1061 /** Find serial number of function by name and number of parameters.
1062 * Throws exception if function was not found. */
1063 unsigned function::find_function(const std::string &name, unsigned nparams)
1065 std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1066 unsigned serial = 0;
1068 if (i->get_name() == name && i->get_nparams() == nparams)
1073 throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1077 // static member variables
1086 const function some_function;
1087 const type_info & typeid_function=typeid(some_function);
1089 #ifndef NO_NAMESPACE_GINAC
1090 } // namespace GiNaC
1091 #endif // ndef NO_NAMESPACE_GINAC
1093 END_OF_IMPLEMENTATION
1095 print "Creating interface file function.h...";
1096 open OUT,">function.h" or die "cannot open function.h";
1097 print OUT $interface;
1101 print "Creating implementation file function.cpp...";
1102 open OUT,">function.cpp" or die "cannot open function.cpp";
1103 print OUT $implementation;