Skip to content

Commit

Permalink
add SLPolyRing(r, :x => 1:3, :y => 2:4) etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jun 24, 2020
1 parent 8222869 commit b59c059
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/slpolys.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ SLPolyRing(r::Ring, s::Union{AbstractVector{<:AbstractString},

SLPolyRing(r::Ring, n::Integer) = SLPolyRing(r, [Symbol("x$i") for i=1:n])

# cf. mpoly.jl in Oscar
SLPolyRing(r::Ring, v::Pair{<:Union{String,Symbol},
<:AbstractVector{<:Integer}}...) =
SLPolyRing(r, [Symbol(s, n) for (s, ns) in v for n in ns])

base_ring(S::SLPolyRing) = S.base_ring

symbols(S::SLPolyRing) = S.S
Expand All @@ -38,6 +43,26 @@ function PolynomialRing(R::Ring, s)
S, gens(S)
end

function PolynomialRing(R::Ring, v::Pair{<:Union{String,Symbol},
<:AbstractVector{<:Integer}}...)
S = SLPolyRing(R, v...)

# TODO: enable on Julia 1.5 (required for init keyword)
# rs = Iterators.accumulate(v; init=0:0) do x, a
# last(x)+1:last(x)+length(a[2])
# end
rs = []
prev = 0
for a in v
newprev = prev+length(a[2])
push!(rs, prev+1:newprev)
prev = newprev
end

gs = gens(S)
S, (gs[r] for r in rs)...
end

Base.one(S::SLPolyRing) = S(one(base_ring(S)))
Base.zero(S::SLPolyRing) = S()

Expand Down
12 changes: 12 additions & 0 deletions test/straightline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ end
@test length(X) == 2
@test string.(X) == ["x1", "x2"]

S5 = SLPolyRing(zz, :x => 1:3, :y => [2, 4])
@test S5 isa SLPolyRing{Int}
XS = gens(S5)
@test string.(XS) == ["x1", "x2", "x3", "y2", "y4"]

S5, X, Y = SL.PolynomialRing(zz, :x => 1:3, "y" => [2, 4])
@test S5 isa SLPolyRing{Int}
XS = gens(S5)
@test string.(XS) == ["x1", "x2", "x3", "y2", "y4"]
@test X == XS[1:3]
@test Y == XS[4:5]

s1 = one(S)
@test s1 == 1
@test s1 isa SLPoly{Int}
Expand Down

0 comments on commit b59c059

Please sign in to comment.