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