  Finding reliably and efficiently the spectrum of the resonant states of an
optical system under varying parameters of the medium surrounding it is a
technologically important task, primarily due to various sensing applications.
Computationally, it presents, however, a fundamental challenge owing to the
nature of the eigenstates of an open system lacking completeness outside it. We
solve this challenge by making a linear transformation of Maxwell's equations
which maps perturbations of the surrounding medium onto effective perturbations
within the system where the resonant states are complete. By treating such
perturbations with the rigorous resonant state expansion, we find the modified
modes of the system for arbitrary perturbations of the medium with any required
accuracy. Numerically efficient single and few mode approximations are shown to
be precise in practically important cases of, respectively, plasmonic
nanoparticles and dielectric micro resonators.
