diff --git a/cmd/root.go b/cmd/root.go index b482de9..a22b84b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -72,27 +72,12 @@ func parse(cmd *cobra.Command, args []string) { panic("not an absorbing Markov chain!") } - fmtOptions := make([]mat.FormatOption, 0) - if matlabFmt { - fmtOptions = append(fmtOptions, mat.FormatMATLAB()) - } else if pythonFmt { - fmtOptions = append(fmtOptions, mat.FormatPython()) - } - - a, err := orderedAdjMatrix(graph) + matrix, err := orderedAdjMatrix(graph) if err != nil { panic(err) } - out := mat.Formatted(a, 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) - } + outputMatrix(matrix) } func orderedAdjMatrix(g *markov.AbsorbingMarkovChain) (*mat.Dense, error) { @@ -125,6 +110,25 @@ func orderedAdjMatrix(g *markov.AbsorbingMarkovChain) (*mat.Dense, error) { 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. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() {