]> www.ginac.de Git - ginac.git/blob - check/time_lw_H.cpp
G_numeric: put convergence/acceleration transofrmations into helper functions.
[ginac.git] / check / time_lw_H.cpp
1 /** @file time_lw_H.cpp
2  *
3  *  Test H from the paper "Comparison of Polynomial-Oriented CAS" by Robert H.
4  *  Lewis and Michael Wester. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2008 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 <iostream>
25 #include <vector>
26 #include "ginac.h"
27 #include "timer.h"
28 using namespace std;
29 using namespace GiNaC;
30
31 static unsigned test(unsigned n)
32 {
33         matrix hilbert(n,n);
34
35         for (unsigned r=0; r<n; ++r)
36                 for (unsigned c=0; c<n; ++c)
37                         hilbert.set(r,c,numeric(1,r+c+1));
38         ex det = hilbert.determinant();
39
40         /*
41            The closed form of the determinant of n x n Hilbert matrices is:
42         
43              n-1   /                   \
44             ----- | pow(factorial(r),3) |
45              | |  | ------------------- |
46              | |  |    factorial(r+n)   |
47             r = 0  \                   /
48         */
49
50         ex hilbdet = 1;
51         for (unsigned r=0; r<n; ++r)
52                 hilbdet *= pow(factorial(r),3)/(factorial(r+n));
53
54         if (det != hilbdet) {
55                 clog << "determinant of " << n << "x" << n << " erroneously returned " << det << endl;
56                 return 1;
57         }
58         return 0;
59 }
60
61 unsigned time_lw_H()
62 {
63         unsigned result = 0;
64         unsigned count = 0;
65         timer rolex;
66         double time = .0;
67
68         cout << "timing Lewis-Wester test H (det of 80x80 Hilbert)" << flush;
69
70         rolex.start();
71         // correct for very small times:
72         do {
73                 result = test(80);
74                 ++count;
75         } while ((time=rolex.read())<0.1 && !result);
76         cout << '.' << flush;
77         cout << time/count << 's' << endl;
78
79         return result;
80 }
81
82 extern void randomify_symbol_serials();
83
84 int main(int argc, char** argv)
85 {
86         randomify_symbol_serials();
87         cout << setprecision(2) << showpoint;
88         return time_lw_H();
89 }