Skip to content

Commit b6da6a1

Browse files
authored
Move gcd tests to separate file (#295)
* Move gcd tests to separate file * Fix format
1 parent 9eb6bf4 commit b6da6a1

File tree

3 files changed

+295
-292
lines changed

3 files changed

+295
-292
lines changed

test/commutativetests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ include("comparison.jl")
2121
include("substitution.jl")
2222
include("differentiation.jl")
2323
include("division.jl")
24+
include("gcd.jl")
2425
include("chain_rules.jl")
2526

2627
include("show.jl")

test/division.jl

Lines changed: 0 additions & 292 deletions
Original file line numberDiff line numberDiff line change
@@ -111,223 +111,6 @@ function multi_div_test()
111111
)) == -((1 + 1e-10) - 1)x + 1
112112
end
113113

114-
function test_gcd_unit(expected, p1, p2, algo)
115-
g = @inferred gcd(p1, p2, algo)
116-
# it does not make sense, in general, to speak of "the" greatest common
117-
# divisor of u and v; there is a set of greatest common divisors, each
118-
# one being a unit multiple of the others [Knu14, p. 424] so `expected` and
119-
# `-expected` are both accepted.
120-
@test g == expected || g == -expected
121-
end
122-
123-
function test_gcdx_unit(expected, p1, p2, algo)
124-
test_gcd_unit(expected, p1, p2, algo)
125-
if !(algo isa SubresultantAlgorithm) # FIXME not implemented yet
126-
a, b, g = @inferred gcdx(p1, p2, algo)
127-
# it does not make sense, in general, to speak of "the" greatest common
128-
# divisor of u and v; there is a set of greatest common divisors, each
129-
# one being a unit multiple of the others [Knu14, p. 424] so `expected` and
130-
# `-expected` are both accepted.
131-
@test iszero(MP.pseudo_rem(g, expected, algo))
132-
@test a * p1 + b * p2 == g
133-
end
134-
end
135-
function _test_gcdx_unit(expected, p1, p2, algo)
136-
test_gcdx_unit(expected, p1, p2, algo)
137-
return test_gcdx_unit(expected, p2, p1, algo)
138-
end
139-
140-
function univariate_gcd_test(algo = GeneralizedEuclideanAlgorithm())
141-
Mod.@polyvar x
142-
test_gcdx_unit(x + 1, x^2 - 1, x^2 + 2x + 1, algo)
143-
test_gcdx_unit(x + 1, x^2 + 2x + 1, x^2 - 1, algo)
144-
test_gcdx_unit(x + 1, x^2 - 1, x + 1, algo)
145-
test_gcdx_unit(x + 1, x + 1, x^2 - 1, algo)
146-
test_gcdx_unit(x + 1, x - x, x + 1, algo)
147-
test_gcdx_unit(x + 1, x + 1, x - x, algo)
148-
test_gcdx_unit(x - x + 1, x + 1, x + 2, algo)
149-
test_gcdx_unit(x - x + 1, x + 1, 2x + 1, algo)
150-
test_gcdx_unit(x - x + 1, x - 1, 2x^2 - 2x - 2, algo)
151-
@test 0 == @inferred gcd(x - x, x^2 - x^2, algo)
152-
@test 0 == @inferred gcd(x^2 - x^2, x - x, algo)
153-
end
154-
155-
function _mult_test(a::Number, b)
156-
@test iszero(maxdegree(b))
157-
end
158-
function _mult_test(a::Number, b::Number)
159-
@test iszero(rem(a, b))
160-
@test iszero(rem(b, a))
161-
end
162-
function _mult_test(a, b)
163-
@test iszero(rem(a, b))
164-
@test iszero(rem(b, a))
165-
end
166-
167-
include("independent.jl")
168-
169-
function _gcd_test(expected, a, b, algo, expected_type)
170-
A = deepcopy(a)
171-
B = deepcopy(b)
172-
g = @inferred MP._simplifier(
173-
a,
174-
b,
175-
algo,
176-
MA.IsNotMutable(),
177-
MA.IsNotMutable(),
178-
)
179-
@test are_independent(g, a)
180-
@test are_independent(g, b)
181-
@test g isa expected_type
182-
return _mult_test(expected, g)
183-
end
184-
185-
function mult_test(expected, a, b, algo)
186-
return _gcd_test(expected, a, b, algo, Base.promote_typeof(a, b))
187-
end
188-
function mult_test(expected, a::Number, b, algo)
189-
return _gcd_test(
190-
expected,
191-
a,
192-
b,
193-
algo,
194-
promote_type(typeof(a), MP.coefficient_type(b)),
195-
)
196-
end
197-
function mult_test(expected, a, b::Number, algo)
198-
return _gcd_test(
199-
expected,
200-
a,
201-
b,
202-
algo,
203-
promote_type(MP.coefficient_type(a), typeof(b)),
204-
)
205-
end
206-
function sym_test(a, b, g, algo)
207-
mult_test(g, a, b, algo)
208-
return mult_test(g, b, a, algo)
209-
end
210-
function triple_test(a, b, c, algo)
211-
sym_test(a * c, b * c, gcd(a, b, algo) * c, algo)
212-
sym_test(b * a, c * a, gcd(b, c, algo) * a, algo)
213-
return sym_test(c * b, a * b, gcd(c, a, algo) * b, algo)
214-
end
215-
216-
function multivariate_gcd_test(
217-
::Type{T},
218-
algo = SubresultantAlgorithm(),
219-
) where {T}
220-
Mod.@polyvar x y z
221-
o = one(T)
222-
zr = zero(T)
223-
sym_test(o, o * x, o, algo)
224-
sym_test(o * x, zr * x, o * x, algo)
225-
sym_test(o * x + o, zr * x, o * x + o, algo)
226-
# Inspired from https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/issues/160
227-
f1 = o * x * y + o * x
228-
f2 = o * y^2
229-
f3 = o * x
230-
sym_test(f1, f2, 1, algo)
231-
sym_test(f2, f3, 1, algo)
232-
sym_test(f3, f1, x, algo)
233-
triple_test(f1, f2, f3, algo)
234-
235-
@testset "Issue #173" begin
236-
p1 = o * x * y + x
237-
p2 = x^2
238-
sym_test(p1, p2, x, algo)
239-
end
240-
241-
p1 = o * z - z
242-
p2 = z
243-
@test gcd(p1, p2, algo) == z
244-
p1 = o * y - y
245-
p2 = z
246-
@test gcd(p1, p2, algo) == z
247-
test_relatively_prime(p1, p2, algo) = test_gcd_unit(o, p1, p2, algo)
248-
test_relatively_prime(2o * y * z - o, y - z, algo)
249-
test_relatively_prime(2o * y^2 * z - y, y - z, algo)
250-
test_relatively_prime(2o * y^2 * z - y, y^3 * z - y - z, algo)
251-
test_relatively_prime(
252-
2o * y^3 + 2 * y^2 * z - y,
253-
y^3 + y^3 * z - y - z,
254-
algo,
255-
)
256-
test_relatively_prime(
257-
z^4 + 2o * y^3 + 2 * y^2 * z - y,
258-
y * z^4 + y^3 + y^3 * z - y - z,
259-
algo,
260-
)
261-
test_relatively_prime(
262-
y * z^3 + z^4 + 2o * y^3 + 2 * y^2 * z - y,
263-
y^2 * z^3 + y * z^4 + y^3 + y^3 * z - y - z,
264-
algo,
265-
)
266-
if T != Int
267-
test_relatively_prime(
268-
-3o * y^2 * z^3 - 3 * y^4 + y * z^3 + z^4 + 2 * y^3 + 2 * y^2 * z -
269-
y,
270-
3o * y^3 * z^3 - 2 * y^5 + y^2 * z^3 + y * z^4 + y^3 + y^3 * z - y -
271-
z,
272-
algo,
273-
)
274-
end
275-
test_relatively_prime(
276-
-z^6 - 3o * y^2 * z^3 - 3 * y^4 +
277-
y * z^3 +
278-
z^4 +
279-
2 * y^3 +
280-
2 * y^2 * z - y,
281-
-y * z^6 - 3o * y^3 * z^3 - 2 * y^5 +
282-
y^2 * z^3 +
283-
y * z^4 +
284-
y^3 +
285-
y^3 * z - y - z,
286-
algo,
287-
)
288-
test_relatively_prime(
289-
-z^6 - 3o * y^2 * z^3 - 3 * y^4 +
290-
y * z^3 +
291-
z^4 +
292-
2 * y^3 +
293-
2 * y^2 * z - y,
294-
-y^2 * z^6 - 3o * y^4 * z^3 - 2 * y^6 +
295-
y^3 * z^3 +
296-
y^2 * z^4 +
297-
y^5 +
298-
y^4 * z - y^2 - y * z,
299-
algo,
300-
)
301-
a = (o * x + o * y^2) * (o * z^3 + o * y^2 + o * x)
302-
b = (o * x + o * y + o * z) * (o * x^2 + o * y)
303-
c = (o * x + o * y + o * z) * (o * z^3 + o * y^2 + o * x)
304-
# if T != Int || (
305-
# algo != GeneralizedEuclideanAlgorithm(false, false) &&
306-
# algo != GeneralizedEuclideanAlgorithm(true, false) &&
307-
# algo != GeneralizedEuclideanAlgorithm(true, true)
308-
# )
309-
# sym_test(a, b, 1, algo)
310-
# end
311-
sym_test(b, c, x + y + z, algo)
312-
sym_test(c, a, z^3 + y^2 + x, algo)
313-
# if T != Int && (
314-
# T != Float64 || (
315-
# algo != GeneralizedEuclideanAlgorithm(false, true) &&
316-
# algo != GeneralizedEuclideanAlgorithm(true, true)
317-
# )
318-
# )
319-
# triple_test(a, b, c, algo)
320-
# end
321-
322-
# https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/issues/195
323-
return sym_test(
324-
x * (y^2) + 2x * y * z + x * (z^2) + x * y + x * z,
325-
y + z,
326-
y + z,
327-
algo,
328-
)
329-
end
330-
331114
function lcm_test()
332115
Mod.@polyvar x
333116
l = @inferred lcm(x^2 - 1, x^2 + 2x + 1)
@@ -343,56 +126,6 @@ function lcm_test()
343126
@test 0 == @inferred lcm(x^2 - x^2, x - x)
344127
end
345128

