
    tKgl                     @    d dl Zd dlmZmZ d dlZdgZddddZddZy)	    N)solveLinAlgWarningnnls)atolc                   t        j                  |       } t        j                  |      }t        | j                        dk7  rt	        dd| j                   z         t        |j                        dk7  rt	        dd|j                   z         | j                  \  }}||j                  d   k7  r"t	        dd	| d
|j                  d   f z         t        | |||      \  }}}|dk7  rt        d      ||fS )a  
    Solve ``argmin_x || Ax - b ||_2`` for ``x>=0``.

    This problem, often called as NonNegative Least Squares, is a convex
    optimization problem with convex constraints. It typically arises when
    the ``x`` models quantities for which only nonnegative values are
    attainable; weight of ingredients, component costs and so on.

    Parameters
    ----------
    A : (m, n) ndarray
        Coefficient array
    b : (m,) ndarray, float
        Right-hand side vector.
    maxiter: int, optional
        Maximum number of iterations, optional. Default value is ``3 * n``.
    atol: float
        Tolerance value used in the algorithm to assess closeness to zero in
        the projected residual ``(A.T @ (A x - b)`` entries. Increasing this
        value relaxes the solution constraints. A typical relaxation value can
        be selected as ``max(m, n) * np.linalg.norm(a, 1) * np.spacing(1.)``.
        This value is not set as default since the norm operation becomes
        expensive for large problems hence can be used only when necessary.

    Returns
    -------
    x : ndarray
        Solution vector.
    rnorm : float
        The 2-norm of the residual, ``|| Ax-b ||_2``.

    See Also
    --------
    lsq_linear : Linear least squares with bounds on the variables

    Notes
    -----
    The code is based on [2]_ which is an improved version of the classical
    algorithm of [1]_. It utilizes an active set method and solves the KKT
    (Karush-Kuhn-Tucker) conditions for the non-negative least squares problem.

    References
    ----------
    .. [1] : Lawson C., Hanson R.J., "Solving Least Squares Problems", SIAM,
       1995, :doi:`10.1137/1.9781611971217`
    .. [2] : Bro, Rasmus and de Jong, Sijmen, "A Fast Non-Negativity-
       Constrained Least Squares Algorithm", Journal Of Chemometrics, 1997,
       :doi:`10.1002/(SICI)1099-128X(199709/10)11:5<393::AID-CEM483>3.0.CO;2-L`

     Examples
    --------
    >>> import numpy as np
    >>> from scipy.optimize import nnls
    ...
    >>> A = np.array([[1, 0], [1, 0], [0, 1]])
    >>> b = np.array([2, 1, 1])
    >>> nnls(A, b)
    (array([1.5, 1. ]), 0.7071067811865475)

    >>> b = np.array([-1, -1, -1])
    >>> nnls(A, b)
    (array([0., 0.]), 1.7320508075688772)

       z)Expected a two-dimensional array (matrix)z, but the shape of A is    z)Expected a one-dimensional array (vector)z, but the shape of b is r   z0Incompatible dimensions. The first dimension of zA is z, while the shape of b is )tolz%Maximum number of iterations reached.)npasarray_chkfinitelenshape
ValueError_nnlsRuntimeError)	Abmaxiterr   mnxrnormmodes	            X/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/scipy/optimize/_nnls.pyr   r      s   D 	QA
QA
177|qD3AGG9=> ? 	?
177|qD3AGG9=> ? 	? 77DAqAGGAJBs4aggaj^4DEFG 	G 1ad3NAudqyBCCe8O    c                    | j                   \  }}| j                  | z  }|| z  }|sd|z  }|%dt        ||      z  t        j                  d      z  }t        j
                  |t        j                        }t        j
                  |t        j                        }	t        j
                  |t              }
|j                         j                  t        j                        }d}|
j                         s||
    |kD  j                         rt        j                  ||
 z        }d|
|<   d|	dd t        j                         5  t        j                  d	d
t                t#        |t        j$                  |
|
         ||
   dd      |	|
<   ddd       ||k  r|	|
   j'                         dk  r|dz  }|
|	dk  z  }||   ||   |	|   z
  z  j'                         }|d|z
  z  }|||	z  z  }d|
||k  <   t        j                         5  t        j                  d	d
t                t#        |t        j$                  |
|
         ||
   dd      |	|
<   ddd       d|	|
 <   ||k  r|	|
   j'                         dk  r|	dd |dd |||z  z
  |dd ||k(  r|ddfS |
j                         s||
    |kD  j                         r|t        j(                  j+                  | |z  |z
        dfS # 1 sw Y   ^xY w# 1 sw Y   xY w)z
    This is a single RHS algorithm from ref [2] above. For multiple RHS
    support, the algorithm is given in  :doi:`10.1002/cem.889`
       N
   g      ?)dtyper   Tg        ignorezIll-conditioned matrix)messagecategorysymF)assume_acheck_finiter	   )r   Tmaxr   spacingzerosfloat64boolcopyastypeallanyargmaxwarningscatch_warningsfilterwarningsr   r   ix_minlinalgnorm)r   r   r   r
   r   r   AtAAtbr   sPwiterkindsalphas                   r   r   r   b   s   
 77DAq
##'C
a%CA#
{3q!9nrzz"~- 	"**%A
"**%A
$A 	
"**%A DuuwQrUS[--/IIaA2h! !$$&##H6N-:<RVVAq\*CFUQVWAaD ' g~AaDHHJNAIDA;DtW$!D' 12779E!e)AqLAAa3hK((*'':R1>@S1.A*/1! +
 AqbE g~AaDHHJN t!S1W}!7?
 b"9I uuwQrUS[--/L biinnQqS1W%q((= '& +*s   A	K+
A	K8+K58L)N)NN)	numpyr   scipy.linalgr   r   r2   __all__r   r    r   r   <module>rF      s*     - (WT WtB)r   