stack#

t3toolbox.backend.stacking.stack(T, axes)#
def stack(
        T,                              # array-like tree: nested tuples of arrays, one per stack element
        axes: typ.Sequence[int],        # len=num stacking levels; target array axis for each stack level
):

Stack array-like nested tree structure.

Examples

>>> import numpy as np
>>> import t3toolbox.backend.stacking as stacking
>>> randn = np.random.randn
>>> a00, a01, a10, a11 = randn(3), randn(3), randn(3), randn(3)
>>> b00, b01, b10, b11 = randn(4,5), randn(4,5), randn(4,5), randn(4,5)
>>> c00, c01, c10, c11 = randn(), randn(), randn(), randn()
>>> T00 = (a00, (b00, c00))
>>> T01 = (a01, (b01, c01))
>>> T10 = (a10, (b10, c10))
>>> T11 = (a11, (b11, c11))
>>> T = ((T00, T01), (T10, T11))
>>> (a, (b, c)) = stacking.stack(T, axes=(0,1))
>>> float(np.linalg.norm(a - np.array([[a00, a01], [a10, a11]])))
0.0
>>> float(np.linalg.norm(b - np.array([[b00, b01], [b10, b11]])))
0.0
>>> float(np.linalg.norm(c - np.array([[c00, c01], [c10, c11]])))
0.0

Stacking along different axes

>>> import numpy as np
>>> import t3toolbox.backend.stacking as stacking
>>> randn = np.random.randn
>>> a00, a01, a10, a11 = randn(3,2,1), randn(3,2,1), randn(3,2,1), randn(3,2,1)
>>> b00, b01, b10, b11 = randn(4,5,6,9), randn(4,5,6,9), randn(4,5,6,9), randn(4,5,6,9)
>>> c00, c01, c10, c11 = randn(7,8), randn(7,8), randn(7,8), randn(7,8)
>>> T00 = (a00, (b00, c00))
>>> T01 = (a01, (b01, c01))
>>> T10 = (a10, (b10, c10))
>>> T11 = (a11, (b11, c11))
>>> T = ((T00, T01), (T10, T11))
>>> (a, (b, c)) = stacking.stack(T, axes=(1,2))
>>> float(np.linalg.norm(a - np.moveaxis(np.array([[a00, a01], [a10, a11]]), 2, 0)))
0.0
>>> float(np.linalg.norm(b - np.moveaxis(np.array([[b00, b01], [b10, b11]]), 2, 0)))
0.0
>>> float(np.linalg.norm(c - np.moveaxis(np.array([[c00, c01], [c10, c11]]), 2, 0)))
0.0

Stacking when there is only one, non-nested, object

>>> import numpy as np
>>> import t3toolbox.backend.stacking as stacking
>>> randn = np.random.randn
>>> a, b, c = randn(3), randn(4,5), randn()
>>> T = (a, (b, c))
>>> (a2, (b2, c2)) = stacking.stack(T, ())
>>> print(np.linalg.norm(a - a2))
0.0
>>> print(np.linalg.norm(b - b2))
0.0
>>> print(np.linalg.norm(c - c2))
0.0

Stack non-nested single array

>>> import numpy as np
>>> import t3toolbox.backend.stacking as stacking
>>> randn = np.random.randn
>>> T = randn(3)
>>> LS = None
>>> T2 = stacking.stack(T, ())
>>> print(np.linalg.norm(T - T2))
0.0

Stack nothing

>>> import numpy as np
>>> import t3toolbox.backend.stacking as stacking
>>> randn = np.random.randn
>>> T = ()
>>> LS = ()
>>> print(stacking.stack(T, ()))
()
Parameters:

axes (t3toolbox.backend.common.typ.Sequence[int])