-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpolyhedra.jl
125 lines (109 loc) · 4.48 KB
/
polyhedra.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
mutable struct Cdd_PolyhedraData{T<:MyType}
representation::Cdd_RepresentationType
# given representation
homogeneous::Cdd_boolean
d::Cdd_colrange
m::Cdd_rowrange
A::Cdd_Amatrix{T}
# Inequality System: m times d matrix
numbtype::Cdd_NumberType
child::Ptr{Void} # dd_ConePtr
# pointing to the homogenized cone data
m_alloc::Cdd_rowrange
# allocated row size of matrix A
d_alloc::Cdd_colrange
# allocated col size of matrix A
c::Cdd_Arow{T}
# cost vector
EqualityIndex::Cdd_rowflag
# ith component is 1 if it is equality, -1 if it is strict inequality, 0 otherwise.
IsEmpty::Cdd_boolean
# This is to tell whether the set is empty or not
NondegAssumed::Cdd_boolean
InitBasisAtBottom::Cdd_boolean
RestrictedEnumeration::Cdd_boolean
RelaxedEnumeration::Cdd_boolean
m1::Cdd_rowrange
# = m or m+1 (when representation=Inequality && !homogeneous)
# This data is written after dd_ConeDataLoad is called. This
# determines the size of Ainc.
AincGenerated::Cdd_boolean
# Indicates whether Ainc, Ared, Adom are all computed.
# All the variables below are valid only when this is TRUE
ldim::Cdd_colrange
# linearity dimension
n::Cdd_bigrange
# the size of output = total number of rays
# in the computed cone + linearity dimension
Ainc::Cdd_Aincidence
# incidence of input and output
Ared::Cdd_rowset
# redundant set of rows whose removal results in a minimal system
Adom::Cdd_rowset
# dominant set of rows (those containing all rays).
end
function dd_matrix2poly(matrix::Ptr{Cdd_MatrixData{Cdouble}})
err = Ref{Cdd_ErrorType}(0)
poly = @ddf_ccall DDMatrix2Poly Ptr{Cdd_PolyhedraData{Cdouble}} (Ptr{Cdd_MatrixData{Cdouble}}, Ref{Cdd_ErrorType}) matrix err
myerror(err[])
poly
end
function dd_matrix2poly(matrix::Ptr{Cdd_MatrixData{GMPRational}})
err = Ref{Cdd_ErrorType}(0)
poly = @dd_ccall DDMatrix2Poly Ptr{Cdd_PolyhedraData{GMPRational}} (Ptr{Cdd_MatrixData{GMPRational}}, Ref{Cdd_ErrorType}) matrix err
myerror(err[])
poly
end
mutable struct CDDPolyhedra{N, T<:PolyType, S}
poly::Ptr{Cdd_PolyhedraData{S}}
inequality::Bool # The input type is inequality
function CDDPolyhedra{N, T, S}(matrix::CDDMatrix{N, T}) where {N, T <: PolyType, S}
polyptr = dd_matrix2poly(matrix.matrix)
poly = new{N, T, S}(polyptr, isaninequalityrepresentation(matrix))
finalizer(poly, myfree)
poly
end
end
function myfree(poly::CDDPolyhedra{N, Cdouble}) where N
@ddf_ccall FreePolyhedra Void (Ptr{Cdd_PolyhedraData{Cdouble}},) poly.poly
end
function myfree(poly::CDDPolyhedra{N, Rational{BigInt}}) where N
@dd_ccall FreePolyhedra Void (Ptr{Cdd_PolyhedraData{GMPRational}},) poly.poly
end
CDDPolyhedra(matrix::CDDMatrix{N, T, S}) where {N, T, S} = CDDPolyhedra{N, T, S}(matrix)
CDDPolyhedra(rep::Representation) = CDDPolyhedra(CDDMatrix(rep))
function Base.convert(::Type{CDDPolyhedra{N, T, S}}, matrix::CDDMatrix{N, T, S}) where {N, T, S}
CDDPolyhedra{N, T, S}(matrix)
end
Base.convert(::Type{CDDPolyhedra{N, T, S}}, repr::Representation{N, T}) where {N, T, S} = CDDPolyhedra(CDDMatrix(repr))
function dd_copyinequalities(poly::Ptr{Cdd_PolyhedraData{Cdouble}})
@ddf_ccall CopyInequalities Ptr{Cdd_MatrixData{Cdouble}} (Ptr{Cdd_PolyhedraData{Cdouble}},) poly
end
function dd_copyinequalities(poly::Ptr{Cdd_PolyhedraData{GMPRational}})
@dd_ccall CopyInequalities Ptr{Cdd_MatrixData{GMPRational}} (Ptr{Cdd_PolyhedraData{GMPRational}},) poly
end
function copyinequalities(poly::CDDPolyhedra)
CDDInequalityMatrix(dd_copyinequalities(poly.poly))
end
function dd_copygenerators(poly::Ptr{Cdd_PolyhedraData{Cdouble}})
@ddf_ccall CopyGenerators Ptr{Cdd_MatrixData{Cdouble}} (Ptr{Cdd_PolyhedraData{Cdouble}},) poly
end
function dd_copygenerators(poly::Ptr{Cdd_PolyhedraData{GMPRational}})
@dd_ccall CopyGenerators Ptr{Cdd_MatrixData{GMPRational}} (Ptr{Cdd_PolyhedraData{GMPRational}},) poly
end
function copygenerators(poly::CDDPolyhedra)
CDDGeneratorMatrix(dd_copygenerators(poly.poly))
end
function switchinputtype!(poly::CDDPolyhedra)
if poly.inequality
ext = copygenerators(poly)
myfree(poly)
poly.poly = dd_matrix2poly(ext.matrix)
else
ine = copyinequalities(poly)
myfree(poly)
poly.poly = dd_matrix2poly(ine.matrix)
end
poly.inequality = ~poly.inequality
end
export CDDPolyhedra, copyinequalities, copygenerators, switchinputtype!