An O(1) Space Algorithm for N-Dimensional Tensor Rotation: A Generalization of the Reversal Method
Dexin Chen
TL;DR
This paper tackles in-place rotation of N-dimensional tensors with no extra storage beyond a small fixed amount. It generalizes the classic three-reversal trick to N dimensions via the 2^n+1 reversal algorithm, which performs one global reversal followed by 2^n sub-tensor reversals to realize a cyclic shift. The authors provide formal definitions for N-dimensional rotation and reversal, prove correctness using the involutive property Rev^2 = I and block-partitioning, and establish time complexity linear in the number of elements with O(n) auxiliary space. The approach is highly parallelizable, enabling near-linear speedups on multi-processor systems, and holds promise for efficient implementations in HPC and memory-constrained environments.
Abstract
The rotation of multi-dimensional arrays, or tensors, is a fundamental operation in computer science with applications ranging from data processing to scientific computing. While various methods exist, achieving this rotation in-place (i.e., with O(1) auxiliary space) presents a significant algorithmic challenge. The elegant three-reversal algorithm provides a well-known O(1) space solution for one-dimensional arrays. This paper introduces a generalization of this method to N dimensions, resulting in the "$2^n+1$ reversal algorithm". This algorithm achieves in-place tensor rotation with O(1) auxiliary space and a time complexity linear in the number of elements. We provide a formal definition for N-dimensional tensor reversal, present the algorithm with detailed pseudocode, and offer a rigorous proof of its correctness, demonstrating that the pattern observed in one dimension ($2^1+1=3$ reversals) and two dimensions ($2^2+1=5$ reversals) holds for any arbitrary number of dimensions.
