1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include <vector>
8
9/* Given:
10 * Resultant[a*t^3 + b*t^2 + c*t + d - x, e*t^3 + f*t^2 + g*t + h - y, t]
11 */
12
13const char result1[] =
14"-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 + "
15" 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 - "
16" 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 - "
17" 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 + "
18" 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 - "
19" 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 + "
20" 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 + "
21" 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 - "
22" 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 - "
23" 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 + "
24" b^2 e g^2 x - 2 a c e g^2 x - a b f g^2 x + a^2 g^3 x + "
25" 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 + "
26" 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 - "
27" 3 d e^3 x^2 + c e^2 f x^2 - b e f^2 x^2 + a f^3 x^2 + "
28" 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 - "
29" 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 - "
30" 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 - "
31" 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 - "
32" 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 + "
33" 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 - "
34" 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 + "
35" 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 - "
36" 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 - "
37" 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 + "
38" 3 a^3 h y^2 + 3 a^2 e x y^2 - a^3 y^3";
39
40const size_t len1 = sizeof(result1) - 1;
41
42/* Given:
43 * Expand[
44 * Det[{{a, b, c, (d - x),      0,      0},
45 *      {0, a, b,      c,  (d - x),     0},
46 *      {0, 0, a,      b,       c, (d - x)},
47 *      {e, f, g, (h - y),      0,      0},
48 *      {0, e, f,      g,  (h - y),     0},
49 *      {0, 0, e,      f,       g, (h - y)}}]]
50 */
51 // result1 and result2 are the same. 102 factors:
52const char result2[] =
53"-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 + "
54" 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   - "
55" 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 - "
56" 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 + "
57" 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   - "
58" 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 + "
59" 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 + "
60" 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 - "
61" 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   - "
62" 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   + "
63" b^2 e g^2 x   - 2 a c e g^2 x - a b f g^2 x   + a^2 g^3 x     + "
64" 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   + "
65" 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 - "
66" 3 d e^3 x^2   + c e^2 f x^2   - b e f^2 x^2   + a f^3 x^2     + "
67" 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       - "
68" 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   - "
69" 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 - "
70" 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   - "
71" 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 + "
72" 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 - "
73" 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 + "
74" 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 - "
75" 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 - "
76" 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   + "
77" 3 a^3 h y^2   + 3 a^2 e x y^2 - a^3 y^3";
78
79const size_t len2 = sizeof(result2) - 1;
80
81/* Given: r1 = Resultant[
82 *      a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - x,
83 *      e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, t]
84 *        Collect[r1, {x, y}, Simplify]
85 *        CForm[%]
86 *      then use regex to replace Power\(([a-h]),3\) with \1*\1*\1
87 *                            and Power\(([a-h]),2\) with \1*\1
88 * yields:
89
90d*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)) -
91   h*(27*c*c*c*e*e - 27*c*c*(3*b*e*f - 3*a*f*f + 2*a*e*g) +
92      h*(-27*b*b*b*e + 27*a*b*b*f - 9*a*a*b*g + a*a*a*h) +
93      9*c*(9*b*b*e*g + a*b*(-9*f*g + 3*e*h) + a*a*(3*g*g - 2*f*h))) +
94   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) +
95      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)))
96
97- Power(e - 3*f + 3*g - h,3)*Power(x,3)
98
99+ 3*(6*b*d*d*e*e - d*d*d*e*e + 18*b*b*d*e*f - 18*b*d*d*e*f -
100      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 +
101      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) +
102      a*a*(-3*b*h*(2*g + h) + d*(-27*g*g + 9*g*h - h*(2*e + h) + 9*f*(g + h))) +
103      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)) +
104         d*d*(e*e + 9*f*(3*f - g) + e*(-9*f - 9*g + 2*h))) -
105      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)))) +
106      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)) +
107         a*d*(-9*f*f - 18*f*g + 6*g*g + f*h + e*(f + 12*g + h)) +
108         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
109
110- 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 -
111      9*b*b*b*(e + 2*h) - a*a*(d*(e - 9*f + 18*g - 7*h) + 3*c*(2*f - 6*g + h)) +
112      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)) +
113      9*b*b*(3*c*(e + g + h) + a*(f + 2*h) - d*(e - 2*(f - 3*g + h))) -
114      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) +
115         a*(c*(-3*e + 9*f + 9*g + 3*h) + d*(e + 3*f - 17*g + 7*h))))*Power(y,2)
116
117+ Power(a - 3*b + 3*c - d,3)*Power(y,3)
118
119+ 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 +
120         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 -
121         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 +
122         18*b*f*h*h - 6*a*g*h*h - 3*b*g*h*h + a*h*h*h +
123         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) +
124            e*(9*f*g + 6*g*g - 17*f*h - 3*g*h + 3*h*h)) -
125         d*(e*e*e + e*e*(-6*f - 3*g + 7*h) - 9*(2*f - g)*(f*f + g*g - f*(g + h)) +
126            e*(18*f*f + 9*g*g + 3*g*h + h*h - 3*f*(3*g + 7*h)))) )
127
128+ Power(x,2)*(3*(a - 3*b + 3*c - d)*Power(e - 3*f + 3*g - h,2)*y)
129
130+ 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 -
131         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 +
132         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 +
133         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) +
134         d*d*(e*e*e - 9*f*f*f + 9*e*f*(f + g) - e*e*(3*f + 6*g + h)) +
135         d*(-3*c*(-9*f*f*g + e*e*(2*f - 6*g - 3*h) + e*(9*f*g + 6*g*g + f*h)) +
136            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) +
137               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 -
138               e*(6*f*f + g*(18*g + h) - 3*f*(3*g + 4*h)))) +
139         3*c*(3*b*(e*e*h + 3*f*g*h - e*(3*f*g - 6*f*h + 6*g*h + h*h)) +
140            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))))) )
141
142+ 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 -
143         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 +
144         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 -
145         9*c*c*(2*e*e + 3*f*f + 3*f*h - 2*g*h + e*(-3*f - 4*g + h)) +
146         9*b*b*(3*g*g - 3*g*h + 2*h*(-2*f + h) + e*(-2*f + 3*g + h)) +
147         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) +
148            d*(-e*e + 12*f*f - 27*f*g + e*(-9*f + 20*g - 5*h) + g*(9*g + h))) +
149         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) +
150            d*(7*e*e + 9*f*f + 9*f*g - 6*g*g - f*h + e*(-14*f - 9*g + 5*h))))*y
151
152- x*3*Power(a - 3*b + 3*c - d,2)*(e - 3*f + 3*g - h)*Power(y,2)
153
154*/
155
156const int factors = 8;
157
158struct coeff {
159    int s; // constant and coefficient sign
160    int n[factors]; // 0 or power of a (1, 2, or 3) for a through h
161};
162
163enum {
164    xxx_coeff,
165    xxy_coeff,
166    xyy_coeff,
167    yyy_coeff,
168    xx_coeff,
169    xy_coeff,
170    yy_coeff,
171    x_coeff,
172    y_coeff,
173    c_coeff,
174    coeff_count
175};
176
177typedef std::vector<coeff> coeffs;
178typedef std::vector<coeffs> n_coeffs;
179
180static char skipSpace(const char* str, size_t& index) {
181    do {
182        ++index;
183    } while (str[index] == ' ');
184    return str[index];
185}
186
187static char backSkipSpace(const char* str, size_t& end) {
188    while (str[end - 1] == ' ') {
189        --end;
190    }
191    return str[end - 1];
192}
193
194static void match(const char* str, size_t len, coeffs& co, const char pattern[]) {
195    size_t patternLen = strlen(pattern);
196    size_t index = 0;
197    while (index < len) {
198        char ch = str[index];
199        if (ch != '-' && ch != '+') {
200            printf("missing sign\n");
201        }
202        size_t end = index + 1;
203        while (str[end] != '+' && str[end] != '-' && ++end < len) {
204            ;
205        }
206        backSkipSpace(str, end);
207        size_t idx = index;
208        index = end;
209        skipSpace(str, index);
210        if (!strncmp(&str[end - patternLen], pattern, patternLen) == 0) {
211            continue;
212        }
213        size_t endCoeff = end - patternLen;
214        char last = backSkipSpace(str, endCoeff);
215        if (last == '2' || last == '3') {
216            last = str[endCoeff - 3]; // skip ^2
217        }
218        if (last == 'x' || last == 'y') {
219            continue;
220        }
221        coeff c;
222        c.s = str[idx] == '-' ? -1 : 1;
223        bzero(c.n, sizeof(c.n));
224        ch = skipSpace(str, idx);
225        if (ch >= '2' && ch <= '6') {
226            c.s *= ch - '0';
227            ch = skipSpace(str, idx);
228        }
229        while (idx < endCoeff) {
230            char x = str[idx];
231            if (x < 'a' || x > 'a' + factors) {
232                printf("expected factor\n");
233            }
234            idx++;
235            int pow = 1;
236            if (str[idx] == '^') {
237                idx++;
238                char exp = str[idx];
239                if (exp < '2' || exp > '3') {
240                    printf("expected exponent\n");
241                }
242                pow = exp - '0';
243            }
244            skipSpace(str, idx);
245            c.n[x - 'a'] = pow;
246        }
247        co.push_back(c);
248    }
249}
250
251void cubecode_test(int test);
252
253void cubecode_test(int test) {
254    const char* str = test ? result2 : result1;
255    size_t len = strlen(str);
256    n_coeffs c(coeff_count);
257    match(str, len, c[xxx_coeff], "x^3");   // 1 factor
258    match(str, len, c[xxy_coeff], "x^2 y"); // 1 factor
259    match(str, len, c[xyy_coeff], "x y^2"); // 1 factor
260    match(str, len, c[yyy_coeff], "y^3");   // 1 factor
261    match(str, len, c[xx_coeff], "x^2");    // 7 factors
262    match(str, len, c[xy_coeff], "x y");    // 8 factors
263    match(str, len, c[yy_coeff], "y^2");    // 7 factors
264    match(str, len, c[x_coeff], "x");       // 21 factors
265    match(str, len, c[y_coeff], "y");       // 21 factors
266    match(str, len, c[c_coeff], "");        // 34 factors : total 102
267#define COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS 0
268#define WRITE_AS_NONOPTIMIZED_C_CODE 0
269#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
270    int count[factors][factors][factors];
271    bzero(count, sizeof(count));
272#endif
273#if WRITE_AS_NONOPTIMIZED_C_CODE
274    printf("// start of generated code");
275#endif
276    for (n_coeffs::iterator it = c.begin(); it < c.end(); ++it) {
277        coeffs& co = *it;
278#if WRITE_AS_NONOPTIMIZED_C_CODE
279        printf("\nstatic double calc_%c(double a, double b, double c, double d,"
280               "\n                     double e, double f, double g, double h) {"
281               "\n    return"
282               "\n ", 'A' + (it - c.begin()));
283        if (co[0].s > 0) {
284            printf(" ");
285        }
286        if (abs(co[0].s) == 1) {
287            printf("    ");
288        }
289#endif
290        for (coeffs::iterator ct = co.begin(); ct < co.end(); ++ct) {
291            const coeff& cf = *ct;
292#if WRITE_AS_NONOPTIMIZED_C_CODE
293            printf("        ");
294            bool firstFactor = false;
295            if (ct - co.begin() > 0 || cf.s < 0) {
296                printf("%c", cf.s < 0 ? '-' : '+');
297            }
298            if (ct - co.begin() > 0) {
299                printf(" ");
300            }
301            if (abs(cf.s) > 1) {
302                printf("%d * ", abs(cf.s));
303            } else {
304                if (ct - co.begin() > 0) {
305                    printf("    ");
306                }
307            }
308#endif
309            for (int x = 0; x < factors; ++x) {
310                if (cf.n[x] == 0) {
311                    continue;
312                }
313#if WRITE_AS_NONOPTIMIZED_C_CODE
314                for (int y = 0 ; y < cf.n[x]; ++y) {
315                    if (y > 0 || firstFactor) {
316                        printf(" * ");
317                    }
318                    printf("%c", 'a' + x);
319                }
320                firstFactor = true;
321#endif
322#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
323                for (int y = x; y < factors; ++y) {
324                    if (cf.n[y] == 0) {
325                        continue;
326                    }
327                    if (x == y && cf.n[y] == 1) {
328                        continue;
329                    }
330                    for (int z = y; z < factors; ++z) {
331                        if (cf.n[z] == 0) {
332                            continue;
333                        }
334                        if ((x == z || y == z) && cf.n[z] == 1) {
335                            continue;
336                        }
337                        if (x == y && y == z && cf.n[z] == 2) {
338                            continue;
339                        }
340                        count[x][y][z]++;
341                    }
342                }
343#endif
344            }
345#if WRITE_AS_NONOPTIMIZED_C_CODE
346            if (ct + 1 < co.end()) {
347                printf("\n");
348            }
349#endif
350        }
351#if WRITE_AS_NONOPTIMIZED_C_CODE
352            printf(";\n}\n");
353#endif
354    }
355#if WRITE_AS_NONOPTIMIZED_C_CODE
356    printf("// end of generated code\n");
357#endif
358#if COMPUTE_MOST_FREQUENT_EXPRESSION_TRIPLETS
359    const int bestCount = 20;
360    int best[bestCount][4];
361    bzero(best, sizeof(best));
362    for (int x = 0; x < factors; ++x) {
363        for (int y = x; y < factors; ++y) {
364            for (int z = y; z < factors; ++z) {
365                if (!count[x][y][z]) {
366                    continue;
367                }
368                for (int w = 0; w < bestCount; ++w) {
369                    if (best[w][0] < count[x][y][z]) {
370                        best[w][0] = count[x][y][z];
371                        best[w][1] = x;
372                        best[w][2] = y;
373                        best[w][3] = z;
374                        break;
375                    }
376                }
377            }
378        }
379    }
380    for (int w = 0; w < bestCount; ++w) {
381        printf("%c%c%c=%d\n", 'a' + best[w][1], 'a'  + best[w][2],
382            'a' + best[w][3], best[w][0]);
383    }
384#endif
385#if WRITE_AS_NONOPTIMIZED_C_CODE
386    printf("\n");
387#endif
388}
389
390/* results: variable triplets used 10 or more times:
391aah=14
392ade=14
393aeh=14
394dee=14
395bce=13
396beg=13
397beh=12
398bbe=11
399bef=11
400cee=11
401cef=11
402def=11
403ceh=10
404deg=10
405*/
406