]> www.ginac.de Git - ginac.git/blob - check/time_lw_M1.cpp
d3dabe69b4b6f3bdc7cfd67fdf08eb202c260781
[ginac.git] / check / time_lw_M1.cpp
1 /** @file time_lw_M1.cpp
2  *
3  *  Test M1 from the paper "Comparison of Polynomial-Oriented CAS" by Robert H.
4  *  Lewis and Michael Wester. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "times.h"
25
26 static unsigned test(void)
27 {
28     // Determinant of a sparse matrix that comes up in graph theory:
29     symbol x1("x1"), x2("x2"), x3("x3"), x4("x4"), x5("x5");
30     ex w[26][11] = {
31         { 1,  1,  1,  7, x4, 12, x3, 17, x2, 22, x1},
32         { 2,  2,  1,  8, x4, 13, x3, 18, x2, 23, x1},
33         { 3,  3,  1,  9, x4, 14, x3, 19, x2, 24, x1},
34         { 4,  4,  1, 10, x4, 15, x3, 20, x2, 25, x1},
35         { 5,  5,  1, 26,  1,  1,  0,  1,  0,  1, 0 },
36         { 6,  2, x5,  6,  1, 12, x3, 17, x2, 22, x1},
37         { 7,  3, x5,  7,  1, 13, x3, 18, x2, 23, x1},
38         { 8,  4, x5,  8,  1, 14, x3, 19, x2, 24, x1},
39         { 9,  5, x5,  9,  1, 15, x3, 20, x2, 25, x1},
40         {10, 10,  1, 26,  1,  1,  0,  1,  0,  1, 0 },
41         {11,  2, x5,  7, x4, 11,  1, 17, x2, 22, x1},
42         {12,  3, x5,  8, x4, 12,  1, 18, x2, 23, x1},
43         {13,  4, x5,  9, x4, 13,  1, 19, x2, 24, x1},
44         {14,  5, x5, 10, x4, 14,  1, 20, x2, 25, x1},
45         {15, 15,  1, 26,  1,  1,  0,  1,  0,  1, 0 },
46         {16,  2, x5,  7, x4, 12, x3, 16,  1, 22, x1},
47         {17,  3, x5,  8, x4, 13, x3, 17,  1, 23, x1},
48         {18,  4, x5,  9, x4, 14, x3, 18,  1, 24, x1},
49         {19,  5, x5, 10, x4, 15, x3, 19,  1, 25, x1},
50         {20, 20,  1, 26,  1,  1,  0,  1,  0,  1, 0 },
51         {21,  2, x5,  7, x4, 12, x3, 17, x2, 21, 1 },
52         {22,  3, x5,  8, x4, 13, x3, 18, x2, 22, 1 },
53         {23,  4, x5,  9, x4, 14, x3, 19, x2, 23, 1 },
54         {24,  5, x5, 10, x4, 15, x3, 20, x2, 24, 1 },
55         {25, 25,  1, 26,  1,  1,  0,  1,  0,  1, 0 },
56         {26,  1, x5,  6, x4, 11, x3, 16, x2, 21, x1}
57     };
58     matrix m(26,26);
59     for (unsigned r=0; r<26; ++r) {
60         for (unsigned c=0; c<5; ++c) {
61             m.set(r,
62                   unsigned(ex_to_numeric(w[r][2*c+1]).to_int()-1),
63                   w[r][2*c+2]);
64         }
65     }
66     ex det = m.determinant();
67     // The result should have been:
68     ex cmp = -12*pow(x1*x5,2)*x4-12*x1*pow(x2*x4,2)-12*x3*pow(x4*x5,2)
69         -pow(x5,3)*pow(x4,2)-12*x1*pow(x4*x5,2)-12*x2*pow(x4*x5,2)
70         -pow(x4,3)*pow(x5,2)-2*pow(x5,3)*x4*x3-2*pow(x4,3)*x5*x3
71         -36*x3*x1*pow(x5,2)*x4-36*x3*x1*pow(x4,2)*x5-12*pow(x3*x5,2)*x4
72         -12*pow(x3*x4,2)*x5-36*x3*x2*pow(x5,2)*x4-36*x3*x2*pow(x4,2)*x5
73         -36*x1*x5*pow(x3,2)*x4-36*x2*x5*pow(x3,2)*x4-12*x1*pow(x3*x5,2)
74         -12*x1*pow(x3*x4,2)-pow(x3,3)*pow(x5,2)-pow(x3,3)*pow(x4,2)
75         -2*pow(x3,3)*x5*x4-12*x2*pow(x3*x5,2)-12*x2*pow(x3*x4,2)
76         -12*x1*pow(x2*x5,2)-12*pow(x2*x5,2)*x4-12*pow(x2*x4,2)*x5
77         -12*pow(x2*x5,2)*x3-12*pow(x2*x4,2)*x3-12*pow(x2*x3,2)*x5
78         -12*pow(x2*x3,2)*x4-pow(x3,2)*pow(x5,3)-pow(x3,2)*pow(x4,3)
79         -2*pow(x5,3)*x4*x2-2*pow(x4,3)*x5*x2-2*x3*pow(x5,3)*x2
80         -2*x3*pow(x4,3)*x2-2*pow(x3,3)*x5*x2-2*pow(x3,3)*x4*x2
81         -2*pow(x2,3)*x5*x4-36*x2*x1*pow(x5,2)*x4-36*x2*x1*pow(x4,2)*x5
82         -120*x2*x1*x5*x4*x3-36*x2*x1*pow(x5,2)*x3-36*x2*x1*pow(x4,2)*x3
83         -36*x2*x1*pow(x3,2)*x5-36*x2*x1*pow(x3,2)*x4-36*pow(x2,2)*x5*x4*x3
84         -36*pow(x1,2)*x5*x4*x3-12*pow(x1*x5,2)*x3-12*pow(x1*x4,2)*x3
85         -12*pow(x1*x3,2)*x5-12*pow(x1*x3,2)*x4-12*pow(x1*x5,2)*x2
86         -12*pow(x1*x4,2)*x2-12*pow(x1*x3,2)*x2-12*pow(x1*x2,2)*x5
87         -12*pow(x1*x2,2)*x4-12*pow(x1*x2,2)*x3-pow(x2,2)*pow(x5,3)
88         -pow(x2,2)*pow(x4,3)-pow(x2,2)*pow(x3,3)-36*x1*x5*pow(x2,2)*x4
89         -36*x1*x5*pow(x2,2)*x3-36*x1*x4*pow(x2,2)*x3-36*x2*pow(x1,2)*x5*x4
90         -pow(x2,3)*pow(x5,2)-pow(x2,3)*pow(x4,2)-pow(x2,3)*pow(x3,2)
91         -2*pow(x2,3)*x5*x3-2*pow(x2,3)*x4*x3-12*x1*pow(x2*x3,2)
92         -12*pow(x1*x4,2)*x5-pow(x1,3)*pow(x5,2)-pow(x1,3)*pow(x4,2)
93         -pow(x1,3)*pow(x3,2)-pow(x1,3)*pow(x2,2)-pow(x1,2)*pow(x5,3)
94         -pow(x1,2)*pow(x4,3)-pow(x1,2)*pow(x3,3)-pow(x1,2)*pow(x2,3)
95         -36*x2*x3*pow(x1,2)*x5-36*x2*x3*pow(x1,2)*x4-2*pow(x5,3)*x4*x1
96         -2*pow(x4,3)*x5*x1-2*x3*pow(x5,3)*x1-2*x3*pow(x4,3)*x1
97         -2*pow(x3,3)*x5*x1-2*pow(x3,3)*x4*x1-2*x2*pow(x5,3)*x1
98         -2*x2*pow(x4,3)*x1-2*x2*pow(x3,3)*x1-2*pow(x2,3)*x5*x1
99         -2*pow(x2,3)*x4*x1-2*pow(x2,3)*x3*x1-2*pow(x1,3)*x5*x4
100         -2*pow(x1,3)*x5*x3-2*pow(x1,3)*x5*x2-2*pow(x1,3)*x4*x3
101         -2*pow(x1,3)*x4*x2-2*pow(x1,3)*x3*x2;
102     if (det!=cmp) {
103         clog << "The determinant was miscalculated" << endl;
104         return 1;
105     }
106     return 0;
107 }
108
109 unsigned time_lw_M1(void)
110 {
111     unsigned result = 0;
112     unsigned count = 0;
113     timer rolex;
114     double time = .0;
115     
116     cout << "timing Lewis-Wester test M1 (26x26 sparse, det)" << flush;
117     clog << "-------Lewis-Wester test M1 (26x26 sparse, det)" << endl;
118     
119     rolex.start();
120     // correct for very small times:
121     do {
122         result = test();
123         ++count;
124     } while ((time=rolex.read())<0.1 && !result);
125     cout << '.' << flush;
126     
127     if (!result) {
128         cout << " passed ";
129         clog << "(no output)" << endl;
130     } else {
131         cout << " failed ";
132     }
133     cout << int(1000*(time/count))*0.001 << 's' << endl;
134     
135     return result;
136 }