def numeric_gemm_var1_flat(A, B, C, mc, kc, nc, mr=1, nr=1): M, N = C.shape K = A.shape[0] mc = min(mc, M) kc = min(kc, K) nc = min(nc, N) tA = Numeric.zeros((mc, kc), typecode = Numeric.Float) tB = Numeric.zeros((kc, N), typecode = Numeric.Float) for k in range(0, K, kc): # Pack B into tB tB[:,:] = B[k:k+kc:,:] for i in range(0, M, mc): imc = i+mc # Pack A into tA tA[:,:] = A[i:imc,k:k+kc] for j in range(0, N): # , nc): # Cj += ABj + Cj # jnc = j+nc ABj = Numeric.matrixmultiply(tA, tB[:,j]) Numeric.add(C[i:imc:,j], ABj, C[i:imc:,j]) # Store Caux into memory return
def add_second_wave(self, to_what): # our second wave travels slower by a factor of e Numeric.add(Numeric.array(-time.time() / Numeric.e % twopi, Numeric.Float32), self.r2, self.tmp2) Numeric.sin(self.tmp2, self.tmp2) Numeric.add(self.tmp2, to_what, to_what)
def projectionOf(self, vector): """Returns the projection of |vector| (a Class:MMTK.ParticleVector object) onto the subspace.""" vector = vector.array basis = self.getBasis().array p = Numeric.zeros(vector.shape, Numeric.Float) for bv in basis: Numeric.add(Numeric.add.reduce(Numeric.ravel(bv*vector))*bv, p, p) return ParticleProperties.ParticleVector(self.universe, p)
def _gebp_opt1(A, B, C, nc): n = C.shape[1] # Assume B is packed # Pack A into tA for j in range(0, n, nc): # Load Bj into cache # Load Cj into cache # Cj += ABj + Cj ABj = Numeric.matrixmultiply(A, B[:,j:j+nc]) Numeric.add(C[:,j:j+nc], ABj, C[:,j:j+nc]) # Store Caux into memory return
def raster_add(s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n, t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n, nodata): import Numeric if verbose != 0: print 'Copy %d,%d,%d,%d to %d,%d,%d,%d.' \ % (s_xoff, s_yoff, s_xsize, s_ysize, t_xoff, t_yoff, t_xsize, t_ysize ) s_band = s_fh.GetRasterBand(s_band_n) t_band = t_fh.GetRasterBand(t_band_n) data_src = s_band.ReadAsArray(s_xoff, s_yoff, s_xsize, s_ysize, t_xsize, t_ysize) data_dst = t_band.ReadAsArray(t_xoff, t_yoff, t_xsize, t_ysize) nodata_test = Numeric.equal(data_src, s_band.GetNoDataValue()) data_src_fill = Numeric.choose(nodata_test, (data_src, 0)) to_write = Numeric.add(data_src_fill, data_dst) t_band.WriteArray(to_write, t_xoff, t_yoff) return 0
def raster_add( s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n, t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n, nodata ): import Numeric if verbose != 0: print 'Copy %d,%d,%d,%d to %d,%d,%d,%d.' \ % (s_xoff, s_yoff, s_xsize, s_ysize, t_xoff, t_yoff, t_xsize, t_ysize ) s_band = s_fh.GetRasterBand( s_band_n ) t_band = t_fh.GetRasterBand( t_band_n ) data_src = s_band.ReadAsArray( s_xoff, s_yoff, s_xsize, s_ysize, t_xsize, t_ysize ) data_dst = t_band.ReadAsArray( t_xoff, t_yoff, t_xsize, t_ysize ) nodata_test = Numeric.equal(data_src,s_band.GetNoDataValue()) data_src_fill = Numeric.choose( nodata_test, (data_src, 0) ) to_write = Numeric.add(data_src_fill,data_dst) t_band.WriteArray( to_write, t_xoff, t_yoff ) return 0
def readParticleTrajectory(self, atom, first=0, last=None, skip=1, variable = "configuration"): total = None self.steps_read = [] for i in range(len(self.trajectories)): if self.nsteps[i+1] <= first: self.steps_read.append(0) continue if last is not None and self.nsteps[i] >= last: break n = max(0, (self.nsteps[i]-first+skip-1)/skip) start = first+skip*n-self.nsteps[i] n = (self.nsteps[i+1]-first+skip-1)/skip stop = first+skip*n if last is not None: stop = min(stop, last) stop = stop-self.nsteps[i] if start >= 0 and start < self.nsteps[i+1]-self.nsteps[i]: t = self.trajectories[i] pt = t.readParticleTrajectory(atom, start, stop, skip, variable) self.steps_read.append((stop-start)/skip) if total is None: total = pt else: if variable == "configuration" \ and self.cell_parameters[0] is not None: jump = pt.array[0]-total.array[-1] mask = Numeric.less(jump, -0.5*self.cell_parameters[i-1])- \ Numeric.greater(jump, 0.5*self.cell_parameters[i-1]) t._boxTransformation(pt.array, pt.array, 1) Numeric.add(pt.array, mask[Numeric.NewAxis, :], pt.array) t._boxTransformation(pt.array, pt.array, 0) elif variable == "box_coordinates" \ and self.cell_parameters[0] is not None: jump = pt.array[0]-total.array[-1] mask = Numeric.less(jump, -0.5)- \ Numeric.greater(jump, 0.5) Numeric.add(pt.array, mask[Numeric.NewAxis, :], pt.array) total.array = Numeric.concatenate((total.array, pt.array)) else: self.steps_read.append(0) return total
def numeric_gemm_var1_row(A, B, C, mc, kc, nc, mr=1, nr=1): """ Observations on flat/row: - Using the same basic approach and keeping the parameters equal, flat is faster by about 100 MFlops. This is counterintuitive, since row is optimized for the row major layout of the arrays and flat is optimized for column major layout. - The key to getting row to be faster lies in making sure the kernel operations really take advantage of the row layout by adjusting nc, which impacts row operations in the inner loop: - In Python, the final add has the largest effect on performance - for flat, nc controls how much of each row is copied - for row, nc controls how much of each row is copied - How much of each row is copied appears to have the biggest impact - In the end, turns out that doing vector/matrix multiply in the innermost loop is most fair. Cache effects aren't really observable since matrixmultiply is already pretty fast. Making the block sizes bigger just gives more to an already fast kernel. """ M, N = C.shape K = A.shape[0] nc = min(nc, N) kc = min(kc, K) mc = min(mc, M) tA = Numeric.zeros((M, kc), typecode = Numeric.Float) tB = Numeric.zeros((kc, nc), typecode = Numeric.Float) for k in range(0, K, kc): # Pack A into tA tA[:,:] = A[:,k:k+kc] for j in range(0, N, nc): jnc = j+nc # Pack B into tB tB[:,:] = B[k:k+kc, j:jnc] for i in range(0, M): # , mc): # Ci = AiB + Ci # imc = i+mc ABi = Numeric.matrixmultiply(tA[i,:], tB) Numeric.add(C[i,j:jnc], ABi, C[i,j:jnc]) # Store Caux into memory return
def multivariate_normal(mean, cov, shape=[]): """multivariate_normal(mean, cov) or multivariate_normal(mean, cov, [m, n, ...]) returns an array containing multivariate normally distributed random numbers with specified mean and covariance. mean must be a 1 dimensional array. cov must be a square two dimensional array with the same number of rows and columns as mean has elements. The first form returns a single 1-D array containing a multivariate normal. The second form returns an array of shape (m, n, ..., cov.shape[0]). In this case, output[i,j,...,:] is a 1-D array containing a multivariate normal.""" # Check preconditions on arguments mean = Numeric.array(mean) cov = Numeric.array(cov) if len(mean.shape) != 1: raise ArgumentError, "mean must be 1 dimensional." if (len(cov.shape) != 2) or (cov.shape[0] != cov.shape[1]): raise ArgumentError, "cov must be 2 dimensional and square." if mean.shape[0] != cov.shape[0]: raise ArgumentError, "mean and cov must have same length." # Compute shape of output if isinstance(shape, IntType): shape = [shape] final_shape = list(shape[:]) final_shape.append(mean.shape[0]) # Create a matrix of independent standard normally distributed random # numbers. The matrix has rows with the same length as mean and as # many rows are necessary to form a matrix of shape final_shape. x = ranlib.standard_normal(Numeric.multiply.reduce(final_shape)) x.shape = (Numeric.multiply.reduce(final_shape[0:len(final_shape) - 1]), mean.shape[0]) # Transform matrix of standard normals into matrix where each row # contains multivariate normals with the desired covariance. # Compute A such that matrixmultiply(transpose(A),A) == cov. # Then the matrix products of the rows of x and A has the desired # covariance. Note that sqrt(s)*v where (u,s,v) is the singular value # decomposition of cov is such an A. (u, s, v) = LinearAlgebra.singular_value_decomposition(cov) x = Numeric.matrixmultiply(x * Numeric.sqrt(s), v) # The rows of x now have the correct covariance but mean 0. Add # mean to each row. Then each row will have mean mean. Numeric.add(mean, x, x) x.shape = final_shape return x
def multivariate_normal(mean, cov, shape=[]): """multivariate_normal(mean, cov) or multivariate_normal(mean, cov, [m, n, ...]) returns an array containing multivariate normally distributed random numbers with specified mean and covariance. mean must be a 1 dimensional array. cov must be a square two dimensional array with the same number of rows and columns as mean has elements. The first form returns a single 1-D array containing a multivariate normal. The second form returns an array of shape (m, n, ..., cov.shape[0]). In this case, output[i,j,...,:] is a 1-D array containing a multivariate normal.""" # Check preconditions on arguments mean = Numeric.array(mean) cov = Numeric.array(cov) if len(mean.shape) != 1: raise ArgumentError, "mean must be 1 dimensional." if (len(cov.shape) != 2) or (cov.shape[0] != cov.shape[1]): raise ArgumentError, "cov must be 2 dimensional and square." if mean.shape[0] != cov.shape[0]: raise ArgumentError, "mean and cov must have same length." # Compute shape of output if isinstance(shape, IntType): shape = [shape] final_shape = list(shape[:]) final_shape.append(mean.shape[0]) # Create a matrix of independent standard normally distributed random # numbers. The matrix has rows with the same length as mean and as # many rows are necessary to form a matrix of shape final_shape. x = ranlib.standard_normal(Numeric.multiply.reduce(final_shape)) x.shape = (Numeric.multiply.reduce(final_shape[0:len(final_shape)-1]), mean.shape[0]) # Transform matrix of standard normals into matrix where each row # contains multivariate normals with the desired covariance. # Compute A such that matrixmultiply(transpose(A),A) == cov. # Then the matrix products of the rows of x and A has the desired # covariance. Note that sqrt(s)*v where (u,s,v) is the singular value # decomposition of cov is such an A. (u,s,v) = LinearAlgebra.singular_value_decomposition(cov) x = Numeric.matrixmultiply(x*Numeric.sqrt(s),v) # The rows of x now have the correct covariance but mean 0. Add # mean to each row. Then each row will have mean mean. Numeric.add(mean,x,x) x.shape = final_shape return x
def fitWithGradient(self, object, r0=0.4, w_neg=1.): r = self._rGrid() atom_map = N.zeros(self.data.shape, N.Float) cutoff = 4. * r0 for a in object.atomList(): ra = a.position().array xi1 = N.sum(self.x_axis < ra[0] - cutoff) xi2 = N.sum(self.x_axis < ra[0] + cutoff) yi1 = N.sum(self.y_axis < ra[1] - cutoff) yi2 = N.sum(self.y_axis < ra[1] + cutoff) zi1 = N.sum(self.z_axis < ra[2] - cutoff) zi2 = N.sum(self.z_axis < ra[2] + cutoff) if xi2 > xi1 and yi2 > yi1 and zi2 > zi1: dr = r[xi1:xi2, yi1:yi2, zi1:zi2] - \ ra[N.NewAxis, N.NewAxis, N.NewAxis, :] w = N.exp(-0.5 * N.sum(dr**2, axis=-1) / r0**2) lmap = atom_map[xi1:xi2, yi1:yi2, zi1:zi2] N.add(lmap, w, lmap) norm_factor = 1. / N.sum(N.sum(N.sum(atom_map))) N.multiply(atom_map, norm_factor, atom_map) N.subtract(atom_map, self.data, atom_map) weight = 1. + (w_neg - 1.) * N.less(atom_map, 0.) N.multiply(atom_map, weight, atom_map) g = ParticleVector(object.universe()) for a in object.atomList(): ra = a.position().array xi1 = N.sum(self.x_axis < ra[0] - cutoff) xi2 = N.sum(self.x_axis < ra[0] + cutoff) yi1 = N.sum(self.y_axis < ra[1] - cutoff) yi2 = N.sum(self.y_axis < ra[1] + cutoff) zi1 = N.sum(self.z_axis < ra[2] - cutoff) zi2 = N.sum(self.z_axis < ra[2] + cutoff) if xi2 > xi1 and yi2 > yi1 and zi2 > zi1: dr = r[xi1:xi2, yi1:yi2, zi1:zi2] - \ ra[N.NewAxis, N.NewAxis, N.NewAxis, :] lmap = atom_map[xi1:xi2, yi1:yi2, zi1:zi2] lw = weight[xi1:xi2, yi1:yi2, zi1:zi2] w = N.exp(-0.5 * N.sum(dr**2, axis=-1) / r0**2) v = N.sum( N.sum( N.sum(lmap[..., N.NewAxis] * lw[..., N.NewAxis] * dr * w[..., N.NewAxis]))) g[a] = Vector(v) return N.sum(N.sum(N.sum(atom_map**2))), -2 * norm_factor * g / r0**2
def testOperators (self): "Test the operators +, -, *, /, %, ^, &, |" x = Numeric.array([1.,2.,3.,4.,5.,6.]) y = Numeric.array([-1.,2.,0.,2.,-1, 3.]) assert_eq(x + y, [0., 4., 3., 6., 4., 9.]) assert_eq(x - y, [2., 0., 3., 2., 6., 3.]) assert_eq(x * y, [-1., 4., 0., 8., -5., 18.]) assert_eq(y / x, [-1, 1., 0., .5, -.2, .5]) assert_eq(x**2, [1., 4., 9., 16., 25., 36.]) xc = Numeric.array([1.,2.,3.,4.,5.,6.]) xc += y assert_eq(xc, x + y) xc = Numeric.array([1.,2.,3.,4.,5.,6.]) xc -= y assert_eq(xc, x - y) xc = Numeric.array([1.,2.,3.,4.,5.,6.]) xc *= y assert_eq(xc, x * y) yc = Numeric.array(y) yc /= x assert_eq( yc, y / x) assert_eq(x + y, Numeric.add(x, y)) assert_eq(x - y, Numeric.subtract(x, y)) assert_eq(x * y, Numeric.multiply(x, y)) assert_eq(y / x, Numeric.divide (y, x)) self.failUnlessRaises(ZeroDivisionError, Numeric.divide, Numeric.array(1), Numeric.array(0)) assert_eq(x**2, Numeric.power(x,2)) x = Numeric.array([1,2]) y = Numeric.zeros((2,)) assert_eq(x%x, y) assert_eq(Numeric.remainder(x,x), y) assert_eq(x <<1, [2,4]) assert_eq(Numeric.left_shift(x,1), [2,4]) assert_eq(x >>1, [0,1]) assert_eq(Numeric.right_shift(x,1), [0,1]) assert_eq(x & 2, [0,2]) assert_eq(Numeric.bitwise_and (x, 2), [0,2]) assert_eq(x | 1, [1,3]) assert_eq(Numeric.bitwise_or (x, 1), [1,3]) assert_eq(x ^ 2, [3,0]) assert_eq(Numeric.bitwise_xor(x,2), [3,0]) x = divmod(Numeric.array([2,1]), Numeric.array([1,2])) assert_eq(x[0], [2,0]) assert_eq(x[1], [0,1]) assert (4L*Numeric.arange(3)).typecode() == Numeric.PyObject x = Numeric.array([1,2,3,4,5,6],'u') y = Numeric.array([1,2,0,2,2,3],'u') assert_eq(x + y, [2, 4, 3, 6, 7, 9]) assert_eq(x - y, [0, 0, 3, 2, 3, 3]) assert_eq(x * y, [1, 4, 0, 8, 10, 18]) assert_eq(y / x, [1, 1, 0, 0, 0, 0]) assert_eq(y // x, [1, 1, 0, 0, 0, 0]) assert_eq(x**2, [1, 4, 9, 16, 25, 36])
def atomMap(self, object, r0=0.3): r = self._rGrid() atom_map = N.zeros(self.data.shape, N.Float) cutoff = 4. * r0 for a in object.atomList(): # An over-eager optimization: it should use # an enlarged box #if not self.box.enclosesPoint(a.position()): # continue ra = a.position().array xi1 = N.sum(self.x_axis < ra[0] - cutoff) xi2 = N.sum(self.x_axis < ra[0] + cutoff) yi1 = N.sum(self.y_axis < ra[1] - cutoff) yi2 = N.sum(self.y_axis < ra[1] + cutoff) zi1 = N.sum(self.z_axis < ra[2] - cutoff) zi2 = N.sum(self.z_axis < ra[2] + cutoff) if xi2 > xi1 and yi2 > yi1 and zi2 > zi1: dr = r[xi1:xi2, yi1:yi2, zi1:zi2] - \ ra[N.NewAxis, N.NewAxis, N.NewAxis, :] w = N.exp(-0.5 * N.sum(dr**2, axis=-1) / r0**2) lmap = atom_map[xi1:xi2, yi1:yi2, zi1:zi2] N.add(lmap, w, lmap) N.divide(atom_map, N.sum(N.sum(N.sum(atom_map))), atom_map) return atom_map
def text_to_image(in_file, datatype, rastercopy): """ turns a textfile into a geotiff image. Assumes that the input will have three locational columns (pixel_ID, Col, Row) and a single value column (Value) inputs: 1) the input txt file to process 2) the datatype as an integer choices are: float32 - 6 int32 - 5 int16 - 3 3) copycat image outputs: 1) geotiff image interacts with: text_to_list, array_to_pan """ # call txt_to_array function and write results to separate variables array_tuple = txt_to_list(in_file, datatype) count = array_tuple[0] col_max = array_tuple[1] row_max = array_tuple[2] val_list = array_tuple[3] # create an array full of zeros the length of the list generated val_line_array = Numeric.zeros((1, count)) # add the list to the array val_line_array = Numeric.add(val_line_array, val_list) # resize the array using the col and rows recorded earlier val_squ_array = Numeric.resize(val_line_array, (row_max, col_max)) # generate outname inprefix, insuffix = path.splitext(in_file) outname = inprefix + '_img.tif' # call function to create output image from array array_to_pan(rastercopy, outname, val_squ_array, datatype) print "%s generated" % (outname) return
def text_to_image(in_file, datatype ,rastercopy): """ turns a textfile into a geotiff image. Assumes that the input will have three locational columns (pixel_ID, Col, Row) and a single value column (Value) inputs: 1) the input txt file to process 2) the datatype as an integer choices are: float32 - 6 int32 - 5 int16 - 3 3) copycat image outputs: 1) geotiff image interacts with: text_to_list, array_to_pan """ #call txt_to_array function and write results to separate variables array_tuple = txt_to_list(in_file,idatatype) count = array_tuple[0] col_max = array_tuple[1] row_max = array_tuple[2] val_list = array_tuple[3] #create an array full of zeros the length of the list generated val_line_array = Numeric.zeros((1,count)) #add the list to the array val_line_array = Numeric.add(val_line_array, val_list) #resize the array using the col and rows recorded earlier val_squ_array = Numeric.resize(val_line_array, (row_max,col_max)) #generate outname inprefix, insuffix = path.splitext(in_file) outname = inprefix + '_img.tif' #call function to create output image from array array_to_pan(rastercopy,outname,val_squ_array,datatype) print "%s generated" % (outname) return
def make_fiber(verts_in, r_start = 0.01, r_end = 0.01, gon = 5): n = len(verts_in) directions = normalize(num.subtract(verts_in[1:], verts_in[:-1])) bisectors = normalize(num.add(directions[1:], directions[:-1])) d = directions[0] if abs(num.dot(d, [1,0,0])) > 0.9: u = cross(d, [0,1,0]) else: u = cross(d, [1,0,0]) v = cross(d, u) u, v = normalize(num.array([u, v])) a = 2 * math.pi / gon section = num.array([u * math.cos(a * i) + v * math.sin(a * i) for i in xrange(gon)]) verts_out = num.zeros([n * gon, 3], "double") normals = num.zeros([n * gon, 3], "double") verts_out[:gon] = section * r_start + verts_in[0] normals[:gon] = section for i in xrange(n - 2): d = bisectors[i] u = directions[i] f = num.sum(section * d, 1)[:, num.NewAxis] section = section - f / num.dot(d, u) * u r = ((i + 1) * r_end + (n - i - 1) * r_start) / n verts_out[(i + 1)*gon : (i + 2)*gon] = section * r + verts_in[i + 1] normals[(i + 1)*gon : (i + 2)*gon] = section d = directions[-1] section = section - num.sum(section * d, 1)[:, num.NewAxis] * d verts_out[-gon:] = section * r_end + verts_in[-1] normals[-gon:] = section return verts_out, normalize(normals), fiber_polygons(n - 1, gon)
def __init__(self, trajectory, object, first=0, last=None, skip=1, reference = None): self.trajectory = trajectory universe = trajectory.universe if last is None: last = len(trajectory) first_conf = trajectory.configuration[first] offset = universe.contiguousObjectOffset([object], first_conf, 1) if reference is None: reference = first_conf reference = universe.contiguousObjectConfiguration([object], reference) steps = (last-first+skip-1)/skip mass = object.mass() ref_cms = object.centerOfMass(reference) atoms = object.atomList() possq = Numeric.zeros((steps,), Numeric.Float) cross = Numeric.zeros((steps, 3, 3), Numeric.Float) rcms = Numeric.zeros((steps, 3), Numeric.Float) # cms of the CONTIGUOUS object made of CONTINUOUS atom trajectories for a in atoms: r = trajectory.readParticleTrajectory(a, first, last, skip, "box_coordinates").array w = a._mass/mass Numeric.add(rcms, w*r, rcms) if offset is not None: Numeric.add(rcms, w*offset[a].array, rcms) # relative coords of the CONTIGUOUS reference r_ref = Numeric.zeros((len(atoms), 3), Numeric.Float) for a in range(len(atoms)): r_ref[a] = atoms[a].position(reference).array - ref_cms.array # main loop: storing data needed to fill M matrix for a in range(len(atoms)): r = trajectory.readParticleTrajectory(atoms[a], first, last, skip, "box_coordinates").array r = r - rcms # (a-b)**2 != a**2 - b**2 if offset is not None: Numeric.add(r, offset[atoms[a]].array,r) trajectory._boxTransformation(r, r) w = atoms[a]._mass/mass Numeric.add(possq, w*Numeric.add.reduce(r*r, -1), possq) Numeric.add(possq, w*Numeric.add.reduce(r_ref[a]*r_ref[a],-1), possq) Numeric.add(cross, w*r[:,:,Numeric.NewAxis]*r_ref[Numeric.NewAxis, a,:],cross) self.trajectory._boxTransformation(rcms, rcms) # filling matrix M (formula no 40) k = Numeric.zeros((steps, 4, 4), Numeric.Float) k[:, 0, 0] = -cross[:, 0, 0]-cross[:, 1, 1]-cross[:, 2, 2] k[:, 0, 1] = cross[:, 1, 2]-cross[:, 2, 1] k[:, 0, 2] = cross[:, 2, 0]-cross[:, 0, 2] k[:, 0, 3] = cross[:, 0, 1]-cross[:, 1, 0] k[:, 1, 1] = -cross[:, 0, 0]+cross[:, 1, 1]+cross[:, 2, 2] k[:, 1, 2] = -cross[:, 0, 1]-cross[:, 1, 0] k[:, 1, 3] = -cross[:, 0, 2]-cross[:, 2, 0] k[:, 2, 2] = cross[:, 0, 0]-cross[:, 1, 1]+cross[:, 2, 2] k[:, 2, 3] = -cross[:, 1, 2]-cross[:, 2, 1] k[:, 3, 3] = cross[:, 0, 0]+cross[:, 1, 1]-cross[:, 2, 2] del cross for i in range(1, 4): for j in range(i): k[:, i, j] = k[:, j, i] Numeric.multiply(k, 2., k) for i in range(4): Numeric.add(k[:,i,i], possq, k[:,i,i]) del possq quaternions = Numeric.zeros((steps, 4), Numeric.Float) fit = Numeric.zeros((steps,), Numeric.Float) import LinearAlgebra for i in range(steps): e, v = LinearAlgebra.eigenvectors(k[i]) j = Numeric.argmin(e) if e[j] < 0.: fit[i] = 0. else: fit[i] = Numeric.sqrt(e[j]) if v[j,0] < 0.: quaternions[i] = -v[j] # eliminate jumps else: quaternions[i] = v[j] self.fit = fit self.cms = rcms self.quaternions = quaternions
def translateBy(self, vector): """Adds |vector| to the values at all steps. This does *not* change the data in the trajectory file.""" Numeric.add(self.array, vector.array[Numeric.NewAxis, :], self.array)
def test1(*s): "Test of basic array creation and arithmetic." x=Numeric.array([1.,1.,1.,-2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.]) y=Numeric.array([5.,0.,3., 2., -1., -4., 0., -10., 10., 1., 0., 3.]) a10 = 10. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0 ,0, 1] xm = array(x, mask=m1) ym = array(y, mask=m2) z = Numeric.array([-.5, 0., .5, .8]) zm = array(z, mask=[0,1,0,0]) xf = Numeric.where(m1, 1.e+20, x) xm.set_fill_value(1.e+20) for item in [x, y, xm, ym, xf]: item.shape = s assert isMaskedArray(x) == 0 assert isMaskedArray(xm) == 1 assert shape(xm) == s assert xm.shape == s assert size(xm) == reduce(lambda x,y:x*y, s) assert xm.size() == size(xm) assert size(xm,0) == s[0] assert xm.size(0) == size(xm,0) assert count(xm) == len(m1) - reduce(lambda x,y:x+y, m1) if rank(xm) > 1: assert count(xm,0) == size(xm,0) - reduce(lambda x,y:x+y, xm.mask()[0]) alltest(xm, xf) alltest(filled(xm, 1.e20), xf) alltest(x, xm) alltest(-x, -xm) alltest(x + y, xm + ym) alltest(x - y, xm - ym) alltest(x * y, xm * ym) alltest(x / y, xm / ym) alltest(a10 + y, a10 + ym) alltest(a10 - y, a10 - ym) alltest(a10 * y, a10 * ym) alltest(a10 / y, a10 / ym) alltest(x + a10, xm + a10) alltest(x - a10, xm - a10) alltest(x * a10, xm * a10) alltest(x / a10, xm / a10) a2d = array([[1,2],[0,4]], Float) a2dm = masked_array(a2d, [[0,0],[1,0]]) alltest (a2d * a2d, a2d * a2dm) alltest (a2d + a2d, a2d + a2dm) alltest (a2d - a2d, a2d - a2dm) alltest(x**2, xm**2) alltest(abs(x)**2.5, abs(xm) **2.5) alltest(x**y, xm**ym) alltest(Numeric.add(x,y), add(xm, ym)) alltest(Numeric.subtract(x,y), subtract(xm, ym)) alltest(Numeric.multiply(x,y), multiply(xm, ym)) alltest(Numeric.divide(x,y), divide(xm, ym)) alltest(Numeric.cos(x), cos(xm)) alltest(Numeric.cosh(x), cosh(xm)) alltest(Numeric.sin(x), sin(xm)) alltest(Numeric.sinh(x), sinh(xm)) alltest(Numeric.tan(x), tan(xm)) alltest(Numeric.tanh(x), tanh(xm)) alltest(Numeric.sqrt(abs(x)), sqrt(xm)) alltest(Numeric.log(abs(x)), log(xm)) alltest(Numeric.log10(abs(x)), log10(xm)) alltest(Numeric.exp(x), exp(xm)) alltest(Numeric.arcsin(z), arcsin(zm)) alltest(Numeric.arccos(z), arccos(zm)) alltest(Numeric.arctan(z), arctan(zm)) alltest(Numeric.arctan2(x, y), arctan2(xm, ym)) alltest(Numeric.absolute(x), absolute(xm)) alltest(Numeric.equal(x,y), equal(xm, ym)) alltest(Numeric.not_equal(x,y), not_equal(xm, ym)) alltest(Numeric.less(x,y), less(xm, ym)) alltest(Numeric.greater(x,y), greater(xm, ym)) alltest(Numeric.less_equal(x,y), less_equal(xm, ym)) alltest(Numeric.greater_equal(x,y), greater_equal(xm, ym)) alltest(Numeric.conjugate(x), conjugate(xm)) alltest(Numeric.concatenate((x,y)), concatenate((xm,ym))) alltest(Numeric.concatenate((x,y)), concatenate((x,y))) alltest(Numeric.concatenate((x,y)), concatenate((xm,y))) alltest(Numeric.concatenate((x,y,x)), concatenate((x,ym,x))) ott = array([0.,1.,2.,3.], mask=[1,0,0,0]) assert isinstance(count(ott), types.IntType) alltest(3, count(ott)) alltest(1, count(1)) alltest(0, array(1,mask=[1])) ott.shape = (2,2) assert isMaskedArray(count(ott,0)) assert isinstance(count(ott), types.IntType) alltest(3, count(ott)) assert getmask(count(ott,0)) is None alltest([1,2],count(ott,0)) xr = Numeric.ravel(x) #max doesn't work if shaped xmr = ravel(xm) alltest(max(xr), maximum(xmr)) #true because of careful selection of data alltest(min(xr), minimum(xmr)) #true because of careful selection of data alltest(Numeric.add.reduce(x), add.reduce(x)) alltest(Numeric.add.accumulate(x), add.accumulate(x)) alltest(4, sum(array(4))) alltest(4, sum(array(4), axis=0)) alltest(Numeric.sum(x), sum(x)) alltest(Numeric.sum(filled(xm,0)), sum(xm)) alltest(Numeric.sum(x,0), sum(x,0)) alltest(Numeric.product(x), product(x)) alltest(Numeric.product(x,0), product(x,0)) alltest(Numeric.product(filled(xm,1)), product(xm)) if len(s) > 1: alltest(Numeric.concatenate((x,y),1), concatenate((xm,ym),1)) alltest(Numeric.add.reduce(x,1), add.reduce(x,1)) alltest(Numeric.sum(x,1), sum(x,1)) alltest(Numeric.product(x,1), product(x,1))
def translateBy(self, vector): conf = self.configuration().array Numeric.add(conf, vector.array[Numeric.NewAxis, :], conf)
import ranlib