Dynamical theory of an atom with two or three levels interacting with quantized cavity fields