Skip to content

Commit 419fefd

Browse files
authored
Merge pull request #96 from rprebet/dimfix
Update I.dim when dimension computed
2 parents b4a7745 + b7ac329 commit 419fefd

File tree

9 files changed

+62
-47
lines changed

9 files changed

+62
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*~
22
docs/build/*
33
docs/make-dep.jl
4+
Manifest.toml

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Random = "1.6"
2424
StaticArrays = "1"
2525
Test = "1.6"
2626
julia = "1.6"
27-
msolve_jll = "0.700.200"
27+
msolve_jll = "0.700.501"

src/algorithms/dimension.jl

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,44 @@ julia> dimension(I)
1818
```
1919
"""
2020
function dimension(I::Ideal{T}) where T <: MPolyRingElem
21-
22-
gb = get(I.gb, 0, groebner_basis(I, complete_reduction = true))
21+
22+
!isnothing(I.dim) && return I.dim
23+
gb = get!(I.gb, 0) do
24+
groebner_basis(I, complete_reduction = true)
25+
end
2326
R = parent(first(gb))
24-
res = [trues(ngens(R))]
2527

28+
res = Set([trues(ngens(R))])
2629
lead_exps = (_drl_lead_exp).(gb)
27-
for lexp in lead_exps
28-
to_del = Int[]
29-
new_miss = BitVector[]
30-
for (i, mis) in enumerate(res)
31-
nz_exps_inds = findall(e -> !iszero(e), lexp)
32-
ind_var_inds = findall(mis)
33-
if issubset(nz_exps_inds, ind_var_inds)
34-
for j in nz_exps_inds
30+
for lexp in lead_exps
31+
nz_exps = (!iszero).(lexp)
32+
nz_exps_ind = findall(nz_exps)
33+
next_res = Set{BitVector}()
34+
for mis in res
35+
if all_lesseq(nz_exps, mis)
36+
@inbounds for j in nz_exps_ind
3537
new_mis = copy(mis)
3638
new_mis[j] = false
37-
push!(new_miss, new_mis)
39+
push!(next_res, new_mis)
3840
end
39-
push!(to_del, i)
41+
else
42+
push!(next_res, mis)
4043
end
4144
end
42-
deleteat!(res, to_del)
43-
append!(res, new_miss)
44-
unique!(res)
45+
res = next_res
4546
end
4647

47-
length(res) == 0 && return -1
48-
max_length = maximum(mis -> length(findall(mis)), res)
49-
return max_length
48+
I.dim = isempty(res) ? -1 : maximum(sum, res)
49+
return I.dim
50+
end
51+
52+
function all_lesseq(a::BitVector, b::BitVector)::Bool
53+
@inbounds for i in eachindex(a)
54+
if a[i] && !b[i]
55+
return false
56+
end
57+
end
58+
return true
5059
end
5160

5261
function _drl_exp_vector(u::Vector{Int})
@@ -55,6 +64,6 @@ end
5564

5665
function _drl_lead_exp(p::MPolyRingElem)
5766
exps = collect(Nemo.exponent_vectors(p))
58-
_, i = findmax((u -> _drl_exp_vector(u)).(exps))
67+
_, i = findmax(_drl_exp_vector.(exps))
5968
return exps[i]
60-
end
69+
end

src/algorithms/groebner-bases.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,5 @@ function _core_groebner_basis(
245245
cglobal(:jl_free), gb_len, gb_exp, gb_cf, jl_ld, field_char)
246246

247247
I.gb[eliminate] = basis
248-
249-
return basis
248+
return I.gb[eliminate]
250249
end

src/algorithms/solvers.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ function _core_msolve(
123123
jl_dquot = res_dquot[]
124124
jl_nb_sols = nb_sols[]
125125

126-
# set dimension
127-
I.dim = jl_dim
128126
if jl_dim > 0
129127
error("Dimension of ideal is greater than zero, no solutions provided.")
130128
end
129+
I.dim = 0
131130

132131
# get rational parametrization
133132
jl_len = Base.unsafe_wrap(Array, res_len[], jl_ld)
134133
nterms = 0
135134

136135
if jl_dquot == 0
136+
I.dim = -1
137137
C,x = polynomial_ring(QQ,"x")
138138
I.rat_param = RationalParametrization(Symbol[], ZZRingElem[], C(-1), C(-1), PolyRingElem[])
139139
I.real_sols = QQFieldElem[]
@@ -392,17 +392,18 @@ QQMPolyRingElem[x1 + 2*x2 + 2*x3 - 1, x1^2 - x1 + 2*x2^2 + 2*x3^2, 2*x1*x2 + 2*x
392392
393393
julia> real_solutions(I)
394394
4-element Vector{Vector{QQFieldElem}}:
395-
[5416829397//8589934592, 2708414699//8589934592, -2844258330290649520990905062759917788583//21778071482940061661655974875633165533184]
395+
[21667317587//34359738368, 21667317587//68719476736, -2844258330290649520990905062759917788583//21778071482940061661655974875633165533184]
396396
[1, 0, 0]
397-
[1945971683//8589934592, 972985841//8589934592, 744426424910260862653434112767010536665//2722258935367507707706996859454145691648]
398-
[2863311531//8589934592, 0, 3629678580490010276942662479272194255531//10889035741470030830827987437816582766592]
397+
[15567773459//68719476736, 15567773459//137438953472, 744426424910260862653434112767010536665//2722258935367507707706996859454145691648]
398+
[11453246123//34359738368, 460175067//5846006549323611672814739330865132078623730171904, 3629678580490010276942662479272194255531//10889035741470030830827987437816582766592]
399399
400400
julia> real_solutions(I, interval=true)
401401
4-element Vector{Vector{Vector{QQFieldElem}}}:
402-
[[1354207349//2147483648, 2708414699//4294967296], [1354207349//4294967296, 677103675//2147483648], [-355532291286331190123863132844989723573//2722258935367507707706996859454145691648, -1422129165145324760495452531379958894291//10889035741470030830827987437816582766592]]
402+
[[10833658793//17179869184, 5416829397//8589934592], [10833658793//34359738368, 5416829397//17179869184], [-355532291286331190123863132844989723573//2722258935367507707706996859454145691648, -1422129165145324760495452531379958894291//10889035741470030830827987437816582766592]]
403403
[[1, 1], [0, 0], [0, 0]]
404-
[[972985841//4294967296, 486492921//2147483648], [60811615//536870912, 486492921//4294967296], [93053303113782607831679264095876317083//340282366920938463463374607431768211456, 372213212455130431326717056383505268333//1361129467683753853853498429727072845824]]
405-
[[1431655765//4294967296, 715827883//2147483648], [-1//4294967296, 1//4294967296], [1814839290245005138471331239636097127765//5444517870735015415413993718908291383296, 907419645122502569235665619818048563883//2722258935367507707706996859454145691648]]
404+
[[7783886729//34359738368, 3891943365//17179869184], [7783886729//68719476736, 3891943365//34359738368], [93053303113782607831679264095876317083//340282366920938463463374607431768211456, 372213212455130431326717056383505268333//1361129467683753853853498429727072845824]]
405+
[[5726623061//17179869184, 2863311531//8589934592], [-4371663141//1461501637330902918203684832716283019655932542976, 9203501349//2923003274661805836407369665432566039311865085952], [1814839290245005138471331239636097127765//5444517870735015415413993718908291383296, 907419645122502569235665619818048563883//2722258935367507707706996859454145691648]]
406+
406407
```
407408
"""
408409
function real_solutions(

src/types.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727

2828
mutable struct Ideal{T <: MPolyRingElem}
2929
gens::Vector{T}
30-
dim::Int
30+
dim::Union{Int, Nothing}
3131
gb::Dict{Int, Vector{T}}
3232
inter_sols::Vector{Vector{Vector{QQFieldElem}}}
3333
real_sols::Vector{Vector{QQFieldElem}}
@@ -37,12 +37,11 @@ mutable struct Ideal{T <: MPolyRingElem}
3737
function Ideal(F::Vector{T}) where {T <: MPolyRingElem}
3838
I = new{T}()
3939
I.gens = F
40-
I.dim = -1
4140
I.gb = Dict()
42-
41+
I.dim = nothing
4342
return I
4443
end
45-
end
44+
end
4645

4746
Base.parent(I::Ideal) = Nemo.parent(I.gens[1])
4847

test/algorithms/dimension.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
R, (x,y) = polynomial_ring(QQ,["x","y"])
33
I = Ideal([x^2,x*y])
44
@test isone(dimension(I))
5+
@test isone(I.dim)
56

67
R, (x,y,z) = polynomial_ring(GF(101),["x","y","z"])
78
I = Ideal([x+2*y+2*z-1, x^2+2*y^2+2*z^2-x, 2*x*y+2*y*z-y])
89
@test iszero(dimension(I))
10+
@test iszero(I.dim)
911

1012
I = Ideal([R(1)])
1113
@test dimension(I) == -1
14+
@test I.dim == -1
1215
end

test/algorithms/groebner-bases.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
z^4 + 38*z^3 + 95*z^2 + 95*z
1717
]
1818
@test G == H
19-
19+
2020
I = Ideal([x+2*y+2*z-1, x^2+2*y^2+2*z^2-x, 2*x*y+2*y*z-y])
2121
G = groebner_basis(I, eliminate=2, intersect=false)
2222
H = MPolyRingElem[

test/algorithms/solvers.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@
55
2*x1*x2 + 2*x2*x3 + 2*x3*x4 - x2,
66
x2^2 + 2*x1*x3 + 2*x2*x4 - x3])
77
sols = Vector{QQFieldElem}[
8-
[4862548775//8589934592, 1281562925//8589934592, 2195068207//8589934592, -32714273694608759819673593948262790419337//174224571863520493293247799005065324265472],
9-
[3779635503//8589934592, 2638476131//8589934592, 908473689//8589934592, -92633843493479102248442236077889080803361//696898287454081973172991196020261297061888],
8+
[9725097551//17179869184, 10252503395//68719476736, 8780272825//34359738368, -32714273694608759819673593948262790419337//174224571863520493293247799005065324265472],
9+
[15118542013//34359738368, 10553904527//34359738368, 14535579013//137438953472, -92633843493479102248442236077889080803361//696898287454081973172991196020261297061888],
1010
[1, 0, 0, 0],
11-
[6410479475//8589934592, 2005530653//8589934592, -1585770177//8589934592, 55658687714722201275489356321691134094555601//713623846352979940529142984724747568191373312],
12-
[1611414365//8589934592, 673053615//8589934592, 632173751//8589934592, 708759148891639684402860468800417934359477//2787593149816327892691964784081045188247552],
13-
[2863311531//8589934592, 0, 0, 14518714321960041107770649917088777022123//43556142965880123323311949751266331066368]
11+
[12820958949//17179869184, 16044245225//68719476736, -12686161419//68719476736, 55658687714722201275489356321691134094555601//713623846352979940529142984724747568191373312],
12+
[12891314913//68719476736, 10768857835//137438953472, 10114780001//137438953472, 708759148891639684402860468800417934359477//2787593149816327892691964784081045188247552],
13+
[11453246123//34359738368, 393751//45671926166590716193865151022383844364247891968, 5717949//182687704666362864775460604089535377456991567872, 14518714321960041107770649917088777022123//43556142965880123323311949751266331066368]
1414
]
1515
inter_sols = Vector{Vector{QQFieldElem}}[
16-
[[2431274387//4294967296, 607818597//1073741824], [320390731//2147483648, 640781463//4294967296], [1097534103//4294967296, 137191763//536870912], [-16357136847304379909836796974131395209669//87112285931760246646623899502532662132736, -4089284211826094977459199243532848802417//21778071482940061661655974875633165533184]],
17-
[[1889817751//4294967296, 236227219//536870912], [1319238065//4294967296, 659619033//2147483648], [113559211//1073741824, 454236845//4294967296], [-46316921746739551124221118038944540401681//348449143727040986586495598010130648530944, -2894807609171221945263819877434033775105//21778071482940061661655974875633165533184]],
16+
[[4862548775//8589934592, 607818597//1073741824], [5126251697//34359738368, 2563125849//17179869184], [1097534103//4294967296, 4390136413//17179869184], [-16357136847304379909836796974131395209669//87112285931760246646623899502532662132736, -4089284211826094977459199243532848802417//21778071482940061661655974875633165533184]],
17+
[[3779635503//8589934592, 7559271007//17179869184], [5276952263//17179869184, 659619033//2147483648], [3633894753//34359738368, 7267789507//68719476736], [-46316921746739551124221118038944540401681//348449143727040986586495598010130648530944, -2894807609171221945263819877434033775105//21778071482940061661655974875633165533184]],
1818
[[1, 1], [0, 0], [0, 0], [0, 0]],
19-
[[3205239737//4294967296, 1602619869//2147483648], [501382663//2147483648, 1002765327//4294967296], [-792885089//4294967296, -24777659//134217728], [3478667982170137579718084770105695880909725//44601490397061246283071436545296723011960832, 27829343857361100637744678160845567047277801//356811923176489970264571492362373784095686656]],
20-
[[402853591//2147483648, 805707183//4294967296], [336526807//4294967296, 42065851//536870912], [316086875//4294967296, 79021719//1073741824], [177189787222909921100715117200104483589869//696898287454081973172991196020261297061888, 354379574445819842201430234400208967179739//1393796574908163946345982392040522594123776]],
21-
[[1431655765//4294967296, 715827883//2147483648], [-1//4294967296, 1//4294967296], [-1//4294967296, 1//4294967296], [7259357160980020553885324958544388511061//21778071482940061661655974875633165533184, 3629678580490010276942662479272194255531//10889035741470030830827987437816582766592]]
19+
[[3205239737//4294967296, 6410479475//8589934592], [2005530653//8589934592, 8022122613//34359738368], [-3171540355//17179869184, -6343080709//34359738368], [3478667982170137579718084770105695880909725//44601490397061246283071436545296723011960832, 27829343857361100637744678160845567047277801//356811923176489970264571492362373784095686656]],
20+
[[402853591//2147483648, 6445657457//34359738368], [5384428917//68719476736, 2692214459//34359738368], [316086875//4294967296, 5057390001//68719476736], [177189787222909921100715117200104483589869//696898287454081973172991196020261297061888, 354379574445819842201430234400208967179739//1393796574908163946345982392040522594123776]],
21+
[[5726623061//17179869184, 2863311531//8589934592], [-3795019955//365375409332725729550921208179070754913983135744, 3801319971//365375409332725729550921208179070754913983135744], [-2148414955//182687704666362864775460604089535377456991567872, 2159850853//182687704666362864775460604089535377456991567872], [7259357160980020553885324958544388511061//21778071482940061661655974875633165533184, 3629678580490010276942662479272194255531//10889035741470030830827987437816582766592]]
2222
]
2323
rat_sols = Vector{QQFieldElem}[[1, 0, 0, 0], [1//3, 0, 0, 1//3]]
2424

2525
@test sols == real_solutions(I)
2626
@test inter_sols == real_solutions(I, interval=true)
2727
@test rat_sols == rational_solutions(I)
2828
@test I.real_sols == real_solutions(I)
29+
@test iszero(I.dim)
2930

3031
C, x = polynomial_ring(QQ, "x")
3132
elim = 128304*x^8 - 93312*x^7 + 15552*x^6 + 3144*x^5 - 1120*x^4 + 36*x^3 + 15*x^2 - x
@@ -55,11 +56,13 @@
5556
I = Ideal([x1^2-x2, x1*x3-x4, x2*x4-12, x4^3-x3^2])
5657
real_solutions(I)
5758
@test I.rat_param.vars == Symbol[]
59+
@test I.dim == -1
5860

5961
I = Ideal([x1^2-x2, x1*x3, x2-12])
6062
@test_throws ErrorException real_solutions(I)
6163
@test_throws ErrorException real_solutions(I, interval=true)
6264
@test_throws ErrorException rational_solutions(I)
65+
@test isnothing(I.dim)
6366

6467
R, (x, y) = polynomial_ring(QQ,["x","y"])
6568
# issue 54

0 commit comments

Comments
 (0)