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