-
Notifications
You must be signed in to change notification settings - Fork 4
/
summary.go
194 lines (170 loc) · 3.77 KB
/
summary.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package numgo
import (
"math"
"sort"
"github.com/Kunde21/numgo/internal"
)
// Sum calculates the sum result array along a given axes.
// Empty call gives the grand sum of all elements.
func (a *Array64) Sum(axis ...int) (r *Array64) {
switch {
case a.valAxis(&axis, "Sum"):
return a
case len(axis) == 0:
tot := float64(0)
for _, v := range a.data {
tot += v
}
return FullArray64(tot, 1)
}
sort.IntSlice(axis).Sort()
n := make([]int, len(a.shape)-len(axis))
axisR:
for i, t := 0, 0; i < len(a.shape); i++ {
for _, w := range axis {
if i == w {
continue axisR
}
}
n[t] = a.shape[i]
t++
}
ln := a.strides[0]
for k := 0; k < len(axis); k++ {
if a.shape[axis[k]] == 1 {
continue
}
v, wd, st := a.shape[axis[k]], a.strides[axis[k]], a.strides[axis[k]+1]
if st == 1 {
asm.Hadd(uint64(wd), a.data)
ln /= v
a.data = a.data[:ln]
continue
}
for w := 0; w < ln; w += wd {
t := a.data[w/wd*st : (w/wd+1)*st]
copy(t, a.data[w:w+st])
for i := 1; i*st+1 < wd; i++ {
asm.Vadd(t, a.data[w+(i)*st:w+(i+1)*st])
}
}
ln /= v
a.data = a.data[:ln]
}
a.shape = n
tmp := 1
for i := len(n); i > 0; i-- {
a.strides[i] = tmp
tmp *= n[i-1]
}
a.strides[0] = tmp
a.data = a.data[:tmp]
a.strides = a.strides[:len(n)+1]
return a
}
// NaNSum calculates the sum result array along a given axes.
// All NaN values will be ignored in the Sum calculation.
// If all element values along the axis are NaN, NaN is in the return element.
//
// Empty call gives the grand sum of all elements.
func (a *Array64) NaNSum(axis ...int) *Array64 {
if a.valAxis(&axis, "NaNSum") {
return a
}
ns := func(d []float64) (r float64) {
flag := false
for _, v := range d {
if !math.IsNaN(v) {
flag = true
r += v
}
}
if flag {
return r
}
return math.NaN()
}
return a.Fold(ns, axis...)
}
// Count gives the number of elements along a set of axis.
// Value in the element is not tested, all elements are counted.
func (a *Array64) Count(axis ...int) *Array64 {
switch {
case a.valAxis(&axis, "Count"):
return a
case len(axis) == 0:
return full(float64(a.strides[0]), 1)
}
tAxis := make([]int, len(a.shape)-len(axis))
cnt := 1
cntAx:
for i, t := 0, 0; i < len(a.shape); i++ {
for _, w := range axis {
if i == w {
cnt *= a.shape[i]
continue cntAx
}
}
tAxis[t] = a.shape[i]
t++
}
return full(float64(cnt), tAxis...)
}
// count is an internal function for scalar count
// TODO: Make public?
func (a *Array64) count(axis ...int) float64 {
if len(axis) == 0 {
return float64(a.strides[0])
}
cnt := 1
for _, w := range axis {
cnt *= a.shape[w]
}
return float64(cnt)
}
// NaNCount calculates the number of values along a given axes.
// Empty call gives the total number of elements.
func (a *Array64) NaNCount(axis ...int) *Array64 {
if a.valAxis(&axis, "NaNCount") {
return a
}
nc := func(d []float64) (r float64) {
for _, v := range d {
if !math.IsNaN(v) {
r++
}
}
return r
}
return a.Fold(nc, axis...)
}
// Mean calculates the mean across the given axes.
// NaN values in the dataa will result in NaN result elements.
func (a *Array64) Mean(axis ...int) *Array64 {
switch {
case a.valAxis(&axis, "Mean"):
return a
}
return a.C().Sum(axis...).DivC(a.count(axis...))
}
// NaNMean calculates the mean across the given axes.
// NaN values are ignored in this calculation.
func (a *Array64) NaNMean(axis ...int) *Array64 {
switch {
case a.valAxis(&axis, "Sum"):
return a
}
return a.NaNSum(axis...).Div(a.NaNCount(axis...))
}
// Nonzero counts the number of non-zero elements in the array
func (a *Array64) Nonzero(axis ...int) *Array64 {
if a.valAxis(&axis, "Nonzero") {
return a
}
return a.Map(func(d float64) float64 {
if d == 0 {
return 0
}
return 1
}).Sum(axis...)
}