SciPy linalg.solveh_banded() Function



The linalg.solveh_banded() function is a matrix with nonzero elements confined to diagonal bands above and below the main diagonal. The upper bandwidth denotes the number of non zero diagonals above the main diagonals above the main diagonal, while the lower bandwidth denotes the number of nonzero diagonals below the main diagonal.

To solve the equation ax = b for x, assuming a is a Hermitian positive-definite banded matrix.

The matrix a is sorted in ab either in lower diagonal or upper diagonal ordered form.

ab[u + i - j, j] == a[i,j] (if upper form; i <= j) ab[ i - j, j] == a[i,j] (if lower form; i >= j)

Example of ab(shape of a is (6,6), u=2):

upper form:
*   *   a02 a13 a24 a35
*   a01 a12 a23 a34 a45
a00 a11 a22 a33 a44 a55

lower form:
a00 a11 a22 a33 a44 a55
a10 a21 a32 a43 a54 *
a20 a31 a42 a53 *   *

Syntax

Following is the syntax for the scipy.linalg.solveh_banded() function.

scipy.linalg.solveh_banded(ab, b, overwrite_ab=False, overwrite_b=False, lower=False, check_finite=True)

Parameters

The parameters for the scipy.linalg.solveh_banded() function are listed below −

  • ab:(u+1,M) This parameter accepts an array that represents a banded matrix.

  • b: This parameter accepts an array as input, representing the constants of the equation, i.e., the right-hand side of the equation.

  • overwrite_ab(): This parameter accepts a Boolean data type that discards data in ab to improve performance. The default value is False.

  • overwrite_b(): This parameter accepts a Boolean data type that discards data in b to improve performance. The default value is False.

  • lower: This parameter accepts a Boolean data type. If True, it applies only when the data type. If True, it applies only when the data is contained in the lower triangle of a. The default is to use the upper triangle.

  • check_finite():This parameter accepts a Boolean datatype used to check whether the input matrix contains only finite numbers. Disabling this parameter increases performance,, but when the input matrix has infinities or NaNs, it can result in issues such as crashes or non-termination. The default value is True.

Example 1

This example code passes matrices in Hermitian form to linalg.solveh_banded(), which returns an array that is the solution of the equations. The array that is the solution of the equations. The array ab contains the main diagonal and non-zero diagonals below the main diagonal, utilizing the lower form. This code solves a banded symmetric positive-definite linear system and prints the solution vector x.

import numpy as np
from scipy.linalg import solveh_banded

ab = np.array([[ 4,  5,  6,  7, 8, 9],
               [ 2,  2,  2,  2, 2, 0],
               [-1, -1, -1, -1, 0, 0]])
b = np.array([1, 2, 2, 3, 3, 3])
x = solveh_banded(ab, b, lower=True)
print(x)

Output

The result is generated as follows −

[0.03431373, 0.45938375, 0.05602241, 0.47759104, 0.17577031,
       0.34733894]

Example 2

In this example code, solve the Hermitian banded system H x = b, where:

    [ 8   2-1j   0     0  ]        [ 1  ]
H = [2+1j  5     1j    0  ]    b = [1+1j]
    [ 0   -1j    9   -2-1j]        [1-2j]
    [ 0    0   -2+1j   6  ]        [ 0  ]

In the code below, we place the upper diagonal in the array hb. This code solves a banded Hermitian positive-definite linear system using solveh_banded, where hb is the banded matrix and b is the right-handed side vector. The solution x is then printed.

import numpy as np
from scipy.linalg import solveh_banded

hb = np.array([[0, 2-1j, 1j, -2-1j],
               [8,  5,    9,   6  ]])
b = np.array([1, 1+1j, 1-2j, 0])
x = solveh_banded(hb, b)
print(x)

Output

The code is generated as follows −

[ 0.07318536-0.02939412j  0.11877624+0.17696461j  0.10077984-0.23035393j
 -0.00479904-0.09358128j]

Example 3

In this example code, we pass matrices and the check_finite parameter. The check_finite

import numpy as np
from scipy.linalg import solveh_banded
ab = np.array([[ 4,  5,  np.nan,  7, 8, 9],
               [ 2,  2,  2,  2, 2, 0],
               [-1, -1, -1, -1, 0, 0]])
b = np.array([1, 2, 2, 3, 3, 3])
x = solveh_banded(ab, b, lower=True,check_finite=True)
print(x)

Output

The output is obtained as follows −

Traceback (most recent call last):
  File "/home/main.py", line 7, in 
    x = solveh_banded(ab, b, lower=True,check_finite=True)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/linalg/_basic.py", line 573, in solveh_banded
    a1 = _asarray_validated(ab, check_finite=check_finite)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/_lib/_util.py", line 240, in _asarray_validated
    a = toarray(a)
        ^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 630, in asarray_chkfinite
    raise ValueError(
ValueError: array must not contain infs or NaNs

parent_file.htm
Advertisements