I need to write a Python code without any loops which for a given regular upper triangular matrix R ∈ Rn×n (that is rij = 0 for j > i and rii ̸= 0) and given c ∈ Rn solves the linear system Rx = c using backward substitution and returns the vector x. The backward substitution is defined as: backward substitution This is what I have so far:
def backward(R, c):
n = len(c)
x = np.zeros(n)
x[-1] = c[-1] / R[-1, -1]
i = np.arange(n-2, -1, -1)
j = i+1
x[i] = (c[i] - R[i, j::-1] @ x[j::-1]) / R[i, i]
return x
If I try to test this code, it returns following error message: ---> 10 x[i] = (c[i] - R[i, j::-1] @ x[j::-1]) / R[i, i] TypeError: only integer scalar arrays can be converted to a scalar index
What I am trying to do is to first compute the last entry of the vector x and from then on compute the other elements using the backward substitution formula. But I have trouble accessing the j-indices since in order for this code to properly work, I have to use more than one element in a row. E.g. if I have a 3x3 matrix A, I have to use A11, A12 and A13 to compute x1.
How do I fix that issue?