Ejemplo n.º 1
0
    def setdata(self, X, V):
        A = self.bialtprodeye(2*self.F.J_coords)
        """Note: p, q <= min(n,m)"""

        self.data.Brand = 2*(random((A.shape[0],self.data.p))-0.5)
        self.data.Crand = 2*(random((A.shape[1],self.data.q))-0.5)
        self.data.B = zeros((A.shape[0],self.data.p), Float)
        self.data.C = zeros((A.shape[1],self.data.q), Float)
        self.data.D = zeros((self.data.q,self.data.p), Float)

        U, S, Vh = linalg.svd(A)
        self.data.b = U[:,-1:]
        self.data.c = num_transpose(Vh)[:,-1:]
        
        if self.update:
            self.data.B[:,1] = self.data.b
            self.data.C[:,1] = self.data.c
            
            U2, S2, Vh2 = linalg.svd(c_[r_[A, transpose(self.data.C[:,1])], r_[self.data.B[:,1], [[0]]]])
            self.data.B[:,2] = U2[0:A.shape[0],-1:]
            self.data.C[:,2] = num_transpose(Vh2)[0:A.shape[1],-1:]
            self.data.D[0,1] = U2[A.shape[0],-1]
            self.data.D[1,0] = num_transpose(Vh2)[A.shape[1],-1]
        else:
            self.data.B = self.data.Brand
            self.data.C = self.data.Crand
Ejemplo n.º 2
0
def binitbinsfix(binsleft, binsright, x, y):  #give bins for binning
    nbin = len(binsleft)
    xbin = N.zeros(len(binsleft), 'f')
    ybin = N.zeros(len(binsleft), 'f')
    ybinerr = N.zeros(len(binsleft), 'f')
    y = N.take(y, N.argsort(x))  #sort y according to x rankings
    x = N.take(x, N.argsort(x))

    j = -1
    for i in range(len(xbin)):
        xmin = binsleft[i]
        xmax = binsright[i]
        yb = []
        for j in range(len(x)):
            if x[j] > xmin:
                yb.append(y[j])
            if x[j] > xmax:
                yb = N.array(yb, 'f')
                xbin[i] = 0.5 * (xmin + xmax)
                try:
                    ybin[i] = pylab.median(yb)
                    ybinerr[i] = pylab.std(yb) / N.sqrt(1. * len(yb))
                except ZeroDivisionError:
                    print "warning: ZeroDivision error in binitbinsfix"
                    ybin[i] = 0.
                    ybinerr[i] = 0.

                break

    return xbin, ybin, ybinerr
