diff 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
line wrap: on
line diff
--- a/src/LazyTensors/tuple_manipulation.jl	Tue Nov 01 22:44:00 2022 +0100
+++ b/src/LazyTensors/tuple_manipulation.jl	Fri Feb 10 08:36:56 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
+