| 1 |
package native_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"gorgonia.org/tensor" |
| 7 |
. "gorgonia.org/tensor/native" |
| 8 |
) |
| 9 |
|
| 10 |
type MyType int |
| 11 |
|
| 12 |
func Example_vector() { |
| 13 |
backing := []MyType{ |
| 14 |
0, 1, 2, 3, |
| 15 |
} |
| 16 |
T := tensor.New(tensor.WithShape(4), tensor.WithBacking(backing)) |
| 17 |
val, err := Vector(T) |
| 18 |
if err != nil { |
| 19 |
fmt.Printf("error: %v", err) |
| 20 |
} |
| 21 |
it := val.([]MyType) |
| 22 |
fmt.Println(it) |
| 23 |
|
| 24 |
// Output: |
| 25 |
// [0 1 2 3] |
| 26 |
} |
| 27 |
|
| 28 |
func Example_matrix() { |
| 29 |
backing := []MyType{ |
| 30 |
0, 1, |
| 31 |
2, 3, |
| 32 |
4, 5, |
| 33 |
} |
| 34 |
T := tensor.New(tensor.WithShape(3, 2), tensor.WithBacking(backing)) |
| 35 |
val, err := Matrix(T) |
| 36 |
if err != nil { |
| 37 |
fmt.Printf("error: %v", err) |
| 38 |
} |
| 39 |
|
| 40 |
it := val.([][]MyType) |
| 41 |
fmt.Println(it) |
| 42 |
|
| 43 |
// Output: |
| 44 |
// [[0 1] [2 3] [4 5]] |
| 45 |
} |
| 46 |
|
| 47 |
func Example_tensor3() { |
| 48 |
backing := []MyType{ |
| 49 |
0, 1, 2, 3, |
| 50 |
4, 5, 6, 7, |
| 51 |
8, 9, 10, 11, |
| 52 |
|
| 53 |
12, 13, 14, 15, |
| 54 |
16, 17, 18, 19, |
| 55 |
20, 21, 22, 23, |
| 56 |
} |
| 57 |
T := tensor.New(tensor.WithShape(2, 3, 4), tensor.WithBacking(backing)) |
| 58 |
val, err := Tensor3(T) |
| 59 |
if err != nil { |
| 60 |
fmt.Printf("error: %v", err) |
| 61 |
} |
| 62 |
it := val.([][][]MyType) |
| 63 |
fmt.Println(it) |
| 64 |
|
| 65 |
//Output: |
| 66 |
// [[[0 1 2 3] [4 5 6 7] [8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] |
| 67 |
} |