Ejemplo n.º 3
0
def binitbins(xmin, xmax, nbin, x, y):  #use equally spaced bins
    dx = float((xmax - xmin) / (nbin))
    xbin = N.arange(xmin, (xmax), dx) + dx / 2.
    #print "within binitbins"
    #print "xbin = ",xbin
    #print "dx = ",dx
    #print "xmax = ",xmax
    #print "xmin = ",xmin
    ybin = N.zeros(len(xbin), 'd')
    ybinerr = N.zeros(len(xbin), 'd')
    xbinnumb = N.array(len(x), 'd')
    x1 = N.compress((x >= xmin) & (x <= xmax), x)
    y1 = N.compress((x >= xmin) & (x <= xmax), y)
    x = x1
    y = y1
    xbinnumb = ((x - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    j = -1
    for i in range(len(xbin)):
        ydata = N.compress(abs(xbinnumb - float(i)) < .5, y)
        try:
            ybin[i] = N.average(ydata)
            #ybin[i]=pylab.median(ydata)
            ybinerr[i] = pylab.std(ydata) / N.sqrt(float(len(ydata)))
        except ZeroDivisionError:
            ybin[i] = 0.
            ybinerr[i] = 0.
    return xbin, ybin, ybinerr
Ejemplo n.º 4
0
def horizontalhist(x, xmin, xmax, nbin):  #give bins for binning
    #nbin=len(bins)
    xbin = N.zeros(nbin, 'f')
    ybin = N.zeros(nbin, 'f')
    ybinerr = N.zeros(nbin, 'f')
    dbin = (xmax - xmin) / (1. * nbin)
    bins = N.arange(xmin, (xmax + dbin), dbin)
    x = N.take(x, N.argsort(x))
    xmin = min(bins)
    xmax = max(bins)
    xbinnumb = ((x - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    #print "from within horizontal hist"
    #print "bins = ",bins
    for i in range(len(xbin) - 1):
        xmin = bins[i]
        xmax = bins[i + 1]
        xbin[i] = xmin
        yb = 0.
        for j in range(len(x)):
            if (x[j] > xmin) & (x[j] <= xmax):
                yb = yb + 1.
        ybin[i] = yb
        ybinerr[i] = N.sqrt(yb)
        #print i,len(xbin),xbin[i],xmin,xmax,ybin[i],bins[i]
    xbin[(len(xbin) - 1)] = xbin[(len(xbin) - 2)] + dbin
    #xbin=bins
    #print "from w/in horizontal hist, ybin = ",ybin
    return xbin, ybin, ybinerr
Ejemplo n.º 5
0
def verticalhist(y, ymin, ymax, nbin):  #give bins for binning
    #nbin=len(bins)
    xbin = N.zeros(nbin, 'f')
    ybin = N.zeros(nbin, 'f')
    ybinerr = N.zeros(nbin, 'f')
    dbin = (ymax - ymin) / (1. * nbin)
    bins = N.arange(ymin, (ymax + dbin), dbin)
    y = N.take(y, N.argsort(y))
    xmin = min(bins)
    xmax = max(bins)
    xbinnumb = ((y - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    for i in range(len(xbin) - 1):
        xmin = bins[i]
        xmax = bins[i + 1]
        yb = 0.
        for j in range(len(y)):
            if y[j] > xmin:
                yb = yb + 1.
            if y[j] > xmax:
                ybin[i] = yb
                ybinerr[i] = N.sqrt(yb)
                #xbin[i]=0.5*(xmin+xmax)
                xbin[i] = xmax
                break
    drawhist(ybin, xbin)
Ejemplo n.º 6
0
def scipyhist2(bins, y):  #give bins for binning
    nbin = len(bins)
    xbin = N.zeros(len(bins), 'f')
    ybin = N.zeros(len(bins), 'f')
    ybinerr = N.zeros(len(bins), 'f')
    y = N.take(y, N.argsort(y))
    xmin = min(bins)
    xmax = max(bins)
    xbinnumb = ((y - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    for i in range(len(xbin) - 1):
        xmin = bins[i]
        xmax = bins[i + 1]
        yb = 0.
        for j in range(len(y)):
            if y[j] > xmin:
                yb = yb + 1.
            if y[j] > xmax:
                ybin[i] = yb
                ybinerr[i] = N.sqrt(yb)
                xbin[i] = 0.5 * (xmin + xmax)

                break

    return xbin, ybin, ybinerr
Ejemplo n.º 7
0
def load_svd(sfile, ufile, vfile):
    """loads dense svd files as output by svdlibc"""
    n_s = int(sfile.readline())
    S = na.zeros((n_s, n_s), type="Float32") #store S as a column vector
    for i in range(n_s):
        S[i,i] = float(sfile.readline())
    assert sfile.readline() == ''
    
    rows, columns = [int(a) for a in ufile.readline().split()]
    U = na.zeros((rows, columns), type="Float32")
    row = 0
    for line in ufile:
        col = 0
        for n in line.split():
            U[row, col] = float(n)
            col += 1
        row += 1

    rows, columns = [int(a) for a in vfile.readline().split()]
    V = na.zeros((rows, columns), type="Float32")
    row = 0
    for line in vfile:
        col = 0
        for n in line.split():
            V[row, col] = float(n)
            col += 1
        row += 1

    return U, S, V
Ejemplo n.º 8
0
def histOutline(dataIn, binsIn=None):
    """
    Make a histogram that can be plotted with plot() so that
    the histogram just has the outline rather than bars as it
    usually does.
    """
    if (binsIn == None):
        (en, eb) = pylab.matplotlib.mlab.hist(dataIn, bins=50, normed=True)
        binsIn = eb
    else:
        (en, eb) = pylab.matplotlib.mlab.hist(dataIn, bins=binsIn)

    stepSize = binsIn[1] - binsIn[0]

    bins = numarray.zeros(len(eb)*2 + 2, type=numarray.Float)
    data = numarray.zeros(len(eb)*2 + 2, type=numarray.Float)
    for bb in range(len(binsIn)):
        bins[2*bb + 1] = binsIn[bb]
        bins[2*bb + 2] = binsIn[bb] + stepSize
        data[2*bb + 1] = en[bb]
        data[2*bb + 2] = en[bb]

    bins[0] = bins[1]
    bins[-1] = bins[-2]
    data[0] = 0
    data[-1] = 0

    return (bins, data)
Ejemplo n.º 9
0
def buildSpearmanCorrelationMatrix(traits, sc):
    dim = (len(traits), sc)
    matrix = numarray.zeros(dim, MA.Float64)
    testMatrix = numarray.zeros(dim, MA.Float64)

    def customCmp(a, b):
        return cmp(a[1], b[1])
    
    for i in range(len(traits)):
        # copy strain data to a temporary list and turn it into
        # (strain, expression) pairs
        sd = traits[i].strainData
        tempList = []
        for key in sd.keys():
            tempList.append((key, sd[key]))

        # sort the temporary list by expression
        tempList.sort(customCmp)
        
        for j in range(len(tempList)):
            # k is the strain id minus 1
            # 1-based strain id -> 0-based column index
            k = int(tempList[j][0]) - 1

            # j is the rank of the particular strain
            matrix[i,k] = j

            testMatrix[i,k] = 1

    return matrix, testMatrix
Ejemplo n.º 10
0
def gaussNodes(m, tol=10e-9):
    def legendre(t, m):
        p0 = 1.0
        p1 = t
        for k in range(1, m):
            p = ((2.0 * k + 1.0) * t * p1 - k * p0) / (1.0 + k)
            p0 = p1
            p1 = p
        dp = m * (p0 - t * p1) / (1.0 - t**2)
        return p, dp

    A = zeros((m), type=Float64)
    x = zeros((m), type=Float64)
    nRoots = (m + 1) / 2  # Number of non-neg. roots
    for i in range(nRoots):
        t = cos(pi * (i + 0.75) / (m + 0.5))  # Approx. root
        for j in range(30):
            p, dp = legendre(t, m)  # Newton-Raphson
            dt = -p / dp
            t = t + dt  # method
            if abs(dt) < tol:
                x[i] = t
                x[m - i - 1] = -t
                A[i] = 2.0 / (1.0 - t**2) / (dp**2)  # Eq.(6.25)
                A[m - i - 1] = A[i]
                break
    return x, A
Ejemplo n.º 11
0
def GenLinSystem(poly):

    n=len(poly)
    N=n*(n-1) /2 
    rhs=numarray.zeros(N )
    a  = numarray.zeros( ( N,N) )
    c= 0

    for i in range(n):
        x = numarray.zeros(len(poly) )
        x[i]=1
        rhs[c]=(poly*x).sum()**2
        for J in range(0,n-1):
            for K in range(J+1,n):
                k = JKtoi(J,K,n)
                a[c,k] = (x[J] - x[K]) **2
        c+=1

    for i in range(n):
        for j in range (i+1,n) :
            x = numarray.zeros(n )
            x[i]=1
            x[j]=2
            rhs[c]=(poly*x).sum()**2
            for J in range(0,n-1):
                for K in range(J+1,n):
                    k = JKtoi(J,K,n)
                    a[c,k] = (x[J] - x[K]) **2
            c+=1
            if c >= N:
                break
        if c >= N:
            break
    return a, rhs
Ejemplo n.º 12
0
 def numhess(self, x0, ind1=None, ind2=None):
     """Computes second derivative using 2nd order centered finite difference scheme.
     MAKE MORE EFFICIENT IN FUTURE (i.e. when an index is in both ind1 and ind2)
     
     Thus, for F: R^n ---> R^m, H is an (m,n1,n2) matrix, where n1, n2 are subsets of [1,...,n]
     """
     eps = 1e-3
     try:
         n1 = len(ind1)
     except:
         n1 = self.n
         ind1 = range(n1)
         
     try:
         n2 = len(ind2)
     except:
         n2 = self.n
         ind2 = range(n2)
     
     H = zeros((self.m,n1,n2), Float)
     for i in range(n1):
         ei = zeros(self.n, Float)
         ei[ind1[i]] = 1.0
         for j in range(n2):
             ej = zeros(self.n, Float)
             ej[ind2[j]] = 1.0
             if ind1[i] == ind2[j]:
                 H[:,i,j] = (-1*self.func(x0+2*eps*ei) + 16*self.func(x0+eps*ei) - 30*self.func(x0) + \
                     16*self.func(x0-eps*ei) - self.func(x0-2*eps*ei))/(12*eps*eps)
             else:
                 H[:,i,j] = (self.func(x0+eps*(ei+ej)) - self.func(x0+eps*(ei-ej)) - \
                     self.func(x0+eps*(ej-ei)) + self.func(x0-eps*(ei+ej)))/(4*eps*eps)
     return H
Ejemplo n.º 13
0
def hess(func, x0, ind):
    """Computes second derivative using 2nd order centered finite difference scheme."""
    eps = 1e-3
    n = len(x0)
    m = len(ind)
    H = zeros((func.m, m, m), Float)
    for i in range(m):
        ei = zeros(n, Float)
        ei[ind[i]] = 1.0
        for j in range(i, m):
            ej = zeros(n, Float)
            ej[ind[j]] = 1.0
            if i == j:
                H[:, i,
                  j] = (-1 * func(x0 + 2 * eps * ei) + 16 * func(x0 + eps * ei)
                        - 30 * func(x0) + 16 * func(x0 - eps * ei) -
                        func(x0 - 2 * eps * ei)) / (12 * eps * eps)
            else:
                H[:, i,
                  j] = H[:, j,
                         i] = (func(x0 + eps * (ei + ej)) -
                               func(x0 + eps * (ei - ej)) - func(x0 + eps *
                                                                 (ej - ei)) +
                               func(x0 - eps * (ei + ej))) / (4 * eps * eps)
    return H
Ejemplo n.º 14
0
 def run_kut5(F, x, y, h):
     # Runge-Kutta-Fehlberg formulas
     C = array([37.0 / 378, 0.0, 250.0 / 621, 125.0 / 594, 0.0, 512.0 / 1771])
     D = array([2825.0 / 27648, 0.0, 18575.0 / 48384, 13525.0 / 55296, 277.0 / 14336, 1.0 / 4])
     n = len(y)
     K = zeros((6, n), type=Float64)
     K[0] = h * F(x, y)
     K[1] = h * F(x + 1.0 / 5 * h, y + 1.0 / 5 * K[0])
     K[2] = h * F(x + 3.0 / 10 * h, y + 3.0 / 40 * K[0] + 9.0 / 40 * K[1])
     K[3] = h * F(x + 3.0 / 5 * h, y + 3.0 / 10 * K[0] - 9.0 / 10 * K[1] + 6.0 / 5 * K[2])
     K[4] = h * F(x + h, y - 11.0 / 54 * K[0] + 5.0 / 2 * K[1] - 70.0 / 27 * K[2] + 35.0 / 27 * K[3])
     K[5] = h * F(
         x + 7.0 / 8 * h,
         y
         + 1631.0 / 55296 * K[0]
         + 175.0 / 512 * K[1]
         + 575.0 / 13824 * K[2]
         + 44275.0 / 110592 * K[3]
         + 253.0 / 4096 * K[4],
     )
     # Initialize arrays {dy} and {E}
     E = zeros((n), type=Float64)
     dy = zeros((n), type=Float64)
     # Compute solution increment {dy} and per-step error {E}
     for i in range(6):
         dy = dy + C[i] * K[i]
         E = E + (C[i] - D[i]) * K[i]
     # Compute RMS error e
     e = sqrt(sum(E ** 2) / n)
     return dy, e
Ejemplo n.º 15
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
Ejemplo n.º 16
0
def load_svd(sfile, ufile, vfile):
    """loads dense svd files as output by svdlibc"""
    n_s = int(sfile.readline())
    S = na.zeros((n_s, n_s), type="Float32")  #store S as a column vector
    for i in range(n_s):
        S[i, i] = float(sfile.readline())
    assert sfile.readline() == ''

    rows, columns = [int(a) for a in ufile.readline().split()]
    U = na.zeros((rows, columns), type="Float32")
    row = 0
    for line in ufile:
        col = 0
        for n in line.split():
            U[row, col] = float(n)
            col += 1
        row += 1

    rows, columns = [int(a) for a in vfile.readline().split()]
    V = na.zeros((rows, columns), type="Float32")
    row = 0
    for line in vfile:
        col = 0
        for n in line.split():
            V[row, col] = float(n)
            col += 1
        row += 1

    return U, S, V
Ejemplo n.º 17
0
def hist(binsIn, dataIn, normed=False):
    """
    Make a histogram that can be plotted with plot() so that
    the histogram just has the outline rather than bars as it
    usually does.

    Example Usage:
    binsIn = numarray.arange(0, 1, 0.1)
    angle = pylab.rand(50)

    (bins, data) = histOutline(binsIn, angle)
    plot(bins, data, 'k-', linewidth=2)

    """
    (en, eb) = matplotlib.mlab.hist(dataIn, bins=binsIn, normed=normed)

    stepSize = binsIn[1] - binsIn[0]

    bins = na.zeros(len(eb) * 2 + 2, type=na.Float)
    data = na.zeros(len(eb) * 2 + 2, type=na.Float)
    for bb in range(len(binsIn)):
        bins[2 * bb + 1] = binsIn[bb]
        bins[2 * bb + 2] = binsIn[bb] + stepSize
        data[2 * bb + 1] = en[bb]
        data[2 * bb + 2] = en[bb]

    bins[0] = bins[1]
    bins[-1] = bins[-2]
    data[0] = 0
    data[-1] = 0

    return (bins, data)
Ejemplo n.º 18
0
def calculate_spectrogram(input_array,
						  framesize,
						  hopsize,
						  window_function = numarray.linear_algebra.mlab.hanning,
						  keep_bands_until = 0,
						  axis = 0):
	"""Calculate the spectrogram."""

	do_fft = lambda arr: abs(numarray.fft.real_fft(arr, axis = axis))[:keep_bands_until]
	
	keep_bands_until = keep_bands_until or int(framesize / 2)
	input_shape = map(None, numarray.shape(input_array))

	window = window_function(framesize)
	if len(input_shape) > 1:
		window = transpose(array([list(window)] * input_shape[1]))
		
	# print "input_shape:", shape(input_array)
	zeros_shape = input_shape  # this allows for both 1 and 2 dim inputs
	zeros_shape[0] = framesize / 2
	input_array_plus_zeros = numarray.concatenate((numarray.zeros(zeros_shape), input_array, numarray.zeros(zeros_shape)))
	fft_range = range(0, len(input_array) - framesize, hopsize)

	fft_array = numarray.zeros(([len(fft_range)] + 
								map(None, numarray.shape(do_fft(window)))),
							   # do_fft(window) is used here because it gives the right shape
							   numarray.Float32) * 1.0  # this *1.0 is necessary!
	for result_counter, input_counter in enumerate(fft_range):
		frame = window * input_array_plus_zeros[input_counter : input_counter + framesize]
		fft_array[result_counter] = 10 * numarray.log10(0.1 + do_fft(frame))

	return fft_array
Ejemplo n.º 19
0
def determinant(a):
    """determinant(a) -> ||a||
 
    *a* may be either rank-2 or rank-3. If it is rank-2, it must square. 
    
    >>> A = [[1,2,3], [3,4,5], [5,6,7]]
    >>> _isClose(determinant(A), 0)
    1
    
    If *a* is rank-3, it is treated as an array of rank-2 matrices and
    must be square along the last 2 axes.
 
    >>> A = [[[1, 3], [2j, 3j]], [[2, 4], [4j, 4j]], [[3, 5], [6j, 5j]]]
    >>> _isClose(determinant(A), [-3j, -8j, -15j])
    1

    If *a* is not square along its last two axes, a LinAlgError is raised.
    
    >>> determinant(na.asarray(A)[...,:1])
    Traceback (most recent call last):
       ...
    LinearAlgebraError: Array (or it submatrices) must be square
    
    """
    a = na.asarray(a)
    _assertRank((2,3), a)
    _assertSubmatrixSquareness(a)
    stretched = (len(a.shape) == 2)
    if stretched:
        a = a[na.NewAxis,]
    t = _commonType(a)
    a = _castCopyAndTranspose(t, a, indices=(0,2,1))
    n_cases, n = a.shape[:2]
    if _array_kind[t] == 1:
        lapack_routine = lapack_lite2.zgetrf
    else:
        lapack_routine = lapack_lite2.dgetrf
    no_pivoting = na.arrayrange(1, n+1)
    pivots = na.zeros((n,), 'l')
    all_pivots = na.zeros((n_cases, n,), 'l')
    sum , not_equal = na.sum, na.not_equal
    stride = n * n * a.itemsize()
    pivots_stride = n * pivots.itemsize()
    view = a[0].view()
    view_pivots = all_pivots[0]
    a_i = view.copy()
    for i in range(n_cases):
        if i:
            a_i._copyFrom(view)
        outcome = lapack_routine(n, n, a_i, n, pivots, 0)
        view_pivots._copyFrom(pivots)
        view._copyFrom(a_i)
        view._byteoffset += stride
        view_pivots._byteoffset += pivots_stride
    signs = na.where(sum(not_equal(all_pivots, no_pivoting), 1) % 2, -1, 1).astype(t)
    for i in range(n):
        signs *= a[:,i,i]
    if stretched:
        signs = signs[0]
    return signs
Ejemplo n.º 20
0
    def run_kut5(F, x, y, h):
        # Runge-Kutta-Fehlberg formulas
        C = array([37./378, 0., 250./621, 125./594,          \
                   0., 512./1771])
        D = array([2825./27648, 0., 18575./48384,            \
                   13525./55296, 277./14336, 1./4])
        n = len(y)
        K = zeros((6, n), type=Float64)
        K[0] = h * F(x, y)
        K[1] = h * F(x + 1. / 5 * h, y + 1. / 5 * K[0])
        K[2] = h * F(x + 3. / 10 * h, y + 3. / 40 * K[0] + 9. / 40 * K[1])
        K[3] = h*F(x + 3./5*h, y + 3./10*K[0]- 9./10*K[1]    \
               + 6./5*K[2])
        K[4] = h*F(x + h, y - 11./54*K[0] + 5./2*K[1]        \
               - 70./27*K[2] + 35./27*K[3])
        K[5] = h*F(x + 7./8*h, y + 1631./55296*K[0]          \
               + 175./512*K[1] + 575./13824*K[2]             \
               + 44275./110592*K[3] + 253./4096*K[4])
        # Initialize arrays {dy} and {E}
        E = zeros((n), type=Float64)
        dy = zeros((n), type=Float64)
        # Compute solution increment {dy} and per-step error {E}
        for i in range(6):
            dy = dy + C[i] * K[i]
            E = E + (C[i] - D[i]) * K[i]

    # Compute RMS error e
        e = sqrt(sum(E**2) / n)
        return dy, e
Ejemplo n.º 21
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
Ejemplo n.º 22
0
    def cluster_vectorspace(self, vectors, trace=False):
        assert len(vectors) > 0

        # set the parameters to initial values
        dimensions = len(vectors[0])
        means = self._means
        priors = self._priors
        if not priors:
            priors = self._priors = numarray.ones(self._num_clusters,
                                        numarray.Float64) / self._num_clusters
        covariances = self._covariance_matrices 
        if not covariances:
            covariances = self._covariance_matrices = \
                [ numarray.identity(dimensions, numarray.Float64) 
                  for i in range(self._num_clusters) ]
            
        # do the E and M steps until the likelihood plateaus
        lastl = self._loglikelihood(vectors, priors, means, covariances)
        converged = False

        while not converged:
            if trace: print 'iteration; loglikelihood', lastl
            # E-step, calculate hidden variables, h[i,j]
            h = numarray.zeros((len(vectors), self._num_clusters),
                numarray.Float64)
            for i in range(len(vectors)):
                for j in range(self._num_clusters):
                    h[i,j] = priors[j] * self._gaussian(means[j],
                                               covariances[j], vectors[i])
                h[i,:] /= sum(h[i,:])

            # M-step, update parameters - cvm, p, mean
            for j in range(self._num_clusters):
                covariance_before = covariances[j]
                new_covariance = numarray.zeros((dimensions, dimensions),
                            numarray.Float64)
                new_mean = numarray.zeros(dimensions, numarray.Float64)
                sum_hj = 0.0
                for i in range(len(vectors)):
                    delta = vectors[i] - means[j]
                    new_covariance += h[i,j] * \
                        numarray.multiply.outer(delta, delta)
                    sum_hj += h[i,j]
                    new_mean += h[i,j] * vectors[i]
                covariances[j] = new_covariance / sum_hj
                means[j] = new_mean / sum_hj
                priors[j] = sum_hj / len(vectors)

                # bias term to stop covariance matrix being singular
                covariances[j] += self._bias * \
                    numarray.identity(dimensions, numarray.Float64)

            # calculate likelihood - FIXME: may be broken
            l = self._loglikelihood(vectors, priors, means, covariances)

            # check for convergence
            if abs(lastl - l) < self._conv_threshold:
                converged = True
            lastl = l
Ejemplo n.º 23
0
 def getVW(self, A):
     # V --> m, W --> n
     #print self.data
     MLU = linalg.lu_factor(c_[r_[A,transpose(self.data.C)], r_[self.data.B,self.data.D]])
     V = linalg.lu_solve(MLU,r_[zeros((self.data.n,self.data.q), Float), eye(self.data.q)])
     W = linalg.lu_solve(MLU,r_[zeros((self.data.m,self.data.p), Float), eye(self.data.p)],trans=1)
     
     return V, W
Ejemplo n.º 24
0
def read_sdd(fin):
    """Read an SDD output file into memory

    The SDD output file starts with 2 comment lines, followed by a line whose
    contents are C{k n m}. C{k} is the user's selected value for truncation
    of the matrices. C{n} is the number of rows in the original matrix, and
    C{m} is the number of columns in the original matrix.

    Following this line are C{k} values which represent B{D}, a diagonal matrix.
    The next n*k values are in the set C{{-1, 0, 1}}, and represent the X
    matrix. Following this, logically, are m*k values representng the Y matrix.
    If you're confused, read the code; it's pretty simple. (although not
    documented anywhere, grumble, grumble)

    The odd C{if buf} statements in the read loops are necessary because
    SDDPACK seems not to write any lines longer than 96 characters. Thus, each
    row may or may not be on the same line.

    @param fin: open sdd file
    @type fin: File

    @returns Tuple (X, D, Y), the three SDD matrices
    """
    for i in range(2): fin.readline() #ignore comments at top of sdd file
    k, n, m = fin.readline().split()
    k = int(k)
    n = int(n)
    m = int(m)

    D = na.zeros((k,k), type="Float32")
    print "reading D matrix from SDD file"
    for i in range(int(k)):
        D[i,i] = float(fin.readline())

    X = na.zeros((n, k), type="Int8")   #the values are in {-1, 0, 1}
    buf = fin.readline().split()
    print "reading X matrix from SDD file"
    for i in xrange(k):
        for j in xrange(n):
            if buf:
                X[j,i] = int(buf.pop(0))  #do I want j, i reversed? (think so)
            else:
                buf = fin.readline().split()
                X[j,i] = int(buf.pop(0))
    assert buf == []  #verify we don't have data left over

    Y = na.zeros((m, k), type="Int8")   #vals in {-1, 0, 1} still
    buf = fin.readline().split()
    print "reading Y matrix from SDD file"
    for i in xrange(k):
        for j in xrange(m):
            if buf:
                Y[j,i] = int(buf.pop(0))
            else:
                buf = fin.readline().split()
                Y[j,i] = int(buf.pop(0))
    assert fin.readline() == '' and buf == [] #verify we done got everything
    return (X, D, Y)
Ejemplo n.º 25
0
def approx_fprime(xk, f, *args):
    f0 = apply(f, (xk, ) + args)
    grad = Num.zeros((len(xk), ), 'd')
    ei = Num.zeros((len(xk), ), 'd')
    for k in range(len(xk)):
        ei[k] = 1.0
        grad[k] = (apply(f, (xk + epsilon * ei, ) + args) - f0) / epsilon
        ei[k] = 0.0
    return grad
Ejemplo n.º 26
0
 def getnearestgen(self,ra1,dec1,ra2,dec2,j):#measure distances from ra1, dec1 to members in catalog ra2, dec2
     sig5=N.zeros(len(ra1),'f')
     sig10=N.zeros(len(ra1),'f')
     for i in range(len(ra1)):
         angdist=my.DA(c.z[j],h100)#kpc/arcsec
         dspec=N.sqrt((ra1[i]-ra2)**2+(dec1[i]-dec2)**2)#sorted array of distances in degrees
         dspecsort=N.take(dspec,N.argsort(dspec))
         sig5[i]=5./(N.pi)/(dspecsort[5]*3600.*angdist/1000.)**2#convert from deg to arcsec, multiply by DA (kpc/arcsec), divide by 1000 to convert to Mpc, index 5 element b/c 0 is itself
         sig10[i]=10./(N.pi)/(dspecsort[10]*3600.*angdist/1000.)**2#convert from deg to arcsec, multiply by DA (kpc/arcsec), divide by 1000 to convert to Mpc, index 5 element b/c 0 is itself
     return sig5, sig10
Ejemplo n.º 27
0
def isovalues(isos,isow):
    x = numarray.zeros(len(isos))*0.  # age
    y = numarray.zeros(len(isos))*0.  # metallicity
    z = numarray.zeros(len(isos))*0.  # metallicity
    for i in range(len(isos)):
        feh,age = iso.scaled2real(isos[i][0],isos[i][1])
        y[i] = feh
        x[i] = numarray.log10(age)
        z[i] = isow[i] 
    return x,y,z
Ejemplo n.º 28
0
 def Kcorr(self):#calculate K_u(z) (Blanton et al 2003) for each cluster
     self.kcorr=N.zeros(len(self.z),'f')
     self.dL=N.zeros(len(self.z),'f')
     z=N.arange(0.,1.2,.1)
     #kc=N.array([0.,0.,.05,.05,.1,.15,.2,.225,.25,.3,.35,.35],'f')#Ku(z)
     kc=N.array([0.,0.,.025,.05,.07,.1,.14,.16,.2,.25,.25,.3],'f')#Kg(z)
     r=self.z/.01
     for i in range(len(self.z)):
         self.kcorr[i]=kc[int(r[i])]+(r[i]-int(r[i]))*(kc[int(r[i]+1)]-kc[int(r[i])])
         self.dL[i] = my.dL(self.z[i],h100)
Ejemplo n.º 29
0
def isovalues(isos, isow):
    x = numarray.zeros(len(isos)) * 0.  # age
    y = numarray.zeros(len(isos)) * 0.  # metallicity
    z = numarray.zeros(len(isos)) * 0.  # metallicity
    for i in range(len(isos)):
        feh, age = iso.scaled2real(isos[i][0], isos[i][1])
        y[i] = feh
        x[i] = numarray.log10(age)
        z[i] = isow[i]
    return x, y, z
Ejemplo n.º 30
0
 def readsdsscompleteness(self):
     self.nspec05 = N.zeros(len(self.z), 'f')
     self.nphot05 = N.zeros(len(self.z), 'f')
     self.compl05 = N.zeros(len(self.z), 'f')
     self.nspec1 = N.zeros(len(self.z), 'f')
     self.nphot1 = N.zeros(len(self.z), 'f')
     self.compl1 = N.zeros(len(self.z), 'f')
     self.nspec2 = N.zeros(len(self.z), 'f')
     self.nphot2 = N.zeros(len(self.z), 'f')
     self.compl2 = N.zeros(len(self.z), 'f')
     complout = open('sdsscompleteness.dat', 'r')
     i = 0
     for line in complout:
         f = line.split()
         #print line
         #print f
         j = 0
         for j in range(len(f)):
             f[j] = float(f[j])
         print f
         (self.nspec05[i], self.nphot05[i], self.compl05[i], self.nspec1[i],
          self.nphot1[i], self.compl1[i], self.nspec2[i], self.nphot2[i],
          self.compl2[i]) = (f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7],
                             f[8])
         #print f[0],f[1],f[2],self.nspec05[i],self.nphot05[i],self.compl05[i]
         #print line
         i = i + 1
     complout.close()
     print "got ", i, " clusters from sdsscompleteness.dat"
     print "min of c.compl05 = ", min(self.compl05)
Ejemplo n.º 31
0
def sample(snx,sny,plist,background):
	#i think that this one just does a little 3 by 3 box	
	import numarray	
	#print "sample" , snx , " " , sny	
	pixellist = numarray.zeros((3,3),type = numarray.Float32) 
	pixels = numarray.zeros((3,3),type = numarray.Float32) 
	#print pixellist	
	cx = round(snx)
	cy = round(sny)
	for xcor in [cx - 1.0 , cx , cx + 1.0]:
		for ycor in [cy - 1.0 , cy , cy + 1.0]:
			#print xcor, cx, ycor, cy, xcor-cx+2, ycor-cy+2
			x = int(xcor-cx+1)
			y = int(ycor-cy+1)	
			#print x ,y 
			#reverse x and y because of image
			pixellist[x][y] = plist[int(ycor)-1][int(xcor)-1]
	#print pixellist	
	#shift to a 3 x 3 grid	
	snx = snx - cx + 1
	sny = sny - cy + 1
	xmin = snx - 0.5
        ymin = sny - 0.5
        xmax = snx + 0.5
        ymax = sny + 0.5
        scale  = 1.00 
       	#figure out which pixels to measure
	tally = 0
	counts = 0	
	vcounts = 0
	for pixelx in range(3):
		for pixely in range(3):
        	        pymin = pixely - scale / 2.0
        	        pymax = pixely + scale / 2.0
        	        pxmin = pixelx - scale / 2.0
        	        pxmax = pixelx + scale / 2.0
        	        compx = overlap(pxmin,pxmax,xmin,xmax)
        	        compy = overlap(pymin,pymax,ymin,ymax)
        	        coeff = compx * compy
        	        #pixels[pixelx][pixely]
        	        #print coeff, tally	
        	        counts = counts + coeff * pixellist[pixelx][pixely]
        	       	vcounts = vcounts + pixellist[pixelx][pixely] 
			tally = tally + coeff
	vavcounts = vcounts / 9.0
	sum = 0
       	for pixelx in range(3):         
        	for pixely in range(3):
			sum = sum + pow((pixellist[pixelx][pixely]-vavcounts),2)
	rms = pow(sum/9.0,0.5)
	#rescale the pixels to account for the difference in pixel scale
        #print counts
        #counts = counts  * pow(scale )
        print "coeff adds up to " , tally
	return [counts,rms]
Ejemplo n.º 32
0
def _mad3(data):
    # adapted from NR function moment
    n = len(data[:,0,0])
    s = num.zeros(data[0,:,:].shape, num.Float32)
    # First pass to get the mean.
    for j in range(n): s += data[j,:,:]
    ave = s/n
    adev = num.zeros(s.shape, num.Float32)
    for j in range(n):
        num.add(adev, num.fabs(data[j] - ave), adev)
    return adev / n
Ejemplo n.º 33
0
def hess3(func, x0, ind):
	"""Computes third derivative using hess function."""
	eps = sqrt(1e-3)
	n = len(x0)
	m = len(ind)
	C = zeros((func.m,m,m,m), Float)
	for i in range(m):
		ei = zeros(n, Float)
		ei[ind[i]] = 1.0
		C[i,:,:,:] = (hess(func,x0+eps*ei,ind) - hess(func,x0-eps*ei,ind))/(2*eps)
	return C
Ejemplo n.º 34
0
    def testInit(self):
        a = self.a
        b = self.b
        assert(a.g == 0.0 and \
               na.allclose(a.h, na.zeros(3)) and \
               na.allclose(a.K, na.zeros(shape=(3,3)))), \
               " Error with standard initialization "

        assert(b.g == 2.0 and \
               na.allclose(b.h, na.array([1,2,3], type='Float32')) and \
               na.allclose(b.K, na.arange(9,shape=(3,3), type='Float32'))), \
               " Error with standard initialization with parameter setting"    
Ejemplo n.º 35
0
def plotFitRms(root, polyroot, gcfitdir):
    s = starset.StarSet(root)
    s.loadPolyfit(polyroot, arcsec=1, accel=0)

    years = s.stars[0].years
    fitPx = s.getArray('fitXv.p')
    fitVx = s.getArray('fitXv.v')
    fitPy = s.getArray('fitYv.p')
    fitVy = s.getArray('fitYv.v')
    t0x = s.getArray('fitXv.t0')
    t0y = s.getArray('fitYv.t0')

    rmsX = na.zeros(len(s.stars), type=na.Float)
    rmsY = na.zeros(len(s.stars), type=na.Float)
    rms = na.zeros(len(s.stars), type=na.Float)
    cnt = na.zeros(len(s.stars), type=na.Int)

    for ee in range(len(years)):
        dtX = years[ee] - t0x
        dtY = years[ee] - t0y

        xfit = fitPx + (dtX * fitVx)
        yfit = fitPy + (dtY * fitVy)

        x = s.getArrayFromEpoch(ee, 'x')
        y = s.getArrayFromEpoch(ee, 'y')
        xpix = s.getArrayFromEpoch(ee, 'xpix')
        ypix = s.getArrayFromEpoch(ee, 'ypix')

        diffx = xfit - x
        diffy = yfit - y
        diff = na.sqrt(diffx**2 + diffy**2)

        idx = (na.where(xpix > -999))[0]

        rmsX[idx] += diffx**2
        rmsY[idx] += diffy**2
        rms[idx] += diff**2
        cnt[idx] += 1

    rmsX = na.sqrt(rmsX / cnt) * 1000.0
    rmsY = na.sqrt(rmsY / cnt) * 1000.0
    rms = na.sqrt(rms / cnt) * 1000.0

    mag = s.getArray('mag')
    x = s.getArray('x')
    y = s.getArray('y')
    r = na.sqrt(x**2 + y**2)

    idx = (na.where(mag < 15))[0]

    p.clf()
    p.semilogy(r[idx], rms[idx], 'k.')
Ejemplo n.º 36
0
def hess3(func, x0, ind):
    """Computes third derivative using hess function."""
    eps = sqrt(1e-3)
    n = len(x0)
    m = len(ind)
    C = zeros((func.m, m, m, m), Float)
    for i in range(m):
        ei = zeros(n, Float)
        ei[ind[i]] = 1.0
        C[i, :, :, :] = (hess(func, x0 + eps * ei, ind) -
                         hess(func, x0 - eps * ei, ind)) / (2 * eps)
    return C
Ejemplo n.º 37
0
def plotFitRms(root, polyroot, gcfitdir):
    s = starset.StarSet(root)
    s.loadPolyfit(polyroot, arcsec=1, accel=0)

    years = s.stars[0].years
    fitPx = s.getArray('fitXv.p')
    fitVx = s.getArray('fitXv.v')
    fitPy = s.getArray('fitYv.p')
    fitVy = s.getArray('fitYv.v')
    t0x = s.getArray('fitXv.t0')
    t0y = s.getArray('fitYv.t0')

    rmsX = na.zeros(len(s.stars), type=na.Float)
    rmsY = na.zeros(len(s.stars), type=na.Float)
    rms = na.zeros(len(s.stars), type=na.Float)
    cnt = na.zeros(len(s.stars), type=na.Int)

    for ee in range(len(years)):
        dtX = years[ee] - t0x
        dtY = years[ee] - t0y

        xfit = fitPx + (dtX * fitVx)
        yfit = fitPy + (dtY * fitVy)

        x = s.getArrayFromEpoch(ee, 'x')
        y = s.getArrayFromEpoch(ee, 'y')
        xpix = s.getArrayFromEpoch(ee, 'xpix')
        ypix = s.getArrayFromEpoch(ee, 'ypix')

        diffx = xfit - x
        diffy = yfit - y
        diff = na.sqrt(diffx**2 + diffy**2)

        idx = (na.where(xpix > -999))[0]

        rmsX[idx] += diffx**2
        rmsY[idx] += diffy**2
        rms[idx] += diff**2
        cnt[idx] += 1

    rmsX = na.sqrt(rmsX / cnt) * 1000.0
    rmsY = na.sqrt(rmsY / cnt) * 1000.0
    rms = na.sqrt(rms / cnt) * 1000.0

    mag = s.getArray('mag')
    x = s.getArray('x')
    y = s.getArray('y')
    r = na.sqrt(x**2 + y**2)

    idx = (na.where(mag < 15))[0]

    p.clf()
    p.semilogy(r[idx], rms[idx], 'k.')
Ejemplo n.º 38
0
 def __init__(self, names, shape, g=None, h=None, K=None):
     Potential.__init__(self, names)
     self.shape = shape
     
     # set parameters to 0s
     self.n = na.sum(shape)
     if not g: self.g = 0.0
     else: self.g = float(g)
     if not h: self.h = na.zeros(shape=(self.n), type='Float32')     
     else: self.h = na.array(h,shape=(self.n), type='Float32')
     if not K: self.K = na.zeros(shape=(self.n, self.n), type='Float32')
     else: self.K = na.array(K, shape=(self.n, self.n), type='Float32')
Ejemplo n.º 39
0
 def processBands():
     # buffer arrays
     red = zeros((ysz,xsz),typecode)
     green = zeros((ysz,xsz),typecode)
     blue = zeros((ysz,xsz),typecode)
     pan = zeros((ysz,xsz),'f')
     # read into buffers
     GDALReadRaster(redBand._o,xbloff,ybloff,xsz,ysz,xsz,ysz,self.srcDt,red)
     GDALReadRaster(greenBand._o,xbloff,ybloff,xsz,ysz,xsz,ysz,self.srcDt,green)
     GDALReadRaster(blueBand._o,xbloff,ybloff,xsz,ysz,xsz,ysz,self.srcDt,blue)
     GDALReadRaster(panBand._o,xbloff,ybloff,xsz,ysz,xsz,ysz,gdal.GDT_Float32,pan)
     # merge
     mergeFn(red.astype('f'),green.astype('f'),blue.astype('f'),pan)
def create_predict_table(LCM, agent_set, agents_index, observed_choices_id, data_objects, geographies=[]):

    resources = data_objects

    mc_choices = sample_choice(LCM.model.probabilities)  # monte carlo choice
    mc_choices_index = LCM.model_resources.translate("index")[mc_choices]
    maxprob_choices = sample_choice(LCM.model.probabilities, method="max_prob")  # max prob choice
    maxprob_choices_index = LCM.model_resources.translate("index")[maxprob_choices]
    results = []

    gcs = resources.translate("gridcell")
    for geography in geographies:
        geos = resources.translate(geography)

        # get observed geo_id
        obs = copy_dataset(agent_set)
        obs.subset_by_index(agents_index)
        obs.set_values_of_one_attribute(gcs.id_name[0], observed_choices_id)

        resources.merge({"household": obs})  # , "gridcell": gcs, "zone": zones, "faz":fazes})
        obs.compute_variables(geos.id_name[0], resources=resources)
        obs_geo_ids = obs.get_attribute(geos.id_name[0])

        # count simulated choices
        sim = copy_dataset(obs)
        sim.set_values_of_one_attribute(gcs.id_name[0], gcs.get_id_attribute()[mc_choices_index])
        resources.merge({"household": sim})

        geos_size = geos.size()
        geo_ids = geos.get_id_attribute()

        pred_matrix = zeros((geos_size, geos_size))
        p_success = zeros((geos_size,)).astype(Float32)

        f = 0
        for geo_id in geo_ids:
            index_in_geo = where(obs_geo_ids == geo_id)[0]
            resources.merge({"select_index": index_in_geo})

            geos.compute_variables("number_of_select_households", resources=resources)
            pred_matrix[f] = geos.get_attribute("number_of_select_households")
            if sum(pred_matrix[f]) > 0:
                p_success[f] = float(pred_matrix[f, f]) / sum(pred_matrix[f])

            sim.increment_version("grid_id")  # to trigger recomputation in next iteration
            f += 1

        print p_success
        results.append((pred_matrix.copy(), p_success.copy()))

    return results
def create_predict_table(LCM, agent_set, agents_index, observed_choices_id, data_objects, geographies=[]):

    resources = data_objects
    
    mc_choices = sample_choice(LCM.model.probabilities)    #monte carlo choice
    mc_choices_index = LCM.model_resources.translate("index")[mc_choices]
    maxprob_choices = sample_choice(LCM.model.probabilities, method="max_prob")  #max prob choice
    maxprob_choices_index = LCM.model_resources.translate("index")[maxprob_choices]
    results = []
    
    gcs = resources.translate("gridcell")
    for geography in geographies:
        geos = resources.translate(geography)
        
        #get observed geo_id
        obs = copy_dataset(agent_set)    
        obs.subset_by_index(agents_index)
        obs.set_values_of_one_attribute(gcs.id_name[0], observed_choices_id) 
    
        resources.merge({"household": obs}) #, "gridcell": gcs, "zone": zones, "faz":fazes})
        obs.compute_variables(geos.id_name[0], resources=resources)
        obs_geo_ids = obs.get_attribute(geos.id_name[0])
        
        #count simulated choices
        sim = copy_dataset(obs)
        sim.set_values_of_one_attribute(gcs.id_name[0], gcs.get_id_attribute()[mc_choices_index]) 
        resources.merge({"household": sim})
    
        geos_size = geos.size()
        geo_ids = geos.get_id_attribute()
        
        pred_matrix = zeros((geos_size, geos_size))
        p_success = zeros((geos_size,)).astype(Float32)
        
        f = 0
        for geo_id in geo_ids:
            index_in_geo = where(obs_geo_ids == geo_id)[0]
            resources.merge({"select_index": index_in_geo})
    
            geos.compute_variables("number_of_select_households", resources=resources)
            pred_matrix[f] = geos.get_attribute("number_of_select_households")
            if sum(pred_matrix[f]) > 0:
                p_success[f] = float(pred_matrix[f, f])/sum(pred_matrix[f])
    
            sim.increment_version('grid_id')  #to trigger recomputation in next iteration
            f += 1
            
        print p_success
        results.append((pred_matrix.copy(), p_success.copy()))
        
    return results
Ejemplo n.º 42
0
def buildPearsonCorrelationMatrix(traits, commonStrains):
    dim = (len(traits), len(commonStrains))
    matrix = numarray.zeros(dim, MA.Float64)
    testMatrix = numarray.zeros(dim, MA.Float64)

    for i in range(len(traits)):
        sd = traits[i].strainData
        keys = sd.keys()
        for j in range(0, len(commonStrains)):
            if keys.__contains__(commonStrains[j]):
                matrix[i,j] = sd[commonStrains[j]]
                testMatrix[i,j] = 1

    return matrix, testMatrix
Ejemplo n.º 43
0
def jac(func, x0, ind):
	"""Compute (n-1) x m Jacobian of function func at x0, where n = len(x0) and
	m = len(ind).  ind denotes the indices along which to compute the 
	Jacobian.  NOTE:  This assumes func has 1 more variable than equations!!."""
	
	eps = 1e-6
	n = len(x0)
	m = len(ind)
	J = zeros((n-1, m), Float)
	for i in range(m):
		ei = zeros(n, Float)
		ei[ind[i]] = 1.0
		J[:,i] = (func(x0+eps*ei)-func(x0-eps*ei))/(2*eps)
	return J
Ejemplo n.º 44
0
 def diff(self, x0, ind=None):
     eps = 1e-6
     try:
         n = len(ind)
     except:
         n = self.n
         ind = range(n)
     
     J = zeros((self.m, n), Float)
     for i in range(n):
         ei = zeros(self.n, Float)
         ei[ind[i]] = 1.0
         J[:,i] = (self.func(x0+eps*ei)-self.func(x0-eps*ei))/(2*eps)
     return J
Ejemplo n.º 45
0
def jac(func, x0, ind):
    """Compute (n-1) x m Jacobian of function func at x0, where n = len(x0) and
	m = len(ind).  ind denotes the indices along which to compute the 
	Jacobian.  NOTE:  This assumes func has 1 more variable than equations!!."""

    eps = 1e-6
    n = len(x0)
    m = len(ind)
    J = zeros((n - 1, m), Float)
    for i in range(m):
        ei = zeros(n, Float)
        ei[ind[i]] = 1.0
        J[:, i] = (func(x0 + eps * ei) - func(x0 - eps * ei)) / (2 * eps)
    return J
Ejemplo n.º 46
0
    def GetCartesianForces(self):
        #Control from ASE API?

        #Make array of positions
        #Get number of atoms
        numAtoms = len(self.GetListOfAtoms())

        #Debugging
        print 'there are %i atoms' % (numAtoms)

        #Make a numarray to hold the atoms
        positions = numa.zeros((
            numAtoms,
            3,
        ), numa.Float64)

        #Get the info from each atom and copy it to the numarray
        count = 0
        tempList = self.GetListOfAtoms()
        for k in tempList:
            positions[count] = k.GetCartesianPosition()
            count = count + 1

        #Debugging
        print positions

        #Run a set positions
        cp2k_interface.c_set_pos(positions, self.env_id)

        #Run a get to check
        positions = numa.zeros((
            numAtoms,
            3,
        ), numa.Float64)
        cp2k_interface.c_get_pos(positions, self.env_id)

        #Debugging
        print positions

        #Calculate forces (update_forces)
        cp2k_interface.c_update_forces(self.env_id)

        #Now run a get force

        #Use parse tuple to get pyobject

        #Junk return
        self.forces = num.zeros((numAtoms, 3), num.Float)
        return self.forces
Ejemplo n.º 47
0
def binit(x, y, n):  #bin arrays x, y into n bins, returning xbin,ybin
    nx = len(x)
    y = N.take(y, N.argsort(x))  #sort y according to x rankings
    x = N.take(x, N.argsort(x))
    xbin = N.zeros(n, 'f')
    ybin = N.zeros(n, 'f')
    for i in range(n):
        nmin = i * int(float(nx) / float(n))
        nmax = (i + 1) * int(float(nx) / float(n))
        xbin[i] = pylab.median(x[nmin:nmax])
        ybin[i] = pylab.median(y[nmin:nmax])
        #xbin[i]=N.average(x[nmin:nmax])
        #ybin[i]=N.average(y[nmin:nmax])
        #ybinerr[i]=scipy.stats.std(y[nmin:nmax])
    return xbin, ybin  #, ybinerr
Ejemplo n.º 48
0
def _mad2(data, included):
    # adapted from NR function moment
    # same as mad3 but ignores zero values
    n = len(data[:,0,0])
    s = num.zeros(data[0,:,:].shape, num.Float32)
    # First pass to get the mean.
    for j in range(n): s += data[j,:,:]
    ave = s/included
    adev = num.zeros(s.shape, num.Float32)
    for j in range(n):
        d = where(num.fabs(data[j]) > tiny,
                  num.fabs(data[j] - ave),
                  0.0)
        num.add(adev, d, adev)
    return adev / included
Ejemplo n.º 49
0
def GenLinSystem(poly):

    """
    Generates the linear system which allows factorisation of
    something like:
    
    (a1 * a+ a2 *b + a3 *c ....) ^2 into:
    c1(a-b)^2 + c2(a-c)^2
    
    For the two dimensional bilinear interpolator, get something like:
    
    p2 = numarray.array([3,-1,-1,-1])

     which may be solved by:
     a,r=GenLinSystem(p2)
     la.solve_linear_equations(a,r)

     """
    
    n=len(poly)

    # number of pairings
    NC=n*(n-1) /2
    NP=n*(n+1) /2

    # The linear system
    rhs = numarray.zeros(NP ,
                         numarray.Float64)
    a   = numarray.zeros( ( NP,NC) ,
                          numarray.Float64)

    for i in range(0, n-1):
        for j in range (i+1, n):
            cdx = CJKtoi(i,j,n)
            a[ PJKtoi(i,i,n) , cdx ] =1
            a[ PJKtoi(i,j,n) , cdx ] =-2
            a[ PJKtoi(j,j,n) , cdx ] =1

    # right hand side
    for i in range(0, n):
        for j in range (i, n):
            pdx=PJKtoi(i,j,n)
            if i == j:
                rhs[pdx]= poly[i]**2
            else:
                rhs[pdx]= 2 * poly[i] * poly[j]

    return a, rhs
Ejemplo n.º 50
0
def dual_perceptron(x, y):
    """implements a dual form perceptron
    
    as defined in "Support Vector Machines", Cristiani, p.18
    @x is a list of i 'numarray.array's that define the input
    @y is a list of i correct outputs for x, must be syncronized with x
        i.e. y[3] == correct f(x[3])
    @n is the learning rate 0 < n < 1"""
    a = na.zeros(len(x), na.Float32)     #embedding strength = alpha
    b = 0                                       #bias
    R = math.pow(max_norm(x), 2)
    mistake_free = 0
    iteration = 0
    while mistake_free == 0 and iteration < 50:
        iteration += 1
        print "iteration #", iteration
        mistake_free = 1
        for i in range(len(x)):
            sum_lc = 0
            for j in range(len(x)):
                sum_lc += a[j] * y[j] * na.dot(x[j], x[i]) #+ b
            print sum_lc, a, y, x, b
            if y[i] * (sum_lc + b) <= 0:
                print b, a, sum_lc, y[i]
                a[i] += 1
                b += y[i] * R
                mistake_free = 0
    return (a, b)
Ejemplo n.º 51
0
def r(u):  # Boundary condition residuals-- see Eq. (8.7)
    r = zeros(len(u),type=Float64)
    X,Y = integrate(F,x,initCond(u),xStop,h)
    y = Y[len(Y) - 1]
    r[0] = y[2]             
    r[1] = y[3] - 1.0       
    return r
Ejemplo n.º 52
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
Ejemplo n.º 53
0
    def testHorizontalLinesARGB32(self):
        colors = (Qt.red, Qt.green, Qt.blue, Qt.white, Qt.black)
        alpha = 0xff000000
        red   = 0x00ff0000
        green = 0x0000ff00
        blue  = 0x000000ff
        white = 0x00ffffff
        black = 0x00000000

        image = QImage(3, len(colors), QImage.Format_ARGB32)
        painter = QPainter(image)
        for i, color in enumerate(colors):
            painter.setPen(color)
            painter.drawLine(0, i, image.width(), i)
        del painter

        array = np.zeros((image.height(), image.width()), dtype=np.UInt32)
        array[0,:] = alpha|red
        array[1,:] = alpha|green
        array[2,:] = alpha|blue
        array[3,:] = alpha|white
        array[4,:] = alpha|black

        self.assertEqual(np.all(array == toNumpy(image)), True)
        self.assertEqual(image == toQImage(array), True)