diff --git a/report/table.go b/report/table.go new file mode 100644 index 0000000..d673796 --- /dev/null +++ b/report/table.go @@ -0,0 +1,65 @@ +// Copyright 2022 wanderer +// SPDX-License-Identifier: GPL-3.0-or-later + +package report + +import ( + "log" + "os" + "text/template" + "time" + + "git.dotya.ml/wanderer/math-optim/util" +) + +type Table struct { + Algo string + Header []string + ColLayout []string + Rows []Row +} + +type Row struct { + // Title of the row should contain the settings used to get the Values. + Title string + Values []float64 +} + +func NewTable() *Table { + return &Table{} +} + +func NewRow() *Row { + return &Row{} +} + +func SaveTableToFile(t Table) { + safeName := util.SanitiseFName(t.Algo) + texTableFile := GetTexDir() + "table-" + safeName + ".tex" + tmplTableFile := "report/table.tmpl" + tmplTable := template.Must(template.ParseFiles(tmplTableFile)) + + f, err := os.Create(texTableFile) + if err != nil { + log.Println(err) + } + defer f.Close() + + err = tmplTable.Execute(f, struct { + Algo string + ColLayout []string + ColNames []string + Rs []Row + Timestamp time.Time + }{ + Algo: t.Algo, + ColNames: t.Header, + ColLayout: t.ColLayout, + Rs: t.Rows, + Timestamp: time.Now(), + }) + + if err != nil { + log.Println(err) + } +} diff --git a/report/table.tmpl b/report/table.tmpl new file mode 100644 index 0000000..a9c12ec --- /dev/null +++ b/report/table.tmpl @@ -0,0 +1,31 @@ +% Code generated by math-optim; DO NOT EDIT. +% +% This file was generated by robots at +% {{ .Timestamp }} +% source: git.dotya.ml/wanderer/math-optim/report/table.tmpl + +% This file is a part of the math-optim project. +% project homepage: https://git.dotya.ml/wanderer/math-optim/ + +\subsection{ {{- .Algo -}} } + +\begin{table}[!htb] + \centering + + % \begin{tabular}[t]{ |l|{{- range $i, $v := .ColLayout }}{{$v}}| {{- end}} } + \begin{tabular}[t]{ ||l||{{- range $i, $v := .ColLayout }}{{$v}}| {{- end -}}| } + \toprule + & {{ range $i, $v := .ColNames }}{{$v }} & {{ end}}\\ + {{- range $j, $v := .Rs }} + {{ printf "%s & " $v.Title }} + {{- range $k, $val := $v.Values }} + {{- printf "%f & " $val }} + {{- end}}\\ + {{- end }} + \bottomrule + \end{tabular} + + \caption{\label{table: {{ .Algo }} algo statistics}} +\end{table} + +% vim: ft=gotexttmpl ts=2 sts=2 sw=2 bs=2 expandtab