4 (gcd_all_options(f,g) := append(makelist((gcd:option, gcd(f,g)), option, [ez, subres, red, spmod]),
5 makelist((gcd:option, gcd(g,f)), option, [ez, subres, red, spmod])),
6 check_gcd(f,g,expected_result) := block([gcds], gcds:gcd_all_options(expand(f), expand(g)),
7 if every (lambda ([e], equal (e, expected_result)), gcds)
9 else FAILED_GCDS ('expected = expected_result, 'actual = gcds)),
13 /* Basic tests: numbers */
27 check_gcd(3/2, 1/2, 1/2);
30 check_gcd(3/2, -1/2, 1/2);
33 check_gcd(1+%i, 2+2*%i, 1+%i);
36 check_gcd(1+%i, -1/2-1/2*%i, 1/2+1/2*%i);
39 /* Basic tests: univariate polynomials */
47 check_gcd(2*x*(3*x-1), 2*x, 2*x);
50 check_gcd(2*x*(3*x-1), 3*x-1, 3*x-1);
53 check_gcd(2*x*(3*x-1), 6*x*(3*x-1), 2*x*(3*x-1));
56 check_gcd(2*x*(3*x-1), 3*x+1, 1);
59 /* Basic tests: multivariate polynomials */
64 check_gcd(6*y*x, 2, 2);
67 check_gcd(2*y*(3*x-1)*(y+1), 2*y, 2*y);
70 check_gcd(2*y*(3*x-1)*(y+1), (3*x-1)*(y-1), 3*x-1);
73 check_gcd(2*y*(3*x-1)*(y+1), 2*y*(3*x-1)*(y-1), 2*y*(3*x-1));
76 check_gcd(2*y*(3*x-1)*(y+1), 6*y*(3*x-1)*(y+1), 2*y*(3*x-1)*(y+1));
79 check_gcd(2*y*(3*x-1)*(y+1), (3*x+1)*(y-1), 1);
82 /* Randomized tests */
84 (random_polynomial(var) := product(random(3)+1+sum((random(4)-2)*var^i, i, 1, random(3)), j, 0, random(3)),
85 check_gcd_common_factor(f,g,common_factor,vars) :=
86 block([gcds], gcds: gcd_all_options (expand (f), expand (g)),
87 if every (lambda ([x], equal (remainder (x, common_factor), 0)), gcds)
89 else FAILED_GCDS ('common_factor = common_factor, 'gcd_results = gcds)),
93 makelist(block([common_factor],
94 common_factor:random_polynomial(x),
95 check_gcd_common_factor(common_factor*random_polynomial(x),
96 common_factor*random_polynomial(x),
100 [true, true, true, true, true, true, true, true, true, true]$
102 makelist(block([common_factor],
103 common_factor:random_polynomial(x)*random_polynomial(y),
104 check_gcd_common_factor(common_factor*random_polynomial(x)*random_polynomial(y),
105 common_factor*random_polynomial(x)*random_polynomial(y),
109 [true, true, true, true, true, true, true, true, true, true]$
111 makelist(block([common_factor],
112 common_factor:random_polynomial(x)*random_polynomial(y)*random_polynomial(z),
113 check_gcd_common_factor(common_factor*random_polynomial(x)*random_polynomial(y)*random_polynomial(z),
114 common_factor*random_polynomial(x)*random_polynomial(y)*random_polynomial(z),
118 [true, true, true, true, true, true, true, true, true, true]$
120 /* SF bug 3832 and merge request 25 */
122 is(equal(gcd(expand((r^2-2*m+a^2)*(a^2*s^2+r^2)^3*(a^2*s^2+r^2-2*m)), expand((r^2-2*m+a^2)*(a^2*s^2+r^2)^2)),
123 expand((r^2-2*m+a^2)*(a^2*s^2+r^2)^2)));