a
    a                     @   s4   d dl mZ ddlmZmZmZ dgZdddZdS )   )__nnls    )asarray_chkfinitezerosdoublennlsNc              
   C   s   t t| |f\} }t| jdkr4tdd| jf  t|jdkrVtdd|jf  | j\}}||jd krtdd	||jd ff  |d
u rdnt|}t|ftd}t|ftd}t|ftd}t	| |||||||\}}	}
|
dkrt
d||	fS )a  
    Solve ``argmin_x || Ax - b ||_2`` for ``x>=0``. This is a wrapper
    for a FORTRAN non-negative least squares solver.

    Parameters
    ----------
    A : ndarray
        Matrix ``A`` as shown above.
    b : ndarray
        Right-hand side vector.
    maxiter: int, optional
        Maximum number of iterations, optional.
        Default is ``3 * A.shape[1]``.

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

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

    Notes
    -----
    The FORTRAN code was published in the book below. The algorithm
    is an active set method. It solves the KKT (Karush-Kuhn-Tucker)
    conditions for the non-negative least squares problem.

    References
    ----------
    Lawson C., Hanson R.J., (1987) Solving Least Squares Problems, SIAM

     Examples
    --------
    >>> 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 %sr   z(Expected a one-dimensional array (vectorz, but the shape of b is %sr   z0Incompatible dimensions. The first dimension of z#A is %s, while the shape of b is %sN)Zdtypeztoo many iterations)mapr   lenshape
ValueErrorintr   r   r   r   RuntimeError)AbmaxitermnwzzindexxZrnormmode r   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/optimize/_nnls.pyr      s0    3


)N) r   Znumpyr   r   r   __all__r   r   r   r   r   <module>   s   