]> www.ginac.de Git - ginac.git/blob - ginac/function.cppy
Use AX_CXX_COMPILE_STDCXX_11 macro to force C++11 compilation.
[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 "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, exvector && v)
251   : exprseq(std::move(v)), 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                 for (auto & it : registered_functions()) {
268                         if (s == it.name) {
269                                 serial = ser;
270                                 return;
271                         }
272                         ++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] == nullptr) {
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                 for (auto & it : seq) {
434                         eseq.push_back(it.evalf(level));
435                 }
436         }
437
438         if (opt.evalf_f==0) {
439                 return function(serial,eseq).hold();
440         }
441         current_serial = serial;
442         if (opt.evalf_use_exvector_args)
443                 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
444         switch (opt.nparams) {
445                 // the following lines have been generated for max. @maxargs@ parameters
446 +++ for N in range(1, maxargs + 1):
447                 case @N@:
448                         return ((evalf_funcp_@N@)(opt.evalf_f))(@seq('eseq[%(n)d]', N, 0)@);
449 ---
450                 // end of generated lines
451         }
452         throw(std::logic_error("function::evalf(): invalid nparams"));
453 }
454
455 /**
456  *  This method is defined to be in line with behaviour of function::return_type()
457  */
458 ex function::eval_ncmul(const exvector & v) const
459 {
460         // If this function is called then the list of arguments is non-empty
461         // and the first argument is non-commutative, see  function::return_type()
462         return seq.begin()->eval_ncmul(v);
463 }
464
465 unsigned function::calchash() const
466 {
467         unsigned v = golden_ratio_hash(make_hash_seed(typeid(*this)) ^ serial);
468         for (size_t i=0; i<nops(); i++) {
469                 v = rotate_left(v);
470                 v ^= this->op(i).gethash();
471         }
472
473         if (flags & status_flags::evaluated) {
474                 setflag(status_flags::hash_calculated);
475                 hashvalue = v;
476         }
477         return v;
478 }
479
480 ex function::thiscontainer(const exvector & v) const
481 {
482         return function(serial, v);
483 }
484
485 ex function::thiscontainer(exvector && v) const
486 {
487         return function(serial, std::move(v));
488 }
489
490 /** Implementation of ex::series for functions.
491  *  \@see ex::series */
492 ex function::series(const relational & r, int order, unsigned options) const
493 {
494         GINAC_ASSERT(serial<registered_functions().size());
495         const function_options &opt = registered_functions()[serial];
496
497         if (opt.series_f==0) {
498                 return basic::series(r, order);
499         }
500         ex res;
501         current_serial = serial;
502         if (opt.series_use_exvector_args) {
503                 try {
504                         res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
505                 } catch (do_taylor) {
506                         res = basic::series(r, order, options);
507                 }
508                 return res;
509         }
510         switch (opt.nparams) {
511                 // the following lines have been generated for max. @maxargs@ parameters
512 +++ for N in range(1, maxargs + 1):
513                 case @N@:
514                         try {
515                                 res = ((series_funcp_@N@)(opt.series_f))(@seq('seq[%(n)d]', N, 0)@, r, order, options);
516                         } catch (do_taylor) {
517                                 res = basic::series(r, order, options);
518                         }
519                         return res;
520 ---
521                 // end of generated lines
522         }
523         throw(std::logic_error("function::series(): invalid nparams"));
524 }
525
526 /** Implementation of ex::conjugate for functions. */
527 ex function::conjugate() const
528 {
529         GINAC_ASSERT(serial<registered_functions().size());
530         const function_options & opt = registered_functions()[serial];
531
532         if (opt.conjugate_f==0) {
533                 return conjugate_function(*this).hold();
534         }
535
536         if (opt.conjugate_use_exvector_args) {
537                 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
538         }
539
540         switch (opt.nparams) {
541                 // the following lines have been generated for max. @maxargs@ parameters
542 +++ for N in range(1, maxargs + 1):
543                 case @N@:
544                         return ((conjugate_funcp_@N@)(opt.conjugate_f))(@seq('seq[%(n)d]', N, 0)@);
545 ---
546                 // end of generated lines
547         }
548         throw(std::logic_error("function::conjugate(): invalid nparams"));
549 }
550
551 /** Implementation of ex::real_part for functions. */
552 ex function::real_part() const
553 {
554         GINAC_ASSERT(serial<registered_functions().size());
555         const function_options & opt = registered_functions()[serial];
556
557         if (opt.real_part_f==0)
558                 return basic::real_part();
559
560         if (opt.real_part_use_exvector_args)
561                 return ((real_part_funcp_exvector)(opt.real_part_f))(seq);
562
563         switch (opt.nparams) {
564                 // the following lines have been generated for max. @maxargs@ parameters
565 +++ for N in range(1, maxargs + 1):
566                 case @N@:
567                         return ((real_part_funcp_@N@)(opt.real_part_f))(@seq('seq[%(n)d]', N, 0)@);
568 ---
569                 // end of generated lines
570         }
571         throw(std::logic_error("function::real_part(): invalid nparams"));
572 }
573
574 /** Implementation of ex::imag_part for functions. */
575 ex function::imag_part() const
576 {
577         GINAC_ASSERT(serial<registered_functions().size());
578         const function_options & opt = registered_functions()[serial];
579
580         if (opt.imag_part_f==0)
581                 return basic::imag_part();
582
583         if (opt.imag_part_use_exvector_args)
584                 return ((imag_part_funcp_exvector)(opt.imag_part_f))(seq);
585
586         switch (opt.nparams) {
587                 // the following lines have been generated for max. @maxargs@ parameters
588 +++ for N in range(1, maxargs + 1):
589                 case @N@:
590                         return ((imag_part_funcp_@N@)(opt.imag_part_f))(@seq('seq[%(n)d]', N, 0)@);
591 ---
592                 // end of generated lines
593         }
594         throw(std::logic_error("function::imag_part(): invalid nparams"));
595 }
596
597 /** Implementation of ex::info for functions. */
598 bool function::info(unsigned inf) const
599 {
600         GINAC_ASSERT(serial<registered_functions().size());
601         const function_options & opt = registered_functions()[serial];
602
603         if (opt.info_f==0) {
604                 return basic::info(inf);
605         }
606
607         if (opt.info_use_exvector_args) {
608                 return ((info_funcp_exvector)(opt.info_f))(seq, inf);
609         }
610
611         switch (opt.nparams) {
612                 // the following lines have been generated for max. @maxargs@ parameters
613 +++ for N in range(1, maxargs + 1):
614                 case @N@:
615                         return ((info_funcp_@N@)(opt.info_f))(@seq('seq[%(n)d]', N, 0)@, inf);
616 ---
617                 // end of generated lines
618         }
619         throw(std::logic_error("function::info(): invalid nparams"));
620 }
621
622 // protected
623
624 /** Implementation of ex::diff() for functions. It applies the chain rule,
625  *  except for the Order term function.
626  *  \@see ex::diff */
627 ex function::derivative(const symbol & s) const
628 {
629         ex result;
630
631         try {
632                 // Explicit derivation
633                 result = expl_derivative(s);
634         } catch (...) {
635                 // Chain rule
636                 ex arg_diff;
637                 size_t num = seq.size();
638                 for (size_t i=0; i<num; i++) {
639                         arg_diff = seq[i].diff(s);
640                         // We apply the chain rule only when it makes sense.  This is not
641                         // just for performance reasons but also to allow functions to
642                         // throw when differentiated with respect to one of its arguments
643                         // without running into trouble with our automatic full
644                         // differentiation:
645                         if (!arg_diff.is_zero())
646                                 result += pderivative(i)*arg_diff;
647                 }
648         }
649         return result;
650 }
651
652 int function::compare_same_type(const basic & other) const
653 {
654         GINAC_ASSERT(is_a<function>(other));
655         const function & o = static_cast<const function &>(other);
656
657         if (serial != o.serial)
658                 return serial < o.serial ? -1 : 1;
659         else
660                 return exprseq::compare_same_type(o);
661 }
662
663 bool function::is_equal_same_type(const basic & other) const
664 {
665         GINAC_ASSERT(is_a<function>(other));
666         const function & o = static_cast<const function &>(other);
667
668         if (serial != o.serial)
669                 return false;
670         else
671                 return exprseq::is_equal_same_type(o);
672 }
673
674 bool function::match_same_type(const basic & other) const
675 {
676         GINAC_ASSERT(is_a<function>(other));
677         const function & o = static_cast<const function &>(other);
678
679         return serial == o.serial;
680 }
681
682 unsigned function::return_type() const
683 {
684         GINAC_ASSERT(serial<registered_functions().size());
685         const function_options &opt = registered_functions()[serial];
686
687         if (opt.use_return_type) {
688                 // Return type was explicitly specified
689                 return opt.return_type;
690         } else {
691                 // Default behavior is to use the return type of the first
692                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
693                 if (seq.empty())
694                         return return_types::commutative;
695                 else
696                         return seq.begin()->return_type();
697         }
698 }
699
700 return_type_t function::return_type_tinfo() const
701 {
702         GINAC_ASSERT(serial<registered_functions().size());
703         const function_options &opt = registered_functions()[serial];
704
705         if (opt.use_return_type) {
706                 // Return type was explicitly specified
707                 return opt.return_type_tinfo;
708         } else {
709                 // Default behavior is to use the return type of the first
710                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
711                 if (seq.empty())
712                         return make_return_type_t<function>();
713                 else
714                         return seq.begin()->return_type_tinfo();
715         }
716 }
717
718 //////////
719 // new virtual functions which can be overridden by derived classes
720 //////////
721
722 // none
723
724 //////////
725 // non-virtual functions in this class
726 //////////
727
728 // protected
729
730 ex function::pderivative(unsigned diff_param) const // partial differentiation
731 {
732         GINAC_ASSERT(serial<registered_functions().size());
733         const function_options &opt = registered_functions()[serial];
734         
735         if (opt.derivative_f) {
736                 // Invoke the defined derivative function.
737                 current_serial = serial;
738                 if (opt.derivative_use_exvector_args)
739                         return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
740                 switch (opt.nparams) {
741                         // the following lines have been generated for max. @maxargs@ parameters
742 +++ for N in range(1, maxargs + 1):
743                         case @N@:
744                                 return ((derivative_funcp_@N@)(opt.derivative_f))(@seq('seq[%(n)d]', N, 0)@, diff_param);
745 ---
746                         // end of generated lines
747                 }
748         }
749         // No derivative defined? Fall back to abstract derivative object.
750         return fderivative(serial, diff_param, seq);
751 }
752
753 ex function::expl_derivative(const symbol & s) const // explicit differentiation
754 {
755         GINAC_ASSERT(serial<registered_functions().size());
756         const function_options &opt = registered_functions()[serial];
757
758         if (opt.expl_derivative_f) {
759                 // Invoke the defined explicit derivative function.
760                 current_serial = serial;
761                 if (opt.expl_derivative_use_exvector_args)
762                         return ((expl_derivative_funcp_exvector)(opt.expl_derivative_f))(seq, s);
763                 switch (opt.nparams) {
764                         // the following lines have been generated for max. @maxargs@ parameters
765 +++ for N in range(1, maxargs + 1):
766                         case @N@:
767                                 return ((expl_derivative_funcp_@N@)(opt.expl_derivative_f))(@seq('seq[%(n)d]', N, 0)@, s);
768 ---
769                         // end of generated lines
770                 }
771         }
772         // There is no fallback for explicit deriviative.
773         throw(std::logic_error("function::expl_derivative(): explicit derivation is called, but no such function defined"));
774 }
775
776 ex function::power(const ex & power_param) const // power of function
777 {
778         GINAC_ASSERT(serial<registered_functions().size());
779         const function_options &opt = registered_functions()[serial];
780         
781         if (opt.power_f) {
782                 // Invoke the defined power function.
783                 current_serial = serial;
784                 if (opt.power_use_exvector_args)
785                         return ((power_funcp_exvector)(opt.power_f))(seq,  power_param);
786                 switch (opt.nparams) {
787                         // the following lines have been generated for max. @maxargs@ parameters
788 +++ for N in range(1, maxargs + 1):
789                         case @N@:
790                                 return ((power_funcp_@N@)(opt.power_f))(@seq('seq[%(n)d]', N, 0)@, power_param);
791 ---
792                         // end of generated lines
793                 }
794         }
795         // No power function defined? Fall back to returning a power object.
796         return (new GiNaC::power(*this, power_param))->setflag(status_flags::dynallocated |
797                                                                status_flags::evaluated);
798 }
799
800 ex function::expand(unsigned options) const
801 {
802         GINAC_ASSERT(serial<registered_functions().size());
803         const function_options &opt = registered_functions()[serial];
804
805         if (opt.expand_f) {
806                 // Invoke the defined expand function.
807                 current_serial = serial;
808                 if (opt.expand_use_exvector_args)
809                         return ((expand_funcp_exvector)(opt.expand_f))(seq,  options);
810                 switch (opt.nparams) {
811                         // the following lines have been generated for max. @maxargs@ parameters
812 +++ for N in range(1, maxargs + 1):
813                         case @N@:
814                                 return ((expand_funcp_@N@)(opt.expand_f))(@seq('seq[%(n)d]', N, 0)@, options);
815 ---
816                         // end of generated lines
817                 }
818         }
819         // No expand function defined? Return the same function with expanded arguments (if required)
820         if (options & expand_options::expand_function_args)
821                 return inherited::expand(options);
822         else
823                 return (options == 0) ? setflag(status_flags::expanded) : *this;
824 }
825
826 std::vector<function_options> & function::registered_functions()
827 {
828         static std::vector<function_options> rf = std::vector<function_options>();
829         return rf;
830 }
831
832 bool function::lookup_remember_table(ex & result) const
833 {
834         return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
835 }
836
837 void function::store_remember_table(ex const & result) const
838 {
839         remember_table::remember_tables()[this->serial].add_entry(*this,result);
840 }
841
842 // public
843
844 unsigned function::register_new(function_options const & opt)
845 {
846         size_t same_name = 0;
847         for (size_t i=0; i<registered_functions().size(); ++i) {
848                 if (registered_functions()[i].name==opt.name) {
849                         ++same_name;
850                 }
851         }
852         if (same_name>=opt.functions_with_same_name) {
853                 // we do not throw an exception here because this code is
854                 // usually executed before main(), so the exception could not
855                 // caught anyhow
856                 std::cerr << "WARNING: function name " << opt.name
857                           << " already in use!" << std::endl;
858         }
859         registered_functions().push_back(opt);
860         if (opt.use_remember) {
861                 remember_table::remember_tables().
862                         push_back(remember_table(opt.remember_size,
863                                                  opt.remember_assoc_size,
864                                                  opt.remember_strategy));
865         } else {
866                 remember_table::remember_tables().push_back(remember_table());
867         }
868         return registered_functions().size()-1;
869 }
870
871 /** Find serial number of function by name and number of parameters.
872  *  Throws exception if function was not found. */
873 unsigned function::find_function(const std::string &name, unsigned nparams)
874 {
875         unsigned serial = 0;
876         for (auto & it : function::registered_functions()) {
877                 if (it.get_name() == name && it.get_nparams() == nparams)
878                         return serial;
879                 ++serial;
880         }
881         throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
882 }
883
884 /** Return the print name of the function. */
885 std::string function::get_name() const
886 {
887         GINAC_ASSERT(serial<registered_functions().size());
888         return registered_functions()[serial].name;
889 }
890
891 } // namespace GiNaC
892