Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/tuple_manipulation.jl @ 997:20c376dffe84 refactor/lazy_tensors
Move tuple functions to their own file
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 18 Mar 2022 21:26:02 +0100 |
parents | |
children | 0905cec43d2e |
comparison
equal
deleted
inserted
replaced
996:aa72f067e771 | 997:20c376dffe84 |
---|---|
1 """ | |
2 split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...) | |
3 | |
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. | |
6 Eg. | |
7 ``` | |
8 split_index(Val(1),Val(3),Val(2),Val(1),(1,2,3,4)) -> (1,:,:,:,4), (2,3) | |
9 ``` | |
10 | |
11 `dim_view` controls how many colons are in the view, and `dim_index` controls | |
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. | |
14 | |
15 Arguments should satisfy `length(I) == dim_before+B_domain+dim_after`. | |
16 | |
17 The returned values satisfy | |
18 * `length(view_index) == dim_before + dim_view + dim_after` | |
19 * `length(I_middle) == dim_index` | |
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} | |
22 I_before, I_middle, I_after = split_tuple(I, Val(dim_before), Val(dim_index)) | |
23 | |
24 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...) | |
25 | |
26 return view_index, I_middle | |
27 end | |
28 | |
29 # TODO: Can this be replaced by something more elegant while still being type stable? 2020-10-21 | |
30 # See: | |
31 # https://github.com/JuliaLang/julia/issues/34884 | |
32 # https://github.com/JuliaLang/julia/issues/30386 | |
33 """ | |
34 slice_tuple(t, Val(l), Val(u)) | |
35 | |
36 Get a slice of a tuple in a type stable way. | |
37 Equivalent to `t[l:u]` but type stable. | |
38 """ | |
39 function slice_tuple(t,::Val{L},::Val{U}) where {L,U} | |
40 return ntuple(i->t[i+L-1], U-L+1) | |
41 end | |
42 | |
43 """ | |
44 split_tuple(t::Tuple{...}, ::Val{M}) where {N,M} | |
45 | |
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 | |
67 | |
68 | |
69 """ | |
70 flatten_tuple(t) | |
71 | |
72 Takes a nested tuple and flattens the whole structure | |
73 """ | |
74 flatten_tuple(t::NTuple{N, Number} where N) = t | |
75 flatten_tuple(t::Tuple) = ((flatten_tuple.(t)...)...,) # simplify? | |
76 flatten_tuple(ts::Vararg) = flatten_tuple(ts) |