+
    /i/                         R t ^ RIt^ RIHtHt ^ RIHtHtH	t	 ^ RI
Ht ^ RIHt ^RIHtHtHtHtHtHtHtHtHtHtHtHt R tR tR	 tRR
 ltR# )a	  
Dogleg algorithm with rectangular trust regions for least-squares minimization.

The description of the algorithm can be found in [Voglis]_. The algorithm does
trust-region iterations, but the shape of trust regions is rectangular as
opposed to conventional elliptical. The intersection of a trust region and
an initial feasible region is again some rectangle. Thus, on each iteration a
bound-constrained quadratic optimization problem is solved.

A quadratic problem is solved by well-known dogleg approach, where the
function is minimized along piecewise-linear "dogleg" path [NumOpt]_,
Chapter 4. If Jacobian is not rank-deficient then the function is decreasing
along this path, and optimization amounts to simply following along this
path as long as a point stays within the bounds. A constrained Cauchy step
(along the anti-gradient) is considered for safety in rank deficient cases,
in this situations the convergence might be slow.

If during iterations some variable hit the initial bound and the component
of anti-gradient points outside the feasible region, then a next dogleg step
won't make any progress. At this state such variables satisfy first-order
optimality conditions and they are excluded before computing a next dogleg
step.

Gauss-Newton step can be computed exactly by `numpy.linalg.lstsq` (for dense
Jacobian matrices) or by iterative procedure `scipy.sparse.linalg.lsmr` (for
dense and sparse matrices, or Jacobian being LinearOperator). The second
option allows to solve very large problems (up to couple of millions of
residuals on a regular PC), provided the Jacobian matrix is sufficiently
sparse. But note that dogbox is not very good for solving problems with
large number of constraints, because of variables exclusion-inclusion on each
iteration (a required number of function evaluations might be high or accuracy
of a solution will be poor), thus its large-scale usage is probably limited
to unconstrained problems.

References
----------
.. [Voglis] C. Voglis and I. E. Lagaris, "A Rectangular Trust Region Dogleg
            Approach for Unconstrained and Bound Constrained Nonlinear
            Optimization", WSEAS International Conference on Applied
            Mathematics, Corfu, Greece, 2004.
.. [NumOpt] J. Nocedal and S. J. Wright, "Numerical optimization, 2nd edition".
N)lstsqnorm)LinearOperatoraslinearoperatorlsmr)OptimizeResult)_call_callback_maybe_halt)step_size_to_bound	in_boundsupdate_tr_radiusevaluate_quadraticbuild_quadratic_1dminimize_quadratic_1dcompute_gradcompute_jac_scalecheck_terminationscale_for_robust_loss_functionprint_header_nonlinearprint_iteration_nonlinearc                j   a aa S P                   w  r4V VV3R lpV VV3R lp\        W43WV\        R7      # )zCompute LinearOperator to use in LSMR by dogbox algorithm.

`active_set` mask is used to excluded active variables from computations
of matrix-vector products.
c                 z   < V P                  4       P                  4       p^ VS&   SP                  V S,          4      #     )ravelcopymatvec)xx_freeJop
active_setds   & X/var/www/html/photoedit/myenv/lib/python3.14/site-packages/scipy/optimize/_lsq/dogbox.pyr   lsmr_operator.<locals>.matvecB   s2    !zzz!a%      c                 B   < SSP                  V 4      ,          p^ VS&   V# r   )rmatvec)r   rr   r   r    s   & r!   r%   lsmr_operator.<locals>.rmatvecG   s#    A*r#   )r   r%   dtype)shaper   float)r   r    r   mnr   r%   s   fff    r!   lsmr_operatorr-   :   s-     99DA!

 1&NNr#   c                8   W ,
          pW0,
          p\         P                  ! WA) 4      p\         P                  ! WQ4      p\         P                  ! Wd4      p\         P                  ! Wu4      p	\         P                  ! Wa) 4      p
\         P                  ! Wq4      pWgWW3# )a  Find intersection of trust-region bounds and initial bounds.

Returns
-------
lb_total, ub_total : ndarray with shape of x
    Lower and upper bounds of the intersection region.
orig_l, orig_u : ndarray of bool with shape of x
    True means that an original bound is taken as a corresponding bound
    in the intersection region.
tr_l, tr_u : ndarray of bool with shape of x
    True means that a trust-region bound is taken as a corresponding bound
    in the intersection region.
)npmaximumminimumequal)r   	tr_boundslbublb_centeredub_centeredlb_totalub_totalorig_lorig_utr_ltr_us   &&&&        r!   find_intersectionr>   O   sw     &K&Kzz+z2Hzz+1HXXh,FXXh,F88Hj)D88H(Dvt99r#   c                   \        WWg4      w  rrr\        P                  ! V \        R7      p\	        WV	4      '       d   WR3# \        \        P                  ! V 4      V) W4      w  pp\        W4^ V4      ^ ,          ) V,          pVV,
          p\        VVW4      w  ppRVV^ 8  V
,          &   ^VV^ 8  V,          &   \        P                  ! V^ 8  V,          V^ 8  V,          ,          4      pVVV,          ,           VV3# )a  Find dogleg step in a rectangular region.

Returns
-------
step : ndarray, shape (n,)
    Computed dogleg step.
bound_hits : ndarray of int, shape (n,)
    Each component shows whether a corresponding variable hits the
    initial bound after the step is taken:
        *  0 - a variable doesn't hit the bound.
        * -1 - lower bound is hit.
        *  1 - upper bound is hit.
tr_hit : bool
    Whether the step hit the boundary of the trust-region.
r(   F)r>   r/   
zeros_likeintr
   r	   r   any)r   newton_stepgabr3   r4   r5   r8   r9   r:   r;   r<   r=   
