]> www.ginac.de Git - ginac.git/blob - check/time_lw_O.cpp
[bugfix] normalize_in_ring: don't set the leading coefficient to 1.
[ginac.git] / check / time_lw_O.cpp
1 /** @file time_lw_O.cpp
2  *
3  *  Test O1 from the paper "Comparison of Polynomial-Oriented CAS" by Robert H.
4  *  Lewis and Michael Wester. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #include <iostream>
25 #include <vector>
26 #include "ginac.h"
27 #include "timer.h"
28 using namespace std;
29 using namespace GiNaC;
30
31 static const bool do_test2 = false;  // set to true in order to run this beast
32
33 static const symbol a1("a1"), a2("a2"), a3("a3"), a4("a4"), a5("a5"), a6("a6");
34 static const symbol b1("b1"), b2("b2"), b3("b3"), b4("b4"), b5("b5"), b6("b6");
35 static const symbol c1("c1"), c2("c2"), c3("c3"), c4("c4"), c5("c5"), c6("c6");
36
37 static const ex det1()
38 {
39         matrix d1(15,15);
40         d1 = a6, a5, a4, a3, a2, a1, 0,  0,  0,  0,  0,  0,  0,  0,  0,
41              0,  0,  a6, 0,  a5, a4, 0,  a3, a2, a1, 0,  0,  0,  0,  0,
42              0,  a6, 0,  a5, a4, 0,  a3, a2, a1, 0,  0,  0,  0,  0,  0,
43              0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1, 0,  0,
44              0,  0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1, 0,
45              0,  0,  0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1,
46              0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1, 0,  0,
47              0,  0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1, 0,
48              0,  b6, 0,  b5, b4, 0,  b3, b2, b1, 0,  0,  0,  0,  0,  0,
49              0,  0,  b6, 0,  b5, b4, 0,  b3, b2, b1, 0,  0,  0,  0,  0,
50              0,  0,  0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1,
51              0,  0,  0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1,
52              0,  0,  c6, 0,  c5, c4, 0,  c3, c2, c1, 0,  0,  0,  0,  0,
53              0,  c6, 0,  c5, c4, 0,  c3, c2, c1, 0,  0,  0,  0,  0,  0,
54              0,  0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1, 0;
55
56         return d1.determinant();
57 }
58
59 static const ex det2()
60 {
61         matrix d2(15,15);
62         d2 = b6, b5, b4, b3, b2, b1, 0,  0,  0,  0,  0,  0,  0,  0,  0,
63              0,  0,  b6, 0,  b5, b4, 0,  b3, b2, b1, 0,  0,  0,  0,  0,
64              0,  b6, 0,  b5, b4, 0,  b3, b2, b1, 0,  0,  0,  0,  0,  0,
65              0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1, 0,  0,
66              0,  0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1, 0,
67              0,  0,  0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1,
68              0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1, 0,  0,
69              0,  0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1, 0,
70              0,  c6, 0,  c5, c4, 0,  c3, c2, c1, 0,  0,  0,  0,  0,  0,
71              0,  0,  c6, 0,  c5, c4, 0,  c3, c2, c1, 0,  0,  0,  0,  0,
72              0,  0,  0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1,
73              0,  0,  0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1,
74              0,  0,  a6, 0,  a5, a4, 0,  a3, a2, a1, 0,  0,  0,  0,  0,
75              0,  a6, 0,  a5, a4, 0,  a3, a2, a1, 0,  0,  0,  0,  0,  0,
76              0,  0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1, 0;
77
78         return d2.determinant();
79 }
80
81 static const ex det3()
82 {
83         matrix d3(15,15);
84         d3 = c6, c5, c4, c3, c2, c1, 0,  0,  0,  0,  0,  0,  0,  0,  0,
85              0,  0,  c6, 0,  c5, c4, 0,  c3, c2, c1, 0,  0,  0,  0,  0,
86              0,  c6, 0,  c5, c4, 0,  c3, c2, c1, 0,  0,  0,  0,  0,  0,
87              0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1, 0,  0,
88              0,  0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1, 0,
89              0,  0,  0,  0,  0,  c6, 0,  0,  c5, c4, 0,  0,  c3, c2, c1,
90              0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1, 0,  0,
91              0,  0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1, 0,
92              0,  a6, 0,  a5, a4, 0,  a3, a2, a1, 0,  0,  0,  0,  0,  0,
93              0,  0,  a6, 0,  a5, a4, 0,  a3, a2, a1, 0,  0,  0,  0,  0,
94              0,  0,  0,  0,  0,  a6, 0,  0,  a5, a4, 0,  0,  a3, a2, a1,
95              0,  0,  0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1,
96              0,  0,  b6, 0,  b5, b4, 0,  b3, b2, b1, 0,  0,  0,  0,  0,
97              0,  b6, 0,  b5, b4, 0,  b3, b2, b1, 0,  0,  0,  0,  0,  0,
98              0,  0,  0,  0,  b6, 0,  0,  b5, b4, 0,  0,  b3, b2, b1, 0;
99
100         return d3.determinant();
101 }
102
103 // The results of test_O1 will be needed for test_O2:
104 static ex d1, d2, d3;
105
106 static unsigned test_O1()
107 {
108         d1 = det1();  cout << '.' << flush;
109         d2 = det2();  cout << '.' << flush;
110         d3 = det3();  cout << '.' << flush;
111         unsigned nops1 = nops(d1);
112         unsigned nops2 = nops(d2);
113         unsigned nops3 = nops(d3);
114
115         if ((nops1 != 37490) || (nops2 != 37490) || (nops3 != 37490)) {
116                 clog << "Determinants were miscalculated" << endl;
117                 return 1;
118         }
119         return 0;
120 }
121
122 static unsigned test_O2()
123 {
124         const ex gcd1 = gcd( d1, d2 );  cout << '.' << flush;
125         const ex resultant = gcd( gcd1, d3 );  cout << '.' << flush;
126         if (nops(resultant) != 21894) {
127                 clog << "Resultant was miscalculated" << endl;
128                 return 1;
129         }
130         return 0;
131 }
132
133 unsigned time_lw_O()
134 {
135         unsigned result = 0;
136         unsigned count = 0;
137         timer rolex;
138         double time = .0;
139
140         cout << "timing Lewis-Wester test O1 (three 15x15 dets)" << flush;
141
142         rolex.start();
143         // correct for very small times:
144         do {
145                 result = test_O1();
146                 ++count;
147         } while ((time=rolex.read())<0.1 && !result);
148
149         if (result)
150                 return result;
151         
152         cout << time/(3*count) << "s (average)" << endl;
153
154         cout << "timing Lewis-Wester test O2 (Resultant)" << flush;
155
156         if (do_test2) {
157                 rolex.reset();
158                 result += test_O2();
159
160                 cout << rolex.read() << "s (combined)" << endl;
161         } else {
162                 cout << " disabled" << endl;
163         }
164
165         return result;
166 }
167
168 extern void randomify_symbol_serials();
169
170 int main(int argc, char** argv)
171 {
172         randomify_symbol_serials();
173         cout << setprecision(2) << showpoint;
174         return time_lw_O();
175 }