]> www.ginac.de Git - ginac.git/blob - ginac/function.pl
- modified the comment blocks so the copyright message no longer appears in
[ginac.git] / ginac / function.pl
1 #!/usr/bin/perl -w
2
3 $maxargs=10;
4
5 sub generate_seq {
6     my ($seq_template,$n)=@_;
7     my ($res,$N);
8     
9     $res='';
10     for ($N=1; $N<=$n; $N++) {
11         $res .= eval('"' . $seq_template . '"');
12         if ($N!=$n) {
13             $res .= ', ';
14         }
15     }
16     return $res;
17 }
18
19 sub generate {
20     my ($template,$seq_template1,$seq_template2)=@_;
21     my ($res,$N,$SEQ);
22
23     $res='';
24     for ($N=1; $N<=$maxargs; $N++) {
25         $SEQ1=generate_seq($seq_template1,$N);
26         $SEQ2=generate_seq($seq_template2,$N);
27         $res .= eval('"' . $template . '"');
28         $SEQ1=''; # to avoid main::SEQ1 used only once warning
29         $SEQ2=''; # same as above
30     }
31     return $res;
32 }
33
34 $declare_function_macro=generate(
35     <<'END_OF_DECLARE_FUNCTION_MACRO','ex const & p${N}','p${N}');
36 #define DECLARE_FUNCTION_${N}P(NAME) \\
37 extern unsigned function_index_##NAME; \\
38 inline function NAME(${SEQ1}) { \\
39     return function(function_index_##NAME, ${SEQ2}); \\
40 }
41
42 END_OF_DECLARE_FUNCTION_MACRO
43
44 $typedef_eval_funcp=generate(
45 'typedef ex (* eval_funcp_${N})(${SEQ1});'."\n",
46 'ex const &','');
47
48 $typedef_evalf_funcp=generate(
49 'typedef ex (* evalf_funcp_${N})(${SEQ1});'."\n",
50 'ex const &','');
51
52 $typedef_diff_funcp=generate(
53 'typedef ex (* diff_funcp_${N})(${SEQ1}, unsigned);'."\n",
54 'ex const &','');
55
56 $typedef_series_funcp=generate(
57 'typedef ex (* series_funcp_${N})(${SEQ1}, symbol const &, ex const &, int);'."\n",
58 'ex const &','');
59
60 $constructors_interface=generate(
61 '    function(unsigned ser, ${SEQ1});'."\n",
62 'ex const & param${N}','');
63
64 $register_new_interface=generate(
65 '    static unsigned register_new(char const * nm, eval_funcp_${N} e,'."\n".
66 '                                 evalf_funcp_${N} ef=0, diff_funcp_${N} d=0, series_funcp_${N} s=0);'.
67 "\n",'','');
68
69 $constructors_implementation=generate(
70     <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','ex const & param${N}','param${N}');
71 function::function(unsigned ser, ${SEQ1})
72     : exprseq(${SEQ2}), serial(ser)
73 {
74     debugmsg(\"function constructor from unsigned,${N}*ex\",LOGLEVEL_CONSTRUCT);
75     tinfo_key = TINFO_function;
76 }
77 END_OF_CONSTRUCTORS_IMPLEMENTATION
78
79 $eval_switch_statement=generate(
80     <<'END_OF_EVAL_SWITCH_STATEMENT','eseq[${N}-1]','');
81     case ${N}:
82         return ((eval_funcp_${N})(registered_functions()[serial].e))(${SEQ1});
83         break;
84 END_OF_EVAL_SWITCH_STATEMENT
85
86 $evalf_switch_statement=generate(
87     <<'END_OF_EVALF_SWITCH_STATEMENT','eseq[${N}-1]','');
88     case ${N}:
89         return ((evalf_funcp_${N})(registered_functions()[serial].ef))(${SEQ1});
90         break;
91 END_OF_EVALF_SWITCH_STATEMENT
92
93 $diff_switch_statement=generate(
94     <<'END_OF_DIFF_SWITCH_STATEMENT','seq[${N}-1]','');
95     case ${N}:
96         return ((diff_funcp_${N})(registered_functions()[serial].d))(${SEQ1},diff_param);
97         break;
98 END_OF_DIFF_SWITCH_STATEMENT
99
100 $series_switch_statement=generate(
101     <<'END_OF_SERIES_SWITCH_STATEMENT','seq[${N}-1]','');
102     case ${N}:
103         return ((series_funcp_${N})(registered_functions()[serial].s))(${SEQ1},s,point,order);
104         break;
105 END_OF_SERIES_SWITCH_STATEMENT
106
107 $register_new_implementation=generate(
108     <<'END_OF_REGISTER_NEW_IMPLEMENTATION','','');
109 unsigned function::register_new(char const * nm, eval_funcp_${N} e,
110                                  evalf_funcp_${N} ef, diff_funcp_${N} d, series_funcp_${N} s)
111 {
112     registered_function_info rfi={nm,${N},0,eval_funcp(e),
113                                   evalf_funcp(ef),diff_funcp(d),series_funcp(s)};
114     registered_functions().push_back(rfi);
115     return registered_functions().size()-1;
116 }
117 END_OF_REGISTER_NEW_IMPLEMENTATION
118
119 $interface=<<END_OF_INTERFACE;
120 /** \@file function.h
121  *
122  *  Interface to abstract class function (new function concept).
123  *
124  *  This file was generated automatically by function.pl.
125  *  Please do not modify it directly, edit the perl script instead!
126  *  function.pl options: \$maxargs=${maxargs}
127  *
128  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
129  *
130  *  This program is free software; you can redistribute it and/or modify
131  *  it under the terms of the GNU General Public License as published by
132  *  the Free Software Foundation; either version 2 of the License, or
133  *  (at your option) any later version.
134  *
135  *  This program is distributed in the hope that it will be useful,
136  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
137  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
138  *  GNU General Public License for more details.
139  *
140  *  You should have received a copy of the GNU General Public License
141  *  along with this program; if not, write to the Free Software
142  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
143  */
144
145 #ifndef __GINAC_FUNCTION_H__
146 #define __GINAC_FUNCTION_H__
147
148 #include <string>
149 #include <vector>
150 #include <ginac/exprseq.h>
151
152 class function;
153
154 // the following lines have been generated for max. ${maxargs} parameters
155 $declare_function_macro
156 // end of generated lines
157
158 #define REGISTER_FUNCTION(NAME,E,EF,D,S) \\
159 unsigned function_index_##NAME=function::register_new(#NAME,E,EF,D,S);
160
161 #define BEGIN_TYPECHECK \\
162 bool automatic_typecheck=true;
163
164 #define TYPECHECK(VAR,TYPE) \\
165 if (!is_ex_exactly_of_type(VAR,TYPE)) { \\
166     automatic_typecheck=false; \\
167 } else
168
169 #define TYPECHECK_INTEGER(VAR) \\
170 if (!(VAR).info(info_flags::integer)) { \\
171     automatic_typecheck=false; \\
172 } else
173
174 #define END_TYPECHECK(RV) \\
175 {} \\
176 if (!automatic_typecheck) { \\
177     return RV.hold(); \\
178 }
179
180 typedef ex (* eval_funcp)();
181 typedef ex (* evalf_funcp)();
182 typedef ex (* diff_funcp)();
183 typedef ex (* series_funcp)();
184
185 // the following lines have been generated for max. ${maxargs} parameters
186 $typedef_eval_funcp
187 $typedef_evalf_funcp
188 $typedef_diff_funcp
189 $typedef_series_funcp
190 // end of generated lines
191
192 struct registered_function_info {
193     char const * name;
194     unsigned nparams;
195     unsigned options;
196     eval_funcp e;
197     evalf_funcp ef;
198     diff_funcp d;
199     series_funcp s;
200 };
201
202 /** The class function is used to implement builtin functions like sin, cos...
203     and user defined functions */
204 class function : public exprseq
205 {
206     friend void ginsh_get_ginac_functions(void);
207
208 // member functions
209
210     // default constructor, destructor, copy constructor assignment operator and helpers
211 public:
212     function();
213     ~function();
214     function(function const & other);
215     function const & operator=(function const & other);
216 protected:
217     void copy(function const & other);
218     void destroy(bool call_parent);
219
220     // other constructors
221 public:
222     function(unsigned ser);
223     // the following lines have been generated for max. ${maxargs} parameters
224 $constructors_interface
225     // end of generated lines
226     function(unsigned ser, exprseq const & es);
227     function(unsigned ser, exvector const & v, bool discardable=0);
228     function(unsigned ser, exvector * vp); // vp will be deleted
229
230     // functions overriding virtual functions from bases classes
231 public:
232     basic * duplicate() const;
233     void printraw(ostream & os) const; 
234     void print(ostream & os, unsigned upper_precedence=0) const;
235     void printtree(ostream & os, unsigned indent) const;
236     void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
237     ex expand(unsigned options=0) const;
238     ex eval(int level=0) const;
239     ex evalf(int level=0) const;
240     ex diff(symbol const & s) const;
241     ex series(symbol const & s, ex const & point, int order) const;
242     ex thisexprseq(exvector const & v) const;
243     ex thisexprseq(exvector * vp) const;
244 protected:
245     int compare_same_type(basic const & other) const;
246     bool is_equal_same_type(basic const & other) const;
247     unsigned return_type(void) const;
248     unsigned return_type_tinfo(void) const;
249     
250     // new virtual functions which can be overridden by derived classes
251     // none
252     
253     // non-virtual functions in this class
254 protected:
255     ex pdiff(unsigned diff_param) const; // partial differentiation
256     static vector<registered_function_info> & registered_functions(void);
257 public:
258     // the following lines have been generated for max. ${maxargs} parameters
259 $register_new_interface
260     // end of generated lines
261     unsigned getserial(void) const {return serial;}
262     
263 // member variables
264
265 protected:
266     unsigned serial;
267 };
268
269 // utility macros
270
271 #define is_ex_the_function(OBJ, FUNCNAME) \\
272     (is_ex_exactly_of_type(OBJ, function) && static_cast<function *>(OBJ.bp)->getserial() == function_index_##FUNCNAME)
273
274 // global constants
275
276 extern const function some_function;
277 extern type_info const & typeid_function;
278
279 #endif // ndef __GINAC_FUNCTION_H__
280
281 END_OF_INTERFACE
282
283 $implementation=<<END_OF_IMPLEMENTATION;
284 /** \@file function.cpp
285  *
286  *  Implementation of class function.
287  *
288  *  This file was generated automatically by function.pl.
289  *  Please do not modify it directly, edit the perl script instead!
290  *  function.pl options: \$maxargs=${maxargs}
291  *
292  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
293  *
294  *  This program is free software; you can redistribute it and/or modify
295  *  it under the terms of the GNU General Public License as published by
296  *  the Free Software Foundation; either version 2 of the License, or
297  *  (at your option) any later version.
298  *
299  *  This program is distributed in the hope that it will be useful,
300  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
301  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
302  *  GNU General Public License for more details.
303  *
304  *  You should have received a copy of the GNU General Public License
305  *  along with this program; if not, write to the Free Software
306  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
307  */
308
309 #include <string>
310 #include <stdexcept>
311
312 #include "function.h"
313 #include "ex.h"
314
315 //////////
316 // default constructor, destructor, copy constructor assignment operator and helpers
317 //////////
318
319 // public
320
321 function::function() : serial(0)
322 {
323     debugmsg("function default constructor",LOGLEVEL_CONSTRUCT);
324     tinfo_key = TINFO_function;
325 }
326
327 function::~function()
328 {
329     debugmsg("function destructor",LOGLEVEL_DESTRUCT);
330     destroy(0);
331 }
332
333 function::function(function const & other)
334 {
335     debugmsg("function copy constructor",LOGLEVEL_CONSTRUCT);
336     copy(other);
337 }
338
339 function const & function::operator=(function const & other)
340 {
341     debugmsg("function operator=",LOGLEVEL_ASSIGNMENT);
342     if (this != &other) {
343         destroy(1);
344         copy(other);
345     }
346     return *this;
347 }
348
349 // protected
350
351 void function::copy(function const & other)
352 {
353     exprseq::copy(other);
354     serial=other.serial;
355 }
356
357 void function::destroy(bool call_parent)
358 {
359     if (call_parent) exprseq::destroy(call_parent);
360 }
361
362 //////////
363 // other constructors
364 //////////
365
366 // public
367
368 function::function(unsigned ser) : serial(ser)
369 {
370     debugmsg("function constructor from unsigned",LOGLEVEL_CONSTRUCT);
371     tinfo_key = TINFO_function;
372 }
373
374 // the following lines have been generated for max. ${maxargs} parameters
375 $constructors_implementation
376 // end of generated lines
377
378 function::function(unsigned ser, exprseq const & es) : exprseq(es), serial(ser)
379 {
380     debugmsg("function constructor from unsigned,exprseq",LOGLEVEL_CONSTRUCT);
381     tinfo_key = TINFO_function;
382 }
383
384 function::function(unsigned ser, exvector const & v, bool discardable) 
385     : exprseq(v,discardable), serial(ser)
386 {
387     debugmsg("function constructor from string,exvector,bool",LOGLEVEL_CONSTRUCT);
388     tinfo_key = TINFO_function;
389 }
390
391 function::function(unsigned ser, exvector * vp) 
392     : exprseq(vp), serial(ser)
393 {
394     debugmsg("function constructor from unsigned,exvector *",LOGLEVEL_CONSTRUCT);
395     tinfo_key = TINFO_function;
396 }
397
398 //////////
399 // functions overriding virtual functions from bases classes
400 //////////
401
402 // public
403
404 basic * function::duplicate() const
405 {
406     debugmsg("function duplicate",LOGLEVEL_DUPLICATE);
407     return new function(*this);
408 }
409
410 void function::printraw(ostream & os) const
411 {
412     debugmsg("function printraw",LOGLEVEL_PRINT);
413
414     ASSERT(serial<registered_functions().size());
415
416     os << "function(name=" << registered_functions()[serial].name;
417     for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
418         os << ",";
419         (*it).bp->print(os);
420     }
421     os << ")";
422 }
423
424 void function::print(ostream & os, unsigned upper_precedence) const
425 {
426     debugmsg("function print",LOGLEVEL_PRINT);
427
428     ASSERT(serial<registered_functions().size());
429
430     os << registered_functions()[serial].name;
431     printseq(os,'(',',',')',exprseq::precedence,function::precedence);
432 }
433
434 void function::printtree(ostream & os, unsigned indent) const
435 {
436     debugmsg("function printtree",LOGLEVEL_PRINT);
437
438     ASSERT(serial<registered_functions().size());
439
440     os << string(indent,' ') << "function "
441        << registered_functions()[serial].name
442        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
443        << ", flags=" << flags
444        << ", nops=" << nops() << endl;
445     for (int i=0; i<nops(); ++i) {
446         seq[i].printtree(os,indent+delta_indent);
447     }
448     os << string(indent+delta_indent,' ') << "=====" << endl;
449 }
450
451 void function::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
452 {
453     debugmsg("function print csrc",LOGLEVEL_PRINT);
454
455     ASSERT(serial<registered_functions().size());
456
457         // Print function name in lowercase
458     string lname;
459     lname=registered_functions()[serial].name;
460     for (unsigned i=0; i<lname.size(); i++)
461         lname[i] = tolower(lname[i]);
462     os << lname << "(";
463
464         // Print arguments, separated by commas
465     exvector::const_iterator it = seq.begin();
466     exvector::const_iterator itend = seq.end();
467     while (it != itend) {
468         it->bp->printcsrc(os, type, 0);
469         it++;
470         if (it != itend)
471             os << ",";
472     }
473     os << ")";
474 }
475
476 ex function::expand(unsigned options) const
477 {
478     return this->setflag(status_flags::expanded);
479 }
480
481 ex function::eval(int level) const
482 {
483     ASSERT(serial<registered_functions().size());
484
485     exvector eseq=evalchildren(level);    
486
487     if (registered_functions()[serial].e==0) {
488         return function(serial,eseq).hold();
489     }
490     switch (registered_functions()[serial].nparams) {
491         // the following lines have been generated for max. ${maxargs} parameters
492 ${eval_switch_statement}
493         // end of generated lines
494     }
495     throw(std::logic_error("function::eval(): invalid nparams"));
496 }
497
498 ex function::evalf(int level) const
499 {
500     ASSERT(serial<registered_functions().size());
501
502     exvector eseq=evalfchildren(level);
503     
504     if (registered_functions()[serial].ef==0) {
505         return function(serial,eseq).hold();
506     }
507     switch (registered_functions()[serial].nparams) {
508         // the following lines have been generated for max. ${maxargs} parameters
509 ${evalf_switch_statement}
510         // end of generated lines
511     }
512     throw(std::logic_error("function::evalf(): invalid nparams"));
513 }
514
515 ex function::thisexprseq(exvector const & v) const
516 {
517     return function(serial,v);
518 }
519
520 ex function::thisexprseq(exvector * vp) const
521 {
522     return function(serial,vp);
523 }
524
525 /** Implementation of ex::series for functions.
526  *  \@see ex::series */
527 ex function::series(symbol const & s, ex const & point, int order) const
528 {
529     ASSERT(serial<registered_functions().size());
530
531     if (registered_functions()[serial].s==0) {
532         return basic::series(s, point, order);
533     }
534     switch (registered_functions()[serial].nparams) {
535         // the following lines have been generated for max. ${maxargs} parameters
536 ${series_switch_statement}
537         // end of generated lines
538     }
539     throw(std::logic_error("function::series(): invalid nparams"));
540 }
541
542 // protected
543
544 int function::compare_same_type(basic const & other) const
545 {
546     ASSERT(is_of_type(other, function));
547     function const & o=static_cast<function &>(const_cast<basic &>(other));
548
549     if (serial!=o.serial) {
550         return serial < o.serial ? -1 : 1;
551     }
552     return exprseq::compare_same_type(o);
553 }
554
555 bool function::is_equal_same_type(basic const & other) const
556 {
557     ASSERT(is_of_type(other, function));
558     function const & o=static_cast<function &>(const_cast<basic &>(other));
559
560     if (serial!=o.serial) return false;
561     return exprseq::is_equal_same_type(o);
562 }
563
564 unsigned function::return_type(void) const
565 {
566     if (seq.size()==0) {
567         return return_types::commutative;
568     }
569     return (*seq.begin()).return_type();
570 }
571    
572 unsigned function::return_type_tinfo(void) const
573 {
574     if (seq.size()==0) {
575         return tinfo_key;
576     }
577     return (*seq.begin()).return_type_tinfo();
578 }
579
580 //////////
581 // new virtual functions which can be overridden by derived classes
582 //////////
583
584 // none
585
586 //////////
587 // non-virtual functions in this class
588 //////////
589
590 // protected
591
592 ex function::pdiff(unsigned diff_param) const // partial differentiation
593 {
594     ASSERT(serial<registered_functions().size());
595     
596     if (registered_functions()[serial].d==0) {
597         throw(std::logic_error(string("function::pdiff(") + registered_functions()[serial].name + "): no diff function defined"));
598     }
599     switch (registered_functions()[serial].nparams) {
600         // the following lines have been generated for max. ${maxargs} parameters
601 ${diff_switch_statement}
602         // end of generated lines
603     }        
604     throw(std::logic_error("function::pdiff(): no diff function defined"));
605 }
606
607 vector<registered_function_info> & function::registered_functions(void)
608 {
609     static vector<registered_function_info> * rf=new vector<registered_function_info>;
610     return *rf;
611 }
612
613 // public
614
615 // the following lines have been generated for max. ${maxargs} parameters
616 $register_new_implementation
617 // end of generated lines
618
619 //////////
620 // static member variables
621 //////////
622
623 // none
624
625 //////////
626 // global constants
627 //////////
628
629 const function some_function;
630 type_info const & typeid_function=typeid(some_function);
631
632 END_OF_IMPLEMENTATION
633
634 print "Creating interface file function.h...";
635 open OUT,">function.h" or die "cannot open function.h";
636 print OUT $interface;
637 close OUT;
638 print "ok.\n";
639
640 print "Creating implementation file function.cpp...";
641 open OUT,">function.cpp" or die "cannot open function.cpp";
642 print OUT $implementation;
643 close OUT;
644 print "ok.\n";
645
646 print "done.\n";