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