title: back substitution in python
body: <p>I'm supposed to implement a back substitution. We receive the row echelon matrix A, the vector b and the solution vector x has already been initialized.</p>
  
  <p>I have a problem with the indices in my code. No matter how I adjust my range I always seem to fall out right in the beginning and get an out of bounds error. I already tested my solution on paper and it should be doing the right thing, however, I just can't get past the index problem. Does anyone have any clues? </p>
  
  <pre><code>def back_substitution(A: np.ndarray, b: np.ndarray) -&gt; np.ndarray:
      n = len(b)
  
      if A[n-1][n-1] == 0:
          raise ValueError
  
      for i in range(n-1, 0, -1):
          x[i] = A[i][i]/b[i]
          for j in range (i-1, 0, -1):
              A[i][i] += A[j][i]*x[i]
  
      return x
  
  x = np.zeros(1)
  M = np.matrix([[1, -1, 2], [0, -1, -2], [0, 0, -6]])
  c = np.matrix([[0],[0],[3]])
  back_substitution(M,c)
  </code></pre>
  
  <p>I get following error: </p>
  
  <pre><code>Traceback (most recent call last):
    File "main.py", line 167, in &lt;module&gt;
      back_substitution(M,c)
    File "main.py", line 125, in back_substitution
      x[i] = A[i][i]/b[i]
    File "/usr/local/lib/python3.4/dist-packages/numpy/matrixlib/defmatrix.py", line 284, in __getitem__
      out = N.ndarray.__getitem__(self, index)
  IndexError: index 2 is out of bounds for axis 0 with size 1
  </code></pre>