1 /** @file inifcns_consist.cpp
3 * This test routine applies assorted tests on initially known higher level
7 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
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.
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.
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
26 #ifndef NO_NAMESPACE_GINAC
27 using namespace GiNaC;
28 #endif // ndef NO_NAMESPACE_GINAC
30 /* Some tests on the sine trigonometric function. */
31 static unsigned inifcns_consist_sin(void)
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))
44 // we don't count each of those errors
45 clog << "sin(n*Pi) with integer n does not always return exact 0"
50 // sin((n+1/2)*Pi) == {+|-}1?
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)))
59 clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
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
70 numeric epsilon(double(1e-8));
71 for (int n=-240; n<=240; ++n) {
73 if (abs(sin(evalf(argument))-evalf(sin(argument)))>epsilon) {
74 clog << "sin(" << argument << ") returns "
75 << sin(argument) << endl;
86 /* Simple tests on the cosine trigonometric function. */
87 static unsigned inifcns_consist_cos(void)
92 // cos((n+1/2)*Pi) == 0?
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))
100 clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
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)))
114 clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
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:
125 numeric epsilon(double(1e-8));
126 for (int n=-240; n<=240; ++n) {
128 if (abs(cos(evalf(argument))-evalf(cos(argument)))>epsilon) {
129 clog << "cos(" << argument << ") returns "
130 << cos(argument) << endl;
141 /* Simple tests on the tangent trigonometric function. */
142 static unsigned inifcns_consist_tan(void)
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
153 numeric epsilon(double(1e-8));
154 for (int n=-240; n<=240; ++n) {
155 if (!(n%30) && (n%60)) // skip poles
158 if (abs(tan(evalf(argument))-evalf(tan(argument)))>epsilon) {
159 clog << "tan(" << argument << ") returns "
160 << tan(argument) << endl;
171 /* Assorted tests on other transcendental functions. */
172 static unsigned inifcns_consist_trans(void)
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;
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;
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;
203 // arbitrary check of type tan(f(x)):
204 chk = tan(acos(x))*tan(asin(x)) - tan(atan(x));
206 clog << "tan(acos(x))*tan(asin(x)) - tan(atan(x)) "
207 << "erroneously returned " << chk << " instead of -x+1" << endl;
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;
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);
224 clog << "(cosh(asinh(x))^2 - 2*cosh(acosh(x))^2) * cosh(atanh(x))^2 "
225 << "erroneously returned " << chk << " instead of 1" << endl;
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);
233 clog << "expand(tanh(acosh(x))^2 - tanh(asinh(x))^(-2)) * tanh(atanh(x))^2 "
234 << "erroneously returned " << chk << " instead of 2" << endl;
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)
249 for (int i=2; i<8; ++i)
251 if (e != numeric(874)) {
252 clog << "gamma(1)+...+gamma(7) erroneously returned "
253 << e << " instead of 874" << endl;
258 for (int i=2; i<8; ++i)
260 if (e != numeric(24883200)) {
261 clog << "gamma(1)*...*gamma(7) erroneously returned "
262 << e << " instead of 24883200" << endl;
266 e = gamma(ex(numeric(5, 2)))*gamma(ex(numeric(9, 2)))*64;
268 clog << "64*gamma(5/2)*gamma(9/2) erroneously returned "
269 << e << " instead of 315*Pi" << endl;
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;
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)
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));
299 clog << "gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) erroneously returned "
300 << e << " instead of 2*log(2)" << endl;
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)
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;
324 for (int i=-1; i>-16; i--)
326 if (e!=numeric(487871,1633632)) {
327 clog << "zeta(-1) + zeta(-2) + ... + zeta(-15) erroneously returned "
328 << e << " instead of 487871/1633632" << endl;
335 unsigned inifcns_consist(void)
339 cout << "checking consistency of symbolic functions..." << flush;
340 clog << "---------consistency of symbolic functions:" << endl;
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();
352 clog << "(no output)" << endl;