code: def leastsqbound(func, x0, bounds, args=(), **kw):
      
      # check for full output
      if "full_output" in kw and kw["full_output"]:
          full = True
      else:
          full = False
  
      # convert x0 to internal variables
      i0 = external2internal(x0, bounds)
  
      # perfrom unconstrained optimization using internal variables
      r = leastsq(err, i0, args=(bounds, func, args), **kw)
  
      # unpack return convert to external variables and return
      if full:
          xi, cov_xi, infodic, mesg, ier = r
          xe = internal2external(xi, bounds)
          cov_xe = i2e_cov_x(xi, bounds, cov_xi)
          # XXX correct infodic 'fjac','ipvt', and 'qtf'
          return xe, cov_xe, infodic, mesg, ier
  
      else:
          xi, ier = r
          xe = internal2external(xi, bounds)
          return xe, ier
docs: Constrained multivariant Levenberg-Marquard optimization
  
      Minimize the sum of squares of a given function using the 
      Levenberg-Marquard algorithm. Contraints on parameters are inforced using 
      variable transformations as described in the MINUIT User's Guide by
      Fred James and Matthias Winkler.
  
      Parameters:
  
      * func      functions to call for optimization.
      * x0        Starting estimate for the minimization.
      * bounds    (min,max) pair for each element of x, defining the bounds on
                  that parameter.  Use None for one of min or max when there is
                  no bound in that direction.
      * args      Any extra arguments to func are places in this tuple.
  
      Returns: (x,{cov_x,infodict,mesg},ier)
  
      Return is described in the scipy.optimize.leastsq function.  x and con_v  
      are corrected to take into account the parameter transformation, infodic 
      is not corrected.
  
      Additional keyword arguments are passed directly to the 
      scipy.optimize.leastsq algorithm.