Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/tuple_manipulation.jl @ 1229:8f4259fbd39c refactor/LazyTensors/tuple_manipulation
Simplify split_index
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sun, 19 Feb 2023 11:43:29 +0100 |
parents | 73f262a0a384 |
children | 723a583cef96 |
comparison
equal
deleted
inserted
replaced
1228:73f262a0a384 | 1229:8f4259fbd39c |
---|---|
1 """ | 1 """ |
2 split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...) | 2 split_index(dim_before, dim_view, dim_index, dim_after, I...) |
3 | 3 |
4 Splits the multi-index `I` into two parts. One part which is expected to be | 4 Splits the multi-index `I` into two parts. One part which is expected to be |
5 used as a view, and one which is expected to be used as an index. | 5 used as a view, and one which is expected to be used as an index. |
6 Eg. | 6 Eg. |
7 ``` | 7 ``` |
8 split_index(Val(1),Val(3),Val(2),Val(1),(1,2,3,4)) -> (1,:,:,:,4), (2,3) | 8 split_index(1, 3, 2, 1, (1,2,3,4)) -> (1,:,:,:,4), (2,3) |
9 ``` | 9 ``` |
10 | 10 |
11 `dim_view` controls how many colons are in the view, and `dim_index` controls | 11 `dim_view` controls how many colons are in the view, and `dim_index` controls |
12 how many elements are extracted from the middle. | 12 how many elements are extracted from the middle. |
13 `dim_before` and `dim_after` decides the length of the index parts before and after the colons in the view index. | 13 `dim_before` and `dim_after` decides the length of the index parts before and after the colons in the view index. |
16 | 16 |
17 The returned values satisfy | 17 The returned values satisfy |
18 * `length(view_index) == dim_before + dim_view + dim_after` | 18 * `length(view_index) == dim_before + dim_view + dim_after` |
19 * `length(I_middle) == dim_index` | 19 * `length(I_middle) == dim_index` |
20 """ | 20 """ |
21 function split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...) where {dim_before,dim_view, dim_index,dim_after} | 21 function split_index(dim_before, dim_view, dim_index, dim_after, I...) |
22 I_before, I_middle, I_after = @inline split_tuple(I, (dim_before, dim_index, dim_after)) | 22 @inline |
23 I_before, I_middle, I_after = split_tuple(I, (dim_before, dim_index, dim_after)) | |
23 | 24 |
24 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...) | 25 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...) |
25 | 26 |
26 return view_index, I_middle | 27 return view_index, I_middle |
27 end | 28 end |