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