In most algorithms of direct methods, the variables are the normalized structure factors (SF) EH. An alternative set of variables is proposed which provides more flexibility for handling, in a single algorithm, phase relationships and direct-space constraints, as well as the complete set of diffraction data. This set of variables ΨH consists of SF associated with a complex periodic function ψ(r) such that ρ(r) = | ψ(r)|2. The pair of variables {EH, ΨH}, called twin variables, play a crucial role in the subsequent theory. The phase relations are enhanced by using pairs of non-negative `twin determinants' {Dm, D′m+1}, Dm is a classical Karle–Hauptman (K-H) determinant involving E and D′m+1 is generated by bordering Dm with an (m + 1)th row and column containing Ψ. The associated regression equation establishes a relation between E and Ψ. Furthermore, a remarkable expression is obtained for the gradient of the phase given by the classical tangent formula, as well as for the gradients involved in the related formulae pertaining to the Ψ set. The flexibility of the algorithm is illustrated by the ab initio transferring to the Ψ set of the a priori known information (such as the whole set of the observed moduli), before starting the sequential phase determination of the unknown phases. All constraints are included in a global minimization function. Analytical formulae are given for the gradient of this function with respect to the Ψ set of variables. In the final result, the Ψ set is simultaneously compatible in the least-squares sense with the whole set of observed SF and with various other constraints and phase relations. Application to two known structures permitted testing the different parts of the algorithm.