]> www.ginac.de Git - ginac.git/blob - check/time_vandermonde.cpp
subs_no_pattern -> no_pattern, subs_algebraic -> algebraic
[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-2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include "times.h"
28
29 static unsigned vandermonde_det(unsigned size)
30 {
31         unsigned result = 0;
32         const symbol a("a");
33
34         // construct Vandermonde matrix:
35         matrix M(size,size);
36         for (unsigned ro=0; ro<size; ++ro) {
37                 for (unsigned co=0; co<size; ++co) {
38                         if (ro%2)
39                                 M(ro,co) = pow(-pow(a,1+ro/2),co);
40                         else
41                                 M(ro,co) = pow(pow(a,1+ro/2),co);
42                 }
43         }
44
45         // compute determinant:
46         ex det = M.determinant();
47
48         // check the result:
49         ex vanddet = 1;
50         for (unsigned i=0; i<size; ++i)
51                 for (unsigned j=0; j<i; ++j)
52                         vanddet *= M(i,1) - M(j,1);
53
54         if (expand(det - vanddet) != 0) {
55                 clog << "Determaint of Vandermonde matrix " << endl
56                      << "M==" << M << endl
57                      << "was miscalculated: det(M)==" << det << endl;
58                 ++result;
59         }
60
61         return result;
62 }
63
64 unsigned time_vandermonde()
65 {
66         unsigned result = 0;
67         
68         cout << "timing determinant of univariate symbolic Vandermonde matrices" << flush;
69         clog << "-------determinant of univariate symbolic Vandermonde matrices:" << endl;
70         
71         vector<unsigned> sizes;
72         vector<double> times;
73         timer swatch;
74         
75         sizes.push_back(6);
76         sizes.push_back(8);
77         sizes.push_back(10);
78         sizes.push_back(12);
79         
80         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
81                 int count = 1;
82                 swatch.start();
83                 result += vandermonde_det(*i);
84                 // correct for very small times:
85                 while (swatch.read()<0.02) {
86                         vandermonde_det(*i);
87                         ++count;
88                 }
89                 times.push_back(swatch.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 }