From e0eddc36e8eba8ad1d0e9c5a630a08d6711804ff Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Mon, 3 Jul 2023 03:33:09 +0300 Subject: [PATCH] fix the bug fix zero input to normalform --- src/interface.jl | 29 +++++++++++++++++++++++++---- test/array_normalform.jl | 6 ++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index d68c3c90..3917773a 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -272,9 +272,19 @@ function normalform( #= check and set algorithm parameters =# metainfo = set_metaparameters(ring, ordering, false, :exact, rng) - iszerobasis = remove_zeros_from_input!(ring, basisexps, basiscoeffs) - iszerobasis && - (return convert_to_output(ring, tobereduced, tbrexps, tbrcoeffs, metainfo)) + # remove zeros from input + input_sz = length(tbrexps) + nonzero_indices = filter(i -> !isempty(tbrexps[i]), 1:input_sz) + tbrexps = tbrexps[nonzero_indices] + tbrcoeffs = tbrcoeffs[nonzero_indices] + + if isempty(tbrexps) + for i in 1:input_sz + push!(tbrexps, empty(basisexps[1])) + push!(tbrcoeffs, empty(basiscoeffs[1])) + end + return convert_to_output(ring, tobereduced, tbrexps, tbrcoeffs, metainfo) + end #= change input ordering if needed =# newring = assure_ordering!(ring, basisexps, basiscoeffs, metainfo.targetord) @@ -286,6 +296,17 @@ function normalform( bexps, bcoeffs = normal_form_f4(newring, basisexps, basiscoeffs, tbrexps, tbrcoeffs, rng) + # bring back zeros + bexps_with_zeros = Vector{eltype(bexps)}(undef, input_sz) + bcoeffs_with_zeros = Vector{eltype(bcoeffs)}(undef, input_sz) + bexps_with_zeros[nonzero_indices] = bexps + bcoeffs_with_zeros[nonzero_indices] = bcoeffs + for i in 1:input_sz + if !(i in nonzero_indices) + bexps_with_zeros[i] = empty(basisexps[1]) + bcoeffs_with_zeros[i] = empty(basiscoeffs[1]) + end + end #= Assuming ordering of `bexps` here matches `newring.ord` =# @@ -295,7 +316,7 @@ function normalform( # ring contains ordering of computation, it is the requested ordering #= convert result back to representation of input =# - convert_to_output(newring, tobereduced, bexps, bcoeffs, metainfo) + convert_to_output(newring, tobereduced, bexps_with_zeros, bcoeffs_with_zeros, metainfo) end """ diff --git a/test/array_normalform.jl b/test/array_normalform.jl index ef64dcee..5e337ad4 100644 --- a/test/array_normalform.jl +++ b/test/array_normalform.jl @@ -5,6 +5,12 @@ using AbstractAlgebra for field in [GF(17), GF(2^31 - 1), ZZ, QQ] R, (x, y) = PolynomialRing(field, ["x", "y"]) gb = [x, y] + + @test Groebner.normalform(gb, [R(0)]) == [R(0)] + @test Groebner.normalform(gb, [R(0), R(1), R(0), R(0)]) == [R(0), R(1), R(0), R(0)] + @test Groebner.normalform(gb, [R(0), R(0), R(0), R(0)]) == [R(0), R(0), R(0), R(0)] + @test Groebner.normalform(gb, [R(0), x, R(0), x + 1, y, R(0)]) == [R(0), R(0), R(0), R(1), R(0), R(0)] + @test Groebner.normalform(gb, [x, y + 1]) == [R(0), R(1)] @test Groebner.normalform(gb, [y + 1, x]) == [R(1), R(0)] @test Groebner.normalform(