Reverse-mode automatic differentiation for sparse Hessians