title: Change numpy array inside recursive function
body: <p>Question 4.1-1 from CLRS Introduction to Algorithms</p>
  <p>I have code that multiply n*n matrix, where n is exact power of 2, using recursion. I need to modify this code, so that n doesn't need to be exact power of 2. Obvious way to do it is adding zeroes to matrix so it became k*k, where k is power of 2.<br />
  I try two ways to modify matrix, but it seems that when I try to add zeroes to C, it's only add it to local version of C inside function, but global version remains the same. I don't see ways to using return statement since I have recursion.<br />
  Code works when n is power of 2 but raise error otherwise.</p>
  <pre><code>def matrix_multiply_recursive_aux(A, B, C, n, A_row, A_col, B_row, B_col, C_row, C_col):
      &quot;&quot;&quot;Compute C = C + (A * B), where A, B, and C are n x n matrices.
         Other parameters:
          A_row, A_col: starting row and column for the n x n submatrix of A
          B_row, B_col: starting row and column for the n x n submatrix of B
          C_row, C_col: starting row and column for the n x n submatrix of C
      &quot;&quot;&quot;
      if n == 1:
          C[C_row, C_col] += A[A_row, A_col] * B[B_row, B_col]  # base case
          return
  
  # Code that I add, first version
      if np.log2(n) % 1 != 0:
          k = int(2 ** np.ceil(np.log2(n)))
          for i in range(n, k):
              C = np.append(C, [[0] * n], axis=0)
          for i in range(n, k):
              C = np.column_stack((C, [0] * k))
          n = k
  # end of my first version
  
  # second version
  
  #     if np.log2(n) % 1 != 0:
  #         C_temp = np.zeros((k, k))
  #         for i in range(n):
  #             for j in range(n):
  #                 C_temp[i, j] = C[i, j]
  #         C = C_temp
  #         n = int(2 ** np.ceil(np.log2(n)))
  
  # end of second version
  
      half = n // 2
      matrix_multiply_recursive_aux(A, B, C, half, A_row, A_col, B_row, B_col, C_row, C_col)
      matrix_multiply_recursive_aux(A, B, C, half, A_row, A_col, B_row, B_col + half, C_row, C_col + half)
      matrix_multiply_recursive_aux(A, B, C, half, A_row + half, A_col, B_row, B_col, C_row + half, C_col)
      matrix_multiply_recursive_aux(A, B, C, half, A_row + half, A_col, B_row, B_col + half, C_row + half, C_col + half)
      matrix_multiply_recursive_aux(A, B, C, half, A_row, A_col + half, B_row + half, B_col, C_row, C_col)
      matrix_multiply_recursive_aux(A, B, C, half, A_row, A_col + half, B_row + half, B_col + half, C_row, C_col + half)
      matrix_multiply_recursive_aux(A, B, C, half, A_row + half, A_col + half, B_row + half, B_col, C_row + half, C_col)
      matrix_multiply_recursive_aux(A, B, C, half, A_row + half, A_col + half, B_row + half, B_col + half, C_row + half, C_col + half)
  </code></pre>
  <p>When I run code I get error.</p>
  <pre><code>A = np.array([[1, 2, 0],
                [0, 3, 1],
                [1, 0, 2]])
  B = np.array([[1, 3, 1],
                [1, 0, 0],
                [1, 1, 2]])
  n = 3
  C = np.array([[0, 0, 0], 
                [0, 0, 0], 
                [0, 0, 0]])
  matrix_multiply_recursive_aux(A, B, C, n, 0, 0, 0, 0, 0, 0)
  </code></pre>
  <pre><code>      7     &quot;&quot;&quot;
        8     if n == 1:
  ----&gt; 9         C[C_row, C_col] += A[A_row, A_col] * B[B_row, B_col]  # base case
       10         return
       11 
  
  IndexError: index 3 is out of bounds for axis 1 with size 3
  </code></pre>
  <p>When I comment recursive calls (last eight lines) and try to print only C I get it 3*3 when I expect 4*4.</p>
  <pre><code>matrix_multiply_recursive_aux(A, B, C, n, 0, 0, 0, 0, 0, 0)
  print(C)
  </code></pre>
  <pre><code>[[0 0 0]
   [0 0 0]
   [0 0 0]]
  </code></pre>
  <p>How can I change nonlocal version of array?</p>