title: How to sum elements of the rows of a lattice periodically
body: <p>Suppose I have a lattice </p>
  
  <pre><code>a = np.array([[1, 1, 1, 1],
                [2, 2, 2, 2],
                [3, 3, 3, 3],
                [4, 4, 4, 4]])
  </code></pre>
  
  <p>I'd like to make a function <code>func(lattice, start, end)</code> that takes in 3 inputs, where start and end are the index of rows for which the function would sum the elements. For example, for <code>func(a,1,3)</code> it'll sum all the elements of those rows such that <code>func(a,1,3) = 2+2+2+2+3+3+3+3+4+4+4+4 = 36</code>.</p>
  
  <p>Now I know this can be done easily with slicing and <code>np.sum()</code> whatever. But crucially what I want <code>func</code> to do is to also have the ability to wrap around. Namely <code>func(a,2,4)</code> should return <code>3+3+3+3+4+4+4+4+1+1+1+1</code>.
  Couple more examples would be</p>
  
  <pre><code>func(a,3,4) = 4+4+4+4+1+1+1+1
  func(a,3,5) = 4+4+4+4+1+1+1+1+2+2+2+2
  func(a,0,1) = 1+1+1+1+2+2+2+2
  </code></pre>
  
  <p>In my situation I'm never gonna get to a point where it'll sum the whole thing again i.e.</p>
  
  <pre><code>func(a,3,6) = sum of all elements
  </code></pre>
  
  <p><strong>Update</strong>: 
  For my algorithm</p>
  
  <pre><code>for i in range(MC_STEPS_NODE):
      sweep(lattice, prob, start_index_master, end_index_master,
                        rows_for_master) 
      # calculate the energy
      Ene = subhamiltonian(lattice, start_index_master, end_index_master)  
      # calculate the magnetisation
      Mag = mag(lattice, start_index_master, end_index_master)
      E1 += Ene
      M1 += Mag
      E2 += Ene*Ene
      M2 += Mag*Mag
  
          if i % sites_for_master == 0:
              comm.Send([lattice[start_index_master:start_index_master+1], L, MPI.INT],
                                dest=(rank-1)%size, tag=4)
              comm.Recv([lattice[end_index_master:end_index_master+1], L, MPI.INT],
                                source = (rank+1)%size, tag=4)
              start_index_master = (start_index_master + 1)
              end_index_master = (end_index_master + 1)
  
              if start_index_master &gt; 100:
                  start_index_master = start_index_master % L
  
              if end_index_master &gt; 100:
                  end_index_master = end_index_master % L
  </code></pre>
  
  <p>The function I want is the <code>mag()</code> function which calculates the magnetisation of a sublattice which are just sum of all its elements. Imagine a <code>LxL</code> lattice split  up into two sublattices, one belongs to the master and the other to the worker. Each <code>sweep</code> sweeps the corresponding sublattice of <code>lattice</code> with <code>start_index_master</code> and <code>end_index_master</code> determining the start and end row of the sublattice. For every <code>i%sites_for_master = 0</code>, the indices move down by adding <code>1</code>, eventually mod by 100 to prevent memory overflow in mpi4py. So you can imagine if the sublattice is at the centre of the main lattice then <code>start_index_master &lt; end_index_master</code>. Eventually the sublattice will keep moving down to the point where <code>start_index_master &lt; end_index_master</code> where <code>end_index_master &gt; L</code>, so in this case if <code>start_index_master = 10</code> for a lattice <code>L=10</code>, the most bottom row of the sublattice is the first row (<code>[0]</code>) of the main lattice.</p>
  
  <p><strong>Energy function</strong>:</p>
  
  <pre><code>def subhamiltonian(lattice: np.ndarray, col_len_start: int,
                     col_len_end: int) -&gt; float:
  
      energy = 0
      for i in range(col_len_start, col_len_end+1):
          for j in range(len(lattice)):
              spin = lattice[i%L, j]
              nb_sum = lattice[(i%L+1) % L, j] + lattice[i%L, (j+1) % L] + \
                       lattice[(i%L-1) % L, j] + lattice[i%L, (j-1) % L]
              energy += -nb_sum*spin
  
      return energy/4.
  </code></pre>
  
  <p>This is my function for computing the energy of the sublattice.</p>