-
Notifications
You must be signed in to change notification settings - Fork 45
/
matrix.go
93 lines (87 loc) · 2.34 KB
/
matrix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Package simulator
// @Author bcy2007 2023/8/17 16:20
package simulator
import (
"github.com/yaklang/yaklang/common/utils"
"golang.org/x/exp/slices"
)
type DataMatrix[T any] struct {
//ItemList rod.Elements
ItemList []T
TagList []string
Data [][]float64
}
func (matrix *DataMatrix[T]) ValidCheck() error {
if len(matrix.Data) != len(matrix.ItemList) {
return utils.Errorf(`data items number: %v, item list length: %v`, len(matrix.Data), len(matrix.ItemList))
}
tagLength := len(matrix.TagList)
for _, d := range matrix.Data {
if len(d) != tagLength {
return utils.Errorf(`data tags number: %v, tag list length: %v`, len(d), tagLength)
}
}
return nil
}
func (matrix *DataMatrix[T]) GetResult() (map[string]T, error) {
result := make(map[string]T)
var tempData = slices.Clone(matrix.Data)
var tempItem = slices.Clone(matrix.ItemList)
var tempTag = slices.Clone(matrix.TagList)
var num int
for len(tempData) != 0 {
num++
var maxRow, maxColumn int
var maxData float64 = -100
for row, items := range tempData {
column, tempMax := getMax(items)
if tempMax > maxData {
maxRow = row
maxColumn = column
maxData = tempMax
}
}
afterRemove := make([][]float64, 0)
for row, rows := range tempData {
if row == maxRow {
continue
}
if len(rows) <= maxColumn {
return result, utils.Errorf("matrix size error on tempData")
}
temp := append(rows[:maxColumn], rows[maxColumn+1:]...)
if len(temp) != 0 {
afterRemove = append(afterRemove, temp)
}
}
if len(tempTag) <= maxColumn {
return result, utils.Errorf("matrix size error on tempTag")
}
if len(tempItem) <= maxRow {
return result, utils.Errorf("matrix size error on tempItem")
}
if maxData <= 0 {
var null T
result[tempTag[maxColumn]] = null
} else {
result[tempTag[maxColumn]] = tempItem[maxRow]
}
tempItem = append(tempItem[:maxRow], tempItem[maxRow+1:]...)
tempTag = append(tempTag[:maxColumn], tempTag[maxColumn+1:]...)
tempData = afterRemove
//info := fmt.Sprintf("round %d result: %v %v %v %v", num, tempTag, tempItem, tempData, result)
//log.Debug(info)
}
return result, nil
}
func getMax(data []float64) (int, float64) {
var maxData float64 = -100
var maxPosition int
for pos, item := range data {
if item > maxData {
maxData = item
maxPosition = pos
}
}
return maxPosition, maxData
}