blob: 9e7a3f69291efdfd76f2cec2eaf3928af5c1784e [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
7const char result[] =
8"-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
34const size_t len = sizeof(result) - 1;
35const int factors = 8;
36
37struct coeff {
38 int s; // constant and coefficient sign
39 int n[factors]; // 0 or power of a (1, 2, or 3) for a through h
40};
41
42enum {
43 xxx_coeff,
44 xxy_coeff,
45 xyy_coeff,
46 yyy_coeff,
47 xx_coeff,
48 xy_coeff,
49 yy_coeff,
50 x_coeff,
51 y_coeff,
52 c_coeff,
53 coeff_count
54};
55
56typedef std::vector<coeff> coeffs;
57typedef std::vector<coeffs> n_coeffs;
58
59static char skipSpace(size_t& index) {
60 do {
61 ++index;
62 } while (result[index] == ' ');
63 return result[index];
64}
65
66static char backSkipSpace(size_t& end) {
67 while (result[end - 1] == ' ') {
68 --end;
69 }
70 return result[end - 1];
71}
72
73static void match(coeffs& co, const char pattern[]) {
74 size_t patternLen = strlen(pattern);
75 size_t index = 0;
76 while (index < len) {
77 char ch = result[index];
78 if (ch != '-' && ch != '+') {
79 printf("missing sign\n");
80 }
81 size_t end = index + 1;
82 while (result[end] != '+' && result[end] != '-' && ++end < len) {
83 ;
84 }
85 backSkipSpace(end);
86 size_t idx = index;
87 index = end;
88 skipSpace(index);
89 if (!strncmp(&result[end - patternLen], pattern, patternLen) == 0) {
90 continue;
91 }
92 size_t endCoeff = end - patternLen;
93 char last = backSkipSpace(endCoeff);
94 if (last == '2' || last == '3') {
95 last = result[endCoeff - 3]; // skip ^2
96 }
97 if (last == 'x' || last == 'y') {
98 continue;
99 }
100 coeff c;
101 c.s = result[idx] == '-' ? -1 : 1;
102 bzero(c.n, sizeof(c.n));
103 ch = skipSpace(idx);
104 if (ch >= '2' && ch <= '6') {
105 c.s *= ch - '0';
106 ch = skipSpace(idx);
107 }
108 while (idx < endCoeff) {
109 char x = result[idx];
110 if (x < 'a' || x > 'a' + factors) {
111 printf("expected factor\n");
112 }
113 idx++;
114 int pow = 1;
115 if (result[idx] == '^') {
116 idx++;
117 char exp = result[idx];
118 if (exp < '2' || exp > '3') {
119 printf("expected exponent\n");
120 }
121 pow = exp - '0';
122 }
123 skipSpace(idx);
124 c.n[x - 'a'] = pow;
125 }
126 co.push_back(c);
127 }
128}
129
130void cubecode_test();
131
132void cubecode_test() {
133 n_coeffs c(coeff_count);
134 match(c[xxx_coeff], "x^3"); // 1 factor
135 match(c[xxy_coeff], "x^2 y"); // 1 factor
136 match(c[xyy_coeff], "x y^2"); // 1 factor
137 match(c[yyy_coeff], "y^3"); // 1 factor
138 match(c[xx_coeff], "x^2"); // 7 factors
139 match(c[xy_coeff], "x y"); // 8 factors
140 match(c[yy_coeff], "y^2"); // 7 factors
141 match(c[x_coeff], "x"); // 21 factors
142 match(c[y_coeff], "y"); // 21 factors
143 match(c[c_coeff], ""); // 34 factors
144#define COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS 0
145#define WRITE_AS_NONOPTIMIZED_C_CODE 0
146#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
147 int count[factors][factors][factors];
148 bzero(count, sizeof(count));
149#endif
150#if WRITE_AS_NONOPTIMIZED_C_CODE
151 printf("// start of generated code");
152#endif
153 for (n_coeffs::iterator it = c.begin(); it < c.end(); ++it) {
154 coeffs& co = *it;
155#if WRITE_AS_NONOPTIMIZED_C_CODE
156 printf("\nstatic double calc_%c(double a, double b, double c, double d,"
157 "\n double e, double f, double g, double h) {"
158 "\n return"
159 "\n ", 'A' + (it - c.begin()));
160 if (co[0].s > 0) {
161 printf(" ");
162 }
163 if (abs(co[0].s) == 1) {
164 printf(" ");
165 }
166#endif
167 for (coeffs::iterator ct = co.begin(); ct < co.end(); ++ct) {
168 const coeff& cf = *ct;
169#if WRITE_AS_NONOPTIMIZED_C_CODE
170 printf(" ");
171 bool firstFactor = false;
172 if (ct - co.begin() > 0 || cf.s < 0) {
173 printf("%c", cf.s < 0 ? '-' : '+');
174 }
175 if (ct - co.begin() > 0) {
176 printf(" ");
177 }
178 if (abs(cf.s) > 1) {
179 printf("%d * ", abs(cf.s));
180 } else {
181 if (ct - co.begin() > 0) {
182 printf(" ");
183 }
184 }
185#endif
186 for (int x = 0; x < factors; ++x) {
187 if (cf.n[x] == 0) {
188 continue;
189 }
190#if WRITE_AS_NONOPTIMIZED_C_CODE
191 for (int y = 0 ; y < cf.n[x]; ++y) {
192 if (y > 0 || firstFactor) {
193 printf(" * ");
194 }
195 printf("%c", 'a' + x);
196 }
197 firstFactor = true;
198#endif
199#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
200 for (int y = x; y < factors; ++y) {
201 if (cf.n[y] == 0) {
202 continue;
203 }
204 if (x == y && cf.n[y] == 1) {
205 continue;
206 }
207 for (int z = y; z < factors; ++z) {
208 if (cf.n[z] == 0) {
209 continue;
210 }
211 if ((x == z || y == z) && cf.n[z] == 1) {
212 continue;
213 }
214 if (x == y && y == z && cf.n[z] == 2) {
215 continue;
216 }
217 count[x][y][z]++;
218 }
219 }
220#endif
221 }
222#if WRITE_AS_NONOPTIMIZED_C_CODE
223 if (ct + 1 < co.end()) {
224 printf("\n");
225 }
226#endif
227 }
228#if WRITE_AS_NONOPTIMIZED_C_CODE
229 printf(";\n}\n");
230#endif
231 }
232#if WRITE_AS_NONOPTIMIZED_C_CODE
233 printf("// end of generated code\n");
234#endif
235#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
236 const int bestCount = 20;
237 int best[bestCount][4];
238 bzero(best, sizeof(best));
239 for (int x = 0; x < factors; ++x) {
240 for (int y = x; y < factors; ++y) {
241 for (int z = y; z < factors; ++z) {
242 if (!count[x][y][z]) {
243 continue;
244 }
245 for (int w = 0; w < bestCount; ++w) {
246 if (best[w][0] < count[x][y][z]) {
247 best[w][0] = count[x][y][z];
248 best[w][1] = x;
249 best[w][2] = y;
250 best[w][3] = z;
251 break;
252 }
253 }
254 }
255 }
256 }
257 for (int w = 0; w < bestCount; ++w) {
258 printf("%c%c%c=%d\n", 'a' + best[w][1], 'a' + best[w][2],
259 'a' + best[w][3], best[w][0]);
260 }
261#endif
262#if WRITE_AS_NONOPTIMIZED_C_CODE
263 printf("\n");
264#endif
265}
266
267/* results: variable triplets used 10 or more times:
268aah=14
269ade=14
270aeh=14
271dee=14
272bce=13
273beg=13
274beh=12
275bbe=11
276bef=11
277cee=11
278cef=11
279def=11
280ceh=10
281deg=10
282*/