Mercurial > repos > public > sbplib_julia
diff src/LazyTensors/tuple_manipulation.jl @ 1219:7ee258e5289e
Merge feature/dissipation_operators
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 08 Feb 2023 10:29:06 +0100 |
parents | 07c213167f7c |
children | 5bfb182e24dc |
line wrap: on
line diff
--- a/src/LazyTensors/tuple_manipulation.jl Thu Feb 02 22:53:11 2023 +0100 +++ b/src/LazyTensors/tuple_manipulation.jl Wed Feb 08 10:29:06 2023 +0100 @@ -74,3 +74,32 @@ flatten_tuple(t::NTuple{N, Number} where N) = t flatten_tuple(t::Tuple) = ((flatten_tuple.(t)...)...,) # simplify? flatten_tuple(ts::Vararg) = flatten_tuple(ts) + +""" + left_pad_tuple(t, val, N) + +Left pad the tuple `t` to length `N` using the value `val`. +""" +function left_pad_tuple(t, val, N) + if N < length(t) + throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements")) + end + + padding = ntuple(i->val, N-length(t)) + return (padding..., t...) +end + +""" + right_pad_tuple(t, val, N) + +Right pad the tuple `t` to length `N` using the value `val`. +""" +function right_pad_tuple(t, val, N) + if N < length(t) + throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements")) + end + + padding = ntuple(i->val, N-length(t)) + return (t..., padding...) +end +