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