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