]> www.ginac.de Git - ginac.git/blob - ginac/function.pl
- New tinfo mechanism
[ginac.git] / ginac / function.pl
1 #  This perl script automatically generates function.h and function.cpp
2
3 #  function.pl options: \$maxargs=${maxargs}
4
5 #  GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
6
7 #  This program is free software; you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2 of the License, or
10 #  (at your option) any later version.
11
12 #  This program is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write to the Free Software
19 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21 $maxargs=14;
22
23 sub generate_seq {
24         my ($seq_template,$n)=@_;
25         my ($res,$N);
26         
27         $res='';
28         for ($N=1; $N<=$n; $N++) {
29                 $res .= eval('"' . $seq_template . '"');
30                 if ($N!=$n) {
31                         $res .= ', ';
32                 }
33         }
34         return $res;
35 }
36
37 sub generate_from_to {
38         my ($template,$seq_template1,$seq_template2,$seq_template3,$from,$to)=@_;
39         my ($res,$N,$SEQ);
40
41         $res='';
42         for ($N=$from; $N<=$to; $N++) {
43                 $SEQ1=generate_seq($seq_template1,$N);
44                 $SEQ2=generate_seq($seq_template2,$N);
45                 $SEQ3=generate_seq($seq_template3,$N);
46                 $res .= eval('"' . $template . '"');
47                 $SEQ1=''; # to avoid main::SEQ1 used only once warning
48                 $SEQ2=''; # same as above
49                 $SEQ3=''; # same as above
50         }
51         return $res;
52 }
53
54 sub generate {
55         my ($template,$seq_template1,$seq_template2,$seq_template3)=@_;
56         return generate_from_to($template,$seq_template1,$seq_template2,$seq_template3,1,$maxargs);
57 }
58
59 $declare_function_macro = generate(
60         <<'END_OF_DECLARE_FUNCTION_MACRO','typename T${N}','const T${N} & p${N}','GiNaC::ex(p${N})');
61 #define DECLARE_FUNCTION_${N}P(NAME) \\
62 class NAME##_SERIAL { public: static unsigned serial; }; \\
63 const unsigned NAME##_NPARAMS = ${N}; \\
64 template<${SEQ1}> const GiNaC::function NAME(${SEQ2}) { \\
65         return GiNaC::function(NAME##_SERIAL::serial, ${SEQ3}); \\
66 }
67
68 END_OF_DECLARE_FUNCTION_MACRO
69
70 $typedef_eval_funcp=generate(
71 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
72 'const ex &','','');
73
74 $typedef_evalf_funcp=generate(
75 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
76 'const ex &','','');
77
78 $typedef_conjugate_funcp=generate(
79 'typedef ex (* conjugate_funcp_${N})(${SEQ1});'."\n",
80 'const ex &','','');
81
82 $typedef_derivative_funcp=generate(
83 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
84 'const ex &','','');
85
86 $typedef_power_funcp=generate(
87 'typedef ex (* power_funcp_${N})(${SEQ1}, const ex &);'."\n",
88 'const ex &','','');
89
90 $typedef_series_funcp=generate(
91 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
92 'const ex &','','');
93
94 $typedef_print_funcp=generate(
95 'typedef void (* print_funcp_${N})(${SEQ1}, const print_context &);'."\n",
96 'const ex &','','');
97
98 $eval_func_interface=generate('    function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
99
100 $evalf_func_interface=generate('    function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
101
102 $conjugate_func_interface=generate('    function_options & conjugate_func(conjugate_funcp_${N} d);'."\n",'','','');
103
104 $derivative_func_interface=generate('    function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
105
106 $power_func_interface=generate('    function_options & power_func(power_funcp_${N} d);'."\n",'','','');
107
108 $series_func_interface=generate('    function_options & series_func(series_funcp_${N} s);'."\n",'','','');
109
110 $print_func_interface=generate(
111         <<'END_OF_PRINT_FUNC_INTERFACE','','','');
112     template <class Ctx> function_options & print_func(print_funcp_${N} p)
113     {
114         test_and_set_nparams(${N});
115         set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
116         return *this;
117     }
118 END_OF_PRINT_FUNC_INTERFACE
119
120 $constructors_interface=generate(
121 '    function(unsigned ser, ${SEQ1});'."\n",
122 'const ex & param${N}','','');
123
124 $constructors_implementation=generate(
125         <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
126 function::function(unsigned ser, ${SEQ1})
127         : exprseq(${SEQ2}), serial(ser)
128 {
129         tinfo_key = &function::tinfo_static;
130 }
131 END_OF_CONSTRUCTORS_IMPLEMENTATION
132
133 $eval_switch_statement=generate(
134         <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
135         case ${N}:
136                 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
137                 break;
138 END_OF_EVAL_SWITCH_STATEMENT
139
140 $evalf_switch_statement=generate(
141         <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
142         case ${N}:
143                 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
144 END_OF_EVALF_SWITCH_STATEMENT
145
146 $conjugate_switch_statement=generate(
147         <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
148         case ${N}:
149                 return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
150 END_OF_DIFF_SWITCH_STATEMENT
151
152 $diff_switch_statement=generate(
153         <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
154         case ${N}:
155                 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
156 END_OF_DIFF_SWITCH_STATEMENT
157
158 $power_switch_statement=generate(
159         <<'END_OF_POWER_SWITCH_STATEMENT','seq[${N}-1]','','');
160         case ${N}:
161                 return ((power_funcp_${N})(opt.power_f))(${SEQ1},power_param);
162 END_OF_POWER_SWITCH_STATEMENT
163
164 $series_switch_statement=generate(
165         <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
166         case ${N}:
167                 try {
168                         res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
169                 } catch (do_taylor) {
170                         res = basic::series(r, order, options);
171                 }
172                 return res;
173 END_OF_SERIES_SWITCH_STATEMENT
174
175 $print_switch_statement=generate(
176         <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
177                 case ${N}:
178                         ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
179                         break;
180 END_OF_PRINT_SWITCH_STATEMENT
181
182 $eval_func_implementation=generate(
183         <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
184 function_options & function_options::eval_func(eval_funcp_${N} e)
185 {
186         test_and_set_nparams(${N});
187         eval_f = eval_funcp(e);
188         return *this;
189 }
190 END_OF_EVAL_FUNC_IMPLEMENTATION
191
192 $evalf_func_implementation=generate(
193         <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
194 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
195 {
196         test_and_set_nparams(${N});
197         evalf_f = evalf_funcp(ef);
198         return *this;
199 }
200 END_OF_EVALF_FUNC_IMPLEMENTATION
201
202 $conjugate_func_implementation=generate(
203         <<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
204 function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
205 {
206         test_and_set_nparams(${N});
207         conjugate_f = conjugate_funcp(c);
208         return *this;
209 }
210 END_OF_CONJUGATE_FUNC_IMPLEMENTATION
211
212 $derivative_func_implementation=generate(
213         <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
214 function_options & function_options::derivative_func(derivative_funcp_${N} d)
215 {
216         test_and_set_nparams(${N});
217         derivative_f = derivative_funcp(d);
218         return *this;
219 }
220 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
221
222 $power_func_implementation=generate(
223         <<'END_OF_POWER_FUNC_IMPLEMENTATION','','','');
224 function_options & function_options::power_func(power_funcp_${N} d)
225 {
226         test_and_set_nparams(${N});
227         power_f = power_funcp(d);
228         return *this;
229 }
230 END_OF_POWER_FUNC_IMPLEMENTATION
231
232 $series_func_implementation=generate(
233         <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
234 function_options & function_options::series_func(series_funcp_${N} s)
235 {
236         test_and_set_nparams(${N});
237         series_f = series_funcp(s);
238         return *this;
239 }
240 END_OF_SERIES_FUNC_IMPLEMENTATION
241
242 $interface=<<END_OF_INTERFACE;
243 /** \@file function.h
244  *
245  *  Interface to class of symbolic functions. */
246
247 /*
248  *  This file was generated automatically by function.pl.
249  *  Please do not modify it directly, edit the perl script instead!
250  *  function.pl options: \$maxargs=${maxargs}
251  *
252  *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
253  *
254  *  This program is free software; you can redistribute it and/or modify
255  *  it under the terms of the GNU General Public License as published by
256  *  the Free Software Foundation; either version 2 of the License, or
257  *  (at your option) any later version.
258  *
259  *  This program is distributed in the hope that it will be useful,
260  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
261  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
262  *  GNU General Public License for more details.
263  *
264  *  You should have received a copy of the GNU General Public License
265  *  along with this program; if not, write to the Free Software
266  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
267  */
268
269 #ifndef __GINAC_FUNCTION_H__
270 #define __GINAC_FUNCTION_H__
271
272 #include <string>
273 #include <vector>
274
275 // CINT needs <algorithm> to work properly with <vector>
276 #include <algorithm>
277
278 #include "exprseq.h"
279
280 // the following lines have been generated for max. ${maxargs} parameters
281 $declare_function_macro
282 // end of generated lines
283
284 #define REGISTER_FUNCTION(NAME,OPT) \\
285 unsigned NAME##_SERIAL::serial = \\
286         GiNaC::function::register_new(GiNaC::function_options(#NAME, NAME##_NPARAMS).OPT);
287
288 namespace GiNaC {
289
290 class function;
291 class symmetry;
292
293 typedef ex (* eval_funcp)();
294 typedef ex (* evalf_funcp)();
295 typedef ex (* conjugate_funcp)();
296 typedef ex (* derivative_funcp)();
297 typedef ex (* power_funcp)();
298 typedef ex (* series_funcp)();
299 typedef void (* print_funcp)();
300
301 // the following lines have been generated for max. ${maxargs} parameters
302 $typedef_eval_funcp
303 $typedef_evalf_funcp
304 $typedef_conjugate_funcp
305 $typedef_derivative_funcp
306 $typedef_power_funcp
307 $typedef_series_funcp
308 $typedef_print_funcp
309 // end of generated lines
310
311 // Alternatively, an exvector may be passed into the static function, instead
312 // of individual ex objects.  Then, the number of arguments is not limited.
313 typedef ex (* eval_funcp_exvector)(const exvector &);
314 typedef ex (* evalf_funcp_exvector)(const exvector &);
315 typedef ex (* conjugate_funcp_exvector)(const exvector &);
316 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
317 typedef ex (* power_funcp_exvector)(const exvector &, const ex &);
318 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
319 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
320
321
322 class function_options
323 {
324         friend class function;
325         friend class fderivative;
326 public:
327         function_options();
328         function_options(std::string const & n, std::string const & tn=std::string());
329         function_options(std::string const & n, unsigned np);
330         ~function_options();
331         void initialize();
332
333         function_options & dummy() { return *this; }
334         function_options & set_name(std::string const & n, std::string const & tn=std::string());
335         function_options & latex_name(std::string const & tn);
336 // the following lines have been generated for max. ${maxargs} parameters
337 $eval_func_interface
338 $evalf_func_interface
339 $conjugate_func_interface
340 $derivative_func_interface
341 $power_func_interface
342 $series_func_interface
343 $print_func_interface
344 // end of generated lines
345         function_options & eval_func(eval_funcp_exvector e);
346         function_options & evalf_func(evalf_funcp_exvector ef);
347         function_options & conjugate_func(conjugate_funcp_exvector d);
348         function_options & derivative_func(derivative_funcp_exvector d);
349         function_options & power_func(power_funcp_exvector d);
350         function_options & series_func(series_funcp_exvector s);
351
352         template <class Ctx> function_options & print_func(print_funcp_exvector p)
353         {
354                 print_use_exvector_args = true;
355                 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
356                 return *this;
357         }
358
359         function_options & set_return_type(unsigned rt);
360         function_options & do_not_evalf_params();
361         function_options & remember(unsigned size, unsigned assoc_size=0,
362                                     unsigned strategy=remember_strategies::delete_never);
363         function_options & overloaded(unsigned o);
364         function_options & set_symmetry(const symmetry & s);
365
366         std::string get_name() const { return name; }
367         unsigned get_nparams() const { return nparams; }
368
369 protected:
370         bool has_derivative() const { return derivative_f != NULL; }
371         bool has_power() const { return power_f != NULL; }
372         void test_and_set_nparams(unsigned n);
373         void set_print_func(unsigned id, print_funcp f);
374
375         std::string name;
376         std::string TeX_name;
377
378         unsigned nparams;
379
380         eval_funcp eval_f;
381         evalf_funcp evalf_f;
382         conjugate_funcp conjugate_f;
383         derivative_funcp derivative_f;
384         power_funcp power_f;
385         series_funcp series_f;
386         std::vector<print_funcp> print_dispatch_table;
387
388         bool evalf_params_first;
389
390         bool use_return_type;
391         unsigned return_type;
392         const basic* return_type_tinfo;
393
394         bool use_remember;
395         unsigned remember_size;
396         unsigned remember_assoc_size;
397         unsigned remember_strategy;
398
399         bool eval_use_exvector_args;
400         bool evalf_use_exvector_args;
401         bool conjugate_use_exvector_args;
402         bool derivative_use_exvector_args;
403         bool power_use_exvector_args;
404         bool series_use_exvector_args;
405         bool print_use_exvector_args;
406
407         unsigned functions_with_same_name;
408
409         ex symtree;
410 };
411
412
413 /** Exception class thrown by classes which provide their own series expansion
414  *  to signal that ordinary Taylor expansion is safe. */
415 class do_taylor {};
416
417
418 /** The class function is used to implement builtin functions like sin, cos...
419         and user defined functions */
420 class function : public exprseq
421 {
422         GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
423
424         // CINT has a linking problem
425 #ifndef __MAKECINT__
426         friend void ginsh_get_ginac_functions();
427 #endif // def __MAKECINT__
428
429         friend class remember_table_entry;
430         // friend class remember_table_list;
431         // friend class remember_table;
432
433 // member functions
434
435         // other constructors
436 public:
437         function(unsigned ser);
438         // the following lines have been generated for max. ${maxargs} parameters
439 $constructors_interface
440         // end of generated lines
441         function(unsigned ser, const exprseq & es);
442         function(unsigned ser, const exvector & v, bool discardable = false);
443         function(unsigned ser, std::auto_ptr<exvector> vp);
444
445         // functions overriding virtual functions from base classes
446 public:
447         void print(const print_context & c, unsigned level = 0) const;
448         unsigned precedence() const {return 70;}
449         ex expand(unsigned options=0) const;
450         ex eval(int level=0) const;
451         ex evalf(int level=0) const;
452         unsigned calchash() const;
453         ex series(const relational & r, int order, unsigned options = 0) const;
454         ex thiscontainer(const exvector & v) const;
455         ex thiscontainer(std::auto_ptr<exvector> vp) const;
456         ex conjugate() const;
457 protected:
458         ex derivative(const symbol & s) const;
459         bool is_equal_same_type(const basic & other) const;
460         bool match_same_type(const basic & other) const;
461         unsigned return_type() const;
462         const basic* return_type_tinfo() const;
463         
464         // new virtual functions which can be overridden by derived classes
465         // none
466         
467         // non-virtual functions in this class
468 protected:
469         ex pderivative(unsigned diff_param) const; // partial differentiation
470         static std::vector<function_options> & registered_functions();
471         bool lookup_remember_table(ex & result) const;
472         void store_remember_table(ex const & result) const;
473 public:
474         ex power(const ex & exp) const;
475         static unsigned register_new(function_options const & opt);
476         static unsigned current_serial;
477         static unsigned find_function(const std::string &name, unsigned nparams);
478         unsigned get_serial() const {return serial;}
479         std::string get_name() const;
480
481 // member variables
482
483 protected:
484         unsigned serial;
485 };
486
487 // utility functions/macros
488
489 template <typename T>
490 inline bool is_the_function(const ex & x)
491 {
492         return is_exactly_a<function>(x)
493             && ex_to<function>(x).get_serial() == T::serial;
494 }
495
496 // Check whether OBJ is the specified symbolic function.
497 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
498
499 } // namespace GiNaC
500
501 #endif // ndef __GINAC_FUNCTION_H__
502
503 END_OF_INTERFACE
504
505 $implementation=<<END_OF_IMPLEMENTATION;
506 /** \@file function.cpp
507  *
508  *  Implementation of class of symbolic functions. */
509
510 /*
511  *  This file was generated automatically by function.pl.
512  *  Please do not modify it directly, edit the perl script instead!
513  *  function.pl options: \$maxargs=${maxargs}
514  *
515  *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
516  *
517  *  This program is free software; you can redistribute it and/or modify
518  *  it under the terms of the GNU General Public License as published by
519  *  the Free Software Foundation; either version 2 of the License, or
520  *  (at your option) any later version.
521  *
522  *  This program is distributed in the hope that it will be useful,
523  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
524  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
525  *  GNU General Public License for more details.
526  *
527  *  You should have received a copy of the GNU General Public License
528  *  along with this program; if not, write to the Free Software
529  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
530  */
531
532 #include <iostream>
533 #include <string>
534 #include <stdexcept>
535 #include <list>
536
537 #include "function.h"
538 #include "operators.h"
539 #include "fderivative.h"
540 #include "ex.h"
541 #include "lst.h"
542 #include "symmetry.h"
543 #include "print.h"
544 #include "power.h"
545 #include "archive.h"
546 #include "inifcns.h"
547 #include "tostring.h"
548 #include "utils.h"
549 #include "remember.h"
550
551 namespace GiNaC {
552
553 //////////
554 // helper class function_options
555 //////////
556
557 function_options::function_options()
558 {
559         initialize();
560 }
561
562 function_options::function_options(std::string const & n, std::string const & tn)
563 {
564         initialize();
565         set_name(n, tn);
566 }
567
568 function_options::function_options(std::string const & n, unsigned np)
569 {
570         initialize();
571         set_name(n, std::string());
572         nparams = np;
573 }
574
575 function_options::~function_options()
576 {
577         // nothing to clean up at the moment
578 }
579
580 void function_options::initialize()
581 {
582         set_name("unnamed_function", "\\\\mbox{unnamed}");
583         nparams = 0;
584         eval_f = evalf_f = conjugate_f = derivative_f = power_f = series_f = 0;
585         evalf_params_first = true;
586         use_return_type = false;
587         eval_use_exvector_args = false;
588         evalf_use_exvector_args = false;
589         conjugate_use_exvector_args = false;
590         derivative_use_exvector_args = false;
591         power_use_exvector_args = false;
592         series_use_exvector_args = false;
593         print_use_exvector_args = false;
594         use_remember = false;
595         functions_with_same_name = 1;
596         symtree = 0;
597 }
598
599 function_options & function_options::set_name(std::string const & n,
600                                               std::string const & tn)
601 {
602         name = n;
603         if (tn==std::string())
604                 TeX_name = "\\\\mbox{"+name+"}";
605         else
606                 TeX_name = tn;
607         return *this;
608 }
609
610 function_options & function_options::latex_name(std::string const & tn)
611 {
612         TeX_name = tn;
613         return *this;
614 }
615
616 // the following lines have been generated for max. ${maxargs} parameters
617 $eval_func_implementation
618 $evalf_func_implementation
619 $conjugate_func_implementation
620 $derivative_func_implementation
621 $power_func_implementation
622 $series_func_implementation
623 // end of generated lines
624
625 function_options& function_options::eval_func(eval_funcp_exvector e)
626 {
627         eval_use_exvector_args = true;
628         eval_f = eval_funcp(e);
629         return *this;
630 }
631 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
632 {
633         evalf_use_exvector_args = true;
634         evalf_f = evalf_funcp(ef);
635         return *this;
636 }
637 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
638 {
639         conjugate_use_exvector_args = true;
640         conjugate_f = conjugate_funcp(c);
641         return *this;
642 }
643 function_options& function_options::derivative_func(derivative_funcp_exvector d)
644 {
645         derivative_use_exvector_args = true;
646         derivative_f = derivative_funcp(d);
647         return *this;
648 }
649 function_options& function_options::power_func(power_funcp_exvector d)
650 {
651         power_use_exvector_args = true;
652         power_f = power_funcp(d);
653         return *this;
654 }
655 function_options& function_options::series_func(series_funcp_exvector s)
656 {
657         series_use_exvector_args = true;
658         series_f = series_funcp(s);
659         return *this;
660 }
661
662 function_options & function_options::set_return_type(unsigned rt)
663 {
664         use_return_type = true;
665         return_type = rt;
666         return *this;
667 }
668
669 function_options & function_options::do_not_evalf_params()
670 {
671         evalf_params_first = false;
672         return *this;
673 }
674
675 function_options & function_options::remember(unsigned size,
676                                               unsigned assoc_size,
677                                               unsigned strategy)
678 {
679         use_remember = true;
680         remember_size = size;
681         remember_assoc_size = assoc_size;
682         remember_strategy = strategy;
683         return *this;
684 }
685
686 function_options & function_options::overloaded(unsigned o)
687 {
688         functions_with_same_name = o;
689         return *this;
690 }
691
692 function_options & function_options::set_symmetry(const symmetry & s)
693 {
694         symtree = s;
695         return *this;
696 }
697         
698 void function_options::test_and_set_nparams(unsigned n)
699 {
700         if (nparams==0) {
701                 nparams = n;
702         } else if (nparams!=n) {
703                 // we do not throw an exception here because this code is
704                 // usually executed before main(), so the exception could not
705                 // be caught anyhow
706                 std::cerr << "WARNING: " << name << "(): number of parameters ("
707                           << n << ") differs from number set before (" 
708                           << nparams << ")" << std::endl;
709         }
710 }
711
712 void function_options::set_print_func(unsigned id, print_funcp f)
713 {
714         if (id >= print_dispatch_table.size())
715                 print_dispatch_table.resize(id + 1);
716         print_dispatch_table[id] = f;
717 }
718
719 /** This can be used as a hook for external applications. */
720 unsigned function::current_serial = 0;
721
722
723 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
724
725 //////////
726 // default constructor
727 //////////
728
729 // public
730
731 function::function() : serial(0)
732 {
733         tinfo_key = &function::tinfo_static;
734 }
735
736 //////////
737 // other constructors
738 //////////
739
740 // public
741
742 function::function(unsigned ser) : serial(ser)
743 {
744         tinfo_key = &function::tinfo_static;
745 }
746
747 // the following lines have been generated for max. ${maxargs} parameters
748 $constructors_implementation
749 // end of generated lines
750
751 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
752 {
753         tinfo_key = &function::tinfo_static;
754
755         // Force re-evaluation even if the exprseq was already evaluated
756         // (the exprseq copy constructor copies the flags)
757         clearflag(status_flags::evaluated);
758 }
759
760 function::function(unsigned ser, const exvector & v, bool discardable) 
761   : exprseq(v,discardable), serial(ser)
762 {
763         tinfo_key = &function::tinfo_static;
764 }
765
766 function::function(unsigned ser, std::auto_ptr<exvector> vp) 
767   : exprseq(vp), serial(ser)
768 {
769         tinfo_key = &function::tinfo_static;
770 }
771
772 //////////
773 // archiving
774 //////////
775
776 /** Construct object from archive_node. */
777 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
778 {
779         // Find serial number by function name
780         std::string s;
781         if (n.find_string("name", s)) {
782                 unsigned int ser = 0;
783                 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
784                 while (i != iend) {
785                         if (s == i->name) {
786                                 serial = ser;
787                                 return;
788                         }
789                         ++i; ++ser;
790                 }
791                 throw (std::runtime_error("unknown function '" + s + "' in archive"));
792         } else
793                 throw (std::runtime_error("unnamed function in archive"));
794 }
795
796 /** Unarchive the object. */
797 ex function::unarchive(const archive_node &n, lst &sym_lst)
798 {
799         return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
800 }
801
802 /** Archive the object. */
803 void function::archive(archive_node &n) const
804 {
805         inherited::archive(n);
806         GINAC_ASSERT(serial < registered_functions().size());
807         n.add_string("name", registered_functions()[serial].name);
808 }
809
810 //////////
811 // functions overriding virtual functions from base classes
812 //////////
813
814 // public
815
816 void function::print(const print_context & c, unsigned level) const
817 {
818         GINAC_ASSERT(serial<registered_functions().size());
819         const function_options &opt = registered_functions()[serial];
820         const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
821
822         // Dynamically dispatch on print_context type
823         const print_context_class_info *pc_info = &c.get_class_info();
824
825 next_context:
826         unsigned id = pc_info->options.get_id();
827         if (id >= pdt.size() || pdt[id] == NULL) {
828
829                 // Method not found, try parent print_context class
830                 const print_context_class_info *parent_pc_info = pc_info->get_parent();
831                 if (parent_pc_info) {
832                         pc_info = parent_pc_info;
833                         goto next_context;
834                 }
835
836                 // Method still not found, use default output
837                 if (is_a<print_tree>(c)) {
838
839                         c.s << std::string(level, ' ') << class_name() << " "
840                             << opt.name << " @" << this
841                             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
842                             << ", nops=" << nops()
843                             << std::endl;
844                         unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
845                         for (size_t i=0; i<seq.size(); ++i)
846                                 seq[i].print(c, level + delta_indent);
847                         c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
848
849                 } else if (is_a<print_csrc>(c)) {
850
851                         // Print function name in lowercase
852                         std::string lname = opt.name;
853                         size_t num = lname.size();
854                         for (size_t i=0; i<num; i++)
855                                 lname[i] = tolower(lname[i]);
856                         c.s << lname;
857                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
858
859                 } else if (is_a<print_latex>(c)) {
860                         c.s << opt.TeX_name;
861                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
862                 } else {
863                         c.s << opt.name;
864                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
865                 }
866
867         } else {
868
869                 // Method found, call it
870                 current_serial = serial;
871                 if (opt.print_use_exvector_args)
872                         ((print_funcp_exvector)pdt[id])(seq, c);
873                 else switch (opt.nparams) {
874                         // the following lines have been generated for max. ${maxargs} parameters
875 ${print_switch_statement}
876                         // end of generated lines
877                 default:
878                         throw(std::logic_error("function::print(): invalid nparams"));
879                 }
880         }
881 }
882
883 ex function::expand(unsigned options) const
884 {
885         // Only expand arguments when asked to do so
886         if (options & expand_options::expand_function_args)
887                 return inherited::expand(options);
888         else
889                 return (options == 0) ? setflag(status_flags::expanded) : *this;
890 }
891
892 ex function::eval(int level) const
893 {
894         if (level>1) {
895                 // first evaluate children, then we will end up here again
896                 return function(serial,evalchildren(level));
897         }
898
899         GINAC_ASSERT(serial<registered_functions().size());
900         const function_options &opt = registered_functions()[serial];
901
902         // Canonicalize argument order according to the symmetry properties
903         if (seq.size() > 1 && !(opt.symtree.is_zero())) {
904                 exvector v = seq;
905                 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
906                 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
907                 if (sig != INT_MAX) {
908                         // Something has changed while sorting arguments, more evaluations later
909                         if (sig == 0)
910                                 return _ex0;
911                         return ex(sig) * thiscontainer(v);
912                 }
913         }
914
915         if (opt.eval_f==0) {
916                 return this->hold();
917         }
918
919         bool use_remember = opt.use_remember;
920         ex eval_result;
921         if (use_remember && lookup_remember_table(eval_result)) {
922                 return eval_result;
923         }
924         current_serial = serial;
925         if (opt.eval_use_exvector_args)
926                 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
927         else
928         switch (opt.nparams) {
929                 // the following lines have been generated for max. ${maxargs} parameters
930 ${eval_switch_statement}
931                 // end of generated lines
932         default:
933                 throw(std::logic_error("function::eval(): invalid nparams"));
934         }
935         if (use_remember) {
936                 store_remember_table(eval_result);
937         }
938         return eval_result;
939 }
940
941 ex function::evalf(int level) const
942 {
943         GINAC_ASSERT(serial<registered_functions().size());
944         const function_options &opt = registered_functions()[serial];
945
946         // Evaluate children first
947         exvector eseq;
948         if (level == 1 || !(opt.evalf_params_first))
949                 eseq = seq;
950         else if (level == -max_recursion_level)
951                 throw(std::runtime_error("max recursion level reached"));
952         else {
953                 eseq.reserve(seq.size());
954                 --level;
955                 exvector::const_iterator it = seq.begin(), itend = seq.end();
956                 while (it != itend) {
957                         eseq.push_back(it->evalf(level));
958                         ++it;
959                 }
960         }
961
962         if (opt.evalf_f==0) {
963                 return function(serial,eseq).hold();
964         }
965         current_serial = serial;
966         if (opt.evalf_use_exvector_args)
967                 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
968         switch (opt.nparams) {
969                 // the following lines have been generated for max. ${maxargs} parameters
970 ${evalf_switch_statement}
971                 // end of generated lines
972         }
973         throw(std::logic_error("function::evalf(): invalid nparams"));
974 }
975
976 unsigned function::calchash() const
977 {
978         unsigned v = golden_ratio_hash(golden_ratio_hash((unsigned)tinfo()) ^ serial);
979         for (size_t i=0; i<nops(); i++) {
980                 v = rotate_left(v);
981                 v ^= this->op(i).gethash();
982         }
983
984         if (flags & status_flags::evaluated) {
985                 setflag(status_flags::hash_calculated);
986                 hashvalue = v;
987         }
988         return v;
989 }
990
991 ex function::thiscontainer(const exvector & v) const
992 {
993         return function(serial, v);
994 }
995
996 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
997 {
998         return function(serial, vp);
999 }
1000
1001 /** Implementation of ex::series for functions.
1002  *  \@see ex::series */
1003 ex function::series(const relational & r, int order, unsigned options) const
1004 {
1005         GINAC_ASSERT(serial<registered_functions().size());
1006         const function_options &opt = registered_functions()[serial];
1007
1008         if (opt.series_f==0) {
1009                 return basic::series(r, order);
1010         }
1011         ex res;
1012         current_serial = serial;
1013         if (opt.series_use_exvector_args) {
1014                 try {
1015                         res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
1016                 } catch (do_taylor) {
1017                         res = basic::series(r, order, options);
1018                 }
1019                 return res;
1020         }
1021         switch (opt.nparams) {
1022                 // the following lines have been generated for max. ${maxargs} parameters
1023 ${series_switch_statement}
1024                 // end of generated lines
1025         }
1026         throw(std::logic_error("function::series(): invalid nparams"));
1027 }
1028
1029 /** Implementation of ex::conjugate for functions. */
1030 ex function::conjugate() const
1031 {
1032         GINAC_ASSERT(serial<registered_functions().size());
1033         const function_options & opt = registered_functions()[serial];
1034
1035         if (opt.conjugate_f==0) {
1036                 return exprseq::conjugate();
1037         }
1038
1039         if (opt.conjugate_use_exvector_args) {
1040                 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
1041         }
1042
1043         switch (opt.nparams) {
1044                 // the following lines have been generated for max. ${maxargs} parameters
1045 ${conjugate_switch_statement}
1046                 // end of generated lines
1047         }
1048         throw(std::logic_error("function::conjugate(): invalid nparams"));
1049 }
1050
1051 // protected
1052
1053 /** Implementation of ex::diff() for functions. It applies the chain rule,
1054  *  except for the Order term function.
1055  *  \@see ex::diff */
1056 ex function::derivative(const symbol & s) const
1057 {
1058         ex result;
1059
1060         if (serial == Order_SERIAL::serial) {
1061                 // Order Term function only differentiates the argument
1062                 return Order(seq[0].diff(s));
1063         } else {
1064                 // Chain rule
1065                 ex arg_diff;
1066                 size_t num = seq.size();
1067                 for (size_t i=0; i<num; i++) {
1068                         arg_diff = seq[i].diff(s);
1069                         // We apply the chain rule only when it makes sense.  This is not
1070                         // just for performance reasons but also to allow functions to
1071                         // throw when differentiated with respect to one of its arguments
1072                         // without running into trouble with our automatic full
1073                         // differentiation:
1074                         if (!arg_diff.is_zero())
1075                                 result += pderivative(i)*arg_diff;
1076                 }
1077         }
1078         return result;
1079 }
1080
1081 int function::compare_same_type(const basic & other) const
1082 {
1083         GINAC_ASSERT(is_a<function>(other));
1084         const function & o = static_cast<const function &>(other);
1085
1086         if (serial != o.serial)
1087                 return serial < o.serial ? -1 : 1;
1088         else
1089                 return exprseq::compare_same_type(o);
1090 }
1091
1092 bool function::is_equal_same_type(const basic & other) const
1093 {
1094         GINAC_ASSERT(is_a<function>(other));
1095         const function & o = static_cast<const function &>(other);
1096
1097         if (serial != o.serial)
1098                 return false;
1099         else
1100                 return exprseq::is_equal_same_type(o);
1101 }
1102
1103 bool function::match_same_type(const basic & other) const
1104 {
1105         GINAC_ASSERT(is_a<function>(other));
1106         const function & o = static_cast<const function &>(other);
1107
1108         return serial == o.serial;
1109 }
1110
1111 unsigned function::return_type() const
1112 {
1113         GINAC_ASSERT(serial<registered_functions().size());
1114         const function_options &opt = registered_functions()[serial];
1115
1116         if (opt.use_return_type) {
1117                 // Return type was explicitly specified
1118                 return opt.return_type;
1119         } else {
1120                 // Default behavior is to use the return type of the first
1121                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1122                 if (seq.empty())
1123                         return return_types::commutative;
1124                 else
1125                         return seq.begin()->return_type();
1126         }
1127 }
1128
1129 const basic* function::return_type_tinfo() const
1130 {
1131         GINAC_ASSERT(serial<registered_functions().size());
1132         const function_options &opt = registered_functions()[serial];
1133
1134         if (opt.use_return_type) {
1135                 // Return type was explicitly specified
1136                 return this;
1137         } else {
1138                 // Default behavior is to use the return type of the first
1139                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1140                 if (seq.empty())
1141                         return this;
1142                 else
1143                         return seq.begin()->return_type_tinfo();
1144         }
1145 }
1146
1147 //////////
1148 // new virtual functions which can be overridden by derived classes
1149 //////////
1150
1151 // none
1152
1153 //////////
1154 // non-virtual functions in this class
1155 //////////
1156
1157 // protected
1158
1159 ex function::pderivative(unsigned diff_param) const // partial differentiation
1160 {
1161         GINAC_ASSERT(serial<registered_functions().size());
1162         const function_options &opt = registered_functions()[serial];
1163         
1164         // No derivative defined? Then return abstract derivative object
1165         if (opt.derivative_f == NULL)
1166                 return fderivative(serial, diff_param, seq);
1167
1168         current_serial = serial;
1169         if (opt.derivative_use_exvector_args)
1170                 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1171         switch (opt.nparams) {
1172                 // the following lines have been generated for max. ${maxargs} parameters
1173 ${diff_switch_statement}
1174                 // end of generated lines
1175         }
1176         throw(std::logic_error("function::pderivative(): no diff function defined"));
1177 }
1178
1179 ex function::power(const ex & power_param) const // power of function
1180 {
1181         GINAC_ASSERT(serial<registered_functions().size());
1182         const function_options &opt = registered_functions()[serial];
1183         
1184         // No derivative defined? Then return abstract derivative object
1185         if (opt.power_f == NULL)
1186                 return (new power::power(*this, power_param))->setflag(status_flags::dynallocated |
1187                                                        status_flags::evaluated);
1188
1189         current_serial = serial;
1190         if (opt.power_use_exvector_args)
1191                 return ((power_funcp_exvector)(opt.power_f))(seq,  power_param);
1192         switch (opt.nparams) {
1193                 // the following lines have been generated for max. ${maxargs} parameters
1194 ${power_switch_statement}
1195                 // end of generated lines
1196         }
1197         throw(std::logic_error("function::power(): no power function defined"));
1198 }
1199
1200 std::vector<function_options> & function::registered_functions()
1201 {
1202         static std::vector<function_options> * rf = new std::vector<function_options>;
1203         return *rf;
1204 }
1205
1206 bool function::lookup_remember_table(ex & result) const
1207 {
1208         return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1209 }
1210
1211 void function::store_remember_table(ex const & result) const
1212 {
1213         remember_table::remember_tables()[this->serial].add_entry(*this,result);
1214 }
1215
1216 // public
1217
1218 unsigned function::register_new(function_options const & opt)
1219 {
1220         size_t same_name = 0;
1221         for (size_t i=0; i<registered_functions().size(); ++i) {
1222                 if (registered_functions()[i].name==opt.name) {
1223                         ++same_name;
1224                 }
1225         }
1226         if (same_name>=opt.functions_with_same_name) {
1227                 // we do not throw an exception here because this code is
1228                 // usually executed before main(), so the exception could not
1229                 // caught anyhow
1230                 std::cerr << "WARNING: function name " << opt.name
1231                           << " already in use!" << std::endl;
1232         }
1233         registered_functions().push_back(opt);
1234         if (opt.use_remember) {
1235                 remember_table::remember_tables().
1236                         push_back(remember_table(opt.remember_size,
1237                                                  opt.remember_assoc_size,
1238                                                  opt.remember_strategy));
1239         } else {
1240                 remember_table::remember_tables().push_back(remember_table());
1241         }
1242         return registered_functions().size()-1;
1243 }
1244
1245 /** Find serial number of function by name and number of parameters.
1246  *  Throws exception if function was not found. */
1247 unsigned function::find_function(const std::string &name, unsigned nparams)
1248 {
1249         std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1250         unsigned serial = 0;
1251         while (i != end) {
1252                 if (i->get_name() == name && i->get_nparams() == nparams)
1253                         return serial;
1254                 ++i;
1255                 ++serial;
1256         }
1257         throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1258 }
1259
1260 /** Return the print name of the function. */
1261 std::string function::get_name() const
1262 {
1263         GINAC_ASSERT(serial<registered_functions().size());
1264         return registered_functions()[serial].name;
1265 }
1266
1267 } // namespace GiNaC
1268
1269 END_OF_IMPLEMENTATION
1270
1271 print "Creating interface file function.h...";
1272 open OUT,">function.h" or die "cannot open function.h";
1273 print OUT $interface;
1274 close OUT;
1275 print "ok.\n";
1276
1277 print "Creating implementation file function.cpp...";
1278 open OUT,">function.cpp" or die "cannot open function.cpp";
1279 print OUT $implementation;
1280 close OUT;
1281 print "ok.\n";
1282
1283 print "done.\n";