output the adjacency matrix
This commit is contained in:
parent
1f589633b7
commit
2e48c11582
|
@ -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}
|
||||
|
|
|
@ -17,12 +17,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"scm.dairydemon.net/filifa/dptdist/cmd/internal/markov"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"gonum.org/v1/gonum/graph/encoding/dot"
|
||||
"gonum.org/v1/gonum/mat"
|
||||
)
|
||||
|
||||
var file string
|
||||
|
@ -57,6 +59,10 @@ func parse(cmd *cobra.Command, args []string) {
|
|||
if !graph.IsValid() {
|
||||
panic("not an absorbing Markov chain!")
|
||||
}
|
||||
|
||||
a := graph.AdjacencyMatrix()
|
||||
out := mat.Formatted(a)
|
||||
fmt.Printf("%v\n", out)
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
|
|
Loading…
Reference in New Issue