def LUdecomp(a, tol=1.0e-9):
    n = len(a)
    seq = array(range(n))

    # Set up scale factors
    s = zeros((n), type=Float64)
    for i in range(n):
        s[i] = max(abs(a[i, :]))

    for k in range(0, n - 1):

        # Row interchange, if needed
        p = int(argmax(abs(a[k:n, k]) / s[k:n])) + k
        if abs(a[p, k]) < tol:
            error.err("Matrix is singular")
        if p != k:
            swap.swapRows(s, k, p)
            swap.swapRows(a, k, p)
            swap.swapRows(seq, k, p)

        # Elimination
        for i in range(k + 1, n):
            if a[i, k] != 0.0:
                lam = a[i, k] / a[k, k]
                a[i, k + 1 : n] = a[i, k + 1 : n] - lam * a[k, k + 1 : n]
                a[i, k] = lam
    return a, seq
Exemple #2
0
def gaussJordanPivot(a, tol=1.0e-9):
    n = len(a)
    seq = array(range(n))

    # Set up scale factors
    s = zeros((n), type=Float64)
    for i in range(n):
        s[i] = max(abs(a[i, :]))

    for i in range(n):

        # Row interchange, if needed
        p = int(argmax(abs(a[i:n, i]) / s[i:n])) + i
        if abs(a[p, i]) < tol: error.err('Matrix is singular')
        if p != i:
            swap.swapRows(s, i, p)
            swap.swapRows(a, i, p)
            swap.swapRows(seq, i, p)

    # Elimination phase
        temp = a[i, i]
        a[i, i] = 1.0
        a[i, 0:n] = a[i, 0:n] / temp
        for k in range(n):
            if k != i:
                temp = a[k, i]
                a[k, i] = 0.0
                a[k, 0:n] = a[k, 0:n] - a[i, 0:n] * temp

# Rearrange columns in the right sequence
    for i in range(n):
        swap.swapCols(a, i, seq[i])
        swap.swapRows(seq, i, seq[i])
    return a
	def numarray_max(inputarray):
		'''
		faster than numarray.nd_image.max
		'''
		f = numarray.ravel(inputarray)
		i = numarray.argmax(f)
		return float(f[i])
Exemple #4
0
def powell(F, x, h=0.1, tol=1.0e-6):
    def f(s):
        return F(x + s * v)  # F in direction of v

    n = len(x)  # Number of design variables
    df = zeros((n), type=Float64)  # Decreases of F stored here
    u = identity(n) * 1.0  # Vectors v stored here by rows
    for j in range(30):  # Allow for 30 cycles:
        xOld = x.copy()  # Save starting point
        fOld = F(xOld)
        # First n line searches record decreases of F
        for i in range(n):
            v = u[i]
            a, b = bracket(f, 0.0, h)
            s, fMin = search(f, a, b)
            df[i] = fOld - fMin
            fOld = fMin
            x = x + s * v
    # Last line search in the cycle
        v = x - xOld
        a, b = bracket(f, 0.0, h)
        s, fLast = search(f, a, b)
        x = x + s * v
        # Check for convergence
        if sqrt(dot(x - xOld, x - xOld) / n) < tol: return x, j + 1
        # Identify biggest decrease & update search directions
        iMax = int(argmax(df))
        for i in range(iMax, n - 1):
            u[i] = u[i + 1]
            u[n - 1] = v
    print "Powell did not converge"
Exemple #5
0
def invwedge(q, n):
    ind = argmax(abs(q), 0)[0]
    q = q / q[ind]

    i, j = indtoij(ind)

    v1 = zeros((n, 1), Float)
    v2 = zeros((n, 1), Float)

    v1[i, 0] = 0
    v2[j, 0] = 0
    v1[j, 0] = 1
    v2[i, 0] = 1
    for k in range(0, i):
        if k != j:
            v1[k, 0] = q[ijtoind(i, k), 0]
    for k in range(i + 1, n):
        v1[k, 0] = -1 * q[ijtoind(k, i), 0]

    for k in range(0, j):
        v2[k, 0] = -1 * q[ijtoind(j, k), 0]
    for k in range(j + 1, n):
        if k != i:
            v2[k, 0] = q[ijtoind(k, j), 0]

    return v1, v2
Exemple #6
0
def gaussJordanPivot(a,tol=1.0e-9):
    n = len(a)
    seq = array(range(n))
    
  # Set up scale factors
    s = zeros((n),type=Float64)
    for i in range(n):
        s[i] = max(abs(a[i,:]))      
    
    for i in range(n):

      # Row interchange, if needed
        p = int(argmax(abs(a[i:n,i])/s[i:n])) + i
        if abs(a[p,i]) <  tol: error.err('Matrix is singular')
        if p != i:
            swap.swapRows(s,i,p)
            swap.swapRows(a,i,p)
            swap.swapRows(seq,i,p)
            
      # Elimination phase
        temp = a[i,i]
        a[i,i] = 1.0
        a[i,0:n] = a[i,0:n]/temp
        for k in range(n):
            if k != i: 
                temp = a[k,i]
                a[k,i] = 0.0
                a[k,0:n] = a[k,0:n] - a[i,0:n]*temp

  # Rearrange columns in the right sequence
    for i in range(n):
        swap.swapCols(a,i,seq[i])
        swap.swapRows(seq,i,seq[i])
    return a
