1 /** @file check_inifcns.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 /* Some tests on the sine trigonometric function. */
27 static unsigned inifcns_consist_sin(void)
30 bool errorflag = false;
34 for (int n=-10; n<=10; ++n) {
35 if (sin(n*Pi).eval() != numeric(0) ||
36 !sin(n*Pi).eval().info(info_flags::integer))
40 // we don't count each of those errors
41 clog << "sin(n*Pi) with integer n does not always return exact 0"
46 // sin((n+1/2)*Pi) == {+|-}1?
48 for (int n=-10; n<=10; ++n) {
49 if (!sin((n+numeric(1,2))*Pi).eval().info(info_flags::integer) ||
50 !(sin((n+numeric(1,2))*Pi).eval() == numeric(1) ||
51 sin((n+numeric(1,2))*Pi).eval() == numeric(-1)))
55 clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
60 // compare sin((q*Pi).evalf()) with sin(q*Pi).eval().evalf() at various
61 // points. E.g. if sin(Pi/10) returns something symbolic this should be
62 // equal to sqrt(5)/4-1/4. This routine will spot programming mistakes
66 numeric epsilon(double(1e-8));
67 for (int n=-340; n<=340; ++n) {
69 if (abs(sin(evalf(argument))-evalf(sin(argument)))>epsilon) {
70 clog << "sin(" << argument << ") returns "
71 << sin(argument) << endl;
82 /* Simple tests on the cosine trigonometric function. */
83 static unsigned inifcns_consist_cos(void)
88 // cos((n+1/2)*Pi) == 0?
90 for (int n=-10; n<=10; ++n) {
91 if (cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
92 !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer))
96 clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
103 for (int n=-10; n<=10; ++n) {
104 if (!cos(n*Pi).eval().info(info_flags::integer) ||
105 !(cos(n*Pi).eval() == numeric(1) ||
106 cos(n*Pi).eval() == numeric(-1)))
110 clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
115 // compare cos((q*Pi).evalf()) with cos(q*Pi).eval().evalf() at various
116 // points. E.g. if cos(Pi/12) returns something symbolic this should be
117 // equal to 1/4*(1+1/3*sqrt(3))*sqrt(6). This routine will spot
118 // programming mistakes of this kind:
121 numeric epsilon(double(1e-8));
122 for (int n=-340; n<=340; ++n) {
124 if (abs(cos(evalf(argument))-evalf(cos(argument)))>epsilon) {
125 clog << "cos(" << argument << ") returns "
126 << cos(argument) << endl;
137 /* Simple tests on the tangent trigonometric function. */
138 static unsigned inifcns_consist_tan(void)
143 // compare tan((q*Pi).evalf()) with tan(q*Pi).eval().evalf() at various
144 // points. E.g. if tan(Pi/12) returns something symbolic this should be
145 // equal to 2-sqrt(3). This routine will spot programming mistakes of
149 numeric epsilon(double(1e-8));
150 for (int n=-340; n<=340; ++n) {
151 if (!(n%30) && (n%60)) // skip poles
154 if (abs(tan(evalf(argument))-evalf(tan(argument)))>epsilon) {
155 clog << "tan(" << argument << ") returns "
156 << tan(argument) << endl;
167 /* Assorted tests on other transcendental functions. */
168 static unsigned inifcns_consist_trans(void)
174 chk = asin(1)-acos(0);
175 if (!chk.is_zero()) {
176 clog << "asin(1)-acos(0) erroneously returned " << chk
177 << " instead of 0" << endl;
181 // arbitrary check of type sin(f(x)):
182 chk = pow(sin(acos(x)),2) + pow(sin(asin(x)),2)
183 - (1+pow(x,2))*pow(sin(atan(x)),2);
184 if (chk != 1-pow(x,2)) {
185 clog << "sin(acos(x))^2 + sin(asin(x))^2 - (1+x^2)*sin(atan(x))^2 "
186 << "erroneously returned " << chk << " instead of 1-x^2" << endl;
190 // arbitrary check of type cos(f(x)):
191 chk = pow(cos(acos(x)),2) + pow(cos(asin(x)),2)
192 - (1+pow(x,2))*pow(cos(atan(x)),2);
193 if (!chk.is_zero()) {
194 clog << "cos(acos(x))^2 + cos(asin(x))^2 - (1+x^2)*cos(atan(x))^2 "
195 << "erroneously returned " << chk << " instead of 0" << endl;
199 // arbitrary check of type tan(f(x)):
200 chk = tan(acos(x))*tan(asin(x)) - tan(atan(x));
202 clog << "tan(acos(x))*tan(asin(x)) - tan(atan(x)) "
203 << "erroneously returned " << chk << " instead of -x+1" << endl;
207 // arbitrary check of type sinh(f(x)):
208 chk = -pow(sinh(acosh(x)),2).expand()*pow(sinh(atanh(x)),2)
209 - pow(sinh(asinh(x)),2);
210 if (!chk.is_zero()) {
211 clog << "expand(-(sinh(acosh(x)))^2)*(sinh(atanh(x))^2) - sinh(asinh(x))^2 "
212 << "erroneously returned " << chk << " instead of 0" << endl;
216 // arbitrary check of type cosh(f(x)):
217 chk = (pow(cosh(asinh(x)),2) - 2*pow(cosh(acosh(x)),2))
218 * pow(cosh(atanh(x)),2);
220 clog << "(cosh(asinh(x))^2 - 2*cosh(acosh(x))^2) * cosh(atanh(x))^2 "
221 << "erroneously returned " << chk << " instead of 1" << endl;
225 // arbitrary check of type tanh(f(x)):
226 chk = (pow(tanh(asinh(x)),-2) - pow(tanh(acosh(x)),2)).expand()
227 * pow(tanh(atanh(x)),2);
229 clog << "expand(tanh(acosh(x))^2 - tanh(asinh(x))^(-2)) * tanh(atanh(x))^2 "
230 << "erroneously returned " << chk << " instead of 2" << endl;
237 /* Simple tests on the Gamma function. We stuff in arguments where the results
238 * exists in closed form and check if it's ok. */
239 static unsigned inifcns_consist_gamma(void)
245 for (int i=2; i<8; ++i)
247 if (e != numeric(874)) {
248 clog << "gamma(1)+...+gamma(7) erroneously returned "
249 << e << " instead of 874" << endl;
254 for (int i=2; i<8; ++i)
256 if (e != numeric(24883200)) {
257 clog << "gamma(1)*...*gamma(7) erroneously returned "
258 << e << " instead of 24883200" << endl;
262 e = gamma(ex(numeric(5, 2)))*gamma(ex(numeric(9, 2)))*64;
264 clog << "64*gamma(5/2)*gamma(9/2) erroneously returned "
265 << e << " instead of 315*Pi" << endl;
269 e = gamma(ex(numeric(-13, 2)));
270 for (int i=-13; i<7; i=i+2)
271 e += gamma(ex(numeric(i, 2)));
272 e = (e*gamma(ex(numeric(15, 2)))*numeric(512));
273 if (e != numeric(633935)*Pi) {
274 clog << "512*(gamma(-13/2)+...+gamma(5/2))*gamma(15/2) erroneously returned "
275 << e << " instead of 633935*Pi" << endl;
282 /* Simple tests on the Psi-function (aka polygamma-function). We stuff in
283 arguments where the result exists in closed form and check if it's ok. */
284 static unsigned inifcns_consist_psi(void)
290 // We check psi(1) and psi(1/2) implicitly by calculating the curious
291 // little identity gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) == 2*log(2).
292 e += (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1));
293 e -= (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1,2));
295 clog << "gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) erroneously returned "
296 << e << " instead of 2*log(2)" << endl;
303 /* Simple tests on the Riemann Zeta function. We stuff in arguments where the
304 * result exists in closed form and check if it's ok. Of course, this checks
305 * the Bernoulli numbers as a side effect. */
306 static unsigned inifcns_consist_zeta(void)
311 for (int i=0; i<13; i+=2)
312 e += zeta(i)/pow(Pi,i);
313 if (e!=numeric(-204992279,638512875)) {
314 clog << "zeta(0) + zeta(2) + ... + zeta(12) erroneously returned "
315 << e << " instead of -204992279/638512875" << endl;
320 for (int i=-1; i>-16; i--)
322 if (e!=numeric(487871,1633632)) {
323 clog << "zeta(-1) + zeta(-2) + ... + zeta(-15) erroneously returned "
324 << e << " instead of 487871/1633632" << endl;
331 unsigned check_inifcns(void)
335 cout << "checking consistency of symbolic functions" << flush;
336 clog << "---------consistency of symbolic functions:" << endl;
338 result += inifcns_consist_sin(); cout << '.' << flush;
339 result += inifcns_consist_cos(); cout << '.' << flush;
340 result += inifcns_consist_tan(); cout << '.' << flush;
341 result += inifcns_consist_trans(); cout << '.' << flush;
342 result += inifcns_consist_gamma(); cout << '.' << flush;
343 result += inifcns_consist_psi(); cout << '.' << flush;
344 result += inifcns_consist_zeta(); cout << '.' << flush;
347 cout << " passed " << endl;
348 clog << "(no output)" << endl;
350 cout << " failed " << endl;