]> www.ginac.de Git - ginac.git/blob - ginac/function.pl
- fixed atan2_evalf()
[ginac.git] / ginac / function.pl
1 #  This perl script automatically generates function.h and function.cpp
2
3 #  function.pl options: \$maxargs=${maxargs}
4
5 #  GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
6
7 #  This program is free software; you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2 of the License, or
10 #  (at your option) any later version.
11
12 #  This program is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 $maxargs=14;
22
23 sub generate_seq {
24         my ($seq_template,$n)=@_;
25         my ($res,$N);
26         
27         $res='';
28         for ($N=1; $N<=$n; $N++) {
29                 $res .= eval('"' . $seq_template . '"');
30                 if ($N!=$n) {
31                         $res .= ', ';
32                 }
33         }
34         return $res;
35 }
36
37 sub generate_from_to {
38         my ($template,$seq_template1,$seq_template2,$seq_template3,$from,$to)=@_;
39         my ($res,$N,$SEQ);
40
41         $res='';
42         for ($N=$from; $N<=$to; $N++) {
43                 $SEQ1=generate_seq($seq_template1,$N);
44                 $SEQ2=generate_seq($seq_template2,$N);
45                 $SEQ3=generate_seq($seq_template3,$N);
46                 $res .= eval('"' . $template . '"');
47                 $SEQ1=''; # to avoid main::SEQ1 used only once warning
48                 $SEQ2=''; # same as above
49                 $SEQ3=''; # same as above
50         }
51         return $res;
52 }
53
54 sub generate {
55         my ($template,$seq_template1,$seq_template2,$seq_template3)=@_;
56         return generate_from_to($template,$seq_template1,$seq_template2,$seq_template3,1,$maxargs);
57 }
58
59 $declare_function_macro = generate(
60         <<'END_OF_DECLARE_FUNCTION_MACRO','typename T${N}','const T${N} & p${N}','GiNaC::ex(p${N})');
61 #define DECLARE_FUNCTION_${N}P(NAME) \\
62 class NAME##_SERIAL { public: static unsigned serial; }; \\
63 template<${SEQ1}> const GiNaC::function NAME(${SEQ2}) { \\
64         return GiNaC::function(NAME##_SERIAL::serial, ${SEQ3}); \\
65 }
66
67 END_OF_DECLARE_FUNCTION_MACRO
68
69 $typedef_eval_funcp=generate(
70 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
71 'const ex &','','');
72
73 $typedef_evalf_funcp=generate(
74 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
75 'const ex &','','');
76
77 $typedef_conjugate_funcp=generate(
78 'typedef ex (* conjugate_funcp_${N})(${SEQ1});'."\n",
79 'const ex &','','');
80
81 $typedef_derivative_funcp=generate(
82 'typedef ex (* derivative_funcp_${N})(${SEQ1}, unsigned);'."\n",
83 'const ex &','','');
84
85 $typedef_series_funcp=generate(
86 'typedef ex (* series_funcp_${N})(${SEQ1}, const relational &, int, unsigned);'."\n",
87 'const ex &','','');
88
89 $typedef_print_funcp=generate(
90 'typedef void (* print_funcp_${N})(${SEQ1}, const print_context &);'."\n",
91 'const ex &','','');
92
93 $eval_func_interface=generate('    function_options & eval_func(eval_funcp_${N} e);'."\n",'','','');
94
95 $evalf_func_interface=generate('    function_options & evalf_func(evalf_funcp_${N} ef);'."\n",'','','');
96
97 $conjugate_func_interface=generate('    function_options & conjugate_func(conjugate_funcp_${N} d);'."\n",'','','');
98
99 $derivative_func_interface=generate('    function_options & derivative_func(derivative_funcp_${N} d);'."\n",'','','');
100
101 $series_func_interface=generate('    function_options & series_func(series_funcp_${N} s);'."\n",'','','');
102
103 $print_func_interface=generate(
104         <<'END_OF_PRINT_FUNC_INTERFACE','','','');
105     template <class Ctx> function_options & print_func(print_funcp_${N} p)
106     {
107         test_and_set_nparams(${N});
108         set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
109         return *this;
110     }
111 END_OF_PRINT_FUNC_INTERFACE
112
113 $constructors_interface=generate(
114 '    function(unsigned ser, ${SEQ1});'."\n",
115 'const ex & param${N}','','');
116
117 $constructors_implementation=generate(
118         <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}','param${N}','');
119 function::function(unsigned ser, ${SEQ1})
120         : exprseq(${SEQ2}), serial(ser)
121 {
122         tinfo_key = TINFO_function;
123 }
124 END_OF_CONSTRUCTORS_IMPLEMENTATION
125
126 $eval_switch_statement=generate(
127         <<'END_OF_EVAL_SWITCH_STATEMENT','seq[${N}-1]','','');
128         case ${N}:
129                 eval_result = ((eval_funcp_${N})(opt.eval_f))(${SEQ1});
130                 break;
131 END_OF_EVAL_SWITCH_STATEMENT
132
133 $evalf_switch_statement=generate(
134         <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','','');
135         case ${N}:
136                 return ((evalf_funcp_${N})(opt.evalf_f))(${SEQ1});
137 END_OF_EVALF_SWITCH_STATEMENT
138
139 $conjugate_switch_statement=generate(
140         <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
141         case ${N}:
142                 return ((conjugate_funcp_${N})(opt.conjugate_f))(${SEQ1});
143 END_OF_DIFF_SWITCH_STATEMENT
144
145 $diff_switch_statement=generate(
146         <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','','');
147         case ${N}:
148                 return ((derivative_funcp_${N})(opt.derivative_f))(${SEQ1},diff_param);
149 END_OF_DIFF_SWITCH_STATEMENT
150
151 $series_switch_statement=generate(
152         <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','','');
153         case ${N}:
154                 try {
155                         res = ((series_funcp_${N})(opt.series_f))(${SEQ1},r,order,options);
156                 } catch (do_taylor) {
157                         res = basic::series(r, order, options);
158                 }
159                 return res;
160 END_OF_SERIES_SWITCH_STATEMENT
161
162 $print_switch_statement=generate(
163         <<'END_OF_PRINT_SWITCH_STATEMENT','seq[${N}-1]','','');
164                 case ${N}:
165                         ((print_funcp_${N})(pdt[id]))(${SEQ1}, c);
166                         break;
167 END_OF_PRINT_SWITCH_STATEMENT
168
169 $eval_func_implementation=generate(
170         <<'END_OF_EVAL_FUNC_IMPLEMENTATION','','','');
171 function_options & function_options::eval_func(eval_funcp_${N} e)
172 {
173         test_and_set_nparams(${N});
174         eval_f = eval_funcp(e);
175         return *this;
176 }
177 END_OF_EVAL_FUNC_IMPLEMENTATION
178
179 $evalf_func_implementation=generate(
180         <<'END_OF_EVALF_FUNC_IMPLEMENTATION','','','');
181 function_options & function_options::evalf_func(evalf_funcp_${N} ef)
182 {
183         test_and_set_nparams(${N});
184         evalf_f = evalf_funcp(ef);
185         return *this;
186 }
187 END_OF_EVALF_FUNC_IMPLEMENTATION
188
189 $conjugate_func_implementation=generate(
190         <<'END_OF_CONJUGATE_FUNC_IMPLEMENTATION','','','');
191 function_options & function_options::conjugate_func(conjugate_funcp_${N} c)
192 {
193         test_and_set_nparams(${N});
194         conjugate_f = conjugate_funcp(c);
195         return *this;
196 }
197 END_OF_CONJUGATE_FUNC_IMPLEMENTATION
198
199 $derivative_func_implementation=generate(
200         <<'END_OF_DERIVATIVE_FUNC_IMPLEMENTATION','','','');
201 function_options & function_options::derivative_func(derivative_funcp_${N} d)
202 {
203         test_and_set_nparams(${N});
204         derivative_f = derivative_funcp(d);
205         return *this;
206 }
207 END_OF_DERIVATIVE_FUNC_IMPLEMENTATION
208
209 $series_func_implementation=generate(
210         <<'END_OF_SERIES_FUNC_IMPLEMENTATION','','','');
211 function_options & function_options::series_func(series_funcp_${N} s)
212 {
213         test_and_set_nparams(${N});
214         series_f = series_funcp(s);
215         return *this;
216 }
217 END_OF_SERIES_FUNC_IMPLEMENTATION
218
219 $interface=<<END_OF_INTERFACE;
220 /** \@file function.h
221  *
222  *  Interface to class of symbolic functions. */
223
224 /*
225  *  This file was generated automatically by function.pl.
226  *  Please do not modify it directly, edit the perl script instead!
227  *  function.pl options: \$maxargs=${maxargs}
228  *
229  *  GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
230  *
231  *  This program is free software; you can redistribute it and/or modify
232  *  it under the terms of the GNU General Public License as published by
233  *  the Free Software Foundation; either version 2 of the License, or
234  *  (at your option) any later version.
235  *
236  *  This program is distributed in the hope that it will be useful,
237  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
238  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
239  *  GNU General Public License for more details.
240  *
241  *  You should have received a copy of the GNU General Public License
242  *  along with this program; if not, write to the Free Software
243  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
244  */
245
246 #ifndef __GINAC_FUNCTION_H__
247 #define __GINAC_FUNCTION_H__
248
249 #include <string>
250 #include <vector>
251
252 // CINT needs <algorithm> to work properly with <vector>
253 #include <algorithm>
254
255 #include "exprseq.h"
256
257 // the following lines have been generated for max. ${maxargs} parameters
258 $declare_function_macro
259 // end of generated lines
260
261 #define REGISTER_FUNCTION(NAME,OPT) \\
262 unsigned NAME##_SERIAL::serial = \\
263         GiNaC::function::register_new(GiNaC::function_options(#NAME).OPT);
264
265 namespace GiNaC {
266
267 class function;
268 class symmetry;
269
270 typedef ex (* eval_funcp)();
271 typedef ex (* evalf_funcp)();
272 typedef ex (* conjugate_funcp)();
273 typedef ex (* derivative_funcp)();
274 typedef ex (* series_funcp)();
275 typedef void (* print_funcp)();
276
277 // the following lines have been generated for max. ${maxargs} parameters
278 $typedef_eval_funcp
279 $typedef_evalf_funcp
280 $typedef_conjugate_funcp
281 $typedef_derivative_funcp
282 $typedef_series_funcp
283 $typedef_print_funcp
284 // end of generated lines
285
286 // Alternatively, an exvector may be passed into the static function, instead
287 // of individual ex objects.  Then, the number of arguments is not limited.
288 typedef ex (* eval_funcp_exvector)(const exvector &);
289 typedef ex (* evalf_funcp_exvector)(const exvector &);
290 typedef ex (* conjugate_funcp_exvector)(const exvector &);
291 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
292 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
293 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
294
295
296 class function_options
297 {
298         friend class function;
299         friend class fderivative;
300 public:
301         function_options();
302         function_options(std::string const & n, std::string const & tn=std::string());
303         ~function_options();
304         void initialize();
305
306         function_options & dummy() { return *this; }
307         function_options & set_name(std::string const & n, std::string const & tn=std::string());
308         function_options & latex_name(std::string const & tn);
309 // the following lines have been generated for max. ${maxargs} parameters
310 $eval_func_interface
311 $evalf_func_interface
312 $conjugate_func_interface
313 $derivative_func_interface
314 $series_func_interface
315 $print_func_interface
316 // end of generated lines
317         function_options & eval_func(eval_funcp_exvector e);
318         function_options & evalf_func(evalf_funcp_exvector ef);
319         function_options & conjugate_func(conjugate_funcp_exvector d);
320         function_options & derivative_func(derivative_funcp_exvector d);
321         function_options & series_func(series_funcp_exvector s);
322
323         template <class Ctx> function_options & print_func(print_funcp_exvector p)
324         {
325                 print_use_exvector_args = true;
326                 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
327                 return *this;
328         }
329
330         function_options & set_return_type(unsigned rt, unsigned rtt=0);
331         function_options & do_not_evalf_params();
332         function_options & remember(unsigned size, unsigned assoc_size=0,
333                                     unsigned strategy=remember_strategies::delete_never);
334         function_options & overloaded(unsigned o);
335         function_options & set_symmetry(const symmetry & s);
336
337         std::string get_name() const { return name; }
338         unsigned get_nparams() const { return nparams; }
339
340 protected:
341         bool has_derivative() const { return derivative_f != NULL; }
342         void test_and_set_nparams(unsigned n);
343         void set_print_func(unsigned id, print_funcp f);
344
345         std::string name;
346         std::string TeX_name;
347
348         unsigned nparams;
349
350         eval_funcp eval_f;
351         evalf_funcp evalf_f;
352         conjugate_funcp conjugate_f;
353         derivative_funcp derivative_f;
354         series_funcp series_f;
355         std::vector<print_funcp> print_dispatch_table;
356
357         bool evalf_params_first;
358
359         bool use_return_type;
360         unsigned return_type;
361         unsigned return_type_tinfo;
362
363         bool use_remember;
364         unsigned remember_size;
365         unsigned remember_assoc_size;
366         unsigned remember_strategy;
367
368         bool eval_use_exvector_args;
369         bool evalf_use_exvector_args;
370         bool conjugate_use_exvector_args;
371         bool derivative_use_exvector_args;
372         bool series_use_exvector_args;
373         bool print_use_exvector_args;
374
375         unsigned functions_with_same_name;
376
377         ex symtree;
378 };
379
380
381 /** Exception class thrown by classes which provide their own series expansion
382  *  to signal that ordinary Taylor expansion is safe. */
383 class do_taylor {};
384
385
386 /** The class function is used to implement builtin functions like sin, cos...
387         and user defined functions */
388 class function : public exprseq
389 {
390         GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
391
392         // CINT has a linking problem
393 #ifndef __MAKECINT__
394         friend void ginsh_get_ginac_functions();
395 #endif // def __MAKECINT__
396
397         friend class remember_table_entry;
398         // friend class remember_table_list;
399         // friend class remember_table;
400
401 // member functions
402
403         // other constructors
404 public:
405         function(unsigned ser);
406         // the following lines have been generated for max. ${maxargs} parameters
407 $constructors_interface
408         // end of generated lines
409         function(unsigned ser, const exprseq & es);
410         function(unsigned ser, const exvector & v, bool discardable = false);
411         function(unsigned ser, std::auto_ptr<exvector> vp);
412
413         // functions overriding virtual functions from base classes
414 public:
415         void print(const print_context & c, unsigned level = 0) const;
416         unsigned precedence() const {return 70;}
417         ex expand(unsigned options=0) const;
418         ex eval(int level=0) const;
419         ex evalf(int level=0) const;
420         unsigned calchash() const;
421         ex series(const relational & r, int order, unsigned options = 0) const;
422         ex thiscontainer(const exvector & v) const;
423         ex thiscontainer(std::auto_ptr<exvector> vp) const;
424         ex conjugate() const;
425 protected:
426         ex derivative(const symbol & s) const;
427         bool is_equal_same_type(const basic & other) const;
428         bool match_same_type(const basic & other) const;
429         unsigned return_type() const;
430         unsigned return_type_tinfo() const;
431         
432         // new virtual functions which can be overridden by derived classes
433         // none
434         
435         // non-virtual functions in this class
436 protected:
437         ex pderivative(unsigned diff_param) const; // partial differentiation
438         static std::vector<function_options> & registered_functions();
439         bool lookup_remember_table(ex & result) const;
440         void store_remember_table(ex const & result) const;
441 public:
442         static unsigned register_new(function_options const & opt);
443         static unsigned current_serial;
444         static unsigned find_function(const std::string &name, unsigned nparams);
445         unsigned get_serial() const {return serial;}
446         std::string get_name() const;
447
448 // member variables
449
450 protected:
451         unsigned serial;
452 };
453
454 // utility functions/macros
455
456 /** Specialization of is_exactly_a<function>(obj) for objects of type function. */
457 template<> inline bool is_exactly_a<function>(const basic & obj)
458 {
459         return obj.tinfo()==TINFO_function;
460 }
461
462 template <typename T>
463 inline bool is_the_function(const ex & x)
464 {
465         return is_exactly_a<function>(x)
466             && ex_to<function>(x).get_serial() == T::serial;
467 }
468
469 // Check whether OBJ is the specified symbolic function.
470 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
471
472 } // namespace GiNaC
473
474 #endif // ndef __GINAC_FUNCTION_H__
475
476 END_OF_INTERFACE
477
478 $implementation=<<END_OF_IMPLEMENTATION;
479 /** \@file function.cpp
480  *
481  *  Implementation of class of symbolic functions. */
482
483 /*
484  *  This file was generated automatically by function.pl.
485  *  Please do not modify it directly, edit the perl script instead!
486  *  function.pl options: \$maxargs=${maxargs}
487  *
488  *  GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
489  *
490  *  This program is free software; you can redistribute it and/or modify
491  *  it under the terms of the GNU General Public License as published by
492  *  the Free Software Foundation; either version 2 of the License, or
493  *  (at your option) any later version.
494  *
495  *  This program is distributed in the hope that it will be useful,
496  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
497  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
498  *  GNU General Public License for more details.
499  *
500  *  You should have received a copy of the GNU General Public License
501  *  along with this program; if not, write to the Free Software
502  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
503  */
504
505 #include <iostream>
506 #include <string>
507 #include <stdexcept>
508 #include <list>
509
510 #include "function.h"
511 #include "operators.h"
512 #include "fderivative.h"
513 #include "ex.h"
514 #include "lst.h"
515 #include "symmetry.h"
516 #include "print.h"
517 #include "archive.h"
518 #include "inifcns.h"
519 #include "tostring.h"
520 #include "utils.h"
521 #include "remember.h"
522
523 namespace GiNaC {
524
525 //////////
526 // helper class function_options
527 //////////
528
529 function_options::function_options()
530 {
531         initialize();
532 }
533
534 function_options::function_options(std::string const & n, std::string const & tn)
535 {
536         initialize();
537         set_name(n, tn);
538 }
539
540 function_options::~function_options()
541 {
542         // nothing to clean up at the moment
543 }
544
545 void function_options::initialize()
546 {
547         set_name("unnamed_function", "\\\\mbox{unnamed}");
548         nparams = 0;
549         eval_f = evalf_f = conjugate_f = derivative_f = series_f = 0;
550         evalf_params_first = true;
551         use_return_type = false;
552         eval_use_exvector_args = false;
553         evalf_use_exvector_args = false;
554         conjugate_use_exvector_args = false;
555         derivative_use_exvector_args = false;
556         series_use_exvector_args = false;
557         print_use_exvector_args = false;
558         use_remember = false;
559         functions_with_same_name = 1;
560 }
561
562 function_options & function_options::set_name(std::string const & n,
563                                               std::string const & tn)
564 {
565         name = n;
566         if (tn==std::string())
567                 TeX_name = "\\\\mbox{"+name+"}";
568         else
569                 TeX_name = tn;
570         return *this;
571 }
572
573 function_options & function_options::latex_name(std::string const & tn)
574 {
575         TeX_name = tn;
576         return *this;
577 }
578
579 // the following lines have been generated for max. ${maxargs} parameters
580 $eval_func_implementation
581 $evalf_func_implementation
582 $conjugate_func_implementation
583 $derivative_func_implementation
584 $series_func_implementation
585 // end of generated lines
586
587 function_options& function_options::eval_func(eval_funcp_exvector e)
588 {
589         eval_use_exvector_args = true;
590         eval_f = eval_funcp(e);
591         return *this;
592 }
593 function_options& function_options::evalf_func(evalf_funcp_exvector ef)
594 {
595         evalf_use_exvector_args = true;
596         evalf_f = evalf_funcp(ef);
597         return *this;
598 }
599 function_options& function_options::conjugate_func(conjugate_funcp_exvector c)
600 {
601         conjugate_use_exvector_args = true;
602         conjugate_f = conjugate_funcp(c);
603         return *this;
604 }
605 function_options& function_options::derivative_func(derivative_funcp_exvector d)
606 {
607         derivative_use_exvector_args = true;
608         derivative_f = derivative_funcp(d);
609         return *this;
610 }
611 function_options& function_options::series_func(series_funcp_exvector s)
612 {
613         series_use_exvector_args = true;
614         series_f = series_funcp(s);
615         return *this;
616 }
617
618 function_options & function_options::set_return_type(unsigned rt, unsigned rtt)
619 {
620         use_return_type = true;
621         return_type = rt;
622         return_type_tinfo = rtt;
623         return *this;
624 }
625
626 function_options & function_options::do_not_evalf_params()
627 {
628         evalf_params_first = false;
629         return *this;
630 }
631
632 function_options & function_options::remember(unsigned size,
633                                               unsigned assoc_size,
634                                               unsigned strategy)
635 {
636         use_remember = true;
637         remember_size = size;
638         remember_assoc_size = assoc_size;
639         remember_strategy = strategy;
640         return *this;
641 }
642
643 function_options & function_options::overloaded(unsigned o)
644 {
645         functions_with_same_name = o;
646         return *this;
647 }
648
649 function_options & function_options::set_symmetry(const symmetry & s)
650 {
651         symtree = s;
652         return *this;
653 }
654         
655 void function_options::test_and_set_nparams(unsigned n)
656 {
657         if (nparams==0) {
658                 nparams = n;
659         } else if (nparams!=n) {
660                 // we do not throw an exception here because this code is
661                 // usually executed before main(), so the exception could not
662                 // be caught anyhow
663                 std::cerr << "WARNING: " << name << "(): number of parameters ("
664                           << n << ") differs from number set before (" 
665                           << nparams << ")" << std::endl;
666         }
667 }
668
669 void function_options::set_print_func(unsigned id, print_funcp f)
670 {
671         if (id >= print_dispatch_table.size())
672                 print_dispatch_table.resize(id + 1);
673         print_dispatch_table[id] = f;
674 }
675
676 /** This can be used as a hook for external applications. */
677 unsigned function::current_serial = 0;
678
679
680 GINAC_IMPLEMENT_REGISTERED_CLASS(function, exprseq)
681
682 //////////
683 // default constructor
684 //////////
685
686 // public
687
688 function::function() : serial(0)
689 {
690         tinfo_key = TINFO_function;
691 }
692
693 //////////
694 // other constructors
695 //////////
696
697 // public
698
699 function::function(unsigned ser) : serial(ser)
700 {
701         tinfo_key = TINFO_function;
702 }
703
704 // the following lines have been generated for max. ${maxargs} parameters
705 $constructors_implementation
706 // end of generated lines
707
708 function::function(unsigned ser, const exprseq & es) : exprseq(es), serial(ser)
709 {
710         tinfo_key = TINFO_function;
711
712         // Force re-evaluation even if the exprseq was already evaluated
713         // (the exprseq copy constructor copies the flags)
714         clearflag(status_flags::evaluated);
715 }
716
717 function::function(unsigned ser, const exvector & v, bool discardable) 
718   : exprseq(v,discardable), serial(ser)
719 {
720         tinfo_key = TINFO_function;
721 }
722
723 function::function(unsigned ser, std::auto_ptr<exvector> vp) 
724   : exprseq(vp), serial(ser)
725 {
726         tinfo_key = TINFO_function;
727 }
728
729 //////////
730 // archiving
731 //////////
732
733 /** Construct object from archive_node. */
734 function::function(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
735 {
736         // Find serial number by function name
737         std::string s;
738         if (n.find_string("name", s)) {
739                 unsigned int ser = 0;
740                 std::vector<function_options>::const_iterator i = registered_functions().begin(), iend = registered_functions().end();
741                 while (i != iend) {
742                         if (s == i->name) {
743                                 serial = ser;
744                                 return;
745                         }
746                         ++i; ++ser;
747                 }
748                 throw (std::runtime_error("unknown function '" + s + "' in archive"));
749         } else
750                 throw (std::runtime_error("unnamed function in archive"));
751 }
752
753 /** Unarchive the object. */
754 ex function::unarchive(const archive_node &n, lst &sym_lst)
755 {
756         return (new function(n, sym_lst))->setflag(status_flags::dynallocated);
757 }
758
759 /** Archive the object. */
760 void function::archive(archive_node &n) const
761 {
762         inherited::archive(n);
763         GINAC_ASSERT(serial < registered_functions().size());
764         n.add_string("name", registered_functions()[serial].name);
765 }
766
767 //////////
768 // functions overriding virtual functions from base classes
769 //////////
770
771 // public
772
773 void function::print(const print_context & c, unsigned level) const
774 {
775         GINAC_ASSERT(serial<registered_functions().size());
776         const function_options &opt = registered_functions()[serial];
777         const std::vector<print_funcp> &pdt = opt.print_dispatch_table;
778
779         // Dynamically dispatch on print_context type
780         const print_context_class_info *pc_info = &c.get_class_info();
781
782 next_context:
783         unsigned id = pc_info->options.get_id();
784         if (id >= pdt.size() || pdt[id] == NULL) {
785
786                 // Method not found, try parent print_context class
787                 const print_context_class_info *parent_pc_info = pc_info->get_parent();
788                 if (parent_pc_info) {
789                         pc_info = parent_pc_info;
790                         goto next_context;
791                 }
792
793                 // Method still not found, use default output
794                 if (is_a<print_tree>(c)) {
795
796                         c.s << std::string(level, ' ') << class_name() << " "
797                             << opt.name << " @" << this
798                             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
799                             << ", nops=" << nops()
800                             << std::endl;
801                         unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
802                         for (size_t i=0; i<seq.size(); ++i)
803                                 seq[i].print(c, level + delta_indent);
804                         c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
805
806                 } else if (is_a<print_csrc>(c)) {
807
808                         // Print function name in lowercase
809                         std::string lname = opt.name;
810                         size_t num = lname.size();
811                         for (size_t i=0; i<num; i++)
812                                 lname[i] = tolower(lname[i]);
813                         c.s << lname;
814                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
815
816                 } else if (is_a<print_latex>(c)) {
817                         c.s << opt.TeX_name;
818                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
819                 } else {
820                         c.s << opt.name;
821                         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
822                 }
823
824         } else {
825
826                 // Method found, call it
827                 current_serial = serial;
828                 if (opt.print_use_exvector_args)
829                         ((print_funcp_exvector)pdt[id])(seq, c);
830                 else switch (opt.nparams) {
831                         // the following lines have been generated for max. ${maxargs} parameters
832 ${print_switch_statement}
833                         // end of generated lines
834                 default:
835                         throw(std::logic_error("function::print(): invalid nparams"));
836                 }
837         }
838 }
839
840 ex function::expand(unsigned options) const
841 {
842         // Only expand arguments when asked to do so
843         if (options & expand_options::expand_function_args)
844                 return inherited::expand(options);
845         else
846                 return (options == 0) ? setflag(status_flags::expanded) : *this;
847 }
848
849 ex function::eval(int level) const
850 {
851         if (level>1) {
852                 // first evaluate children, then we will end up here again
853                 return function(serial,evalchildren(level));
854         }
855
856         GINAC_ASSERT(serial<registered_functions().size());
857         const function_options &opt = registered_functions()[serial];
858
859         // Canonicalize argument order according to the symmetry properties
860         if (seq.size() > 1 && !(opt.symtree.is_zero())) {
861                 exvector v = seq;
862                 GINAC_ASSERT(is_a<symmetry>(opt.symtree));
863                 int sig = canonicalize(v.begin(), ex_to<symmetry>(opt.symtree));
864                 if (sig != INT_MAX) {
865                         // Something has changed while sorting arguments, more evaluations later
866                         if (sig == 0)
867                                 return _ex0;
868                         return ex(sig) * thiscontainer(v);
869                 }
870         }
871
872         if (opt.eval_f==0) {
873                 return this->hold();
874         }
875
876         bool use_remember = opt.use_remember;
877         ex eval_result;
878         if (use_remember && lookup_remember_table(eval_result)) {
879                 return eval_result;
880         }
881         current_serial = serial;
882         if (opt.eval_use_exvector_args)
883                 eval_result = ((eval_funcp_exvector)(opt.eval_f))(seq);
884         else
885         switch (opt.nparams) {
886                 // the following lines have been generated for max. ${maxargs} parameters
887 ${eval_switch_statement}
888                 // end of generated lines
889         default:
890                 throw(std::logic_error("function::eval(): invalid nparams"));
891         }
892         if (use_remember) {
893                 store_remember_table(eval_result);
894         }
895         return eval_result;
896 }
897
898 ex function::evalf(int level) const
899 {
900         GINAC_ASSERT(serial<registered_functions().size());
901         const function_options &opt = registered_functions()[serial];
902
903         // Evaluate children first
904         exvector eseq;
905         if (level == 1 || !(opt.evalf_params_first))
906                 eseq = seq;
907         else if (level == -max_recursion_level)
908                 throw(std::runtime_error("max recursion level reached"));
909         else {
910                 eseq.reserve(seq.size());
911                 --level;
912                 exvector::const_iterator it = seq.begin(), itend = seq.end();
913                 while (it != itend) {
914                         eseq.push_back(it->evalf(level));
915                         ++it;
916                 }
917         }
918
919         if (opt.evalf_f==0) {
920                 return function(serial,eseq).hold();
921         }
922         current_serial = serial;
923         if (opt.evalf_use_exvector_args)
924                 return ((evalf_funcp_exvector)(opt.evalf_f))(seq);
925         switch (opt.nparams) {
926                 // the following lines have been generated for max. ${maxargs} parameters
927 ${evalf_switch_statement}
928                 // end of generated lines
929         }
930         throw(std::logic_error("function::evalf(): invalid nparams"));
931 }
932
933 unsigned function::calchash() const
934 {
935         unsigned v = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
936         for (size_t i=0; i<nops(); i++) {
937                 v = rotate_left(v);
938                 v ^= this->op(i).gethash();
939         }
940
941         if (flags & status_flags::evaluated) {
942                 setflag(status_flags::hash_calculated);
943                 hashvalue = v;
944         }
945         return v;
946 }
947
948 ex function::thiscontainer(const exvector & v) const
949 {
950         return function(serial, v);
951 }
952
953 ex function::thiscontainer(std::auto_ptr<exvector> vp) const
954 {
955         return function(serial, vp);
956 }
957
958 /** Implementation of ex::series for functions.
959  *  \@see ex::series */
960 ex function::series(const relational & r, int order, unsigned options) const
961 {
962         GINAC_ASSERT(serial<registered_functions().size());
963         const function_options &opt = registered_functions()[serial];
964
965         if (opt.series_f==0) {
966                 return basic::series(r, order);
967         }
968         ex res;
969         current_serial = serial;
970         if (opt.series_use_exvector_args) {
971                 try {
972                         res = ((series_funcp_exvector)(opt.series_f))(seq, r, order, options);
973                 } catch (do_taylor) {
974                         res = basic::series(r, order, options);
975                 }
976                 return res;
977         }
978         switch (opt.nparams) {
979                 // the following lines have been generated for max. ${maxargs} parameters
980 ${series_switch_statement}
981                 // end of generated lines
982         }
983         throw(std::logic_error("function::series(): invalid nparams"));
984 }
985
986 /** Implementation of ex::conjugate for functions. */
987 ex function::conjugate() const
988 {
989         GINAC_ASSERT(serial<registered_functions().size());
990         const function_options & opt = registered_functions()[serial];
991
992         if (opt.conjugate_f==0) {
993                 return exprseq::conjugate();
994         }
995
996         if (opt.conjugate_use_exvector_args) {
997                 return ((conjugate_funcp_exvector)(opt.conjugate_f))(seq);
998         }
999
1000         switch (opt.nparams) {
1001                 // the following lines have been generated for max. ${maxargs} parameters
1002 ${conjugate_switch_statement}
1003                 // end of generated lines
1004         }
1005         throw(std::logic_error("function::conjugate(): invalid nparams"));
1006 }
1007
1008 // protected
1009
1010 /** Implementation of ex::diff() for functions. It applies the chain rule,
1011  *  except for the Order term function.
1012  *  \@see ex::diff */
1013 ex function::derivative(const symbol & s) const
1014 {
1015         ex result;
1016
1017         if (serial == Order_SERIAL::serial) {
1018                 // Order Term function only differentiates the argument
1019                 return Order(seq[0].diff(s));
1020         } else {
1021                 // Chain rule
1022                 ex arg_diff;
1023                 size_t num = seq.size();
1024                 for (size_t i=0; i<num; i++) {
1025                         arg_diff = seq[i].diff(s);
1026                         // We apply the chain rule only when it makes sense.  This is not
1027                         // just for performance reasons but also to allow functions to
1028                         // throw when differentiated with respect to one of its arguments
1029                         // without running into trouble with our automatic full
1030                         // differentiation:
1031                         if (!arg_diff.is_zero())
1032                                 result += pderivative(i)*arg_diff;
1033                 }
1034         }
1035         return result;
1036 }
1037
1038 int function::compare_same_type(const basic & other) const
1039 {
1040         GINAC_ASSERT(is_a<function>(other));
1041         const function & o = static_cast<const function &>(other);
1042
1043         if (serial != o.serial)
1044                 return serial < o.serial ? -1 : 1;
1045         else
1046                 return exprseq::compare_same_type(o);
1047 }
1048
1049 bool function::is_equal_same_type(const basic & other) const
1050 {
1051         GINAC_ASSERT(is_a<function>(other));
1052         const function & o = static_cast<const function &>(other);
1053
1054         if (serial != o.serial)
1055                 return false;
1056         else
1057                 return exprseq::is_equal_same_type(o);
1058 }
1059
1060 bool function::match_same_type(const basic & other) const
1061 {
1062         GINAC_ASSERT(is_a<function>(other));
1063         const function & o = static_cast<const function &>(other);
1064
1065         return serial == o.serial;
1066 }
1067
1068 unsigned function::return_type() const
1069 {
1070         GINAC_ASSERT(serial<registered_functions().size());
1071         const function_options &opt = registered_functions()[serial];
1072
1073         if (opt.use_return_type) {
1074                 // Return type was explicitly specified
1075                 return opt.return_type;
1076         } else {
1077                 // Default behavior is to use the return type of the first
1078                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1079                 if (seq.empty())
1080                         return return_types::commutative;
1081                 else
1082                         return seq.begin()->return_type();
1083         }
1084 }
1085
1086 unsigned function::return_type_tinfo() const
1087 {
1088         GINAC_ASSERT(serial<registered_functions().size());
1089         const function_options &opt = registered_functions()[serial];
1090
1091         if (opt.use_return_type) {
1092                 // Return type was explicitly specified
1093                 return opt.return_type_tinfo;
1094         } else {
1095                 // Default behavior is to use the return type of the first
1096                 // argument. Thus, exp() of a matrix behaves like a matrix, etc.
1097                 if (seq.empty())
1098                         return tinfo_key;
1099                 else
1100                         return seq.begin()->return_type_tinfo();
1101         }
1102 }
1103
1104 //////////
1105 // new virtual functions which can be overridden by derived classes
1106 //////////
1107
1108 // none
1109
1110 //////////
1111 // non-virtual functions in this class
1112 //////////
1113
1114 // protected
1115
1116 ex function::pderivative(unsigned diff_param) const // partial differentiation
1117 {
1118         GINAC_ASSERT(serial<registered_functions().size());
1119         const function_options &opt = registered_functions()[serial];
1120         
1121         // No derivative defined? Then return abstract derivative object
1122         if (opt.derivative_f == NULL)
1123                 return fderivative(serial, diff_param, seq);
1124
1125         current_serial = serial;
1126         if (opt.derivative_use_exvector_args)
1127                 return ((derivative_funcp_exvector)(opt.derivative_f))(seq, diff_param);
1128         switch (opt.nparams) {
1129                 // the following lines have been generated for max. ${maxargs} parameters
1130 ${diff_switch_statement}
1131                 // end of generated lines
1132         }
1133         throw(std::logic_error("function::pderivative(): no diff function defined"));
1134 }
1135
1136 std::vector<function_options> & function::registered_functions()
1137 {
1138         static std::vector<function_options> * rf = new std::vector<function_options>;
1139         return *rf;
1140 }
1141
1142 bool function::lookup_remember_table(ex & result) const
1143 {
1144         return remember_table::remember_tables()[this->serial].lookup_entry(*this,result);
1145 }
1146
1147 void function::store_remember_table(ex const & result) const
1148 {
1149         remember_table::remember_tables()[this->serial].add_entry(*this,result);
1150 }
1151
1152 // public
1153
1154 unsigned function::register_new(function_options const & opt)
1155 {
1156         size_t same_name = 0;
1157         for (size_t i=0; i<registered_functions().size(); ++i) {
1158                 if (registered_functions()[i].name==opt.name) {
1159                         ++same_name;
1160                 }
1161         }
1162         if (same_name>=opt.functions_with_same_name) {
1163                 // we do not throw an exception here because this code is
1164                 // usually executed before main(), so the exception could not
1165                 // caught anyhow
1166                 std::cerr << "WARNING: function name " << opt.name
1167                           << " already in use!" << std::endl;
1168         }
1169         registered_functions().push_back(opt);
1170         if (opt.use_remember) {
1171                 remember_table::remember_tables().
1172                         push_back(remember_table(opt.remember_size,
1173                                                  opt.remember_assoc_size,
1174                                                  opt.remember_strategy));
1175         } else {
1176                 remember_table::remember_tables().push_back(remember_table());
1177         }
1178         return registered_functions().size()-1;
1179 }
1180
1181 /** Find serial number of function by name and number of parameters.
1182  *  Throws exception if function was not found. */
1183 unsigned function::find_function(const std::string &name, unsigned nparams)
1184 {
1185         std::vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
1186         unsigned serial = 0;
1187         while (i != end) {
1188                 if (i->get_name() == name && i->get_nparams() == nparams)
1189                         return serial;
1190                 ++i;
1191                 ++serial;
1192         }
1193         throw (std::runtime_error("no function '" + name + "' with " + ToString(nparams) + " parameters defined"));
1194 }
1195
1196 /** Return the print name of the function. */
1197 std::string function::get_name() const
1198 {
1199         GINAC_ASSERT(serial<registered_functions().size());
1200         return registered_functions()[serial].name;
1201 }
1202
1203 } // namespace GiNaC
1204
1205 END_OF_IMPLEMENTATION
1206
1207 print "Creating interface file function.h...";
1208 open OUT,">function.h" or die "cannot open function.h";
1209 print OUT $interface;
1210 close OUT;
1211 print "ok.\n";
1212
1213 print "Creating implementation file function.cpp...";
1214 open OUT,">function.cpp" or die "cannot open function.cpp";
1215 print OUT $implementation;
1216 close OUT;
1217 print "ok.\n";
1218
1219 print "done.\n";