From 016e801f80c38faeee453a5c3bf987a93e36c9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Tue, 8 Oct 2024 10:21:26 +0200 Subject: [PATCH] Add `zero!`, `one!`, `neg!` for MPoly --- src/generic/MPoly.jl | 42 ++++++++++++++++++--- test/Rings-conformance-tests.jl | 12 ++++++ test/generic/FreeAssociativeAlgebra-test.jl | 4 +- test/generic/MPoly-test.jl | 15 ++++++++ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/generic/MPoly.jl b/src/generic/MPoly.jl index 0a4d93e5b..96861478b 100644 --- a/src/generic/MPoly.jl +++ b/src/generic/MPoly.jl @@ -3886,6 +3886,43 @@ end # ############################################################################### +function zero!(a::MPoly{T}) where {T <: RingElement} + a.length = 0 + return a +end + +function one!(a::MPoly{T}) where {T <: RingElement} + a.length = 1 + fit!(a, 1) + a.coeffs[1] = one(base_ring(a)) + a.exps = zero(a.exps) + return a +end + +function neg!(a::MPoly{T}) where {T <: RingElement} + for i in 1:length(a) + a.coeffs[i] = neg!(a.coeffs[i]) + end + return a +end + +function neg!(z::MPoly{T}, a::MPoly{T}) where {T <: RingElement} + if z === a + return neg!(a) + end + z.length = length(a) + fit!(z, length(a)) + for i in 1:length(a) + if isassigned(z.coeffs, i) + z.coeffs[i] = neg!(z.coeffs[i], a.coeffs[i]) + else + z.coeffs[i] = -a.coeffs[i] + end + end + z.exps[:,1:length(a)] .= a.exps[:,1:length(a)] + return z +end + function add!(a::MPoly{T}, b::MPoly{T}, c::MPoly{T}) where {T <: RingElement} t = b + c a.coeffs = t.coeffs @@ -3933,11 +3970,6 @@ function fit!(a::MPoly{T}, n::Int) where {T <: RingElement} end return nothing end -# -function zero!(a::MPoly{T}) where {T <: RingElement} - a.length = 0 - return a -end @doc raw""" setcoeff!(a::MPoly{T}, i::Int, c::T) where T <: RingElement diff --git a/test/Rings-conformance-tests.jl b/test/Rings-conformance-tests.jl index 8a97e7b28..442fbe839 100644 --- a/test/Rings-conformance-tests.jl +++ b/test/Rings-conformance-tests.jl @@ -42,6 +42,18 @@ function test_elem(Rx::AbstractAlgebra.PolyRing) return Rx(elem_type(R)[test_elem(R) for i in 1:rand(0:6)]) end +function test_elem(Rx::AbstractAlgebra.MPolyRing) + R = base_ring(Rx) + num_gens = ngens(Rx) + iszero(num_gens) && return Rx(test_elem(R)) + len_bound = 8 + exp_bound = rand(1:5) + len = rand(0:len_bound) + coeffs = [test_elem(R) for _ in 1:len] + exps = [[rand(0:exp_bound) for _ in 1:num_gens] for _ in 1:len] + return Rx(coeffs, exps) +end + function test_elem(S::Union{AbstractAlgebra.MatSpace, AbstractAlgebra.MatRing}) R = base_ring(S) diff --git a/test/generic/FreeAssociativeAlgebra-test.jl b/test/generic/FreeAssociativeAlgebra-test.jl index 31fcec114..5ee356686 100644 --- a/test/generic/FreeAssociativeAlgebra-test.jl +++ b/test/generic/FreeAssociativeAlgebra-test.jl @@ -220,8 +220,8 @@ end S, = free_associative_algebra(ZZ, 3) test_NCRing_interface(S) - R, x = QQ[:x] - S, = free_associative_algebra(R, 3) + R, = QQ[:x, :y] + S, = free_associative_algebra(R, :z => 1:3) test_NCRing_interface(S) end diff --git a/test/generic/MPoly-test.jl b/test/generic/MPoly-test.jl index 961ba2312..16045227a 100644 --- a/test/generic/MPoly-test.jl +++ b/test/generic/MPoly-test.jl @@ -1704,3 +1704,18 @@ end @test leading_coefficient(f, 1) == coefficients(f, 1)[end] @test content(f, 1) == y end + +@testset "Generic.MPoly.Ring_interface" begin + S, = polynomial_ring(QQ, 0) + test_Ring_interface_recursive(S) + + S, = polynomial_ring(QQ, 1) + test_Ring_interface_recursive(S) + + S, = polynomial_ring(ZZ, 2) + test_Ring_interface_recursive(S) + + R, = QQ[:x] + S, = polynomial_ring(R, :z => 1:3) + test_Ring_interface(S) # _recursive needs too many ressources +end