]> www.ginac.de Git - ginac.git/blob - check/exam_inifcns_nstdsums.cpp
Fixed bug in gcd (patch from Sheplyakov Alexei).
[ginac.git] / check / exam_inifcns_nstdsums.cpp
1 /** @file exam_inifcns_nstdsums.cpp
2  *
3  *  This test routine applies assorted tests on initially known higher level
4  *  functions. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2005 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 "exams.h"
25
26 #include <fstream>
27
28
29 ////////////////////////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////////////////////////
31 //  S exam
32 ////////////////////////////////////////////////////////////////////////////////
33 ////////////////////////////////////////////////////////////////////////////////
34
35
36 /*
37  * The data in the following include file has been produced by the following
38  * Mathematica (V4.1) script:
39  *
40  *
41  *    x={2/10,1,14/10,30/10}
42  *    y={0,3/10,-14/10}
43  *    st = OpenAppend["exam_inifcns_nstdsums_data.raw"]
44  *    $NumberMarks = False
45  *    Do[
46  *      Do[
47  *        Do[Write[st, i]; Write[st,j]; Write[st,x[[k]]+I*y[[l]]];
48  *          Write[st,Chop[N[PolyLog[i,j,x[[k]]+I*y[[l]]],25]]],{i,3},{j,3}], {k,4}],{l,3}]
49  *    Do[
50  *      Do[
51  *        Do[Write[st, i]; Write[st,j]; Write[st,-x[[k]]+I*y[[l]]];
52  *          Write[st,Chop[N[PolyLog[i,j,-x[[k]]+I*y[[l]]],25]]],{i,3},{j,3}], {k,4}], {l,3}]
53  *    Close[st]
54  *
55  *    
56  * and postprocessed by the following shell script
57  *
58  *
59  *    #/bin/sh
60  *    IFS=$'\n'
61  *    cat exam_inifcns_nstdsums_data.raw | sed -e 's/\*\^/E/g' > exam_inifcns_nstdsums_data.raw2
62  *    echo 'const char *data[] = {' > exam_inifcns_nstdsums_data.raw3
63  *    for i in `cat exam_inifcns_nstdsums_data.raw2`; do echo \"$i\",; done >> exam_inifcns_nstdsums_data.raw3
64  *    echo '"-999"};' >> exam_inifcns_nstdsums.h
65  *
66  *
67  */
68 #include "exam_inifcns_nstdsums.h"
69
70
71 // signals end of data
72 const int ENDMARK = -999;
73
74
75 static unsigned inifcns_test_S()
76 {
77         int digitsbuf = Digits;
78         // precision of data
79         Digits = 22;
80         ex prec = 5 * pow(10, -(int)Digits);
81         
82         unsigned result = 0;
83         
84         int i = 0;
85         while (true) {
86                 ex n(data[i++],symbol());
87                 if (n == ENDMARK) {
88                         break;
89                 }
90                 ex p(data[i++],symbol());
91                 ex x(data[i++],symbol());
92                 ex res(data[i++],symbol());
93                 ex res2 = S(n, p, x).evalf();
94                 if (abs(res-res2) > prec) {
95                         clog << "S(" << n << "," << p << "," << x << ") seems to be wrong:" << endl;
96                         clog << "GiNaC           : " << res2 << endl;
97                         clog << "Reference       : " << res << endl;
98                         clog << "Abs. Difference : " << res2-res << endl;
99                         if (res2 != 0) {
100                                 ex reldiff = abs((res2-res)/res2);
101                                 clog << "Rel. Difference : " << reldiff << endl;
102                         }
103                         result++;
104                 }
105                 if (i % 80) {
106                         cout << "." << flush;
107                 }
108         }
109
110         Digits = digitsbuf;
111
112         return result;
113 }
114
115
116 ////////////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////////////
118 //  H/Li exam
119 ////////////////////////////////////////////////////////////////////////////////
120 ////////////////////////////////////////////////////////////////////////////////
121
122
123 static unsigned inifcns_test_HLi()
124 {
125         int digitsbuf = Digits;
126         Digits = 17;
127         ex prec = 5 * pow(10, -(int)Digits);
128         numeric almostone("0.999999999999999999");
129         unsigned result = 0;
130
131         lst res;
132         
133         res.append(H(lst(2,1),numeric(1)/2).hold() - (zeta(3)/8 - pow(log(2),3)/6));
134         res.append(H(lst(2,1,3),numeric(1)/3).hold() - Li(lst(2,1,3),lst(numeric(1)/3,1,1)).hold());
135         res.append(H(lst(2,1,3),numeric(98)/100).hold() - Li(lst(2,1,3),lst(numeric(98)/100,1,1)).hold());
136         res.append(H(lst(2,1,3),numeric(245)/100).hold() - Li(lst(2,1,3),lst(numeric(245)/100,1,1)).hold());
137         res.append(H(lst(4,1,1,1),numeric(1)/3).hold() - S(3,4,numeric(1)/3).hold());
138         res.append(H(lst(4,1,1,1),numeric(98)/100).hold() - S(3,4,numeric(98)/100).hold());
139         res.append(H(lst(4,1,1,1),numeric(245)/100).hold() - S(3,4,numeric(245)/100).hold());
140         res.append(H(lst(2,2,3),almostone).hold() - zeta(lst(2,2,3)));
141         res.append(H(lst(-3,-1,2,1),almostone).hold() - zeta(lst(3,1,2,1),lst(-1,1,-1,1)));
142         res.append(H(lst(-2,1,3),numeric(1)/3).hold() - -Li(lst(2,1,3),lst(-numeric(1)/3,-1,1)).hold());
143         res.append(H(lst(-2,1,3),numeric(98)/100).hold() - -Li(lst(2,1,3),lst(-numeric(98)/100,-1,1)).hold());
144         res.append(H(lst(-2,1,3),numeric(245)/100).hold() - -Li(lst(2,1,3),lst(-numeric(245)/100,-1,1)).hold());
145         res.append(H(lst(-3,1,-2,0,0),numeric(3)/10).hold() - convert_H_to_Li(lst(-3,1,-2,0,0),numeric(3)/10).eval());
146         
147         for (lst::const_iterator it = res.begin(); it != res.end(); it++) {
148                 ex diff = abs((*it).evalf());
149                 if (diff > prec) {
150                         clog << *it << " seems to be wrong: " << diff << endl;
151                         result++;
152                 }
153                 cout << "." << flush;
154         }
155
156         Digits = digitsbuf;
157
158         // conjugate test
159         numeric cdif = ex_to<numeric>(H(lst(2,2,1),5.0-5.0*I) - H(lst(2,2,1),5.0+5.0*I));
160         numeric cadd = ex_to<numeric>(H(lst(2,2,1),5.0-5.0*I) + H(lst(2,2,1),5.0+5.0*I));
161         if ((cdif.real() > prec) || (cadd.imag() > prec)) {
162                 clog << "complex conjugation test of H({2,2,1},5.0-5.0*I) seems to be wrong: " << cdif << " " << cadd << endl;
163                 result++;
164         }
165
166         return result;
167 }
168
169
170 ////////////////////////////////////////////////////////////////////////////////
171 ////////////////////////////////////////////////////////////////////////////////
172 //  zeta exam
173 ////////////////////////////////////////////////////////////////////////////////
174 ////////////////////////////////////////////////////////////////////////////////
175
176
177 static unsigned inifcns_test_zeta()
178 {
179         int digitsbuf = Digits;
180         
181         unsigned result = 0;
182
183         lst res;
184         
185         res.append(zeta(lst(2,1)) - zeta(3));
186         res.append(zeta(lst(2,1,1,1,1)) - zeta(6));
187         res.append(zeta(lst(6,3)) - (zeta(9)*83/2 - zeta(2)*zeta(7)*21 - zeta(2)*zeta(2)*zeta(5)*12/5));
188         res.append(zeta(lst(4,2,3)) - (-zeta(9)*59 + zeta(2)*zeta(7)*28 + pow(zeta(2),2)*zeta(5)*4 -
189                                        pow(zeta(3),3)/3 + pow(zeta(2),3)*zeta(3)*8/21));
190         res.append(zeta(lst(3,1,3,1,3,1,3,1)) - (2*pow(Pi,16)/factorial(18)));
191         res.append(zeta(lst(2),lst(-1)) - -zeta(2)/2);
192         res.append(zeta(lst(1,2),lst(-1,1)) - (-zeta(3)/4 - zeta(lst(1),lst(-1))*zeta(2)/2));
193         res.append(zeta(lst(2,1,1),lst(-1,-1,1)) - (-pow(zeta(2),2)*23/40 - pow(zeta(lst(1),lst(-1)),2)*zeta(2)*3/4
194                                                     - zeta(lst(3,1),lst(-1,1))*3/2 - zeta(lst(1),lst(-1))*zeta(3)*21/8));
195         
196         for (lst::const_iterator it = res.begin(); it != res.end(); it++) {
197                 Digits = 17;
198                 ex prec = 5 * pow(10, -(int)Digits);
199                 ex diff = abs((*it).evalf());
200                 if (diff > prec) {
201                         clog << *it << " seems to be wrong: " << diff << endl;
202                         clog << "Digits: " << Digits << endl;
203                         result++;
204                 }
205                 cout << "." << flush;
206                 Digits = 40;
207                 prec = 5 * pow(10, -(int)Digits);
208                 diff = abs((*it).evalf());
209                 if (diff > prec) {
210                         clog << *it << " seems to be wrong: " << diff << endl;
211                         clog << "Digits: " << Digits << endl;
212                         result++;
213                 }
214                 cout << "." << flush;
215         }
216
217         Digits = digitsbuf;
218
219         return result;
220 }
221
222
223 ////////////////////////////////////////////////////////////////////////////////
224 ////////////////////////////////////////////////////////////////////////////////
225 //  H/Li exam
226 ////////////////////////////////////////////////////////////////////////////////
227 ////////////////////////////////////////////////////////////////////////////////
228
229
230 static unsigned inifcns_test_LiG()
231 {
232         int digitsbuf = Digits;
233         Digits = 17;
234         ex prec = 5 * pow(10, -(int)Digits);
235         numeric almostone("0.99999999999999999999");
236         unsigned result = 0;
237
238         lst res;
239         
240         res.append(Li(lst(4), lst(6)).hold() - Li(4, 6.0));
241         res.append(G(lst(0,0,5.0,0,2.0,0,0,0,3.0),0.5).hold()
242                    + Li(lst(3,2,4), lst(numeric(1,10), numeric(5,2), numeric(2,3))));
243         res.append(Li(lst(2,1,1), lst(almostone, almostone, almostone)) - zeta(lst(2,1,1)));
244
245         // check Li_{1,1} against known expression
246         symbol x("x"), y("y");
247         ex eps = 1e-30*I;
248         ex s1 = Li(lst(1,1),lst(x,y));
249         ex s2 = log(1-1/x/y-eps)*log((1-1/x-eps)/(1/x/y-1/x)) + Li(2,(1-1/x/y-eps)/(1/x-1/x/y))
250                         - log(-1/x/y-eps)*log((-1/x-eps)/(1/x/y-1/x)) - Li(2,(-1/x/y-eps)/(1/x-1/x/y))
251                         - log(-1/x/y-eps)*log(1-1/x-eps) + log(-1/x/y-eps)*log(-1/x-eps);
252         res.append(s1.subs(lst(x==numeric(1)/2, y==3)) - s2.subs(lst(x==numeric(1)/2, y==3)));
253         res.append(s1.subs(lst(x==numeric(3)/2, y==numeric(1)/2)) - s2.subs(lst(x==numeric(3)/2, y==numeric(1)/2)));
254         res.append(s1.subs(lst(x==2, y==numeric(4)/5)) - s2.subs(lst(x==2, y==numeric(4)/5)));
255
256         // shuffle and quasi-shuffle identities
257         res.append(G(lst(0,0.2),1).hold() * G(lst(0.5),1).hold() - G(lst(0.5,0,0.2),1).hold()
258                         - G(lst(0,0.5,0.2),1).hold() - G(lst(0,0.2,0.5),1).hold());
259         res.append(G(lst(0,0.5),1).hold() * G(lst(0.6),1).hold() - G(lst(0,0.5,0.5*0.6),1).hold()
260                         - G(lst(0.6,0,0.5*0.6),1).hold() + G(lst(0,0,0.5*0.6),1).hold());
261         res.append(Li(lst(2),lst(numeric(1,5))).hold() * Li(lst(3),lst(7)).hold() - Li(lst(2,3),lst(numeric(1,5),7)).hold()
262                         - Li(lst(3,2),lst(7,numeric(1,5))).hold() - Li(lst(5),lst(numeric(7,5))).hold());
263         symbol a1, a2, a3, a4;
264         res.append((G(lst(a1,a2),1) * G(lst(a3,a4),1) - G(lst(a1,a2,a3,a4),1)
265                         - G(lst(a1,a3,a2,a4),1) - G(lst(a3,a1,a2,a4),1)
266                         - G(lst(a1,a3,a4,a2),1) - G(lst(a3,a1,a4,a2),1) - G(lst(a3,a4,a1,a2),1))
267                                 .subs(lst(a1==numeric(1)/10, a2==numeric(3)/10, a3==numeric(7)/10, a4==5)));
268         res.append(G(lst(-0.009),1).hold() * G(lst(-8,1.4999),1).hold() - G(lst(-0.009,-8,1.4999),1).hold()
269                         - G(lst(-8,-0.009,1.4999),1).hold() - G(lst(-8,1.4999,-0.009),1).hold());
270         res.append(G(lst(sqrt(numeric(1)/2)+I*sqrt(numeric(1)/2)),1).hold() * G(lst(1.51,-0.999),1).hold()
271                         - G(lst(sqrt(numeric(1)/2)+I*sqrt(numeric(1)/2),1.51,-0.999),1).hold()
272                         - G(lst(1.51,sqrt(numeric(1)/2)+I*sqrt(numeric(1)/2),-0.999),1).hold()
273                         - G(lst(1.51,-0.999,sqrt(numeric(1)/2)+I*sqrt(numeric(1)/2)),1).hold());
274         // checks for hoelder convolution which is used if one argument has a distance to one smaller than 0.01 
275         res.append(G(lst(0, 1.2, 1, 1.01), 1).hold() - G(lst(0, 1.2, 1, numeric("1.009999999999999999")), 1).hold());
276
277         for (lst::const_iterator it = res.begin(); it != res.end(); it++) {
278                 ex diff = abs((*it).evalf());
279                 if (diff > prec) {
280                         clog << *it << " seems to be wrong: " << diff << endl;
281                         result++;
282                 }
283                 cout << "." << flush;
284         }
285
286         return result;
287 }
288
289
290 unsigned exam_inifcns_nstdsums(void)
291 {
292         unsigned result = 0;
293         
294         cout << "examining consistency of nestedsums functions" << flush;
295         clog << "----------consistency of nestedsums functions:" << endl;
296         
297         result += inifcns_test_zeta();
298         result += inifcns_test_S();
299         result += inifcns_test_HLi();
300         result += inifcns_test_LiG();
301         
302         if (!result) {
303                 cout << " passed " << endl;
304                 clog << "(no output)" << endl;
305         } else {
306                 cout << " failed " << endl;
307         }
308         
309         return result;
310 }