Skip to main content
`Ctrl`+`K`
scipy.linalg.

# bandwidth

scipy.linalg.bandwidth(a)

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

  The documentation is written assuming array arguments are of specified “core” shapes. However, array argument(s) of this function may have additional “batch” dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see Batched Linear Operations for details. Note that calls with zero-size batches are unsupported and will raise a `ValueError`.

  Parameters:
    a ndarray

      Input array of size (N, M)

  Returns:
    lu tuple

      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).

  Raises:
    TypeError

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

  Notes

  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.

  Examples

      >>> 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)