github.com/wzzhu/tensor@v0.9.24/example_dense_cmp_test.go (about) 1 package tensor 2 3 import "fmt" 4 5 // Comparison functions return a Tensor of bool by default. To return the same type, simply pass in the AsSameType function option 6 func ExampleDense_Gt_basic() { 7 var T1, T2, T3, V *Dense 8 var sliced Tensor 9 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 10 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 11 T3, _ = T1.Gt(T2) 12 fmt.Println("Basic operations are safe\n=========================\nT3 = T1 > T2") 13 fmt.Printf("T3:\n%v\n", T3) 14 15 // To return the same type, use the AsSameType function option 16 T3, _ = T1.Gt(T2, AsSameType()) 17 fmt.Println("Returning same type\n===================") 18 fmt.Printf("T3 (Returns Same Type):\n%v\n", T3) 19 20 // Sliced tensors are safe too 21 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 22 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 23 V = sliced.(*Dense) 24 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 25 T3, _ = V.Gt(T2) 26 fmt.Printf("Safe slicing\n============\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 27 28 // Similarly for tensors that return the same type 29 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 30 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 31 V = sliced.(*Dense) 32 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 33 T3, _ = V.Gt(T2, AsSameType()) // AsSameType returns a tensor of the same type 34 fmt.Printf("Safe slicing (Same type)\n========================\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 35 36 // Output: 37 // Basic operations are safe 38 // ========================= 39 // T3 = T1 > T2 40 // T3: 41 // ⎡false false false⎤ 42 // ⎢false false false⎥ 43 // ⎣false false false⎦ 44 // 45 // Returning same type 46 // =================== 47 // T3 (Returns Same Type): 48 // ⎡0 0 0⎤ 49 // ⎢0 0 0⎥ 50 // ⎣0 0 0⎦ 51 // 52 // Safe slicing 53 // ============ 54 // T3: 55 // ⎡false false⎤ 56 // ⎣false false⎦ 57 // 58 // T1 remains unchanged: 59 // ⎡0 1 2⎤ 60 // ⎢3 4 5⎥ 61 // ⎣6 7 8⎦ 62 // 63 // Safe slicing (Same type) 64 // ======================== 65 // T3: 66 // ⎡0 0⎤ 67 // ⎣0 0⎦ 68 // 69 // T1 remains unchanged: 70 // ⎡0 1 2⎤ 71 // ⎢3 4 5⎥ 72 // ⎣6 7 8⎦ 73 } 74 75 // If the UseUnsafe function option is passed into the call, the assumption is made that it will be returning the same type 76 func ExampleDense_Gt_unsafe() { 77 var T1, T2, V *Dense 78 var sliced Tensor 79 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 80 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 81 T1.Gt(T2, UseUnsafe()) 82 fmt.Printf("Unsafe operation\n================\nT1:\n%v\n", T1) 83 84 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 85 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 86 V = sliced.(*Dense) 87 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 88 V.Gt(T2, UseUnsafe()) 89 fmt.Printf("Unsafe operation, with a sliced Tensor\n======================================\nT1:\n%v", T1) 90 91 // Output: 92 // Unsafe operation 93 // ================ 94 // T1: 95 // ⎡0 0 0⎤ 96 // ⎢0 0 0⎥ 97 // ⎣0 0 0⎦ 98 // 99 // Unsafe operation, with a sliced Tensor 100 // ====================================== 101 // T1: 102 // ⎡0 0 2⎤ 103 // ⎢0 0 5⎥ 104 // ⎣6 7 8⎦ 105 } 106 107 // The WithReuse function option can be used to pass in reuse tensors. But be sure to also use the AsSameType() function option 108 // or else funny results will happen 109 func ExampleDense_Gt_reuse() { 110 var T1, T2, T3, V *Dense 111 var sliced Tensor 112 // The reuse tensor is a Tensor of bools... 113 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 114 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 115 T3 = New(WithBacking([]bool{ 116 true, false, true, 117 false, true, false, 118 true, false, true}), WithShape(3, 3)) 119 T1.Gt(T2, WithReuse(T3)) // note that AsSameType is not used here 120 fmt.Printf("Default behaviour: Reuse tensor is expected to be of Bools\n==========================================================\nT3:\n%v\n", T3) 121 122 // If you want to use a Reuse tensor of the same type, then besure to also pass in the AsSameType() flag 123 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 124 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 125 T3 = New(WithBacking(Range(Float64, 100, 109)), WithShape(3, 3)) // The reuse tensor is a Tensor of Float64... 126 T1.Gt(T2, WithReuse(T3), AsSameType()) // AsSameType is used to return float64s 127 fmt.Printf("Reuse With Same Type\n=====================\nT3:\n%v\n", T3) 128 129 // Slicing is similar: 130 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 131 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 132 V = sliced.(*Dense) 133 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 134 T3 = New(WithBacking([]bool{true, true, true, true}), WithShape(2, 2)) 135 V.Gt(T2, WithReuse(T3)) 136 fmt.Printf("Reuse on sliced tensors\n======================\nT3\n%v\n", T3) 137 138 // Again, bear in mind same types 139 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 140 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 141 V = sliced.(*Dense) 142 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 143 T3 = New(WithBacking(Range(Float64, 100, 104)), WithShape(2, 2)) 144 V.Gt(T2, WithReuse(T3), AsSameType()) 145 fmt.Printf("Reuse on sliced tensors (same type)\n=================================\nT3\n%v\n", T3) 146 147 // Output: 148 // Default behaviour: Reuse tensor is expected to be of Bools 149 // ========================================================== 150 // T3: 151 // ⎡false false false⎤ 152 // ⎢false false false⎥ 153 // ⎣false false false⎦ 154 // 155 // Reuse With Same Type 156 // ===================== 157 // T3: 158 // ⎡0 0 0⎤ 159 // ⎢0 0 0⎥ 160 // ⎣0 0 0⎦ 161 // 162 // Reuse on sliced tensors 163 // ====================== 164 // T3 165 // ⎡false false⎤ 166 // ⎣ true true⎦ 167 // 168 // Reuse on sliced tensors (same type) 169 // ================================= 170 // T3 171 // ⎡0 0⎤ 172 // ⎣1 1⎦ 173 } 174 175 /* GTE */ 176 177 // Comparison functions return a Tensor of bool by default. To return the same type, simply pass in the AsSameType function option 178 func ExampleDense_Gte_basic() { 179 var T1, T2, T3, V *Dense 180 var sliced Tensor 181 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 182 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 183 T3, _ = T1.Gte(T2) 184 fmt.Println("Basic operations are safe\n=========================\nT3 = T1 >= T2") 185 fmt.Printf("T3:\n%v\n", T3) 186 187 // To return the same type, use the AsSameType function option 188 T3, _ = T1.Gte(T2, AsSameType()) 189 fmt.Println("Returning same type\n===================") 190 fmt.Printf("T3 (Returns Same Type):\n%v\n", T3) 191 192 // Sliced tensors are safe too 193 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 194 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 195 V = sliced.(*Dense) 196 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 197 T3, _ = V.Gte(T2) 198 fmt.Printf("Safe slicing\n============\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 199 200 // Similarly for tensors that return the same type 201 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 202 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 203 V = sliced.(*Dense) 204 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 205 T3, _ = V.Gte(T2, AsSameType()) // AsSameType returns a tensor of the same type 206 fmt.Printf("Safe slicing (Same type)\n========================\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 207 208 // Output: 209 // Basic operations are safe 210 // ========================= 211 // T3 = T1 >= T2 212 // T3: 213 // ⎡true true true⎤ 214 // ⎢true true true⎥ 215 // ⎣true true true⎦ 216 // 217 // Returning same type 218 // =================== 219 // T3 (Returns Same Type): 220 // ⎡1 1 1⎤ 221 // ⎢1 1 1⎥ 222 // ⎣1 1 1⎦ 223 // 224 // Safe slicing 225 // ============ 226 // T3: 227 // ⎡false false⎤ 228 // ⎣ true true⎦ 229 // 230 // T1 remains unchanged: 231 // ⎡0 1 2⎤ 232 // ⎢3 4 5⎥ 233 // ⎣6 7 8⎦ 234 // 235 // Safe slicing (Same type) 236 // ======================== 237 // T3: 238 // ⎡0 0⎤ 239 // ⎣1 1⎦ 240 // 241 // T1 remains unchanged: 242 // ⎡0 1 2⎤ 243 // ⎢3 4 5⎥ 244 // ⎣6 7 8⎦ 245 } 246 247 // If the UseUnsafe function option is passed into the call, the assumption is made that it will be returning the same type 248 func ExampleDense_Gte_unsafe() { 249 var T1, T2, V *Dense 250 var sliced Tensor 251 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 252 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 253 T1.Gte(T2, UseUnsafe()) 254 fmt.Printf("Unsafe operation\n================\nT1:\n%v\n", T1) 255 256 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 257 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 258 V = sliced.(*Dense) 259 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 260 V.Gte(T2, UseUnsafe()) 261 fmt.Printf("Unsafe operation, with a sliced Tensor\n======================================\nT1:\n%v", T1) 262 263 // Output: 264 // Unsafe operation 265 // ================ 266 // T1: 267 // ⎡1 1 1⎤ 268 // ⎢1 1 1⎥ 269 // ⎣1 1 1⎦ 270 // 271 // Unsafe operation, with a sliced Tensor 272 // ====================================== 273 // T1: 274 // ⎡0 0 2⎤ 275 // ⎢1 1 5⎥ 276 // ⎣6 7 8⎦ 277 } 278 279 // The WithReuse function option can be used to pass in reuse tensors. But be sure to also use the AsSameType() function option 280 // or else funny results will happen 281 func ExampleDense_Gte_reuse() { 282 var T1, T2, T3, V *Dense 283 var sliced Tensor 284 // The reuse tensor is a Tensor of bools... 285 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 286 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 287 T3 = New(WithBacking([]bool{ 288 true, false, true, 289 false, true, false, 290 true, false, true}), WithShape(3, 3)) 291 T1.Gte(T2, WithReuse(T3)) // note that AsSameType is not used here 292 fmt.Printf("Default behaviour: Reuse tensor is expected to be of Bools\n==========================================================\nT3:\n%v\n", T3) 293 294 // If you want to use a Reuse tensor of the same type, then besure to also pass in the AsSameType() flag 295 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 296 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 297 T3 = New(WithBacking(Range(Float64, 100, 109)), WithShape(3, 3)) // The reuse tensor is a Tensor of Float64... 298 T1.Gte(T2, WithReuse(T3), AsSameType()) // AsSameType is used to return float64s 299 fmt.Printf("Reuse With Same Type\n=====================\nT3:\n%v\n", T3) 300 301 // Slicing is similar: 302 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 303 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 304 V = sliced.(*Dense) 305 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 306 T3 = New(WithBacking([]bool{true, true, true, true}), WithShape(2, 2)) 307 V.Gte(T2, WithReuse(T3)) 308 fmt.Printf("Reuse on sliced tensors\n======================\nT3\n%v\n", T3) 309 310 // Again, bear in mind same types 311 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 312 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 313 V = sliced.(*Dense) 314 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 315 T3 = New(WithBacking(Range(Float64, 100, 104)), WithShape(2, 2)) 316 V.Gte(T2, WithReuse(T3), AsSameType()) 317 fmt.Printf("Reuse on sliced tensors (same type)\n=================================\nT3\n%v\n", T3) 318 319 // Output: 320 // Default behaviour: Reuse tensor is expected to be of Bools 321 // ========================================================== 322 // T3: 323 // ⎡true true true⎤ 324 // ⎢true true true⎥ 325 // ⎣true true true⎦ 326 // 327 // Reuse With Same Type 328 // ===================== 329 // T3: 330 // ⎡1 1 1⎤ 331 // ⎢1 1 1⎥ 332 // ⎣1 1 1⎦ 333 // 334 // Reuse on sliced tensors 335 // ====================== 336 // T3 337 // ⎡true true⎤ 338 // ⎣true true⎦ 339 // 340 // Reuse on sliced tensors (same type) 341 // ================================= 342 // T3 343 // ⎡1 1⎤ 344 // ⎣1 1⎦ 345 } 346 347 /* LT */ 348 349 // Comparison functions return a Tensor of bool by default. To return the same type, simply pass in the AsSameType function option 350 func ExampleDense_Lt_basic() { 351 var T1, T2, T3, V *Dense 352 var sliced Tensor 353 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 354 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 355 T3, _ = T1.Lt(T2) 356 fmt.Println("Basic operations are safe\n=========================\nT3 = T1 < T2") 357 fmt.Printf("T3:\n%v\n", T3) 358 359 // To return the same type, use the AsSameType function option 360 T3, _ = T1.Lt(T2, AsSameType()) 361 fmt.Println("Returning same type\n===================") 362 fmt.Printf("T3 (Returns Same Type):\n%v\n", T3) 363 364 // Sliced tensors are safe too 365 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 366 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 367 V = sliced.(*Dense) 368 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 369 T3, _ = V.Lt(T2) 370 fmt.Printf("Safe slicing\n============\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 371 372 // Similarly for tensors that return the same type 373 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 374 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 375 V = sliced.(*Dense) 376 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 377 T3, _ = V.Lt(T2, AsSameType()) // AsSameType returns a tensor of the same type 378 fmt.Printf("Safe slicing (Same type)\n========================\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 379 380 // Output: 381 // Basic operations are safe 382 // ========================= 383 // T3 = T1 < T2 384 // T3: 385 // ⎡false false false⎤ 386 // ⎢false false false⎥ 387 // ⎣false false false⎦ 388 // 389 // Returning same type 390 // =================== 391 // T3 (Returns Same Type): 392 // ⎡0 0 0⎤ 393 // ⎢0 0 0⎥ 394 // ⎣0 0 0⎦ 395 // 396 // Safe slicing 397 // ============ 398 // T3: 399 // ⎡ true true⎤ 400 // ⎣false false⎦ 401 // 402 // T1 remains unchanged: 403 // ⎡0 1 2⎤ 404 // ⎢3 4 5⎥ 405 // ⎣6 7 8⎦ 406 // 407 // Safe slicing (Same type) 408 // ======================== 409 // T3: 410 // ⎡1 1⎤ 411 // ⎣0 0⎦ 412 // 413 // T1 remains unchanged: 414 // ⎡0 1 2⎤ 415 // ⎢3 4 5⎥ 416 // ⎣6 7 8⎦ 417 } 418 419 // If the UseUnsafe function option is passed into the call, the assumption is made that it will be returning the same type 420 func ExampleDense_Lt_unsafe() { 421 var T1, T2, V *Dense 422 var sliced Tensor 423 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 424 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 425 T1.Lt(T2, UseUnsafe()) 426 fmt.Printf("Unsafe operation\n================\nT1:\n%v\n", T1) 427 428 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 429 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 430 V = sliced.(*Dense) 431 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 432 V.Lt(T2, UseUnsafe()) 433 fmt.Printf("Unsafe operation, with a sliced Tensor\n======================================\nT1:\n%v", T1) 434 435 // Output: 436 // Unsafe operation 437 // ================ 438 // T1: 439 // ⎡0 0 0⎤ 440 // ⎢0 0 0⎥ 441 // ⎣0 0 0⎦ 442 // 443 // Unsafe operation, with a sliced Tensor 444 // ====================================== 445 // T1: 446 // ⎡1 1 2⎤ 447 // ⎢0 0 5⎥ 448 // ⎣6 7 8⎦ 449 } 450 451 // The WithReuse function option can be used to pass in reuse tensors. But be sure to also use the AsSameType() function option 452 // or else funny results will happen 453 func ExampleDense_Lt_reuse() { 454 var T1, T2, T3, V *Dense 455 var sliced Tensor 456 // The reuse tensor is a Tensor of bools... 457 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 458 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 459 T3 = New(WithBacking([]bool{ 460 true, false, true, 461 false, true, false, 462 true, false, true}), WithShape(3, 3)) 463 T1.Lt(T2, WithReuse(T3)) // note that AsSameType is not used here 464 fmt.Printf("Default behaviour: Reuse tensor is expected to be of Bools\n==========================================================\nT3:\n%v\n", T3) 465 466 // If you want to use a Reuse tensor of the same type, then besure to also pass in the AsSameType() flag 467 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 468 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 469 T3 = New(WithBacking(Range(Float64, 100, 109)), WithShape(3, 3)) // The reuse tensor is a Tensor of Float64... 470 T1.Lt(T2, WithReuse(T3), AsSameType()) // AsSameType is used to return float64s 471 fmt.Printf("Reuse With Same Type\n=====================\nT3:\n%v\n", T3) 472 473 // Slicing is similar: 474 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 475 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 476 V = sliced.(*Dense) 477 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 478 T3 = New(WithBacking([]bool{true, true, true, true}), WithShape(2, 2)) 479 V.Lt(T2, WithReuse(T3)) 480 fmt.Printf("Reuse on sliced tensors\n======================\nT3\n%v\n", T3) 481 482 // Again, bear in mind same types 483 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 484 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 485 V = sliced.(*Dense) 486 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 487 T3 = New(WithBacking(Range(Float64, 100, 104)), WithShape(2, 2)) 488 V.Lt(T2, WithReuse(T3), AsSameType()) 489 fmt.Printf("Reuse on sliced tensors (same type)\n=================================\nT3\n%v\n", T3) 490 491 // Output: 492 // Default behaviour: Reuse tensor is expected to be of Bools 493 // ========================================================== 494 // T3: 495 // ⎡false false false⎤ 496 // ⎢false false false⎥ 497 // ⎣false false false⎦ 498 // 499 // Reuse With Same Type 500 // ===================== 501 // T3: 502 // ⎡0 0 0⎤ 503 // ⎢0 0 0⎥ 504 // ⎣0 0 0⎦ 505 // 506 // Reuse on sliced tensors 507 // ====================== 508 // T3 509 // ⎡false false⎤ 510 // ⎣false false⎦ 511 // 512 // Reuse on sliced tensors (same type) 513 // ================================= 514 // T3 515 // ⎡0 0⎤ 516 // ⎣0 0⎦ 517 } 518 519 /* LTE */ 520 // Comparison functions return a Tensor of bool by default. To return the same type, simply pass in the AsSameType function option 521 func ExampleDense_Lte_basic() { 522 var T1, T2, T3, V *Dense 523 var sliced Tensor 524 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 525 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 526 T3, _ = T1.Lte(T2) 527 fmt.Println("Basic operations are safe\n=========================\nT3 = T1 <= T2") 528 fmt.Printf("T3:\n%v\n", T3) 529 530 // To return the same type, use the AsSameType function option 531 T3, _ = T1.Lte(T2, AsSameType()) 532 fmt.Println("Returning same type\n===================") 533 fmt.Printf("T3 (Returns Same Type):\n%v\n", T3) 534 535 // Sliced tensors are safe too 536 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 537 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 538 V = sliced.(*Dense) 539 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 540 T3, _ = V.Lte(T2) 541 fmt.Printf("Safe slicing\n============\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 542 543 // Similarly for tensors that return the same type 544 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 545 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 546 V = sliced.(*Dense) 547 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 548 T3, _ = V.Lte(T2, AsSameType()) // AsSameType returns a tensor of the same type 549 fmt.Printf("Safe slicing (Same type)\n========================\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 550 551 // Output: 552 // Basic operations are safe 553 // ========================= 554 // T3 = T1 <= T2 555 // T3: 556 // ⎡true true true⎤ 557 // ⎢true true true⎥ 558 // ⎣true true true⎦ 559 // 560 // Returning same type 561 // =================== 562 // T3 (Returns Same Type): 563 // ⎡1 1 1⎤ 564 // ⎢1 1 1⎥ 565 // ⎣1 1 1⎦ 566 // 567 // Safe slicing 568 // ============ 569 // T3: 570 // ⎡true true⎤ 571 // ⎣true true⎦ 572 // 573 // T1 remains unchanged: 574 // ⎡0 1 2⎤ 575 // ⎢3 4 5⎥ 576 // ⎣6 7 8⎦ 577 // 578 // Safe slicing (Same type) 579 // ======================== 580 // T3: 581 // ⎡1 1⎤ 582 // ⎣1 1⎦ 583 // 584 // T1 remains unchanged: 585 // ⎡0 1 2⎤ 586 // ⎢3 4 5⎥ 587 // ⎣6 7 8⎦ 588 } 589 590 // If the UseUnsafe function option is passed into the call, the assumption is made that it will be returning the same type 591 func ExampleDense_Lte_unsafe() { 592 var T1, T2, V *Dense 593 var sliced Tensor 594 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 595 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 596 T1.Lte(T2, UseUnsafe()) 597 fmt.Printf("Unsafe operation\n================\nT1:\n%v\n", T1) 598 599 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 600 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 601 V = sliced.(*Dense) 602 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 603 V.Lte(T2, UseUnsafe()) 604 fmt.Printf("Unsafe operation, with a sliced Tensor\n======================================\nT1:\n%v", T1) 605 606 // Output: 607 // Unsafe operation 608 // ================ 609 // T1: 610 // ⎡1 1 1⎤ 611 // ⎢1 1 1⎥ 612 // ⎣1 1 1⎦ 613 // 614 // Unsafe operation, with a sliced Tensor 615 // ====================================== 616 // T1: 617 // ⎡1 1 2⎤ 618 // ⎢1 1 5⎥ 619 // ⎣6 7 8⎦ 620 } 621 622 // The WithReuse function option can be used to pass in reuse tensors. But be sure to also use the AsSameType() function option 623 // or else funny results will happen 624 func ExampleDense_Lte_reuse() { 625 var T1, T2, T3, V *Dense 626 var sliced Tensor 627 // The reuse tensor is a Tensor of bools... 628 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 629 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 630 T3 = New(WithBacking([]bool{ 631 true, false, true, 632 false, true, false, 633 true, false, true}), WithShape(3, 3)) 634 T1.Lte(T2, WithReuse(T3)) // note that AsSameType is not used here 635 fmt.Printf("Default behaviour: Reuse tensor is expected to be of Bools\n==========================================================\nT3:\n%v\n", T3) 636 637 // If you want to use a Reuse tensor of the same type, then besure to also pass in the AsSameType() flag 638 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 639 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 640 T3 = New(WithBacking(Range(Float64, 100, 109)), WithShape(3, 3)) // The reuse tensor is a Tensor of Float64... 641 T1.Lte(T2, WithReuse(T3), AsSameType()) // AsSameType is used to return float64s 642 fmt.Printf("Reuse With Same Type\n=====================\nT3:\n%v\n", T3) 643 644 // Slicing is similar: 645 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 646 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 647 V = sliced.(*Dense) 648 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 649 T3 = New(WithBacking([]bool{true, true, true, true}), WithShape(2, 2)) 650 V.Lte(T2, WithReuse(T3)) 651 fmt.Printf("Reuse on sliced tensors\n======================\nT3\n%v\n", T3) 652 653 // Again, bear in mind same types 654 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 655 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 656 V = sliced.(*Dense) 657 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 658 T3 = New(WithBacking(Range(Float64, 100, 104)), WithShape(2, 2)) 659 V.Lte(T2, WithReuse(T3), AsSameType()) 660 fmt.Printf("Reuse on sliced tensors (same type)\n=================================\nT3\n%v\n", T3) 661 662 // Output: 663 // Default behaviour: Reuse tensor is expected to be of Bools 664 // ========================================================== 665 // T3: 666 // ⎡true true true⎤ 667 // ⎢true true true⎥ 668 // ⎣true true true⎦ 669 // 670 // Reuse With Same Type 671 // ===================== 672 // T3: 673 // ⎡1 1 1⎤ 674 // ⎢1 1 1⎥ 675 // ⎣1 1 1⎦ 676 // 677 // Reuse on sliced tensors 678 // ====================== 679 // T3 680 // ⎡ true true⎤ 681 // ⎣false false⎦ 682 // 683 // Reuse on sliced tensors (same type) 684 // ================================= 685 // T3 686 // ⎡1 1⎤ 687 // ⎣0 0⎦ 688 } 689 690 /* ELEQ */ 691 692 // Comparison functions return a Tensor of bool by default. To return the same type, simply pass in the AsSameType function option 693 func ExampleDense_ElEq_basic() { 694 var T1, T2, T3, V *Dense 695 var sliced Tensor 696 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 697 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 698 T3, _ = T1.ElEq(T2) 699 fmt.Println("Basic operations are safe\n=========================\nT3 = T1 == T2") 700 fmt.Printf("T3:\n%v\n", T3) 701 702 // To return the same type, use the AsSameType function option 703 T3, _ = T1.ElEq(T2, AsSameType()) 704 fmt.Println("Returning same type\n===================") 705 fmt.Printf("T3 (Returns Same Type):\n%v\n", T3) 706 707 // Sliced tensors are safe too 708 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 709 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 710 V = sliced.(*Dense) 711 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 712 T3, _ = V.ElEq(T2) 713 fmt.Printf("Safe slicing\n============\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 714 715 // Similarly for tensors that return the same type 716 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 717 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 718 V = sliced.(*Dense) 719 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 720 T3, _ = V.ElEq(T2, AsSameType()) // AsSameType returns a tensor of the same type 721 fmt.Printf("Safe slicing (Same type)\n========================\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 722 723 // Output: 724 // Basic operations are safe 725 // ========================= 726 // T3 = T1 == T2 727 // T3: 728 // ⎡true true true⎤ 729 // ⎢true true true⎥ 730 // ⎣true true true⎦ 731 // 732 // Returning same type 733 // =================== 734 // T3 (Returns Same Type): 735 // ⎡1 1 1⎤ 736 // ⎢1 1 1⎥ 737 // ⎣1 1 1⎦ 738 // 739 // Safe slicing 740 // ============ 741 // T3: 742 // ⎡false false⎤ 743 // ⎣ true true⎦ 744 // 745 // T1 remains unchanged: 746 // ⎡0 1 2⎤ 747 // ⎢3 4 5⎥ 748 // ⎣6 7 8⎦ 749 // 750 // Safe slicing (Same type) 751 // ======================== 752 // T3: 753 // ⎡0 0⎤ 754 // ⎣1 1⎦ 755 // 756 // T1 remains unchanged: 757 // ⎡0 1 2⎤ 758 // ⎢3 4 5⎥ 759 // ⎣6 7 8⎦ 760 } 761 762 // If the UseUnsafe function option is passed into the call, the assumption is made that it will be returning the same type 763 func ExampleDense_ElEq_unsafe() { 764 var T1, T2, V *Dense 765 var sliced Tensor 766 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 767 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 768 T1.ElEq(T2, UseUnsafe()) 769 fmt.Printf("Unsafe operation\n================\nT1:\n%v\n", T1) 770 771 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 772 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 773 V = sliced.(*Dense) 774 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 775 V.ElEq(T2, UseUnsafe()) 776 fmt.Printf("Unsafe operation, with a sliced Tensor\n======================================\nT1:\n%v", T1) 777 778 // Output: 779 // Unsafe operation 780 // ================ 781 // T1: 782 // ⎡1 1 1⎤ 783 // ⎢1 1 1⎥ 784 // ⎣1 1 1⎦ 785 // 786 // Unsafe operation, with a sliced Tensor 787 // ====================================== 788 // T1: 789 // ⎡0 0 2⎤ 790 // ⎢1 1 5⎥ 791 // ⎣6 7 8⎦ 792 } 793 794 // The WithReuse function option can be used to pass in reuse tensors. But be sure to also use the AsSameType() function option 795 // or else funny results will happen 796 func ExampleDense_ElEq_reuse() { 797 var T1, T2, T3, V *Dense 798 var sliced Tensor 799 // The reuse tensor is a Tensor of bools... 800 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 801 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 802 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 803 T3 = New(WithBacking([]bool{ 804 true, false, true, 805 false, true, false, 806 true, false, true}), WithShape(3, 3)) 807 T1.ElEq(T2, WithReuse(T3)) // note that AsSameType is not used here 808 fmt.Printf("Default behaviour: Reuse tensor is expected to be of Bools\n==========================================================\nT3:\n%v\n", T3) 809 810 // If you want to use a Reuse tensor of the same type, then besure to also pass in the AsSameType() flag 811 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 812 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 813 T3 = New(WithBacking(Range(Float64, 100, 109)), WithShape(3, 3)) // The reuse tensor is a Tensor of Float64... 814 T1.ElEq(T2, WithReuse(T3), AsSameType()) // AsSameType is used to return float64s 815 fmt.Printf("Reuse With Same Type\n=====================\nT3:\n%v\n", T3) 816 817 // Slicing is similar: 818 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 819 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 820 V = sliced.(*Dense) 821 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 822 T3 = New(WithBacking([]bool{true, true, true, true}), WithShape(2, 2)) 823 V.ElEq(T2, WithReuse(T3)) 824 fmt.Printf("Reuse on sliced tensors\n======================\nT3\n%v\n", T3) 825 826 // Again, bear in mind same types 827 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 828 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 829 V = sliced.(*Dense) 830 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 831 T3 = New(WithBacking(Range(Float64, 100, 104)), WithShape(2, 2)) 832 V.ElEq(T2, WithReuse(T3), AsSameType()) 833 fmt.Printf("Reuse on sliced tensors (same type)\n=================================\nT3\n%v\n", T3) 834 835 // Output: 836 // Default behaviour: Reuse tensor is expected to be of Bools 837 // ========================================================== 838 // T3: 839 // ⎡true true true⎤ 840 // ⎢true true true⎥ 841 // ⎣true true true⎦ 842 // 843 // Reuse With Same Type 844 // ===================== 845 // T3: 846 // ⎡1 1 1⎤ 847 // ⎢1 1 1⎥ 848 // ⎣1 1 1⎦ 849 // 850 // Reuse on sliced tensors 851 // ====================== 852 // T3 853 // ⎡ true true⎤ 854 // ⎣false false⎦ 855 // 856 // Reuse on sliced tensors (same type) 857 // ================================= 858 // T3 859 // ⎡1 1⎤ 860 // ⎣0 0⎦ 861 } 862 863 /* ELNE */ 864 865 // Comparison functions return a Tensor of bool by default. To return the same type, simply pass in the AsSameType function option 866 func ExampleDense_ElNe_basic() { 867 var T1, T2, T3, V *Dense 868 var sliced Tensor 869 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 870 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 871 T3, _ = T1.ElNe(T2) 872 fmt.Println("Basic operations are safe\n=========================\nT3 = T1 != T2") 873 fmt.Printf("T3:\n%v\n", T3) 874 875 // To return the same type, use the AsSameType function option 876 T3, _ = T1.ElNe(T2, AsSameType()) 877 fmt.Println("Returning same type\n===================") 878 fmt.Printf("T3 (Returns Same Type):\n%v\n", T3) 879 880 // Sliced tensors are safe too 881 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 882 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 883 V = sliced.(*Dense) 884 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 885 T3, _ = V.ElNe(T2) 886 fmt.Printf("Safe slicing\n============\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 887 888 // Similarly for tensors that return the same type 889 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 890 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 891 V = sliced.(*Dense) 892 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 893 T3, _ = V.ElNe(T2, AsSameType()) // AsSameType returns a tensor of the same type 894 fmt.Printf("Safe slicing (Same type)\n========================\nT3:\n%v\nT1 remains unchanged:\n%v\n", T3, T1) 895 896 // Output: 897 // Basic operations are safe 898 // ========================= 899 // T3 = T1 != T2 900 // T3: 901 // ⎡false false false⎤ 902 // ⎢false false false⎥ 903 // ⎣false false false⎦ 904 // 905 // Returning same type 906 // =================== 907 // T3 (Returns Same Type): 908 // ⎡0 0 0⎤ 909 // ⎢0 0 0⎥ 910 // ⎣0 0 0⎦ 911 // 912 // Safe slicing 913 // ============ 914 // T3: 915 // ⎡ true true⎤ 916 // ⎣false false⎦ 917 // 918 // T1 remains unchanged: 919 // ⎡0 1 2⎤ 920 // ⎢3 4 5⎥ 921 // ⎣6 7 8⎦ 922 // 923 // Safe slicing (Same type) 924 // ======================== 925 // T3: 926 // ⎡1 1⎤ 927 // ⎣0 0⎦ 928 // 929 // T1 remains unchanged: 930 // ⎡0 1 2⎤ 931 // ⎢3 4 5⎥ 932 // ⎣6 7 8⎦ 933 } 934 935 // If the UseUnsafe function option is passed into the call, the assumption is made that it will be returning the same type 936 func ExampleDense_ElNe_unsafe() { 937 var T1, T2, V *Dense 938 var sliced Tensor 939 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 940 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 941 T1.ElNe(T2, UseUnsafe()) 942 fmt.Printf("Unsafe operation\n================\nT1:\n%v\n", T1) 943 944 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 945 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 946 V = sliced.(*Dense) 947 T2 = New(WithBacking(Range(Float64, 1, 5)), WithShape(2, 2)) 948 V.ElNe(T2, UseUnsafe()) 949 fmt.Printf("Unsafe operation, with a sliced Tensor\n======================================\nT1:\n%v", T1) 950 951 // Output: 952 // Unsafe operation 953 // ================ 954 // T1: 955 // ⎡0 0 0⎤ 956 // ⎢0 0 0⎥ 957 // ⎣0 0 0⎦ 958 // 959 // Unsafe operation, with a sliced Tensor 960 // ====================================== 961 // T1: 962 // ⎡1 1 2⎤ 963 // ⎢0 0 5⎥ 964 // ⎣6 7 8⎦ 965 } 966 967 // The WithReuse function option can be used to pass in reuse tensors. But be sure to also use the AsSameType() function option 968 // or else funny results will happen 969 func ExampleDense_ElNe_reuse() { 970 var T1, T2, T3, V *Dense 971 var sliced Tensor 972 // The reuse tensor is a Tensor of bools... 973 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 974 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 975 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 976 T3 = New(WithBacking([]bool{ 977 true, false, true, 978 false, true, false, 979 true, false, true}), WithShape(3, 3)) 980 T1.ElNe(T2, WithReuse(T3)) // note that AsSameType is not used here 981 fmt.Printf("Default behaviour: Reuse tensor is expected to be of Bools\n==========================================================\nT3:\n%v\n", T3) 982 983 // If you want to use a Reuse tensor of the same type, then besure to also pass in the AsSameType() flag 984 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 985 T2 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 986 T3 = New(WithBacking(Range(Float64, 100, 109)), WithShape(3, 3)) // The reuse tensor is a Tensor of Float64... 987 T1.ElNe(T2, WithReuse(T3), AsSameType()) // AsSameType is used to return float64s 988 fmt.Printf("Reuse With Same Type\n=====================\nT3:\n%v\n", T3) 989 990 // Slicing is similar: 991 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 992 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 993 V = sliced.(*Dense) 994 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 995 T3 = New(WithBacking([]bool{true, true, true, true}), WithShape(2, 2)) 996 V.ElNe(T2, WithReuse(T3)) 997 fmt.Printf("Reuse on sliced tensors\n======================\nT3\n%v\n", T3) 998 999 // Again, bear in mind same types 1000 T1 = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3)) 1001 sliced, _ = T1.Slice(makeRS(0, 2), makeRS(0, 2)) 1002 V = sliced.(*Dense) 1003 T2 = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2)) 1004 T3 = New(WithBacking(Range(Float64, 100, 104)), WithShape(2, 2)) 1005 V.ElNe(T2, WithReuse(T3), AsSameType()) 1006 fmt.Printf("Reuse on sliced tensors (same type)\n=================================\nT3\n%v\n", T3) 1007 1008 // Output: 1009 // Default behaviour: Reuse tensor is expected to be of Bools 1010 // ========================================================== 1011 // T3: 1012 // ⎡false false false⎤ 1013 // ⎢false false false⎥ 1014 // ⎣false false false⎦ 1015 // 1016 // Reuse With Same Type 1017 // ===================== 1018 // T3: 1019 // ⎡0 0 0⎤ 1020 // ⎢0 0 0⎥ 1021 // ⎣0 0 0⎦ 1022 // 1023 // Reuse on sliced tensors 1024 // ====================== 1025 // T3 1026 // ⎡false false⎤ 1027 // ⎣ true true⎦ 1028 // 1029 // Reuse on sliced tensors (same type) 1030 // ================================= 1031 // T3 1032 // ⎡0 0⎤ 1033 // ⎣1 1⎦ 1034 }