def nnls(A, b):
    A, b = map(np.asarray_chkfinite, (A, b))

    #if len(A.shape) != 2:
    #    raise ValueError("expected matrix")
    #if len(b.shape) != 1:
    #    raise ValueError("expected vector")

    m, n = A.shape

    #if m != b.shape[0]:
    #    raise ValueError("incompatible dimensions")

    #maxiter = -1 if maxiter is None else int(maxiter)
    maxiter = -1
    #maxiter = int(5*n)

    w = np.zeros((n, ), dtype=np.double)
    zz = np.zeros((m, ), dtype=np.double)
    index = np.zeros((n, ), dtype=int)

    x, rnorm, mode = _nnls.nnls(A, m, n, b, w, zz, index, maxiter)

    #if mode != 1:
    #    raise RuntimeError("too many iterations")
    return x, rnorm
예제 #2
0
def nnls(A, b):
    """
    Solve ``argmin_x || Ax - b ||_2`` for ``x>=0``. This is a wrapper
    for a FORTAN non-negative least squares solver.

    Parameters
    ----------
    A : ndarray
        Matrix ``A`` as shown above.
    b : ndarray
        Right-hand side vector.

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

    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

    """

    A, b = map(asarray_chkfinite, (A, b))

    if len(A.shape) != 2:
        raise ValueError("expected matrix")
    if len(b.shape) != 1:
        raise ValueError("expected vector")

    m, n = A.shape

    if m != b.shape[0]:
        raise ValueError("incompatible dimensions")

    w = zeros((n, ), dtype=double)
    zz = zeros((m, ), dtype=double)
    index = zeros((n, ), dtype=int)

    x, rnorm, mode = _nnls.nnls(A, m, n, b, w, zz, index)
    if mode != 1:
        raise RuntimeError("too many iterations")

    return x, rnorm
예제 #3
0
파일: nnls.py 프로젝트: GaelVaroquaux/scipy
def nnls(A,b):
    """
    Solve ``argmin_x || Ax - b ||_2`` for ``x>=0``. This is a wrapper
    for a FORTAN non-negative least squares solver. 

    Parameters
    ----------
    A : ndarray
        Matrix ``A`` as shown above.
    b : ndarray
        Right-hand side vector.

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

    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 

    """

    A,b = map(asarray_chkfinite, (A,b))

    if len(A.shape)!=2:
        raise ValueError("expected matrix")
    if len(b.shape)!=1:
        raise ValueError("expected vector")

    m,n = A.shape

    if m != b.shape[0]:
        raise ValueError("incompatible dimensions")

    w   = zeros((n,), dtype=double)
    zz  = zeros((m,), dtype=double)
    index=zeros((n,), dtype=int)

    x,rnorm,mode = _nnls.nnls(A,m,n,b,w,zz,index)
    if mode != 1:
        raise RuntimeError("too many iterations")

    return x, rnorm
예제 #4
0
def nnls(A, b):
    """
    Solve ``argmin_x || Ax - b ||_2`` for ``x>=0``.

    Parameters
    ----------
    A : ndarray
        Matrix ``A`` as shown above.
    b : ndarray
        Right-hand side vector.

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

    Notes
    -----
    This is a wrapper for ``NNLS.F``.

    """

    A, b = map(asarray_chkfinite, (A, b))

    if len(A.shape) != 2:
        raise ValueError("expected matrix")
    if len(b.shape) != 1:
        raise ValueError("expected vector")

    m, n = A.shape

    if m != b.shape[0]:
        raise ValueError("incompatible dimensions")

    w = zeros((n, ), dtype=double)
    zz = zeros((m, ), dtype=double)
    index = zeros((n, ), dtype=int)

    x, rnorm, mode = _nnls.nnls(A, m, n, b, w, zz, index)
    if mode != 1:
        raise RuntimeError("too many iterations")

    return x, rnorm
예제 #5
0
def nnls(A,b):
    """
    Solve ``argmin_x || Ax - b ||_2`` for ``x>=0``.

    Parameters
    ----------
    A : ndarray
        Matrix ``A`` as shown above.
    b : ndarray
        Right-hand side vector.

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

    Notes
    -----
    This is a wrapper for ``NNLS.F``.

    """

    A,b = map(asarray_chkfinite, (A,b))

    if len(A.shape)!=2:
        raise ValueError("expected matrix")
    if len(b.shape)!=1:
        raise ValueError("expected vector")

    m,n = A.shape

    if m != b.shape[0]:
        raise ValueError("incompatible dimensions")

    w   = zeros((n,), dtype=double)
    zz  = zeros((m,), dtype=double)
    index=zeros((n,), dtype=int)

    x,rnorm,mode = _nnls.nnls(A,m,n,b,w,zz,index)
    if mode != 1:
        raise RuntimeError("too many iterations")

    return x, rnorm
예제 #6
0
def nnls(A, b):
    """ 
          Solve  || Ax - b ||_2 -> min  with  x>=0 

          Inputs:
                    A     --   matrix as above
                    b     --   vector as above

          Outputs:
                    x     --   solution vector
                    rnorm --  residual || Ax-b ||_2


    wrapper around NNLS.F code below nnls/ directory

    """

    A, b = map(asarray_chkfinite, (A, b))

    if len(A.shape) != 2:
        raise ValueError, "expected matrix"
    if len(b.shape) != 1:
        raise ValueError, "expected vector"

    m, n = A.shape

    if m != b.shape[0]:
        raise ValueError, "incompatible dimensions"

    w = zeros((n, ), dtype=double)
    zz = zeros((m, ), dtype=double)
    index = zeros((n, ), dtype=int)

    x, rnorm, mode = _nnls.nnls(A, m, n, b, w, zz, index)
    if mode != 1: raise RuntimeError, "too many iterations"

    return x, rnorm
예제 #7
0
def nnls(A,b):
    """ 
          Solve  || Ax - b ||_2 -> min  with  x>=0 

          Inputs:
                    A     --   matrix as above
                    b     --   vector as above

          Outputs:
                    x     --   solution vector
                    rnorm --  residual || Ax-b ||_2


    wrapper around NNLS.F code below nnls/ directory

    """

    A,b = map(asarray_chkfinite, (A,b))

    if len(A.shape)!=2:
        raise ValueError, "expected matrix"
    if len(b.shape)!=1:
        raise ValueError, "expected vector"
        
    m,n = A.shape

    if m != b.shape[0]:
        raise ValueError, "incompatible dimensions"

    w   = zeros((n,), dtype=double)
    zz  = zeros((m,), dtype=double)
    index=zeros((n,), dtype=int)

    x,rnorm,mode = _nnls.nnls(A,m,n,b,w,zz,index)
    if mode != 1: raise RuntimeError, "too many iterations"

    return x, rnorm