Skip to content

Commit 12a5cf5

Browse files
committed
Test is_power(::PolyRingElem, ::Int)
1 parent 6a174e4 commit 12a5cf5

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/generic/Poly-test.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,3 +3053,66 @@ end
30533053
@test is_separable(x)
30543054
@test !is_separable(x^2)
30553055
end
3056+
3057+
@testset "Generic.Poly.Misc" begin
3058+
# factor function is required for is_power. Implement naive ones here.
3059+
function AbstractAlgebra.factor(a::Integer)
3060+
R = parent(a)
3061+
f = Fac{typeof(a)}()
3062+
f.unit = sign(a)
3063+
a = abs(a)
3064+
for ix in [R(2), R(3), R(5), R(7), R(11)]
3065+
if is_zero(a % ix)
3066+
a /= ix
3067+
f.fac[ix] = 1
3068+
end
3069+
while is_zero(a % ix)
3070+
a /= ix
3071+
f.fac[ix] += 1
3072+
end
3073+
end
3074+
if !is_one(a)
3075+
error()
3076+
end
3077+
return f
3078+
end
3079+
function AbstractAlgebra.factor(p::PolyRingElem)
3080+
P = parent(p)
3081+
x = gen(P)
3082+
f = Fac{typeof(p)}()
3083+
ct = content(p)
3084+
cf = factor(ct)
3085+
ut = sign(leading_coefficient(p))
3086+
for (t, k) in cf
3087+
f.fac[P(t)] = k
3088+
end
3089+
f.unit = P(ut)
3090+
p /= ut * ct
3091+
for y in [x + c for c in -2:2]
3092+
if is_zero(p % y)
3093+
p /= y
3094+
f.fac[y] = 1
3095+
end
3096+
while is_zero(p % y)
3097+
p /= y
3098+
f.fac[y] += 1
3099+
end
3100+
end
3101+
if !is_one(p)
3102+
error()
3103+
end
3104+
return f
3105+
end
3106+
ZZx, x = ZZ[:x]
3107+
@test is_power(x, 2) == (false, x)
3108+
@test is_power(x, 3) == (false, x)
3109+
@test is_power(x^2, 2) == (true, x)
3110+
@test is_power(3 * x^2, 2) == (false, 3 * x^2)
3111+
@test is_power(2^2 * x^2, 2) == (true, 2 * x)
3112+
@test is_power(2^3 * x^3, 3) == (true, 2 * x)
3113+
@test is_power(3^4 * x^4, 2) == (true, 9 * x^2)
3114+
@test is_power(-3^4 * x^4, 2) == (false, -81 * x^4)
3115+
@test is_power(-3^3 * x^3, 3) == (true, -3 * x)
3116+
@test is_power(3^2 * (x + 1)^2 * x^2, 2) == (true, 3 * (x + 1) * x)
3117+
@test is_power(-3^2 * (x + 1)^2 * x^2, 2) == (false, -3^2 * (x + 1)^2 * x^2)
3118+
end

0 commit comments

Comments
 (0)