Derivation and efficient implementation of the fast multipole method