]> www.ginac.de Git - ginac.git/blob - ginac/function.pl
normal() applied to a power expression also normalizes the exponent
[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         // default constructor, destructor, copy constructor assignment operator and helpers
390 public:
391         function();
392         ~function();
393         function(const function & other);
394         const function & operator=(const function & other);
395 protected:
396         void copy(const function & other);
397         void destroy(bool call_parent);
398
399         // other constructors
400 public:
401         function(unsigned ser);
402         // the following lines have been generated for max. ${maxargs} parameters
403 $constructors_interface
404         // end of generated lines
405         function(unsigned ser, const exprseq & es);
406         function(unsigned ser, const exvector & v, bool discardable=0);
407         function(unsigned ser, exvector * vp); // vp will be deleted
408
409         // functions overriding virtual functions from bases classes
410 public:
411         basic * duplicate() const;
412         void printraw(std::ostream & os) const; 
413         void print(std::ostream & os, unsigned upper_precedence=0) const;
414         void printtree(std::ostream & os, unsigned indent) const;
415         void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence=0) const;
416         ex expand(unsigned options=0) const;
417         ex eval(int level=0) const;
418         ex evalf(int level=0) const;
419         ex series(const relational & r, int order, unsigned options = 0) const;
420         ex thisexprseq(const exvector & v) const;
421         ex thisexprseq(exvector * vp) const;
422 protected:
423         ex derivative(const symbol & s) const;
424         int compare_same_type(const basic & other) const;
425         bool is_equal_same_type(const basic & other) const;
426         unsigned return_type(void) const;
427         unsigned return_type_tinfo(void) const;
428         
429         // new virtual functions which can be overridden by derived classes
430         // none
431         
432         // non-virtual functions in this class
433 protected:
434         ex pderivative(unsigned diff_param) const; // partial differentiation
435         static std::vector<function_options> & registered_functions(void);
436         bool lookup_remember_table(ex & result) const;
437         void store_remember_table(ex const & result) const;
438 public:
439         static unsigned register_new(function_options const & opt);
440         static unsigned find_function(const std::string &name, unsigned nparams);
441         unsigned getserial(void) const {return serial;}
442         
443 // member variables
444
445 protected:
446         unsigned serial;
447 };
448
449 // utility functions/macros
450 inline const function &ex_to_function(const ex &e)
451 {
452         return static_cast<const function &>(*e.bp);
453 }
454
455 #ifndef NO_NAMESPACE_GINAC
456
457 #define is_ex_the_function(OBJ, FUNCNAME) \\
458         (is_ex_exactly_of_type(OBJ, function) && static_cast<GiNaC::function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
459
460 #else // ndef NO_NAMESPACE_GINAC
461
462 #define is_ex_the_function(OBJ, FUNCNAME) \\
463         (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
464
465 #endif // ndef NO_NAMESPACE_GINAC
466
467 // global constants
468
469 extern const function some_function;
470 extern const std::type_info & typeid_function;
471
472 #ifndef NO_NAMESPACE_GINAC
473 } // namespace GiNaC
474 #endif // ndef NO_NAMESPACE_GINAC
475
476 #endif // ndef __GINAC_FUNCTION_H__
477
478 END_OF_INTERFACE
479
480 $implementation=<<END_OF_IMPLEMENTATION;
481 /** \@file function.cpp
482  *
483  *  Implementation of class function. */
484
485 /*
486  *  This file was generated automatically by function.pl.
487  *  Please do not modify it directly, edit the perl script instead!
488  *  function.pl options: \$maxargs=${maxargs}
489  *
490  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
491  *
492  *  This program is free software; you can redistribute it and/or modify
493  *  it under the terms of the GNU General Public License as published by
494  *  the Free Software Foundation; either version 2 of the License, or
495  *  (at your option) any later version.
496  *
497  *  This program is distributed in the hope that it will be useful,
498  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
499  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
500  *  GNU General Public License for more details.
501  *
502  *  You should have received a copy of the GNU General Public License
503  *  along with this program; if not, write to the Free Software
504  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
505  */
506
507 #include <string>
508 #include <stdexcept>
509 #include <list>
510
511 #include "function.h"
512 #include "ex.h"
513 #include "lst.h"
514 #include "archive.h"
515 #include "inifcns.h"
516 #include "utils.h"
517 #include "debugmsg.h"
518 #include "remember.h"
519
520 #ifndef NO_NAMESPACE_GINAC
521 namespace GiNaC {
522 #endif // ndef NO_NAMESPACE_GINAC
523
524 //////////
525 // helper class function_options
526 //////////
527
528 function_options::function_options()
529 {
530         initialize();
531 }
532
533 function_options::function_options(std::string const & n, std::string const & tn)
534 {
535         initialize();
536         set_name(n,tn);
537 }
538
539 function_options::~function_options()
540 {
541         // nothing to clean up at the moment
542 }
543
544 void function_options::initialize(void)
545 {
546         set_name("unnamed_function","\\\\operatorname{unnamed}");
547         nparams=0;
548         eval_f=evalf_f=derivative_f=series_f=0;
549         evalf_params_first=true;
550         use_return_type=false;
551         use_remember=false;
552         functions_with_same_name=1;
553 }
554
555 function_options & function_options::set_name(std::string const & n,
556                                               std::string const & tn)
557 {
558         name=n;
559         if (tn==std::string()) {
560                 TeX_name="\\\\operatorname{"+name+"}";
561         } else {
562                 TeX_name=tn;
563         }
564         return *this;
565 }
566
567 // the following lines have been generated for max. ${maxargs} parameters
568 $eval_func_implementation
569 $evalf_func_implementation
570 $derivative_func_implementation
571 $series_func_implementation
572 // end of generated lines
573
574 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
575 {
576         use_return_type=true;
577         return_type=rt;
578         return_type_tinfo=rtt;
579         return *this;
580 }
581
582 function_options & function_options::do_not_evalf_params(void)
583 {
584         evalf_params_first=false;
585         return *this;
586 }
587
588 function_options & function_options::remember(unsigned size,
589                                               unsigned assoc_size,
590                                               unsigned strategy)
591 {
592         use_remember=true;
593         remember_size=size;
594         remember_assoc_size=assoc_size;
595         remember_strategy=strategy;
596         return *this;
597 }
598
599 function_options & function_options::overloaded(unsigned o)
600 {
601         functions_with_same_name=o;
602         return *this;
603 }
604         
605 void function_options::test_and_set_nparams(unsigned n)
606 {
607         if (nparams==0) {
608                 nparams=n;
609         } else if (nparams!=n) {
610                 // we do not throw an exception here because this code is
611                 // usually executed before main(), so the exception could not
612                 // caught anyhow
613                 std::cerr << "WARNING: number of parameters ("
614                           << n << ") differs from number set before (" 
615                           << nparams << ")" << std::endl;
616         }
617 }
618
619 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
620
621 //////////
622 // default constructor, destructor, copy constructor assignment operator and helpers
623 //////////
624
625 // public
626
627 function::function() : serial(0)
628 {
629         debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
630         tinfo_key = TINFO_function;
631 }
632
633 function::~function()
634 {
635         debugmsg("function destructor",LOGLEVEL_DESTRUCT);
636         destroy(false);
637 }
638
639 function::function(const function & other)
640 {
641         debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
642         copy(other);
643 }
644
645 const function & function::operator=(const function & other)
646 {
647         debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
648         if (this != &other) {
649                 destroy(true);
650                 copy(other);
651         }
652         return *this;
653 }
654
655 // protected
656
657 void function::copy(const function & other)
658 {
659         exprseq::copy(other);
660         serial=other.serial;
661 }
662
663 void function::destroy(bool call_parent)
664 {
665         if (call_parent) exprseq::destroy(call_parent);
666 }
667
668 //////////
669 // other constructors
670 //////////
671
672 // public
673
674 function::function(unsigned ser) : serial(ser)
675 {
676         debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
677         tinfo_key = TINFO_function;
678 }
679
680 // the following lines have been generated for max. ${maxargs} parameters
681 $constructors_implementation
682 // end of generated lines
683
684 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
685 {
686         debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
687         tinfo_key = TINFO_function;
688 }
689
690 function::function(unsigned ser, const exvector & v, bool discardable) 
691   : exprseq(v,discardable), serial(ser)
692 {
693         debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
694         tinfo_key = TINFO_function;
695 }
696
697 function::function(unsigned ser, exvector * vp) 
698   : exprseq(vp), serial(ser)
699 {
700         debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
701         tinfo_key = TINFO_function;
702 }
703
704 //////////
705 // archiving
706 //////////
707
708 /** Construct object from archive_node. */
709 function::function(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
710 {
711         debugmsg("function constructor from archive_node", LOGLEVEL_CONSTRUCT);
712
713         // Find serial number by function name
714         std::string s;
715         if (n.find_string("name", s)) {
716                 unsigned int ser = 0;
717                 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
718                 while (i != iend) {
719                         if (s == i->name) {
720                                 serial = ser;
721                                 return;
722                         }
723                         i++; ser++;
724                 }
725                 throw (std::runtime_error("unknown function '" + s + "' in archive"));
726         } else
727                 throw (std::runtime_error("unnamed function in archive"));
728 }
729
730 /** Unarchive the object. */
731 ex function::unarchive(const archive_node &n, const lst &sym_lst)
732 {
733         return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
734 }
735
736 /** Archive the object. */
737 void function::archive(archive_node &n) const
738 {
739         inherited::archive(n);
740         GINAC_ASSERT(serial < registered_functions().size());
741         n.add_string("name", registered_functions()[serial].name);
742 }
743
744 //////////
745 // functions overriding virtual functions from bases classes
746 //////////
747
748 // public
749
750 basic * function::duplicate() const
751 {
752         debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
753         return new function(*this);
754 }
755
756 void function::printraw(std::ostream & os) const
757 {
758         debugmsg("function printraw",LOGLEVEL_PRINT);
759
760         GINAC_ASSERT(serial<registered_functions().size());
761
762         os << "function(name=" << registered_functions()[serial].name;
763         for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
764                 os << ",";
765                 (*it).bp->print(os);
766         }
767         os << ")";
768 }
769
770 void function::print(std::ostream & os, unsigned upper_precedence) const
771 {
772         debugmsg("function print",LOGLEVEL_PRINT);
773
774         GINAC_ASSERT(serial<registered_functions().size());
775
776         os << registered_functions()[serial].name;
777         printseq(os,'(',',',')',exprseq::precedence,function::precedence);
778 }
779
780 void function::printtree(std::ostream & os, unsigned indent) const
781 {
782         debugmsg("function printtree",LOGLEVEL_PRINT);
783
784         GINAC_ASSERT(serial<registered_functions().size());
785
786         os << std::string(indent,' ') << "function "
787            << registered_functions()[serial].name
788            << ", hash=" << hashvalue 
789            << " (0x" << std::hex << hashvalue << std::dec << ")"
790            << ", flags=" << flags
791            << ", nops=" << nops() << std::endl;
792         for (unsigned i=0; i<nops(); ++i) {
793                 seq[i].printtree(os,indent+delta_indent);
794         }
795         os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
796 }
797
798 void function::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
799 {
800         debugmsg("function print csrc",LOGLEVEL_PRINT);
801
802         GINAC_ASSERT(serial<registered_functions().size());
803
804         // Print function name in lowercase
805         std::string lname;
806         lname=registered_functions()[serial].name;
807         for (unsigned i=0; i<lname.size(); i++)
808                 lname[i] = tolower(lname[i]);
809         os << lname << "(";
810
811         // Print arguments, separated by commas
812         exvector::const_iterator it = seq.begin();
813         exvector::const_iterator itend = seq.end();
814         while (it != itend) {
815                 it->bp->printcsrc(os, type, 0);
816                 it++;
817                 if (it != itend)
818                         os << ",";
819         }
820         os << ")";
821 }
822
823 ex function::expand(unsigned options) const
824 {
825         return this->setflag(status_flags::expanded);
826 }
827
828 ex function::eval(int level) const
829 {
830         GINAC_ASSERT(serial<registered_functions().size());
831
832         if (level>1) {
833                 // first evaluate children, then we will end up here again
834                 return function(serial,evalchildren(level));
835         }
836
837         if (registered_functions()[serial].eval_f==0) {
838                 return this->hold();
839         }
840
841         bool use_remember=registered_functions()[serial].use_remember;
842         ex eval_result;
843         if (use_remember && lookup_remember_table(eval_result)) {
844                 return eval_result;
845         }
846
847         switch (registered_functions()[serial].nparams) {
848                 // the following lines have been generated for max. ${maxargs} parameters
849 ${eval_switch_statement}
850                 // end of generated lines
851         default:
852                 throw(std::logic_error("function::eval(): invalid nparams"));
853         }
854         if (use_remember) {
855                 store_remember_table(eval_result);
856         }
857         return eval_result;
858 }
859
860 ex function::evalf(int level) const
861 {
862         GINAC_ASSERT(serial<registered_functions().size());
863
864         exvector eseq=evalfchildren(level);
865         
866         if (registered_functions()[serial].evalf_f==0) {
867                 return function(serial,eseq).hold();
868         }
869         switch (registered_functions()[serial].nparams) {
870                 // the following lines have been generated for max. ${maxargs} parameters
871 ${evalf_switch_statement}
872                 // end of generated lines
873         }
874         throw(std::logic_error("function::evalf(): invalid nparams"));
875 }
876
877 ex function::thisexprseq(const exvector & v) const
878 {
879         return function(serial,v);
880 }
881
882 ex function::thisexprseq(exvector * vp) const
883 {
884         return function(serial,vp);
885 }
886
887 /** Implementation of ex::series for functions.
888  *  \@see ex::series */
889 ex function::series(const relational & r, int order, unsigned options) const
890 {
891         GINAC_ASSERT(serial<registered_functions().size());
892
893         if (registered_functions()[serial].series_f==0) {
894                 return basic::series(r, order);
895         }
896         ex res;
897         switch (registered_functions()[serial].nparams) {
898                 // the following lines have been generated for max. ${maxargs} parameters
899 ${series_switch_statement}
900                 // end of generated lines
901         }
902         throw(std::logic_error("function::series(): invalid nparams"));
903 }
904
905 // protected
906
907
908 /** Implementation of ex::diff() for functions. It applies the chain rule,
909  *  except for the Order term function.
910  *  \@see ex::diff */
911 ex function::derivative(const symbol & s) const
912 {
913         ex result;
914         
915         if (serial == function_index_Order) {
916                 // Order Term function only differentiates the argument
917                 return Order(seq[0].diff(s));
918         } else if (serial == function_index_Derivative) {
919                 // Inert derivative performs chain rule on the first argument only, and
920                 // adds differentiation parameter to list (second argument)
921                 GINAC_ASSERT(is_ex_exactly_of_type(seq[0], function));
922                 GINAC_ASSERT(is_ex_exactly_of_type(seq[1], function));
923                 ex fcn = seq[0];
924                 ex arg_diff;
925                 for (unsigned i=0; i!=fcn.nops(); i++) {
926                         arg_diff = fcn.op(i).diff(s);
927                         if (!arg_diff.is_zero()) {
928                                 lst new_lst = ex_to_lst(seq[1]);
929                                 new_lst.append(i);
930                                 result += arg_diff * Derivative(fcn, new_lst);
931                         }
932                 }
933         } else {
934                 // Chain rule
935                 ex arg_diff;
936                 for (unsigned i=0; i!=seq.size(); i++) {
937                         arg_diff = seq[i].diff(s);
938                         // We apply the chain rule only when it makes sense.  This is not
939                         // just for performance reasons but also to allow functions to
940                         // throw when differentiated with respect to one of its arguments
941                         // without running into trouble with our automatic full
942                         // differentiation:
943                         if (!arg_diff.is_zero())
944                                 result += pderivative(i)*arg_diff;
945                 }
946         }
947         return result;
948 }
949
950 int function::compare_same_type(const basic & other) const
951 {
952         GINAC_ASSERT(is_of_type(other, function));
953         const function & o=static_cast<function &>(const_cast<basic &>(other));
954
955         if (serial!=o.serial) {
956                 return serial < o.serial ? -1 : 1;
957         }
958         return exprseq::compare_same_type(o);
959 }
960
961 bool function::is_equal_same_type(const basic & other) const
962 {
963         GINAC_ASSERT(is_of_type(other, function));
964         const function & o=static_cast<function &>(const_cast<basic &>(other));
965
966         if (serial!=o.serial) return false;
967         return exprseq::is_equal_same_type(o);
968 }
969
970 unsigned function::return_type(void) const
971 {
972         if (seq.size()==0) {
973                 return return_types::commutative;
974         }
975         return (*seq.begin()).return_type();
976 }
977    
978 unsigned function::return_type_tinfo(void) const
979 {
980         if (seq.size()==0) {
981                 return tinfo_key;
982         }
983         return (*seq.begin()).return_type_tinfo();
984 }
985
986 //////////
987 // new virtual functions which can be overridden by derived classes
988 //////////
989
990 // none
991
992 //////////
993 // non-virtual functions in this class
994 //////////
995
996 // protected
997
998 ex function::pderivative(unsigned diff_param) const // partial differentiation
999 {
1000         GINAC_ASSERT(serial<registered_functions().size());
1001         
1002         if (registered_functions()[serial].derivative_f==0) {
1003                 return Derivative(*this, lst(ex(diff_param)));
1004         }
1005         switch (registered_functions()[serial].nparams) {
1006                 // the following lines have been generated for max. ${maxargs} parameters
1007 ${diff_switch_statement}
1008                 // end of generated lines
1009         }        
1010         throw(std::logic_error("function::pderivative(): no diff function defined"));
1011 }
1012
1013 std::vector<function_options> & function::registered_functions(void)
1014 {
1015         static std::vector<function_options> * rf = new std::vector<function_options>;
1016         return *rf;
1017 }
1018
1019 bool function::lookup_remember_table(ex & result) const
1020 {
1021         return remember_table::remember_tables()[serial].lookup_entry(*this,result);
1022 }
1023
1024 void function::store_remember_table(ex const & result) const
1025 {
1026         remember_table::remember_tables()[serial].add_entry(*this,result);
1027 }
1028
1029 // public
1030
1031 unsigned function::register_new(function_options const & opt)
1032 {
1033         unsigned same_name=0;
1034         for (unsigned i=0; i<registered_functions().size(); ++i) {
1035                 if (registered_functions()[i].name==opt.name) {
1036                         same_name++;
1037                 }
1038         }
1039         if (same_name>=opt.functions_with_same_name) {
1040                 // we do not throw an exception here because this code is
1041                 // usually executed before main(), so the exception could not
1042                 // caught anyhow
1043                 std::cerr << "WARNING: function name " << opt.name
1044                                   << " already in use!" << std::endl;
1045         }
1046         registered_functions().push_back(opt);
1047         if (opt.use_remember) {
1048                 remember_table::remember_tables().
1049                         push_back(remember_table(opt.remember_size,
1050                                                                          opt.remember_assoc_size,
1051                                                                          opt.remember_strategy));
1052         } else {
1053                 remember_table::remember_tables().push_back(remember_table());
1054         }
1055         return registered_functions().size()-1;
1056 }
1057
1058 /** Find serial number of function by name and number of parameters.
1059  *  Throws exception if function was not found. */
1060 unsigned function::find_function(const std::string &name, unsigned nparams)
1061 {
1062         std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1063         unsigned serial = 0;
1064         while (i != end) {
1065                 if (i->get_name() == name && i->get_nparams() == nparams)
1066                         return serial;
1067                 i++;
1068                 serial++;
1069         }
1070         throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1071 }
1072
1073 //////////
1074 // static member variables
1075 //////////
1076
1077 // none
1078
1079 //////////
1080 // global constants
1081 //////////
1082
1083 const function some_function;
1084 const std::type_info & typeid_function=typeid(some_function);
1085
1086 #ifndef NO_NAMESPACE_GINAC
1087 } // namespace GiNaC
1088 #endif // ndef NO_NAMESPACE_GINAC
1089
1090 END_OF_IMPLEMENTATION
1091
1092 print "Creating interface file function.h...";
1093 open OUT,">function.h" or die "cannot open function.h";
1094 print OUT $interface;
1095 close OUT;
1096 print "ok.\n";
1097
1098 print "Creating implementation file function.cpp...";
1099 open OUT,">function.cpp" or die "cannot open function.cpp";
1100 print OUT $implementation;
1101 close OUT;
1102 print "ok.\n";
1103
1104 print "done.\n";