A matrix partitioning approach to the calculation of intermolecular potentials. General theory and some examples