Skip to content

Commit f50db22

Browse files
authored
Merge pull request #78 from ederc/faster-nemo-interface
Faster nemo interface
2 parents 5c1ba82 + 93bd89f commit f50db22

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

src/interfaces/nemo.jl

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
function _resize_ff!(a::FqMPolyRingElem, n::Int)
2+
if a.data isa Generic.MPoly
3+
fit!(a, n)
4+
elseif a.data isa fqPolyRepMPolyRingElem
5+
ccall((:fq_nmod_mpoly_resize, Nemo.libflint), Nothing,
6+
(Ref{fqPolyRepMPolyRingElem}, Int, Ref{fqPolyRepMPolyRing}), a.data,
7+
n, a.parent.data)
8+
else
9+
@assert a.data isa Nemo.fpMPolyRingElem
10+
ccall((:nmod_mpoly_resize, Nemo.libflint), Nothing,
11+
(Ref{fpMPolyRingElem}, Int, Ref{fpMPolyRing}), a.data, n,
12+
a.parent.data)
13+
end
14+
end
15+
16+
function _resize_qq!(a::QQMPolyRingElem, n::Int)
17+
ccall((:fmpq_mpoly_resize, Nemo.libflint), Cvoid, (Ref{QQMPolyRingElem}, Int, Ref{QQMPolyRing}), a, n, parent(a))
18+
end
119

220
@doc Markdown.doc"""
321
_convert_to_msolve(
@@ -93,14 +111,13 @@ function _convert_finite_field_array_to_abstract_algebra(
93111
nr_vars = nvars(R)
94112
CR = coefficient_ring(R)
95113

96-
basis = (typeof(R(0)))[]
97-
#= basis = Vector{MPolyRingElem}(undef, bld) =#
98-
99-
len = 0
114+
len = 0
115+
ctr = 0
100116

101117
if eliminate > 0
102118
z = zeros(Int, eliminate)
103119
end
120+
g = [zero(R) for j in 1:nr_gens]
104121
for i in 1:nr_gens
105122
#= check if element is part of the eliminated basis =#
106123
if eliminate > 0
@@ -110,18 +127,21 @@ function _convert_finite_field_array_to_abstract_algebra(
110127
end
111128
end
112129
if bcf[len+1] == 0
113-
push!(basis, R(0))
130+
g[i] = R(0)
114131
else
115-
g = MPolyBuildCtx(R)
132+
_resize_ff!(g[i], Int(blen[i]))
116133
for j in 1:blen[i]
117-
push_term!(g, CR(bcf[len+j]),
118-
convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
134+
Nemo.setcoeff!(g[i], j, CR(bcf[len+j]))
135+
end
136+
for j in 1:blen[i]
137+
Nemo.set_exponent_vector!(g[i], j, convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
119138
end
120-
push!(basis, finish(g))
121139
end
140+
ctr += 1
122141
len += blen[i]
123142
end
124-
143+
basis = g[1:ctr]
144+
sort_terms!.(basis)
125145
return basis
126146
end
127147

@@ -146,32 +166,32 @@ function _convert_rational_array_to_abstract_algebra(
146166
nr_vars = nvars(R)
147167
CR = coefficient_ring(R)
148168

149-
basis = (typeof(R(0)))[]
150-
151169
len = 0
152170

171+
g = [zero(R) for j in 1:nr_gens]
153172
for i in 1:nr_gens
154173
if bcf[len+1] == 0
155-
push!(basis, R(0))
174+
g[i] = R(0)
156175
else
157-
g = MPolyBuildCtx(R)
176+
_resize_qq!(g[i], Int(blen[i]))
158177
lc = bcf[len+1]
159178

160179
if normalize && lc != 1
161180
for j in 1:blen[i]
162-
push_term!(g, bcf[len+j]/lc,
163-
convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
181+
Nemo.setcoeff!(g[i], j, bcf[len+j]/lc)
164182
end
165183
else
166184
for j in 1:blen[i]
167-
push_term!(g, bcf[len+j],
168-
convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
185+
Nemo.setcoeff!(g[i], j, bcf[len+j])
169186
end
170187
end
171-
push!(basis, finish(g))
188+
for j in 1:blen[i]
189+
Nemo.set_exponent_vector!(g[i], j, convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
190+
end
172191
end
173192
len += blen[i]
174193
end
175194

176-
return basis
195+
sort_terms!.(g)
196+
return g
177197
end

test/interfaces/nemo.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
cmp = (Int32[1], BigInt[1], Int32[1, 0], 1)
1010
@test AlgebraicSolving._convert_to_msolve(I.gens) == cmp
1111
for _GF in [GF, AlgebraicSolving.Nemo.Native.GF]
12-
R, (x,y,z) = polynomial_ring(_GF(2147483659),["x","y","z"], internal_ordering=:degrevlex)
12+
R, (x,y,z) = polynomial_ring(GF(2147483659),["x","y","z"], internal_ordering=:degrevlex)
1313
F = [x^2+1-3, x*y-z, x*z^2-3*y^2]
1414
# prime is bigger than 2^31, should throw an error
1515
@test_throws ErrorException AlgebraicSolving._convert_to_msolve(F)
16-
R, (x,y,z) = polynomial_ring(_GF(101),["x","y","z"], internal_ordering=:degrevlex)
16+
R, (x,y,z) = polynomial_ring(GF(101),["x","y","z"], internal_ordering=:degrevlex)
1717
F = [x^2+1-3, x*y-z, x*z^2-3*y^2]
18-
@show F
1918
res = AlgebraicSolving._convert_to_msolve(F)
2019
@test AlgebraicSolving._convert_finite_field_array_to_abstract_algebra(Int32(3), res[1], res[2], res[3], R) == F
2120
end

0 commit comments

Comments
 (0)