]> www.ginac.de Git - ginac.git/blob - dummies.pl
890bafe8dd258c071d8e2946401af6594eeb52e3
[ginac.git] / dummies.pl
1 # Create files containing dummies, wrappers and so on to overcome certain
2 # deficiencies in Cint.  In an ideal world it would be unnecessary.
3
4 $header='dummies.h';
5 $source='dummies.cpp';
6
7 # Generate a header file which is additionally included in cint
8 # to work around the broken overloading resolution of cint for
9 # C library functions and other problems.
10 # E.g: if a function declared as ex sin(ex const & x) and called
11 # with sin(y) where y is a symbol, cint favours a conversion from
12 # symbol to void * to double over symbol to ex and thus calls the
13 # C math library function double sin(double x) (sigh!)
14
15 # types which need help to be converted to ex
16 @types=('symbol','function','constant','idx','lorentzidx','coloridx');
17 @moretypes=('numeric','int','double');
18 @extype=('ex');
19
20 # C math library functions with one parameter and return type 'function'
21 @cfunc_1p_function=('sin','cos','tan','asin','acos','atan','exp','log',
22                     'sinh','cosh','tanh','abs');
23
24 # C math library functions with one parameter and return type 'ex'
25 @cfunc_1p_ex=('sqrt');
26
27 # C math library functions with two parameters and return type 'function'
28 @cfunc_2p_function=('atan2');
29
30 # C math library functions with two parameters and return type 'ex'
31 @cfunc_2p_ex=('pow');
32
33 @binops_ex=('+','-','*','/','%');
34 @binops_relational=('==','!=','<','<=','>','>=');
35 @binops_exconstref=('+=','-=','*=','/=');
36
37 open OUT,">$header";
38
39 $opening=<<END_OF_OPENING;
40 /*  dummies.h
41  *
42  *  Dummies and wrappers to overcome certain deficiencies of Cint.
43  *  This file was generated automatically by dummies.pl.
44  *  Please do not modify it directly, edit the perl script instead!
45  */
46
47 END_OF_OPENING
48
49 print OUT $opening;
50
51 sub inline_cfunc_1p {
52     my ($rettype,$funcsref)=@_;
53     foreach $f (@$funcsref) {
54         print OUT "// fixes for $rettype $f(x)\n";
55         foreach $t (@types) {
56             print OUT "inline $rettype $f($t const & x) { return $f(ex(x)); }\n";
57         }
58         print OUT "\n";
59     }
60 }  
61
62 inline_cfunc_1p('function',\@cfunc_1p_function);
63 inline_cfunc_1p('ex',\@cfunc_1p_ex);
64
65 sub inline_single_cfunc_2p {
66     my ($rettype,$types1ref,$types2ref)=@_;
67     foreach $t1 (@$types1ref) {
68         foreach $t2 (@$types2ref) {
69             print OUT "inline $rettype $f($t1 const & x,$t2 const & y) {\n";
70             print OUT "    return $f(ex(x),ex(y));\n";
71             print OUT "}\n";
72         }
73     }
74 }
75
76 sub inline_cfunc_2p {
77     my ($rettype,$funcsref)=@_;
78     foreach $f (@$funcsref) {
79         print OUT "// fixes for $rettype $f(x,y)\n";
80         inline_single_cfunc_2p($rettype,\@types,\@types);
81         inline_single_cfunc_2p($rettype,\@types,\@moretypes);
82         inline_single_cfunc_2p($rettype,\@moretypes,\@types);
83         inline_single_cfunc_2p($rettype,\@extype,\@moretypes);
84         inline_single_cfunc_2p($rettype,\@moretypes,\@extype);
85         print OUT "\n";
86     }
87 }  
88
89 inline_cfunc_2p('function',\@cfunc_2p_function);
90 inline_cfunc_2p('ex',\@cfunc_2p_ex);
91
92 sub inline_function_1p {
93     my ($rettype,$func)=@_;
94     print OUT "inline $rettype $func(basic const & x) {\n";
95     print OUT "    return $func(ex(x));\n";
96     print OUT "}\n";
97 }
98
99 sub inline_single_function_2p {
100     my ($rettype,$func,$t1,$cast1,$t2,$cast2)=@_;
101     print OUT "inline $rettype $func($t1 x, $t2 y) {\n";
102     print OUT "    return $func($cast1(x),$cast2(y));\n";
103     print OUT "}\n";
104 }
105
106 sub inline_single_function_2p_with_defarg {
107     my ($rettype,$func,$t1,$defarg)=@_;
108     print OUT "inline $rettype $func(basic const & x, $t1 y=$defarg) {\n";
109     print OUT "    return $func(ex(x),y);\n";
110     print OUT "}\n";
111 }
112
113 sub inline_single_function_3p {
114     my ($rettype,$func,$t1,$cast1,$t2,$cast2,$t3,$cast3)=@_;
115     print OUT "inline $rettype $func($t1 x, $t2 y, $t3 z) {\n";
116     print OUT "    return $func($cast1(x),$cast2(y),$cast3(z));\n";
117     print OUT "}\n";
118 }
119
120 sub inline_single_function_3p_with_defarg {
121     my ($rettype,$func,$t1,$cast1,$t2,$cast2,$t3,$defarg)=@_;
122     print OUT "inline $rettype $func($t1 x, $t2 y, $t3 z=$defarg) {\n";
123     print OUT "    return $func($cast1(x),$cast2(y),z);\n";
124     print OUT "}\n";
125 }
126
127 sub inline_single_function_4p_with_defarg {
128     my ($rettype,$func,$t1,$cast1,$t2,$cast2,$t3,$cast3,$t4,$defarg)=@_;
129     print OUT "inline $rettype $func($t1 x, $t2 y, $t3 z, $t4 zz=$defarg) {\n";
130     print OUT "    return $func($cast1(x),$cast2(y),$cast3(z),zz);\n";
131     print OUT "}\n";
132 }
133
134 sub inline_single_binop {
135     my ($rettype,$op,$t1,$cast1,$t2,$cast2)=@_;
136     inline_single_function_2p($rettype,'operator'.$op,$t1,$cast1,$t2,$cast2);
137 }
138
139 sub inline_single_unaryop {
140     my ($rettype,$op)=@_;
141     print OUT "inline $rettype operator$op(basic const & x) {\n";
142     print OUT "    return operator$op(ex(x));\n";
143     print OUT "}\n";
144 }
145
146 sub inline_function_2p {
147     my ($rettype,$func)=@_;
148     print OUT "// fixes for $rettype $func(x,y)\n";
149     inline_single_function_2p($rettype,$func,'ex const &','','basic const &','ex');
150     inline_single_function_2p($rettype,$func,'basic const &','ex','ex const &','');
151     inline_single_function_2p($rettype,$func,'basic const &','ex','basic const &','ex');
152     print OUT "\n";
153 }
154
155 sub inline_binops {
156     my ($rettype,$opsref)=@_;
157     foreach $op (@$opsref) {
158         inline_function_2p($rettype,'operator'.$op);
159     }
160 }
161
162 inline_binops('ex',\@binops_ex);
163 inline_binops('relational',\@binops_relational);
164 foreach $op (@binops_exconstref) {
165     print OUT "// fixes for ex const & operator$op(x,y)\n";
166     inline_single_binop('ex const &',$op,'ex &','','basic const &','ex');
167 }
168
169 print OUT "// fixes for other operators\n";
170 inline_single_unaryop('ex','+');
171 inline_single_unaryop('ex','-');
172 print OUT "inline ostream & operator<<(ostream & os, basic const & x) {\n";
173 print OUT "    return operator<<(os,ex(x));\n";
174 print OUT "}\n";
175
176 print OUT "// fixes for functions\n";
177 inline_function_2p('bool','are_ex_trivially_equal');
178 inline_function_1p('unsigned','nops');
179 inline_function_1p('ex','expand');
180 inline_function_2p('bool','has');
181 inline_single_function_2p('int','degree','basic const &','ex','symbol const &','');
182 inline_single_function_2p('int','ldegree','basic const &','ex','symbol const &','');
183 inline_single_function_3p_with_defarg('ex','coeff','basic const &','ex','symbol const &','','int','1');
184 inline_function_1p('ex','numer');
185 inline_function_1p('ex','denom');
186 inline_single_function_2p_with_defarg('ex','normal','int','0');
187 inline_single_function_2p('ex','collect','basic const &','ex','symbol const &','');
188 inline_single_function_2p_with_defarg('ex','eval','int','0');
189 inline_single_function_2p_with_defarg('ex','evalf','int','0');
190 inline_single_function_3p_with_defarg('ex','diff','basic const &','ex','symbol const &','','int','1');
191 inline_single_function_3p('ex','series','const basic &','ex','const relational &','ex','int','');
192 inline_single_function_3p('ex','series','const basic &','ex','const symbol &','ex','int','');
193 inline_function_2p('ex','subs');
194 inline_single_function_3p('ex','subs','basic const &','ex','lst const &','','lst const &','');
195 inline_single_function_2p('ex','op','basic const &','ex','int','');
196 inline_function_1p('ex','lhs');
197 inline_function_1p('ex','rhs');
198 inline_function_1p('bool','is_zero');
199
200 # fixes for simp_lor.h
201 inline_function_2p('simp_lor','lor_g');
202 inline_single_function_2p('simp_lor','lor_vec','const string &','','const basic &','ex');
203
204 close OUT;
205
206 # Create a file containing stubs that may be necessary because Cint always
207 # wants to link against anything that was ever declared:
208
209 open OUT,">$source";
210
211 $opening=<<END_OF_OPENING;
212 /*  dummies.cpp
213  *
214  *  Dummies and stubs to overcome certain deficiencies of Cint.
215  *  This file was generated automatically by dummies.pl.
216  *  Please do not modify it directly, edit the perl script instead!
217  */
218
219 #include <ginac/function.h>
220
221 #ifndef NO_NAMESPACE_GINAC
222 using namespace GiNaC;
223 #endif // ndef NO_NAMESPACE_GINAC
224
225 END_OF_OPENING
226
227 print OUT $opening;
228 print OUT "void ginsh_get_ginac_functions(void) { }\n";
229
230 close OUT;
231
232 # Create dummies