@@ -4,26 +4,28 @@ export FactorGraph, jsondata
44
55alphabets = [:spin , :boolean , :integer , :integer_pos , :real , :real_pos ]
66
7- type FactorGraph{T <: Real }
7+
8+ mutable struct FactorGraph{T <: Real }
89 order:: Int
910 varible_count:: Int
1011 alphabet:: Symbol
1112 terms:: Dict{Tuple,T} # TODO , would be nice to have a stronger tuple type here
12- variable_names:: Nullable {Vector{String}}
13- FactorGraph (a,b,c,d,e) = check_model_data (a,b,c,d,e) ? new (a,b,c,d,e) : error (" generic init problem" )
13+ variable_names:: Union {Vector{String}, Nothing }
14+ # FactorGraph(a,b,c,d,e) = check_model_data(a,b,c,d,e) ? new(a,b,c,d,e) : error("generic init problem")
1415end
15- FactorGraph {T <: Real} (order:: Int , varible_count:: Int , alphabet:: Symbol , terms:: Dict{Tuple,T} ) = FactorGraph {T} (order, varible_count, alphabet, terms, Nullable {Vector{String}} ())
16- FactorGraph {T <: Real} (matrix:: Array{T,2} ) = convert (FactorGraph{T}, matrix)
17- FactorGraph {T <: Real} (dict:: Dict{Tuple,T} ) = convert (FactorGraph{T}, dict)
16+
17+ FactorGraph (order:: Int , varible_count:: Int , alphabet:: Symbol , terms:: Dict{Tuple,T} ) where T <: Real = FactorGraph {T} (order, varible_count, alphabet, terms, nothing )
18+ FactorGraph (matrix:: Array{T,2} ) where T <: Real = convert (FactorGraph{T}, matrix)
19+ FactorGraph (dict:: Dict{Tuple,T} ) where T <: Real = convert (FactorGraph{T}, dict)
1820FactorGraph (list:: Array{Any,1} ) = convert (FactorGraph, list)
1921
2022
21- function check_model_data {T <: Real} (order:: Int , varible_count:: Int , alphabet:: Symbol , terms:: Dict{Tuple,T} , variable_names:: Nullable {Vector{String}} )
23+ function check_model_data (order:: Int , varible_count:: Int , alphabet:: Symbol , terms:: Dict{Tuple,T} , variable_names:: Union {Vector{String}, Nothing} ) where T <: Real
2224 if ! in (alphabet, alphabets)
2325 error (" alphabet $(alphabet) is not supported" )
2426 return false
2527 end
26- if ! isnull ( variable_names) && length (variable_names) != varible_count
28+ if variable_names != nothing && length (variable_names) != varible_count
2729 error (" expected $(varible_count) but only given $(length (variable_names)) " )
2830 return false
2931 end
5456function Base. show (io:: IO , gm:: FactorGraph )
5557 println (io, " alphabet: " , gm. alphabet)
5658 println (io, " vars: " , gm. varible_count)
57- if ! isnull ( gm. variable_names)
59+ if gm. variable_names != nothing
5860 println (io, " variable names: " )
5961 println (io, " " , get (gm. variable_names))
6062 end
@@ -65,19 +67,26 @@ function Base.show(io::IO, gm::FactorGraph)
6567 end
6668end
6769
68- function jsondata {T <: Real} (gm:: FactorGraph{T} )
70+ function jsondata (gm:: FactorGraph{T} ) where T <: Real
6971 data = []
7072 for k in sort (collect (keys (gm. terms)), by= (x)-> (length (x),x))
7173 push! (data, Dict (" term" => k, " weight" => gm. terms[k]))
7274 end
7375 return data
7476end
7577
76- Base. start (gm:: FactorGraph ) = start (gm. terms)
77- Base. next (gm:: FactorGraph , state) = next (gm. terms, state)
78- Base. done (gm:: FactorGraph , state) = done (gm. terms, state)
78+
79+ if VERSION < v " 0.7.0-"
80+ Base. start (gm:: FactorGraph ) = start (gm. terms)
81+ Base. next (gm:: FactorGraph , state) = next (gm. terms, state)
82+ Base. done (gm:: FactorGraph , state) = done (gm. terms, state)
83+ else
84+ Base. iterate (gm:: FactorGraph , kwargs... ) = Base. iterate (gm. terms, kwargs... )
85+ end
86+
7987
8088Base. length (gm:: FactorGraph ) = length (gm. terms)
89+ # Base.size(gm::FactorGraph, a...) = size(gm.terms, a...)
8190
8291Base. getindex (gm:: FactorGraph , i) = gm. terms[i]
8392Base. keys (gm:: FactorGraph ) = keys (gm. terms)
95104
96105diag_key (gm:: FactorGraph , i:: Int ) = tuple (fill (i, gm. order)... )
97106
98- # Base.diag{T <: Real} (gm::FactorGraph{T}) = [ get(gm.terms, diag_key(gm, i), zero(T)) for i in 1:gm.varible_count ]
107+ # Base.diag(gm::FactorGraph{T}) where T <: Real = [ get(gm.terms, diag_key(gm, i), zero(T)) for i in 1:gm.varible_count ]
99108
100- Base. DataFmt. writecsv {T <: Real} (io, gm:: FactorGraph{T} , args... ; kwargs... ) = writecsv (io, convert (Array{T,2 }, gm), args... ; kwargs... )
109+ # Base.DataFmt.writecsv(io, gm::FactorGraph{T}, args...; kwargs...) where T <: Real = writecsv(io, convert(Array{T,2}, gm), args...; kwargs...)
101110
102- Base. convert {T <: Real} (:: Type{FactorGraph} , m:: Array{T,2} ) = convert (FactorGraph{T}, m)
103- function Base. convert {T <: Real} (:: Type{FactorGraph{T}} , m:: Array{T,2} )
111+ Base. convert (:: Type{FactorGraph} , m:: Array{T,2} ) where T <: Real = convert (FactorGraph{T}, m)
112+ function Base. convert (:: Type{FactorGraph{T}} , m:: Array{T,2} ) where T <: Real
104113 @assert size (m,1 ) == size (m,2 ) # check matrix is square
105114
106- info ( " assuming spin alphabet" )
115+ @ info " assuming spin alphabet"
107116 alphabet = :spin
108117 varible_count = size (m,1 )
109118
@@ -132,7 +141,7 @@ function Base.convert{T <: Real}(::Type{FactorGraph{T}}, m::Array{T,2})
132141 return FactorGraph (2 , varible_count, alphabet, terms)
133142end
134143
135- function Base. convert {T <: Real} (:: Type{Array{T,2}} , gm:: FactorGraph{T} )
144+ function Base. convert (:: Type{Array{T,2}} , gm:: FactorGraph{T} ) where T <: Real
136145 if gm. order != 2
137146 error (" cannot convert a FactorGraph of order $(gm. order) to a matrix" )
138147 end
@@ -152,8 +161,8 @@ function Base.convert{T <: Real}(::Type{Array{T,2}}, gm::FactorGraph{T})
152161end
153162
154163
155- Base. convert {T <: Real} (:: Type{Dict} , m:: Array{T,2} ) = convert (Dict{Tuple,T}, m)
156- function Base. convert {T <: Real} (:: Type{Dict{Tuple,T}} , m:: Array{T,2} )
164+ Base. convert (:: Type{Dict} , m:: Array{T,2} ) where T <: Real = convert (Dict{Tuple,T}, m)
165+ function Base. convert (:: Type{Dict{Tuple,T}} , m:: Array{T,2} ) where T <: Real
157166 @assert size (m,1 ) == size (m,2 ) # check matrix is square
158167
159168 varible_count = size (m,1 )
181190
182191
183192function Base. convert (:: Type{FactorGraph} , list:: Array{Any,1} )
184- info ( " assuming spin alphabet" )
193+ @ info " assuming spin alphabet"
185194 alphabet = :spin
186195
187196 max_variable = 0
@@ -198,15 +207,15 @@ function Base.convert(::Type{FactorGraph}, list::Array{Any,1})
198207 max_variable = max (max_variable, maximum (term))
199208 end
200209
201- info ( " dectected $(max_variable) variables with order $(max_order) " )
210+ @ info " dectected $(max_variable) variables with order $(max_order) "
202211
203212 return FactorGraph (max_order, max_variable, alphabet, terms)
204213end
205214
206215
207- Base. convert {T <: Real} (:: Type{FactorGraph} , dict:: Dict{Tuple,T} ) = convert (FactorGraph{T}, dict)
208- function Base. convert {T <: Real} (:: Type{FactorGraph{T}} , dict:: Dict{Tuple,T} )
209- info ( " assuming spin alphabet" )
216+ Base. convert (:: Type{FactorGraph} , dict:: Dict{Tuple,T} ) where T <: Real = convert (FactorGraph{T}, dict)
217+ function Base. convert (:: Type{FactorGraph{T}} , dict:: Dict{Tuple,T} ) where T <: Real
218+ @ info " assuming spin alphabet"
210219 alphabet = :spin
211220
212221 max_variable = 0
@@ -217,7 +226,7 @@ function Base.convert{T <: Real}(::Type{FactorGraph{T}}, dict::Dict{Tuple,T})
217226 max_variable = max (max_variable, maximum (term))
218227 end
219228
220- info ( " dectected $(max_variable) variables with order $(max_order) " )
229+ @ info " dectected $(max_variable) variables with order $(max_order) "
221230
222231 return FactorGraph (max_order, max_variable, alphabet, dict)
223232end
0 commit comments