Overview
I am looking for a way to solve a structured linear system in Python without using a for loop (preferably using vectorization, if possible).
Background
Consider the following linear system: \begin{align} \begin{pmatrix} E_0 \\ F_1 & E_1 \\ & F_2 & E_2 \\ && \ddots & \ddots \\ &&& F_{K-1} & E_{K-1} \end{pmatrix} \begin{pmatrix} x_0 \\ x_1 \\ x_2 \\ \vdots \\ x_{K-1} \end{pmatrix} = \begin{pmatrix} b_0 \\ b_1 \\ b_2 \\ \vdots \\ b_{K-1} \end{pmatrix} \end{align} where $E_i, F_i \in \mathbb{R}^{n \times n}$, and $x_i, b_i \in \mathbb{R}^n$ for $i = 0, \ldots, K-1$
Further, the $E_i$ are invertible for $i = 0, \ldots, K-1$.
Then this system can be solved through forward substitution:
Solve $E_0 x_0 = b_0$
for $i = 1, \ldots, K-1$: Solve $E_i x_i = b_i - F_i x_{i-1}$
My Current Implementation
The block matrices $E_i$ and $F_i$ are available by calling Ek(i)
and Fk(i)
.
Currently $x$ and $b$ are shaped as a numpy arrays with shape $K \times n$ so that x[k]
gives $x_k$, and so forth.
import numpy as np
from scipy.sparse.linalg import spsolve
# define K and n, create b and initialize x
x[0] = spsolve(Ek(0), b[0])
for i in range(1, K):
x[i] = spsolve(Ek(i), b[i] - Fk(i) @ x[i-1])
Can this be vectorized? I would like to not use a for loop here since they are quite slow in Python.