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