]> www.ginac.de Git - ginac.git/blob - check/time_dennyfliegner.cpp
- extended the "Algorithms" chapter which has been renamed to "Methods and
[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-2000 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include "times.h"
28
29 #define VECSIZE 200
30
31 static unsigned expand_subs(unsigned size)
32 {
33     unsigned result = 0;
34     symbol a1("a1");
35     symbol a[VECSIZE];
36     ex e, aux;
37     
38     a[1] = a1;
39     for (unsigned i=0; i<size; ++i) {
40         e = e + a[i];
41     }
42     
43     // prepare aux so it will swallow anything but a1^2:
44     aux = -e + a[0] + a[1];
45     e = expand(subs(expand(pow(e, 2)), a[0] == aux));
46     
47     if (e != pow(a1,2)) {
48         clog << "Denny Fliegner's quick consistency check erroneously returned "
49              << e << "." << endl;
50         ++result;
51     }
52     
53     return result;
54 }
55
56 unsigned time_dennyfliegner(void)
57 {
58     unsigned result = 0;
59     
60     cout << "timing commutative expansion and substitution" << flush;
61     clog << "-------commutative expansion and substitution:" << endl;
62     
63     vector<unsigned> sizes;
64     vector<double> times;
65     timer breitling;
66     
67     sizes.push_back(25);
68     sizes.push_back(50);
69     sizes.push_back(100);
70     sizes.push_back(200);
71     
72     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
73         breitling.start();
74         result += expand_subs(*i);
75         times.push_back(breitling.read());
76         cout << '.' << flush;
77     }
78     
79     if (!result) {
80         cout << " passed ";
81         clog << "(no output)" << endl;
82     } else {
83         cout << " failed ";
84     }
85     // print the report:
86     cout << endl << "    size:  ";
87     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
88         cout << '\t' << (*i);
89     cout << endl << "    time/s:";
90     for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
91         cout << '\t' << int(1000*(*i))*0.001;
92     cout << endl;
93     
94     return result;
95 }