Fix jacobian computation from gradients when sparse matrices are used.