I'm using numpy to write the "back substitution" method for solving linear system where "A" is a nonsingular upper triangular matrix.
import numpy as np def upperTriSol(A, b): n = np.size(b) x = np.zeros_like(b) x[-1] = 1. / A[-1, -1] * b[-1] for i in xrange(n-2, -1, -1): x[i] = 1. / A[i, i] * (b[i] - np.sum(A[i, i+1:] * x[i+1:])) return x
I know that "for loops" are slow, so I wonder if there is any way to avoid them in this case? If not, is there a "correct" way to write efficient "for loops" in numpy?