Return the lower and upper bandwidth of a 2D numeric array.


Input array of size (N, M)


2-tuple of ints indicating the lower and upper bandwidth. A zero denotes no sub- or super-diagonal on that side (triangular), and, say for N rows (N-1) means that side is full. Same example applies to the upper triangular part with (M-1).


If the dtype of the array is not supported, in particular, NumPy float16, float128 and complex256 dtypes.


This helper function simply runs over the array looking for the nonzero entries whether there exists a banded structure in the array or not. Hence, the performance depends on the density of nonzero entries and also memory-layout. Fortran- or C- contiguous arrays are handled best and otherwise suffers from extra random memory access cost.

The strategy is to look for only untested band elements in the upper and lower triangular parts separately; depending on the memory layout we scan row-wise or column-wise. Moreover, say we are scanning rows and in the 6th row, 4th entry is nonzero then, on the succeeding rows the horizontal search is done only up to that band entries since we know that band is occupied. Therefore, a completely dense matrix scan cost is in the order of n.


>>> import numpy as np
>>> from scipy.linalg import bandwidth
>>> A = np.array([[3., 0., 0., 0., 0.],
...               [0., 4., 0., 0., 0.],
...               [0., 0., 5., 1., 0.],
...               [8., 0., 0., 6., 2.],
...               [0., 9., 0., 0., 7.]])
>>> bandwidth(A)
(3, 1)