Path optimization by a variational reaction coordinate method. I. Development of formalism and algorithms.