ó
    Eñi P  ã                   óT  • S r SSKJr  SSKrSSKJr  SSKJrJ	r	J
r
  SSKJrJr  SSKJr  \R                   " \5      R$                  rS r  S"S	 jrS
 rS rS#S jrS$S jrS%S jrS rS rS&S jrS&S jrS rS r S r!S r"S r#S r$S r%S%S jr&S r'S r(S r)S'S jr*S'S jr+S  r,S! r-g)(z+Functions used by least-squares algorithms.é    )ÚcopysignN)Únorm)Ú
cho_factorÚ	cho_solveÚLinAlgError)ÚLinearOperatorÚaslinearoperator)Úissparsec                 óT  • [         R                  " X5      nUS:X  a  [        S5      e[         R                  " X5      n[         R                  " X 5      US-  -
  nUS:”  a  [        S5      e[         R                  " XD-  X5-  -
  5      nU[	        Xd5      -   * nXs-  nXW-  n	X‰:  a  X‰4$ X˜4$ )aE  Find the intersection of a line with the boundary of a trust region.

This function solves the quadratic equation with respect to t
||(x + s*t)||**2 = Delta**2.

Returns
-------
t_neg, t_pos : tuple of float
    Negative and positive roots.

Raises
------
ValueError
    If `s` is zero or `x` is not within the trust region.
r   z`s` is zero.é   z#`x` is not within the trust region.)ÚnpÚdotÚ
ValueErrorÚsqrtr   )
ÚxÚsÚDeltaÚaÚbÚcÚdÚqÚt1Út2s
             ÚW/home/mande/repo/quber/.venv/lib/python3.13/site-packages/scipy/optimize/_lsq/common.pyÚintersect_trust_regionr      s£   € ô  	�Šˆq‹€AØˆAƒvÜ˜Ó(Ð(ä
�Šˆq‹€Aä
�Šˆq‹�u˜a‘xÑ€AØˆ1ƒuÜÐ>Ó?Ð?ä
�Š�‘�a‘c‘	Ó€Að Œh�q‹nÑ
Ð€AØ	
‰€BØ	
‰€Bà	ƒwØˆvˆàˆvˆó    c	                 óÄ  • S n	X2-  n
X:¼  a  [         U-  US   -  nUS   U:„  nOSnU(       a(  UR                  X#-  5      * n[        U5      U::  a  USS4$ [        U
5      U-  nU(       a  U	" SX£U5      u  nnU* U-  nOSnUb  U(       d  US:X  a  [        SU-  UU-  S-  5      nOUn[	        U5       Hx  nUU:  d  UU:”  a  [        SU-  UU-  S-  5      nU	" UX£U5      u  nnUS:  a  UnUU-  n[        UUU-
  5      nUXõ-   U-  U-  -  n[
        R                  " U5      Xu-  :  d  Mx    O   UR                  X£S-  U-   -  5      * nXÕ[        U5      -  -  nUUWS	-   4$ )
a  Solve a trust-region problem arising in least-squares minimization.

This function implements a method described by J. J. More [1]_ and used
in MINPACK, but it relies on a single SVD of Jacobian instead of series
of Cholesky decompositions. Before running this function, compute:
``U, s, VT = svd(J, full_matrices=False)``.

Parameters
----------
n : int
    Number of variables.
m : int
    Number of residuals.
uf : ndarray
    Computed as U.T.dot(f).
s : ndarray
    Singular values of J.
V : ndarray
    Transpose of VT.
Delta : float
    Radius of a trust region.
initial_alpha : float, optional
    Initial guess for alpha, which might be available from a previous
    iteration. If None, determined automatically.
rtol : float, optional
    Stopping tolerance for the root-finding procedure. Namely, the
    solution ``p`` will satisfy ``abs(norm(p) - Delta) < rtol * Delta``.
max_iter : int, optional
    Maximum allowed number of iterations for the root-finding procedure.

Returns
-------
p : ndarray, shape (n,)
    Found solution of a trust-region problem.
alpha : float
    Positive value such that (J.T*J + alpha*I)*p = -J.T*f.
    Sometimes called Levenberg-Marquardt parameter.
n_iter : int
    Number of iterations made by root-finding procedure. Zero means
    that Gauss-Newton step was selected as the solution.

References
----------
.. [1] More, J. J., "The Levenberg-Marquardt Algorithm: Implementation
       and Theory," Numerical Analysis, ed. G. A. Watson, Lecture Notes
       in Mathematics 630, Springer Verlag, pp. 105-116, 1977.
c                 ó€   • US-  U -   n[        X-  5      nXS-
  n[        R                  " US-  US-  -  5      * U-  nXg4$ )zˆFunction of which to find zero.

It is defined as "norm of regularized (by alpha) least-squares
solution minus `Delta`". Refer to [1]_.
r   é   )r   r   Úsum)ÚalphaÚsufr   r   ÚdenomÚp_normÚphiÚ	phi_primes           r   Úphi_and_derivativeÚ2solve_lsq_trust_region.<locals>.phi_and_derivativej   sO   € ð �1‘�u‘ˆÜ�c‘kÓ"ˆØ‰nˆÜ—V’V˜C 1™H u¨a¡xÑ/Ó0Ð0°6Ñ9ˆ	Øˆ~Ðr   r   éÿÿÿÿFg        gü©ñÒMbP?ç      à?r   é   )ÚEPSr   r   ÚmaxÚranger   Úabs)ÚnÚmÚufr   ÚVr   Úinitial_alphaÚrtolÚmax_iterr(   r#   Ú	thresholdÚ	full_rankÚpÚalpha_upperr&   r'   Úalpha_lowerr"   ÚitÚratios                        r   Úsolve_lsq_trust_regionr?   9   s¦  € òb
ð ‰&€Cð 	ƒvÜ˜!‘G˜a ™d‘Nˆ	Ø�b‘E˜IÑ%‰	àˆ	æØ�U‰U�2‘6‹]ˆNˆÜ�‹7�eÓØ�c˜1�9Ðä�s“)˜eÑ#€KæÙ+¨C°¸Ó?‰ˆˆYØ�d˜YÑ&‰àˆàÑ¦I°-À1Ó2DÜ�E˜KÑ'¨+¸Ñ*CÀcÑ)IÓJ‰àˆä�HŽoˆØ�;Ó %¨+Ó"5Ü˜ Ñ+¨k¸KÑ.GÈ#Ñ-MÓNˆEá+¨E°3¸5ÓA‰ˆˆYà�‹7ØˆKà�i‘ˆÜ˜+ u¨u¡}Ó5ˆØ�#‘+ Ñ&¨Ñ.Ñ.ˆä�6Š6�#‹;˜™Õ%Ùñ ð  
�‰ˆs˜‘d˜U‘lÑ#Ó	$Ð$€Að
 ”�a“‰Ñ€Aàˆe�R˜!‘VÐÐr   c                 ó*  •  [        U 5      u  p4[        X44U5      * n[        R                  " XU5      US-  ::  a  US4$  U S   US-  -  nU S   US-  -  nU S   US-  -  nUS   U-  n	US   U-  n
[        R
                  " U* U	-   SXh-
  U
-   -  SU-  SU* U-   U
-   -  U* U	-
  /5      n[        R                  " U5      n[        R                  " U[        R                  " U5         5      nU[        R                  " SU-  SUS-  -   -  SUS-  -
  SUS-  -   -  45      -  nS	[        R                  " XPR                  U5      -  SS
9-  [        R                  " X5      -   n[        R                  " U5      nUSS2U4   nUS4$ ! [         a     GNRf = f)a2  Solve a general trust-region problem in 2 dimensions.

The problem is reformulated as a 4th order algebraic equation,
the solution of which is found by numpy.roots.

Parameters
----------
B : ndarray, shape (2, 2)
    Symmetric matrix, defines a quadratic term of the function.
g : ndarray, shape (2,)
    Defines a linear term of the function.
Delta : float
    Radius of a trust region.

Returns
-------
p : ndarray, shape (2,)
    Found solution.
newton_step : bool
    Whether the returned solution is the Newton step which lies within
    the trust region.
r   T)r   r   )r   r,   )r,   r,   r   r,   é   r+   ©ÚaxisNF)r   r   r   r   r   ÚarrayÚrootsÚrealÚisrealÚvstackr!   Úargmin)ÚBÚgr   ÚRÚlowerr:   r   r   r   r   ÚfÚcoeffsÚtÚvalueÚis                  r   Úsolve_trust_region_2drS   «   s§  € ð.Ü˜a“=‰ˆÜ˜�z 1Ó%Ð%ˆÜ�6Š6�!‹<˜5 !™8Ó#Ø�d�7ˆNð $ð
 	
ˆ$‰�%˜‘(Ñ€AØ	ˆ$‰�%˜‘(Ñ€AØ	ˆ$‰�%˜‘(Ñ€Aà	ˆ!‰ˆu‰€AØ	ˆ!‰ˆu‰€Aä�XŠXØ
ˆˆa‰��a‘e˜a‘i‘ ! a¡%¨¨q¨b°1©f°q©jÑ)9¸A¸2À¹6ÐBóD€Fä
�Š�Ó€AÜ
�Š�”"—)’)˜A“,‘Ó €Aà”—	’	˜1˜q™5 A¨¨1©¡HÑ-°°A°q±D±¸QÀÀAÁ¹XÑ/FÐGÓHÑH€AØ”"—&’&˜ŸU™U 1›X™¨AÑ.Ñ.´·²¸³Ñ=€EÜ
�	Š	�%Ó€AØ	Š!ˆQˆ$‰€Aàˆeˆ8€Oøô) ó Úðús   ‚;F Æ
FÆFc                 ó†   • US:”  a  X-  nOX!s=:X  a  S:X  a  O  OSnOSnUS:  a  SU-  n X4$ US:”  a  U(       a  U S-  n X4$ )z±Update the radius of a trust region based on the cost reduction.

Returns
-------
Delta : float
    New radius.
ratio : float
    Ratio between actual and predicted reductions.
r   r,   ç      Ð?g      è?g       @© )r   Úactual_reductionÚpredicted_reductionÚ	step_normÚ	bound_hitr>   s         r   Úupdate_tr_radiusr[   Þ   s`   € ð ˜QÓØ Ñ6‰Ø	Õ	5°AÖ	5Ø‰àˆàˆtƒ|Ø�yÑ ˆð ˆ<Ðð 
�‹ž)Ø�‰ˆàˆ<Ðr   c                 ó  • U R                  U5      n[        R                   " XU5      nUb  U[        R                   " X#-  U5      -  nUS-  n[        R                   " X5      nUbœ  U R                  U5      nU[        R                   " X…5      -  nS[        R                   " Xˆ5      -  [        R                   " X5      -   n	Ub;  U[        R                   " XC-  U5      -  nU	S[        R                   " XC-  U5      -  -  n	XgU	4$ Xg4$ )aH  Parameterize a multivariate quadratic function along a line.

The resulting univariate quadratic function is given as follows::

    f(t) = 0.5 * (s0 + s*t).T * (J.T*J + diag) * (s0 + s*t) +
           g.T * (s0 + s*t)

Parameters
----------
J : ndarray, sparse array or LinearOperator shape (m, n)
    Jacobian matrix, affects the quadratic term.
g : ndarray, shape (n,)
    Gradient, defines the linear term.
s : ndarray, shape (n,)
    Direction vector of a line.
diag : None or ndarray with shape (n,), optional
    Addition diagonal part, affects the quadratic term.
    If None, assumed to be 0.
s0 : None or ndarray with shape (n,), optional
    Initial point. If None, assumed to be 0.

Returns
-------
a : float
    Coefficient for t**2.
b : float
    Coefficient for t.
c : float
    Free term. Returned only if `s0` is provided.
r+   )r   r   )
ÚJrK   r   ÚdiagÚs0Úvr   r   Úur   s
             r   Úbuild_quadratic_1drb   û   sÝ   € ð> 	
�‰ˆa‹€AÜ
�Šˆq‹€AØÑØ	ŒR�VŠV�A‘H˜aÓ Ñ ˆØˆ�H€Aä
�Šˆq‹€Aà	�~Ø�E‰E�"‹IˆØ	ŒR�VŠV�A‹\ÑˆØ”"—&’&˜“,Ñ¤§¢¨£Ñ.ˆØÑØ”—’˜™	 1Ó%Ñ%ˆAØ�”r—v’v˜b™i¨Ó,Ñ,Ñ,ˆAØ�Qˆwˆàˆtˆr   c                 óæ   • X#/nU S:w  a(  SU-  U -  nX&s=:  a  U:  a  O  OUR                  U5        [        R                  " U5      nXPU-  U-   -  U-   n[        R                  " U5      nXX   Xx   4$ )z»Minimize a 1-D quadratic function subject to bounds.

The free term `c` is 0 by default. Bounds must be finite.

Returns
-------
t : float
    Minimum point.
y : float
    Minimum value.
r   g      à¿)Úappendr   ÚasarrayrI   )	r   r   ÚlbÚubr   rP   ÚextremumÚyÚ	min_indexs	            r   Úminimize_quadratic_1drk   .  ss   € ð 
ˆ€AØˆAƒvØ˜!‘8˜a‘<ˆØÕ˜2ÖØ�H‰H�XÔÜ
�
Š
�1‹€AØ	�‰U�Q‰Y‰˜!Ñ€AÜ—	’	˜!“€IØ‰<˜™Ð%Ð%r   c                 ó’  • UR                   S:X  aG  U R                  U5      n[        R                  " XD5      nUb  U[        R                  " X#-  U5      -  nOSU R                  UR                  5      n[        R                  " US-  SS9nUb  U[        R                  " X2S-  -  SS9-  n[        R                  " X!5      nSU-  U-   $ )a£  Compute values of a quadratic function arising in least squares.

The function is 0.5 * s.T * (J.T * J + diag) * s + g.T * s.

Parameters
----------
J : ndarray, sparse array or LinearOperator, shape (m, n)
    Jacobian matrix, affects the quadratic term.
g : ndarray, shape (n,)
    Gradient, defines the linear term.
s : ndarray, shape (k, n) or (n,)
    Array containing steps as rows.
diag : ndarray, shape (n,), optional
    Addition diagonal part, affects the quadratic term.
    If None, assumed to be 0.

Returns
-------
values : ndarray with shape (k,) or float
    Values of the function. If `s` was 2-D, then ndarray is
    returned, otherwise, float is returned.
r,   r   r   rB   r+   )Úndimr   r   ÚTr!   )r]   rK   r   r^   ÚJsr   Úls          r   Úevaluate_quadraticrq   E  s¨   € ð. 	‡v�v�ƒ{Ø�U‰U�1‹XˆÜ�FŠF�2‹NˆØÑØ”—’˜™ !Ó$Ñ$ˆAøà�U‰U�1—3‘3‹ZˆÜ�FŠF�2�q‘5˜qÑ!ˆØÑØ”—’˜ !™t™¨!Ñ,Ñ,ˆAä
�Šˆq‹€Aà�‰7�Q‰;Ðr   c                 ó<   • [         R                  " X:¬  X:*  -  5      $ )z$Check if a point lies within bounds.)r   Úall)r   rf   rg   s      r   Ú	in_boundsrt   o  s   € ä�6Š6�1‘7˜q™wÑ'Ó(Ð(r   c                 óþ  • [         R                  " U5      nX   n[         R                  " U 5      nUR                  [         R                  5        [         R
                  " SS9   [         R                  " X -
  U   U-  X0-
  U   U-  5      Xd'   SSS5        [         R                  " U5      nU[         R                  " Xg5      [         R                  " U5      R                  [        5      -  4$ ! , (       d  f       Nf= f)a¼  Compute a min_step size required to reach a bound.

The function computes a positive scalar t, such that x + s * t is on
the bound.

Returns
-------
step : float
    Computed step. Non-negative value.
hits : ndarray of int with shape of x
    Each element indicates whether a corresponding variable reaches the
    bound:

         *  0 - the bound was not hit.
         * -1 - the lower bound was hit.
         *  1 - the upper bound was hit.
Úignore)ÚoverN)r   ÚnonzeroÚ
empty_likeÚfillÚinfÚerrstateÚmaximumÚminÚequalÚsignÚastypeÚint)r   r   rf   rg   Únon_zeroÚ
s_non_zeroÚstepsÚmin_steps           r   Ústep_size_to_boundr‡   t  s¹   € ô$ �zŠz˜!‹}€HØ‘€JÜ�MŠM˜!Ó€EØ	‡J�JŒr�v‰vÔÜ	�Š˜(Ó	#ÜŸ*š* b¡f¨hÑ%7¸*Ñ%DØ&(¡f¨hÑ%7¸*Ñ%DóFˆ‰÷ 
$ô �vŠv�e‹}€HØ”R—X’X˜eÓ.´·²¸³×1BÑ1BÄ3Ó1GÑGÐGÐG÷	 
$Õ	#ús   Á$*C.Ã.
C<c                 óü  • [         R                  " U [        S9nUS:X  a  SX@U:*  '   SX@U:¬  '   U$ X-
  nX -
  nU[         R                  " S[         R                  " U5      5      -  nU[         R                  " S[         R                  " U5      5      -  n[         R
                  " U5      U[         R                  " Xg5      :*  -  n	SXI'   [         R
                  " U5      U[         R                  " XX5      :*  -  n
SXJ'   U$ )a�  Determine which constraints are active in a given point.

The threshold is computed using `rtol` and the absolute value of the
closest bound.

Returns
-------
active : ndarray of int with shape of x
    Each component shows whether the corresponding constraint is active:

         *  0 - a constraint is not active.
         * -1 - a lower bound is active.
         *  1 - a upper bound is active.
©Údtyper   r*   r,   )r   Ú
zeros_liker‚   r}   r0   ÚisfiniteÚminimum)r   rf   rg   r6   ÚactiveÚ
lower_distÚ
upper_distÚlower_thresholdÚupper_thresholdÚlower_activeÚupper_actives              r   Úfind_active_constraintsr•   ‘  sÛ   € ô �]Š]˜1¤CÑ(€FàˆqƒyØˆ�B‰w‰Øˆ�B‰w‰Øˆà‘€JØ‘€JàœRŸZšZ¨¬2¯6ª6°"«:Ó6Ñ6€OØœRŸZšZ¨¬2¯6ª6°"«:Ó6Ñ6€Oä—K’K “OØ¤2§:¢:¨jÓ#JÑJñL€Là€FÑä—K’K “OØ¤2§:¢:¨jÓ#JÑJñL€Là€FÑà€Mr   c           	      ó&  • U R                  5       n[        XX#5      n[        R                  " US5      n[        R                  " US5      nUS:X  a;  [        R                  " X   X&   5      XF'   [        R                  " X'   X   5      XG'   OnX   U[        R
                  " S[        R                  " X   5      5      -  -   XF'   X'   U[        R
                  " S[        R                  " X'   5      5      -  -
  XG'   XA:  XB:„  -  nSX   X(   -   -  XH'   U$ )zÈShift a point to the interior of a feasible region.

Each element of the returned vector is at least at a relative distance
`rstep` from the closest bound. If ``rstep=0`` then `np.nextafter` is used.
r*   r,   r   r+   )Úcopyr•   r   r   Ú	nextafterr}   r0   )	r   rf   rg   ÚrstepÚx_newrŽ   Ú
lower_maskÚ
upper_maskÚtight_boundss	            r   Úmake_strictly_feasiblerž   ¸  sù   € ð �F‰F‹H€Eä$ Q¨BÓ6€FÜ—’˜& "Ó%€JÜ—’˜& !Ó$€Jà�ƒzÜŸLšL¨©¸¹ÓHˆÑÜŸLšL¨©¸¹ÓHˆÒà™^Ø"¤R§Z¢Z°´2·6²6¸"¹.Ó3IÓ%JÑJñKˆÑà™^Ø"¤R§Z¢Z°´2·6²6¸"¹.Ó3IÓ%JÑJñKˆÑð ‘J 5¡:Ñ.€LØ Ñ!1°BÑ4DÑ!DÑE€EÑà€Lr   c                 ó  • [         R                  " U 5      n[         R                  " U 5      nUS:  [         R                  " U5      -  nX6   X   -
  XF'   SXV'   US:„  [         R                  " U5      -  nX   X&   -
  XF'   SXV'   XE4$ )aØ  Compute Coleman-Li scaling vector and its derivatives.

Components of a vector v are defined as follows::

           | ub[i] - x[i], if g[i] < 0 and ub[i] < np.inf
    v[i] = | x[i] - lb[i], if g[i] > 0 and lb[i] > -np.inf
           | 1,           otherwise

According to this definition v[i] >= 0 for all i. It differs from the
definition in paper [1]_ (eq. (2.2)), where the absolute value of v is
used. Both definitions are equivalent down the line.
Derivatives of v with respect to x take value 1, -1 or 0 depending on a
case.

Returns
-------
v : ndarray with shape of x
    Scaling vector.
dv : ndarray with shape of x
    Derivatives of v[i] with respect to x[i], diagonal elements of v's
    Jacobian.

References
----------
.. [1] M.A. Branch, T.F. Coleman, and Y. Li, "A Subspace, Interior,
       and Conjugate Gradient Method for Large-Scale Bound-Constrained
       Minimization Problems," SIAM Journal on Scientific Computing,
       Vol. 21, Number 1, pp 1-23, 1999.
r   r*   r,   )r   Ú	ones_liker‹   rŒ   )r   rK   rf   rg   r`   ÚdvÚmasks          r   ÚCL_scaling_vectorr£   Ó  s€   € ô< 	�Š�Q‹€AÜ	�Š�qÓ	€Bà�‰E”R—[’[ “_Ñ$€DØ‰h˜™Ñ €A�GØ€B�Hà�‰E”R—[’[ “_Ñ$€DØ‰g˜™Ñ €A�GØ€B�Hàˆ5€Lr   c                 óÂ  • [        XU5      (       a  U [        R                  " U 5      4$ [        R                  " U5      n[        R                  " U5      nU R	                  5       n[        R
                  " U [        S9nX4) -  n[        R                  " X   SX   -  X   -
  5      XW'   X   X   :  Xg'   U) U-  n[        R                  " X   SX'   -  X   -
  5      XW'   X   X'   :„  Xg'   X4-  nX!-
  n[        R                  " X   X   -
  SX‡   -  5      n	X   [        R                  " U	SX‡   -  U	-
  5      -   XW'   X˜U   :„  Xg'   [        R                  " U 5      n
SX¦'   XZ4$ )z3Compute reflective transformation and its gradient.r‰   r   r*   )
rt   r   r    rŒ   r—   r‹   Úboolr}   r�   Ú	remainder)ri   rf   rg   Ú	lb_finiteÚ	ub_finiter   Ú
g_negativer¢   r   rP   rK   s              r   Úreflective_transformationrª   ÿ  sK  € ä�˜×ÑØ”"—,’,˜q“/Ð!Ð!ä—’˜B“€IÜ—’˜B“€Ià	�‰‹€AÜ—’˜q¬Ñ-€Jà�zÑ!€DÜ�jŠj˜™ ! b¡h¡,°±Ñ"8Ó9€A�GØ‘w ¡Ñ)€JÑàˆ:˜	Ñ!€DÜ�jŠj˜™ ! b¡h¡,°±Ñ"8Ó9€A�GØ‘w ¡Ñ)€JÑàÑ €DØ
‰€AÜ
�Š�Q‘W˜r™xÑ'¨¨Q©W©Ó5€AØ‰hœŸš A q¨1©7¡{°Q¡Ó7Ñ7€A�GØ˜T™7‘{€JÑä
�Š�Q‹€AØ€A�Màˆ4€Kr   c            
      óB   • [        SR                  SSSSSS5      5        g )Nz${:^15}{:^15}{:^15}{:^15}{:^15}{:^15}Ú	Iterationz
Total nfevÚCostúCost reductionú	Step normÚ
Optimality©ÚprintÚformatrV   r   r   Úprint_header_nonlinearr´   !  s%   € Ü	Ð
0ß‰6�+˜|¨VÐ5EØ˜|ó-õ.r   c           	      ób   • Uc  SnOUS nUc  SnOUS n[        U S US US U U US 35        g ©Nz               z^15.2ez^15z^15.4e©r²   )Ú	iterationÚnfevÚcostÚcost_reductionrY   Ú
optimalitys         r   Úprint_iteration_nonlinearr½   '  sX   € àÑØ!‰à*¨6Ð2ˆàÑØ‰	à  Ð(ˆ	ä	ˆY�sˆO˜D ˜: d¨6 ]°>Ð2BÀ9À+ÈjÐY_ÐM`Ð
aÕbr   c            	      ó@   • [        SR                  SSSSS5      5        g )Nz{:^15}{:^15}{:^15}{:^15}{:^15}r¬   r­   r®   r¯   r°   r±   rV   r   r   Úprint_header_linearr¿   6  s#   € Ü	Ð
*ß‰6�+˜vÐ'7¸Øó õ!r   c                 ó\   • Uc  SnOUS nUc  SnOUS n[        U S US U U US 35        g r¶   r·   )r¸   rº   r»   rY   r¼   s        r   Úprint_iteration_linearrÁ   <  sQ   € àÑØ!‰à*¨6Ð2ˆàÑØ‰	à  Ð(ˆ	ä	ˆY�sˆO˜D ˜=¨Ð(8¸¸ÀJÈvÐCVÐ
WÕXr   c                 ó„   • [        U [        5      (       a  U R                  U5      $ U R                  R	                  U5      $ )z4Compute gradient of the least-squares cost function.)Ú
isinstancer   Úrmatvecrn   r   )r]   rN   s     r   Úcompute_gradrÅ   N  s/   € ä�!”^×$Ñ$Ø�y‰y˜‹|Ðà�s‰s�w‰w�q‹zÐr   c                 ó0  • [        U 5      (       aD  [        R                  " U R                  S5      R	                  SS95      R                  5       S-  nO[        R                  " U S-  SS9S-  nUc  SX"S:H  '   O[        R                  " X!5      nSU-  U4$ )z5Compute variables scale based on the Jacobian matrix.r   r   rB   r+   r,   )r
   r   re   Úpowerr!   Úravelr}   )r]   Úscale_inv_oldÚ	scale_invs      r   Úcompute_jac_scalerË   V  s‚   € ä�‡{�{Ü—J’J˜qŸw™w q›zŸ~™~°1˜~Ð5Ó6×<Ñ<Ó>ÀÑC‰	ä—F’F˜1˜a™4 aÑ(¨#Ñ-ˆ	àÑØ$%ˆ	˜q‘.Ò!ä—J’J˜yÓ8ˆ	àˆy‰=˜)Ð#Ð#r   c                 óp   ^ ^• [        T 5      m U U4S jnU U4S jnU U4S jn[        T R                  X#US9$ )z#Return diag(d) J as LinearOperator.c                 ó,   >• TTR                  U 5      -  $ ©N)Úmatvec©r   r]   r   s    €€r   rÏ   Ú(left_multiplied_operator.<locals>.matveci  s   ø€ Ø�1—8‘8˜A“;‰Ðr   c                 óV   >• TS S 2[         R                  4   TR                  U 5      -  $ rÎ   )r   ÚnewaxisÚmatmat©ÚXr]   r   s    €€r   rÔ   Ú(left_multiplied_operator.<locals>.matmatl  s#   ø€ Ø’”B—J‘J�Ñ !§(¡(¨1£+Ñ-Ð-r   c                 óH   >• TR                  U R                  5       T-  5      $ rÎ   )rÄ   rÈ   rÐ   s    €€r   rÄ   Ú)left_multiplied_operator.<locals>.rmatveco  s   ø€ Ø�y‰y˜Ÿ™› Q™Ó'Ð'r   ©rÏ   rÔ   rÄ   ©r	   r   Úshape©r]   r   rÏ   rÔ   rÄ   s   ``   r   Úleft_multiplied_operatorrÞ   e  s6   ù€ ä˜Ó€Aöö.ö(ô ˜!Ÿ'™'¨&Ø")ñ+ð +r   c                 óp   ^ ^• [        T 5      m U U4S jnU U4S jnU U4S jn[        T R                  X#US9$ )z#Return J diag(d) as LinearOperator.c                 óT   >• TR                  [        R                  " U 5      T-  5      $ rÎ   )rÏ   r   rÈ   rÐ   s    €€r   rÏ   Ú)right_multiplied_operator.<locals>.matvecz  s   ø€ Ø�x‰xœŸš › a™Ó(Ð(r   c                 óV   >• TR                  U TS S 2[        R                  4   -  5      $ rÎ   )rÔ   r   rÓ   rÕ   s    €€r   rÔ   Ú)right_multiplied_operator.<locals>.matmat}  s$   ø€ Ø�x‰x˜˜Aša¤§¡˜mÑ,Ñ,Ó-Ð-r   c                 ó,   >• TTR                  U 5      -  $ rÎ   ©rÄ   rÐ   s    €€r   rÄ   Ú*right_multiplied_operator.<locals>.rmatvec€  s   ø€ Ø�1—9‘9˜Q“<ÑÐr   rÚ   rÛ   rÝ   s   ``   r   Úright_multiplied_operatorrç   v  s6   ù€ ä˜Ó€Aö)ö.ö ô ˜!Ÿ'™'¨&Ø")ñ+ð +r   c                 óx   ^ ^^• [        T 5      m T R                  u  mnU U4S jnU UU4S jn[        TU-   U4X4S9$ )z¡Return a matrix arising in regularized least squares as LinearOperator.

The matrix is
    [ J ]
    [ D ]
where D is diagonal matrix with elements from `diag`.
c                 óX   >• [         R                  " TR                  U 5      TU -  45      $ rÎ   )r   ÚhstackrÏ   )r   r]   r^   s    €€r   rÏ   Ú(regularized_lsq_operator.<locals>.matvec’  s#   ø€ Ü�yŠy˜!Ÿ(™( 1›+ t¨a¡xÐ0Ó1Ð1r   c                 óF   >• U S T nU TS  nTR                  U5      TU-  -   $ rÎ   rå   )r   Úx1Úx2r]   r^   r2   s      €€€r   rÄ   Ú)regularized_lsq_operator.<locals>.rmatvec•  s0   ø€ Øˆr�ˆUˆØˆqˆrˆUˆØ�y‰y˜‹}˜t b™yÑ(Ð(r   )rÏ   rÄ   )r	   rÜ   r   )r]   r^   r1   rÏ   rÄ   r2   s   ``   @r   Úregularized_lsq_operatorrð   ‡  s=   ú€ ô 	˜Ó€AØ�7‰7�D€A€qö2÷)ô
 ˜1˜q™5 !˜*¨VÑEÐEr   c                 ó(  • U(       a%  [        U [        5      (       d  U R                  5       n [        U 5      (       a/  U =R                  UR                  U R                  SS9-  sl        U $ [        U [        5      (       a  [        X5      n U $ X-  n U $ )z`Compute J diag(d).

If `copy` is False, `J` is modified in place (unless being LinearOperator).
Úclip)Úmode)rÃ   r   r—   r
   ÚdataÚtakeÚindicesrç   ©r]   r   r—   s      r   Úright_multiplyrø   �  s{   € ö
 ”J˜q¤.×1Ñ1Ø�F‰F‹Hˆä�‡{�{Ø	�Š�!—&‘&˜Ÿ™¨�&Ð0Ñ0�ð €Hô 
�A”~×	&Ñ	&Ü% aÓ+ˆð €Hð 	
‰ˆà€Hr   c                 óˆ  • U(       a%  [        U [        5      (       d  U R                  5       n [        U 5      (       aJ  U =R                  [
        R                  " U[
        R                  " U R                  5      5      -  sl        U $ [        U [        5      (       a  [        X5      n U $ XSS2[
        R                  4   -  n U $ )z`Compute diag(d) J.

If `copy` is False, `J` is modified in place (unless being LinearOperator).
N)rÃ   r   r—   r
   rô   r   ÚrepeatÚdiffÚindptrrÞ   rÓ   r÷   s      r   Úleft_multiplyrý   ¯  s�   € ö
 ”J˜q¤.×1Ñ1Ø�F‰F‹Hˆä�‡{�{Ø	�Š”"—)’)˜AœrŸwšw q§x¡xÓ0Ó1Ñ1�ð €Hô 
�A”~×	&Ñ	&Ü$ QÓ*ˆð €Hð 	
Šq”"—*‘*ˆ}ÑÑˆà€Hr   c                 óz   • XU-  :  =(       a    US:„  nX&Xc-   -  :  nU(       a  U(       a  gU(       a  gU(       a  gg)z8Check termination condition for nonlinear least squares.rU   é   r   r    NrV   )	ÚdFÚFÚdx_normÚx_normr>   ÚftolÚxtolÚftol_satisfiedÚxtol_satisfieds	            r   Úcheck_terminationr  Á  s<   € à ™(‘]×3 u¨t¡|€NØ t¡}Ñ5Ñ5€Næž.ØÞ	ØÞ	Øàr   c                 ó~   • US   SUS   -  US-  -  -   n[         X3[         :  '   US-  nXS   U-  -  n[        XSS9U4$ )zXScale Jacobian and residuals for a robust loss function.

Arrays are modified in place.
r,   r   r+   F)r—   )r-   rý   )r]   rN   ÚrhoÚJ_scales       r   Úscale_for_robust_loss_functionr  Ð  s[   € ð
 �!‰f�q˜3˜q™6‘z A q¡DÑ(Ñ(€GÜ €G”c‰MÑØ��O€GàˆQ‰�'Ñ	Ñ€Aä˜¨%Ñ0°!Ð3Ð3r   )Ng{®Gáz„?é
   )NN)r   rÎ   )g»½×Ùß|Û=)T).Ú__doc__Úmathr   Únumpyr   Únumpy.linalgr   Úscipy.linalgr   r   r   Úscipy.sparse.linalgr   r	   Úscipy._lib._sparser
   ÚfinfoÚfloatÚepsr-   r   r?   rS   r[   rb   rk   rq   rt   r‡   r•   rž   r£   rª   r´   r½   r¿   rÁ   rÅ   rË   rÞ   rç   rð   rø   rý   r  r  rV   r   r   Ú<module>r     sË   ðÙ 1Ý ã Ý ç ;Ñ ;ß @Ý 'ð 	‡h‚hˆuƒo×Ñ€ò$ðN AEØ/1ôoòd0òfô:0ôf&ô.$òT)ò
Hô:$ôNò6)òXòD.òcò!òYò$ô$ò+ò"+ò"Fô,ô$ò$ó4r   