comparison src/LazyTensors/tuple_manipulation.jl @ 1031:0905cec43d2e feature/dissipation_operators

Add left and right pad functions for tuple
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 22 Mar 2022 10:42:19 +0100
parents 20c376dffe84
children 423a6442efc3
comparison
equal deleted inserted replaced
1030:8fe630a20b64 1031:0905cec43d2e
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 function left_pad_tuple(t, val, N)
80 if N < length(t)
81 throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements"))
82 end
83
84 padding = ntuple(i->val, N-length(t))
85 return (padding..., t...)
86 end
87
88 function right_pad_tuple(t, val, N)
89 if N < length(t)
90 throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements"))
91 end
92
93 padding = ntuple(i->val, N-length(t))
94 return (t..., padding...)
95 end
96