gorgonia.org/tensor@v0.9.24/native/iterator_native.go (about) 1 // Code generated by genlib2. DO NOT EDIT. 2 3 package native 4 5 import ( 6 "reflect" 7 "unsafe" 8 9 "github.com/pkg/errors" 10 . "gorgonia.org/tensor" 11 ) 12 13 func checkNativeIterable(t *Dense, dims int, dt Dtype) error { 14 // checks: 15 if !t.IsNativelyAccessible() { 16 return errors.Errorf("Cannot convert *Dense to *mat.Dense. Data is inaccessible") 17 } 18 19 if t.Shape().Dims() != dims { 20 return errors.Errorf("Cannot convert *Dense to native iterator. Expected number of dimension: %d, T has got %d dimensions (Shape: %v)", dims, t.Dims(), t.Shape()) 21 } 22 23 if t.F() || t.RequiresIterator() { 24 return errors.Errorf("Not yet implemented: native matrix for colmajor or unpacked matrices") 25 } 26 27 if t.Dtype() != dt { 28 return errors.Errorf("Conversion to native iterable only works on %v. Got %v", dt, t.Dtype()) 29 } 30 31 return nil 32 } 33 34 /* Native Iterables for bool */ 35 36 // VectorB converts a *Dense into a []bool 37 // If the *Dense does not represent a vector of the wanted type, it will return 38 // an error. 39 func VectorB(t *Dense) (retVal []bool, err error) { 40 if err = checkNativeIterable(t, 1, Bool); err != nil { 41 return nil, err 42 } 43 return t.Bools(), nil 44 } 45 46 // MatrixB converts a *Dense into a [][]bool 47 // If the *Dense does not represent a matrix of the wanted type, it 48 // will return an error. 49 func MatrixB(t *Dense) (retVal [][]bool, err error) { 50 if err = checkNativeIterable(t, 2, Bool); err != nil { 51 return nil, err 52 } 53 54 data := t.Bools() 55 shape := t.Shape() 56 strides := t.Strides() 57 58 rows := shape[0] 59 cols := shape[1] 60 rowStride := strides[0] 61 retVal = make([][]bool, rows) 62 for i := range retVal { 63 start := i * rowStride 64 retVal[i] = make([]bool, 0) 65 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 66 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 67 hdr.Cap = cols 68 hdr.Len = cols 69 } 70 return 71 } 72 73 // Tensor3B converts a *Dense into a [][][]bool. 74 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 75 func Tensor3B(t *Dense) (retVal [][][]bool, err error) { 76 if err = checkNativeIterable(t, 3, Bool); err != nil { 77 return nil, err 78 } 79 80 data := t.Bools() 81 shape := t.Shape() 82 strides := t.Strides() 83 84 layers := shape[0] 85 rows := shape[1] 86 cols := shape[2] 87 layerStride := strides[0] 88 rowStride := strides[1] 89 retVal = make([][][]bool, layers) 90 for i := range retVal { 91 retVal[i] = make([][]bool, rows) 92 for j := range retVal[i] { 93 retVal[i][j] = make([]bool, 0) 94 start := i*layerStride + j*rowStride 95 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 96 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 97 hdr.Cap = cols 98 hdr.Len = cols 99 } 100 } 101 return 102 } 103 104 /* Native Iterables for int */ 105 106 // VectorI converts a *Dense into a []int 107 // If the *Dense does not represent a vector of the wanted type, it will return 108 // an error. 109 func VectorI(t *Dense) (retVal []int, err error) { 110 if err = checkNativeIterable(t, 1, Int); err != nil { 111 return nil, err 112 } 113 return t.Ints(), nil 114 } 115 116 // MatrixI converts a *Dense into a [][]int 117 // If the *Dense does not represent a matrix of the wanted type, it 118 // will return an error. 119 func MatrixI(t *Dense) (retVal [][]int, err error) { 120 if err = checkNativeIterable(t, 2, Int); err != nil { 121 return nil, err 122 } 123 124 data := t.Ints() 125 shape := t.Shape() 126 strides := t.Strides() 127 128 rows := shape[0] 129 cols := shape[1] 130 rowStride := strides[0] 131 retVal = make([][]int, rows) 132 for i := range retVal { 133 start := i * rowStride 134 retVal[i] = make([]int, 0) 135 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 136 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 137 hdr.Cap = cols 138 hdr.Len = cols 139 } 140 return 141 } 142 143 // Tensor3I converts a *Dense into a [][][]int. 144 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 145 func Tensor3I(t *Dense) (retVal [][][]int, err error) { 146 if err = checkNativeIterable(t, 3, Int); err != nil { 147 return nil, err 148 } 149 150 data := t.Ints() 151 shape := t.Shape() 152 strides := t.Strides() 153 154 layers := shape[0] 155 rows := shape[1] 156 cols := shape[2] 157 layerStride := strides[0] 158 rowStride := strides[1] 159 retVal = make([][][]int, layers) 160 for i := range retVal { 161 retVal[i] = make([][]int, rows) 162 for j := range retVal[i] { 163 retVal[i][j] = make([]int, 0) 164 start := i*layerStride + j*rowStride 165 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 166 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 167 hdr.Cap = cols 168 hdr.Len = cols 169 } 170 } 171 return 172 } 173 174 /* Native Iterables for int8 */ 175 176 // VectorI8 converts a *Dense into a []int8 177 // If the *Dense does not represent a vector of the wanted type, it will return 178 // an error. 179 func VectorI8(t *Dense) (retVal []int8, err error) { 180 if err = checkNativeIterable(t, 1, Int8); err != nil { 181 return nil, err 182 } 183 return t.Int8s(), nil 184 } 185 186 // MatrixI8 converts a *Dense into a [][]int8 187 // If the *Dense does not represent a matrix of the wanted type, it 188 // will return an error. 189 func MatrixI8(t *Dense) (retVal [][]int8, err error) { 190 if err = checkNativeIterable(t, 2, Int8); err != nil { 191 return nil, err 192 } 193 194 data := t.Int8s() 195 shape := t.Shape() 196 strides := t.Strides() 197 198 rows := shape[0] 199 cols := shape[1] 200 rowStride := strides[0] 201 retVal = make([][]int8, rows) 202 for i := range retVal { 203 start := i * rowStride 204 retVal[i] = make([]int8, 0) 205 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 206 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 207 hdr.Cap = cols 208 hdr.Len = cols 209 } 210 return 211 } 212 213 // Tensor3I8 converts a *Dense into a [][][]int8. 214 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 215 func Tensor3I8(t *Dense) (retVal [][][]int8, err error) { 216 if err = checkNativeIterable(t, 3, Int8); err != nil { 217 return nil, err 218 } 219 220 data := t.Int8s() 221 shape := t.Shape() 222 strides := t.Strides() 223 224 layers := shape[0] 225 rows := shape[1] 226 cols := shape[2] 227 layerStride := strides[0] 228 rowStride := strides[1] 229 retVal = make([][][]int8, layers) 230 for i := range retVal { 231 retVal[i] = make([][]int8, rows) 232 for j := range retVal[i] { 233 retVal[i][j] = make([]int8, 0) 234 start := i*layerStride + j*rowStride 235 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 236 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 237 hdr.Cap = cols 238 hdr.Len = cols 239 } 240 } 241 return 242 } 243 244 /* Native Iterables for int16 */ 245 246 // VectorI16 converts a *Dense into a []int16 247 // If the *Dense does not represent a vector of the wanted type, it will return 248 // an error. 249 func VectorI16(t *Dense) (retVal []int16, err error) { 250 if err = checkNativeIterable(t, 1, Int16); err != nil { 251 return nil, err 252 } 253 return t.Int16s(), nil 254 } 255 256 // MatrixI16 converts a *Dense into a [][]int16 257 // If the *Dense does not represent a matrix of the wanted type, it 258 // will return an error. 259 func MatrixI16(t *Dense) (retVal [][]int16, err error) { 260 if err = checkNativeIterable(t, 2, Int16); err != nil { 261 return nil, err 262 } 263 264 data := t.Int16s() 265 shape := t.Shape() 266 strides := t.Strides() 267 268 rows := shape[0] 269 cols := shape[1] 270 rowStride := strides[0] 271 retVal = make([][]int16, rows) 272 for i := range retVal { 273 start := i * rowStride 274 retVal[i] = make([]int16, 0) 275 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 276 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 277 hdr.Cap = cols 278 hdr.Len = cols 279 } 280 return 281 } 282 283 // Tensor3I16 converts a *Dense into a [][][]int16. 284 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 285 func Tensor3I16(t *Dense) (retVal [][][]int16, err error) { 286 if err = checkNativeIterable(t, 3, Int16); err != nil { 287 return nil, err 288 } 289 290 data := t.Int16s() 291 shape := t.Shape() 292 strides := t.Strides() 293 294 layers := shape[0] 295 rows := shape[1] 296 cols := shape[2] 297 layerStride := strides[0] 298 rowStride := strides[1] 299 retVal = make([][][]int16, layers) 300 for i := range retVal { 301 retVal[i] = make([][]int16, rows) 302 for j := range retVal[i] { 303 retVal[i][j] = make([]int16, 0) 304 start := i*layerStride + j*rowStride 305 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 306 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 307 hdr.Cap = cols 308 hdr.Len = cols 309 } 310 } 311 return 312 } 313 314 /* Native Iterables for int32 */ 315 316 // VectorI32 converts a *Dense into a []int32 317 // If the *Dense does not represent a vector of the wanted type, it will return 318 // an error. 319 func VectorI32(t *Dense) (retVal []int32, err error) { 320 if err = checkNativeIterable(t, 1, Int32); err != nil { 321 return nil, err 322 } 323 return t.Int32s(), nil 324 } 325 326 // MatrixI32 converts a *Dense into a [][]int32 327 // If the *Dense does not represent a matrix of the wanted type, it 328 // will return an error. 329 func MatrixI32(t *Dense) (retVal [][]int32, err error) { 330 if err = checkNativeIterable(t, 2, Int32); err != nil { 331 return nil, err 332 } 333 334 data := t.Int32s() 335 shape := t.Shape() 336 strides := t.Strides() 337 338 rows := shape[0] 339 cols := shape[1] 340 rowStride := strides[0] 341 retVal = make([][]int32, rows) 342 for i := range retVal { 343 start := i * rowStride 344 retVal[i] = make([]int32, 0) 345 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 346 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 347 hdr.Cap = cols 348 hdr.Len = cols 349 } 350 return 351 } 352 353 // Tensor3I32 converts a *Dense into a [][][]int32. 354 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 355 func Tensor3I32(t *Dense) (retVal [][][]int32, err error) { 356 if err = checkNativeIterable(t, 3, Int32); err != nil { 357 return nil, err 358 } 359 360 data := t.Int32s() 361 shape := t.Shape() 362 strides := t.Strides() 363 364 layers := shape[0] 365 rows := shape[1] 366 cols := shape[2] 367 layerStride := strides[0] 368 rowStride := strides[1] 369 retVal = make([][][]int32, layers) 370 for i := range retVal { 371 retVal[i] = make([][]int32, rows) 372 for j := range retVal[i] { 373 retVal[i][j] = make([]int32, 0) 374 start := i*layerStride + j*rowStride 375 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 376 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 377 hdr.Cap = cols 378 hdr.Len = cols 379 } 380 } 381 return 382 } 383 384 /* Native Iterables for int64 */ 385 386 // VectorI64 converts a *Dense into a []int64 387 // If the *Dense does not represent a vector of the wanted type, it will return 388 // an error. 389 func VectorI64(t *Dense) (retVal []int64, err error) { 390 if err = checkNativeIterable(t, 1, Int64); err != nil { 391 return nil, err 392 } 393 return t.Int64s(), nil 394 } 395 396 // MatrixI64 converts a *Dense into a [][]int64 397 // If the *Dense does not represent a matrix of the wanted type, it 398 // will return an error. 399 func MatrixI64(t *Dense) (retVal [][]int64, err error) { 400 if err = checkNativeIterable(t, 2, Int64); err != nil { 401 return nil, err 402 } 403 404 data := t.Int64s() 405 shape := t.Shape() 406 strides := t.Strides() 407 408 rows := shape[0] 409 cols := shape[1] 410 rowStride := strides[0] 411 retVal = make([][]int64, rows) 412 for i := range retVal { 413 start := i * rowStride 414 retVal[i] = make([]int64, 0) 415 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 416 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 417 hdr.Cap = cols 418 hdr.Len = cols 419 } 420 return 421 } 422 423 // Tensor3I64 converts a *Dense into a [][][]int64. 424 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 425 func Tensor3I64(t *Dense) (retVal [][][]int64, err error) { 426 if err = checkNativeIterable(t, 3, Int64); err != nil { 427 return nil, err 428 } 429 430 data := t.Int64s() 431 shape := t.Shape() 432 strides := t.Strides() 433 434 layers := shape[0] 435 rows := shape[1] 436 cols := shape[2] 437 layerStride := strides[0] 438 rowStride := strides[1] 439 retVal = make([][][]int64, layers) 440 for i := range retVal { 441 retVal[i] = make([][]int64, rows) 442 for j := range retVal[i] { 443 retVal[i][j] = make([]int64, 0) 444 start := i*layerStride + j*rowStride 445 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 446 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 447 hdr.Cap = cols 448 hdr.Len = cols 449 } 450 } 451 return 452 } 453 454 /* Native Iterables for uint */ 455 456 // VectorU converts a *Dense into a []uint 457 // If the *Dense does not represent a vector of the wanted type, it will return 458 // an error. 459 func VectorU(t *Dense) (retVal []uint, err error) { 460 if err = checkNativeIterable(t, 1, Uint); err != nil { 461 return nil, err 462 } 463 return t.Uints(), nil 464 } 465 466 // MatrixU converts a *Dense into a [][]uint 467 // If the *Dense does not represent a matrix of the wanted type, it 468 // will return an error. 469 func MatrixU(t *Dense) (retVal [][]uint, err error) { 470 if err = checkNativeIterable(t, 2, Uint); err != nil { 471 return nil, err 472 } 473 474 data := t.Uints() 475 shape := t.Shape() 476 strides := t.Strides() 477 478 rows := shape[0] 479 cols := shape[1] 480 rowStride := strides[0] 481 retVal = make([][]uint, rows) 482 for i := range retVal { 483 start := i * rowStride 484 retVal[i] = make([]uint, 0) 485 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 486 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 487 hdr.Cap = cols 488 hdr.Len = cols 489 } 490 return 491 } 492 493 // Tensor3U converts a *Dense into a [][][]uint. 494 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 495 func Tensor3U(t *Dense) (retVal [][][]uint, err error) { 496 if err = checkNativeIterable(t, 3, Uint); err != nil { 497 return nil, err 498 } 499 500 data := t.Uints() 501 shape := t.Shape() 502 strides := t.Strides() 503 504 layers := shape[0] 505 rows := shape[1] 506 cols := shape[2] 507 layerStride := strides[0] 508 rowStride := strides[1] 509 retVal = make([][][]uint, layers) 510 for i := range retVal { 511 retVal[i] = make([][]uint, rows) 512 for j := range retVal[i] { 513 retVal[i][j] = make([]uint, 0) 514 start := i*layerStride + j*rowStride 515 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 516 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 517 hdr.Cap = cols 518 hdr.Len = cols 519 } 520 } 521 return 522 } 523 524 /* Native Iterables for uint8 */ 525 526 // VectorU8 converts a *Dense into a []uint8 527 // If the *Dense does not represent a vector of the wanted type, it will return 528 // an error. 529 func VectorU8(t *Dense) (retVal []uint8, err error) { 530 if err = checkNativeIterable(t, 1, Uint8); err != nil { 531 return nil, err 532 } 533 return t.Uint8s(), nil 534 } 535 536 // MatrixU8 converts a *Dense into a [][]uint8 537 // If the *Dense does not represent a matrix of the wanted type, it 538 // will return an error. 539 func MatrixU8(t *Dense) (retVal [][]uint8, err error) { 540 if err = checkNativeIterable(t, 2, Uint8); err != nil { 541 return nil, err 542 } 543 544 data := t.Uint8s() 545 shape := t.Shape() 546 strides := t.Strides() 547 548 rows := shape[0] 549 cols := shape[1] 550 rowStride := strides[0] 551 retVal = make([][]uint8, rows) 552 for i := range retVal { 553 start := i * rowStride 554 retVal[i] = make([]uint8, 0) 555 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 556 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 557 hdr.Cap = cols 558 hdr.Len = cols 559 } 560 return 561 } 562 563 // Tensor3U8 converts a *Dense into a [][][]uint8. 564 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 565 func Tensor3U8(t *Dense) (retVal [][][]uint8, err error) { 566 if err = checkNativeIterable(t, 3, Uint8); err != nil { 567 return nil, err 568 } 569 570 data := t.Uint8s() 571 shape := t.Shape() 572 strides := t.Strides() 573 574 layers := shape[0] 575 rows := shape[1] 576 cols := shape[2] 577 layerStride := strides[0] 578 rowStride := strides[1] 579 retVal = make([][][]uint8, layers) 580 for i := range retVal { 581 retVal[i] = make([][]uint8, rows) 582 for j := range retVal[i] { 583 retVal[i][j] = make([]uint8, 0) 584 start := i*layerStride + j*rowStride 585 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 586 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 587 hdr.Cap = cols 588 hdr.Len = cols 589 } 590 } 591 return 592 } 593 594 /* Native Iterables for uint16 */ 595 596 // VectorU16 converts a *Dense into a []uint16 597 // If the *Dense does not represent a vector of the wanted type, it will return 598 // an error. 599 func VectorU16(t *Dense) (retVal []uint16, err error) { 600 if err = checkNativeIterable(t, 1, Uint16); err != nil { 601 return nil, err 602 } 603 return t.Uint16s(), nil 604 } 605 606 // MatrixU16 converts a *Dense into a [][]uint16 607 // If the *Dense does not represent a matrix of the wanted type, it 608 // will return an error. 609 func MatrixU16(t *Dense) (retVal [][]uint16, err error) { 610 if err = checkNativeIterable(t, 2, Uint16); err != nil { 611 return nil, err 612 } 613 614 data := t.Uint16s() 615 shape := t.Shape() 616 strides := t.Strides() 617 618 rows := shape[0] 619 cols := shape[1] 620 rowStride := strides[0] 621 retVal = make([][]uint16, rows) 622 for i := range retVal { 623 start := i * rowStride 624 retVal[i] = make([]uint16, 0) 625 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 626 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 627 hdr.Cap = cols 628 hdr.Len = cols 629 } 630 return 631 } 632 633 // Tensor3U16 converts a *Dense into a [][][]uint16. 634 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 635 func Tensor3U16(t *Dense) (retVal [][][]uint16, err error) { 636 if err = checkNativeIterable(t, 3, Uint16); err != nil { 637 return nil, err 638 } 639 640 data := t.Uint16s() 641 shape := t.Shape() 642 strides := t.Strides() 643 644 layers := shape[0] 645 rows := shape[1] 646 cols := shape[2] 647 layerStride := strides[0] 648 rowStride := strides[1] 649 retVal = make([][][]uint16, layers) 650 for i := range retVal { 651 retVal[i] = make([][]uint16, rows) 652 for j := range retVal[i] { 653 retVal[i][j] = make([]uint16, 0) 654 start := i*layerStride + j*rowStride 655 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 656 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 657 hdr.Cap = cols 658 hdr.Len = cols 659 } 660 } 661 return 662 } 663 664 /* Native Iterables for uint32 */ 665 666 // VectorU32 converts a *Dense into a []uint32 667 // If the *Dense does not represent a vector of the wanted type, it will return 668 // an error. 669 func VectorU32(t *Dense) (retVal []uint32, err error) { 670 if err = checkNativeIterable(t, 1, Uint32); err != nil { 671 return nil, err 672 } 673 return t.Uint32s(), nil 674 } 675 676 // MatrixU32 converts a *Dense into a [][]uint32 677 // If the *Dense does not represent a matrix of the wanted type, it 678 // will return an error. 679 func MatrixU32(t *Dense) (retVal [][]uint32, err error) { 680 if err = checkNativeIterable(t, 2, Uint32); err != nil { 681 return nil, err 682 } 683 684 data := t.Uint32s() 685 shape := t.Shape() 686 strides := t.Strides() 687 688 rows := shape[0] 689 cols := shape[1] 690 rowStride := strides[0] 691 retVal = make([][]uint32, rows) 692 for i := range retVal { 693 start := i * rowStride 694 retVal[i] = make([]uint32, 0) 695 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 696 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 697 hdr.Cap = cols 698 hdr.Len = cols 699 } 700 return 701 } 702 703 // Tensor3U32 converts a *Dense into a [][][]uint32. 704 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 705 func Tensor3U32(t *Dense) (retVal [][][]uint32, err error) { 706 if err = checkNativeIterable(t, 3, Uint32); err != nil { 707 return nil, err 708 } 709 710 data := t.Uint32s() 711 shape := t.Shape() 712 strides := t.Strides() 713 714 layers := shape[0] 715 rows := shape[1] 716 cols := shape[2] 717 layerStride := strides[0] 718 rowStride := strides[1] 719 retVal = make([][][]uint32, layers) 720 for i := range retVal { 721 retVal[i] = make([][]uint32, rows) 722 for j := range retVal[i] { 723 retVal[i][j] = make([]uint32, 0) 724 start := i*layerStride + j*rowStride 725 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 726 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 727 hdr.Cap = cols 728 hdr.Len = cols 729 } 730 } 731 return 732 } 733 734 /* Native Iterables for uint64 */ 735 736 // VectorU64 converts a *Dense into a []uint64 737 // If the *Dense does not represent a vector of the wanted type, it will return 738 // an error. 739 func VectorU64(t *Dense) (retVal []uint64, err error) { 740 if err = checkNativeIterable(t, 1, Uint64); err != nil { 741 return nil, err 742 } 743 return t.Uint64s(), nil 744 } 745 746 // MatrixU64 converts a *Dense into a [][]uint64 747 // If the *Dense does not represent a matrix of the wanted type, it 748 // will return an error. 749 func MatrixU64(t *Dense) (retVal [][]uint64, err error) { 750 if err = checkNativeIterable(t, 2, Uint64); err != nil { 751 return nil, err 752 } 753 754 data := t.Uint64s() 755 shape := t.Shape() 756 strides := t.Strides() 757 758 rows := shape[0] 759 cols := shape[1] 760 rowStride := strides[0] 761 retVal = make([][]uint64, rows) 762 for i := range retVal { 763 start := i * rowStride 764 retVal[i] = make([]uint64, 0) 765 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 766 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 767 hdr.Cap = cols 768 hdr.Len = cols 769 } 770 return 771 } 772 773 // Tensor3U64 converts a *Dense into a [][][]uint64. 774 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 775 func Tensor3U64(t *Dense) (retVal [][][]uint64, err error) { 776 if err = checkNativeIterable(t, 3, Uint64); err != nil { 777 return nil, err 778 } 779 780 data := t.Uint64s() 781 shape := t.Shape() 782 strides := t.Strides() 783 784 layers := shape[0] 785 rows := shape[1] 786 cols := shape[2] 787 layerStride := strides[0] 788 rowStride := strides[1] 789 retVal = make([][][]uint64, layers) 790 for i := range retVal { 791 retVal[i] = make([][]uint64, rows) 792 for j := range retVal[i] { 793 retVal[i][j] = make([]uint64, 0) 794 start := i*layerStride + j*rowStride 795 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 796 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 797 hdr.Cap = cols 798 hdr.Len = cols 799 } 800 } 801 return 802 } 803 804 /* Native Iterables for float32 */ 805 806 // VectorF32 converts a *Dense into a []float32 807 // If the *Dense does not represent a vector of the wanted type, it will return 808 // an error. 809 func VectorF32(t *Dense) (retVal []float32, err error) { 810 if err = checkNativeIterable(t, 1, Float32); err != nil { 811 return nil, err 812 } 813 return t.Float32s(), nil 814 } 815 816 // MatrixF32 converts a *Dense into a [][]float32 817 // If the *Dense does not represent a matrix of the wanted type, it 818 // will return an error. 819 func MatrixF32(t *Dense) (retVal [][]float32, err error) { 820 if err = checkNativeIterable(t, 2, Float32); err != nil { 821 return nil, err 822 } 823 824 data := t.Float32s() 825 shape := t.Shape() 826 strides := t.Strides() 827 828 rows := shape[0] 829 cols := shape[1] 830 rowStride := strides[0] 831 retVal = make([][]float32, rows) 832 for i := range retVal { 833 start := i * rowStride 834 retVal[i] = make([]float32, 0) 835 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 836 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 837 hdr.Cap = cols 838 hdr.Len = cols 839 } 840 return 841 } 842 843 // Tensor3F32 converts a *Dense into a [][][]float32. 844 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 845 func Tensor3F32(t *Dense) (retVal [][][]float32, err error) { 846 if err = checkNativeIterable(t, 3, Float32); err != nil { 847 return nil, err 848 } 849 850 data := t.Float32s() 851 shape := t.Shape() 852 strides := t.Strides() 853 854 layers := shape[0] 855 rows := shape[1] 856 cols := shape[2] 857 layerStride := strides[0] 858 rowStride := strides[1] 859 retVal = make([][][]float32, layers) 860 for i := range retVal { 861 retVal[i] = make([][]float32, rows) 862 for j := range retVal[i] { 863 retVal[i][j] = make([]float32, 0) 864 start := i*layerStride + j*rowStride 865 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 866 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 867 hdr.Cap = cols 868 hdr.Len = cols 869 } 870 } 871 return 872 } 873 874 /* Native Iterables for float64 */ 875 876 // VectorF64 converts a *Dense into a []float64 877 // If the *Dense does not represent a vector of the wanted type, it will return 878 // an error. 879 func VectorF64(t *Dense) (retVal []float64, err error) { 880 if err = checkNativeIterable(t, 1, Float64); err != nil { 881 return nil, err 882 } 883 return t.Float64s(), nil 884 } 885 886 // MatrixF64 converts a *Dense into a [][]float64 887 // If the *Dense does not represent a matrix of the wanted type, it 888 // will return an error. 889 func MatrixF64(t *Dense) (retVal [][]float64, err error) { 890 if err = checkNativeIterable(t, 2, Float64); err != nil { 891 return nil, err 892 } 893 894 data := t.Float64s() 895 shape := t.Shape() 896 strides := t.Strides() 897 898 rows := shape[0] 899 cols := shape[1] 900 rowStride := strides[0] 901 retVal = make([][]float64, rows) 902 for i := range retVal { 903 start := i * rowStride 904 retVal[i] = make([]float64, 0) 905 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 906 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 907 hdr.Cap = cols 908 hdr.Len = cols 909 } 910 return 911 } 912 913 // Tensor3F64 converts a *Dense into a [][][]float64. 914 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 915 func Tensor3F64(t *Dense) (retVal [][][]float64, err error) { 916 if err = checkNativeIterable(t, 3, Float64); err != nil { 917 return nil, err 918 } 919 920 data := t.Float64s() 921 shape := t.Shape() 922 strides := t.Strides() 923 924 layers := shape[0] 925 rows := shape[1] 926 cols := shape[2] 927 layerStride := strides[0] 928 rowStride := strides[1] 929 retVal = make([][][]float64, layers) 930 for i := range retVal { 931 retVal[i] = make([][]float64, rows) 932 for j := range retVal[i] { 933 retVal[i][j] = make([]float64, 0) 934 start := i*layerStride + j*rowStride 935 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 936 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 937 hdr.Cap = cols 938 hdr.Len = cols 939 } 940 } 941 return 942 } 943 944 /* Native Iterables for complex64 */ 945 946 // VectorC64 converts a *Dense into a []complex64 947 // If the *Dense does not represent a vector of the wanted type, it will return 948 // an error. 949 func VectorC64(t *Dense) (retVal []complex64, err error) { 950 if err = checkNativeIterable(t, 1, Complex64); err != nil { 951 return nil, err 952 } 953 return t.Complex64s(), nil 954 } 955 956 // MatrixC64 converts a *Dense into a [][]complex64 957 // If the *Dense does not represent a matrix of the wanted type, it 958 // will return an error. 959 func MatrixC64(t *Dense) (retVal [][]complex64, err error) { 960 if err = checkNativeIterable(t, 2, Complex64); err != nil { 961 return nil, err 962 } 963 964 data := t.Complex64s() 965 shape := t.Shape() 966 strides := t.Strides() 967 968 rows := shape[0] 969 cols := shape[1] 970 rowStride := strides[0] 971 retVal = make([][]complex64, rows) 972 for i := range retVal { 973 start := i * rowStride 974 retVal[i] = make([]complex64, 0) 975 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 976 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 977 hdr.Cap = cols 978 hdr.Len = cols 979 } 980 return 981 } 982 983 // Tensor3C64 converts a *Dense into a [][][]complex64. 984 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 985 func Tensor3C64(t *Dense) (retVal [][][]complex64, err error) { 986 if err = checkNativeIterable(t, 3, Complex64); err != nil { 987 return nil, err 988 } 989 990 data := t.Complex64s() 991 shape := t.Shape() 992 strides := t.Strides() 993 994 layers := shape[0] 995 rows := shape[1] 996 cols := shape[2] 997 layerStride := strides[0] 998 rowStride := strides[1] 999 retVal = make([][][]complex64, layers) 1000 for i := range retVal { 1001 retVal[i] = make([][]complex64, rows) 1002 for j := range retVal[i] { 1003 retVal[i][j] = make([]complex64, 0) 1004 start := i*layerStride + j*rowStride 1005 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 1006 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 1007 hdr.Cap = cols 1008 hdr.Len = cols 1009 } 1010 } 1011 return 1012 } 1013 1014 /* Native Iterables for complex128 */ 1015 1016 // VectorC128 converts a *Dense into a []complex128 1017 // If the *Dense does not represent a vector of the wanted type, it will return 1018 // an error. 1019 func VectorC128(t *Dense) (retVal []complex128, err error) { 1020 if err = checkNativeIterable(t, 1, Complex128); err != nil { 1021 return nil, err 1022 } 1023 return t.Complex128s(), nil 1024 } 1025 1026 // MatrixC128 converts a *Dense into a [][]complex128 1027 // If the *Dense does not represent a matrix of the wanted type, it 1028 // will return an error. 1029 func MatrixC128(t *Dense) (retVal [][]complex128, err error) { 1030 if err = checkNativeIterable(t, 2, Complex128); err != nil { 1031 return nil, err 1032 } 1033 1034 data := t.Complex128s() 1035 shape := t.Shape() 1036 strides := t.Strides() 1037 1038 rows := shape[0] 1039 cols := shape[1] 1040 rowStride := strides[0] 1041 retVal = make([][]complex128, rows) 1042 for i := range retVal { 1043 start := i * rowStride 1044 retVal[i] = make([]complex128, 0) 1045 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 1046 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 1047 hdr.Cap = cols 1048 hdr.Len = cols 1049 } 1050 return 1051 } 1052 1053 // Tensor3C128 converts a *Dense into a [][][]complex128. 1054 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 1055 func Tensor3C128(t *Dense) (retVal [][][]complex128, err error) { 1056 if err = checkNativeIterable(t, 3, Complex128); err != nil { 1057 return nil, err 1058 } 1059 1060 data := t.Complex128s() 1061 shape := t.Shape() 1062 strides := t.Strides() 1063 1064 layers := shape[0] 1065 rows := shape[1] 1066 cols := shape[2] 1067 layerStride := strides[0] 1068 rowStride := strides[1] 1069 retVal = make([][][]complex128, layers) 1070 for i := range retVal { 1071 retVal[i] = make([][]complex128, rows) 1072 for j := range retVal[i] { 1073 retVal[i][j] = make([]complex128, 0) 1074 start := i*layerStride + j*rowStride 1075 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 1076 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 1077 hdr.Cap = cols 1078 hdr.Len = cols 1079 } 1080 } 1081 return 1082 } 1083 1084 /* Native Iterables for string */ 1085 1086 // VectorStr converts a *Dense into a []string 1087 // If the *Dense does not represent a vector of the wanted type, it will return 1088 // an error. 1089 func VectorStr(t *Dense) (retVal []string, err error) { 1090 if err = checkNativeIterable(t, 1, String); err != nil { 1091 return nil, err 1092 } 1093 return t.Strings(), nil 1094 } 1095 1096 // MatrixStr converts a *Dense into a [][]string 1097 // If the *Dense does not represent a matrix of the wanted type, it 1098 // will return an error. 1099 func MatrixStr(t *Dense) (retVal [][]string, err error) { 1100 if err = checkNativeIterable(t, 2, String); err != nil { 1101 return nil, err 1102 } 1103 1104 data := t.Strings() 1105 shape := t.Shape() 1106 strides := t.Strides() 1107 1108 rows := shape[0] 1109 cols := shape[1] 1110 rowStride := strides[0] 1111 retVal = make([][]string, rows) 1112 for i := range retVal { 1113 start := i * rowStride 1114 retVal[i] = make([]string, 0) 1115 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i])) 1116 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 1117 hdr.Cap = cols 1118 hdr.Len = cols 1119 } 1120 return 1121 } 1122 1123 // Tensor3Str converts a *Dense into a [][][]string. 1124 // If the *Dense does not represent a 3-tensor of the wanted type, it will return an error. 1125 func Tensor3Str(t *Dense) (retVal [][][]string, err error) { 1126 if err = checkNativeIterable(t, 3, String); err != nil { 1127 return nil, err 1128 } 1129 1130 data := t.Strings() 1131 shape := t.Shape() 1132 strides := t.Strides() 1133 1134 layers := shape[0] 1135 rows := shape[1] 1136 cols := shape[2] 1137 layerStride := strides[0] 1138 rowStride := strides[1] 1139 retVal = make([][][]string, layers) 1140 for i := range retVal { 1141 retVal[i] = make([][]string, rows) 1142 for j := range retVal[i] { 1143 retVal[i][j] = make([]string, 0) 1144 start := i*layerStride + j*rowStride 1145 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&retVal[i][j])) 1146 hdr.Data = uintptr(unsafe.Pointer(&data[start])) 1147 hdr.Cap = cols 1148 hdr.Len = cols 1149 } 1150 } 1151 return 1152 }