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