From 0004eaafb3ac8097294f741cc9796036d3656ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Wed, 28 Feb 2024 12:36:31 +0100 Subject: [PATCH] Add `is_associated` --- src/AbstractAlgebra.jl | 1 + src/Rings.jl | 9 +++++++++ src/julia/Integer.jl | 4 ++++ test/Rings-test.jl | 16 ++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/src/AbstractAlgebra.jl b/src/AbstractAlgebra.jl index da5d59d619..0fe7cd1dd9 100644 --- a/src/AbstractAlgebra.jl +++ b/src/AbstractAlgebra.jl @@ -871,6 +871,7 @@ export inverse_fn export inverse_image_fn export inverse_mat export invmod +export is_associated export is_compatible export is_constant export is_degree diff --git a/src/Rings.jl b/src/Rings.jl index cc02013e26..0b3929006f 100644 --- a/src/Rings.jl +++ b/src/Rings.jl @@ -44,6 +44,15 @@ function is_divisible_by(x::T, y::T) where T <: RingElem return divides(x, y)[1] end +""" + is_associated(x::T, y::T) where T <: RingElem + +Check if `x` and `y` are associated, i.e. if `x` is a unit times `y`. +""" +function is_associated(x::T, y::T) where T <: RingElem + return is_divisible_by(x, y) && is_divisible_by(y, x) +end + ############################################################################### # # Evaluation diff --git a/src/julia/Integer.jl b/src/julia/Integer.jl index 4db4e9d093..07f0b079fb 100644 --- a/src/julia/Integer.jl +++ b/src/julia/Integer.jl @@ -149,6 +149,10 @@ function is_divisible_by(a::BigInt, b::UInt) (Ref{BigInt}, UInt), a, b)) end +function is_associated(x::Integer, y::Integer) + return x == y || x == -y +end + ############################################################################### # # Exact division diff --git a/test/Rings-test.jl b/test/Rings-test.jl index a790decd07..f4b2f89fe5 100644 --- a/test/Rings-test.jl +++ b/test/Rings-test.jl @@ -67,3 +67,19 @@ end @test is_finite(GF(2)) @test !is_finite(QQ) end + +@testset "is_associated" begin + F = GF(13) + for x in F, y in F + @test is_associated(x, y) == (iszero(x) == iszero(y)) + end + + Fx, x = polynomial_ring(F) + f = x^3 + 4x + 3 + @test is_associated(f, f) + @test is_associated(f, 6*f) + @test is_associated(7*f, f) + @test !is_associated(f, 0*f) + @test !is_associated(0*f, f) + @test !is_associated(f, 2x^2-x+2) +end