comparison src/LazyTensors/tuple_manipulation.jl @ 1232:a8fa8c1137cc refactor/grids

Merge refactor/LazyTensors/tuple_manipulation
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 19 Feb 2023 22:07:57 +0100
parents de6a9635f293
children d7bc11053951
comparison
equal deleted inserted replaced
1222:5f677cd6f0b6 1232:a8fa8c1137cc
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 ```julia-repl
8 split_index(Val(1),Val(3),Val(2),Val(1),(1,2,3,4)) -> (1,:,:,:,4), (2,3) 8 julia> LazyTensors.split_index(1, 3, 2, 1, (1,2,3,4)...)
9 ((1, Colon(), Colon(), Colon(), 4), (2, 3))
9 ``` 10 ```
10 11
11 `dim_view` controls how many colons are in the view, and `dim_index` controls 12 `dim_view` controls how many colons are in the view, and `dim_index` controls
12 how many elements are extracted from the middle. 13 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. 14 `dim_before` and `dim_after` decides the length of the index parts before and after the colons in the view index.
16 17
17 The returned values satisfy 18 The returned values satisfy
18 * `length(view_index) == dim_before + dim_view + dim_after` 19 * `length(view_index) == dim_before + dim_view + dim_after`
19 * `length(I_middle) == dim_index` 20 * `length(I_middle) == dim_index`
20 """ 21 """
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} 22 function split_index(dim_before, dim_view, dim_index, dim_after, I...)
22 I_before, I_middle, I_after = split_tuple(I, Val(dim_before), Val(dim_index)) 23 @inline
24 I_before, I_middle, I_after = split_tuple(I, (dim_before, dim_index, dim_after))
23 25
24 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...) 26 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...)
25 27
26 return view_index, I_middle 28 return view_index, I_middle
27 end 29 end
28 30
29 # TODO: Can this be replaced by something more elegant while still being type stable? 2020-10-21 31
30 # See:
31 # https://github.com/JuliaLang/julia/issues/34884
32 # https://github.com/JuliaLang/julia/issues/30386
33 """ 32 """
34 slice_tuple(t, Val(l), Val(u)) 33 split_tuple(t, szs)
35 34
36 Get a slice of a tuple in a type stable way. 35 Split the tuple `t` into a set of tuples of the sizes given in `szs`.
37 Equivalent to `t[l:u]` but type stable. 36 `sum(szs)` should equal `lenght(t)`.
37
38 E.g
39 ```julia-repl
40 julia> LazyTensors.split_tuple((1,2,3,4,5,6), (3,1,2))
41 ((1, 2, 3), (4,), (5, 6))
42 ```
38 """ 43 """
39 function slice_tuple(t,::Val{L},::Val{U}) where {L,U} 44 function split_tuple(t, szs)
40 return ntuple(i->t[i+L-1], U-L+1) 45 @inline
46 if length(t) != sum(szs; init=0)
47 throw(ArgumentError("length(t) must equal sum(szs)"))
48 end
49
50 rs = sizes_to_ranges(szs)
51 return map(r->t[r], rs)
41 end 52 end
42 53
43 """ 54 function sizes_to_ranges(szs)
44 split_tuple(t::Tuple{...}, ::Val{M}) where {N,M} 55 cum_szs = cumsum((0, szs...))
45 56 return ntuple(i->cum_szs[i]+1:cum_szs[i+1], length(szs))
46 Split the tuple `t` into two parts. the first part is `M` long.
47 E.g
48 ```julia
49 split_tuple((1,2,3,4),Val(3)) -> (1,2,3), (4,)
50 ```
51 """
52 function split_tuple(t::NTuple{N,Any},::Val{M}) where {N,M}
53 return slice_tuple(t,Val(1), Val(M)), slice_tuple(t,Val(M+1), Val(N))
54 end
55
56 """
57 split_tuple(t::Tuple{...},::Val{M},::Val{K}) where {N,M,K}
58
59 Same as `split_tuple(t::NTuple{N},::Val{M})` but splits the tuple in three parts. With the first
60 two parts having lenght `M` and `K`.
61 """
62 function split_tuple(t::NTuple{N,Any},::Val{M},::Val{K}) where {N,M,K}
63 p1, tail = split_tuple(t, Val(M))
64 p2, p3 = split_tuple(tail, Val(K))
65 return p1,p2,p3
66 end 57 end
67 58
68 59
69 """ 60 """
70 flatten_tuple(t) 61 concatenate_tuples(t...)
71 62
72 Takes a nested tuple and flattens the whole structure 63 Concatenate tuples.
73 """ 64 """
74 flatten_tuple(t::NTuple{N, Number} where N) = t 65 concatenate_tuples(t::Tuple,ts::Vararg{Tuple}) = (t..., concatenate_tuples(ts...)...)
75 flatten_tuple(t::Tuple) = ((flatten_tuple.(t)...)...,) # simplify? 66 concatenate_tuples(t::Tuple) = t
76 flatten_tuple(ts::Vararg) = flatten_tuple(ts) 67
77 68
78 """ 69 """
79 left_pad_tuple(t, val, N) 70 left_pad_tuple(t, val, N)
80 71
81 Left pad the tuple `t` to length `N` using the value `val`. 72 Left pad the tuple `t` to length `N` using the value `val`.