Matrix formulation of direct perturbation theory of relativistic effects in a kinetically balanced basis