pad_or_truncate#

t3toolbox.backend.linalg.pad_or_truncate(array, pad_width, mode='constant', **kwargs)#
def pad_or_truncate(
        array,
        pad_width,
        mode='constant',
        **kwargs
):

Pad and/or truncate an array per axis, using signed (before, after) widths.

pad_width has one (before, after) pair per axis (like numpy.pad()), but a negative width removes that many entries instead of adding them: positive pads (mode controls fill, default zeros), negative truncates. The two compose – an axis can be truncated on one side and padded on the other.

Examples

>>> import numpy as np
>>> import t3toolbox.backend.linalg as linalg
>>> a = np.arange(6).reshape(2, 3)
>>> linalg.pad_or_truncate(a, [(1, 0), (0, 2)]).tolist()   # +1 row before, +2 cols after (zeros)
[[0, 0, 0, 0, 0], [0, 1, 2, 0, 0], [3, 4, 5, 0, 0]]
>>> linalg.pad_or_truncate(a, [(0, -1), (-1, 0)]).tolist()  # drop last row, drop first col
[[1, 2]]