]> www.ginac.de Git - ginac.git/blob - check/time_toeplitz.cpp
6f138d3e50c94d6d1489acc5298e0b5723a0019f
[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-2000 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     // compute determinant:
51     ex tdet = M.determinant();
52     
53     // dirty consistency check of result:
54     if (!tdet.subs(a==0).subs(b==0).is_zero()) {
55         clog << "Determaint of Toeplitz matrix " << endl
56              << "M==" << M << endl
57              << "was miscalculated: det(M)==" << tdet << endl;
58         ++result;
59     }
60     
61     return result;
62 }
63
64 unsigned time_toeplitz(void)
65 {
66     unsigned result = 0;
67     
68     cout << "timing determinant of polyvariate symbolic Toeplitz matrices" << flush;
69     clog << "-------determinant of polyvariate symbolic Toeplitz matrices:" << endl;
70     
71     vector<unsigned> sizes;
72     vector<double> times;
73     timer longines;
74     
75     sizes.push_back(5);
76     sizes.push_back(6);
77     sizes.push_back(7);
78     sizes.push_back(8);
79     
80     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
81         int count = 1;
82         longines.start();
83         result += toeplitz_det(*i);
84         // correct for very small times:
85         while (longines.read()<0.1) {
86             toeplitz_det(*i);
87             ++count;
88         }
89         times.push_back(longines.read()/count);
90         cout << '.' << flush;
91     }
92     
93     if (!result) {
94         cout << " passed ";
95         clog << "(no output)" << endl;
96     } else {
97         cout << " failed ";
98     }
99     // print the report:
100     cout << endl << "    dim:   ";
101     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
102         cout << '\t' << *i << 'x' << *i;
103     cout << endl << "    time/s:";
104     for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
105         cout << '\t' << int(1000*(*i))*0.001;
106     cout << endl;
107     
108     return result;
109 }