]> www.ginac.de Git - ginac.git/blob - ginac/function.pl
some cleanups
[ginac.git] / ginac / function.pl
1 $maxargs=13;
2
3 sub generate_seq {
4         my ($seq_template,$n)=@_;
5         my ($res,$N);
6         
7         $res='';
8         for ($N=1; $N<=$n; $N++) {
9                 $res .= eval('"' . $seq_template . '"');
10                 if ($N!=$n) {
11                         $res .= ', ';
12                 }
13         }
14         return $res;
15 }
16
17 sub generate_from_to {
18         my ($template,$seq_template1,$seq_template2,$from,$to)=@_;
19         my ($res,$N,$SEQ);
20
21         $res='';
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
28         }
29         return $res;
30 }
31
32 sub generate {
33         my ($template,$seq_template1,$seq_template2)=@_;
34         return generate_from_to($template,$seq_template1,$seq_template2,1,$maxargs);
35 }
36
37 $declare_function_macro = generate_from_to(
38         <<'END_OF_DECLARE_FUNCTION_MACRO','const GiNaC::ex & p${N}','p${N}',1,$maxargs);
39 #define DECLARE_FUNCTION_${N}P(NAME) \\
40 extern const unsigned function_index_##NAME; \\
41 inline const GiNaC::function NAME(${SEQ1}) { \\
42         return GiNaC::function(function_index_##NAME, ${SEQ2}); \\
43 }
44
45 END_OF_DECLARE_FUNCTION_MACRO
46
47 $typedef_eval_funcp=generate(
48 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
49 'const ex &','');
50
51 $typedef_evalf_funcp=generate(
52 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
53 'const ex &','');
54
55 $typedef_derivative_funcp=generate(
56 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
57 'const ex &','');
58
59 $typedef_series_funcp=generate(
60 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
61 'const ex &','');
62
63 $eval_func_interface=generate('    function_options & eval_func(eval_funcp_${N} e);'."\n",'','');
64
65 $evalf_func_interface=generate('    function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','');
66
67 $derivative_func_interface=generate('    function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','');
68
69 $series_func_interface=generate('    function_options & series_func(series_funcp_${N} s);'."\n",'','');
70
71 $constructors_interface=generate(
72 '    function(unsigned ser, ${SEQ1});'."\n",
73 'const ex & param${N}','');
74
75 $constructors_implementation=generate(
76         <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}');
77 function::function(unsigned ser, ${SEQ1})
78         : exprseq(${SEQ2}), serial(ser)
79 {
80         debugmsg(\"function ctor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
81         tinfo_key = TINFO_function;
82 }
83 END_OF_CONSTRUCTORS_IMPLEMENTATION
84
85 $eval_switch_statement=generate(
86         <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','');
87         case ${N}:
88                 eval_result = ((eval_funcp_${N})(registered_functions()[serial].eval_f))(${SEQ1});
89                 break;
90 END_OF_EVAL_SWITCH_STATEMENT
91
92 $evalf_switch_statement=generate(
93         <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
94         case ${N}:
95                 return ((evalf_funcp_${N})(registered_functions()[serial].evalf_f))(${SEQ1});
96 END_OF_EVALF_SWITCH_STATEMENT
97
98 $diff_switch_statement=generate(
99         <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
100         case ${N}:
101                 return ((derivative_funcp_${N})(registered_functions()[serial].derivative_f))(${SEQ1},diff_param);
102 END_OF_DIFF_SWITCH_STATEMENT
103
104 $series_switch_statement=generate(
105         <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
106         case ${N}:
107                 try {
108                         res = ((series_funcp_${N})(registered_functions()[serial].series_f))(${SEQ1},r,order,options);
109                 } catch (do_taylor) {
110                         res = basic::series(r, order, options);
111                 }
112                 return res;
113 END_OF_SERIES_SWITCH_STATEMENT
114
115 $eval_func_implementation=generate(
116         <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','');
117 function_options & function_options::eval_func(eval_funcp_${N} e)
118 {
119         test_and_set_nparams(${N});
120         eval_f = eval_funcp(e);
121         return *this;
122 }
123 END_OF_EVAL_FUNC_IMPLEMENTATION
124
125 $evalf_func_implementation=generate(
126         <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','');
127 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
128 {
129         test_and_set_nparams(${N});
130         evalf_f = evalf_funcp(ef);
131         return *this;
132 }
133 END_OF_EVALF_FUNC_IMPLEMENTATION
134
135 $derivative_func_implementation=generate(
136         <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','');
137 function_options & function_options::derivative_func(derivative_funcp_${N} d)
138 {
139         test_and_set_nparams(${N});
140         derivative_f = derivative_funcp(d);
141         return *this;
142 }
143 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
144
145 $series_func_implementation=generate(
146         <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','');
147 function_options & function_options::series_func(series_funcp_${N} s)
148 {
149         test_and_set_nparams(${N});
150         series_f = series_funcp(s);
151         return *this;
152 }
153 END_OF_SERIES_FUNC_IMPLEMENTATION
154
155 $interface=<<END_OF_INTERFACE;
156 /** \@file function.h
157  *
158  *  Interface to abstract class function (new function concept). */
159
160 /*
161  *  This file was generated automatically by function.pl.
162  *  Please do not modify it directly, edit the perl script instead!
163  *  function.pl options: \$maxargs=${maxargs}
164  *
165  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
166  *
167  *  This program is free software; you can redistribute it and/or modify
168  *  it under the terms of the GNU General Public License as published by
169  *  the Free Software Foundation; either version 2 of the License, or
170  *  (at your option) any later version.
171  *
172  *  This program is distributed in the hope that it will be useful,
173  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
174  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175  *  GNU General Public License for more details.
176  *
177  *  You should have received a copy of the GNU General Public License
178  *  along with this program; if not, write to the Free Software
179  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
180  */
181
182 #ifndef __GINAC_FUNCTION_H__
183 #define __GINAC_FUNCTION_H__
184
185 #include <string>
186 #include <vector>
187
188 // CINT needs <algorithm> to work properly with <vector>
189 #include <algorithm>
190
191 #include "exprseq.h"
192
193 // the following lines have been generated for max. ${maxargs} parameters
194 $declare_function_macro
195 // end of generated lines
196
197 #define REGISTER_FUNCTION(NAME,OPT) \\
198 const unsigned function_index_##NAME= \\
199         GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
200
201 #define BEGIN_TYPECHECK \\
202 bool automatic_typecheck=true;
203
204 #define TYPECHECK(VAR,TYPE) \\
205 if (!is_exactly_a<TYPE>(VAR)) { \\
206         automatic_typecheck=false; \\
207 } else
208
209 #define TYPECHECK_INTEGER(VAR) \\
210 if (!(VAR).info(GiNaC::info_flags::integer)) { \\
211         automatic_typecheck=false; \\
212 } else
213
214 #define END_TYPECHECK(RV) \\
215 {} \\
216 if (!automatic_typecheck) { \\
217         return RV.hold(); \\
218 }
219
220 namespace GiNaC {
221
222 class function;
223 class symmetry;
224
225 typedef ex (* eval_funcp)();
226 typedef ex (* evalf_funcp)();
227 typedef ex (* derivative_funcp)();
228 typedef ex (* series_funcp)();
229
230 // the following lines have been generated for max. ${maxargs} parameters
231 $typedef_eval_funcp
232 $typedef_evalf_funcp
233 $typedef_derivative_funcp
234 $typedef_series_funcp
235 // end of generated lines
236
237 class function_options
238 {
239         friend class function;
240 public:
241         function_options();
242         function_options(std::string const & n, std::string const & tn=std::string());
243         ~function_options();
244         void initialize(void);
245         function_options & set_name(std::string const & n, std::string const & tn=std::string());
246         function_options & latex_name(std::string const & tn);
247 // the following lines have been generated for max. ${maxargs} parameters
248 $eval_func_interface
249 $evalf_func_interface
250 $derivative_func_interface
251 $series_func_interface
252 // end of generated lines
253         function_options & set_return_type(unsigned rt, unsigned rtt=0);
254         function_options & do_not_evalf_params(void);
255         function_options & remember(unsigned size, unsigned assoc_size=0,
256                                     unsigned strategy=remember_strategies::delete_never);
257         function_options & overloaded(unsigned o);
258         function_options & set_symmetry(const symmetry & s);
259         void test_and_set_nparams(unsigned n);
260         std::string get_name(void) const { return name; }
261         unsigned get_nparams(void) const { return nparams; }
262         bool has_derivative(void) const { return derivative_f != NULL; }
263
264 protected:
265         std::string name;
266         std::string TeX_name;
267
268         unsigned nparams;
269
270         eval_funcp eval_f;
271         evalf_funcp evalf_f;
272         derivative_funcp derivative_f;
273         series_funcp series_f;
274
275         bool evalf_params_first;
276
277         bool use_return_type;
278         unsigned return_type;
279         unsigned return_type_tinfo;
280
281         bool use_remember;
282         unsigned remember_size;
283         unsigned remember_assoc_size;
284         unsigned remember_strategy;
285
286         unsigned functions_with_same_name;
287
288         ex symtree;
289 };
290
291 /** The class function is used to implement builtin functions like sin, cos...
292         and user defined functions */
293 class function : public exprseq
294 {
295         GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
296
297         // CINT has a linking problem
298 #ifndef __MAKECINT__
299         friend void ginsh_get_ginac_functions(void);
300 #endif // def __MAKECINT__
301
302         friend class remember_table_entry;
303         // friend class remember_table_list;
304         // friend class remember_table;
305         friend ex Derivative_eval(const ex &, const ex &);
306
307 // member functions
308
309         // other ctors
310 public:
311         function(unsigned ser);
312         // the following lines have been generated for max. ${maxargs} parameters
313 $constructors_interface
314         // end of generated lines
315         function(unsigned ser, const exprseq & es);
316         function(unsigned ser, const exvector & v, bool discardable = false);
317         function(unsigned ser, exvector * vp); // vp will be deleted
318
319         // functions overriding virtual functions from bases classes
320 public:
321         void print(const print_context & c, unsigned level = 0) const;
322         unsigned precedence(void) const {return 70;}
323         int degree(const ex & s) const;
324         int ldegree(const ex & s) const;
325         ex coeff(const ex & s, int n = 1) const;
326         ex expand(unsigned options=0) const;
327         ex eval(int level=0) const;
328         ex evalf(int level=0) const;
329         unsigned calchash(void) const;
330         ex series(const relational & r, int order, unsigned options = 0) const;
331         ex thisexprseq(const exvector & v) const;
332         ex thisexprseq(exvector * vp) const;
333 protected:
334         ex derivative(const symbol & s) const;
335         bool is_equal_same_type(const basic & other) const;
336         bool match_same_type(const basic & other) const;
337         unsigned return_type(void) const;
338         unsigned return_type_tinfo(void) const;
339         
340         // new virtual functions which can be overridden by derived classes
341         // none
342         
343         // non-virtual functions in this class
344 protected:
345         ex pderivative(unsigned diff_param) const; // partial differentiation
346         static std::vector<function_options> & registered_functions(void);
347         bool lookup_remember_table(ex & result) const;
348         void store_remember_table(ex const & result) const;
349 public:
350         static unsigned register_new(function_options const & opt);
351         static unsigned find_function(const std::string &name, unsigned nparams);
352         unsigned get_serial(void) const {return serial;}
353         std::string get_name(void) const;
354         
355 // member variables
356
357 protected:
358         unsigned serial;
359 };
360
361 // utility functions/macros
362 /** Return the object of type function handled by an ex.
363  *  This is unsafe: you need to check the type first. */
364 inline const function &ex_to_function(const ex &e)
365 {
366         return static_cast<const function &>(*e.bp);
367 }
368
369 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
370 template<> inline bool is_exactly_a<function>(const basic & obj)
371 {
372         return obj.tinfo()==TINFO_function;
373 }
374
375 #define is_ex_the_function(OBJ, FUNCNAME) \\
376         (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->get_serial() == function_index_##FUNCNAME)
377
378 } // namespace GiNaC
379
380 #endif // ndef __GINAC_FUNCTION_H__
381
382 END_OF_INTERFACE
383
384 $implementation=<<END_OF_IMPLEMENTATION;
385 /** \@file function.cpp
386  *
387  *  Implementation of class function. */
388
389 /*
390  *  This file was generated automatically by function.pl.
391  *  Please do not modify it directly, edit the perl script instead!
392  *  function.pl options: \$maxargs=${maxargs}
393  *
394  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
395  *
396  *  This program is free software; you can redistribute it and/or modify
397  *  it under the terms of the GNU General Public License as published by
398  *  the Free Software Foundation; either version 2 of the License, or
399  *  (at your option) any later version.
400  *
401  *  This program is distributed in the hope that it will be useful,
402  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
403  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
404  *  GNU General Public License for more details.
405  *
406  *  You should have received a copy of the GNU General Public License
407  *  along with this program; if not, write to the Free Software
408  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
409  */
410
411 #include <string>
412 #include <stdexcept>
413 #include <list>
414
415 #include "function.h"
416 #include "ex.h"
417 #include "lst.h"
418 #include "symmetry.h"
419 #include "print.h"
420 #include "archive.h"
421 #include "inifcns.h"
422 #include "utils.h"
423 #include "debugmsg.h"
424 #include "remember.h"
425
426 namespace GiNaC {
427
428 //////////
429 // helper class function_options
430 //////////
431
432 function_options::function_options()
433 {
434         initialize();
435 }
436
437 function_options::function_options(std::string const & n, std::string const & tn)
438 {
439         initialize();
440         set_name(n,tn);
441 }
442
443 function_options::~function_options()
444 {
445         // nothing to clean up at the moment
446 }
447
448 void function_options::initialize(void)
449 {
450         set_name("unnamed_function","\\\\mbox{unnamed}");
451         nparams = 0;
452         eval_f = evalf_f = derivative_f = series_f = 0;
453         evalf_params_first = true;
454         use_return_type = false;
455         use_remember = false;
456         functions_with_same_name = 1;
457         symtree = 0;
458 }
459
460 function_options & function_options::set_name(std::string const & n,
461                                               std::string const & tn)
462 {
463         name=n;
464         if (tn==std::string())
465                 TeX_name = "\\\\mbox{"+name+"}";
466         else
467                 TeX_name = tn;
468         return *this;
469 }
470
471 function_options & function_options::latex_name(std::string const & tn)
472 {
473         TeX_name=tn;
474         return *this;
475 }
476
477 // the following lines have been generated for max. ${maxargs} parameters
478 $eval_func_implementation
479 $evalf_func_implementation
480 $derivative_func_implementation
481 $series_func_implementation
482 // end of generated lines
483
484 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
485 {
486         use_return_type = true;
487         return_type = rt;
488         return_type_tinfo = rtt;
489         return *this;
490 }
491
492 function_options & function_options::do_not_evalf_params(void)
493 {
494         evalf_params_first = false;
495         return *this;
496 }
497
498 function_options & function_options::remember(unsigned size,
499                                               unsigned assoc_size,
500                                               unsigned strategy)
501 {
502         use_remember = true;
503         remember_size = size;
504         remember_assoc_size = assoc_size;
505         remember_strategy = strategy;
506         return *this;
507 }
508
509 function_options & function_options::overloaded(unsigned o)
510 {
511         functions_with_same_name = o;
512         return *this;
513 }
514
515 function_options & function_options::set_symmetry(const symmetry & s)
516 {
517         symtree = s;
518         return *this;
519 }
520         
521 void function_options::test_and_set_nparams(unsigned n)
522 {
523         if (nparams==0) {
524                 nparams = n;
525         } else if (nparams!=n) {
526                 // we do not throw an exception here because this code is
527                 // usually executed before main(), so the exception could not
528                 // caught anyhow
529                 std::cerr << "WARNING: number of parameters ("
530                           << n << ") differs from number set before (" 
531                           << nparams << ")" << std::endl;
532         }
533 }
534
535 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
536
537 //////////
538 // default ctor, dtor, copy ctor assignment operator and helpers
539 //////////
540
541 // public
542
543 function::function() : serial(0)
544 {
545         debugmsg("function default ctor",LOGLEVEL_CONSTRUCT);
546         tinfo_key = TINFO_function;
547 }
548
549 // protected
550
551 void function::copy(const function & other)
552 {
553         inherited::copy(other);
554         serial = other.serial;
555 }
556
557 void function::destroy(bool call_parent)
558 {
559         if (call_parent)
560                 inherited::destroy(call_parent);
561 }
562
563 //////////
564 // other ctors
565 //////////
566
567 // public
568
569 function::function(unsigned ser) : serial(ser)
570 {
571         debugmsg("function ctor from unsigned",LOGLEVEL_CONSTRUCT);
572         tinfo_key = TINFO_function;
573 }
574
575 // the following lines have been generated for max. ${maxargs} parameters
576 $constructors_implementation
577 // end of generated lines
578
579 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
580 {
581         debugmsg("function ctor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
582         tinfo_key = TINFO_function;
583 }
584
585 function::function(unsigned ser, const exvector & v, bool discardable) 
586   : exprseq(v,discardable), serial(ser)
587 {
588         debugmsg("function ctor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
589         tinfo_key = TINFO_function;
590 }
591
592 function::function(unsigned ser, exvector * vp) 
593   : exprseq(vp), serial(ser)
594 {
595         debugmsg("function ctor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
596         tinfo_key = TINFO_function;
597 }
598
599 //////////
600 // archiving
601 //////////
602
603 /** Construct object from archive_node. */
604 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
605 {
606         debugmsg("function ctor from archive_node", LOGLEVEL_CONSTRUCT);
607
608         // Find serial number by function name
609         std::string s;
610         if (n.find_string("name", s)) {
611                 unsigned int ser = 0;
612                 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
613                 while (i != iend) {
614                         if (s == i->name) {
615                                 serial = ser;
616                                 return;
617                         }
618                         ++i; ++ser;
619                 }
620                 throw (std::runtime_error("unknown function '" + s + "' in archive"));
621         } else
622                 throw (std::runtime_error("unnamed function in archive"));
623 }
624
625 /** Unarchive the object. */
626 ex function::unarchive(const archive_node &n, const lst &sym_lst)
627 {
628         return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
629 }
630
631 /** Archive the object. */
632 void function::archive(archive_node &n) const
633 {
634         inherited::archive(n);
635         GINAC_ASSERT(serial < registered_functions().size());
636         n.add_string("name", registered_functions()[serial].name);
637 }
638
639 //////////
640 // functions overriding virtual functions from bases classes
641 //////////
642
643 // public
644
645 void function::print(const print_context & c, unsigned level) const
646 {
647         debugmsg("function print", LOGLEVEL_PRINT);
648
649         GINAC_ASSERT(serial<registered_functions().size());
650
651         if (is_of_type(c, print_tree)) {
652
653                 c.s << std::string(level, ' ') << class_name() << " "
654                     << registered_functions()[serial].name
655                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
656                     << ", nops=" << nops()
657                     << std::endl;
658                 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
659                 for (unsigned i=0; i<seq.size(); ++i)
660                         seq[i].print(c, level + delta_indent);
661                 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
662
663         } else if (is_of_type(c, print_csrc)) {
664
665                 // Print function name in lowercase
666                 std::string lname = registered_functions()[serial].name;
667                 unsigned num = lname.size();
668                 for (unsigned i=0; i<num; i++)
669                         lname[i] = tolower(lname[i]);
670                 c.s << lname << "(";
671
672                 // Print arguments, separated by commas
673                 exvector::const_iterator it = seq.begin(), itend = seq.end();
674                 while (it != itend) {
675                         it->print(c);
676                         ++it;
677                         if (it != itend)
678                                 c.s << ",";
679                 }
680                 c.s << ")";
681
682         } else if (is_of_type(c, print_latex)) {
683                 c.s << registered_functions()[serial].TeX_name;
684                 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
685         } else {
686                 c.s << registered_functions()[serial].name;
687                 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
688         }
689 }
690
691 ex function::expand(unsigned options) const
692 {
693         // Only expand arguments when asked to do so
694         if (options & expand_options::expand_function_args)
695                 return inherited::expand(options);
696         else
697                 return (options == 0) ? setflag(status_flags::expanded) : *this;
698 }
699
700 int function::degree(const ex & s) const
701 {
702         return is_equal(*s.bp) ? 1 : 0;
703 }
704
705 int function::ldegree(const ex & s) const
706 {
707         return is_equal(*s.bp) ? 1 : 0;
708 }
709
710 ex function::coeff(const ex & s, int n) const
711 {
712         if (is_equal(*s.bp))
713                 return n==1 ? _ex1() : _ex0();
714         else
715                 return n==0 ? ex(*this) : _ex0();
716 }
717
718 ex function::eval(int level) const
719 {
720         GINAC_ASSERT(serial<registered_functions().size());
721
722         if (level>1) {
723                 // first evaluate children, then we will end up here again
724                 return function(serial,evalchildren(level));
725         }
726
727         const function_options &opt = registered_functions()[serial];
728
729         // Canonicalize argument order according to the symmetry properties
730         if (seq.size() > 1 && !(opt.symtree.is_zero())) {
731                 exvector v = seq;
732                 GINAC_ASSERT(is_ex_exactly_of_type(opt.symtree, symmetry));
733                 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
734                 if (sig != INT_MAX) {
735                         // Something has changed while sorting arguments, more evaluations later
736                         if (sig == 0)
737                                 return _ex0();
738                         return ex(sig) * thisexprseq(v);
739                 }
740         }
741
742         if (opt.eval_f==0) {
743                 return this->hold();
744         }
745
746         bool use_remember = opt.use_remember;
747         ex eval_result;
748         if (use_remember && lookup_remember_table(eval_result)) {
749                 return eval_result;
750         }
751
752         switch (opt.nparams) {
753                 // the following lines have been generated for max. ${maxargs} parameters
754 ${eval_switch_statement}
755                 // end of generated lines
756         default:
757                 throw(std::logic_error("function::eval(): invalid nparams"));
758         }
759         if (use_remember) {
760                 store_remember_table(eval_result);
761         }
762         return eval_result;
763 }
764
765 ex function::evalf(int level) const
766 {
767         GINAC_ASSERT(serial<registered_functions().size());
768
769         // Evaluate children first
770         exvector eseq;
771         if (level == 1)
772                 eseq = seq;
773         else if (level == -max_recursion_level)
774                 throw(std::runtime_error("max recursion level reached"));
775         else
776                 eseq.reserve(seq.size());
777         --level;
778         exvector::const_iterator it = seq.begin(), itend = seq.end();
779         while (it != itend) {
780                 eseq.push_back(it->evalf(level));
781                 ++it;
782         }
783         
784         if (registered_functions()[serial].evalf_f==0) {
785                 return function(serial,eseq).hold();
786         }
787         switch (registered_functions()[serial].nparams) {
788                 // the following lines have been generated for max. ${maxargs} parameters
789 ${evalf_switch_statement}
790                 // end of generated lines
791         }
792         throw(std::logic_error("function::evalf(): invalid nparams"));
793 }
794
795 unsigned function::calchash(void) const
796 {
797         unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
798         for (unsigned i=0; i<nops(); i++) {
799                 v = rotate_left_31(v);
800                 v ^= this->op(i).gethash();
801         }
802         v &= 0x7FFFFFFFU;
803         if (flags & status_flags::evaluated) {
804                 setflag(status_flags::hash_calculated);
805                 hashvalue = v;
806         }
807         return v;
808 }
809
810 ex function::thisexprseq(const exvector & v) const
811 {
812         return function(serial,v);
813 }
814
815 ex function::thisexprseq(exvector * vp) const
816 {
817         return function(serial,vp);
818 }
819
820 /** Implementation of ex::series for functions.
821  *  \@see ex::series */
822 ex function::series(const relational & r, int order, unsigned options) const
823 {
824         GINAC_ASSERT(serial<registered_functions().size());
825
826         if (registered_functions()[serial].series_f==0) {
827                 return basic::series(r, order);
828         }
829         ex res;
830         switch (registered_functions()[serial].nparams) {
831                 // the following lines have been generated for max. ${maxargs} parameters
832 ${series_switch_statement}
833                 // end of generated lines
834         }
835         throw(std::logic_error("function::series(): invalid nparams"));
836 }
837
838 // protected
839
840 /** Implementation of ex::diff() for functions. It applies the chain rule,
841  *  except for the Order term function.
842  *  \@see ex::diff */
843 ex function::derivative(const symbol & s) const
844 {
845         ex result;
846         
847         if (serial == function_index_Order) {
848                 // Order Term function only differentiates the argument
849                 return Order(seq[0].diff(s));
850         } else if (serial == function_index_Derivative) {
851                 // Inert derivative performs chain rule on the first argument only, and
852                 // adds differentiation parameter to list (second argument)
853                 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
854                 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
855                 ex fcn = seq[0];
856                 ex arg_diff;
857                 for (unsigned i=0; i!=fcn.nops(); i++) {
858                         arg_diff = fcn.op(i).diff(s);
859                         if (!arg_diff.is_zero()) {
860                                 lst new_lst = ex_to<lst>(seq[1]);
861                                 new_lst.append(i);
862                                 result += arg_diff * Derivative(fcn, new_lst);
863                         }
864                 }
865         } else {
866                 // Chain rule
867                 ex arg_diff;
868                 unsigned num = seq.size();
869                 for (unsigned i=0; i<num; i++) {
870                         arg_diff = seq[i].diff(s);
871                         // We apply the chain rule only when it makes sense.  This is not
872                         // just for performance reasons but also to allow functions to
873                         // throw when differentiated with respect to one of its arguments
874                         // without running into trouble with our automatic full
875                         // differentiation:
876                         if (!arg_diff.is_zero())
877                                 result += pderivative(i)*arg_diff;
878                 }
879         }
880         return result;
881 }
882
883 int function::compare_same_type(const basic & other) const
884 {
885         GINAC_ASSERT(is_of_type(other, function));
886         const function & o = static_cast<const function &>(other);
887
888         if (serial != o.serial)
889                 return serial < o.serial ? -1 : 1;
890         else
891                 return exprseq::compare_same_type(o);
892 }
893
894 bool function::is_equal_same_type(const basic & other) const
895 {
896         GINAC_ASSERT(is_of_type(other, function));
897         const function & o = static_cast<const function &>(other);
898
899         if (serial != o.serial)
900                 return false;
901         else
902                 return exprseq::is_equal_same_type(o);
903 }
904
905 bool function::match_same_type(const basic & other) const
906 {
907         GINAC_ASSERT(is_of_type(other, function));
908         const function & o = static_cast<const function &>(other);
909
910         return serial == o.serial;
911 }
912
913 unsigned function::return_type(void) const
914 {
915         if (seq.empty())
916                 return return_types::commutative;
917         else
918                 return seq.begin()->return_type();
919 }
920
921 unsigned function::return_type_tinfo(void) const
922 {
923         if (seq.empty())
924                 return tinfo_key;
925         else
926                 return seq.begin()->return_type_tinfo();
927 }
928
929 //////////
930 // new virtual functions which can be overridden by derived classes
931 //////////
932
933 // none
934
935 //////////
936 // non-virtual functions in this class
937 //////////
938
939 // protected
940
941 ex function::pderivative(unsigned diff_param) const // partial differentiation
942 {
943         GINAC_ASSERT(serial<registered_functions().size());
944         
945         if (registered_functions()[serial].derivative_f==0) {
946                 return Derivative(*this, lst(ex(diff_param)));
947         }
948         switch (registered_functions()[serial].nparams) {
949                 // the following lines have been generated for max. ${maxargs} parameters
950 ${diff_switch_statement}
951                 // end of generated lines
952         }
953         throw(std::logic_error("function::pderivative(): no diff function defined"));
954 }
955
956 std::vector<function_options> & function::registered_functions(void)
957 {
958         static std::vector<function_options> * rf = new std::vector<function_options>;
959         return *rf;
960 }
961
962 bool function::lookup_remember_table(ex & result) const
963 {
964         return remember_table::remember_tables()[serial].lookup_entry(*this,result);
965 }
966
967 void function::store_remember_table(ex const & result) const
968 {
969         remember_table::remember_tables()[serial].add_entry(*this,result);
970 }
971
972 // public
973
974 unsigned function::register_new(function_options const & opt)
975 {
976         unsigned same_name = 0;
977         for (unsigned i=0; i<registered_functions().size(); ++i) {
978                 if (registered_functions()[i].name==opt.name) {
979                         ++same_name;
980                 }
981         }
982         if (same_name>=opt.functions_with_same_name) {
983                 // we do not throw an exception here because this code is
984                 // usually executed before main(), so the exception could not
985                 // caught anyhow
986                 std::cerr << "WARNING: function name " << opt.name
987                                   << " already in use!" << std::endl;
988         }
989         registered_functions().push_back(opt);
990         if (opt.use_remember) {
991                 remember_table::remember_tables().
992                         push_back(remember_table(opt.remember_size,
993                                                                          opt.remember_assoc_size,
994                                                                          opt.remember_strategy));
995         } else {
996                 remember_table::remember_tables().push_back(remember_table());
997         }
998         return registered_functions().size()-1;
999 }
1000
1001 /** Find serial number of function by name and number of parameters.
1002  *  Throws exception if function was not found. */
1003 unsigned function::find_function(const std::string &name, unsigned nparams)
1004 {
1005         std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1006         unsigned serial = 0;
1007         while (i != end) {
1008                 if (i->get_name() == name && i->get_nparams() == nparams)
1009                         return serial;
1010                 ++i;
1011                 ++serial;
1012         }
1013         throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1014 }
1015
1016 /** Return the print name of the function. */
1017 std::string function::get_name(void) const
1018 {
1019         GINAC_ASSERT(serial<registered_functions().size());
1020         return registered_functions()[serial].name;
1021 }
1022
1023 } // namespace GiNaC
1024
1025 END_OF_IMPLEMENTATION
1026
1027 print "Creating interface file function.h...";
1028 open OUT,">function.h" or die "cannot open function.h";
1029 print OUT $interface;
1030 close OUT;
1031 print "ok.\n";
1032
1033 print "Creating implementation file function.cpp...";
1034 open OUT,">function.cpp" or die "cannot open function.cpp";
1035 print OUT $implementation;
1036 close OUT;
1037 print "ok.\n";
1038
1039 print "done.\n";