Skip to content

Commit dfa00f0

Browse files
committed
fixes resizing of polynomials thanks to @thofma
1 parent b0e0c22 commit dfa00f0

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

src/interfaces/nemo.jl

Lines changed: 38 additions & 19 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,39 +111,41 @@ 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-
99114
len = 0
100115

116+
@show eliminate
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
107124
cmp = convert(Vector{Int}, bexp[(len)*nr_vars+1:(len+1)*nr_vars])
125+
@show i
126+
@show cmp[1:eliminate]
127+
@show z
128+
@show cmp[1:eliminate] > z
108129
if cmp[1:eliminate] > z
109130
continue
110131
end
111132
end
112133
if bcf[len+1] == 0
113-
push!(basis, R(0))
134+
g[i] = R(0)
114135
else
115-
g = zero(R)
116-
ccall((:fq_nmod_mpoly_resize, Nemo.libflint), Cvoid, (Ref{FqMPolyRingElem}, Int, Ref{FqMPolyRing}), g, blen[i], parent(g))
136+
_resize_ff!(g[i], Int(blen[i]))
117137
for j in 1:blen[i]
118-
Nemo.setcoeff!(g, j, CR(bcf[len+j]))
138+
Nemo.setcoeff!(g[i], j, CR(bcf[len+j]))
119139
end
120140
for j in 1:blen[i]
121-
Nemo.set_exponent_vector!(g, j, convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
141+
Nemo.set_exponent_vector!(g[i], j, convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
122142
end
123-
push!(basis, g)
124143
end
125144
len += blen[i]
126145
end
127146

128-
return basis
147+
sort_terms!.(g)
148+
return g
129149
end
130150

131151
function _convert_rational_array_to_abstract_algebra(
@@ -149,33 +169,32 @@ function _convert_rational_array_to_abstract_algebra(
149169
nr_vars = nvars(R)
150170
CR = coefficient_ring(R)
151171

152-
basis = (typeof(R(0)))[]
153-
154172
len = 0
155173

174+
g = [zero(R) for j in 1:nr_gens]
156175
for i in 1:nr_gens
157176
if bcf[len+1] == 0
158-
push!(basis, R(0))
177+
g[i] = R(0)
159178
else
160-
g = zero(R)
179+
_resize_qq!(g[i], Int(blen[i]))
161180
lc = bcf[len+1]
162181

163182
if normalize && lc != 1
164183
for j in 1:blen[i]
165-
Nemo.setcoeff!(g, j, bcf[len+j]/lc)
184+
Nemo.setcoeff!(g[i], j, bcf[len+j]/lc)
166185
end
167186
else
168187
for j in 1:blen[i]
169-
Nemo.setcoeff!(g, j, bcf[len+j])
188+
Nemo.setcoeff!(g[i], j, bcf[len+j])
170189
end
171190
end
172191
for j in 1:blen[i]
173-
Nemo.set_exponent_vector!(g, j, convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
192+
Nemo.set_exponent_vector!(g[i], j, convert(Vector{Int}, bexp[(len+j-1)*nr_vars+1:(len+j)*nr_vars]))
174193
end
175-
push!(basis, g)
176194
end
177195
len += blen[i]
178196
end
179197

180-
return basis
198+
sort_terms!.(g)
199+
return g
181200
end

test/interfaces/nemo.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
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]
1818
@show F
1919
res = AlgebraicSolving._convert_to_msolve(F)

0 commit comments

Comments
 (0)