Exemple #7
0
def LUdecomp(a,tol=1.0e-9):
    n = len(a)
    seq = array(range(n))
    
  # Set up scale factors
    s = zeros((n),type=Float64)
    for i in range(n):
        s[i] = max(abs(a[i,:]))        
    
    for k in range(0,n-1):
        
      # Row interchange, if needed
        p = int(argmax(abs(a[k:n,k])/s[k:n])) + k
        if abs(a[p,k]) <  tol: error.err('Matrix is singular')
        if p != k:
            swap.swapRows(s,k,p)
            swap.swapRows(a,k,p)
            swap.swapRows(seq,k,p)
            
      # Elimination            
        for i in range(k+1,n):
            if a[i,k] != 0.0:
                lam = a[i,k]/a[k,k]
                a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
                a[i,k] = lam
    return a,seq
Exemple #8
0
def powell(F,x,h=0.1,tol=1.0e-6):
    
    def f(s): return F(x + s*v)    # F in direction of v

    n = len(x)                     # Number of design variables
    df = zeros((n),type=Float64)   # Decreases of F stored here
    u = identity(n)*1.0            # Vectors v stored here by rows
    for j in range(30):            # Allow for 30 cycles:
        xOld = x.copy()            # Save starting point
        fOld = F(xOld)
      # First n line searches record decreases of F
        for i in range(n):
            v = u[i]
            a,b = bracket(f,0.0,h)
            s,fMin = search(f,a,b)
            df[i] = fOld - fMin
            fOld = fMin
            x = x + s*v
      # Last line search in the cycle    
        v = x - xOld
        a,b = bracket(f,0.0,h)
        s,fLast = search(f,a,b)
        x = x + s*v
      # Check for convergence
        if sqrt(dot(x-xOld,x-xOld)/n) < tol: return x,j+1
      # Identify biggest decrease & update search directions
        iMax = int(argmax(df))
        for i in range(iMax,n-1):
            u[i] = u[i+1]
            u[n-1] = v
    print "Powell did not converge"        
	def numarray_max(inputarray):
		'''
		faster than numarray.nd_image.max
		'''
		f = numarray.ravel(inputarray)
		i = numarray.argmax(f)
		return float(f[i])
def invwedge(q, n):
	ind = argmax(abs(q),0)[0]
	q = q/q[ind]
	
	i, j = indtoij(ind)
	
	v1 = zeros((n,1), Float)
	v2 = zeros((n,1), Float)
	
	v1[i,0] = 0
	v2[j,0] = 0
	v1[j,0] = 1
	v2[i,0] = 1
	for k in range(0,i):
		if k != j:
			v1[k,0] = q[ijtoind(i, k),0]
	for k in range(i+1,n):
		v1[k,0] = -1*q[ijtoind(k, i),0]
	
	for k in range(0,j):
		v2[k,0] = -1*q[ijtoind(j, k),0]
	for k in range(j+1,n):
		if k != i:
			v2[k,0] = q[ijtoind(k, j),0]
	
	return v1, v2
Exemple #11
0
def decode_mixture(P, entropy_cutoff):
    """ Given P, a (|sequences| x |models|)-matrix where P_{ij} =
    P[model j| sequence i] return a list of size (|sequences|)
    which contains j, the model which maximizes  P[model j| sequence i]
    for a sequence, if the entropy of the discrete distribution
    { P[ . | sequence i] } is less than the entropy_cutoff and None else.
    """
    nr_seqs = numarray.shape(P)[0]
    result = [None] * nr_seqs
    for i in xrange(nr_seqs):
        e = Entropy(P[i])
        #print e, P[i]
        if e < entropy_cutoff:
            result[i] = int(numarray.argmax(P[i]))
    return result
def decode_mixture(P, entropy_cutoff):
    """ Given P, a (|sequences| x |models|)-matrix where P_{ij} =
    P[model j| sequence i] return a list of size (|sequences|)
    which contains j, the model which maximizes  P[model j| sequence i]
    for a sequence, if the entropy of the discrete distribution
    { P[ . | sequence i] } is less than the entropy_cutoff and None else.
    """
    nr_seqs = numarray.shape(P)[0]
    result = [None] * nr_seqs
    for i in xrange(nr_seqs):
        e = Entropy(P[i])
        #print e, P[i]
        if e < entropy_cutoff:
            result[i] = int(numarray.argmax(P[i]))
    return result
