  We develop a formalism and a computational method to study polarons in
insulators and semi-conductors from first principles. Unlike in standard
calculations requiring large supercells, we solve a secular equation involving
phonons and electron-phonon matrix elements from density-functional
perturbation theory, in a spirit similar to the Bethe-Salpeter equation for
excitons. We show that our approach describes seamlessly large and small
polarons, and we illustrate its capability by calculating wavefunctions,
formation energies, and spectral decomposition of polarons in LiF and Li2O2.
