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