Exemple #13
0
def matchum(file1,
            file2,
            tol=10,
            perr=4,
            aerr=1.0,
            nmax=40,
            im_masks1=[],
            im_masks2=[],
            debug=0,
            domags=0,
            xrange=None,
            yrange=None,
            sigma=4,
            aoffset=0):
    '''Take the output of two sextractor runs and match up the objects with
   each other (find out which objects in the first file match up with
   objects in the second file.  The routine considers a 'match' to be any 
   two objects that are closer than tol pixels (after applying the shift).  
   Returns a 6-tuple:  (x1,y1,x2,y2,o1,o2).  o1 and o2 are the ojbects
   numbers such that o1[i] in file 1 corresponds to o2[i] in file 2.'''
    NA = num.NewAxis

    sexdata1 = readsex(file1)
    sexdata2 = readsex(file2)

    # Use the readsex data to get arrays of the (x,y) positions
    x1 = num.asarray(sexdata1[0]['X_IMAGE'])
    y1 = num.asarray(sexdata1[0]['Y_IMAGE'])
    x2 = num.asarray(sexdata2[0]['X_IMAGE'])
    y2 = num.asarray(sexdata2[0]['Y_IMAGE'])
    m1 = num.asarray(sexdata1[0]['MAG_BEST'])
    m2 = num.asarray(sexdata2[0]['MAG_BEST'])
    o1 = num.asarray(sexdata1[0]['NUMBER'])
    o2 = num.asarray(sexdata2[0]['NUMBER'])
    f1 = num.asarray(sexdata1[0]['FLAGS'])
    f2 = num.asarray(sexdata2[0]['FLAGS'])

    # First, make a cut on the flags:
    gids = num.where(f1 < 4)
    x1 = x1[gids]
    y1 = y1[gids]
    m1 = m1[gids]
    o1 = o1[gids]
    gids = num.where(f2 < 4)
    x2 = x2[gids]
    y2 = y2[gids]
    m2 = m2[gids]
    o2 = o2[gids]

    # next, if there is a range to use:
    if xrange is not None and yrange is not None:
        cond = num.greater(x1, xrange[0])*num.less(x1,xrange[1])*\
              num.greater(y1, yrange[0])*num.less(y1,yrange[1])
        gids = num.where(cond)
        x1 = x1[gids]
        y1 = y1[gids]
        m1 = m1[gids]
        o1 = o1[gids]
        cond = num.greater(x2, xrange[0])*num.less(x2,xrange[1])*\
              num.greater(y2, yrange[0])*num.less(y2,yrange[1])
        gids = num.where(cond)
        x2 = x2[gids]
        y2 = y2[gids]
        m2 = m2[gids]
        o2 = o2[gids]

    # Use the user masks
    for m in im_masks1:
        print "applying mask (%d,%d,%d,%d)" % tuple(m)
        condx = num.less(x1, m[0]) + num.greater(x1, m[1])
        condy = num.less(y1, m[2]) + num.greater(y1, m[3])
        gids = num.where(condx + condy)
        x1 = x1[gids]
        y1 = y1[gids]
        m1 = m1[gids]
        o1 = o1[gids]

    for m in im_masks2:
        print "applying mask (%d,%d,%d,%d)" % tuple(m)
        condx = num.less(x2, m[0]) + num.greater(x2, m[1])
        condy = num.less(y2, m[2]) + num.greater(y2, m[3])
        gids = num.where(condx + condy)
        x2 = x2[gids]
        y2 = y2[gids]
        m2 = m2[gids]
        o2 = o2[gids]

    if nmax:
        if len(x1) > nmax:
            ids = num.argsort(m1)[0:nmax]
            x1 = x1[ids]
            y1 = y1[ids]
            m1 = m1[ids]
            o1 = o1[ids]
        if len(x2) > nmax:
            ids = num.argsort(m2)[0:nmax]
            x2 = x2[ids]
            y2 = y2[ids]
            m2 = m2[ids]
            o2 = o2[ids]
    if debug:
        print "objects in frame 1:"
        print o1
        print "objects in frame 2:"
        print o2
        mp = pygplot.MPlot(2, 1, device='/XWIN')
        p = pygplot.Plot()
        p.point(x1, y1)
        [p.label(x1[i], y1[i], "%d" % o1[i]) for i in range(len(x1))]
        mp.add(p)
        p = pygplot.Plot()
        p.point(x2, y2)
        [p.label(x2[i], y2[i], "%d" % o2[i]) for i in range(len(x2))]
        mp.add(p)
        mp.plot()
        mp.close()

    # Now, we make 2-D arrays of all the differences in x and y between each pair
    #  of objects.  e.g., dx1[n,m] is the delta-x between object n and m in file 1 and
    #  dy2[n,m] is the y-distance between object n and m in file 2.
    dx1 = x1[NA, :] - x1[:, NA]
    dx2 = x2[NA, :] - x2[:, NA]
    dy1 = y1[NA, :] - y1[:, NA]
    dy2 = y2[NA, :] - y2[:, NA]
    # Same, but with angles
    da1 = num.arctan2(dy1, dx1) * 180 / num.pi
    da2 = num.arctan2(dy2, dx2) * 180 / num.pi
    # Same, but with absolute distances
    ds1 = num.sqrt(num.power(dx1, 2) + num.power(dy1, 2))
    ds2 = num.sqrt(num.power(dx2, 2) + num.power(dy2, 2))

    # Here's the real magic:  this is a matrix of matrices (4-D).  Consider 4 objects:
    #  objects i and j in file 1 and objects m and n in file 2.  dx[i,j,m,n] is the
    #  difference between delta-xs for objects i,j in file 1 and m,n in file 2.  If object
    #  i corresponds to object m and object j corresponds to object n, this should be a small
    #  number, irregardless of an overall shift in coordinate systems between file 1 and 2.
    dx = dx1[::, ::, NA, NA] - dx2[NA, NA, ::, ::]
    dy = dy1[::, ::, NA, NA] - dy2[NA, NA, ::, ::]
    da = da1[::, ::, NA, NA] - da2[NA, NA, ::, ::] + aoffset
    ds = ds1[::, ::, NA, NA] - ds2[NA, NA, ::, ::]
    # pick out close pairs.
    #use = num.less(dy,perr)*num.less(dx,perr)*num.less(num.abs(da),aerr)
    use = num.less(ds, perr) * num.less(num.abs(da), aerr)
    use = use.astype(num.Int32)

    #use = num.less(num.abs(da),perr)
    suse = num.add.reduce(num.add.reduce(use, 3), 1)
    print suse[0]

    guse = num.greater(suse, suse.flat.max() / 2)
    i = [j for j in range(x1.shape[0]) if num.sum(guse[j])]
    m = [num.argmax(guse[j]) for j in range(x1.shape[0]) if num.sum(guse[j])]
    xx0, yy0, oo0, mm0 = num.take([x1, y1, o1, m1], i, 1)
    xx1, yy1, oo1, mm1 = num.take([x2, y2, o2, m2], m, 1)
    if debug:
        mp = pygplot.MPlot(2, 1, device='/XWIN')
        p = pygplot.Plot()
        p.point(xx0, yy0)
        [p.label(xx0[i], yy0[i], "%d" % oo0[i]) for i in range(len(xx0))]
        mp.add(p)
        p = pygplot.Plot()
        p.point(xx1, yy1)
        [p.label(xx1[i], yy1[i], "%d" % oo1[i]) for i in range(len(xx1))]
        mp.add(p)
        mp.plot()
        mp.close()
    xshift, xscat = stats.bwt(xx0 - xx1)
    xscat = max([1.0, xscat])
    yshift, yscat = stats.bwt(yy0 - yy1)
    yscat = max([1.0, yscat])
    mshift, mscat = stats.bwt(mm0 - mm1)
    print "xscat = ", xscat
    print "yscat = ", yscat
    print "xshift = ", xshift
    print "yshift = ", yshift
    print "mshift = ", mshift
    print "mscat = ", mscat
    keep = num.less(num.abs(xx0-xx1-xshift),sigma*xscat)*\
          num.less(num.abs(yy0-yy1-yshift),sigma*yscat)
    # This is a list of x,y,object# in each file.
    xx0, yy0, oo0, xx1, yy1, oo1 = num.compress(keep,
                                                [xx0, yy0, oo0, xx1, yy1, oo1],
                                                1)

    if debug:
        print file1, oo0
        print file2, oo1
        mp = pygplot.MPlot(2, 1, device='temp.ps/CPS')
        p1 = pygplot.Plot()
        p1.point(xx0, yy0, symbol=25, color='red')
        for i in range(len(xx0)):
            p1.label(xx0[i], yy0[i], " %d" % oo0[i], color='red')
        mp.add(p1)
        p2 = pygplot.Plot()
        p2.point(xx1, yy1, symbol=25, color='green')
        for i in range(len(xx1)):
            p2.label(xx1[i], yy1[i], " %d" % oo1[i], color='green')
        mp.add(p2)
        mp.plot()
        mp.close()

    if domags:
        return (xx0, yy0, mm0, xx1, yy1, mm1, mshift, mscat, oo0, oo1)
    else:
        return (xx0, yy0, xx1, yy1, oo0, oo1)
def createA(n): #function to create matrix A
    A = np.empty((n,n), order='F') #declare an empty array of nxn dimension
    for i in range(n):
        for j in range(n):
             A[i,j]= -1+2*argmax([i,j])
    return A