Automatic differentiation of algorithms : theory, implementation, and application