Automatic discrete differentiation and its applications