Skip to content

Commit 4d5b63d

Browse files
authored
Merge pull request #34 from matbesancon/prealloc-matrix
Prealloc matrix
2 parents 91b1d00 + 6bc2a8e commit 4d5b63d

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ os:
66
julia:
77
- 1.0
88
- 1.1
9+
- 1.2
910
addons:
1011
apt_packages:
1112
- libgmp-dev

Project.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ julia = "1"
1818
[extras]
1919
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
2020
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
21+
GeometryTypes = "4d00f742-c7ba-57c2-abde-4428a4b178cb"
2122
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
2223
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
23-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2424
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
25-
GeometryTypes = "4d00f742-c7ba-57c2-abde-4428a4b178cb"
25+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
26+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
27+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2628

2729
[targets]
28-
test = ["Combinatorics", "GLPK", "LinearAlgebra", "Test", "JuMP", "RecipesBase", "GeometryTypes"]
30+
test = ["Combinatorics", "GLPK", "LinearAlgebra", "Test", "JuMP", "RecipesBase", "GeometryTypes", "SparseArrays", "StaticArrays"]

src/conversion.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export enumtomat, generatorproducer
2+
23
function generatorproducer(m::RepMatrix)
3-
Channel() do c
4+
Channel(; ctype = Vector{Rational{BigInt}}) do c
45
# code from here is borrowed from lrs_main
56
if !(m.status in [:AtNoBasis, :AtFirstBasis, :Empty])
67
error("I am not at first basis")
@@ -13,7 +14,11 @@ function generatorproducer(m::RepMatrix)
1314
if m.lin !== nothing # FIXME should I do that if m.status is :Empty ?
1415
L = getmat(m.lin)
1516
for i in 1:size(L, 1)
16-
put!(c, L[i, :])
17+
@static if VERSION >= v"1.1"
18+
put!(c, L[i, :])
19+
else
20+
put!(c, convert(Vector{Rational{BigInt}}, L[i, :]))
21+
end
1722
end
1823
end
1924

@@ -27,7 +32,11 @@ function generatorproducer(m::RepMatrix)
2732
for col in 0:getd(m)
2833
output = getsolution(m, col)
2934
if output !== nothing
30-
put!(c, output)
35+
@static if VERSION >= v"1.1"
36+
put!(c, output)
37+
else
38+
put!(c, convert(Vector{Rational{BigInt}}, output))
39+
end
3140
end
3241
end
3342
if !getnextbasis(m)
@@ -37,12 +46,18 @@ function generatorproducer(m::RepMatrix)
3746
end
3847
end
3948
end
49+
4050
function enumtomat(m::RepMatrix)
41-
M = Matrix{Rational{BigInt}}(undef, 0, fulldim(m)+1)
42-
for output in generatorproducer(m)
43-
M = [M; output']
51+
rows = Vector{Vector{Rational{BigInt}}}(undef, 0)
52+
sizehint!(rows, fulldim(m)) # heuristic size hint
53+
for row in generatorproducer(m)
54+
push!(rows, row)
55+
end
56+
M = Matrix{Rational{BigInt}}(undef, length(rows), fulldim(m)+1)
57+
for i in eachindex(rows)
58+
M[i,:] .= rows[i]
4459
end
45-
M
60+
return M
4661
end
4762

4863
function Base.convert(::Type{LiftedHRepresentation{Rational{BigInt}}}, m::VMatrix)

src/matrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747

4848
function fillmatrix(inequality::Bool, P::Ptr{Clrs_dic}, Q::Ptr{Clrs_dat}, itr::Polyhedra.ElemIt, offset::Int)
4949
for (i, item) in enumerate(itr)
50-
a = vec(coord(lift(item)))
50+
a = convert(Vector{Rational{BigInt}}, vec(coord(lift(item))))
5151
setrow(P, Q, offset+i, inequality ? -a : a, !islin(item))
5252
end
5353
end

0 commit comments

Comments
 (0)