]> www.ginac.de Git - ginac.git/blob - check/time_dennyfliegner.cpp
[PATCH 2/3] Make a stronger normalisation for expressions with exponents.
[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-2020 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 "ginac.h"
28 #include "timer.h"
29 using namespace GiNaC;
30
31 #include <iostream>
32 #include <sstream>
33 #include <vector>
34 using namespace std;
35
36 static unsigned expand_subs(unsigned size)
37 {
38         unsigned result = 0;
39         // create a vector of size symbols named "a0", "a1", ...
40         vector<symbol> a;
41         ex e;
42         for (unsigned i=0; i<size; ++i) {
43                 ostringstream buf;
44                 buf << "a" << i << ends;
45                 a.push_back(symbol(buf.str()));
46                 e += a[i];
47         }
48         ex aux;
49         
50         // prepare aux so it will swallow anything but a1^2:
51         aux = -e + a[0] + a[1];
52         e = pow(e,2).expand().subs(a[0]==aux, subs_options::no_pattern).expand();
53         
54         if (e != pow(a[1],2)) {
55                 clog << "Denny Fliegner's quick consistency check erroneously returned "
56                      << e << "." << endl;
57                 ++result;
58         }
59         
60         return result;
61 }
62
63 unsigned time_dennyfliegner()
64 {
65         unsigned result = 0;
66         
67         cout << "timing commutative expansion and substitution" << flush;
68         
69         vector<unsigned> sizes = {100, 200, 400, 800};
70         vector<double> times;
71         timer breitling;
72         
73         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
74                 breitling.start();
75                 result += expand_subs(*i);
76                 times.push_back(breitling.read());
77                 cout << '.' << flush;
78         }
79         
80         // print the report:
81         cout << endl << "       size:  ";
82         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
83                 cout << '\t' << *i;
84         cout << endl << "       time/s:";
85         for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
86                 cout << '\t' << *i;
87         cout << endl;
88         
89         return result;
90 }
91
92 extern void randomify_symbol_serials();
93
94 int main(int argc, char** argv)
95 {
96         randomify_symbol_serials();
97         cout << setprecision(2) << showpoint;
98         return time_dennyfliegner();
99 }