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