]> www.ginac.de Git - ginac.git/blob - check/time_toeplitz.cpp
- Pearu Peterson's patch for class function giving them better support
[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         symbol a("a"), b("b");
32         ex p[8] = {a,
33                    b,
34                    a+b,
35                    pow(a,2) + a*b + pow(b,2),
36                    pow(a,3) + pow(a,2)*b - a*pow(b,2) + pow(b,3),
37                    pow(a,4) + pow(a,3)*b + pow(a*b,2) + a*pow(b,3) + pow(b,4),
38                    pow(a,5) + pow(a,4)*b + pow(a,3)*pow(b,2) - pow(a,2)*pow(b,3) + a*pow(b,4) + pow(b,5),
39                    pow(a,6) + pow(a,5)*b + pow(a,4)*pow(b,2) + pow(a*b,3) + pow(a,2)*pow(b,4) + a*pow(b,5) + pow(b,6)
40         };
41         
42         // construct Toeplitz matrix:
43         matrix M(size,size);
44         for (unsigned ro=0; ro<size; ++ro) {
45                 for (unsigned nd=ro; nd<size; ++nd) {
46                         M.set(nd-ro,nd,p[ro]);
47                         M.set(nd,nd-ro,p[ro]);
48                 }
49         }
50         
51         // compute determinant:
52         ex tdet = M.determinant();
53         
54         // dirty consistency check of result:
55         if (!tdet.subs(a==0).subs(b==0).is_zero()) {
56                 clog << "Determaint of Toeplitz matrix " << endl
57                      << "M==" << M << endl
58                      << "was miscalculated: det(M)==" << tdet << endl;
59                 ++result;
60         }
61         
62         return result;
63 }
64
65 unsigned time_toeplitz(void)
66 {
67         unsigned result = 0;
68         
69         cout << "timing determinant of polyvariate symbolic Toeplitz matrices" << flush;
70         clog << "-------determinant of polyvariate symbolic Toeplitz matrices:" << endl;
71         
72         vector<unsigned> sizes;
73         vector<double> times;
74         timer longines;
75         
76         sizes.push_back(5);
77         sizes.push_back(6);
78         sizes.push_back(7);
79         sizes.push_back(8);
80         
81         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
82                 int count = 1;
83                 longines.start();
84                 result += toeplitz_det(*i);
85                 // correct for very small times:
86                 while (longines.read()<0.1) {
87                         toeplitz_det(*i);
88                         ++count;
89                 }
90                 times.push_back(longines.read()/count);
91                 cout << '.' << flush;
92         }
93         
94         if (!result) {
95                 cout << " passed ";
96                 clog << "(no output)" << endl;
97         } else {
98                 cout << " failed ";
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' << int(1000*(*i))*0.001;
107         cout << endl;
108         
109         return result;
110 }