]> www.ginac.de Git - ginac.git/blob - check/time_vandermonde.cpp
Make workarounds for sqrfree_yun() obsolete.
[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-2020 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 "ginac.h"
28 #include "timer.h"
29 using namespace GiNaC;
30
31 #include <iostream>
32 #include <vector>
33 using namespace std;
34
35 static unsigned vandermonde_det(unsigned size)
36 {
37         unsigned result = 0;
38         const symbol a("a");
39
40         // construct Vandermonde matrix:
41         matrix M(size,size);
42         for (unsigned ro=0; ro<size; ++ro) {
43                 for (unsigned co=0; co<size; ++co) {
44                         if (ro%2)
45                                 M(ro,co) = pow(-pow(a,1+ro/2),co);
46                         else
47                                 M(ro,co) = pow(pow(a,1+ro/2),co);
48                 }
49         }
50
51         // compute determinant:
52         ex det = M.determinant();
53
54         // check the result:
55         ex vanddet = 1;
56         for (unsigned i=0; i<size; ++i)
57                 for (unsigned j=0; j<i; ++j)
58                         vanddet *= M(i,1) - M(j,1);
59
60         if (expand(det - vanddet) != 0) {
61                 clog << "Determinant of Vandermonde matrix " << endl
62                      << "M==" << M << endl
63                      << "was miscalculated: det(M)==" << det << endl;
64                 ++result;
65         }
66
67         return result;
68 }
69
70 unsigned time_vandermonde()
71 {
72         unsigned result = 0;
73         
74         cout << "timing determinant of univariate symbolic Vandermonde matrices" << flush;
75         
76         vector<unsigned> sizes = {8, 10, 12, 14};
77         vector<double> times;
78         timer swatch;
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         // print the report:
94         cout << endl << "       dim:   ";
95         for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
96                 cout << '\t' << *i << 'x' << *i;
97         cout << endl << "       time/s:";
98         for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
99                 cout << '\t' << *i;
100         cout << endl;
101         
102         return result;
103 }
104
105 extern void randomify_symbol_serials();
106
107 int main(int argc, char** argv)
108 {
109         randomify_symbol_serials();
110         cout << setprecision(2) << showpoint;
111         return time_vandermonde();
112 }