346-
function deflation_test()
347-
Mod.@polyvar a b c
348-
function _test(p, eshift, edefl)
349-
shift, defl = MP.deflation(p)
350-
@test shift == eshift
351-
@test defl == edefl
352-
q = MP.deflate(p, shift, defl)
353-
@test p == MP.inflate(q, shift, defl)
354-
end
355-
for (x, y, z) in permutations((a, b, c))
356-
_test(x + x^2 + y * x, x, x * y)
357-
p = x^2 + x^2 * y^3 + y^6 * x^4
358-
_test(p, x^2, x^2 * y^3)
359-
@test p == MP.deflate(p, z^0, z^0)
360-
_test(p, x^2, x^2 * y^3)
361-
end
362-
end
363-
364-
function extracted_variable_test()
365-
Mod.@polyvar x y z
366-
function _test(p1, p2, w1, w2)
367-
i1, i2, n = MP._extracted_variable(p1, p2)
368-
v(p, i) = iszero(i) ? nothing : variables(p)[i]
369-
if string(Mod) == "DynamicPolynomials"
370-
alloc_test(() -> MP._extracted_variable(p1, p2), 0)
371-
end
372-
@test v(p1, i1) == w1
373-
@test v(p2, i2) == w2
374-
i2, i1, n = MP._extracted_variable(p2, p1)
375-
if string(Mod) == "DynamicPolynomials"
376-
alloc_test(() -> MP._extracted_variable(p2, p1), 0)
377-
end
378-
@test v(p1, i1) == w1
379-
@test v(p2, i2) == w2
380-
end
381-
_test(x - x, y - y, nothing, nothing)
382-
_test(x + y + z, x - z, y, nothing)
383-
_test(x + y + z, x - y, z, nothing)
384-
_test(x + y + z, y - z, x, nothing)
385-
_test(x^4 + y^5 + z^6, x^3 + y^2 + z, z, z)
386-
_test(x^6 + y^5 + z^4, x + y^2 + z^3, x, x)
387-
_test(x^6 + y + z^4 - y, x + y + z^3 - y, x, x)
388-
return _test(
389-
x * (y^2) + 2x * y * z + x * (z^2) + x * y + x * z,
390-
y + z,
391-
y,
392-
y,
393-
)
394-
end
395-
396129
@testset "Division" begin
397130
@testset "div by number" begin
398131
div_number_test()
@@ -412,31 +145,6 @@ end
412145
@testset "Division by multiple polynomials examples" begin
413146
multi_div_test()
414147
end
415-
univariate_gcd_test(SubresultantAlgorithm())
416-
@testset "Univariate gcd primitive_rem=$primitive_rem" for primitive_rem in
417-
[false, true]
418-
@testset "skip_last=$skip_last" for skip_last in [false, true]
419-
univariate_gcd_test(
420-
GeneralizedEuclideanAlgorithm(primitive_rem, skip_last),
421-
)
422-
end
423-
end
424-
@testset "Multivariate gcd $T" for T in
425-
[Int, BigInt, Rational{BigInt}, Float64]
426-
if T != Rational{BigInt} || VERSION >= v"1.6"
427-
multivariate_gcd_test(T, SubresultantAlgorithm())
428-
# `gcd` for `Rational{BigInt}` got defined at some point between v1.0 and v1.6
429-
@testset "primitive_rem=$primitive_rem" for primitive_rem in
430-
[false, true]
431-
@testset "skip_last=$skip_last" for skip_last in [false, true]
432-
multivariate_gcd_test(
433-
T,
434-
GeneralizedEuclideanAlgorithm(primitive_rem, skip_last),
435-
)
436-
end
437-
end
438-
end
439-
end
440148
@testset "lcm" begin
441149
lcm_test()
442150
end

0 commit comments

Comments
 (0)