A general formulation of reweighted least squares fitting