Fix BadJacobian for sparse matrices