def poisson2d(N, dtype='d', format=None):
 if (N == 1):
  diags = asarray([[4]], dtype=dtype)
  return dia_matrix((diags, [0]), shape=(1, 1)).asformat(format)
 offsets = array([0, (- N), N, (-1), 1])
 diags = empty((5, (N ** 2)), dtype=dtype)
 diags[0] = 4
 diags[1:] = (-1)
 diags[3, (N - 1)::N] = 0
 diags[4, N::N] = 0
 return dia_matrix((diags, offsets), shape=((N ** 2), (N ** 2))).asformat(format)

def poisson2d ( N , dtype = 'd' , format = None ) : if ( N == 1 ) : diags = asarray ( [ [ 4 ] ] , dtype = dtype ) return dia_matrix ( ( diags , [ 0 ] ) , shape = ( 1 , 1 ) ) . asformat ( format ) offsets = array ( [ 0 , ( - N ) , N , ( - 1 ) , 1 ] ) diags = empty ( ( 5 , ( N ** 2 ) ) , dtype = dtype ) diags [ 0 ] = 4 diags [ 1 : ] = ( - 1 ) diags [ 3 , ( N - 1 ) : : N ] = 0 diags [ 4 , N : : N ] = 0 return dia_matrix ( ( diags , offsets ) , shape = ( ( N ** 2 ) , ( N ** 2 ) ) ) . asformat ( format )
return a sparse matrix for the 2d poisson problem with standard 5-point finite difference stencil on a square n-by-n grid .