refactor output code
This commit is contained in:
parent
65d4e9a15d
commit
cfc6b24814
38
cmd/root.go
38
cmd/root.go
|
@ -72,27 +72,12 @@ func parse(cmd *cobra.Command, args []string) {
|
||||||
panic("not an absorbing Markov chain!")
|
panic("not an absorbing Markov chain!")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmtOptions := make([]mat.FormatOption, 0)
|
matrix, err := orderedAdjMatrix(graph)
|
||||||
if matlabFmt {
|
|
||||||
fmtOptions = append(fmtOptions, mat.FormatMATLAB())
|
|
||||||
} else if pythonFmt {
|
|
||||||
fmtOptions = append(fmtOptions, mat.FormatPython())
|
|
||||||
}
|
|
||||||
|
|
||||||
a, err := orderedAdjMatrix(graph)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
out := mat.Formatted(a, fmtOptions...)
|
outputMatrix(matrix)
|
||||||
|
|
||||||
// for matlab and python formats, %#v outputs as matrix and %v is
|
|
||||||
// oneline, but for standard format, it's the opposite, so we xor
|
|
||||||
if (matlabFmt || pythonFmt) != oneline {
|
|
||||||
fmt.Printf("%#v\n", out)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("%v\n", out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func orderedAdjMatrix(g *markov.AbsorbingMarkovChain) (*mat.Dense, error) {
|
func orderedAdjMatrix(g *markov.AbsorbingMarkovChain) (*mat.Dense, error) {
|
||||||
|
@ -125,6 +110,25 @@ func orderedAdjMatrix(g *markov.AbsorbingMarkovChain) (*mat.Dense, error) {
|
||||||
return matrix, nil
|
return matrix, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func outputMatrix(matrix mat.Matrix) {
|
||||||
|
fmtOptions := make([]mat.FormatOption, 0)
|
||||||
|
if matlabFmt {
|
||||||
|
fmtOptions = append(fmtOptions, mat.FormatMATLAB())
|
||||||
|
} else if pythonFmt {
|
||||||
|
fmtOptions = append(fmtOptions, mat.FormatPython())
|
||||||
|
}
|
||||||
|
|
||||||
|
out := mat.Formatted(matrix, fmtOptions...)
|
||||||
|
|
||||||
|
// for matlab and python formats, %#v outputs as matrix and %v is
|
||||||
|
// oneline, but for standard format, it's the opposite, so we xor
|
||||||
|
if (matlabFmt || pythonFmt) != oneline {
|
||||||
|
fmt.Printf("%#v\n", out)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%v\n", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
func Execute() {
|
func Execute() {
|
||||||
|
|
Loading…
Reference in New Issue