]> www.ginac.de Git - ginac.git/blob - ginac/function.pl
- ++version_major.
[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         function_options & latex_name(std::string const & tn);
268 // the following lines have been generated for max. ${maxargs} parameters
269 $eval_func_interface
270 $evalf_func_interface
271 $derivative_func_interface
272 $series_func_interface
273 // end of generated lines
274         function_options & set_return_type(unsigned rt, unsigned rtt=0);
275         function_options & do_not_evalf_params(void);
276         function_options & remember(unsigned size, unsigned assoc_size=0,
277                                     unsigned strategy=remember_strategies::delete_never);
278         function_options & overloaded(unsigned o);
279         void test_and_set_nparams(unsigned n);
280         std::string get_name(void) const { return name; }
281         unsigned get_nparams(void) const { return nparams; }
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         unsigned functions_with_same_name;
306 };
307
308 /** The class function is used to implement builtin functions like sin, cos...
309         and user defined functions */
310 class function : public exprseq
311 {
312         GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
313
314         // CINT has a linking problem
315 #ifndef __MAKECINT__
316         friend void ginsh_get_ginac_functions(void);
317 #endif // def __MAKECINT__
318
319         friend class remember_table_entry;
320         // friend class remember_table_list;
321         // friend class remember_table;
322
323 // member functions
324
325         // other ctors
326 public:
327         function(unsigned ser);
328         // the following lines have been generated for max. ${maxargs} parameters
329 $constructors_interface
330         // end of generated lines
331         function(unsigned ser, const exprseq & es);
332         function(unsigned ser, const exvector & v, bool discardable=0);
333         function(unsigned ser, exvector * vp); // vp will be deleted
334
335         // functions overriding virtual functions from bases classes
336 public:
337         void print(const print_context & c, unsigned level = 0) const;
338         unsigned precedence(void) const {return 70;}
339         int degree(const ex & s) const;
340         int ldegree(const ex & s) const;
341         ex coeff(const ex & s, int n = 1) const;
342         ex expand(unsigned options=0) const;
343         ex eval(int level=0) const;
344         ex evalf(int level=0) const;
345         unsigned calchash(void) const;
346         ex series(const relational & r, int order, unsigned options = 0) const;
347         bool match(const ex & pattern, lst & repl_lst) const;
348         ex thisexprseq(const exvector & v) const;
349         ex thisexprseq(exvector * vp) const;
350 protected:
351         ex derivative(const symbol & s) const;
352         bool is_equal_same_type(const basic & other) const;
353         unsigned return_type(void) const;
354         unsigned return_type_tinfo(void) const;
355         
356         // new virtual functions which can be overridden by derived classes
357         // none
358         
359         // non-virtual functions in this class
360 protected:
361         ex pderivative(unsigned diff_param) const; // partial differentiation
362         static std::vector<function_options> & registered_functions(void);
363         bool lookup_remember_table(ex & result) const;
364         void store_remember_table(ex const & result) const;
365 public:
366         static unsigned register_new(function_options const & opt);
367         static unsigned find_function(const std::string &name, unsigned nparams);
368         unsigned get_serial(void) const {return serial;}
369         std::string get_name(void) const;
370         
371 // member variables
372
373 protected:
374         unsigned serial;
375 };
376
377 // utility functions/macros
378 inline const function &ex_to_function(const ex &e)
379 {
380         return static_cast<const function &>(*e.bp);
381 }
382
383 #define is_ex_the_function(OBJ, FUNCNAME) \\
384         (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->get_serial() == function_index_##FUNCNAME)
385
386 } // namespace GiNaC
387
388 #endif // ndef __GINAC_FUNCTION_H__
389
390 END_OF_INTERFACE
391
392 $implementation=<<END_OF_IMPLEMENTATION;
393 /** \@file function.cpp
394  *
395  *  Implementation of class function. */
396
397 /*
398  *  This file was generated automatically by function.pl.
399  *  Please do not modify it directly, edit the perl script instead!
400  *  function.pl options: \$maxargs=${maxargs}
401  *
402  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
403  *
404  *  This program is free software; you can redistribute it and/or modify
405  *  it under the terms of the GNU General Public License as published by
406  *  the Free Software Foundation; either version 2 of the License, or
407  *  (at your option) any later version.
408  *
409  *  This program is distributed in the hope that it will be useful,
410  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
411  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
412  *  GNU General Public License for more details.
413  *
414  *  You should have received a copy of the GNU General Public License
415  *  along with this program; if not, write to the Free Software
416  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
417  */
418
419 #include <string>
420 #include <stdexcept>
421 #include <list>
422
423 #include "function.h"
424 #include "ex.h"
425 #include "lst.h"
426 #include "print.h"
427 #include "archive.h"
428 #include "inifcns.h"
429 #include "utils.h"
430 #include "debugmsg.h"
431 #include "remember.h"
432
433 namespace GiNaC {
434
435 //////////
436 // helper class function_options
437 //////////
438
439 function_options::function_options()
440 {
441         initialize();
442 }
443
444 function_options::function_options(std::string const & n, std::string const & tn)
445 {
446         initialize();
447         set_name(n,tn);
448 }
449
450 function_options::~function_options()
451 {
452         // nothing to clean up at the moment
453 }
454
455 void function_options::initialize(void)
456 {
457         set_name("unnamed_function","\\\\mbox{unnamed}");
458         nparams=0;
459         eval_f=evalf_f=derivative_f=series_f=0;
460         evalf_params_first=true;
461         use_return_type=false;
462         use_remember=false;
463         functions_with_same_name=1;
464 }
465
466 function_options & function_options::set_name(std::string const & n,
467                                               std::string const & tn)
468 {
469         name=n;
470         if (tn==std::string()) {
471                 TeX_name="\\\\mbox{"+name+"}";
472         } else {
473                 TeX_name=tn;
474         }
475         return *this;
476 }
477
478 function_options & function_options::latex_name(std::string const & tn)
479 {
480         TeX_name=tn;
481         return *this;
482 }
483
484 // the following lines have been generated for max. ${maxargs} parameters
485 $eval_func_implementation
486 $evalf_func_implementation
487 $derivative_func_implementation
488 $series_func_implementation
489 // end of generated lines
490
491 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
492 {
493         use_return_type=true;
494         return_type=rt;
495         return_type_tinfo=rtt;
496         return *this;
497 }
498
499 function_options & function_options::do_not_evalf_params(void)
500 {
501         evalf_params_first=false;
502         return *this;
503 }
504
505 function_options & function_options::remember(unsigned size,
506                                               unsigned assoc_size,
507                                               unsigned strategy)
508 {
509         use_remember=true;
510         remember_size=size;
511         remember_assoc_size=assoc_size;
512         remember_strategy=strategy;
513         return *this;
514 }
515
516 function_options & function_options::overloaded(unsigned o)
517 {
518         functions_with_same_name=o;
519         return *this;
520 }
521         
522 void function_options::test_and_set_nparams(unsigned n)
523 {
524         if (nparams==0) {
525                 nparams=n;
526         } else if (nparams!=n) {
527                 // we do not throw an exception here because this code is
528                 // usually executed before main(), so the exception could not
529                 // caught anyhow
530                 std::cerr << "WARNING: number of parameters ("
531                           << n << ") differs from number set before (" 
532                           << nparams << ")" << std::endl;
533         }
534 }
535
536 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
537
538 //////////
539 // default ctor, dtor, copy ctor assignment operator and helpers
540 //////////
541
542 // public
543
544 function::function() : serial(0)
545 {
546         debugmsg("function default ctor",LOGLEVEL_CONSTRUCT);
547         tinfo_key = TINFO_function;
548 }
549
550 // protected
551
552 void function::copy(const function & other)
553 {
554         exprseq::copy(other);
555         serial=other.serial;
556 }
557
558 void function::destroy(bool call_parent)
559 {
560         if (call_parent) exprseq::destroy(call_parent);
561 }
562
563 //////////
564 // other ctors
565 //////////
566
567 // public
568
569 function::function(unsigned ser) : serial(ser)
570 {
571         debugmsg("function ctor from unsigned",LOGLEVEL_CONSTRUCT);
572         tinfo_key = TINFO_function;
573 }
574
575 // the following lines have been generated for max. ${maxargs} parameters
576 $constructors_implementation
577 // end of generated lines
578
579 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
580 {
581         debugmsg("function ctor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
582         tinfo_key = TINFO_function;
583 }
584
585 function::function(unsigned ser, const exvector & v, bool discardable) 
586   : exprseq(v,discardable), serial(ser)
587 {
588         debugmsg("function ctor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
589         tinfo_key = TINFO_function;
590 }
591
592 function::function(unsigned ser, exvector * vp) 
593   : exprseq(vp), serial(ser)
594 {
595         debugmsg("function ctor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
596         tinfo_key = TINFO_function;
597 }
598
599 //////////
600 // archiving
601 //////////
602
603 /** Construct object from archive_node. */
604 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
605 {
606         debugmsg("function ctor from archive_node", LOGLEVEL_CONSTRUCT);
607
608         // Find serial number by function name
609         std::string s;
610         if (n.find_string("name", s)) {
611                 unsigned int ser = 0;
612                 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
613                 while (i != iend) {
614                         if (s == i->name) {
615                                 serial = ser;
616                                 return;
617                         }
618                         i++; ser++;
619                 }
620                 throw (std::runtime_error("unknown function '" + s + "' in archive"));
621         } else
622                 throw (std::runtime_error("unnamed function in archive"));
623 }
624
625 /** Unarchive the object. */
626 ex function::unarchive(const archive_node &n, const lst &sym_lst)
627 {
628         return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
629 }
630
631 /** Archive the object. */
632 void function::archive(archive_node &n) const
633 {
634         inherited::archive(n);
635         GINAC_ASSERT(serial < registered_functions().size());
636         n.add_string("name", registered_functions()[serial].name);
637 }
638
639 //////////
640 // functions overriding virtual functions from bases classes
641 //////////
642
643 // public
644
645 void function::print(const print_context & c, unsigned level) const
646 {
647         debugmsg("function print", LOGLEVEL_PRINT);
648
649         GINAC_ASSERT(serial<registered_functions().size());
650
651         if (is_of_type(c, print_tree)) {
652
653                 c.s << std::string(level, ' ') << class_name() << " "
654                     << registered_functions()[serial].name
655                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
656                     << ", nops=" << nops()
657                     << std::endl;
658                 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
659                 for (unsigned i=0; i<nops(); ++i)
660                         seq[i].print(c, level + delta_indent);
661                 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
662
663         } else if (is_of_type(c, print_csrc)) {
664
665                 // Print function name in lowercase
666                 std::string lname = registered_functions()[serial].name;
667                 for (unsigned i=0; i<lname.size(); i++)
668                         lname[i] = tolower(lname[i]);
669                 c.s << lname << "(";
670
671                 // Print arguments, separated by commas
672                 exvector::const_iterator it = seq.begin(), itend = seq.end();
673                 while (it != itend) {
674                         it->print(c);
675                         it++;
676                         if (it != itend)
677                                 c.s << ",";
678                 }
679                 c.s << ")";
680
681         } else if (is_of_type(c, print_latex)) {
682                 c.s << registered_functions()[serial].TeX_name;
683                 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
684         } else {
685                 c.s << registered_functions()[serial].name;
686                 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
687         }
688 }
689
690 ex function::expand(unsigned options) const
691 {
692         return this->setflag(status_flags::expanded);
693 }
694
695 int function::degree(const ex & s) const
696 {
697         return is_equal(*s.bp) ? 1 : 0;
698 }
699
700 int function::ldegree(const ex & s) const
701 {
702         return is_equal(*s.bp) ? 1 : 0;
703 }
704
705 ex function::coeff(const ex & s, int n) const
706 {
707         if (is_equal(*s.bp))
708                 return n==1 ? _ex1() : _ex0();
709         else
710                 return n==0 ? ex(*this) : _ex0();
711 }
712
713 ex function::eval(int level) const
714 {
715         GINAC_ASSERT(serial<registered_functions().size());
716
717         if (level>1) {
718                 // first evaluate children, then we will end up here again
719                 return function(serial,evalchildren(level));
720         }
721
722         if (registered_functions()[serial].eval_f==0) {
723                 return this->hold();
724         }
725
726         bool use_remember=registered_functions()[serial].use_remember;
727         ex eval_result;
728         if (use_remember && lookup_remember_table(eval_result)) {
729                 return eval_result;
730         }
731
732         switch (registered_functions()[serial].nparams) {
733                 // the following lines have been generated for max. ${maxargs} parameters
734 ${eval_switch_statement}
735                 // end of generated lines
736         default:
737                 throw(std::logic_error("function::eval(): invalid nparams"));
738         }
739         if (use_remember) {
740                 store_remember_table(eval_result);
741         }
742         return eval_result;
743 }
744
745 ex function::evalf(int level) const
746 {
747         GINAC_ASSERT(serial<registered_functions().size());
748
749         exvector eseq=evalfchildren(level);
750         
751         if (registered_functions()[serial].evalf_f==0) {
752                 return function(serial,eseq).hold();
753         }
754         switch (registered_functions()[serial].nparams) {
755                 // the following lines have been generated for max. ${maxargs} parameters
756 ${evalf_switch_statement}
757                 // end of generated lines
758         }
759         throw(std::logic_error("function::evalf(): invalid nparams"));
760 }
761
762 unsigned function::calchash(void) const
763 {
764         unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
765         for (unsigned i=0; i<nops(); i++) {
766                 v = rotate_left_31(v);
767                 v ^= this->op(i).gethash();
768         }
769         v &= 0x7FFFFFFFU;
770         if (flags & status_flags::evaluated) {
771                 setflag(status_flags::hash_calculated);
772                 hashvalue = v;
773         }
774         return v;
775 }
776
777 ex function::thisexprseq(const exvector & v) const
778 {
779         return function(serial,v);
780 }
781
782 ex function::thisexprseq(exvector * vp) const
783 {
784         return function(serial,vp);
785 }
786
787 /** Implementation of ex::series for functions.
788  *  \@see ex::series */
789 ex function::series(const relational & r, int order, unsigned options) const
790 {
791         GINAC_ASSERT(serial<registered_functions().size());
792
793         if (registered_functions()[serial].series_f==0) {
794                 return basic::series(r, order);
795         }
796         ex res;
797         switch (registered_functions()[serial].nparams) {
798                 // the following lines have been generated for max. ${maxargs} parameters
799 ${series_switch_statement}
800                 // end of generated lines
801         }
802         throw(std::logic_error("function::series(): invalid nparams"));
803 }
804
805 bool function::match(const ex & pattern, lst & repl_lst) const
806 {
807         // Serial number must match
808         if (is_ex_of_type(pattern, function) && serial != ex_to_function(pattern).serial)
809                 return false;
810         return inherited::match(pattern, repl_lst);
811 }
812
813 // protected
814
815
816 /** Implementation of ex::diff() for functions. It applies the chain rule,
817  *  except for the Order term function.
818  *  \@see ex::diff */
819 ex function::derivative(const symbol & s) const
820 {
821         ex result;
822         
823         if (serial == function_index_Order) {
824                 // Order Term function only differentiates the argument
825                 return Order(seq[0].diff(s));
826         } else if (serial == function_index_Derivative) {
827                 // Inert derivative performs chain rule on the first argument only, and
828                 // adds differentiation parameter to list (second argument)
829                 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
830                 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
831                 ex fcn = seq[0];
832                 ex arg_diff;
833                 for (unsigned i=0; i!=fcn.nops(); i++) {
834                         arg_diff = fcn.op(i).diff(s);
835                         if (!arg_diff.is_zero()) {
836                                 lst new_lst = ex_to_lst(seq[1]);
837                                 new_lst.append(i);
838                                 result += arg_diff * Derivative(fcn, new_lst);
839                         }
840                 }
841         } else {
842                 // Chain rule
843                 ex arg_diff;
844                 for (unsigned i=0; i!=seq.size(); i++) {
845                         arg_diff = seq[i].diff(s);
846                         // We apply the chain rule only when it makes sense.  This is not
847                         // just for performance reasons but also to allow functions to
848                         // throw when differentiated with respect to one of its arguments
849                         // without running into trouble with our automatic full
850                         // differentiation:
851                         if (!arg_diff.is_zero())
852                                 result += pderivative(i)*arg_diff;
853                 }
854         }
855         return result;
856 }
857
858 int function::compare_same_type(const basic & other) const
859 {
860         GINAC_ASSERT(is_of_type(other, function));
861         const function & o=static_cast<function &>(const_cast<basic &>(other));
862
863         if (serial!=o.serial) {
864                 return serial < o.serial ? -1 : 1;
865         }
866         return exprseq::compare_same_type(o);
867 }
868
869 bool function::is_equal_same_type(const basic & other) const
870 {
871         GINAC_ASSERT(is_of_type(other, function));
872         const function & o=static_cast<function &>(const_cast<basic &>(other));
873
874         if (serial!=o.serial) return false;
875         return exprseq::is_equal_same_type(o);
876 }
877
878 unsigned function::return_type(void) const
879 {
880         if (seq.size()==0) {
881                 return return_types::commutative;
882         }
883         return (*seq.begin()).return_type();
884 }
885    
886 unsigned function::return_type_tinfo(void) const
887 {
888         if (seq.size()==0) {
889                 return tinfo_key;
890         }
891         return (*seq.begin()).return_type_tinfo();
892 }
893
894 //////////
895 // new virtual functions which can be overridden by derived classes
896 //////////
897
898 // none
899
900 //////////
901 // non-virtual functions in this class
902 //////////
903
904 // protected
905
906 ex function::pderivative(unsigned diff_param) const // partial differentiation
907 {
908         GINAC_ASSERT(serial<registered_functions().size());
909         
910         if (registered_functions()[serial].derivative_f==0) {
911                 return Derivative(*this, lst(ex(diff_param)));
912         }
913         switch (registered_functions()[serial].nparams) {
914                 // the following lines have been generated for max. ${maxargs} parameters
915 ${diff_switch_statement}
916                 // end of generated lines
917         }        
918         throw(std::logic_error("function::pderivative(): no diff function defined"));
919 }
920
921 std::vector<function_options> & function::registered_functions(void)
922 {
923         static std::vector<function_options> * rf = new std::vector<function_options>;
924         return *rf;
925 }
926
927 bool function::lookup_remember_table(ex & result) const
928 {
929         return remember_table::remember_tables()[serial].lookup_entry(*this,result);
930 }
931
932 void function::store_remember_table(ex const & result) const
933 {
934         remember_table::remember_tables()[serial].add_entry(*this,result);
935 }
936
937 // public
938
939 unsigned function::register_new(function_options const & opt)
940 {
941         unsigned same_name=0;
942         for (unsigned i=0; i<registered_functions().size(); ++i) {
943                 if (registered_functions()[i].name==opt.name) {
944                         same_name++;
945                 }
946         }
947         if (same_name>=opt.functions_with_same_name) {
948                 // we do not throw an exception here because this code is
949                 // usually executed before main(), so the exception could not
950                 // caught anyhow
951                 std::cerr << "WARNING: function name " << opt.name
952                                   << " already in use!" << std::endl;
953         }
954         registered_functions().push_back(opt);
955         if (opt.use_remember) {
956                 remember_table::remember_tables().
957                         push_back(remember_table(opt.remember_size,
958                                                                          opt.remember_assoc_size,
959                                                                          opt.remember_strategy));
960         } else {
961                 remember_table::remember_tables().push_back(remember_table());
962         }
963         return registered_functions().size()-1;
964 }
965
966 /** Find serial number of function by name and number of parameters.
967  *  Throws exception if function was not found. */
968 unsigned function::find_function(const std::string &name, unsigned nparams)
969 {
970         std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
971         unsigned serial = 0;
972         while (i != end) {
973                 if (i->get_name() == name && i->get_nparams() == nparams)
974                         return serial;
975                 i++;
976                 serial++;
977         }
978         throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
979 }
980
981 /** Return the print name of the function. */
982 std::string function::get_name(void) const
983 {
984         GINAC_ASSERT(serial<registered_functions().size());
985         return registered_functions()[serial].name;
986 }
987
988 } // namespace GiNaC
989
990 END_OF_IMPLEMENTATION
991
992 print "Creating interface file function.h...";
993 open OUT,">function.h" or die "cannot open function.h";
994 print OUT $interface;
995 close OUT;
996 print "ok.\n";
997
998 print "Creating implementation file function.cpp...";
999 open OUT,">function.cpp" or die "cannot open function.cpp";
1000 print OUT $implementation;
1001 close OUT;
1002 print "ok.\n";
1003
1004 print "done.\n";