Skip to content

Commit

Permalink
Add zero!, one!, neg! for MPoly
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Oct 10, 2024
1 parent 4e7207e commit 016e801
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
42 changes: 37 additions & 5 deletions src/generic/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/Rings-conformance-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/generic/FreeAssociativeAlgebra-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

15 changes: 15 additions & 0 deletions test/generic/MPoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 016e801

Please sign in to comment.