blob: d799b15d158923d68d491c1a4d3b2fe9bac487f9 [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001#include <vector>
2
3/* Given:
4 * Resultant[a*t^3 + b*t^2 + c*t + d - x, e*t^3 + f*t^2 + g*t + h - y, t]
5 */
6
caryclark@google.com27accef2012-01-25 18:57:23 +00007const char result1[] =
caryclark@google.com639df892012-01-10 21:46:10 +00008"-d^3 e^3 + c d^2 e^2 f - b d^2 e f^2 + a d^2 f^3 - c^2 d e^2 g + "
9" 2 b d^2 e^2 g + b c d e f g - 3 a d^2 e f g - a c d f^2 g - "
10" b^2 d e g^2 + 2 a c d e g^2 + a b d f g^2 - a^2 d g^3 + c^3 e^2 h - "
11" 3 b c d e^2 h + 3 a d^2 e^2 h - b c^2 e f h + 2 b^2 d e f h + "
12" a c d e f h + a c^2 f^2 h - 2 a b d f^2 h + b^2 c e g h - "
13" 2 a c^2 e g h - a b d e g h - a b c f g h + 3 a^2 d f g h + "
14" a^2 c g^2 h - b^3 e h^2 + 3 a b c e h^2 - 3 a^2 d e h^2 + "
15" a b^2 f h^2 - 2 a^2 c f h^2 - a^2 b g h^2 + a^3 h^3 + 3 d^2 e^3 x - "
16" 2 c d e^2 f x + 2 b d e f^2 x - 2 a d f^3 x + c^2 e^2 g x - "
17" 4 b d e^2 g x - b c e f g x + 6 a d e f g x + a c f^2 g x + "
18" b^2 e g^2 x - 2 a c e g^2 x - a b f g^2 x + a^2 g^3 x + "
19" 3 b c e^2 h x - 6 a d e^2 h x - 2 b^2 e f h x - a c e f h x + "
20" 2 a b f^2 h x + a b e g h x - 3 a^2 f g h x + 3 a^2 e h^2 x - "
21" 3 d e^3 x^2 + c e^2 f x^2 - b e f^2 x^2 + a f^3 x^2 + "
22" 2 b e^2 g x^2 - 3 a e f g x^2 + 3 a e^2 h x^2 + e^3 x^3 - "
23" c^3 e^2 y + 3 b c d e^2 y - 3 a d^2 e^2 y + b c^2 e f y - "
24" 2 b^2 d e f y - a c d e f y - a c^2 f^2 y + 2 a b d f^2 y - "
25" b^2 c e g y + 2 a c^2 e g y + a b d e g y + a b c f g y - "
26" 3 a^2 d f g y - a^2 c g^2 y + 2 b^3 e h y - 6 a b c e h y + "
27" 6 a^2 d e h y - 2 a b^2 f h y + 4 a^2 c f h y + 2 a^2 b g h y - "
28" 3 a^3 h^2 y - 3 b c e^2 x y + 6 a d e^2 x y + 2 b^2 e f x y + "
29" a c e f x y - 2 a b f^2 x y - a b e g x y + 3 a^2 f g x y - "
30" 6 a^2 e h x y - 3 a e^2 x^2 y - b^3 e y^2 + 3 a b c e y^2 - "
31" 3 a^2 d e y^2 + a b^2 f y^2 - 2 a^2 c f y^2 - a^2 b g y^2 + "
32" 3 a^3 h y^2 + 3 a^2 e x y^2 - a^3 y^3";
33
caryclark@google.com27accef2012-01-25 18:57:23 +000034const size_t len1 = sizeof(result1) - 1;
35
36/* Given:
37 * Expand[
38 * Det[{{a, b, c, (d - x), 0, 0},
39 * {0, a, b, c, (d - x), 0},
40 * {0, 0, a, b, c, (d - x)},
41 * {e, f, g, (h - y), 0, 0},
42 * {0, e, f, g, (h - y), 0},
43 * {0, 0, e, f, g, (h - y)}}]]
44 */
45 // result1 and result2 are the same. 102 factors:
46const char result2[] =
47"-d^3 e^3 + c d^2 e^2 f - b d^2 e f^2 + a d^2 f^3 - c^2 d e^2 g + "
48" 2 b d^2 e^2 g + b c d e f g - 3 a d^2 e f g - a c d f^2 g - "
49" b^2 d e g^2 + 2 a c d e g^2 + a b d f g^2 - a^2 d g^3 + c^3 e^2 h - "
50" 3 b c d e^2 h + 3 a d^2 e^2 h - b c^2 e f h + 2 b^2 d e f h + "
51" a c d e f h + a c^2 f^2 h - 2 a b d f^2 h + b^2 c e g h - "
52" 2 a c^2 e g h - a b d e g h - a b c f g h + 3 a^2 d f g h + "
53" a^2 c g^2 h - b^3 e h^2 + 3 a b c e h^2 - 3 a^2 d e h^2 + "
54" a b^2 f h^2 - 2 a^2 c f h^2 - a^2 b g h^2 + a^3 h^3 + 3 d^2 e^3 x - "
55" 2 c d e^2 f x + 2 b d e f^2 x - 2 a d f^3 x + c^2 e^2 g x - "
56" 4 b d e^2 g x - b c e f g x + 6 a d e f g x + a c f^2 g x + "
57" b^2 e g^2 x - 2 a c e g^2 x - a b f g^2 x + a^2 g^3 x + "
58" 3 b c e^2 h x - 6 a d e^2 h x - 2 b^2 e f h x - a c e f h x + "
59" 2 a b f^2 h x + a b e g h x - 3 a^2 f g h x + 3 a^2 e h^2 x - "
60" 3 d e^3 x^2 + c e^2 f x^2 - b e f^2 x^2 + a f^3 x^2 + "
61" 2 b e^2 g x^2 - 3 a e f g x^2 + 3 a e^2 h x^2 + e^3 x^3 - "
62" c^3 e^2 y + 3 b c d e^2 y - 3 a d^2 e^2 y + b c^2 e f y - "
63" 2 b^2 d e f y - a c d e f y - a c^2 f^2 y + 2 a b d f^2 y - "
64" b^2 c e g y + 2 a c^2 e g y + a b d e g y + a b c f g y - "
65" 3 a^2 d f g y - a^2 c g^2 y + 2 b^3 e h y - 6 a b c e h y + "
66" 6 a^2 d e h y - 2 a b^2 f h y + 4 a^2 c f h y + 2 a^2 b g h y - "
67" 3 a^3 h^2 y - 3 b c e^2 x y + 6 a d e^2 x y + 2 b^2 e f x y + "
68" a c e f x y - 2 a b f^2 x y - a b e g x y + 3 a^2 f g x y - "
69" 6 a^2 e h x y - 3 a e^2 x^2 y - b^3 e y^2 + 3 a b c e y^2 - "
70" 3 a^2 d e y^2 + a b^2 f y^2 - 2 a^2 c f y^2 - a^2 b g y^2 + "
71" 3 a^3 h y^2 + 3 a^2 e x y^2 - a^3 y^3";
72
73
74const size_t len2 = sizeof(result2) - 1;
75
caryclark@google.com639df892012-01-10 21:46:10 +000076const int factors = 8;
77
78struct coeff {
79 int s; // constant and coefficient sign
80 int n[factors]; // 0 or power of a (1, 2, or 3) for a through h
81};
82
83enum {
84 xxx_coeff,
85 xxy_coeff,
86 xyy_coeff,
87 yyy_coeff,
88 xx_coeff,
89 xy_coeff,
90 yy_coeff,
91 x_coeff,
92 y_coeff,
93 c_coeff,
94 coeff_count
95};
96
97typedef std::vector<coeff> coeffs;
98typedef std::vector<coeffs> n_coeffs;
99
caryclark@google.com27accef2012-01-25 18:57:23 +0000100static char skipSpace(const char* str, size_t& index) {
caryclark@google.com639df892012-01-10 21:46:10 +0000101 do {
102 ++index;
caryclark@google.com27accef2012-01-25 18:57:23 +0000103 } while (str[index] == ' ');
104 return str[index];
caryclark@google.com639df892012-01-10 21:46:10 +0000105}
106
caryclark@google.com27accef2012-01-25 18:57:23 +0000107static char backSkipSpace(const char* str, size_t& end) {
108 while (str[end - 1] == ' ') {
caryclark@google.com639df892012-01-10 21:46:10 +0000109 --end;
110 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000111 return str[end - 1];
caryclark@google.com639df892012-01-10 21:46:10 +0000112}
113
caryclark@google.com27accef2012-01-25 18:57:23 +0000114static void match(const char* str, size_t len, coeffs& co, const char pattern[]) {
caryclark@google.com639df892012-01-10 21:46:10 +0000115 size_t patternLen = strlen(pattern);
116 size_t index = 0;
117 while (index < len) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000118 char ch = str[index];
caryclark@google.com639df892012-01-10 21:46:10 +0000119 if (ch != '-' && ch != '+') {
120 printf("missing sign\n");
121 }
122 size_t end = index + 1;
caryclark@google.com27accef2012-01-25 18:57:23 +0000123 while (str[end] != '+' && str[end] != '-' && ++end < len) {
caryclark@google.com639df892012-01-10 21:46:10 +0000124 ;
125 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000126 backSkipSpace(str, end);
caryclark@google.com639df892012-01-10 21:46:10 +0000127 size_t idx = index;
128 index = end;
caryclark@google.com27accef2012-01-25 18:57:23 +0000129 skipSpace(str, index);
130 if (!strncmp(&str[end - patternLen], pattern, patternLen) == 0) {
caryclark@google.com639df892012-01-10 21:46:10 +0000131 continue;
132 }
133 size_t endCoeff = end - patternLen;
caryclark@google.com27accef2012-01-25 18:57:23 +0000134 char last = backSkipSpace(str, endCoeff);
caryclark@google.com639df892012-01-10 21:46:10 +0000135 if (last == '2' || last == '3') {
caryclark@google.com27accef2012-01-25 18:57:23 +0000136 last = str[endCoeff - 3]; // skip ^2
caryclark@google.com639df892012-01-10 21:46:10 +0000137 }
138 if (last == 'x' || last == 'y') {
139 continue;
140 }
141 coeff c;
caryclark@google.com27accef2012-01-25 18:57:23 +0000142 c.s = str[idx] == '-' ? -1 : 1;
caryclark@google.com639df892012-01-10 21:46:10 +0000143 bzero(c.n, sizeof(c.n));
caryclark@google.com27accef2012-01-25 18:57:23 +0000144 ch = skipSpace(str, idx);
caryclark@google.com639df892012-01-10 21:46:10 +0000145 if (ch >= '2' && ch <= '6') {
146 c.s *= ch - '0';
caryclark@google.com27accef2012-01-25 18:57:23 +0000147 ch = skipSpace(str, idx);
caryclark@google.com639df892012-01-10 21:46:10 +0000148 }
149 while (idx < endCoeff) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000150 char x = str[idx];
caryclark@google.com639df892012-01-10 21:46:10 +0000151 if (x < 'a' || x > 'a' + factors) {
152 printf("expected factor\n");
153 }
154 idx++;
155 int pow = 1;
caryclark@google.com27accef2012-01-25 18:57:23 +0000156 if (str[idx] == '^') {
caryclark@google.com639df892012-01-10 21:46:10 +0000157 idx++;
caryclark@google.com27accef2012-01-25 18:57:23 +0000158 char exp = str[idx];
caryclark@google.com639df892012-01-10 21:46:10 +0000159 if (exp < '2' || exp > '3') {
160 printf("expected exponent\n");
161 }
162 pow = exp - '0';
163 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000164 skipSpace(str, idx);
caryclark@google.com639df892012-01-10 21:46:10 +0000165 c.n[x - 'a'] = pow;
166 }
167 co.push_back(c);
168 }
169}
170
caryclark@google.com27accef2012-01-25 18:57:23 +0000171void cubecode_test(int test);
caryclark@google.com639df892012-01-10 21:46:10 +0000172
caryclark@google.com27accef2012-01-25 18:57:23 +0000173void cubecode_test(int test) {
174 const char* str = test ? result2 : result1;
175 size_t len = strlen(str);
caryclark@google.com639df892012-01-10 21:46:10 +0000176 n_coeffs c(coeff_count);
caryclark@google.com27accef2012-01-25 18:57:23 +0000177 match(str, len, c[xxx_coeff], "x^3"); // 1 factor
178 match(str, len, c[xxy_coeff], "x^2 y"); // 1 factor
179 match(str, len, c[xyy_coeff], "x y^2"); // 1 factor
180 match(str, len, c[yyy_coeff], "y^3"); // 1 factor
181 match(str, len, c[xx_coeff], "x^2"); // 7 factors
182 match(str, len, c[xy_coeff], "x y"); // 8 factors
183 match(str, len, c[yy_coeff], "y^2"); // 7 factors
184 match(str, len, c[x_coeff], "x"); // 21 factors
185 match(str, len, c[y_coeff], "y"); // 21 factors
186 match(str, len, c[c_coeff], ""); // 34 factors : total 102
caryclark@google.com639df892012-01-10 21:46:10 +0000187#define COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS 0
188#define WRITE_AS_NONOPTIMIZED_C_CODE 0
189#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
190 int count[factors][factors][factors];
191 bzero(count, sizeof(count));
192#endif
193#if WRITE_AS_NONOPTIMIZED_C_CODE
194 printf("// start of generated code");
195#endif
196 for (n_coeffs::iterator it = c.begin(); it < c.end(); ++it) {
197 coeffs& co = *it;
198#if WRITE_AS_NONOPTIMIZED_C_CODE
199 printf("\nstatic double calc_%c(double a, double b, double c, double d,"
200 "\n double e, double f, double g, double h) {"
201 "\n return"
202 "\n ", 'A' + (it - c.begin()));
203 if (co[0].s > 0) {
204 printf(" ");
205 }
206 if (abs(co[0].s) == 1) {
207 printf(" ");
208 }
209#endif
210 for (coeffs::iterator ct = co.begin(); ct < co.end(); ++ct) {
211 const coeff& cf = *ct;
212#if WRITE_AS_NONOPTIMIZED_C_CODE
213 printf(" ");
214 bool firstFactor = false;
215 if (ct - co.begin() > 0 || cf.s < 0) {
216 printf("%c", cf.s < 0 ? '-' : '+');
217 }
218 if (ct - co.begin() > 0) {
219 printf(" ");
220 }
221 if (abs(cf.s) > 1) {
222 printf("%d * ", abs(cf.s));
223 } else {
224 if (ct - co.begin() > 0) {
225 printf(" ");
226 }
227 }
228#endif
229 for (int x = 0; x < factors; ++x) {
230 if (cf.n[x] == 0) {
231 continue;
232 }
233#if WRITE_AS_NONOPTIMIZED_C_CODE
234 for (int y = 0 ; y < cf.n[x]; ++y) {
235 if (y > 0 || firstFactor) {
236 printf(" * ");
237 }
238 printf("%c", 'a' + x);
239 }
240 firstFactor = true;
241#endif
242#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
243 for (int y = x; y < factors; ++y) {
244 if (cf.n[y] == 0) {
245 continue;
246 }
247 if (x == y && cf.n[y] == 1) {
248 continue;
249 }
250 for (int z = y; z < factors; ++z) {
251 if (cf.n[z] == 0) {
252 continue;
253 }
254 if ((x == z || y == z) && cf.n[z] == 1) {
255 continue;
256 }
257 if (x == y && y == z && cf.n[z] == 2) {
258 continue;
259 }
260 count[x][y][z]++;
261 }
262 }
263#endif
264 }
265#if WRITE_AS_NONOPTIMIZED_C_CODE
266 if (ct + 1 < co.end()) {
267 printf("\n");
268 }
269#endif
270 }
271#if WRITE_AS_NONOPTIMIZED_C_CODE
272 printf(";\n}\n");
273#endif
274 }
275#if WRITE_AS_NONOPTIMIZED_C_CODE
276 printf("// end of generated code\n");
277#endif
278#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
279 const int bestCount = 20;
280 int best[bestCount][4];
281 bzero(best, sizeof(best));
282 for (int x = 0; x < factors; ++x) {
283 for (int y = x; y < factors; ++y) {
284 for (int z = y; z < factors; ++z) {
285 if (!count[x][y][z]) {
286 continue;
287 }
288 for (int w = 0; w < bestCount; ++w) {
289 if (best[w][0] < count[x][y][z]) {
290 best[w][0] = count[x][y][z];
291 best[w][1] = x;
292 best[w][2] = y;
293 best[w][3] = z;
294 break;
295 }
296 }
297 }
298 }
299 }
300 for (int w = 0; w < bestCount; ++w) {
301 printf("%c%c%c=%d\n", 'a' + best[w][1], 'a' + best[w][2],
302 'a' + best[w][3], best[w][0]);
303 }
304#endif
305#if WRITE_AS_NONOPTIMIZED_C_CODE
306 printf("\n");
307#endif
308}
309
310/* results: variable triplets used 10 or more times:
311aah=14
312ade=14
313aeh=14
314dee=14
315bce=13
316beg=13
317beh=12
318bbe=11
319bef=11
320cee=11
321cef=11
322def=11
323ceh=10
324deg=10
325*/