forked from gorgonia/tensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_iterator_test.go
52 lines (43 loc) · 1.25 KB
/
example_iterator_test.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
package tensor
import "fmt"
// This is an example of how to use `IteratorFromDense` from a row-major Dense tensor
func Example_iteratorRowmajor() {
T := New(WithShape(2, 3), WithBacking([]float64{0, 1, 2, 3, 4, 5}))
it := IteratorFromDense(T)
fmt.Printf("T:\n%v\n", T)
for i, err := it.Start(); err == nil; i, err = it.Next() {
fmt.Printf("i: %d, coord: %v\n", i, it.Coord())
}
// Output:
// T:
// ⎡0 1 2⎤
// ⎣3 4 5⎦
//
// i: 0, coord: [0 1]
// i: 1, coord: [0 2]
// i: 2, coord: [1 0]
// i: 3, coord: [1 1]
// i: 4, coord: [1 2]
// i: 5, coord: [0 0]
}
// This is an example of using `IteratorFromDense` on a col-major Dense tensor. More importantly
// this example shows the order of the iteration.
func Example_iteratorcolMajor() {
T := New(WithShape(2, 3), WithBacking([]float64{0, 1, 2, 3, 4, 5}), AsFortran(nil))
it := IteratorFromDense(T)
fmt.Printf("T:\n%v\n", T)
for i, err := it.Start(); err == nil; i, err = it.Next() {
fmt.Printf("i: %d, coord: %v\n", i, it.Coord())
}
// Output:
// T:
// ⎡0 2 4⎤
// ⎣1 3 5⎦
//
// i: 0, coord: [0 1]
// i: 2, coord: [0 2]
// i: 4, coord: [1 0]
// i: 1, coord: [1 1]
// i: 3, coord: [1 2]
// i: 5, coord: [0 0]
}