Computing the scattering matrix of a device