]> www.ginac.de Git - ginac.git/blob - check/inifcns_consist.cpp
- Renamed flag NO_GINAC_NAMESPACE to NO_NAMESPACE_GINAC because of m4.
[ginac.git] / check / inifcns_consist.cpp
1 /** @file inifcns_consist.cpp
2  *
3  *  This test routine applies assorted tests on initially known higher level
4  *  functions. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <ginac/ginac.h>
25
26 #ifndef NO_NAMESPACE_GINAC
27 using namespace GiNaC;
28 #endif // ndef NO_NAMESPACE_GINAC
29
30 /* Some tests on the sine trigonometric function. */
31 static unsigned inifcns_consist_sin(void)
32 {
33     unsigned result = 0;
34     bool errorflag;
35     
36     // sin(n*Pi) == 0?
37     errorflag = false;
38     for (int n=-10; n<=10; ++n) {
39         if (sin(n*Pi).eval() != numeric(0) ||
40             !sin(n*Pi).eval().info(info_flags::integer))
41             errorflag = true;
42     }
43     if (errorflag) {
44         // we don't count each of those errors
45         clog << "sin(n*Pi) with integer n does not always return exact 0"
46              << endl;
47         ++result;
48     }
49     
50     // sin((n+1/2)*Pi) == {+|-}1?
51     errorflag = false;
52     for (int n=-10; n<=10; ++n) {
53         if (!sin((n+numeric(1,2))*Pi).eval().info(info_flags::integer) ||
54             !(sin((n+numeric(1,2))*Pi).eval() == numeric(1) ||
55               sin((n+numeric(1,2))*Pi).eval() == numeric(-1)))
56             errorflag = true;
57     }
58     if (errorflag) {
59         clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
60              << endl;
61         ++result;
62     }
63     
64     // compare sin((q*Pi).evalf()) with sin(q*Pi).eval().evalf() at various
65     // points.  E.g. if sin(Pi/10) returns something symbolic this should be
66     // equal to sqrt(5)/4-1/4.  This routine will spot programming mistakes
67     // of this kind:
68     errorflag = false;
69     ex argument;
70     numeric epsilon(double(1e-8));
71     for (int n=-240; n<=240; ++n) {
72         argument = n*Pi/60;
73         if (abs(sin(evalf(argument))-evalf(sin(argument)))>epsilon) {
74             clog << "sin(" << argument << ") returns "
75                  << sin(argument) << endl;
76             errorflag = true;
77         }
78     }
79     if (errorflag) {
80         ++result;
81     }
82     
83     return result;
84 }
85
86 /* Simple tests on the cosine trigonometric function. */
87 static unsigned inifcns_consist_cos(void)
88 {
89     unsigned result = 0;
90     bool errorflag;
91     
92     // cos((n+1/2)*Pi) == 0?
93     errorflag = false;
94     for (int n=-10; n<=10; ++n) {
95         if (cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
96             !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer))
97             errorflag = true;
98     }
99     if (errorflag) {
100         clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
101              << endl;
102         ++result;
103     }
104     
105     // cos(n*Pi) == 0?
106     errorflag = false;
107     for (int n=-10; n<=10; ++n) {
108         if (!cos(n*Pi).eval().info(info_flags::integer) ||
109             !(cos(n*Pi).eval() == numeric(1) ||
110               cos(n*Pi).eval() == numeric(-1)))
111             errorflag = true;
112     }
113     if (errorflag) {
114         clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
115              << endl;
116         ++result;
117     }
118     
119     // compare cos((q*Pi).evalf()) with cos(q*Pi).eval().evalf() at various
120     // points.  E.g. if cos(Pi/12) returns something symbolic this should be
121     // equal to 1/4*(1+1/3*sqrt(3))*sqrt(6).  This routine will spot
122     // programming mistakes of this kind:
123     errorflag = false;
124     ex argument;
125     numeric epsilon(double(1e-8));
126     for (int n=-240; n<=240; ++n) {
127         argument = n*Pi/60;
128         if (abs(cos(evalf(argument))-evalf(cos(argument)))>epsilon) {
129             clog << "cos(" << argument << ") returns "
130                  << cos(argument) << endl;
131             errorflag = true;
132         }
133     }
134     if (errorflag) {
135         ++result;
136     }
137     
138     return result;
139 }
140
141 /* Simple tests on the tangent trigonometric function. */
142 static unsigned inifcns_consist_tan(void)
143 {
144     unsigned result = 0;
145     bool errorflag;
146     
147     // compare tan((q*Pi).evalf()) with tan(q*Pi).eval().evalf() at various
148     // points.  E.g. if tan(Pi/12) returns something symbolic this should be
149     // equal to 2-sqrt(3).  This routine will spot programming mistakes of 
150     // this kind:
151     errorflag = false;
152     ex argument;
153     numeric epsilon(double(1e-8));
154     for (int n=-240; n<=240; ++n) {
155         if (!(n%30) && (n%60))  // skip poles
156             ++n;
157         argument = n*Pi/60;
158         if (abs(tan(evalf(argument))-evalf(tan(argument)))>epsilon) {
159             clog << "tan(" << argument << ") returns "
160                  << tan(argument) << endl;
161             errorflag = true;
162         }
163     }
164     if (errorflag) {
165         ++result;
166     }
167     
168     return result;
169 }
170
171 /* Assorted tests on other transcendental functions. */
172 static unsigned inifcns_consist_trans(void)
173 {
174     unsigned result = 0;
175     symbol x("x");
176     ex chk;
177     
178     chk = asin(1)-acos(0);
179     if (!chk.is_zero()) {
180         clog << "asin(1)-acos(0) erroneously returned " << chk
181              << " instead of 0" << endl;
182         ++result;
183     }
184     
185     // arbitrary check of type sin(f(x)):
186     chk = pow(sin(acos(x)),2) + pow(sin(asin(x)),2)
187         - (1+pow(x,2))*pow(sin(atan(x)),2);
188     if (chk != 1-pow(x,2)) {
189         clog << "sin(acos(x))^2 + sin(asin(x))^2 - (1+x^2)*sin(atan(x))^2 "
190              << "erroneously returned " << chk << " instead of 1-x^2" << endl;
191         ++result;
192     }
193     
194     // arbitrary check of type cos(f(x)):
195     chk = pow(cos(acos(x)),2) + pow(cos(asin(x)),2)
196         - (1+pow(x,2))*pow(cos(atan(x)),2);
197     if (!chk.is_zero()) {
198         clog << "cos(acos(x))^2 + cos(asin(x))^2 - (1+x^2)*cos(atan(x))^2 "
199              << "erroneously returned " << chk << " instead of 0" << endl;
200         ++result;
201     }
202     
203     // arbitrary check of type tan(f(x)):
204     chk = tan(acos(x))*tan(asin(x)) - tan(atan(x));
205     if (chk != 1-x) {
206         clog << "tan(acos(x))*tan(asin(x)) - tan(atan(x)) "
207              << "erroneously returned " << chk << " instead of -x+1" << endl;
208         ++result;
209     }
210     
211     // arbitrary check of type sinh(f(x)):
212     chk = -pow(sinh(acosh(x)),2).expand()*pow(sinh(atanh(x)),2)
213         - pow(sinh(asinh(x)),2);
214     if (!chk.is_zero()) {
215         clog << "expand(-(sinh(acosh(x)))^2)*(sinh(atanh(x))^2) - sinh(asinh(x))^2 "
216              << "erroneously returned " << chk << " instead of 0" << endl;
217         ++result;
218     }
219     
220     // arbitrary check of type cosh(f(x)):
221     chk = (pow(cosh(asinh(x)),2) - 2*pow(cosh(acosh(x)),2))
222         * pow(cosh(atanh(x)),2);
223     if (chk != 1) {
224         clog << "(cosh(asinh(x))^2 - 2*cosh(acosh(x))^2) * cosh(atanh(x))^2 "
225              << "erroneously returned " << chk << " instead of 1" << endl;
226         ++result;
227     }
228     
229     // arbitrary check of type tanh(f(x)):
230     chk = (pow(tanh(asinh(x)),-2) - pow(tanh(acosh(x)),2)).expand()
231         * pow(tanh(atanh(x)),2);
232     if (chk != 2) {
233         clog << "expand(tanh(acosh(x))^2 - tanh(asinh(x))^(-2)) * tanh(atanh(x))^2 "
234              << "erroneously returned " << chk << " instead of 2" << endl;
235         ++result;
236     }
237     
238     return result;
239 }
240
241 /* Simple tests on the Gamma function.  We stuff in arguments where the results
242  * exists in closed form and check if it's ok. */
243 static unsigned inifcns_consist_gamma(void)
244 {
245     unsigned result = 0;
246     ex e;
247     
248     e = gamma(ex(1));
249     for (int i=2; i<8; ++i)
250         e += gamma(ex(i));
251     if (e != numeric(874)) {
252         clog << "gamma(1)+...+gamma(7) erroneously returned "
253              << e << " instead of 874" << endl;
254         ++result;
255     }
256     
257     e = gamma(ex(1));
258     for (int i=2; i<8; ++i)
259         e *= gamma(ex(i));    
260     if (e != numeric(24883200)) {
261         clog << "gamma(1)*...*gamma(7) erroneously returned "
262              << e << " instead of 24883200" << endl;
263         ++result;
264     }
265     
266     e = gamma(ex(numeric(5, 2)))*gamma(ex(numeric(9, 2)))*64;
267     if (e != 315*Pi) {
268         clog << "64*gamma(5/2)*gamma(9/2) erroneously returned "
269              << e << " instead of 315*Pi" << endl;
270         ++result;
271     }
272     
273     e = gamma(ex(numeric(-13, 2)));
274     for (int i=-13; i<7; i=i+2)
275         e += gamma(ex(numeric(i, 2)));
276     e = (e*gamma(ex(numeric(15, 2)))*numeric(512));
277     if (e != numeric(633935)*Pi) {
278         clog << "512*(gamma(-13/2)+...+gamma(5/2))*gamma(15/2) erroneously returned "
279              << e << " instead of 633935*Pi" << endl;
280         ++result;
281     }
282     
283     return result;
284 }
285
286 /* Simple tests on the Psi-function (aka polygamma-function).  We stuff in
287    arguments where the result exists in closed form and check if it's ok. */
288 static unsigned inifcns_consist_psi(void)
289 {
290     unsigned result = 0;
291     symbol x;
292     ex e, f;
293     
294     // We check psi(1) and psi(1/2) implicitly by calculating the curious
295     // little identity gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) == 2*log(2).
296     e += (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1));
297     e -= (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1,2));
298     if (e!=2*log(2)) {
299         clog << "gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) erroneously returned "
300              << e << " instead of 2*log(2)" << endl;
301         ++result;
302     }
303     
304     return result;
305 }
306
307 /* Simple tests on the Riemann Zeta function.  We stuff in arguments where the
308  * result exists in closed form and check if it's ok.  Of course, this checks
309  * the Bernoulli numbers as a side effect. */
310 static unsigned inifcns_consist_zeta(void)
311 {
312     unsigned result = 0;
313     ex e;
314     
315     for (int i=0; i<13; i+=2)
316         e += zeta(i)/pow(Pi,i);
317     if (e!=numeric(-204992279,638512875)) {
318         clog << "zeta(0) + zeta(2) + ... + zeta(12) erroneously returned "
319              << e << " instead of -204992279/638512875" << endl;
320         ++result;
321     }
322     
323     e = 0;
324     for (int i=-1; i>-16; i--)
325         e += zeta(i);
326     if (e!=numeric(487871,1633632)) {
327         clog << "zeta(-1) + zeta(-2) + ... + zeta(-15) erroneously returned "
328              << e << " instead of 487871/1633632" << endl;
329         ++result;
330     }
331     
332     return result;
333 }
334
335 unsigned inifcns_consist(void)
336 {
337     unsigned result = 0;
338
339     cout << "checking consistency of symbolic functions..." << flush;
340     clog << "---------consistency of symbolic functions:" << endl;
341     
342     result += inifcns_consist_sin();
343     result += inifcns_consist_cos();
344     result += inifcns_consist_tan();
345     result += inifcns_consist_trans();
346     result += inifcns_consist_gamma();
347     result += inifcns_consist_psi();
348     result += inifcns_consist_zeta();
349
350     if (!result) {
351         cout << " passed ";
352         clog << "(no output)" << endl;
353     } else {
354         cout << " failed ";
355     }
356     
357     return result;
358 }