def cosine(x):
    '''
    cosine(x) is equivalent to cos(x) except that it also works on sparse arrays.
    '''
    # cos(0) = 1 so no point in keeping these sparse
    if sps.issparse(x): x = x.toarray(x)
    return np.cos(x)