From d0c11c89d069a86baffff7da027bdd9f0b63d393 Mon Sep 17 00:00:00 2001 From: chewxy Date: Wed, 4 May 2022 14:21:29 +1000 Subject: [PATCH] Added examples for SliceInto --- dense_matop.go | 6 ++-- example_dense_matop_test.go | 65 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/dense_matop.go b/dense_matop.go index 3a04f9c..3a9f005 100644 --- a/dense_matop.go +++ b/dense_matop.go @@ -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 { @@ -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) } } diff --git a/example_dense_matop_test.go b/example_dense_matop_test.go index 1e81271..c6f50a5 100644 --- a/example_dense_matop_test.go +++ b/example_dense_matop_test.go @@ -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