blob: 15767ddff3e16bcb55a308c566a9bf51c864d668 [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
caryclark@google.com27accef2012-01-25 18:57:23 +000073const size_t len2 = sizeof(result2) - 1;
74
caryclark@google.comc6825902012-02-03 22:07:47 +000075/* Given: r1 = Resultant[
76 * a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - x,
77 * e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, t]
78 * Collect[r1, {x, y}, Simplify]
79 * CForm[%]
80 * then use regex to replace Power\(([a-h]),3\) with \1*\1*\1
81 * and Power\(([a-h]),2\) with \1*\1
82 * yields:
83
84d*d*d*e*e*e - 3*d*d*(3*c*e*e*f + 3*b*e*(-3*f*f + 2*e*g) + a*(9*f*f*f - 9*e*f*g + e*e*h)) -
85 h*(27*c*c*c*e*e - 27*c*c*(3*b*e*f - 3*a*f*f + 2*a*e*g) +
86 h*(-27*b*b*b*e + 27*a*b*b*f - 9*a*a*b*g + a*a*a*h) +
87 9*c*(9*b*b*e*g + a*b*(-9*f*g + 3*e*h) + a*a*(3*g*g - 2*f*h))) +
88 3*d*(9*c*c*e*e*g + 9*b*b*e*(3*g*g - 2*f*h) + 3*a*b*(-9*f*g*g + 6*f*f*h + e*g*h) +
89 a*a*(9*g*g*g - 9*f*g*h + e*h*h) + 3*c*(3*b*e*(-3*f*g + e*h) + a*(9*f*f*g - 6*e*g*g - e*f*h)))
90
91- Power(e - 3*f + 3*g - h,3)*Power(x,3)
92
93+ 3*(6*b*d*d*e*e - d*d*d*e*e + 18*b*b*d*e*f - 18*b*d*d*e*f -
94 9*b*d*d*f*f - 54*b*b*d*e*g + 12*b*d*d*e*g - 27*b*b*d*g*g - 18*b*b*b*e*h + 18*b*b*d*e*h +
95 18*b*b*d*f*h + a*a*a*h*h - 9*b*b*b*h*h + 9*c*c*c*e*(e + 2*h) +
96 a*a*(-3*b*h*(2*g + h) + d*(-27*g*g + 9*g*h - h*(2*e + h) + 9*f*(g + h))) +
97 a*(9*b*b*h*(2*f + h) - 3*b*d*(6*f*f - 6*f*(3*g - 2*h) + g*(-9*g + h) + e*(g + h)) +
98 d*d*(e*e + 9*f*(3*f - g) + e*(-9*f - 9*g + 2*h))) -
99 9*c*c*(d*e*(e + 2*g) + 3*b*(f*h + e*(f + h)) + a*(-3*f*f - 6*f*h + 2*(g*h + e*(g + h)))) +
100 3*c*(d*d*e*(e + 2*f) + a*a*(3*g*g + 6*g*h - 2*h*(2*f + h)) + 9*b*b*(g*h + e*(g + h)) +
101 a*d*(-9*f*f - 18*f*g + 6*g*g + f*h + e*(f + 12*g + h)) +
102 b*(d*(-3*e*e + 9*f*g + e*(9*f + 9*g - 6*h)) + 3*a*(h*(2*e - 3*g + h) - 3*f*(g + h)))))*y
103
104- 3*(18*c*c*c*e - 18*c*c*d*e + 6*c*d*d*e - d*d*d*e + 3*c*d*d*f - 9*c*c*d*g + a*a*a*h + 9*c*c*c*h -
105 9*b*b*b*(e + 2*h) - a*a*(d*(e - 9*f + 18*g - 7*h) + 3*c*(2*f - 6*g + h)) +
106 a*(-9*c*c*(2*e - 6*f + 2*g - h) + d*d*(-7*e + 18*f - 9*g + h) + 3*c*d*(7*e - 17*f + 3*g + h)) +
107 9*b*b*(3*c*(e + g + h) + a*(f + 2*h) - d*(e - 2*(f - 3*g + h))) -
108 3*b*(-(d*d*(e - 6*f + 2*g)) - 3*c*d*(e + 3*f + 3*g - h) + 9*c*c*(e + f + h) + a*a*(g + 2*h) +
109 a*(c*(-3*e + 9*f + 9*g + 3*h) + d*(e + 3*f - 17*g + 7*h))))*Power(y,2)
110
111+ Power(a - 3*b + 3*c - d,3)*Power(y,3)
112
113+ Power(x,2)*(-3*(-9*b*e*f*f + 9*a*f*f*f + 6*b*e*e*g - 9*a*e*f*g + 27*b*e*f*g - 27*a*f*f*g + 18*a*e*g*g - 54*b*e*g*g +
114 27*a*f*g*g + 27*b*f*g*g - 18*a*g*g*g + a*e*e*h - 9*b*e*e*h + 3*a*e*f*h + 9*b*e*f*h + 9*a*f*f*h -
115 18*b*f*f*h - 21*a*e*g*h + 51*b*e*g*h - 9*a*f*g*h - 27*b*f*g*h + 18*a*g*g*h + 7*a*e*h*h - 18*b*e*h*h - 3*a*f*h*h +
116 18*b*f*h*h - 6*a*g*h*h - 3*b*g*h*h + a*h*h*h +
117 3*c*(-9*f*f*(g - 2*h) + 3*g*g*h - f*h*(9*g + 2*h) + e*e*(f - 6*g + 6*h) +
118 e*(9*f*g + 6*g*g - 17*f*h - 3*g*h + 3*h*h)) -
119 d*(e*e*e + e*e*(-6*f - 3*g + 7*h) - 9*(2*f - g)*(f*f + g*g - f*(g + h)) +
120 e*(18*f*f + 9*g*g + 3*g*h + h*h - 3*f*(3*g + 7*h)))) )
121
122+ Power(x,2)*(3*(a - 3*b + 3*c - d)*Power(e - 3*f + 3*g - h,2)*y)
123
124+ x*(-3*(27*b*b*e*g*g - 27*a*b*f*g*g + 9*a*a*g*g*g - 18*b*b*e*f*h + 18*a*b*f*f*h + 3*a*b*e*g*h -
125 27*b*b*e*g*h - 9*a*a*f*g*h + 27*a*b*f*g*h - 9*a*a*g*g*h + a*a*e*h*h - 9*a*b*e*h*h +
126 27*b*b*e*h*h + 6*a*a*f*h*h - 18*a*b*f*h*h - 9*b*b*f*h*h + 3*a*a*g*h*h +
127 6*a*b*g*h*h - a*a*h*h*h + 9*c*c*(e*e*(g - 3*h) - 3*f*f*h + e*(3*f + 2*g)*h) +
128 d*d*(e*e*e - 9*f*f*f + 9*e*f*(f + g) - e*e*(3*f + 6*g + h)) +
129 d*(-3*c*(-9*f*f*g + e*e*(2*f - 6*g - 3*h) + e*(9*f*g + 6*g*g + f*h)) +
130 a*(-18*f*f*f - 18*e*g*g + 18*g*g*g - 2*e*e*h + 3*e*g*h + 2*e*h*h + 9*f*f*(3*g + 2*h) +
131 3*f*(6*e*g - 9*g*g - e*h - 6*g*h)) - 3*b*(9*f*g*g + e*e*(4*g - 3*h) - 6*f*f*h -
132 e*(6*f*f + g*(18*g + h) - 3*f*(3*g + 4*h)))) +
133 3*c*(3*b*(e*e*h + 3*f*g*h - e*(3*f*g - 6*f*h + 6*g*h + h*h)) +
134 a*(9*f*f*(g - 2*h) + f*h*(-e + 9*g + 4*h) - 3*(2*g*g*h + e*(2*g*g - 4*g*h + h*h))))) )
135
136+ x*3*(-2*a*d*e*e - 7*d*d*e*e + 15*a*d*e*f + 21*d*d*e*f - 9*a*d*f*f - 18*d*d*f*f - 15*a*d*e*g -
137 3*d*d*e*g - 9*a*a*f*g + 9*d*d*f*g + 18*a*a*g*g + 9*a*d*g*g + 2*a*a*e*h - 2*d*d*e*h +
138 3*a*a*f*h + 15*a*d*f*h - 21*a*a*g*h - 15*a*d*g*h + 7*a*a*h*h + 2*a*d*h*h -
139 9*c*c*(2*e*e + 3*f*f + 3*f*h - 2*g*h + e*(-3*f - 4*g + h)) +
140 9*b*b*(3*g*g - 3*g*h + 2*h*(-2*f + h) + e*(-2*f + 3*g + h)) +
141 3*b*(3*c*(e*e + 3*e*(f - 3*g) + (9*f - 3*g - h)*h) + a*(6*f*f + e*g - 9*f*g - 9*g*g - 5*e*h + 9*f*h + 14*g*h - 7*h*h) +
142 d*(-e*e + 12*f*f - 27*f*g + e*(-9*f + 20*g - 5*h) + g*(9*g + h))) +
143 3*c*(a*(-(e*f) - 9*f*f + 27*f*g - 12*g*g + 5*e*h - 20*f*h + 9*g*h + h*h) +
144 d*(7*e*e + 9*f*f + 9*f*g - 6*g*g - f*h + e*(-14*f - 9*g + 5*h))))*y
145
146- x*3*Power(a - 3*b + 3*c - d,2)*(e - 3*f + 3*g - h)*Power(y,2)
147
148*/
149
caryclark@google.com639df892012-01-10 21:46:10 +0000150const int factors = 8;
151
152struct coeff {
153 int s; // constant and coefficient sign
154 int n[factors]; // 0 or power of a (1, 2, or 3) for a through h
155};
156
157enum {
158 xxx_coeff,
159 xxy_coeff,
160 xyy_coeff,
161 yyy_coeff,
162 xx_coeff,
163 xy_coeff,
164 yy_coeff,
165 x_coeff,
166 y_coeff,
167 c_coeff,
168 coeff_count
169};
170
171typedef std::vector<coeff> coeffs;
172typedef std::vector<coeffs> n_coeffs;
173
caryclark@google.com27accef2012-01-25 18:57:23 +0000174static char skipSpace(const char* str, size_t& index) {
caryclark@google.com639df892012-01-10 21:46:10 +0000175 do {
176 ++index;
caryclark@google.com27accef2012-01-25 18:57:23 +0000177 } while (str[index] == ' ');
178 return str[index];
caryclark@google.com639df892012-01-10 21:46:10 +0000179}
180
caryclark@google.com27accef2012-01-25 18:57:23 +0000181static char backSkipSpace(const char* str, size_t& end) {
182 while (str[end - 1] == ' ') {
caryclark@google.com639df892012-01-10 21:46:10 +0000183 --end;
184 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000185 return str[end - 1];
caryclark@google.com639df892012-01-10 21:46:10 +0000186}
187
caryclark@google.com27accef2012-01-25 18:57:23 +0000188static void match(const char* str, size_t len, coeffs& co, const char pattern[]) {
caryclark@google.com639df892012-01-10 21:46:10 +0000189 size_t patternLen = strlen(pattern);
190 size_t index = 0;
191 while (index < len) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000192 char ch = str[index];
caryclark@google.com639df892012-01-10 21:46:10 +0000193 if (ch != '-' && ch != '+') {
194 printf("missing sign\n");
195 }
196 size_t end = index + 1;
caryclark@google.com27accef2012-01-25 18:57:23 +0000197 while (str[end] != '+' && str[end] != '-' && ++end < len) {
caryclark@google.com639df892012-01-10 21:46:10 +0000198 ;
199 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000200 backSkipSpace(str, end);
caryclark@google.com639df892012-01-10 21:46:10 +0000201 size_t idx = index;
202 index = end;
caryclark@google.com27accef2012-01-25 18:57:23 +0000203 skipSpace(str, index);
204 if (!strncmp(&str[end - patternLen], pattern, patternLen) == 0) {
caryclark@google.com639df892012-01-10 21:46:10 +0000205 continue;
206 }
207 size_t endCoeff = end - patternLen;
caryclark@google.com27accef2012-01-25 18:57:23 +0000208 char last = backSkipSpace(str, endCoeff);
caryclark@google.com639df892012-01-10 21:46:10 +0000209 if (last == '2' || last == '3') {
caryclark@google.com27accef2012-01-25 18:57:23 +0000210 last = str[endCoeff - 3]; // skip ^2
caryclark@google.com639df892012-01-10 21:46:10 +0000211 }
212 if (last == 'x' || last == 'y') {
213 continue;
214 }
215 coeff c;
caryclark@google.com27accef2012-01-25 18:57:23 +0000216 c.s = str[idx] == '-' ? -1 : 1;
caryclark@google.com639df892012-01-10 21:46:10 +0000217 bzero(c.n, sizeof(c.n));
caryclark@google.com27accef2012-01-25 18:57:23 +0000218 ch = skipSpace(str, idx);
caryclark@google.com639df892012-01-10 21:46:10 +0000219 if (ch >= '2' && ch <= '6') {
220 c.s *= ch - '0';
caryclark@google.com27accef2012-01-25 18:57:23 +0000221 ch = skipSpace(str, idx);
caryclark@google.com639df892012-01-10 21:46:10 +0000222 }
223 while (idx < endCoeff) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000224 char x = str[idx];
caryclark@google.com639df892012-01-10 21:46:10 +0000225 if (x < 'a' || x > 'a' + factors) {
226 printf("expected factor\n");
227 }
228 idx++;
229 int pow = 1;
caryclark@google.com27accef2012-01-25 18:57:23 +0000230 if (str[idx] == '^') {
caryclark@google.com639df892012-01-10 21:46:10 +0000231 idx++;
caryclark@google.com27accef2012-01-25 18:57:23 +0000232 char exp = str[idx];
caryclark@google.com639df892012-01-10 21:46:10 +0000233 if (exp < '2' || exp > '3') {
234 printf("expected exponent\n");
235 }
236 pow = exp - '0';
237 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000238 skipSpace(str, idx);
caryclark@google.com639df892012-01-10 21:46:10 +0000239 c.n[x - 'a'] = pow;
240 }
241 co.push_back(c);
242 }
243}
244
caryclark@google.com27accef2012-01-25 18:57:23 +0000245void cubecode_test(int test);
caryclark@google.com639df892012-01-10 21:46:10 +0000246
caryclark@google.com27accef2012-01-25 18:57:23 +0000247void cubecode_test(int test) {
248 const char* str = test ? result2 : result1;
249 size_t len = strlen(str);
caryclark@google.com639df892012-01-10 21:46:10 +0000250 n_coeffs c(coeff_count);
caryclark@google.com27accef2012-01-25 18:57:23 +0000251 match(str, len, c[xxx_coeff], "x^3"); // 1 factor
252 match(str, len, c[xxy_coeff], "x^2 y"); // 1 factor
253 match(str, len, c[xyy_coeff], "x y^2"); // 1 factor
254 match(str, len, c[yyy_coeff], "y^3"); // 1 factor
255 match(str, len, c[xx_coeff], "x^2"); // 7 factors
256 match(str, len, c[xy_coeff], "x y"); // 8 factors
257 match(str, len, c[yy_coeff], "y^2"); // 7 factors
258 match(str, len, c[x_coeff], "x"); // 21 factors
259 match(str, len, c[y_coeff], "y"); // 21 factors
260 match(str, len, c[c_coeff], ""); // 34 factors : total 102
caryclark@google.com639df892012-01-10 21:46:10 +0000261#define COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS 0
262#define WRITE_AS_NONOPTIMIZED_C_CODE 0
263#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
264 int count[factors][factors][factors];
265 bzero(count, sizeof(count));
266#endif
267#if WRITE_AS_NONOPTIMIZED_C_CODE
268 printf("// start of generated code");
269#endif
270 for (n_coeffs::iterator it = c.begin(); it < c.end(); ++it) {
271 coeffs& co = *it;
272#if WRITE_AS_NONOPTIMIZED_C_CODE
273 printf("\nstatic double calc_%c(double a, double b, double c, double d,"
274 "\n double e, double f, double g, double h) {"
275 "\n return"
276 "\n ", 'A' + (it - c.begin()));
277 if (co[0].s > 0) {
278 printf(" ");
279 }
280 if (abs(co[0].s) == 1) {
281 printf(" ");
282 }
283#endif
284 for (coeffs::iterator ct = co.begin(); ct < co.end(); ++ct) {
285 const coeff& cf = *ct;
286#if WRITE_AS_NONOPTIMIZED_C_CODE
287 printf(" ");
288 bool firstFactor = false;
289 if (ct - co.begin() > 0 || cf.s < 0) {
290 printf("%c", cf.s < 0 ? '-' : '+');
291 }
292 if (ct - co.begin() > 0) {
293 printf(" ");
294 }
295 if (abs(cf.s) > 1) {
296 printf("%d * ", abs(cf.s));
297 } else {
298 if (ct - co.begin() > 0) {
299 printf(" ");
300 }
301 }
302#endif
303 for (int x = 0; x < factors; ++x) {
304 if (cf.n[x] == 0) {
305 continue;
306 }
307#if WRITE_AS_NONOPTIMIZED_C_CODE
308 for (int y = 0 ; y < cf.n[x]; ++y) {
309 if (y > 0 || firstFactor) {
310 printf(" * ");
311 }
312 printf("%c", 'a' + x);
313 }
314 firstFactor = true;
315#endif
316#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
317 for (int y = x; y < factors; ++y) {
318 if (cf.n[y] == 0) {
319 continue;
320 }
321 if (x == y && cf.n[y] == 1) {
322 continue;
323 }
324 for (int z = y; z < factors; ++z) {
325 if (cf.n[z] == 0) {
326 continue;
327 }
328 if ((x == z || y == z) && cf.n[z] == 1) {
329 continue;
330 }
331 if (x == y && y == z && cf.n[z] == 2) {
332 continue;
333 }
334 count[x][y][z]++;
335 }
336 }
337#endif
338 }
339#if WRITE_AS_NONOPTIMIZED_C_CODE
340 if (ct + 1 < co.end()) {
341 printf("\n");
342 }
343#endif
344 }
345#if WRITE_AS_NONOPTIMIZED_C_CODE
346 printf(";\n}\n");
347#endif
348 }
349#if WRITE_AS_NONOPTIMIZED_C_CODE
350 printf("// end of generated code\n");
351#endif
352#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
353 const int bestCount = 20;
354 int best[bestCount][4];
355 bzero(best, sizeof(best));
356 for (int x = 0; x < factors; ++x) {
357 for (int y = x; y < factors; ++y) {
358 for (int z = y; z < factors; ++z) {
359 if (!count[x][y][z]) {
360 continue;
361 }
362 for (int w = 0; w < bestCount; ++w) {
363 if (best[w][0] < count[x][y][z]) {
364 best[w][0] = count[x][y][z];
365 best[w][1] = x;
366 best[w][2] = y;
367 best[w][3] = z;
368 break;
369 }
370 }
371 }
372 }
373 }
374 for (int w = 0; w < bestCount; ++w) {
375 printf("%c%c%c=%d\n", 'a' + best[w][1], 'a' + best[w][2],
376 'a' + best[w][3], best[w][0]);
377 }
378#endif
379#if WRITE_AS_NONOPTIMIZED_C_CODE
380 printf("\n");
381#endif
382}
383
384/* results: variable triplets used 10 or more times:
385aah=14
386ade=14
387aeh=14
388dee=14
389bce=13
390beg=13
391beh=12
392bbe=11
393bef=11
394cee=11
395cef=11
396def=11
397ceh=10
398deg=10
399*/