output the adjacency matrix

This commit is contained in:
filifa
2025-05-02 01:04:00 -04:00
parent 1f589633b7
commit 2e48c11582
2 changed files with 26 additions and 6 deletions

View File

@@ -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}