SciPy - bandwidth() Function



The SciPy's bandwidth() function is used to calculate the lower (l) and upper (u) bandwidth of a 2D numeric array.

The term Matrix Bandwidth denotes the spread of non-zero elements around the main diagonal of the matrix. Matrices with small bandwidth are known as banded matrices.

The terms l (lower bandwidth) and u (upper bandwidth) indicate how many diagonals are non-zero below and above the main diagonal. Together, l and u define the matrix's total bandwidth, which indicates how far the non-zero values stretch from the main diagonal in both directions.

This method raises Type error if the data type is unsupported for example − float16, float128 or complex256. To resolve this you can convert the matrix into supported data types such as float32, float64, int32, and int64.

The bandwidth() method is useful because knowing the smaller bandwidth of a matrix indicates faster computations and memory-efficient storage as it shows that most of the important entries of the matrix are clustered close to the diagonal.

Syntax

The syntax for the Scipy bandwidth() method is as follows −

.bandwidth(a)

Parameters

This method accepts only a single parameter −

  • a (ndarray) − A 2D array-like or NumPy array representing the matrix.

Return Value

This method returns a tuple containing two integers representing lower bandwidth(l) − diagonals below the main diagonal and upper bandwidth(u) − diagonals above the main diagonal.

Example 1

This is the basic example of how to use the bandwidth() method in SciPy to calculate the lower (l) and upper (u) bandwidths of a 2D square matrix, which represent the distances of the non-zero diagonals from the main diagonal.

The main diagonal in the matrix have values of 1, 4, 7, 10. We obtain 1 as a bandwidth for both lower and upper because the nonzero diagonals in the following code had lengths of one above and below to the main diagonal.

import numpy as np
from scipy import linalg
from scipy.linalg import bandwidth

A = np.array([
    [1, 2, 0, 0],
    [3, 4, 5, 0],
    [0, 6, 7, 8],
    [0, 0, 9, 10]
])
bw = linalg.bandwidth(A)
print("Lower Bandwidth (l), Upper Bandwidth (u):", bw)

When we run above program, it produces following result

Lower Bandwidth (l), Upper Bandwidth (u): (1, 1)

Example 2

The code below calculates the lower and upper bandwidth of matrix C using bandwidth() method. It also proves that it has two non-zero rows below the diagonal with a lower bandwidth of two and no non-zero elements, above the diagonal with an upper bandwidth of zero.

import numpy as np
from scipy.linalg import bandwidth

C = np.array([
    [1., 0., 0.],
    [2., 3., 0.],
    [4., 5., 6.]
])
lu = bandwidth(C)
print(f"Lower Bandwidth: {lu[0]}, Upper Bandwidth: {lu[1]}")

Following is an output of the above code

Lower Bandwidth: 2, Upper Bandwidth: 0

Example 3

In the below example let us see the lower(l) and upper(u) bandwidth() for a upper triangle matrix. In the below code the non-zero elements are above the main diagonal in 2 rows, hence the upper bandwidth is '2' and the lower bandwidth is 0.

import numpy as np
from scipy.linalg import bandwidth

D = np.array([
    [1., 2., 3.],
    [0., 4., 5.],
    [0., 0., 6.]
])
lu = bandwidth(D)
print(f"Lower Bandwidth: {lu[0]}, Upper Bandwidth: {lu[1]}")

Output of the above code is as follows

Lower Bandwidth: 0, Upper Bandwidth: 2

Example 4

The example demonstrates how to convert the matrix to a compatible type, such as float64, in order to accommodate unsupported data types in the bandwidth() method.

import numpy as np
from scipy.linalg import bandwidth

# Matrix with unsupported type
matrix = np.array([[1, 0, 0], [4, 5, 0], [0, 2, 3]], dtype=np.float128)

# Convert to supported type and calculate bandwidth
matrix = matrix.astype(np.float64)
l, u = bandwidth(matrix)

print(matrix)
print("Lower bandwidth:", l)
print("Upper bandwidth:", u)

Output of the above code is as follows

[[1. 0. 0.]
 [4. 5. 0.]
 [0. 2. 3.]]
Lower bandwidth: 1
Upper bandwidth: 0
scipy_linalg.htm
Advertisements