+
    LV-j‘/  ã                   ó¢   € 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      # )z Compute 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   & €€€Úk/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/scipy/optimize/_lsq/dogbox.pyr   Úlsmr_operator.<locals>.matvecB   s2   ø€ Ø—‘“—‘Ó!ˆØˆˆzÑØ�z‰z˜!˜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-   ú€ ð �9‰9�D€A÷!÷
ô
 ˜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WgW‰W«3# )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ä�zŠz˜+ zÓ2€HÜ�zŠz˜+Ó1€Hä�XŠX�hÓ,€FÜ�XŠX�hÓ,€Fä�8Š8�H˜jÓ)€DÜ�8Š8�HÓ(€Dà˜v¨tÐ9Ð9r#   c                óþ  € \        WWg4      w  r‰r«rÍ\        P                  ! V \        R7      p\	        WV	4      '       d   WR3# \        \        P                  ! V 4      V) W‰4      w  pp\        W4^ V4      ^ ,          ) V,          pVV,
          p\        VVW‰4      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Ñ2€H˜¨ô —’˜q¬Ô,€Jä�¨×1Ò1Ø¨Ð-Ð-ä%¤b§m¢m°AÓ&6¸¸¸HÓO�L€Iˆqô )¨¨q°)Ó<¸QÕ?Ð?À!ÕC€Kà˜kÕ)€IÜ(¨°iØ)1ó=�O€Iˆtà&(€J��q‘˜FÕ"Ñ#Ø&'€J��q‘˜FÕ"Ñ#Ü�VŠV�T˜A‘X Õ%¨°©°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MúV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Ø�V‰V‹X€FØ€Dà
€AØ€DàÒ Ù˜AÓˆØ”R—V’V˜C �F“^Õ#ˆÜ-¨a°°CÓ8‰ˆ‰1à”R—V’V˜A˜q“\Õ!ˆä�Q˜Ó€Aä˜7¤CÓ(×=Ð=¨W¸Ñ-=€IßÜ,¨QÓ/Ñˆ‰yà" A¨¥Kˆyˆä��i•¤R§V¡VÔ,€EØ�„zØˆä�}Š}˜R¤sÔ+€HØ!#€HŒR�XŠX�bÓÑØ!"€HŒR�XŠX�bÓÑà
€AÜ�=Š=˜Ó€DàÒØ—7‘7˜S•=ˆàÐØ€IØ€IØÐà�!„|ÜÔ à
Ø •\ AÑ%ˆ
Ø�;ˆà�8•ˆØ—‘“ˆØˆˆ*‰ä�aœRŸV™VÔ$ˆØ�DŒ=Ø!"Ðà�aŒ<Ü% i°°tÐ=MØ&/°ô9ð Ò)¨T°XÔ-=Ùà�8•ˆØ�X•,ˆØ�X•,ˆØ˜8•_ˆ
ð ˜ÔØ�q˜(�{•^ˆFÜ ¨¨°"Ô5°aÕ8ˆKô & f¨f°v°gÓ>‰DˆA‰qØ˜&Ô Ü" 1Ó%ˆCô $ C¨°
Ó;ˆGÜ ¨Ñ9¨jÑ9¸!Õ<¸XÕFÐFˆKØ˜:Õ%ˆKô & c¨1¨q¨bÓ1‰DˆAˆqàÐØ !Õ#¨¨x­Ø 
Õ*ˆIä/:Ø˜ V¨Q°°9¸gÀwó0PÑ,ˆI�} fð �I‰I�cŒNØ&ˆD�‰Nà˜GÔ#Ü'9¸&À&Ø:Có(Eð 'EÑ#à˜fÔ$Ü'9¸#¸qÀ$Ó'GÐ&GÐ#ô —G’G˜A �H bÓ-ˆEá˜“JˆEØ�A�IˆDä˜t iÕ/´R·V±VÔ<ˆKä—6’6œ"Ÿ+š+ eÓ,×-Ò-Ø˜{Õ*�Ùð Ò(Ù(¨¸$Ô?‘à¤§¢¨¨uÓ!5Õ5�Ø# h�Ðä+ØÐ'Ð)<Ø˜Vó‰LˆE�5ô
 ˜T›
ˆIÜ!2Ø  $¨	´4¸³7¸EÀ4ó"OÐð "Õ-Øà˜aÔØ!.ˆH�XÑàˆAà˜r‘>ˆDØ˜•hˆAˆd‰GØ˜q‘=ˆDØ˜•hˆAˆd‰GàˆAØ—V‘V“XˆFàˆDá�A“ˆAØ�A�IˆDàÒ(Ù# AÓ&�Ü5°a¸¸CÓ@‘��1ä˜Q Ó"ˆAçÜ#4°Q¸	Ó#BÑ ��yøàˆIØ Ðà�Q�ˆ	ð ÕÜ"0Ø˜ 	°ô#6Ðà*2Ð Ñ'ä(ØÐ-÷õ ð &(Ð"ØàÒ!ØÐäØ
�$˜F¨°À6Ø 4¨dÐ;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#   