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