Numpy adds unstack in 2.1.0 Bradley Lucier 21 Jul 2024 22:02 UTC

In version 2.1.0 Numpy adds unstack:

https://numpy.org/devdocs/reference/generated/numpy.unstack.html

=============================
numpy.unstack(x, /, *, axis=0)
[source]

Split an array into a sequence of arrays along the given axis.

The axis parameter specifies the dimension along which the array will be
split. For example, if axis=0 (the default) it will be the first
dimension and if axis=-1 it will be the last dimension.

The result is a tuple of arrays split along axis.
=============================

Numpy.unstack undoes numpy.stack, and is a limited version of
array-curry in this SRFI; array-unstack could be defined as follows
(imitating the examples in the numpy documentation):

 > (import (srfi 231))
 > (define (array-unstack A #!optional (d 0))
   (let ((n (array-dimension A)))
     (array->list
      (array-curry
       (array-permute
        A (index-first n d))
       (- n 1)))))
 > (define arr
   (specialized-array-reshape
    (list*->array 1 (iota 24))
    (make-interval '#(2 3 4))))
 > (map array->vector* (array-unstack arr))
(#(#(0 1 2 3)
    #(4 5 6 7)
    #(8 9 10 11))
  #(#(12 13 14 15)
    #(16 17 18 19)
    #(20 21 22 23)))
 > (map array->vector* (array-unstack arr 1))
(#(#(0 1 2 3)
    #(12 13 14 15))
  #(#(4 5 6 7)
    #(16 17 18 19))
  #(#(8 9 10 11)
    #(20 21 22 23)))