Skip to content

Commit

Permalink
Added documentation for Transpose and T()
Browse files Browse the repository at this point in the history
  • Loading branch information
chewxy committed May 4, 2022
1 parent 5d33203 commit 460aba2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
3 changes: 1 addition & 2 deletions api_matop.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ func T(t Tensor, axes ...int) (retVal Tensor, err error) {
case DenseView:
var ret *Dense
if ret, err = tt.SafeT(axes...); err != nil {
return nil, errors.Wrap(err, ".T() off a DenseView")
return nil, errors.Wrap(err, "T() off a DenseView")
}
return DenseView{ret}, nil

default:
return nil, nyierr(typeNYI, t)
}
Expand Down
59 changes: 59 additions & 0 deletions example_matop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tensor_test

import (
"fmt"

"gorgonia.org/tensor"
)

func ExampleTranspose() {
t := tensor.New(tensor.WithShape(2, 3), tensor.WithBacking([]int{1, 2, 3, 4, 5, 6}))
t2, err := tensor.Transpose(t)
if err != nil {
fmt.Printf("ERR: %v\n", err)
}
fmt.Printf("Transpose is a safe operation.\nT:\n%v\nT':\n%v\n", t, t2)
fmt.Printf("The data is changed:\nT : %v\nT': %v", t.Data(), t2.Data())

// Output:
// Transpose is a safe operation.
// T:
// ⎡1 2 3⎤
// ⎣4 5 6⎦
//
// T':
// ⎡1 4⎤
// ⎢2 5⎥
// ⎣3 6⎦
//
// The data is changed:
// T : [1 2 3 4 5 6]
// T': [1 4 2 5 3 6]

}

func ExampleT() {
t := tensor.New(tensor.WithShape(2, 3), tensor.WithBacking([]int{1, 2, 3, 4, 5, 6}))
t2, err := tensor.T(t)
if err != nil {
fmt.Printf("ERR: %v\n", err)
}
fmt.Printf("T is a safe version of the .T() method\nT:\n%v\nT':\n%v\n", t, t2)
fmt.Printf("The data is unchanged:\nT : %v\nT': %v\n", t.Data(), t2.Data())

// Output:
// T is a safe version of the .T() method
// T:
// ⎡1 2 3⎤
// ⎣4 5 6⎦
//
// T':
// ⎡1 4⎤
// ⎢2 5⎥
// ⎣3 6⎦
//
// The data is unchanged:
// T : [1 2 3 4 5 6]
// T': [1 2 3 4 5 6]

}

0 comments on commit 460aba2

Please sign in to comment.