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