Skip to content

Commit

Permalink
Example iterators (#114)
Browse files Browse the repository at this point in the history
* Fixed #90

* Added an example for SliceIterator
  • Loading branch information
chewxy committed Apr 6, 2021
1 parent e0f1fcd commit da5e1e2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 53 deletions.
4 changes: 3 additions & 1 deletion api_matop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tensor

import "github.com/pkg/errors"
import (
"github.com/pkg/errors"
)

// this file handles matops. While by default most of these matops should already have been defined as part of the
// Tensor interface, not all are possible(for example, concatenating a sparse tensor), hence the need for the following functions
Expand Down
129 changes: 77 additions & 52 deletions example_iterator_test.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,77 @@
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]

}
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]

}

func ExampleSliceIter() {
T := New(WithShape(3, 3), WithBacking(Range(Float64, 0, 9)))
S, err := T.Slice(makeRS(1, 3), makeRS(1, 3))
if err != nil {
fmt.Printf("Err %v\n", err)
return
}
fmt.Printf("S (requires iterator? %t)\n%v\n", S.(*Dense).RequiresIterator(), S)
it := IteratorFromDense(S.(*Dense))
for i, err := it.Start(); err == nil; i, err = it.Next() {
fmt.Printf("i %d, coord %v\n", i, it.Coord())
}

// Output:
// S (requires iterator? true)
// ⎡4 5⎤
// ⎣7 8⎦
//
// i 0, coord [0 1]
// i 1, coord [1 0]
// i 3, coord [1 1]
// i 4, coord [0 0]

}

0 comments on commit da5e1e2

Please sign in to comment.