-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (115 loc) · 2.54 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bytes"
"context"
"fmt"
"math"
"math/rand"
minimus "github.com/cowdude/minimus-encoder"
)
func printFloats(s []float64) {
for _, f := range s {
fmt.Printf(" %7.4f", f)
}
}
func getData() (res [][]float64) {
res = make([][]float64, 1000, 1001)
for i := range res {
const pre = 100
n := rand.Float64()
n = math.Round(n*pre*pre) / pre
res[i] = []float64{
n,
1 / (1 + n),
float64(1 + (i/5)%42),
float64(1 + i/13),
}
}
res = append(res, []float64{1.11111, 2.22222, 3.33333, 4.44444})
return
}
func printInputsOutputs(c chan minimus.Vec64, pool *minimus.VecPool, inputs [][]float64) {
var i int
const logLines = 2
for vec := range c {
if i < (logLines+1)/2 || i >= len(inputs)-logLines/2 {
fmt.Printf(" %4d:", i)
printFloats(inputs[i])
fmt.Print(" =>")
printFloats(vec.Float64())
fmt.Println()
}
//recycle `vec` after using it
pool.Put(vec)
i++
}
}
func lossyEncodeDecode(buf *bytes.Buffer, span int, maxError float64, series [][]float64) {
var (
enc = minimus.NewEncoder(buf, span)
tmp = make([]float64, span)
)
for _, vec := range series {
for i, val := range vec {
tmp[i] = minimus.LossyFloat64(val, maxError)
}
enc.PutFloat64(tmp)
}
enc.Close()
numBytes := buf.Len()
var (
pool = minimus.NewVecPool(span)
c = make(chan minimus.Vec64, 1)
dec = minimus.NewDecoder(buf, span)
ctx = context.TODO()
)
go func() {
defer close(c)
if err := dec.EnumBorrow(ctx, c, pool); err != nil {
panic(err)
}
}()
fmt.Println()
printInputsOutputs(c, pool, series)
bps := float64(numBytes*8) / float64(len(series)*span)
fmt.Printf("|e|=%-5.e: %6.3f b/sample\n", maxError, bps)
}
func encodeDecode(buf *bytes.Buffer, span int, series [][]float64) {
var (
enc = minimus.NewEncoder(buf, span)
)
for _, vec := range series {
enc.PutFloat64(vec)
}
enc.Close()
numBytes := buf.Len()
var (
pool = minimus.NewVecPool(span)
c = make(chan minimus.Vec64, 1)
dec = minimus.NewDecoder(buf, span)
ctx = context.TODO()
)
go func() {
defer close(c)
if err := dec.EnumBorrow(ctx, c, pool); err != nil {
panic(err)
}
}()
fmt.Println()
printInputsOutputs(c, pool, series)
bps := float64(numBytes*8) / float64(len(series)*span)
fmt.Printf("(lossless) %.3f b/sample\n", bps)
}
func main() {
var (
buf bytes.Buffer
series = getData()
span = len(series[0])
)
for maxError := 1e-1; maxError > 1e-16; maxError *= 1e-3 {
buf.Reset()
lossyEncodeDecode(&buf, span, maxError, series)
}
buf.Reset()
encodeDecode(&buf, span, series)
}