]> www.ginac.de Git - ginac.git/blob - check/inifcns_consist.cpp
- tutorial now built with texinfo
[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 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 using namespace GiNaC;
26
27 /* Simple tests on the sine trigonometric function. */
28 static unsigned inifcns_consist_sin(void)
29 {
30     unsigned result = 0;
31     bool errorflag;
32     
33     // sin(n*Pi) == 0?
34     errorflag = false;
35     for (int n=-10; n<=10; ++n) {
36         if ( sin(n*Pi).eval() != numeric(0) ||
37             !sin(n*Pi).eval().info(info_flags::integer))
38             errorflag = true;
39     }
40     if (errorflag) {
41         clog << "sin(n*Pi) with integer n does not always return exact 0"
42              << endl;
43         ++result;
44     }
45     
46     // sin((n+1/2)*Pi) == {+|-}1?
47     errorflag = false;
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)))
52             errorflag = true;
53     }
54     if (errorflag) {
55         clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
56              << endl;
57         ++result;
58     }
59     
60     return result;
61 }
62
63 /* Simple tests on the cosine trigonometric function. */
64 static unsigned inifcns_consist_cos(void)
65 {
66     unsigned result = 0;
67     bool errorflag;
68     
69     // cos((n+1/2)*Pi) == 0?
70     errorflag = false;
71     for (int n=-10; n<=10; ++n) {
72         if ( cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
73             !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer))
74             errorflag = true;
75     }
76     if (errorflag) {
77         clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
78              << endl;
79         ++result;
80     }
81     
82     // cos(n*Pi) == 0?
83     errorflag = false;
84     for (int n=-10; n<=10; ++n) {
85         if (! cos(n*Pi).eval().info(info_flags::integer) ||
86             !(cos(n*Pi).eval() == numeric(1) ||
87               cos(n*Pi).eval() == numeric(-1)))
88             errorflag = true;
89     }
90     if (errorflag) {
91         clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
92              << endl;
93         ++result;
94     }
95     
96     return result;
97 }
98
99 /* Assorted tests on other transcendental functions. */
100 static unsigned inifcns_consist_trans(void)
101 {
102     unsigned result = 0;
103     symbol x("x");
104     ex chk;
105     
106     chk = asin(1)-acos(0);
107     if (!chk.is_zero()) {
108         clog << "asin(1)-acos(0) erroneously returned " << chk
109              << " instead of 0" << endl;
110         ++result;
111     }
112     
113     // arbitrary check of type sin(f(x)):
114     chk = pow(sin(acos(x)),2) + pow(sin(asin(x)),2)
115         - (1+pow(x,2))*pow(sin(atan(x)),2);
116     if (chk != 1-pow(x,2)) {
117         clog << "sin(acos(x))^2 + sin(asin(x))^2 - (1+x^2)*sin(atan(x))^2 "
118              << "erroneously returned " << chk << " instead of 1-x^2" << endl;
119         ++result;
120     }
121     
122     // arbitrary check of type cos(f(x)):
123     chk = pow(cos(acos(x)),2) + pow(cos(asin(x)),2)
124         - (1+pow(x,2))*pow(cos(atan(x)),2);
125     if (!chk.is_zero()) {
126         clog << "cos(acos(x))^2 + cos(asin(x))^2 - (1+x^2)*cos(atan(x))^2 "
127              << "erroneously returned " << chk << " instead of 0" << endl;
128         ++result;
129     }
130     
131     // arbitrary check of type tan(f(x)):
132     chk = tan(acos(x))*tan(asin(x)) - tan(atan(x));
133     if (chk != 1-x) {
134         clog << "tan(acos(x))*tan(asin(x)) - tan(atan(x)) "
135              << "erroneously returned " << chk << " instead of -x+1" << endl;
136         ++result;
137     }
138     
139     // arbitrary check of type sinh(f(x)):
140     chk = -pow(sinh(acosh(x)),2).expand()*pow(sinh(atanh(x)),2)
141         - pow(sinh(asinh(x)),2);
142     if (!chk.is_zero()) {
143         clog << "expand(-(sinh(acosh(x)))^2)*(sinh(atanh(x))^2) - sinh(asinh(x))^2 "
144              << "erroneously returned " << chk << " instead of 0" << endl;
145         ++result;
146     }
147     
148     // arbitrary check of type cosh(f(x)):
149     chk = (pow(cosh(asinh(x)),2) - 2*pow(cosh(acosh(x)),2))
150         * pow(cosh(atanh(x)),2);
151     if (chk != 1) {
152         clog << "(cosh(asinh(x))^2 - 2*cosh(acosh(x))^2) * cosh(atanh(x))^2 "
153              << "erroneously returned " << chk << " instead of 1" << endl;
154         ++result;
155     }
156     
157     // arbitrary check of type tanh(f(x)):
158     chk = (pow(tanh(asinh(x)),-2) - pow(tanh(acosh(x)),2)).expand()
159         * pow(tanh(atanh(x)),2);
160     if (chk != 2) {
161         clog << "expand(tanh(acosh(x))^2 - tanh(asinh(x))^(-2)) * tanh(atanh(x))^2 "
162              << "erroneously returned " << chk << " instead of 2" << endl;
163         ++result;
164     }
165     
166     return result;
167 }
168
169 /* Simple tests on the Gamma combinatorial function.  We stuff in arguments
170  * where the result exists in closed form and check if it's ok. */
171 static unsigned inifcns_consist_gamma(void)
172 {
173     unsigned result = 0;
174     ex e;
175     
176     e = gamma(ex(1));
177     for (int i=2; i<8; ++i)
178         e += gamma(ex(i));
179     if (e != numeric(874)) {
180         clog << "gamma(1)+...+gamma(7) erroneously returned "
181              << e << " instead of 874" << endl;
182         ++result;
183     }
184     
185     e = gamma(ex(1));
186     for (int i=2; i<8; ++i)
187         e *= gamma(ex(i));    
188     if (e != numeric(24883200)) {
189         clog << "gamma(1)*...*gamma(7) erroneously returned "
190              << e << " instead of 24883200" << endl;
191         ++result;
192     }
193     
194     e = gamma(ex(numeric(5, 2)))*gamma(ex(numeric(9, 2)))*64;
195     if (e != 315*Pi) {
196         clog << "64*gamma(5/2)*gamma(9/2) erroneously returned "
197              << e << " instead of 315*Pi" << endl;
198         ++result;
199     }
200     
201     e = gamma(ex(numeric(-13, 2)));
202     for (int i=-13; i<7; i=i+2)
203         e += gamma(ex(numeric(i, 2)));
204     e = (e*gamma(ex(numeric(15, 2)))*numeric(512));
205     if (e != numeric(633935)*Pi) {
206         clog << "512*(gamma(-13/2)+...+gamma(5/2))*gamma(15/2) erroneously returned "
207              << e << " instead of 633935*Pi" << endl;
208         ++result;
209     }
210     
211     return result;
212 }
213
214 /* Simple tests on the Riemann Zeta function.  We stuff in arguments where the
215  * result exists in closed form and check if it's ok.  Of course, this checks
216  * the Bernoulli numbers as a side effect. */
217 static unsigned inifcns_consist_zeta(void)
218 {
219     unsigned result = 0;
220     ex e;
221     
222     for (int i=0; i<13; i+=2)
223         e += zeta(i)/pow(Pi,i);
224     if (e!=numeric(-204992279,638512875)) {
225         clog << "zeta(0) + zeta(2) + ... + zeta(12) erroneously returned "
226              << e << " instead of -204992279/638512875" << endl;
227         ++result;
228     }
229     
230     e = 0;
231     for (int i=-1; i>-16; i--)
232         e += zeta(i);
233     if (e!=numeric(487871,1633632)) {
234         clog << "zeta(-1) + zeta(-2) + ... + zeta(-15) erroneously returned "
235              << e << " instead of 487871/1633632" << endl;
236         ++result;
237     }
238     
239     return result;
240 }
241
242 unsigned inifcns_consist(void)
243 {
244     unsigned result = 0;
245
246     cout << "checking consistency of symbolic functions..." << flush;
247     clog << "---------consistency of symbolic functions:" << endl;
248     
249     result += inifcns_consist_sin();
250     result += inifcns_consist_cos();
251     result += inifcns_consist_trans();
252     result += inifcns_consist_gamma();
253     result += inifcns_consist_zeta();
254
255     if ( !result ) {
256         cout << " passed ";
257         clog << "(no output)" << endl;
258     } else {
259         cout << " failed ";
260     }
261     
262     return result;
263 }