-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbool.go
539 lines (477 loc) · 12.8 KB
/
bool.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package numgo
import (
"encoding/json"
"fmt"
"runtime"
"strings"
)
// Arrayb is an n-dimensional array of boolean values
type Arrayb struct {
shape []int
strides []int
data []bool
err error
debug, stack string
}
// NewArrayB creates an Arrayb object with dimensions given in order from outer-most to inner-most
// All values will default to false
func NewArrayB(data []bool, shape ...int) (a *Arrayb) {
if data != nil && len(shape) == 0 {
shape = append(shape, len(data))
}
a = new(Arrayb)
var sz = 1
sh := make([]int, len(shape))
for _, v := range shape {
if v <= 0 {
a.err = NegativeAxis
if debug {
a.debug = fmt.Sprintf("Negative axis length received by Createb. Shape: %v", shape)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return
}
sz *= v
}
copy(sh, shape)
a.shape = sh
a.data = make([]bool, sz)
if data != nil {
copy(a.data, data)
}
a.strides = make([]int, len(sh)+1)
tmp := 1
for i := len(a.strides) - 1; i > 0; i-- {
a.strides[i] = tmp
tmp *= sh[i-1]
}
a.strides[0] = tmp
a.err = nil
return
}
// Internal function to create using the shape of another array
func newArrayB(shape ...int) (a *Arrayb) {
a = new(Arrayb)
var sz = 1
sh := make([]int, len(shape))
for _, v := range shape {
sz *= v
}
copy(sh, shape)
a.shape = sh
a.data = make([]bool, sz)
a.strides = make([]int, len(sh)+1)
tmp := 1
for i := len(a.strides) - 1; i > 0; i-- {
a.strides[i] = tmp
tmp *= sh[i-1]
}
a.strides[0] = tmp
a.err = nil
return
}
// Fullb creates an Arrayb object with dimensions givin in order from outer-most to inner-most
// All elements will be set to 'val' in the returned array.
func Fullb(val bool, shape ...int) (a *Arrayb) {
a = NewArrayB(nil, shape...)
if a.HasErr() || !val {
return a
}
for i := 0; i < len(a.data); i++ {
a.data[i] = val
}
return
}
func fullb(val bool, shape ...int) (a *Arrayb) {
a = newArrayB(shape...)
if a.HasErr() || !val {
return a
}
for i := 0; i < len(a.data); i++ {
a.data[i] = val
}
return
}
// String Satisfies the Stringer interface for fmt package
func (a *Arrayb) String() (s string) {
switch {
case a == nil:
return "<nil>"
case a.err != nil:
return "Error: " + a.err.Error()
case a.shape == nil || a.strides == nil || a.data == nil:
return "<nil>"
case a.strides[0] == 0:
return "[]"
}
stride := a.strides[len(a.strides)-2]
for i, k := 0, 0; i+stride <= len(a.data); i, k = i+stride, k+1 {
t := ""
for j, v := range a.strides {
if i%v == 0 && j < len(a.strides)-2 {
t += "["
}
}
s += strings.Repeat(" ", len(a.shape)-len(t)-1) + t
s += fmt.Sprint(a.data[i : i+stride])
t = ""
for j, v := range a.strides {
if (i+stride)%v == 0 && j < len(a.strides)-2 {
t += "]"
}
}
s += t + strings.Repeat(" ", len(a.shape)-len(t)-1)
if i+stride != len(a.data) {
s += "\n"
if len(t) > 0 {
s += "\n"
}
}
}
return
}
// Reshape Changes the size of the array axes. Values are not changed or moved.
// This must not change the size of the array.
// Incorrect dimensions will return a nil pointer
func (a *Arrayb) Reshape(shape ...int) *Arrayb {
if a.HasErr() {
return a
}
var sz = 1
sh := make([]int, len(shape))
for _, v := range shape {
if v < 0 {
a.err = NegativeAxis
if debug {
a.debug = fmt.Sprintf("Negative axis length received by Reshape(). Shape: %v", shape)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
sz *= v
}
copy(sh, shape)
if sz != len(a.data) {
a.err = ReshapeError
if debug {
a.debug = fmt.Sprintf("Reshape() can not change data size. Dimensions: %v reshape: %v", a.shape, shape)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
a.strides = make([]int, len(sh)+1)
tmp := 1
for i := len(a.strides) - 1; i > 0; i-- {
a.strides[i] = tmp
tmp *= sh[i-1]
}
a.strides[0] = tmp
a.shape = sh
return a
}
// C will return a deep copy of the source array.
func (a *Arrayb) C() (b *Arrayb) {
if a.HasErr() {
return a
}
b = newArrayB(a.shape...)
copy(b.data, a.data)
return
}
// At returns a copy of the element at the given index.
// Any errors will return a false value and record the error for the
// HasErr() and GetErr() functions.
func (a *Arrayb) At(index ...int) bool {
idx := a.valIdx(index, "At")
if a.HasErr() {
return false
}
return a.data[idx]
}
// SliceElement returns the element group at one axis above the leaf elements.
// Data is returned as a copy in a float slice.
func (a *Arrayb) SliceElement(index ...int) (ret []bool) {
idx := a.valIdx(index, "SliceElement")
switch {
case a.HasErr():
return nil
case len(a.shape)-1 != len(index):
a.err = InvIndexError
if debug {
a.debug = fmt.Sprintf("Incorrect number of indicies received by SliceElement(). Shape: %v Index: %v", a.shape, index)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return nil
}
return append(ret, a.data[idx:idx+a.strides[len(a.strides)-2]]...)
}
// SubArr slices the array object by the index received on each corresponding axis.
//
// These are applied startig from the top axis.
// Intermediate slicing of axes is not available at this point.
func (a *Arrayb) SubArr(index ...int) (ret *Arrayb) {
idx := a.valIdx(index, "SubArr")
if a.HasErr() {
return nil
}
ret = newArrayB(a.shape[len(index):]...)
copy(ret.data, a.data[idx:idx+a.strides[len(index)]])
return
}
// Set sets the element at the given index.
// There should be one index per axis. Generates a ShapeError if incorrect index.
func (a *Arrayb) Set(val bool, index ...int) *Arrayb {
idx := a.valIdx(index, "Set")
if a.HasErr() {
return a
}
a.data[idx] = val
return a
}
// SetSliceElement sets the element group at one axis above the leaf elements.
// Source Array is returned, for function-chaining design.
func (a *Arrayb) SetSliceElement(vals []bool, index ...int) *Arrayb {
idx := a.valIdx(index, "SetSliceElement")
switch {
case a.HasErr():
return a
case len(a.shape)-1 != len(index):
if debug {
a.debug = fmt.Sprintf("Incorrect number of indicies received by SetSliceElement(). Shape: %v Index: %v", a.shape, index)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
fallthrough
case len(vals) != a.shape[len(a.shape)-1]:
a.err = InvIndexError
if debug {
a.debug = fmt.Sprintf("Incorrect slice length received by SetSliceElement(). Shape: %v Index: %v", a.shape, len(index))
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
copy(a.data[idx:idx+a.strides[len(a.strides)-2]], vals[:a.strides[len(a.strides)-2]])
return a
}
// SetSubArr sets the array below a given index to the values in vals.
// Values will be broadcast up multiple axes if the shapes match.
func (a *Arrayb) SetSubArr(vals *Arrayb, index ...int) *Arrayb {
idx := a.valIdx(index, "SetSubArr")
switch {
case a.HasErr():
return a
case vals.HasErr():
a.err = vals.getErr()
if debug {
a.debug = "Array received by SetSubArr() is in error."
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
case len(vals.shape)+len(index) > len(a.shape):
a.err = InvIndexError
if debug {
a.debug = fmt.Sprintf("Array received by SetSubArr() cant be broadcast. Shape: %v Vals shape: %v index: %v", a.shape, vals.shape, index)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
for i, j := len(a.shape)-1, len(vals.shape)-1; j >= 0; i, j = i-1, j-1 {
if a.shape[i] != vals.shape[j] {
a.err = ShapeError
if debug {
a.debug = fmt.Sprintf("Shape of array recieved by SetSubArr() doesn't match receiver. Shape: %v Vals Shape: %v", a.shape, vals.shape)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
}
if len(a.shape)-len(index)-len(vals.shape) == 0 {
copy(a.data[idx:idx+len(vals.data)], vals.data)
return a
}
reps := 1
for i := len(index); i < len(a.shape)-len(vals.shape); i++ {
reps *= a.shape[i]
}
ln := len(vals.data)
for i := 1; i <= reps; i++ {
copy(a.data[idx+ln*(i-1):idx+ln*i], vals.data)
}
return a
}
// Resize will change the underlying array size.
//
// Make a copy C() if the original array needs to remain unchanged.
// Element location in the underlying slice will not be adjusted to the new shape.
func (a *Arrayb) Resize(shape ...int) *Arrayb {
switch {
case a.HasErr():
return a
case len(shape) == 0:
tmp := newArrayB(0)
a.shape, a.strides = tmp.shape, tmp.strides
a.data = tmp.data
return a
}
var sz int = 1
for _, v := range shape {
if v >= 0 {
sz *= v
continue
}
a.err = NegativeAxis
if debug {
a.debug = fmt.Sprintf("Negative axis length received by Resize. Shape: %v", shape)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
ln, cp := len(shape), cap(a.shape)
if ln > cp {
a.shape = append(a.shape[:cp], make([]int, ln-cp)...)
} else {
a.shape = a.shape[:ln]
}
ln, cp = ln+1, cap(a.strides)
if ln > cp {
a.strides = append(a.strides[:cp], make([]int, ln-cp)...)
} else {
a.strides = a.strides[:ln]
}
a.strides[ln-1] = 1
copy(a.shape, shape)
for i := ln - 2; i >= 0; i-- {
a.strides[i] = a.shape[i] * a.strides[i+1]
}
cp = cap(a.data)
if sz > cp {
a.data = append(a.data[:cp], make([]bool, sz-cp)...)
} else {
a.data = a.data[:sz]
}
return a
}
// Append will concatenate a and val at the given axis.
//
// Source array will be changed, so use C() if the original data is needed.
// All axes must be the same except the appending axis.
func (a *Arrayb) Append(val *Arrayb, axis int) *Arrayb {
switch {
case a.HasErr():
return a
case axis >= len(a.shape) || axis < 0:
a.err = IndexError
if debug {
a.debug = fmt.Sprintf("Axis received by Append() out of range. Shape: %v Axis: %v", a.shape, axis)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
case val.HasErr():
a.err = val.getErr()
if debug {
a.debug = "Array received by Append() is in error."
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
case len(a.shape) != len(val.shape):
a.err = ShapeError
if debug {
a.debug = fmt.Sprintf("Array received by Append() can not be matched. Shape: %v Val shape: %v", a.shape, val.shape)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
for k, v := range a.shape {
if v != val.shape[k] && k != axis {
a.err = ShapeError
if debug {
a.debug = fmt.Sprintf("Array received by Append() can not be matched. Shape: %v Val shape: %v", a.shape, val.shape)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return a
}
}
ln := len(a.data) + len(val.data)
var dat []bool
cp := cap(a.data)
if ln > cp {
dat = append(a.data, make([]bool, ln-cp)...)
} else {
dat = a.data[:ln]
}
as, vs := a.strides[axis+1], val.strides[axis+1]
for i, j := a.strides[0], val.strides[0]; i > 0; i, j = i-as, j-vs {
copy(dat[i+j-vs:i+j], val.data[j-vs:j])
copy(dat[i+j-as-vs:i+j-vs], a.data[i-as:i])
}
a.data = dat
a.shape[axis] += val.shape[axis]
for i := axis; i >= 0; i-- {
a.strides[i] = a.strides[axis+1] * a.shape[i]
}
return a
}
// MarshalJSON fulfills the json.Marshaler Interface for encoding data.
// Custom Unmarshaler is needed to encode/send unexported values.
func (a *Arrayb) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Shape []int `json:"shape"`
Data []bool `json:"data"`
Err int8 `json:"err,omitempty"`
}{
Shape: a.shape,
Data: a.data,
Err: encodeErr(a.err),
})
}
// UnmarshalJSON fulfills the json.Unmarshaler interface for decoding data.
// Custom Unmarshaler is needed to load/decode unexported values and build strides.
func (a *Arrayb) UnmarshalJSON(b []byte) error {
tmpA := new(struct {
Shape []int `json:"shape"`
Data []bool `json:"data"`
Err int8 `json:"err,omitempty"`
})
err := json.Unmarshal(b, tmpA)
a.shape = tmpA.Shape
a.data = tmpA.Data
a.err = decodeErr(tmpA.Err)
if a.data == nil && a.err == nil {
a.err = NilError
a.strides = nil
return nil
}
a.strides = make([]int, len(a.shape)+1)
tmp := 1
for i := len(a.strides) - 1; i > 0; i-- {
a.strides[i] = tmp
tmp *= a.shape[i-1]
}
a.strides[0] = tmp
return err
}
// helper function to validate index inputs
func (a *Arrayb) valIdx(index []int, mthd string) (idx int) {
if a.HasErr() {
return 0
}
if len(index) > len(a.shape) {
a.err = InvIndexError
if debug {
a.debug = fmt.Sprintf("Incorrect number of indicies received by %s(). Shape: %v Index: %v", mthd, a.shape, index)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return 0
}
for i, v := range index {
if v >= a.shape[i] || v < 0 {
a.err = IndexError
if debug {
a.debug = fmt.Sprintf("Index received by %s() does not exist shape: %v index: %v", mthd, a.shape, index)
a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return 0
}
idx += v * a.strides[i+1]
}
return
}