diff 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
line wrap: on
line diff
--- a/src/LazyTensors/tuple_manipulation.jl	Tue Mar 22 09:53:26 2022 +0100
+++ b/src/LazyTensors/tuple_manipulation.jl	Tue Mar 22 10:42:19 2022 +0100
@@ -74,3 +74,23 @@
 flatten_tuple(t::NTuple{N, Number} where N) = t
 flatten_tuple(t::Tuple) = ((flatten_tuple.(t)...)...,) # simplify?
 flatten_tuple(ts::Vararg) = flatten_tuple(ts)
+
+
+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
+
+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
+