1 /** @file time_vandermonde.cpp
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]]
10 * GiNaC Copyright (C) 1999-2018 Johannes Gutenberg University Mainz, Germany
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.
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.
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
29 using namespace GiNaC;
35 static unsigned vandermonde_det(unsigned size)
40 // construct Vandermonde matrix:
42 for (unsigned ro=0; ro<size; ++ro) {
43 for (unsigned co=0; co<size; ++co) {
45 M(ro,co) = pow(-pow(a,1+ro/2),co);
47 M(ro,co) = pow(pow(a,1+ro/2),co);
51 // compute determinant:
52 ex det = M.determinant();
56 for (unsigned i=0; i<size; ++i)
57 for (unsigned j=0; j<i; ++j)
58 vanddet *= M(i,1) - M(j,1);
60 if (expand(det - vanddet) != 0) {
61 clog << "Determinant of Vandermonde matrix " << endl
63 << "was miscalculated: det(M)==" << det << endl;
70 unsigned time_vandermonde()
74 cout << "timing determinant of univariate symbolic Vandermonde matrices" << flush;
76 vector<unsigned> sizes = {8, 10, 12, 14};
80 for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
83 result += vandermonde_det(*i);
84 // correct for very small times:
85 while (swatch.read()<0.02) {
89 times.push_back(swatch.read()/count);
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)
105 extern void randomify_symbol_serials();
107 int main(int argc, char** argv)
109 randomify_symbol_serials();
110 cout << setprecision(2) << showpoint;
111 return time_vandermonde();