]> www.ginac.de Git - ginac.git/blob - check/time_dennyfliegner.cpp
multiple zeta values: make crandall_Y_loop helper function reentrant.
[ginac.git] / check / time_dennyfliegner.cpp
1 /** @file time_dennyfliegner.cpp
2  *
3  *  The first test routine implements Denny Fliegner's quick consistency check:
4  *  1)  e = (a0 + a1 + a2 + a3 + ...)^2, in expanded form
5  *  2)  substitute a0 by (-a2 - a3 - ...) in e
6  *  3)  expand e
7  *  after which e should be just a1^2. */
8
9 /*
10  *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25  */
26
27 #include <iostream>
28 #include <sstream>
29 #include <vector>
30 #include "ginac.h"
31 #include "timer.h"
32 using namespace std;
33 using namespace GiNaC;
34
35 static unsigned expand_subs(unsigned size)
36 {
37         unsigned result = 0;
38         // create a vector of size symbols named "a0", "a1", ...
39         vector<symbol> a;
40         ex e;
41         for (unsigned i=0; i<size; ++i) {
42                 ostringstream buf;
43                 buf << "a" << i << ends;
44                 a.push_back(symbol(buf.str()));
45                 e += a[i];
46         }
47         ex aux;
48         
49         // prepare aux so it will swallow anything but a1^2:
50         aux = -e + a[0] + a[1];
51         e = pow(e,2).expand().subs(a[0]==aux, subs_options::no_pattern).expand();
52         
53         if (e != pow(a[1],2)) {
54                 clog << "Denny Fliegner's quick consistency check erroneously returned "
55                      << e << "." << endl;
56                 ++result;
57         }
58         
59         return result;
60 }
61
62 unsigned time_dennyfliegner()
63 {
64         unsigned result = 0;
65         
66         cout << "timing commutative expansion and substitution" << flush;
67         
68         vector<unsigned> sizes;
69         vector<double> times;
70         timer breitling;
71         
72         sizes.push_back(100);
73         sizes.push_back(200);
74         sizes.push_back(400);
75         sizes.push_back(800);
76         
77         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
78                 breitling.start();
79                 result += expand_subs(*i);
80                 times.push_back(breitling.read());
81                 cout << '.' << flush;
82         }
83         
84         // print the report:
85         cout << endl << "       size:  ";
86         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
87                 cout << '\t' << *i;
88         cout << endl << "       time/s:";
89         for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
90                 cout << '\t' << *i;
91         cout << endl;
92         
93         return result;
94 }
95
96 extern void randomify_symbol_serials();
97
98 int main(int argc, char** argv)
99 {
100         randomify_symbol_serials();
101         cout << setprecision(2) << showpoint;
102         return time_dennyfliegner();
103 }