
    4[g                         d Z ddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd
Zd Zd ZdddZ y)zWThe adaptation of Trust Region Reflective algorithm for a linear
least-squares problem.    N)norm)qrsolve_triangular)lsmr)OptimizeResult   )givens_elimination)EPSstep_size_to_boundfind_active_constraints	in_boundsmake_strictly_feasiblebuild_quadratic_1devaluate_quadraticminimize_quadratic_1dCL_scaling_vectorreflective_transformationprint_header_linearprint_iteration_linearcompute_gradregularized_lsq_operatorright_multiplied_operatorc                    |r|j                         }|j                         }t        ||||          t        j                  t        j                  |            }t
        t        | |      z  t        j                  |      z  }	t        j                  ||	kD        \  }
|t        j                  |
|
         }||
   }t        j                  |      }t        ||      |||
   <   |S )a  Solve regularized least squares using information from QR-decomposition.

    The initial problem is to solve the following system in a least-squares
    sense::

        A x = b
        D x = 0

    where D is diagonal matrix. The method is based on QR decomposition
    of the form A P = Q R, where P is a column permutation matrix, Q is an
    orthogonal matrix and R is an upper triangular matrix.

    Parameters
    ----------
    m, n : int
        Initial shape of A.
    R : ndarray, shape (n, n)
        Upper triangular matrix from QR decomposition of A.
    QTb : ndarray, shape (n,)
        First n components of Q^T b.
    perm : ndarray, shape (n,)
        Array defining column permutation of A, such that ith column of
        P is perm[i]-th column of identity matrix.
    diag : ndarray, shape (n,)
        Array containing diagonal elements of D.

    Returns
    -------
    x : ndarray, shape (n,)
        Found least-squares solution.
    )copyr	   npabsdiagr
   maxnonzeroix_zerosr   )mnRQTbpermr   copy_Rv
abs_diag_R	thresholdnnsxs               Y/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/optimize/_lsq/trf_linear.pyregularized_lsq_with_qrr.      s    @ FFH
Aq!T$Z(
#Jc!Qi"&&"44I::j9,-DC	"&&c
A	#A
A#Aq)Ad3iLH    c                 H   d}	 t        |||z  z   ||      \  }	}
|	|z
  }t        | ||       }|d|z  |z  kD  rn|dz  };t        |	||      }t        j                  |dk7        r;t        |||z  |z  z   ||      \  }	}
t        |	||d      }	|	|z
  }t        | ||       }|||fS )z=Find an appropriate step size using backtracking line search.r   g      ?r   rstep)r   r   r   r   anyr   )Agr,   pthetap_dot_glbubalphax_new_stepcost_changeactives                 r-   backtrackingrB   E   s    E
,Q]BCqqy)!Q55//  %UB3F	vvfk,Q1B-BBKq&ub"A>qy)!Q55dKr/   c
                    t        | |z   ||      r|S t        | |||      \  }
}t        j                  |      }||j	                  t
              xx   dz  cc<   ||z  }||
z  }||
z  }| |z   }t        ||||      \  }}d|	z
  |z  }||	z  }|dkD  r5t        |||||      \  }}}t        |||||      \  }}|||z  z   }||z  }nt        j                  }||	z  }||	z  }t        ||||      }| }||z  }t        | |||      \  }}||	z  }t        ||||      \  }}t        ||d|      \  }}||z  }||k  r||k  r|S ||k  r||k  r|S |S )zDSelect the best step according to Trust Region Reflective algorithm.r   r   )s0r   )c)r   )
r   r   r   r   astypeboolr   r   infr   )r,   A_hg_hc_hr7   p_hdr:   r;   r8   p_stridehitsr_hr
x_on_bound
r_stride_ur>   
r_stride_labrF   r_strider_valuep_valueag_hagag_stride_u	ag_strideag_values                                r-   select_stepr`   Z   s   QB'1b"5NHd
''#,CDb 	CA MA8OCQJ 'z1b"=MJ e)z)J%JA~$S#ssE1a1q*jA/'C(N"G&& 5LCJA c3S9G4D	
TB'2r26NK5Kc337DAq/1aEIx)OBWx/	7	w1	r/   )lsmr_maxiterc
                `   | j                   \  }}t        |||      \  }}t        |||d      }|dk(  rtt        | dd      \  }}}|j                  }||k  r/t        j                  |t        j                  ||z
  |f      f      }t        j                  |      }t        ||      }n.|dk(  r)t        j                  ||z         }d}|d	|z  }n|d
k(  rd}| j                  |      |z
  }t        | |      }dt        j                  ||      z  }|}d }d }d }|d}|	dk(  r
t                t        |      D ]  }t        ||||      \  }}||z  } t        | t
        j                        }!|!|k  rd}|	dk(  rt!        |||||!       | n}||z  }"|"dz  }#|dz  }$|$|z  }%t#        | |$      }&|dk(  r.j                  |      d  t%        |||$   z  |||#d       }'nX|dk(  rSt'        |&|#      }(|d | r,d	t        d|!      z  })t)        t*        t        d|)|!z              }t-        |(||
||      d    }'|$'z  }*t        j                  |*|      }+|+dkD  rd}dt        d|!      z
  },t/        ||&|%|"|*|'|$|||,
      }-t1        | ||-       }|dk  rt3        | |||*|,|+||      \  }}-}nt        ||-z   ||d      }t        |-      }| j                  |      |z
  }t        | |      }|||z  k  rd}dt        j                  ||      z  } |d}t5        ||||      }.t7        |||!|.dz   ||      S )Ng?r2   exacteconomicT)modepivotingr   Fg{Gz?autor1   d      )ordr   )r'   )maxiteratolbtolr   rD   g{Gzt?)rtol)r,   funcost
optimalityactive_masknitstatusinitial_cost)shaper   r   r   Tr   vstackr!   mindotr   r   ranger   r   rI   r   r   r.   r   r   r
   r   r`   r   rB   r   r   )/r5   rW   x_lsqr:   r;   tol
lsq_solverlsmr_tolmax_iterverbosera   r"   r#   r,   r>   QTr$   r&   QTrkr_augauto_lsmr_tolrR   r6   rp   ru   termination_status	step_normr@   	iterationr(   dvg_scaledg_normdiag_hdiag_root_hrN   rK   rJ   rM   lsmr_opetar7   r9   r8   r?   rr   s/                                                  r-   
trf_linearr      s   77DAq$UB3DAqq"b4AWd;AtTTq5		1bhhAqz234Ahhqk1I	v	QczH M	a1AQA1DLIK!|8_	!!QB/2q5hBFF+C<!"a<"9dK#,f6 )RmH!e'1- ffQiCG*1aQtWc4+6uF FC6!.sK@GE"1ISf--sCS6\$:;%H6679 9C G&&A,Q;!#Cv&&1c332r5I)!Q55
 ?#/1aE7B$4 At[ 'q4xRqAAJ	EE!HqLAt#!"RVVAq\!w %z !)!R#>K
&kM"4!# #r/   )T)!__doc__numpyr   numpy.linalgr   scipy.linalgr   r   scipy.sparse.linalgr   scipy.optimizer   r	   commonr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r.   rB   r`   r    r/   r-   <module>r      sQ      - $ ) 29 9 9 9 90f *1j 37k#r/   