Add AutoDiffFunction to factorize function evaluation and differentiation code.