Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions src/treetensornetworks/opsum_to_ttn/matelem.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@

#
# MatElem
#

struct MatElem{T}
row::Int
col::Int
val::T
end

#function Base.show(io::IO,m::MatElem)
# print(io,"($(m.row),$(m.col),$(m.val))")
#end
Base.eltype(::MatElem{T}) where {T} = T

function toMatrix(els::Vector{MatElem{T}})::Matrix{T} where {T}
nr = 0
nc = 0
for el in els
nr = max(nr, el.row)
nc = max(nc, el.col)
function col_major_order(el1::MatElem, el2::MatElem)
if el1.col == el2.col
return el1.row < el2.row
end
M = zeros(T, nr, nc)
return el1.col < el2.col
end

function to_matrix(els::Vector{<:MatElem})
els = sort(els; lt=col_major_order)
nr = maximum(el -> el.row, els)
nc = last(els).col
M = zeros(eltype(first(els)), nr, nc)
for el in els
M[el.row, el.col] = el.val
end
return M
end

function Base.:(==)(m1::MatElem{T}, m2::MatElem{T})::Bool where {T}
return (m1.row == m2.row && m1.col == m2.col && m1.val == m2.val)
end

function Base.isless(m1::MatElem{T}, m2::MatElem{T})::Bool where {T}
if m1.row != m2.row
return m1.row < m2.row
elseif m1.col != m2.col
return m1.col < m2.col
end
return m1.val < m2.val
end
Loading