Skip to content

Commit

Permalink
Added examples for SliceInto
Browse files Browse the repository at this point in the history
  • Loading branch information
chewxy committed May 4, 2022
1 parent 460aba2 commit d0c11c8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dense_matop.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ func (t *Dense) Slice(slices ...Slice) (retVal View, err error) {
// SliceInto is a convenience method. It does NOT copy the values - it simply updates the AP of the view.
// The underlying data is the same.
// This method will override ALL the metadata in view.
func (t *Dense) SliceInto(view Tensor, slices ...Slice) (retVal View, err error) {
func (t *Dense) SliceInto(view Tensor, slices ...Slice) (retVal Tensor, err error) {
switch view := view.(type) {
case nil:
return t.Slice(slices...)
case DenseView:
v := view.Dense
if v, err = t.sliceIntoDense(v, slices...); err != nil {
Expand All @@ -261,7 +263,7 @@ func (t *Dense) SliceInto(view Tensor, slices ...Slice) (retVal View, err error)
}
return DenseView{view}, nil
default:
return nil, nyierr(typeNYI)
return nil, nyierr(typeNYI, view)
}
}

Expand Down
65 changes: 65 additions & 0 deletions example_dense_matop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,71 @@ func ExampleDense_Slice() {
// [1 4]
}

func ExampleDense_SliceInto() {
var v Tensor
var err error
T := New(WithBacking(Range(Int, 0, 9)), WithShape(3, 3))
fmt.Println("SliceInto works with nil values. It simply creates a View.\n==========================================================")
fmt.Printf("T:\n%v\n", T)

if v, err = T.SliceInto(v, makeRS(0, 2), makeRS(0, 2)); err != nil {
fmt.Printf("ERR %v\n", err)
return
}
fmt.Printf("T[0:2, 0:2]:\n%v\n", v)

v.Zero()
fmt.Printf("When v is zeroed, T is zeroed too.\n==================================\nv:\n%v\nT:\n%v\n", v, T)

fmt.Println("Primary use case of SliceInto.\n==============================")
T = New(WithBacking(Range(Int, 0, 9)), WithShape(3, 3))
fmt.Printf("T:\n%v\nv:\n%v\n", T, v)
if v, err = T.SliceInto(v, makeRS(0, 2), makeRS(0, 2)); err != nil {
fmt.Printf("ERR %v\n", err)
return
}
fmt.Printf("v = T[0:2, 0:2]:\n%v\n", v)

// Output:
// SliceInto works with nil values. It simply creates a View.
// ==========================================================
// T:
// ⎡0 1 2⎤
// ⎢3 4 5⎥
// ⎣6 7 8⎦
//
// T[0:2, 0:2]:
// ⎡0 1⎤
// ⎣3 4⎦
//
// When v is zeroed, T is zeroed too.
// ==================================
// v:
// ⎡0 0⎤
// ⎣0 0⎦
//
// T:
// ⎡0 0 0⎤
// ⎢0 0 5⎥
// ⎣6 7 8⎦
//
// Primary use case of SliceInto.
// ==============================
// T:
// ⎡0 1 2⎤
// ⎢3 4 5⎥
// ⎣6 7 8⎦
//
// v:
// ⎡0 0⎤
// ⎣0 0⎦
//
// v = T[0:2, 0:2]:
// ⎡0 1⎤
// ⎣3 4⎦

}

// Slicing works on one dimensional arrays too:
func ExampleDense_Slice_oneDimension() {
var T Tensor
Expand Down

0 comments on commit d0c11c8

Please sign in to comment.