added functions for sparse matrices