output the adjacency matrix
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"gonum.org/v1/gonum/graph"
|
||||
"gonum.org/v1/gonum/graph/encoding"
|
||||
"gonum.org/v1/gonum/graph/simple"
|
||||
"gonum.org/v1/gonum/mat"
|
||||
)
|
||||
|
||||
type AbsorbingMarkovChain struct {
|
||||
@@ -37,12 +38,7 @@ func NewAbsorbingMarkovChain() *AbsorbingMarkovChain {
|
||||
func (g *AbsorbingMarkovChain) IsValid() bool {
|
||||
for nodes := g.Nodes(); nodes.Next(); {
|
||||
u := nodes.Node().(*node)
|
||||
|
||||
if g.From(u.ID()).Len() == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if g.outWeightSum(u) != 1 {
|
||||
if g.outWeightSum(u) > 1 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -63,6 +59,24 @@ func (g *AbsorbingMarkovChain) outWeightSum(u *node) float64 {
|
||||
return sum
|
||||
}
|
||||
|
||||
func (g *AbsorbingMarkovChain) AdjacencyMatrix() mat.Matrix {
|
||||
adj := simple.NewDirectedMatrix(g.Nodes().Len(), 0, math.NaN(), 0)
|
||||
for edges := g.WeightedEdges(); edges.Next(); {
|
||||
adj.SetWeightedEdge(edges.WeightedEdge())
|
||||
}
|
||||
|
||||
a := mat.DenseCopyOf(adj.Matrix())
|
||||
|
||||
nodes := adj.Nodes()
|
||||
for i := 0; nodes.Next(); i++ {
|
||||
id := nodes.Node().ID()
|
||||
u := g.Node(id).(*node)
|
||||
a.Set(i, i, 1 - g.outWeightSum(u))
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func (g *AbsorbingMarkovChain) NewEdge(from, to graph.Node) graph.Edge {
|
||||
e := g.WeightedDirectedGraph.NewWeightedEdge(from, to, math.NaN()).(simple.WeightedEdge)
|
||||
return &weightedEdge{WeightedEdge: e}
|
||||
|
||||
Reference in New Issue
Block a user