Skip to content

Commit

Permalink
added adjacency_matrix for SCplx & minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed Mar 27, 2019
1 parent 2eeb5d3 commit b1d069f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/ComputationalHomology.jl
@@ -1,7 +1,7 @@
module ComputationalHomology

using LinearAlgebra
using SparseArrays
import SparseArrays: SparseMatrixCSC, spzeros, sparse, findnz
import SmithNormalForm: smith
import Distances
import Random
Expand All @@ -14,13 +14,14 @@ import Base.Iterators: pairs
export AbstractCell,
dim, faces, volume,
AbstractSimplex,
values, vertecies,
Simplex, Cube, Cell,

AbstractChain,
Chain, simplify,

AbstractComplex,
boundary, coboundary, celltype, cells,
boundary, coboundary, celltype, cells, vertecies,
SimplicialComplex, addsimplex!, addsimplices!,
vietorisrips, witness, cech, čech,

Expand Down
2 changes: 1 addition & 1 deletion src/cells.jl
Expand Up @@ -36,7 +36,7 @@ abstract type AbstractSimplex <: AbstractCell end
Get an array of `cell` values.
"""
values(c::AbstractSimplex) = throw(MethodError(values,(typeof(c),))) #TODO: consider to move to AbstractSimplex
values(c::AbstractSimplex) = throw(MethodError(values,(typeof(c),)))

"""
vertecies(cell)
Expand Down
2 changes: 1 addition & 1 deletion src/filtration.jl
Expand Up @@ -126,7 +126,7 @@ function boundary(flt::Filtration; reduced=false)
return bm
end

function SparseArrays.sparse(∂::Vector{<:AbstractSet})
function sparse(∂::Vector{<:AbstractSet})
m = length(∂)
ret = spzeros(Int, m, m)
for i in 1:m
Expand Down
19 changes: 18 additions & 1 deletion src/simplicial.jl
Expand Up @@ -48,7 +48,7 @@ union(u::Simplex, v::Simplex) = Simplex(collect(u.vs ∪ v.vs))

function volume(S::AbstractMatrix)
d, vc = size(S)
@assert d == vc-1 "Number of vertexes in simplex should be dim+1"
@assert d == vc-1 "Number of vertecies in simplex should be dim+1"
v0 = S[:,1]
return abs(det(S[:,2:end] .- v0))/prod(1:d)
end
Expand Down Expand Up @@ -258,6 +258,23 @@ function Base.write(io::IO, cplx::SimplicialComplex)
return
end

"""
adjacency_matrix(cplx, [T=Int])
Construct an adjacency matrix of type `T` from a 1-skeleton (1D subcomplex) of the complex `cplx`.
"""
function adjacency_matrix(cplx::SimplicialComplex, ::Type{T}) where {T<:Integer}
C0 = map(hash, cells(cplx, 0))
N = length(C0)
adj = spzeros(T,N,N)
for c in cells(cplx, 1)
idxs = map(h->findfirst(isequal(h), C0), vertecies(c))
adj[idxs, idxs] .= one(T)
end
return adj
end
adjacency_matrix(cplx::SimplicialComplex) = adjacency_matrix(cplx, Int)

#
# Complex simplex iterator
#
Expand Down
4 changes: 4 additions & 0 deletions test/simplicial.jl
Expand Up @@ -106,4 +106,8 @@

ch = boundary(cplx2, 1, 0, Int)
@test dim(ch) == -1

A = ComputationalHomology.adjacency_matrix(ComputationalHomology.sphere(1), UInt8)
@test collect(A) == fill(0x01,2,2)
@test eltype(A) == UInt8
end

0 comments on commit b1d069f

Please sign in to comment.