Computationally efficient gradients for relaxation matrix-based structure refinement including the accommodation of internal motions