1 /** @file exam_polygcd.cpp
3 * Some test with polynomial GCD calculations. See also the checks for
4 * rational function normalization in normalization.cpp. */
7 * GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 using namespace GiNaC;
30 const int MAX_VARIABLES = 3;
32 static symbol x("x"), z("z");
33 static symbol y[MAX_VARIABLES];
36 static unsigned poly_gcd1()
38 for (int v=1; v<=MAX_VARIABLES; v++) {
41 for (int i=0; i<v; i++) {
46 ex f = (e1 + 1) * (e1 + 2);
47 ex g = e2 * (-pow(x, 2) * y[0] * 3 + pow(y[0], 2) - 1);
50 clog << "case 1, gcd(" << f << "," << g << ") = " << r << " (should be 1)" << endl;
57 // Linearly dense quartic inputs with quadratic GCDs
58 static unsigned poly_gcd2()
60 for (int v=1; v<=MAX_VARIABLES; v++) {
63 for (int i=0; i<v; i++) {
68 ex d = pow(e1 + 1, 2);
69 ex f = d * pow(e2 - 2, 2);
70 ex g = d * pow(e1 + 2, 2);
71 ex r = gcd(f.expand(), g.expand());
72 if (!(r - d).expand().is_zero()) {
73 clog << "case 2, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
80 // Sparse GCD and inputs where degrees are proportional to the number of variables
81 static unsigned poly_gcd3()
83 for (int v=1; v<=MAX_VARIABLES; v++) {
84 ex e1 = pow(x, v + 1);
85 for (int i=0; i<v; i++)
86 e1 += pow(y[i], v + 1);
91 ex r = gcd(f.expand(), g.expand());
92 if (!(r - d).expand().is_zero()) {
93 clog << "case 3, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
100 // Variation of case 3; major performance degradation with PRS
101 static unsigned poly_gcd3p()
103 for (int v=1; v<=MAX_VARIABLES; v++) {
104 ex e1 = pow(x, v + 1);
106 for (int i=0; i<v; i++) {
107 e1 += pow(y[i], v + 1);
114 ex r = gcd(f.expand(), g.expand());
115 if (!(r - d).expand().is_zero()) {
116 clog << "case 3p, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
123 // Quadratic non-monic GCD; f and g have other quadratic factors
124 static unsigned poly_gcd4()
126 for (int v=1; v<=MAX_VARIABLES; v++) {
127 ex e1 = pow(x, 2) * pow(y[0], 2);
128 ex e2 = pow(x, 2) - pow(y[0], 2);
130 for (int i=1; i<v; i++) {
138 ex g = d * pow(e3 + 2, 2);
139 ex r = gcd(f.expand(), g.expand());
140 if (!(r - d).expand().is_zero()) {
141 clog << "case 4, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
148 // Completely dense non-monic quadratic inputs with dense non-monic linear GCDs
149 static unsigned poly_gcd5()
151 for (int v=1; v<=MAX_VARIABLES; v++) {
155 for (int i=0; i<v; i++) {
164 ex r = gcd(f.expand(), g.expand());
165 if (!(r - d).expand().is_zero()) {
166 clog << "case 5, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
173 // Sparse non-monic quadratic inputs with linear GCDs
174 static unsigned poly_gcd5p()
176 for (int v=1; v<=MAX_VARIABLES; v++) {
178 for (int i=0; i<v; i++)
184 ex r = gcd(f.expand(), g.expand());
185 if (!(r - d).expand().is_zero()) {
186 clog << "case 5p, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
193 // Trivariate inputs with increasing degrees
194 static unsigned poly_gcd6()
198 for (int j=1; j<=MAX_VARIABLES; j++) {
199 ex d = pow(x, j) * y * (z - 1);
200 ex f = d * (pow(x, j) + pow(y, j + 1) * pow(z, j) + 1);
201 ex g = d * (pow(x, j + 1) + pow(y, j) * pow(z, j + 1) - 7);
202 ex r = gcd(f.expand(), g.expand());
203 if (!(r - d).expand().is_zero()) {
204 clog << "case 6, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
211 // Trivariate polynomials whose GCD has common factors with its cofactors
212 static unsigned poly_gcd7()
215 ex p = x - y * z + 1;
216 ex q = x - y + z * 3;
218 for (int j=1; j<=MAX_VARIABLES; j++) {
219 for (int k=j+1; k<=4; k++) {
220 ex d = pow(p, j) * pow(q, j);
221 ex f = pow(p, j) * pow(q, k);
222 ex g = pow(p, k) * pow(q, j);
224 if (!(r - d).expand().is_zero() && !(r + d).expand().is_zero()) {
225 clog << "case 7, gcd(" << f << "," << g << ") = " << r << " (should be " << d << ")" << endl;
233 unsigned exam_polygcd()
237 cout << "examining polynomial GCD computation" << flush;
239 result += poly_gcd1(); cout << '.' << flush;
240 result += poly_gcd2(); cout << '.' << flush;
241 result += poly_gcd3(); cout << '.' << flush;
242 result += poly_gcd3p(); cout << '.' << flush; // PRS "worst" case
243 result += poly_gcd4(); cout << '.' << flush;
244 result += poly_gcd5(); cout << '.' << flush;
245 result += poly_gcd5p(); cout << '.' << flush;
246 result += poly_gcd6(); cout << '.' << flush;
247 result += poly_gcd7(); cout << '.' << flush;
252 int main(int argc, char** argv)
254 return exam_polygcd();