]> www.ginac.de Git - ginac.git/blob - ginac/function.cppy
Happy New Year!
[ginac.git] / ginac / function.cppy
1 /** @file function.cpp
2  *
3  *  Implementation of class of symbolic functions. */
4
5 /*
6  *  This file was generated automatically by function.py.
7  *  Please do not modify it directly, edit function.cppy instead!
8  *  function.py options: maxargs=@maxargs@
9  *
10  *  GiNaC Copyright (C) 1999-2016 Johannes Gutenberg University Mainz, Germany
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25  */
26
27 #include "function.h"
28 #include "operators.h"
29 #include "fderivative.h"
30 #include "ex.h"
31 #include "lst.h"
32 #include "symmetry.h"
33 #include "print.h"
34 #include "power.h"
35 #include "archive.h"
36 #include "inifcns.h"
37 #include "tostring.h"
38 #include "utils.h"
39 #include "hash_seed.h"
40 #include "remember.h"
41
42 #include <iostream>
43 #include <limits>
44 #include <list>
45 #include <stdexcept>
46 #include <string>
47
48 namespace GiNaC {
49
50 //////////
51 // helper class function_options
52 //////////
53
54 function_options::function_options()
55 {
56         initialize();
57 }
58
59 function_options::function_options(std::string const & n, std::string const & tn)
60 {
61         initialize();
62         set_name(n, tn);
63 }
64
65 function_options::function_options(std::string const & n, unsigned np)
66 {
67         initialize();
68         set_name(n, std::string());
69         nparams = np;
70 }
71
72 function_options::~function_options()
73 {
74         // nothing to clean up at the moment
75 }
76
77 void function_options::initialize()
78 {
79         set_name("unnamed_function", "\\\\mbox{unnamed}");
80         nparams = 0;
81         eval_f = evalf_f = real_part_f = imag_part_f = conjugate_f = expand_f
82                 = derivative_f = expl_derivative_f = power_f = series_f = 0;
83         info_f = 0;
84         evalf_params_first = true;
85         use_return_type = false;
86         eval_use_exvector_args = false;
87         evalf_use_exvector_args = false;
88         conjugate_use_exvector_args = false;
89         real_part_use_exvector_args = false;
90         imag_part_use_exvector_args = false;
91         expand_use_exvector_args = false;
92         derivative_use_exvector_args = false;
93         expl_derivative_use_exvector_args = false;
94         power_use_exvector_args = false;
95         series_use_exvector_args = false;
96         print_use_exvector_args = false;
97         info_use_exvector_args = false;
98         use_remember = false;
99         functions_with_same_name = 1;
100         symtree = 0;
101 }
102
103 function_options & function_options::set_name(std::string const & n,
104                                               std::string const & tn)
105 {
106         name = n;
107         if (tn==std::string())
108                 TeX_name = "\\\\mbox{"+name+"}";
109         else
110                 TeX_name = tn;
111         return *this;
112 }
113
114 function_options & function_options::latex_name(std::string const & tn)
115 {
116         TeX_name = tn;
117         return *this;
118 }
119
120 // the following lines have been generated for max. @maxargs@ parameters
121 +++ for method, N in [ (f, N) for f in methods[0:-1] for N in range(1, maxargs + 1)]:
122 function_options & function_options::@method@_func(@method@_funcp_@N@ e) 
123 {
124         test_and_set_nparams(@N@);
125         @method@_f = @method@_funcp(e);
126         return *this;
127 }
128 ---
129 // end of generated lines
130
131 +++ for method in methods[0:-1]:
132 function_options & function_options::@method@_func(@method@_funcp_exvector e)
133 {
134         @method@_use_exvector_args = true;
135         @method@_f = @method@_funcp(e);
136         return *this;
137 }
138 ---
139
140 // end of generated lines
141
142 function_options & function_options::set_return_type(unsigned rt, const return_type_t* rtt)
143 {
144         use_return_type = true;
145         return_type = rt;
146         if (rtt != 0)
147                 return_type_tinfo = *rtt;
148         else
149                 return_type_tinfo = make_return_type_t<function>();
150         return *this;
151 }
152
153 function_options & function_options::do_not_evalf_params()
154 {
155         evalf_params_first = false;
156         return *this;
157 }
158
159 function_options & function_options::remember(unsigned size,
160                                               unsigned assoc_size,
161                                               unsigned strategy)
162 {
163         use_remember = true;
164         remember_size = size;
165         remember_assoc_size = assoc_size;
166         remember_strategy = strategy;
167         return *this;
168 }
169
170 function_options & function_options::overloaded(unsigned o)
171 {
172         functions_with_same_name = o;
173         return *this;
174 }
175
176 function_options & function_options::set_symmetry(const symmetry & s)
177 {
178         symtree = s;
179         return *this;
180 }
181         
182 void function_options::test_and_set_nparams(unsigned n)
183 {
184         if (nparams==0) {
185                 nparams = n;
186         } else if (nparams!=n) {
187                 // we do not throw an exception here because this code is
188                 // usually executed before main(), so the exception could not
189                 // be caught anyhow
190                 std::cerr << "WARNING: " << name << "(): number of parameters ("
191                           << n << ") differs from number set before (" 
192                           << nparams << ")" << std::endl;
193         }
194 }
195
196 void function_options::set_print_func(unsigned id, print_funcp f)
197 {
198         if (id >= print_dispatch_table.size())
199                 print_dispatch_table.resize(id + 1);
200         print_dispatch_table[id] = f;
201 }
202
203 /** This can be used as a hook for external applications. */
204 unsigned function::current_serial = 0;
205
206
207 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
208
209 //////////
210 // default constructor
211 //////////
212
213 // public
214
215 function::function() : serial(0)
216 {
217 }
218
219 //////////
220 // other constructors
221 //////////
222
223 // public
224
225 function::function(unsigned ser) : serial(ser)
226 {
227 }
228
229 // the following lines have been generated for max. @maxargs@ parameters
230 +++ for N in range(1, maxargs + 1):
231 function::function(unsigned ser, @seq('const ex & param%(n)d', N)@)
232         : exprseq(@seq('param%(n)d', N)@), serial(ser)
233 {
234 }
235 ---
236
237 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
238 {
239
240         // Force re-evaluation even if the exprseq was already evaluated
241         // (the exprseq copy constructor copies the flags)
242         clearflag(status_flags::evaluated);
243 }
244
245 function::function(unsigned ser, const exvector & v, bool discardable) 
246   : exprseq(v,discardable), serial(ser)
247 {
248 }
249
250 function::function(unsigned ser, std::auto_ptr<exvector> vp) 
251   : exprseq(vp), serial(ser)
252 {
253 }
254
255 //////////
256 // archiving
257 //////////
258
259 /** Construct object from archive_node. */
260 void function::read_archive(const archive_node& n, lst& sym_lst)
261 {
262         inherited::read_archive(n, sym_lst);
263         // Find serial number by function name
264         std::string s;
265         if (n.find_string("name", s)) {
266                 unsigned int ser = 0;
267                 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
268                 while (i != iend) {
269                         if (s == i->name) {
270                                 serial = ser;
271                                 return;
272                         }
273                         ++i; ++ser;
274                 }
275                 throw (std::runtime_error("unknown function '" + s + "' in archive"));
276         } else
277                 throw (std::runtime_error("unnamed function in archive"));
278 }
279
280 /** Archive the object. */
281 void function::archive(archive_node &n) const
282 {
283         inherited::archive(n);
284         GINAC_ASSERT(serial < registered_functions().size());
285         n.add_string("name", registered_functions()[serial].name);
286 }
287
288 GINAC_BIND_UNARCHIVER(function);
289
290 //////////
291 // functions overriding virtual functions from base classes
292 //////////
293
294 // public
295
296 void function::print(const print_context & c, unsigned level) const
297 {
298         GINAC_ASSERT(serial<registered_functions().size());
299         const function_options &opt = registered_functions()[serial];
300         const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
301
302         // Dynamically dispatch on print_context type
303         const print_context_class_info *pc_info = &c.get_class_info();
304
305 next_context:
306         unsigned id = pc_info->options.get_id();
307         if (id >= pdt.size() || pdt[id] == NULL) {
308
309                 // Method not found, try parent print_context class
310                 const print_context_class_info *parent_pc_info = pc_info->get_parent();
311                 if (parent_pc_info) {
312                         pc_info = parent_pc_info;
313                         goto next_context;
314                 }
315
316                 // Method still not found, use default output
317                 if (is_a<print_tree>(c)) {
318
319                         c.s << std::string(level, ' ') << class_name() << " "
320                             << opt.name << " @" << this
321                             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
322                             << ", nops=" << nops()
323                             << std::endl;
324                         unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
325                         for (size_t i=0; i<seq.size(); ++i)
326                                 seq[i].print(c, level + delta_indent);
327                         c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
328
329                 } else if (is_a<print_csrc>(c)) {
330
331                         // Print function name in lowercase
332                         std::string lname = opt.name;
333                         size_t num = lname.size();
334                         for (size_t i=0; i<num; i++)
335                                 lname[i] = tolower(lname[i]);
336                         c.s << lname;
337                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
338
339                 } else if (is_a<print_latex>(c)) {
340                         c.s << opt.TeX_name;
341                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
342                 } else {
343                         c.s << opt.name;
344                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
345                 }
346
347         } else {
348
349                 // Method found, call it
350                 current_serial = serial;
351                 if (opt.print_use_exvector_args)
352                         ((print_funcp_exvector)pdt[id])(seq, c);
353                 else switch (opt.nparams) {
354                         // the following lines have been generated for max. @maxargs@ parameters
355 +++ for N in range(1, maxargs + 1):
356                         case @N@:
357                                 ((print_funcp_@N@)(pdt[id]))(@seq('seq[%(n)d]', N, 0)@, c);
358                                 break;
359 ---
360                         // end of generated lines
361                 default:
362                         throw(std::logic_error("function::print(): invalid nparams"));
363                 }
364         }
365 }
366
367 ex function::eval(int level) const
368 {
369         if (level>1) {
370                 // first evaluate children, then we will end up here again
371                 return function(serial,evalchildren(level));
372         }
373
374         GINAC_ASSERT(serial<registered_functions().size());
375         const function_options &opt = registered_functions()[serial];
376
377         // Canonicalize argument order according to the symmetry properties
378         if (seq.size() > 1 && !(opt.symtree.is_zero())) {
379                 exvector v = seq;
380                 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
381                 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
382                 if (sig != std::numeric_limits<int>::max()) {
383                         // Something has changed while sorting arguments, more evaluations later
384                         if (sig == 0)
385                                 return _ex0;
386                         return ex(sig) * thiscontainer(v);
387                 }
388         }
389
390         if (opt.eval_f==0) {
391                 return this->hold();
392         }
393
394         bool use_remember = opt.use_remember;
395         ex eval_result;
396         if (use_remember && lookup_remember_table(eval_result)) {
397                 return eval_result;
398         }
399         current_serial = serial;
400         if (opt.eval_use_exvector_args)
401                 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
402         else
403         switch (opt.nparams) {
404                 // the following lines have been generated for max. @maxargs@ parameters
405 +++ for N in range(1, maxargs + 1):
406                 case @N@:
407                         eval_result = ((eval_funcp_@N@)(opt.eval_f))(@seq('seq[%(n)d]', N, 0)@);
408                         break;
409 ---
410                 // end of generated lines
411         default:
412                 throw(std::logic_error("function::eval(): invalid nparams"));
413         }
414         if (use_remember) {
415                 store_remember_table(eval_result);
416         }
417         return eval_result;
418 }
419
420 ex function::evalf(int level) const
421 {
422         GINAC_ASSERT(serial<registered_functions().size());
423         const function_options &opt = registered_functions()[serial];
424
425         // Evaluate children first
426         exvector eseq;
427         if (level == 1 || !(opt.evalf_params_first))
428                 eseq = seq;
429         else if (level == -max_recursion_level)
430                 throw(std::runtime_error("max recursion level reached"));
431         else {
432                 eseq.reserve(seq.size());
433                 --level;
434                 exvector::const_iterator it = seq.begin(), itend = seq.end();
435                 while (it != itend) {
436                         eseq.push_back(it->evalf(level));
437                         ++it;
438                 }
439         }
440
441         if (opt.evalf_f==0) {
442                 return function(serial,eseq).hold();
443         }
444         current_serial = serial;
445         if (opt.evalf_use_exvector_args)
446                 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
447         switch (opt.nparams) {
448                 // the following lines have been generated for max. @maxargs@ parameters
449 +++ for N in range(1, maxargs + 1):
450                 case @N@:
451                         return ((evalf_funcp_@N@)(opt.evalf_f))(@seq('eseq[%(n)d]', N, 0)@);
452 ---
453                 // end of generated lines
454         }
455         throw(std::logic_error("function::evalf(): invalid nparams"));
456 }
457
458 /**
459  *  This method is defined to be in line with behaviour of function::return_type()
460  */
461 ex function::eval_ncmul(const exvector & v) const
462 {
463         // If this function is called then the list of arguments is non-empty
464         // and the first argument is non-commutative, see  function::return_type()
465         return seq.begin()->eval_ncmul(v);
466 }
467
468 unsigned function::calchash() const
469 {
470         unsigned v = golden_ratio_hash(make_hash_seed(typeid(*this)) ^ serial);
471         for (size_t i=0; i<nops(); i++) {
472                 v = rotate_left(v);
473                 v ^= this->op(i).gethash();
474         }
475
476         if (flags & status_flags::evaluated) {
477                 setflag(status_flags::hash_calculated);
478                 hashvalue = v;
479         }
480         return v;
481 }
482
483 ex function::thiscontainer(const exvector & v) const
484 {
485         return function(serial, v);
486 }
487
488 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
489 {
490         return function(serial, vp);
491 }
492
493 /** Implementation of ex::series for functions.
494  *  \@see ex::series */
495 ex function::series(const relational & r, int order, unsigned options) const
496 {
497         GINAC_ASSERT(serial<registered_functions().size());
498         const function_options &opt = registered_functions()[serial];
499
500         if (opt.series_f==0) {
501                 return basic::series(r, order);
502         }
503         ex res;
504         current_serial = serial;
505         if (opt.series_use_exvector_args) {
506                 try {
507                         res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
508                 } catch (do_taylor) {
509                         res = basic::series(r, order, options);
510                 }
511                 return res;
512         }
513         switch (opt.nparams) {
514                 // the following lines have been generated for max. @maxargs@ parameters
515 +++ for N in range(1, maxargs + 1):
516                 case @N@:
517                         try {
518                                 res = ((series_funcp_@N@)(opt.series_f))(@seq('seq[%(n)d]', N, 0)@, r, order, options);
519                         } catch (do_taylor) {
520                                 res = basic::series(r, order, options);
521                         }
522                         return res;
523 ---
524                 // end of generated lines
525         }
526         throw(std::logic_error("function::series(): invalid nparams"));
527 }
528
529 /** Implementation of ex::conjugate for functions. */
530 ex function::conjugate() const
531 {
532         GINAC_ASSERT(serial<registered_functions().size());
533         const function_options & opt = registered_functions()[serial];
534
535         if (opt.conjugate_f==0) {
536                 return conjugate_function(*this).hold();
537         }
538
539         if (opt.conjugate_use_exvector_args) {
540                 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
541         }
542
543         switch (opt.nparams) {
544                 // the following lines have been generated for max. @maxargs@ parameters
545 +++ for N in range(1, maxargs + 1):
546                 case @N@:
547                         return ((conjugate_funcp_@N@)(opt.conjugate_f))(@seq('seq[%(n)d]', N, 0)@);
548 ---
549                 // end of generated lines
550         }
551         throw(std::logic_error("function::conjugate(): invalid nparams"));
552 }
553
554 /** Implementation of ex::real_part for functions. */
555 ex function::real_part() const
556 {
557         GINAC_ASSERT(serial<registered_functions().size());
558         const function_options & opt = registered_functions()[serial];
559
560         if (opt.real_part_f==0)
561                 return basic::real_part();
562
563         if (opt.real_part_use_exvector_args)
564                 return ((real_part_funcp_exvector)(opt.real_part_f))(seq);
565
566         switch (opt.nparams) {
567                 // the following lines have been generated for max. @maxargs@ parameters
568 +++ for N in range(1, maxargs + 1):
569                 case @N@:
570                         return ((real_part_funcp_@N@)(opt.real_part_f))(@seq('seq[%(n)d]', N, 0)@);
571 ---
572                 // end of generated lines
573         }
574         throw(std::logic_error("function::real_part(): invalid nparams"));
575 }
576
577 /** Implementation of ex::imag_part for functions. */
578 ex function::imag_part() const
579 {
580         GINAC_ASSERT(serial<registered_functions().size());
581         const function_options & opt = registered_functions()[serial];
582
583         if (opt.imag_part_f==0)
584                 return basic::imag_part();
585
586         if (opt.imag_part_use_exvector_args)
587                 return ((imag_part_funcp_exvector)(opt.imag_part_f))(seq);
588
589         switch (opt.nparams) {
590                 // the following lines have been generated for max. @maxargs@ parameters
591 +++ for N in range(1, maxargs + 1):
592                 case @N@:
593                         return ((imag_part_funcp_@N@)(opt.imag_part_f))(@seq('seq[%(n)d]', N, 0)@);
594 ---
595                 // end of generated lines
596         }
597         throw(std::logic_error("function::imag_part(): invalid nparams"));
598 }
599
600 /** Implementation of ex::info for functions. */
601 bool function::info(unsigned inf) const
602 {
603         GINAC_ASSERT(serial<registered_functions().size());
604         const function_options & opt = registered_functions()[serial];
605
606         if (opt.info_f==0) {
607                 return basic::info(inf);
608         }
609
610         if (opt.info_use_exvector_args) {
611                 return ((info_funcp_exvector)(opt.info_f))(seq, inf);
612         }
613
614         switch (opt.nparams) {
615                 // the following lines have been generated for max. @maxargs@ parameters
616 +++ for N in range(1, maxargs + 1):
617                 case @N@:
618                         return ((info_funcp_@N@)(opt.info_f))(@seq('seq[%(n)d]', N, 0)@, inf);
619 ---
620                 // end of generated lines
621         }
622         throw(std::logic_error("function::info(): invalid nparams"));
623 }
624
625 // protected
626
627 /** Implementation of ex::diff() for functions. It applies the chain rule,
628  *  except for the Order term function.
629  *  \@see ex::diff */
630 ex function::derivative(const symbol & s) const
631 {
632         ex result;
633
634         try {
635                 // Explicit derivation
636                 result = expl_derivative(s);
637         } catch (...) {
638                 // Chain rule
639                 ex arg_diff;
640                 size_t num = seq.size();
641                 for (size_t i=0; i<num; i++) {
642                         arg_diff = seq[i].diff(s);
643                         // We apply the chain rule only when it makes sense.  This is not
644                         // just for performance reasons but also to allow functions to
645                         // throw when differentiated with respect to one of its arguments
646                         // without running into trouble with our automatic full
647                         // differentiation:
648                         if (!arg_diff.is_zero())
649                                 result += pderivative(i)*arg_diff;
650                 }
651         }
652         return result;
653 }
654
655 int function::compare_same_type(const basic & other) const
656 {
657         GINAC_ASSERT(is_a<function>(other));
658         const function & o = static_cast<const function &>(other);
659
660         if (serial != o.serial)
661                 return serial < o.serial ? -1 : 1;
662         else
663                 return exprseq::compare_same_type(o);
664 }
665
666 bool function::is_equal_same_type(const basic & other) const
667 {
668         GINAC_ASSERT(is_a<function>(other));
669         const function & o = static_cast<const function &>(other);
670
671         if (serial != o.serial)
672                 return false;
673         else
674                 return exprseq::is_equal_same_type(o);
675 }
676
677 bool function::match_same_type(const basic & other) const
678 {
679         GINAC_ASSERT(is_a<function>(other));
680         const function & o = static_cast<const function &>(other);
681
682         return serial == o.serial;
683 }
684
685 unsigned function::return_type() const
686 {
687         GINAC_ASSERT(serial<registered_functions().size());
688         const function_options &opt = registered_functions()[serial];
689
690         if (opt.use_return_type) {
691                 // Return type was explicitly specified
692                 return opt.return_type;
693         } else {
694                 // Default behavior is to use the return type of the first
695                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
696                 if (seq.empty())
697                         return return_types::commutative;
698                 else
699                         return seq.begin()->return_type();
700         }
701 }
702
703 return_type_t function::return_type_tinfo() const
704 {
705         GINAC_ASSERT(serial<registered_functions().size());
706         const function_options &opt = registered_functions()[serial];
707
708         if (opt.use_return_type) {
709                 // Return type was explicitly specified
710                 return opt.return_type_tinfo;
711         } else {
712                 // Default behavior is to use the return type of the first
713                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
714                 if (seq.empty())
715                         return make_return_type_t<function>();
716                 else
717                         return seq.begin()->return_type_tinfo();
718         }
719 }
720
721 //////////
722 // new virtual functions which can be overridden by derived classes
723 //////////
724
725 // none
726
727 //////////
728 // non-virtual functions in this class
729 //////////
730
731 // protected
732
733 ex function::pderivative(unsigned diff_param) const // partial differentiation
734 {
735         GINAC_ASSERT(serial<registered_functions().size());
736         const function_options &opt = registered_functions()[serial];
737         
738         // No derivative defined? Then return abstract derivative object
739         if (opt.derivative_f == NULL)
740                 return fderivative(serial, diff_param, seq);
741
742         current_serial = serial;
743         if (opt.derivative_use_exvector_args)
744                 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
745         switch (opt.nparams) {
746                 // the following lines have been generated for max. @maxargs@ parameters
747 +++ for N in range(1, maxargs + 1):
748                 case @N@:
749                         return ((derivative_funcp_@N@)(opt.derivative_f))(@seq('seq[%(n)d]', N, 0)@, diff_param);
750 ---
751                 // end of generated lines
752         }
753         throw(std::logic_error("function::pderivative(): no diff function defined"));
754 }
755
756 ex function::expl_derivative(const symbol & s) const // explicit differentiation
757 {
758         GINAC_ASSERT(serial<registered_functions().size());
759         const function_options &opt = registered_functions()[serial];
760
761         // No explicit derivative defined? Then this function shall not be called!
762         if (opt.expl_derivative_f == NULL)
763                 throw(std::logic_error("function::expl_derivative(): explicit derivation is called, but no such function defined"));
764
765         current_serial = serial;
766         if (opt.expl_derivative_use_exvector_args)
767                 return ((expl_derivative_funcp_exvector)(opt.expl_derivative_f))(seq, s);
768         switch (opt.nparams) {
769                 // the following lines have been generated for max. @maxargs@ parameters
770 +++ for N in range(1, maxargs + 1):
771                 case @N@:
772                         return ((expl_derivative_funcp_@N@)(opt.expl_derivative_f))(@seq('seq[%(n)d]', N, 0)@, s);
773 ---
774                 // end of generated lines
775         }
776 }
777
778 ex function::power(const ex & power_param) const // power of function
779 {
780         GINAC_ASSERT(serial<registered_functions().size());
781         const function_options &opt = registered_functions()[serial];
782         
783         if (opt.power_f == NULL)
784                 return (new GiNaC::power(*this, power_param))->setflag(status_flags::dynallocated |
785                                                                        status_flags::evaluated);
786
787         current_serial = serial;
788         if (opt.power_use_exvector_args)
789                 return ((power_funcp_exvector)(opt.power_f))(seq,  power_param);
790         switch (opt.nparams) {
791                 // the following lines have been generated for max. @maxargs@ parameters
792 +++ for N in range(1, maxargs + 1):
793                 case @N@:
794                         return ((power_funcp_@N@)(opt.power_f))(@seq('seq[%(n)d]', N, 0)@, power_param);
795 ---
796                 // end of generated lines
797         }
798         throw(std::logic_error("function::power(): no power function defined"));
799 }
800
801 ex function::expand(unsigned options) const
802 {
803         GINAC_ASSERT(serial<registered_functions().size());
804         const function_options &opt = registered_functions()[serial];
805
806         // No expand defined? Then return the same function with expanded arguments (if required)
807         if (opt.expand_f == NULL) {
808                 // Only expand arguments when asked to do so
809                 if (options & expand_options::expand_function_args)
810                         return inherited::expand(options);
811                 else
812                         return (options == 0) ? setflag(status_flags::expanded) : *this;
813         }
814
815         current_serial = serial;
816         if (opt.expand_use_exvector_args)
817                 return ((expand_funcp_exvector)(opt.expand_f))(seq,  options);
818         switch (opt.nparams) {
819                 // the following lines have been generated for max. @maxargs@ parameters
820 +++ for N in range(1, maxargs + 1):
821                 case @N@:
822                         return ((expand_funcp_@N@)(opt.expand_f))(@seq('seq[%(n)d]', N, 0)@, options);
823 ---
824                 // end of generated lines
825         }
826         throw(std::logic_error("function::expand(): no expand of function defined"));
827 }
828
829 std::vector<function_options> & function::registered_functions()
830 {
831         static std::vector<function_options> rf = std::vector<function_options>();
832         return rf;
833 }
834
835 bool function::lookup_remember_table(ex & result) const
836 {
837         return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
838 }
839
840 void function::store_remember_table(ex const & result) const
841 {
842         remember_table::remember_tables()[this->serial].add_entry(*this,result);
843 }
844
845 // public
846
847 unsigned function::register_new(function_options const & opt)
848 {
849         size_t same_name = 0;
850         for (size_t i=0; i<registered_functions().size(); ++i) {
851                 if (registered_functions()[i].name==opt.name) {
852                         ++same_name;
853                 }
854         }
855         if (same_name>=opt.functions_with_same_name) {
856                 // we do not throw an exception here because this code is
857                 // usually executed before main(), so the exception could not
858                 // caught anyhow
859                 std::cerr << "WARNING: function name " << opt.name
860                           << " already in use!" << std::endl;
861         }
862         registered_functions().push_back(opt);
863         if (opt.use_remember) {
864                 remember_table::remember_tables().
865                         push_back(remember_table(opt.remember_size,
866                                                  opt.remember_assoc_size,
867                                                  opt.remember_strategy));
868         } else {
869                 remember_table::remember_tables().push_back(remember_table());
870         }
871         return registered_functions().size()-1;
872 }
873
874 /** Find serial number of function by name and number of parameters.
875  *  Throws exception if function was not found. */
876 unsigned function::find_function(const std::string &name, unsigned nparams)
877 {
878         std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
879         unsigned serial = 0;
880         while (i != end) {
881                 if (i->get_name() == name && i->get_nparams() == nparams)
882                         return serial;
883                 ++i;
884                 ++serial;
885         }
886         throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
887 }
888
889 /** Return the print name of the function. */
890 std::string function::get_name() const
891 {
892         GINAC_ASSERT(serial<registered_functions().size());
893         return registered_functions()[serial].name;
894 }
895
896 } // namespace GiNaC
897