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