refactor output code

This commit is contained in:
filifa 2025-05-03 23:42:02 -04:00
parent 65d4e9a15d
commit cfc6b24814
1 changed files with 21 additions and 17 deletions

View File

@ -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() {