Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/tuple_manipulation.jl @ 1221:b3b4d29b46c3 refactor/grids
Merge default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 10 Feb 2023 08:36:56 +0100 |
parents | 07c213167f7c |
children | 5bfb182e24dc |
comparison
equal
deleted
inserted
replaced
1220:93bba649aea2 | 1221:b3b4d29b46c3 |
---|---|
72 Takes a nested tuple and flattens the whole structure | 72 Takes a nested tuple and flattens the whole structure |
73 """ | 73 """ |
74 flatten_tuple(t::NTuple{N, Number} where N) = t | 74 flatten_tuple(t::NTuple{N, Number} where N) = t |
75 flatten_tuple(t::Tuple) = ((flatten_tuple.(t)...)...,) # simplify? | 75 flatten_tuple(t::Tuple) = ((flatten_tuple.(t)...)...,) # simplify? |
76 flatten_tuple(ts::Vararg) = flatten_tuple(ts) | 76 flatten_tuple(ts::Vararg) = flatten_tuple(ts) |
77 | |
78 """ | |
79 left_pad_tuple(t, val, N) | |
80 | |
81 Left pad the tuple `t` to length `N` using the value `val`. | |
82 """ | |
83 function left_pad_tuple(t, val, N) | |
84 if N < length(t) | |
85 throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements")) | |
86 end | |
87 | |
88 padding = ntuple(i->val, N-length(t)) | |
89 return (padding..., t...) | |
90 end | |
91 | |
92 """ | |
93 right_pad_tuple(t, val, N) | |
94 | |
95 Right pad the tuple `t` to length `N` using the value `val`. | |
96 """ | |
97 function right_pad_tuple(t, val, N) | |
98 if N < length(t) | |
99 throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements")) | |
100 end | |
101 | |
102 padding = ntuple(i->val, N-length(t)) | |
103 return (t..., padding...) | |
104 end | |
105 |