SOLVE_BANDED
Solves a linear system where the coefficient matrix A is banded. Banded matrices only have non-zero elements in a few diagonals around the main diagonal, which allows for significantly reduced storage and computation time. The matrix must be provided in the ‘ab’ format used by LAPACK.
Excel Usage
=SOLVE_BANDED(l_diags, u_diags, ab_matrix, b_vector)
l_diags(int, required): Number of non-zero lower diagonals.u_diags(int, required): Number of non-zero upper diagonals.ab_matrix(list[list], required): The banded matrix in ‘ab’ format (shape is l + u + 1 by M).b_vector(list[list], required): 2D array representing the right-hand side.
Returns (list[list]): 2D array representing the solution x.
Example 1: Solve diagonal matrix (special case of banded)
Inputs:
| l_diags | u_diags | ab_matrix | b_vector | |
|---|---|---|---|---|
| 0 | 0 | 2 | 3 | 4 |
| 9 |
Excel formula:
=SOLVE_BANDED(0, 0, {2,3}, {4;9})
Expected output:
| Result |
|---|
| 2 |
| 3 |
Example 2: Solve tridiagonal system with identity-like right-hand side
Inputs:
| l_diags | u_diags | ab_matrix | b_vector | ||
|---|---|---|---|---|---|
| 1 | 1 | 0 | -1 | -1 | 1 |
| 2 | 2 | 2 | 0 | ||
| -1 | -1 | 0 | 1 |
Excel formula:
=SOLVE_BANDED(1, 1, {0,-1,-1;2,2,2;-1,-1,0}, {1;0;1})
Expected output:
| Result |
|---|
| 1 |
| 1 |
| 1 |
Example 3: Solve banded system (1 lower, 1 upper)
Inputs:
| l_diags | u_diags | ab_matrix | b_vector | ||
|---|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 1 | 1 |
| 2 | 2 | 2 | 1 | ||
| 1 | 1 | 0 | 1 |
Excel formula:
=SOLVE_BANDED(1, 1, {0,1,1;2,2,2;1,1,0}, {1;1;1})
Expected output:
| Result |
|---|
| 0.5 |
| 0 |
| 0.5 |
Example 4: Solve upper-banded diagonal case with two right-hand sides
Inputs:
| l_diags | u_diags | ab_matrix | b_vector | ||
|---|---|---|---|---|---|
| 0 | 0 | 4 | 5 | 8 | 4 |
| 10 | 15 |
Excel formula:
=SOLVE_BANDED(0, 0, {4,5}, {8,4;10,15})
Expected output:
| Result | |
|---|---|
| 2 | 1 |
| 2 | 3 |
Python Code
import numpy as np
from scipy.linalg import solve_banded as scipy_solve_banded
def solve_banded(l_diags, u_diags, ab_matrix, b_vector):
"""
Solve the equation Ax = b for x, assuming A is a banded matrix.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve_banded.html
This example function is provided as-is without any representation of accuracy.
Args:
l_diags (int): Number of non-zero lower diagonals.
u_diags (int): Number of non-zero upper diagonals.
ab_matrix (list[list]): The banded matrix in 'ab' format (shape is l + u + 1 by M).
b_vector (list[list]): 2D array representing the right-hand side.
Returns:
list[list]: 2D array representing the solution x.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
ab_matrix = to2d(ab_matrix)
b_vector = to2d(b_vector)
try:
ab = np.array(ab_matrix, dtype=float)
b = np.array(b_vector, dtype=float)
except (ValueError, TypeError):
return "Error: matrix entries must contain numeric values"
try:
x = scipy_solve_banded((int(l_diags), int(u_diags)), ab, b)
except Exception as e:
return f"Error: {str(e)}"
return x.tolist()
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Number of non-zero lower diagonals.
Number of non-zero upper diagonals.
The banded matrix in 'ab' format (shape is l + u + 1 by M).
2D array representing the right-hand side.