File: nnls.py

package info (click to toggle)
python-scipy 0.7.2%2Bdfsg1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 28,572 kB
  • ctags: 36,183
  • sloc: cpp: 216,880; fortran: 76,016; python: 71,833; ansic: 62,118; makefile: 243; sh: 17
file content (41 lines) | stat: -rw-r--r-- 999 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import _nnls
from numpy import asarray_chkfinite, zeros, double

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