- Make some tests more stringent and meaningful.
[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-2001 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include "times.h"
27
28 static unsigned toeplitz_det(unsigned size)
29 {
30         unsigned result = 0;
31         const symbol a("a"), b("b");
32         ex p[9] = {ex("a",lst(a,b)),
33                    ex("b",lst(a,b)),
34                    ex("a+b",lst(a,b)),
35                    ex("a^2+a*b+b^2",lst(a,b)),
36                    ex("a^3+a^2*b-a*b^2+b^3",lst(a,b)),
37                    ex("a^4+a^3*b+a^2*b^2+a*b^3+b^4",lst(a,b)),
38                    ex("a^5+a^4*b+a^3*b^2-a^2*b^3+a*b^4+b^5",lst(a,b)),
39                    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)),
40                    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))
41         };
42
43         // construct Toeplitz matrix (diagonal structure: [[x,y,z],[y,x,y],[z,y,x]]):
44         matrix M(size,size);
45         for (unsigned ro=0; ro<size; ++ro) {
46                 for (unsigned nd=ro; nd<size; ++nd) {
47                         M.set(nd-ro,nd,p[ro]);
48                         M.set(nd,nd-ro,p[ro]);
49                 }
50         }
51
52         // compute determinant:
53         ex tdet = M.determinant();
54
55         // dirty consistency check of result:
56         if (!tdet.subs(a==0).subs(b==0).is_zero()) {
57                 clog << "Determaint of Toeplitz matrix " << endl
58                      << "M==" << M << endl
59                      << "was miscalculated: det(M)==" << tdet << endl;
60                 ++result;
61         }
62
63         return result;
64 }
65
66 unsigned time_toeplitz(void)
67 {
68         unsigned result = 0;
69
70         cout << "timing determinant of polyvariate symbolic Toeplitz matrices" << flush;
71         clog << "-------determinant of polyvariate symbolic Toeplitz matrices:" << endl;
72
73         vector<unsigned> sizes;
74         vector<double> times;
75         timer longines;
76
77         sizes.push_back(6);
78         sizes.push_back(7);
79         sizes.push_back(8);
80         sizes.push_back(9);
81
82         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
83                 int count = 1;
84                 longines.start();
85                 result += toeplitz_det(*i);
86                 // correct for very small times:
87                 while (longines.read()<0.1) {
88                         toeplitz_det(*i);
89                         ++count;
90                 }
91                 times.push_back(longines.read()/count);
92                 cout << '.' << flush;
93         }
94
95         if (!result) {
96                 cout << " passed ";
97                 clog << "(no output)" << endl;
98         } else {
99                 cout << " failed ";
100         }
101         // print the report:
102         cout << endl << "       dim:   ";
103         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
104                 cout << '\t' << *i << 'x' << *i;
105         cout << endl << "       time/s:";
106         for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
107                 cout << '\t' << int(1000*(*i))*0.001;
108         cout << endl;
109
110         return result;
111 }