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