]> www.ginac.de Git - ginac.git/blob - check/exam_inifcns.cpp
utils.h: use <stdint.h> (if available) instead of reinventing it.
[ginac.git] / check / exam_inifcns.cpp
1 /** @file exam_inifcns.cpp
2  *
3  *  This test routine applies assorted tests on initially known higher level
4  *  functions. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #include <iostream>
25 #include "ginac.h"
26 using namespace std;
27 using namespace GiNaC;
28
29 /* Assorted tests on other transcendental functions. */
30 static unsigned inifcns_consist_trans()
31 {
32         using GiNaC::asin; using GiNaC::acos;
33
34         unsigned result = 0;
35         symbol x("x");
36         ex chk;
37         
38         chk = asin(1)-acos(0);
39         if (!chk.is_zero()) {
40                 clog << "asin(1)-acos(0) erroneously returned " << chk
41                      << " instead of 0" << endl;
42                 ++result;
43         }
44         
45         // arbitrary check of type sin(f(x)):
46         chk = pow(sin(acos(x)),2) + pow(sin(asin(x)),2)
47                 - (1+pow(x,2))*pow(sin(atan(x)),2);
48         if (chk != 1-pow(x,2)) {
49                 clog << "sin(acos(x))^2 + sin(asin(x))^2 - (1+x^2)*sin(atan(x))^2 "
50                      << "erroneously returned " << chk << " instead of 1-x^2" << endl;
51                 ++result;
52         }
53         
54         // arbitrary check of type cos(f(x)):
55         chk = pow(cos(acos(x)),2) + pow(cos(asin(x)),2)
56                 - (1+pow(x,2))*pow(cos(atan(x)),2);
57         if (!chk.is_zero()) {
58                 clog << "cos(acos(x))^2 + cos(asin(x))^2 - (1+x^2)*cos(atan(x))^2 "
59                      << "erroneously returned " << chk << " instead of 0" << endl;
60                 ++result;
61         }
62         
63         // arbitrary check of type tan(f(x)):
64         chk = tan(acos(x))*tan(asin(x)) - tan(atan(x));
65         if (chk != 1-x) {
66                 clog << "tan(acos(x))*tan(asin(x)) - tan(atan(x)) "
67                      << "erroneously returned " << chk << " instead of -x+1" << endl;
68                 ++result;
69         }
70         
71         // arbitrary check of type sinh(f(x)):
72         chk = -pow(sinh(acosh(x)),2).expand()*pow(sinh(atanh(x)),2)
73                 - pow(sinh(asinh(x)),2);
74         if (!chk.is_zero()) {
75                 clog << "expand(-(sinh(acosh(x)))^2)*(sinh(atanh(x))^2) - sinh(asinh(x))^2 "
76                      << "erroneously returned " << chk << " instead of 0" << endl;
77                 ++result;
78         }
79         
80         // arbitrary check of type cosh(f(x)):
81         chk = (pow(cosh(asinh(x)),2) - 2*pow(cosh(acosh(x)),2))
82                 * pow(cosh(atanh(x)),2);
83         if (chk != 1) {
84                 clog << "(cosh(asinh(x))^2 - 2*cosh(acosh(x))^2) * cosh(atanh(x))^2 "
85                      << "erroneously returned " << chk << " instead of 1" << endl;
86                 ++result;
87         }
88         
89         // arbitrary check of type tanh(f(x)):
90         chk = (pow(tanh(asinh(x)),-2) - pow(tanh(acosh(x)),2)).expand()
91                 * pow(tanh(atanh(x)),2);
92         if (chk != 2) {
93                 clog << "expand(tanh(acosh(x))^2 - tanh(asinh(x))^(-2)) * tanh(atanh(x))^2 "
94                      << "erroneously returned " << chk << " instead of 2" << endl;
95                 ++result;
96         }
97         
98         // check consistency of log and eta phases:
99         for (int r1=-1; r1<=1; ++r1) {
100                 for (int i1=-1; i1<=1; ++i1) {
101                         ex x1 = r1+I*i1;
102                         if (x1.is_zero())
103                                 continue;
104                         for (int r2=-1; r2<=1; ++r2) {
105                                 for (int i2=-1; i2<=1; ++i2) {
106                                         ex x2 = r2+I*i2;
107                                         if (x2.is_zero())
108                                                 continue;
109                                         if (abs(evalf(eta(x1,x2)-log(x1*x2)+log(x1)+log(x2)))>.1e-12) {
110                                                 clog << "either eta(x,y), log(x), log(y) or log(x*y) is wrong"
111                                                      << " at x==" << x1 << ", y==" << x2 << endl;
112                                                 ++result;
113                                         }
114                                 }
115                         }
116                 }
117         }
118                 
119         return result;
120 }
121
122 /* Simple tests on the tgamma function.  We stuff in arguments where the results
123  * exists in closed form and check if it's ok. */
124 static unsigned inifcns_consist_gamma()
125 {
126         unsigned result = 0;
127         ex e;
128         
129         e = tgamma(1);
130         for (int i=2; i<8; ++i)
131                 e += tgamma(ex(i));
132         if (e != numeric(874)) {
133                 clog << "tgamma(1)+...+tgamma(7) erroneously returned "
134                      << e << " instead of 874" << endl;
135                 ++result;
136         }
137         
138         e = tgamma(1);
139         for (int i=2; i<8; ++i)
140                 e *= tgamma(ex(i));     
141         if (e != numeric(24883200)) {
142                 clog << "tgamma(1)*...*tgamma(7) erroneously returned "
143                      << e << " instead of 24883200" << endl;
144                 ++result;
145         }
146         
147         e = tgamma(ex(numeric(5, 2)))*tgamma(ex(numeric(9, 2)))*64;
148         if (e != 315*Pi) {
149                 clog << "64*tgamma(5/2)*tgamma(9/2) erroneously returned "
150                      << e << " instead of 315*Pi" << endl;
151                 ++result;
152         }
153         
154         e = tgamma(ex(numeric(-13, 2)));
155         for (int i=-13; i<7; i=i+2)
156                 e += tgamma(ex(numeric(i, 2)));
157         e = (e*tgamma(ex(numeric(15, 2)))*numeric(512));
158         if (e != numeric(633935)*Pi) {
159                 clog << "512*(tgamma(-13/2)+...+tgamma(5/2))*tgamma(15/2) erroneously returned "
160                      << e << " instead of 633935*Pi" << endl;
161                 ++result;
162         }
163         
164         return result;
165 }
166
167 /* Simple tests on the Psi-function (aka polygamma-function).  We stuff in
168    arguments where the result exists in closed form and check if it's ok. */
169 static unsigned inifcns_consist_psi()
170 {
171         using GiNaC::log;
172
173         unsigned result = 0;
174         symbol x;
175         ex e, f;
176         
177         // We check psi(1) and psi(1/2) implicitly by calculating the curious
178         // little identity tgamma(1)'/tgamma(1) - tgamma(1/2)'/tgamma(1/2) == 2*log(2).
179         e += (tgamma(x).diff(x)/tgamma(x)).subs(x==numeric(1));
180         e -= (tgamma(x).diff(x)/tgamma(x)).subs(x==numeric(1,2));
181         if (e!=2*log(2)) {
182                 clog << "tgamma(1)'/tgamma(1) - tgamma(1/2)'/tgamma(1/2) erroneously returned "
183                      << e << " instead of 2*log(2)" << endl;
184                 ++result;
185         }
186         
187         return result;
188 }
189
190 /* Simple tests on the Riemann Zeta function.  We stuff in arguments where the
191  * result exists in closed form and check if it's ok.  Of course, this checks
192  * the Bernoulli numbers as a side effect. */
193 static unsigned inifcns_consist_zeta()
194 {
195         unsigned result = 0;
196         ex e;
197         
198         for (int i=0; i<13; i+=2)
199                 e += zeta(i)/pow(Pi,i);
200         if (e!=numeric(-204992279,638512875)) {
201                 clog << "zeta(0) + zeta(2) + ... + zeta(12) erroneously returned "
202                      << e << " instead of -204992279/638512875" << endl;
203                 ++result;
204         }
205         
206         e = 0;
207         for (int i=-1; i>-16; i--)
208                 e += zeta(i);
209         if (e!=numeric(487871,1633632)) {
210                 clog << "zeta(-1) + zeta(-2) + ... + zeta(-15) erroneously returned "
211                      << e << " instead of 487871/1633632" << endl;
212                 ++result;
213         }
214         
215         return result;
216 }
217
218 unsigned exam_inifcns()
219 {
220         unsigned result = 0;
221         
222         cout << "examining consistency of symbolic functions" << flush;
223         
224         result += inifcns_consist_trans();  cout << '.' << flush;
225         result += inifcns_consist_gamma();  cout << '.' << flush;
226         result += inifcns_consist_psi();  cout << '.' << flush;
227         result += inifcns_consist_zeta();  cout << '.' << flush;
228         
229         return result;
230 }
231
232 int main(int argc, char** argv)
233 {
234         return exam_inifcns();
235 }