Update.
authorFrançois Fleuret <francois@fleuret.org>
Wed, 24 May 2023 15:30:54 +0000 (17:30 +0200)
committerFrançois Fleuret <francois@fleuret.org>
Wed, 24 May 2023 15:30:54 +0000 (17:30 +0200)
eingather.py

index b271100..588aacb 100755 (executable)
@@ -19,40 +19,27 @@ def eingather(op, src, *indexes):
     s_all = "".join([s_src] + s_indexes)
     shape = tuple(all_sizes[s_all.index(v)] for v in s_dst)
 
-    idx = []
-    n_index = 0
-
-    for i in range(src.dim()):
-        v = s_src[i]
-        if v == "_":
-            index, s_index = indexes[n_index], s_indexes[n_index]
-            n_index += 1
-
-            sub_idx = []
-
-            for i in range(index.dim()):
-                v = s_index[i]
+    def do(x,s_x):
+        idx = []
+        n_index = 0
+
+        for i in range(x.dim()):
+            v = s_x[i]
+            if v == "_":
+                idx.append(do(indexes[n_index], s_indexes[n_index]))
+                n_index += 1
+            else:
                 j = s_dst.index(v)
                 a = (
-                    torch.arange(index.size(i))
+                    torch.arange(x.size(i))
                     .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
                     .expand(shape)
                 )
-                sub_idx.append(a)
-
-            index = index[sub_idx]
-            idx.append(index)
-        else:
-            j = s_dst.index(v)
-            a = (
-                torch.arange(src.size(i))
-                .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
-                .expand(shape)
-            )
-            idx.append(a)
+                idx.append(a)
 
-    return src[idx]
+        return x[idx]
 
+    return do(src,s_src)
 
 #######################