Projection2DIndex returns the flat 1D index for given row, col coords for a 2D projection of the given tensor shape, collapsing higher dimensions down to 2D (and 1D up to 2D). For any odd number of dimensions, the remaining outer-most dimension can either be multipliexed across the row or column, gi
(shp *Shape, oddRow bool, row, col int)
| 51 | // Even multiples of inner-most dimensions are assumed to be row, then column. |
| 52 | // RowMajor and ColMajor layouts are handled appropriately. |
| 53 | func Projection2DIndex(shp *Shape, oddRow bool, row, col int) int { |
| 54 | nd := shp.NumDims() |
| 55 | switch nd { |
| 56 | case 1: |
| 57 | if oddRow { |
| 58 | return row |
| 59 | } else { |
| 60 | return col |
| 61 | } |
| 62 | case 2: |
| 63 | return shp.Offset([]int{row, col}) |
| 64 | case 3: |
| 65 | if oddRow { |
| 66 | ny := shp.DimSize(1) |
| 67 | yy := row / ny |
| 68 | y := row % ny |
| 69 | return shp.Offset([]int{yy, y, col}) |
| 70 | } else { |
| 71 | nx := shp.DimSize(2) |
| 72 | xx := col / nx |
| 73 | x := col % nx |
| 74 | return shp.Offset([]int{xx, row, x}) |
| 75 | } |
| 76 | case 4: |
| 77 | ny := shp.DimSize(2) |
| 78 | yy := row / ny |
| 79 | y := row % ny |
| 80 | nx := shp.DimSize(3) |
| 81 | xx := col / nx |
| 82 | x := col % nx |
| 83 | return shp.Offset([]int{yy, xx, y, x}) |
| 84 | case 5: |
| 85 | // todo: oddRows version! |
| 86 | nyy := shp.DimSize(1) |
| 87 | ny := shp.DimSize(3) |
| 88 | yyy := row / (nyy * ny) |
| 89 | yy := row % (nyy * ny) |
| 90 | y := yy % ny |
| 91 | yy = yy / ny |
| 92 | nx := shp.DimSize(4) |
| 93 | xx := col / nx |
| 94 | x := col % nx |
| 95 | return shp.Offset([]int{yyy, yy, xx, y, x}) |
| 96 | } |
| 97 | return 0 |
| 98 | } |
| 99 | |
| 100 | // Projection2DCoords returns the corresponding full-dimensional coordinates |
| 101 | // that go into the given row, col coords for a 2D projection of the given tensor, |
no test coverage detected