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