Debian Codegraph @

example_tensor_basics_test.go

raw
1 package tensor
2
3 import "fmt"
4
5 // This example showcases the very basics of the package.
6 func Example_basics() {
7 // Create a (2, 2)-Matrix of integers
8 a := New(WithShape(2, 2), WithBacking([]int{1, 2, 3, 4}))
9 fmt.Printf("a:\n%v\n", a)
10
11 // Create a (2, 3, 4)-tensor of float32s
12 b := New(WithBacking(Range(Float32, 0, 24)), WithShape(2, 3, 4))
13 fmt.Printf("b:\n%1.1f", b)
14
15 // Accessing data
16 x, _ := b.At(0, 1, 2) // in Numpy syntax: b[0,1,2]
17 fmt.Printf("x: %1.1f\n\n", x)
18
19 // Setting data
20 b.SetAt(float32(1000), 0, 1, 2)
21 fmt.Printf("b:\n%v", b)
22
23 // Output:
24 // a:
25 // ⎡1 2⎤
26 // ⎣3 4⎦
27 //
28 // b:
29 // ⎡ 0.0 1.0 2.0 3.0⎤
30 // ⎢ 4.0 5.0 6.0 7.0⎥
31 // ⎣ 8.0 9.0 10.0 11.0⎦
32 //
33 // ⎡12.0 13.0 14.0 15.0⎤
34 // ⎢16.0 17.0 18.0 19.0⎥
35 // ⎣20.0 21.0 22.0 23.0⎦
36 //
37 // x: 6.0
38 //
39 // b:
40 // ⎡ 0 1 2 3⎤
41 // ⎢ 4 5 1000 7⎥
42 // ⎣ 8 9 10 11⎦
43 //
44 // ⎡ 12 13 14 15⎤
45 // ⎢ 16 17 18 19⎥
46 // ⎣ 20 21 22 23⎦
47 }
48
49 // This example showcases interactions between different data orders
50 func Example_differingDataOrders() {
51 T0 := New(WithShape(2, 3), WithBacking(Range(Int, 0, 6))) // Create a (2, 3)-matrix with the standard row-major backing
52 T1 := New(WithShape(2, 3), WithBacking(Range(Int, 0, 6)), AsFortran(nil)) // Create a (2, 3)-matrix with a col-major backing
53 T2, _ := Add(T0, T1)
54 fmt.Printf("T0:\n%vT1:\n%vT2:\n%vT2 Data Order: %v\n\n", T0, T1, T2, T2.DataOrder())
55
56 // the result's data order is highly dependent on the order of operation. It will take after the first operand
57 T0 = New(WithShape(2, 3), WithBacking(Range(Int, 1, 7)), AsFortran(nil)) // Create a (2, 3)-matrix with a col-major backing
58 T1 = New(WithShape(2, 3), WithBacking(Range(Int, 1, 7))) // Create a (2, 3)-matrix with the standard row-major backing
59 T2, _ = Add(T0, T1)
60 fmt.Printf("T0:\n%vT1:\n%vT2:\n%vT2 Data Order: %v\n\n", T0, T1, T2, T2.DataOrder())
61
62 reuse := New(WithShape(2, 3), WithBacking([]int{1000, 1000, 1000, 1000, 1000, 1000}))
63 fmt.Printf("reuse Data Order: %v\n", reuse.DataOrder())
64 T2, _ = Add(T0, T1, WithReuse(reuse))
65 fmt.Printf("T2:\n%vT2 Data Order: %v\n\n", T2, T2.DataOrder())
66
67 // Output:
68 // T0:
69 // ⎡0 1 2⎤
70 // ⎣3 4 5⎦
71 // T1:
72 // ⎡0 2 4⎤
73 // ⎣1 3 5⎦
74 // T2:
75 // ⎡ 0 3 6⎤
76 // ⎣ 4 7 10⎦
77 // T2 Data Order: Contiguous, RowMajor
78 //
79 //
80 // T0:
81 // ⎡1 3 5⎤
82 // ⎣2 4 6⎦
83 // T1:
84 // ⎡1 2 3⎤
85 // ⎣4 5 6⎦
86 // T2:
87 // ⎡ 2 5 8⎤
88 // ⎣ 6 9 12⎦
89 // T2 Data Order: Contiguous, ColMajor
90 //
91 //
92 // reuse Data Order: Contiguous, RowMajor
93 // T2:
94 // ⎡ 2 5 8⎤
95 // ⎣ 6 9 12⎦
96 // T2 Data Order: Contiguous, ColMajor
97
98 }
99
100 // The AsFortran construction option is a bit finnicky.
101 func Example_asFortran() {
102 // Here the data is passed in and directly used without changing the underlying data
103 T0 := New(WithShape(2, 3), WithBacking([]float64{0, 1, 2, 3, 4, 5}), AsFortran(nil))
104 fmt.Printf("T0:\n%vData: %v\n\n", T0, T0.Data())
105
106 // Here the data is passed into the AsFortran construction option, and it assumes that the data is already in
107 // row-major form. Therefore a transpose will be performed.
108 T1 := New(WithShape(2, 3), AsFortran([]float64{0, 1, 2, 3, 4, 5}))
109 fmt.Printf("T1:\n%vData: %v\n\n", T1, T1.Data())
110
111 // Further example of how AsFortran works:
112 orig := New(WithShape(2, 3), WithBacking([]float64{0, 1, 2, 3, 4, 5}))
113 T2 := New(WithShape(2, 3), AsFortran(orig))
114 fmt.Printf("Original\n%vData: %v\n", orig, orig.Data())
115 fmt.Printf("T2:\n%vData: %v\n", T2, T2.Data())
116
117 // Output:
118 // T0:
119 // ⎡0 2 4⎤
120 // ⎣1 3 5⎦
121 // Data: [0 1 2 3 4 5]
122 //
123 // T1:
124 // ⎡0 1 2⎤
125 // ⎣3 4 5⎦
126 // Data: [0 3 1 4 2 5]
127 //
128 // Original
129 // ⎡0 1 2⎤
130 // ⎣3 4 5⎦
131 // Data: [0 1 2 3 4 5]
132 // T2:
133 // ⎡0 1 2⎤
134 // ⎣3 4 5⎦
135 // Data: [0 3 1 4 2 5]
136 }
137
138 // The AsDenseDiag construction option creates a dense diagonal matrix from the input, either a slice or a tensor.
139 // The resulting shape is automatically inferred from the input vector.
140 //
141 // This is like Numpy's `diag()` function, except not stupid. Numpy's `diag()` has been a cause of errors because it's somewhat isometric:
142 // >>> np.diag(np.diag(np.array([1,2,3])))
143 // array([1,2,3])
144 func Example_asDenseDiag() {
145 T := New(WithShape(3), WithBacking([]int{1, 2, 3}))
146 T1 := New(AsDenseDiag(T))
147 fmt.Printf("T1:\n%v", T1)
148
149 T2 := New(AsDenseDiag([]float64{3.14, 6.28, 11111}))
150 fmt.Printf("T2:\n%v", T2)
151 // Output:
152 // T1:
153 //⎡1 0 0⎤
154 //⎢0 2 0⎥
155 //⎣0 0 3⎦
156 // T2:
157 // ⎡ 3.14 0 0⎤
158 // ⎢ 0 6.28 0⎥
159 // ⎣ 0 0 11111⎦
160 }