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