title: Buildin a sparse 2D laplacian matrix using SciPy modules
body: <p>I need to construct the 2D laplacian which looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/JgEAr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JgEAr.png" alt="2D_Laplacian"></a></p>
  
  <p>, where </p>
  
  <p><a href="https://i.stack.imgur.com/m8pLS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m8pLS.png" alt="enter image description here"></a></p>
  
  <p>, and I is the identity matrix. So far, I have done it using the <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.diags.html#scipy.sparse.diags" rel="nofollow noreferrer">diags method of scipy</a>, but I wonder whether there is a smarter way to do it using the <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.block_diag.html#scipy.sparse.block_diag" rel="nofollow noreferrer">block_diag method</a>. Has anyone tried to build the 2D laplacian with this method?</p>
  
  <p>My current method to create this is by this function:</p>
  
  <pre><code>from scipy.sparse import diags
  # Defining the size of the matrix
  nx = 3
  ny = 3
  N  = nx*ny
  main_diag = [-4.0 for i in xrange(N)]
  side_diag = []
  for i in xrange(1,N):
      if i%4 == 0:
          side_diag.append(0)
      else:
          side_diag.append(1)
  up_down_diag = [1.0 for i in xrange(N-4)]
  diagonals = [main_diag,side_diag,side_diag,up_down_diag,up_down_diag]
  laplacian = diags(diagonals, [0, -1, 1,nx,-nx], format="csr")
  print laplacian.toarray()
  </code></pre>