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