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