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