]> www.ginac.de Git - ginac.git/blob - check/time_toeplitz.cpp
ginsh: use exmap for storing assigned symbols.
[ginac.git] / check / time_toeplitz.cpp
1 /** @file time_toeplitz.cpp
2  *
3  *  Calculates determinants of dense symbolic Toeplitz materices.
4  *  For 4x4 our matrix would look like this:
5  *  [[a,b,a+b,a^2+a*b+b^2], [b,a,b,a+b], [a+b,b,a,b], [a^2+a*b+b^2,a+b,b,a]]
6  */
7
8 /*
9  *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  */
25
26 #include <iostream>
27 #include <vector>
28 #include "ginac.h"
29 #include "timer.h"
30 using namespace std;
31 using namespace GiNaC;
32
33 static unsigned toeplitz_det(unsigned size)
34 {
35         unsigned result = 0;
36         const symbol a("a"), b("b");
37         ex p[10] = {ex("a",lst(a,b)),
38                    ex("b",lst(a,b)),
39                    ex("a+b",lst(a,b)),
40                    ex("a^2+a*b+b^2",lst(a,b)),
41                    ex("a^3+a^2*b-a*b^2+b^3",lst(a,b)),
42                    ex("a^4+a^3*b+a^2*b^2+a*b^3+b^4",lst(a,b)),
43                    ex("a^5+a^4*b+a^3*b^2-a^2*b^3+a*b^4+b^5",lst(a,b)),
44                    ex("a^6+a^5*b+a^4*b^2+a^3*b^3+a^2*b^4+a*b^5+b^6",lst(a,b)),
45                    ex("a^7+a^6*b+a^5*b^2+a^4*b^3-a^3*b^4+a^2*b^5+a*b^6+b^7",lst(a,b)),
46                    ex("a^8+a^7*b+a^6*b^2+a^5*b^3+a^4*b^4+a^3*b^5+a^2*b^6+a*b^7+b^8",lst(a,b))
47         };
48
49         // construct Toeplitz matrix (diagonal structure: [[x,y,z],[y,x,y],[z,y,x]]):
50         matrix M(size,size);
51         for (unsigned ro=0; ro<size; ++ro) {
52                 for (unsigned nd=ro; nd<size; ++nd) {
53                         M.set(nd-ro,nd,p[ro]);
54                         M.set(nd,nd-ro,p[ro]);
55                 }
56         }
57
58         // compute determinant:
59         ex tdet = M.determinant();
60
61         // dirty consistency check of result:
62         if (!tdet.subs(a==0).subs(b==0).is_zero()) {
63                 clog << "Determaint of Toeplitz matrix " << endl
64                      << "M==" << M << endl
65                      << "was miscalculated: det(M)==" << tdet << endl;
66                 ++result;
67         }
68
69         return result;
70 }
71
72 unsigned time_toeplitz()
73 {
74         unsigned result = 0;
75
76         cout << "timing determinant of polyvariate symbolic Toeplitz matrices" << flush;
77
78         vector<unsigned> sizes;
79         vector<double> times;
80         timer longines;
81
82         sizes.push_back(7);
83         sizes.push_back(8);
84         sizes.push_back(9);
85         sizes.push_back(10);
86
87         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
88                 int count = 1;
89                 longines.start();
90                 result += toeplitz_det(*i);
91                 // correct for very small times:
92                 while (longines.read()<0.1) {
93                         toeplitz_det(*i);
94                         ++count;
95                 }
96                 times.push_back(longines.read()/count);
97                 cout << '.' << flush;
98         }
99
100         // print the report:
101         cout << endl << "       dim:   ";
102         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
103                 cout << '\t' << *i << 'x' << *i;
104         cout << endl << "       time/s:";
105         for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
106                 cout << '\t' << *i;
107         cout << endl;
108
109         return result;
110 }
111
112 extern void randomify_symbol_serials();
113
114 int main(int argc, char** argv)
115 {
116         randomify_symbol_serials();
117         cout << setprecision(2) << showpoint;
118         return time_toeplitz();
119 }