@@ -0,0 +1,1861 @@
+%22%22%22Defines subroutines for converting a leastsq closure to minimize closure%0A%0ASince the :py:func:%60scipy.optimize.leastsq%60 seems to be limited in terms of its%0Aperformance, the more generate :py:func:%60scipy.optimize.minimize%60 function can be%0Atried to be used. However, they required different kind of closures for%0Acomputing the residues and the Jacobian. Here some utility functions are%0Aprovided to translate the closures for least square minimization into closures%0Afor general minimizers.%0A%0AThe convention for naming argument is always :math:%60N%60 parameter and :math:%60M%60%0Aconfiguration problem.%0A%0A%22%22%22%0A%0Aimport numpy as np%0A%0A%0Adef conv_residue(residue_closure, N, M):%0A%0A    %22%22%22Converts a residue closure into the residue square closure%0A%0A    :param func residue_closure: The closure returning the :math:%60M%60 residues.%0A    :param int N: The number of parameters, not actually used here.%0A    :param int M: The number of configurations.%0A%0A    %22%22%22%0A%0A    def norm_sq_closure(param):%0A        residues = residue_closure(param)%0A        return sum(i_residue ** 2 for i_residue in residues)%0A%0A    return norm_sq_closure%0A%0A%0Adef conv_jacobian(jacobian_closure, residue_closure, N, M):%0A%0A    %22%22%22Converts a Jacobian closure into the Jacobian for minimize%0A%0A    :param func jacobian_closure: The closure for computing the Jacobian matrix.%0A    :param func residue_closure: The closure for computing the residue vector.%0A    :param int N: The number of parameters%0A    :param int M: The number of residues%0A%0A    %22%22%22%0A%0A    def jacobian(param):%0A        residue = residue_closure(param)%0A        jacobian = jacobian_closure(param)%0A        result = np.empty(N, dtype=np.float64)%0A        for i in xrange(0, N):%0A            result%5Bi%5D = sum(%0A                    2 * residue%5Bj%5D * jacobian%5Bi%5D%5Bj%5D for j in xrange(0, M)%0A                    )%0A            continue%0A        return result%0A%0A    return jacobian%0A%0A%0A%0A%0A%0A%0A%0A

Add the closure conversion functions