]> www.ginac.de Git - ginac.git/blob - check/time_dennyfliegner.cpp
- Complete revamp of methods in class matrix. Some redundant (and poor)
[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     // create a vector of size symbols named "a0", "a1", ...
35     vector<symbol> a;
36     for (unsigned i=0; i<size; ++i) {
37         char buf[4];
38         ostrstream(buf,sizeof(buf)) << i << ends;
39         a.push_back(symbol(string("a")+buf));
40     }
41     ex e, aux;
42     
43     for (unsigned i=0; i<size; ++i)
44         e = e + a[i];
45     
46     // prepare aux so it will swallow anything but a1^2:
47     aux = -e + a[0] + a[1];
48     e = pow(e,2).expand().subs(a[0]==aux).expand();
49     
50     if (e != pow(a[1],2)) {
51         clog << "Denny Fliegner's quick consistency check erroneously returned "
52              << e << "." << endl;
53         ++result;
54     }
55     
56     return result;
57 }
58
59 unsigned time_dennyfliegner(void)
60 {
61     unsigned result = 0;
62     
63     cout << "timing commutative expansion and substitution" << flush;
64     clog << "-------commutative expansion and substitution:" << endl;
65     
66     vector<unsigned> sizes;
67     vector<double> times;
68     timer breitling;
69     
70     sizes.push_back(25);
71     sizes.push_back(50);
72     sizes.push_back(100);
73     sizes.push_back(200);
74     
75     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
76         breitling.start();
77         result += expand_subs(*i);
78         times.push_back(breitling.read());
79         cout << '.' << flush;
80     }
81     
82     if (!result) {
83         cout << " passed ";
84         clog << "(no output)" << endl;
85     } else {
86         cout << " failed ";
87     }
88     // print the report:
89     cout << endl << "    size:  ";
90     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
91         cout << '\t' << (*i);
92     cout << endl << "    time/s:";
93     for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
94         cout << '\t' << int(1000*(*i))*0.001;
95     cout << endl;
96     
97     return result;
98 }