bound_hits	to_bounds_cauchy_step	step_diff	step_sizehitstr_hits   &&&&&&&&              r!   dogleg_steprQ   l   s      6G	b62H q,J11--%bmmA&6HOLIq )q)<Q??!CKk)I(i)1=OIt&(JqF"#&'JqF"#VVTAX%T(99:FY..
FBBr#   c                 
   TpVP                  4       p^pTp^pVe>   V! V4      pR\        P                  ! V^ ,          4      ,          p\        VVV4      w  ppMR\        P                  ! VV4      ,          p\        VV4      p\        V\        4      ;'       d    VR8H  pV'       d   \        V4      w  ppMT^V,          pp\        VV,          \        P                  R7      pV^ 8X  d   Rp\        P                  ! V\        R7      pRV\        P                  ! W%4      &   ^V\        P                  ! W&4      &   Tp\        P                  ! V4      pV
f   VP                  ^d,          p
Rp ^ p!Rp"Rp#V^8X  d   \!        4         VV,          ^ 8  p$V$( p%VV%,          p&VP                  4       p'^ VV$&   \        V\        P                  R7      p(V(V	8  d   ^p V^8X  d   \#        V!VVV#V"V(4       V f   VV
8X  d   EMVV%,          p)VV%,          p*VV%,          p+VV%,          p,VR8X  d4   VR	V%3,          p-\%        V-V) RR
7      ^ ,          p.\'        V-V&V&) 4      w  p/p0MUVR8X  dO   \)        V4      p1\+        V1VV$4      p2\-        V2V3/ VB ^ ,          V%,          ) p.V.V,,          p.\'        V1VV) 4      w  p/p0Rp#V#^ 8:  Edi   VV
8  Eda   VV,,          p3\/        V)X.V&X/X0V3V*V+4      w  p4p5p6VP1                  R4       V4VV%&   VR8X  d   \3        X-V&V44      ) p7MVR8X  d   \3        X1VV4      ) p7\        P4                  ! VV,           WV4      p8V ! V84      p9V^,          p\        VV,          \        P                  R7      p:\        P6                  ! \        P8                  ! V94      4      '       g   RV:,          pK  Ve   V! V9RR7      p;MR\        P                  ! V9V94      ,          p;VV;,
          p#\;        VV#X7V:V64      w  pp<\        V4      p"\=        V#VV"\        V4      V<Wx4      p V f   EKo   V#^ 8  d   X5VV%&   X8pVR8H  p=VV=,          VV=&   V^8H  p=VV=,          VV=&   X9pVP                  4       pX;pV! V4      pV^,          pVe   V! V4      p\        VVV4      w  pp\        VV4      pV'       d   \        VV4      w  ppM^ p"^ p#V!^,          p!Vf   EKD  \?        VVV!VR7      p>X;V>R&   \A        VV>4      '       g   EKm  Rp  V f   ^ p \?        VVVVV'V(VVVV R7
      # )   Ng      ?jac)ordg      ?r@   Texact:NNN)rcondr   g        g      ?)	cost_only)r   funnitnfevcost)
r   r\   rY   rT   grad
optimalityactive_maskr[   njevstatusrA   g      )!r   r/   sumr   dotr   
isinstancestrr   r   infrB   rC   r2   
empty_likesizer   r   r   r   r   r-   r   rQ   fillr   clipallisfiniter   r   r   r   )?rY   rT   x0f0J0r4   r5   ftolxtolgtolmax_nfevx_scaleloss_function	tr_solver
tr_optionsverbosecallbackff_truer[   Jr`   rhor\   rF   	jac_scalescale	scale_invDeltaon_boundr   steptermination_status	iteration	step_normactual_reductionr   free_setg_freeg_fullg_normr   lb_freeub_free
scale_freeJ_freerE   rG   rH   r   lsmr_opr3   	step_freeon_bound_freerP   predicted_reductionx_newf_newstep_h_normcost_newratiomaskintermediate_results?   &&&&&&&&&&&&&&&&&                                              r!   dogboxr      sE   
AVVXFD
AD ARVVCF^#-aC81RVVAq\!QA7C(==W-=I,Q/y"AKyiRVV,Ez}}Rs+H!#HRXXb!"HRXXb
A==D77S=II!| 
\A%
;8*aRVV$D=!"a<%it=M&/9 )TX-=8X,X,8_
 q({^F"5a8K &ffvg>DAq& "1%C $C
;G9j9!<XFFK:%K &c1qb1DAq!#x
*I/:VQ9gw0P,I}f IIcN&DNG#'9&&:C(E 'E#f$'9#q$'G&G# GGAHb-EJEAIDti/RVV<K66"++e,--{* (($?u!55#h+')<VLE5
 T
I!2 $	47E4"O "-a!.HXAr>DhAdGq=DhAdGAVVXFDAAAID(#A&5aC@1Q"A#4Q	#B yI Q	 "0	#6*2'(-  &("!
$F64d;MO Or#   )N)__doc__numpyr/   numpy.linalgr   r   scipy.sparse.linalgr   r   r   scipy.optimizer   scipy._lib._utilr   commonr	   r
   r   r   r   r   r   r   r   r   r   r   r-   r>   rQ   r    r#   r!   <module>r      sL   )T  $ F F ) 67 7 7 7O*::(CVBOr#   