]> www.ginac.de Git - ginac.git/blob - check/time_vandermonde.cpp
- Two new timimgs that are interesting for optimizing.
[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-2000 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     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.set(ro,co,pow(-pow(a,1+ro/2),co));
40             else
41                 M.set(ro,co,pow(pow(a,1+ro/2),co));
42     
43     // compute determinant:
44     ex vdet = M.determinant();
45     
46     // dirty consistency check of result:
47     if (!vdet.subs(a==1).is_zero()) {
48         clog << "Determaint of Vandermonde matrix " << endl
49              << "M==" << M << endl
50              << "was miscalculated: det(M)==" << vdet << endl;
51         ++result;
52     }
53     
54     return result;
55 }
56
57 unsigned time_vandermonde(void)
58 {
59     unsigned result = 0;
60     
61     cout << "timing determinant of univariate symbolic Vandermonde matrices" << flush;
62     clog << "-------determinant of univariate symbolic Vandermonde matrices:" << endl;
63     
64     vector<unsigned> sizes;
65     vector<double> times;
66     timer swatch;
67     
68     sizes.push_back(4);
69     sizes.push_back(6);
70     sizes.push_back(8);
71     sizes.push_back(10);
72     
73     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i) {
74         int count = 1;
75         swatch.start();
76         result += vandermonde_det(*i);
77         // correct for very small times:
78         while (swatch.read()<0.02) {
79             vandermonde_det(*i);
80             ++count;
81         }
82         times.push_back(swatch.read()/count);
83         cout << '.' << flush;
84     }
85     
86     if (!result) {
87         cout << " passed ";
88         clog << "(no output)" << endl;
89     } else {
90         cout << " failed ";
91     }
92     // print the report:
93     cout << endl << "    dim:   ";
94     for (vector<unsigned>::iterator i=sizes.begin(); i!=sizes.end(); ++i)
95         cout << '\t' << *i << 'x' << *i;
96     cout << endl << "    time/s:";
97     for (vector<double>::iterator i=times.begin(); i!=times.end(); ++i)
98         cout << '\t' << int(1000*(*i))*0.001;
99     cout << endl;
100     
101     return result;
102 }