gradient_sparse implementation