def buildsparse(connectivity):
    """
    Build a sparse matrix as a dictionary:
    smat[(i,j)] holds matrix with row-column pair (i,j).
    smat is returned.
    
    Input is the connectivity array associated with finite
    element grids. connectivity(e,r) gives the global matrix
    degree of freedom (i or j) corresponding to a local degree
    of freedom r in an element e.
    """
    smat = {}
    nel = connectivity.shape[0]  # no of elements
    nne = connectivity.shape[1]  # no of nodes in an element
    for e in range(nel):
        for r in range(nne):
            for s in range(r+1):
                i = connectivity[e, r]
                j = connectivity[e, s]
                smat[(i,j)] = 0.0   # add (i,j) pair
                smat[(j,i)] = 0.0   # matrix pattern is symmetric
    return smat