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