]> www.ginac.de Git - ginac.git/blob - ginac/function.cppy
cache pseries coeff accesses in pseries::mul_series.
[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-2015 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 "utils.h"
38 #include "hash_seed.h"
39 #include "remember.h"
40
41 #include <iostream>
42 #include <limits>
43 #include <list>
44 #include <stdexcept>
45 #include <string>
46
47 namespace GiNaC {
48
49 //////////
50 // helper class function_options
51 //////////
52
53 function_options::function_options()
54 {
55         initialize();
56 }
57
58 function_options::function_options(std::string const & n, std::string const & tn)
59 {
60         initialize();
61         set_name(n, tn);
62 }
63
64 function_options::function_options(std::string const & n, unsigned np)
65 {
66         initialize();
67         set_name(n, std::string());
68         nparams = np;
69 }
70
71 function_options::~function_options()
72 {
73         // nothing to clean up at the moment
74 }
75
76 void function_options::initialize()
77 {
78         set_name("unnamed_function", "\\\\mbox{unnamed}");
79         nparams = 0;
80         eval_f = evalf_f = real_part_f = imag_part_f = conjugate_f = expand_f
81                 = derivative_f = expl_derivative_f = power_f = series_f = nullptr;
82         info_f = 0;
83         evalf_params_first = true;
84         use_return_type = false;
85         eval_use_exvector_args = false;
86         evalf_use_exvector_args = false;
87         conjugate_use_exvector_args = false;
88         real_part_use_exvector_args = false;
89         imag_part_use_exvector_args = false;
90         expand_use_exvector_args = false;
91         derivative_use_exvector_args = false;
92         expl_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 != nullptr)
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)
245   : exprseq(v), serial(ser)
246 {
247 }
248
249 function::function(unsigned ser, exvector && v)
250   : exprseq(std::move(v)), 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                 for (auto & it : registered_functions()) {
267                         if (s == it.name) {
268                                 serial = ser;
269                                 return;
270                         }
271                         ++ser;
272                 }
273                 throw (std::runtime_error("unknown function '" + s + "' in archive"));
274         } else
275                 throw (std::runtime_error("unnamed function in archive"));
276 }
277
278 /** Archive the object. */
279 void function::archive(archive_node &n) const
280 {
281         inherited::archive(n);
282         GINAC_ASSERT(serial < registered_functions().size());
283         n.add_string("name", registered_functions()[serial].name);
284 }
285
286 GINAC_BIND_UNARCHIVER(function);
287
288 //////////
289 // functions overriding virtual functions from base classes
290 //////////
291
292 // public
293
294 void function::print(const print_context & c, unsigned level) const
295 {
296         GINAC_ASSERT(serial<registered_functions().size());
297         const function_options &opt = registered_functions()[serial];
298         const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
299
300         // Dynamically dispatch on print_context type
301         const print_context_class_info *pc_info = &c.get_class_info();
302
303 next_context:
304         unsigned id = pc_info->options.get_id();
305         if (id >= pdt.size() || pdt[id] == nullptr) {
306
307                 // Method not found, try parent print_context class
308                 const print_context_class_info *parent_pc_info = pc_info->get_parent();
309                 if (parent_pc_info) {
310                         pc_info = parent_pc_info;
311                         goto next_context;
312                 }
313
314                 // Method still not found, use default output
315                 if (is_a<print_tree>(c)) {
316
317                         c.s << std::string(level, ' ') << class_name() << " "
318                             << opt.name << " @" << this
319                             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
320                             << ", nops=" << nops()
321                             << std::endl;
322                         unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
323                         for (size_t i=0; i<seq.size(); ++i)
324                                 seq[i].print(c, level + delta_indent);
325                         c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
326
327                 } else if (is_a<print_csrc>(c)) {
328
329                         // Print function name in lowercase
330                         std::string lname = opt.name;
331                         size_t num = lname.size();
332                         for (size_t i=0; i<num; i++)
333                                 lname[i] = tolower(lname[i]);
334                         c.s << lname;
335                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
336
337                 } else if (is_a<print_latex>(c)) {
338                         c.s << opt.TeX_name;
339                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
340                 } else {
341                         c.s << opt.name;
342                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
343                 }
344
345         } else {
346
347                 // Method found, call it
348                 current_serial = serial;
349                 if (opt.print_use_exvector_args)
350                         ((print_funcp_exvector)pdt[id])(seq, c);
351                 else switch (opt.nparams) {
352                         // the following lines have been generated for max. @maxargs@ parameters
353 +++ for N in range(1, maxargs + 1):
354                         case @N@:
355                                 ((print_funcp_@N@)(pdt[id]))(@seq('seq[%(n)d]', N, 0)@, c);
356                                 break;
357 ---
358                         // end of generated lines
359                 default:
360                         throw(std::logic_error("function::print(): invalid nparams"));
361                 }
362         }
363 }
364
365 ex function::eval() const
366 {
367         if (flags & status_flags::evaluated) {
368                 return *this;
369         }
370
371         GINAC_ASSERT(serial<registered_functions().size());
372         const function_options &opt = registered_functions()[serial];
373
374         // Canonicalize argument order according to the symmetry properties
375         if (seq.size() > 1 && !(opt.symtree.is_zero())) {
376                 exvector v = seq;
377                 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
378                 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
379                 if (sig != std::numeric_limits<int>::max()) {
380                         // Something has changed while sorting arguments, more evaluations later
381                         if (sig == 0)
382                                 return _ex0;
383                         return ex(sig) * thiscontainer(std::move(v));
384                 }
385         }
386
387         if (opt.eval_f==nullptr) {
388                 return this->hold();
389         }
390
391         bool use_remember = opt.use_remember;
392         ex eval_result;
393         if (use_remember && lookup_remember_table(eval_result)) {
394                 return eval_result;
395         }
396         current_serial = serial;
397         if (opt.eval_use_exvector_args)
398                 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
399         else
400         switch (opt.nparams) {
401                 // the following lines have been generated for max. @maxargs@ parameters
402 +++ for N in range(1, maxargs + 1):
403                 case @N@:
404                         eval_result = ((eval_funcp_@N@)(opt.eval_f))(@seq('seq[%(n)d]', N, 0)@);
405                         break;
406 ---
407                 // end of generated lines
408         default:
409                 throw(std::logic_error("function::eval(): invalid nparams"));
410         }
411         if (use_remember) {
412                 store_remember_table(eval_result);
413         }
414         return eval_result;
415 }
416
417 ex function::evalf(int level) const
418 {
419         GINAC_ASSERT(serial<registered_functions().size());
420         const function_options &opt = registered_functions()[serial];
421
422         // Evaluate children first
423         exvector eseq;
424         if (level == 1 || !(opt.evalf_params_first))
425                 eseq = seq;
426         else if (level == -max_recursion_level)
427                 throw(std::runtime_error("max recursion level reached"));
428         else {
429                 eseq.reserve(seq.size());
430                 --level;
431                 for (auto & it : seq) {
432                         eseq.push_back(it.evalf(level));
433                 }
434         }
435
436         if (opt.evalf_f==0) {
437                 return function(serial,eseq).hold();
438         }
439         current_serial = serial;
440         if (opt.evalf_use_exvector_args)
441                 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
442         switch (opt.nparams) {
443                 // the following lines have been generated for max. @maxargs@ parameters
444 +++ for N in range(1, maxargs + 1):
445                 case @N@:
446                         return ((evalf_funcp_@N@)(opt.evalf_f))(@seq('eseq[%(n)d]', N, 0)@);
447 ---
448                 // end of generated lines
449         }
450         throw(std::logic_error("function::evalf(): invalid nparams"));
451 }
452
453 /**
454  *  This method is defined to be in line with behaviour of function::return_type()
455  */
456 ex function::eval_ncmul(const exvector & v) const
457 {
458         // If this function is called then the list of arguments is non-empty
459         // and the first argument is non-commutative, see  function::return_type()
460         return seq.begin()->eval_ncmul(v);
461 }
462
463 unsigned function::calchash() const
464 {
465         unsigned v = golden_ratio_hash(make_hash_seed(typeid(*this)) ^ serial);
466         for (size_t i=0; i<nops(); i++) {
467                 v = rotate_left(v);
468                 v ^= this->op(i).gethash();
469         }
470
471         if (flags & status_flags::evaluated) {
472                 setflag(status_flags::hash_calculated);
473                 hashvalue = v;
474         }
475         return v;
476 }
477
478 ex function::thiscontainer(const exvector & v) const
479 {
480         return function(serial, v);
481 }
482
483 ex function::thiscontainer(exvector && v) const
484 {
485         return function(serial, std::move(v));
486 }
487
488 /** Implementation of ex::series for functions.
489  *  \@see ex::series */
490 ex function::series(const relational & r, int order, unsigned options) const
491 {
492         GINAC_ASSERT(serial<registered_functions().size());
493         const function_options &opt = registered_functions()[serial];
494
495         if (opt.series_f==0) {
496                 return basic::series(r, order);
497         }
498         ex res;
499         current_serial = serial;
500         if (opt.series_use_exvector_args) {
501                 try {
502                         res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
503                 } catch (do_taylor) {
504                         res = basic::series(r, order, options);
505                 }
506                 return res;
507         }
508         switch (opt.nparams) {
509                 // the following lines have been generated for max. @maxargs@ parameters
510 +++ for N in range(1, maxargs + 1):
511                 case @N@:
512                         try {
513                                 res = ((series_funcp_@N@)(opt.series_f))(@seq('seq[%(n)d]', N, 0)@, r, order, options);
514                         } catch (do_taylor) {
515                                 res = basic::series(r, order, options);
516                         }
517                         return res;
518 ---
519                 // end of generated lines
520         }
521         throw(std::logic_error("function::series(): invalid nparams"));
522 }
523
524 /** Implementation of ex::conjugate for functions. */
525 ex function::conjugate() const
526 {
527         GINAC_ASSERT(serial<registered_functions().size());
528         const function_options & opt = registered_functions()[serial];
529
530         if (opt.conjugate_f==0) {
531                 return conjugate_function(*this).hold();
532         }
533
534         if (opt.conjugate_use_exvector_args) {
535                 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
536         }
537
538         switch (opt.nparams) {
539                 // the following lines have been generated for max. @maxargs@ parameters
540 +++ for N in range(1, maxargs + 1):
541                 case @N@:
542                         return ((conjugate_funcp_@N@)(opt.conjugate_f))(@seq('seq[%(n)d]', N, 0)@);
543 ---
544                 // end of generated lines
545         }
546         throw(std::logic_error("function::conjugate(): invalid nparams"));
547 }
548
549 /** Implementation of ex::real_part for functions. */
550 ex function::real_part() const
551 {
552         GINAC_ASSERT(serial<registered_functions().size());
553         const function_options & opt = registered_functions()[serial];
554
555         if (opt.real_part_f==0)
556                 return basic::real_part();
557
558         if (opt.real_part_use_exvector_args)
559                 return ((real_part_funcp_exvector)(opt.real_part_f))(seq);
560
561         switch (opt.nparams) {
562                 // the following lines have been generated for max. @maxargs@ parameters
563 +++ for N in range(1, maxargs + 1):
564                 case @N@:
565                         return ((real_part_funcp_@N@)(opt.real_part_f))(@seq('seq[%(n)d]', N, 0)@);
566 ---
567                 // end of generated lines
568         }
569         throw(std::logic_error("function::real_part(): invalid nparams"));
570 }
571
572 /** Implementation of ex::imag_part for functions. */
573 ex function::imag_part() const
574 {
575         GINAC_ASSERT(serial<registered_functions().size());
576         const function_options & opt = registered_functions()[serial];
577
578         if (opt.imag_part_f==0)
579                 return basic::imag_part();
580
581         if (opt.imag_part_use_exvector_args)
582                 return ((imag_part_funcp_exvector)(opt.imag_part_f))(seq);
583
584         switch (opt.nparams) {
585                 // the following lines have been generated for max. @maxargs@ parameters
586 +++ for N in range(1, maxargs + 1):
587                 case @N@:
588                         return ((imag_part_funcp_@N@)(opt.imag_part_f))(@seq('seq[%(n)d]', N, 0)@);
589 ---
590                 // end of generated lines
591         }
592         throw(std::logic_error("function::imag_part(): invalid nparams"));
593 }
594
595 /** Implementation of ex::info for functions. */
596 bool function::info(unsigned inf) const
597 {
598         GINAC_ASSERT(serial<registered_functions().size());
599         const function_options & opt = registered_functions()[serial];
600
601         if (opt.info_f==0) {
602                 return basic::info(inf);
603         }
604
605         if (opt.info_use_exvector_args) {
606                 return ((info_funcp_exvector)(opt.info_f))(seq, inf);
607         }
608
609         switch (opt.nparams) {
610                 // the following lines have been generated for max. @maxargs@ parameters
611 +++ for N in range(1, maxargs + 1):
612                 case @N@:
613                         return ((info_funcp_@N@)(opt.info_f))(@seq('seq[%(n)d]', N, 0)@, inf);
614 ---
615                 // end of generated lines
616         }
617         throw(std::logic_error("function::info(): invalid nparams"));
618 }
619
620 // protected
621
622 /** Implementation of ex::diff() for functions. It applies the chain rule,
623  *  except for the Order term function.
624  *  \@see ex::diff */
625 ex function::derivative(const symbol & s) const
626 {
627         ex result;
628
629         try {
630                 // Explicit derivation
631                 result = expl_derivative(s);
632         } catch (...) {
633                 // Chain rule
634                 ex arg_diff;
635                 size_t num = seq.size();
636                 for (size_t i=0; i<num; i++) {
637                         arg_diff = seq[i].diff(s);
638                         // We apply the chain rule only when it makes sense.  This is not
639                         // just for performance reasons but also to allow functions to
640                         // throw when differentiated with respect to one of its arguments
641                         // without running into trouble with our automatic full
642                         // differentiation:
643                         if (!arg_diff.is_zero())
644                                 result += pderivative(i)*arg_diff;
645                 }
646         }
647         return result;
648 }
649
650 int function::compare_same_type(const basic & other) const
651 {
652         GINAC_ASSERT(is_a<function>(other));
653         const function & o = static_cast<const function &>(other);
654
655         if (serial != o.serial)
656                 return serial < o.serial ? -1 : 1;
657         else
658                 return exprseq::compare_same_type(o);
659 }
660
661 bool function::is_equal_same_type(const basic & other) const
662 {
663         GINAC_ASSERT(is_a<function>(other));
664         const function & o = static_cast<const function &>(other);
665
666         if (serial != o.serial)
667                 return false;
668         else
669                 return exprseq::is_equal_same_type(o);
670 }
671
672 bool function::match_same_type(const basic & other) const
673 {
674         GINAC_ASSERT(is_a<function>(other));
675         const function & o = static_cast<const function &>(other);
676
677         return serial == o.serial;
678 }
679
680 unsigned function::return_type() const
681 {
682         GINAC_ASSERT(serial<registered_functions().size());
683         const function_options &opt = registered_functions()[serial];
684
685         if (opt.use_return_type) {
686                 // Return type was explicitly specified
687                 return opt.return_type;
688         } else {
689                 // Default behavior is to use the return type of the first
690                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
691                 if (seq.empty())
692                         return return_types::commutative;
693                 else
694                         return seq.begin()->return_type();
695         }
696 }
697
698 return_type_t function::return_type_tinfo() const
699 {
700         GINAC_ASSERT(serial<registered_functions().size());
701         const function_options &opt = registered_functions()[serial];
702
703         if (opt.use_return_type) {
704                 // Return type was explicitly specified
705                 return opt.return_type_tinfo;
706         } else {
707                 // Default behavior is to use the return type of the first
708                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
709                 if (seq.empty())
710                         return make_return_type_t<function>();
711                 else
712                         return seq.begin()->return_type_tinfo();
713         }
714 }
715
716 //////////
717 // new virtual functions which can be overridden by derived classes
718 //////////
719
720 // none
721
722 //////////
723 // non-virtual functions in this class
724 //////////
725
726 // protected
727
728 ex function::pderivative(unsigned diff_param) const // partial differentiation
729 {
730         GINAC_ASSERT(serial<registered_functions().size());
731         const function_options &opt = registered_functions()[serial];
732         
733         if (opt.derivative_f) {
734                 // Invoke the defined derivative function.
735                 current_serial = serial;
736                 if (opt.derivative_use_exvector_args)
737                         return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
738                 switch (opt.nparams) {
739                         // the following lines have been generated for max. @maxargs@ parameters
740 +++ for N in range(1, maxargs + 1):
741                         case @N@:
742                                 return ((derivative_funcp_@N@)(opt.derivative_f))(@seq('seq[%(n)d]', N, 0)@, diff_param);
743 ---
744                         // end of generated lines
745                 }
746         }
747         // No derivative defined? Fall back to abstract derivative object.
748         return fderivative(serial, diff_param, seq);
749 }
750
751 ex function::expl_derivative(const symbol & s) const // explicit differentiation
752 {
753         GINAC_ASSERT(serial<registered_functions().size());
754         const function_options &opt = registered_functions()[serial];
755
756         if (opt.expl_derivative_f) {
757                 // Invoke the defined explicit derivative function.
758                 current_serial = serial;
759                 if (opt.expl_derivative_use_exvector_args)
760                         return ((expl_derivative_funcp_exvector)(opt.expl_derivative_f))(seq, s);
761                 switch (opt.nparams) {
762                         // the following lines have been generated for max. @maxargs@ parameters
763 +++ for N in range(1, maxargs + 1):
764                         case @N@:
765                                 return ((expl_derivative_funcp_@N@)(opt.expl_derivative_f))(@seq('seq[%(n)d]', N, 0)@, s);
766 ---
767                         // end of generated lines
768                 }
769         }
770         // There is no fallback for explicit deriviative.
771         throw(std::logic_error("function::expl_derivative(): explicit derivation is called, but no such function defined"));
772 }
773
774 ex function::power(const ex & power_param) const // power of function
775 {
776         GINAC_ASSERT(serial<registered_functions().size());
777         const function_options &opt = registered_functions()[serial];
778         
779         if (opt.power_f) {
780                 // Invoke the defined power function.
781                 current_serial = serial;
782                 if (opt.power_use_exvector_args)
783                         return ((power_funcp_exvector)(opt.power_f))(seq,  power_param);
784                 switch (opt.nparams) {
785                         // the following lines have been generated for max. @maxargs@ parameters
786 +++ for N in range(1, maxargs + 1):
787                         case @N@:
788                                 return ((power_funcp_@N@)(opt.power_f))(@seq('seq[%(n)d]', N, 0)@, power_param);
789 ---
790                         // end of generated lines
791                 }
792         }
793         // No power function defined? Fall back to returning a power object.
794         return dynallocate<GiNaC::power>(*this, power_param).setflag(status_flags::evaluated);
795 }
796
797 ex function::expand(unsigned options) const
798 {
799         GINAC_ASSERT(serial<registered_functions().size());
800         const function_options &opt = registered_functions()[serial];
801
802         if (opt.expand_f) {
803                 // Invoke the defined expand function.
804                 current_serial = serial;
805                 if (opt.expand_use_exvector_args)
806                         return ((expand_funcp_exvector)(opt.expand_f))(seq,  options);
807                 switch (opt.nparams) {
808                         // the following lines have been generated for max. @maxargs@ parameters
809 +++ for N in range(1, maxargs + 1):
810                         case @N@:
811                                 return ((expand_funcp_@N@)(opt.expand_f))(@seq('seq[%(n)d]', N, 0)@, options);
812 ---
813                         // end of generated lines
814                 }
815         }
816         // No expand function defined? Return the same function with expanded arguments (if required)
817         if (options & expand_options::expand_function_args)
818                 return inherited::expand(options);
819         else
820                 return (options == 0) ? setflag(status_flags::expanded) : *this;
821 }
822
823 std::vector<function_options> & function::registered_functions()
824 {
825         static std::vector<function_options> rf = std::vector<function_options>();
826         return rf;
827 }
828
829 bool function::lookup_remember_table(ex & result) const
830 {
831         return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
832 }
833
834 void function::store_remember_table(ex const & result) const
835 {
836         remember_table::remember_tables()[this->serial].add_entry(*this,result);
837 }
838
839 // public
840
841 unsigned function::register_new(function_options const & opt)
842 {
843         size_t same_name = 0;
844         for (size_t i=0; i<registered_functions().size(); ++i) {
845                 if (registered_functions()[i].name==opt.name) {
846                         ++same_name;
847                 }
848         }
849         if (same_name>=opt.functions_with_same_name) {
850                 // we do not throw an exception here because this code is
851                 // usually executed before main(), so the exception could not
852                 // caught anyhow
853                 std::cerr << "WARNING: function name " << opt.name
854                           << " already in use!" << std::endl;
855         }
856         registered_functions().push_back(opt);
857         if (opt.use_remember) {
858                 remember_table::remember_tables().
859                         push_back(remember_table(opt.remember_size,
860                                                  opt.remember_assoc_size,
861                                                  opt.remember_strategy));
862         } else {
863                 remember_table::remember_tables().push_back(remember_table());
864         }
865         return registered_functions().size()-1;
866 }
867
868 /** Find serial number of function by name and number of parameters.
869  *  Throws exception if function was not found. */
870 unsigned function::find_function(const std::string &name, unsigned nparams)
871 {
872         unsigned serial = 0;
873         for (auto & it : function::registered_functions()) {
874                 if (it.get_name() == name && it.get_nparams() == nparams)
875                         return serial;
876                 ++serial;
877         }
878         throw (std::runtime_error("no function '" + name + "' with " + std::to_string(nparams) + " parameters defined"));
879 }
880
881 /** Return the print name of the function. */
882 std::string function::get_name() const
883 {
884         GINAC_ASSERT(serial<registered_functions().size());
885         return registered_functions()[serial].name;
886 }
887
888 } // namespace GiNaC
889