我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用numba.int64()。
def encode_jit(self, nr_samples, lengths, out, positions): """ out[samples, reads, bin_nodes+tria_nodes] Need to use this function to call the jit compiled function, as class support via numba is disabled sice 0.12""" if self.double_range: encode_double_triangles_jit(n_binary_bits=np.int64(self.n_binary_bits), n_inputnodes=np.int64(self.data_nodes), tria_nodes_end=self.data_nodes-self.n_binary_bits-1, #this is the last tria index -> nr nodes - 1 node_range=np.float64(self.node_range), nr_samples=np.int64(nr_samples), lengths=np.uint32(lengths), out=np.float32(out), positions=np.uint32(positions)) else: encode_triangles_jit(n_binary_bits=np.int64(self.n_binary_bits), n_inputnodes=np.int64(self.data_nodes), node_range=np.float64(self.node_range), nr_samples=np.int64(nr_samples), lengths=np.uint32(lengths), out=np.float32(out), positions=np.uint32(positions))
def encode_overlapping_binary_jit(n_inputnodes, nr_samples, lengths, out, positions): # out: float32[:,:,:] with [samples, timesteps, features] # pos_ind: int64 at which position in out['features'] the position # is stored and encoding should start # lengths: lengths without tickersteps - tickersteps will be added here mask = 1 n_bits = (n_inputnodes / 2) + 1 #lowbit only in binary for s_i in range(nr_samples): # Enocde position for t_i in range(lengths[s_i]): # normal bitwise for all bits for n_i in range(n_bits): out[s_i, t_i, n_i-n_inputnodes] = ((mask << n_i) & positions[s_i, t_i]) != 0 # overlapping bitwise (low bit ignored) for n_i in range(1, n_bits): # for bit at bitposition bp add 2**(bp-1) = 1<<(bp-1) out[s_i, t_i, n_i-n_bits] = ((mask << n_i) & (positions[s_i, t_i] + (mask << (n_i-1)))) != 0
def r_shape_cubic(cell_position, index): flip_factor = 1. ir = int64(math.floor(cell_position)) - 1 if index == 0: if ir < 0: flip_factor = -1. return flip_factor*(-1./6.)*((cell_position-ir)-2)**3 if index == 1: if ir+1 < 0: flip_factor = -1. return flip_factor*(1./6.)*(3*((cell_position-(ir+1))**3)-6*((cell_position-(ir+1))**2)+4) if index == 2: if ir+2 < 0: flip_factor = -1. return flip_factor*(1./6.)*(3*(((ir+2)-cell_position)**3)-6*(((ir+2)-cell_position)**2)+4) if index == 3: if ir+3 < 0: flip_factor = -1. return flip_factor*(-1./6.)*(((ir+3)-cell_position)-2)**3 # ------------------------------- # Field deposition - linear - rho # -------------------------------
def ang2pix_ring(nside, theta, phi): """Calculate the pixel indexes in RING ordering scheme for each pair of angular coordinates on the sphere. Parameters ---------- theta : 1D or 2D `~numpy.ndarray` The polar angles (i.e., latitudes), ? ? [0, ?]. (unit: rad) phi : 1D or 2D `~numpy.ndarray` The azimuthal angles (i.e., longitudes), ? ? [0, 2?). (unit: rad) Returns ------- ipix : 1D or 1D `~numpy.ndarray` The indexes of the pixels corresponding to the input coordinates. The shape is the same as the input array. NOTE ---- * Only support the *RING* ordering scheme * This is the JIT-optimized version that partially replaces the ``healpy.ang2pix`` """ shape = theta.shape size = theta.size theta = theta.flatten() phi = phi.flatten() ipix = np.zeros(size, dtype=np.int64) for i in range(size): ipix[i] = ang2pix_ring_single(nside, theta[i], phi[i]) return ipix.reshape(shape)
def __init__(self, ticker_steps=0, tstep_resolution=1.0, min_dif=None, node_range=None, nr_tria_nodes=None, add_binary_encoding=True, double_range=False): if min_dif != None: self.node_range = np.ceil(np.float64(tstep_resolution) / min_dif) # half the (symmetric) range of the node elif node_range != None: self.node_range = np.ceil(np.float64(node_range) / 2.) elif nr_tria_nodes != None: self.node_range = None self.nr_tria_nodes = nr_tria_nodes else: raise(ValueError, "min_dif or node_range have to be specified!") self.ticker_steps = np.int64(ticker_steps) self.add_binary_encoding = add_binary_encoding self.double_range = double_range
def calculate_parameters(self, max_val): if self.node_range == None: self.node_range = np.ceil(np.float64(max_val) / self.nr_tria_nodes) if self.add_binary_encoding: self.n_binary_bits = np.int64(np.ceil(np.log2(self.node_range))) #encode one triangle range binary for more precision else: self.n_binary_bits = 0 self.helper_nodes = (self.ticker_steps > 0) self.max_val = np.float64(max_val) self.data_nodes = np.ceil(self.max_val / self.node_range) + 1 + self.n_binary_bits # +1 for right side node (no wrap-around) self.n_inputnodes = np.int64(self.data_nodes + self.helper_nodes)
def encode_overlapping_binary_inplace_jit(ticker_steps, n_inputnodes, nr_samples, lengths, out, out_uint): # out: float32[:,:,:] with [samples, timesteps, features] # pos_ind: int64 at which position in out['features'] the position # is stored and encoding should start # lengths: lengths without tickersteps - tickersteps will be added here # Note: positions=0 are ignored mask = 1 pos_bit = n_inputnodes if ticker_steps > 0: n_inputnodes -= 1 n_bits = (n_inputnodes / 2) + 1 #lowbit only in binary for s_i in range(nr_samples): # Enocde position for t_i in range(lengths[s_i]): orig_int = out_uint[s_i, t_i, -pos_bit] #to be encoded # normal bitwise for all bits for n_i in range(n_bits): out[s_i, t_i, n_i-n_inputnodes] = ((mask << n_i) & orig_int) != 0 # overlapping bitwise (low bit ignored) for n_i in range(1, n_bits): # for bit at bitposition bp add 2**(bp-1) = 1<<(bp-1) out[s_i, t_i, n_i-n_bits] = ((mask << n_i) & (orig_int + (mask << (n_i-1)))) != 0 out[s_i, t_i, -pos_bit] = 0 #remove position # Set ticker steps, if they exist if ticker_steps > 0: for n_i in range(ticker_steps): out[s_i, lengths[s_i]+n_i, -pos_bit] = 1 # add ticker steps to lenght lengths[s_i] += n_i # := lengths[s_i] += lengths[s_i-1 for index of last element instead of length
def calculate_parameters(self, max_val): self.exp = np.int64(np.ceil(np.log2(max_val))) self.data_nodes = self.exp * 2 - 1 self.n_inputnodes = np.int64(self.data_nodes + self.helper_nodes) self.max_val = max_val
def z_shape_linear(cell_position, index): iz = int64(math.floor(cell_position)) if index == 0: return iz+1.-cell_position if index == 1: return cell_position - iz
def r_shape_linear(cell_position, index): flip_factor = 1. ir = int64(math.floor(cell_position)) if index == 0: if ir < 0: flip_factor = -1. return flip_factor*(ir+1.-cell_position) if index == 1: return flip_factor*(cell_position - ir) # Cubic shapes
def z_shape_cubic(cell_position, index): iz = int64(math.floor(cell_position)) - 1 if index == 0: return (-1./6.)*((cell_position-iz)-2)**3 if index == 1: return (1./6.)*(3*((cell_position-(iz+1))**3)-6*((cell_position-(iz+1))**2)+4) if index == 2: return (1./6.)*(3*(((iz+2)-cell_position)**3)-6*(((iz+2)-cell_position)**2)+4) if index == 3: return (-1./6.)*(((iz+3)-cell_position)-2)**3
def _calc_hpx_indexes(nside): """Calculate HEALPix element indexes for the HPX projection scheme. Parameters ---------- nside : int Nside of the input/output HEALPix data Returns ------- indexes : 2D `~numpy.ndarray` 2D integer array of same size as the input/output HPX FITS image, with elements tracking the indexes of the HPX pixels in the HEALPix 1D array, while elements with value "-1" indicating null/empty HPX pixels. NOTE ---- * The indexes are 0-based; * Currently only HEALPix RING ordering supported; * The null/empty elements in the HPX projection are filled with "-1". """ # number of horizontal/vertical facet nfacet = 5 # Facets layout of the HPX projection scheme. # Note that this appears to be upside-down, and the blank facets # are marked with "-1". # Ref: ref.[2], Fig.4 # # XXX: # Cannot use the nested list here, which fails with ``numba`` error: # ``NotImplementedError: unsupported nested memory-managed object`` FACETS_LAYOUT = np.zeros((nfacet, nfacet), dtype=np.int64) FACETS_LAYOUT[0, :] = [6, 9, -1, -1, -1] FACETS_LAYOUT[1, :] = [1, 5, 8, -1, -1] FACETS_LAYOUT[2, :] = [-1, 0, 4, 11, -1] FACETS_LAYOUT[3, :] = [-1, -1, 3, 7, 10] FACETS_LAYOUT[4, :] = [-1, -1, -1, 2, 6] # shape = (nfacet*nside, nfacet*nside) indexes = -np.ones(shape, dtype=np.int64) # # Loop vertically facet-by-facet for jfacet in range(nfacet): # Loop row-by-row for j in range(nside): row = jfacet * nside + j # Loop horizontally facet-by-facet for ifacet in range(nfacet): facet = FACETS_LAYOUT[jfacet, ifacet] if facet < 0: # blank facet pass else: idx = _calc_hpx_row_idx(nside, facet, j) col = ifacet * nside indexes[row, col:(col+nside)] = idx # return indexes
def make_grid_ellipse(center, size, resolution, rotation=0.0): """ Make a square coordinate grid just containing the specified (rotated) ellipse. Parameters ---------- center : 2-float tuple Center coordinate (longitude, latitude) of the grid, with longitude [0, 360) degree, latitude [-90, 90] degree. size : 2-float tuple The (major, minor) axes of the filling ellipse, unit [ degree ]. resolution : float The grid resolution, unit [ degree ]. rotation : float The rotation angle (unit [ degree ]) of the filling ellipse. Returns ------- lon : 2D `~numpy.ndarray` The array with elements representing the longitudes of each grid pixel. The array is odd-sized and square, with the input center locating at the exact grid central pixel. Also, the longitudes are fixed to be in the valid range [0, 360). lat : 2D `~numpy.ndarray` The array with elements representing the latitudes of each grid pixel. Also, the latitudes are fixed to be in the valid range [-90, 90]. gridmap : 2D float `~numpy.ndarray` The array containing the specified ellipse, where the pixels corresponding to the ellipse with positive values, while other pixels are zeros. NOTE ---- The generated grid is square, determined by the major axis of the ellipse, therefore, we can simply rotate the ellipse without reshaping. """ size_major = max(size) size = (size_major, size_major) lon, lat = make_coordinate_grid(center, size, resolution) shape = lon.shape # Fill the ellipse into the grid r0, c0 = np.floor(np.array(shape) / 2.0).astype(np.int64) radii = np.ceil(0.5*np.array(size)/resolution).astype(np.int64) rr, cc = ellipse((r0, c0), (radii[0], radii[1]), shape=shape) gridmap = np.zeros(shape) # XXX: ``numba`` only support one advanced index for ri, ci in zip(rr, cc): gridmap[ri, ci] = 1.0 # Rotate the ellipse about the grid center gridmap = rotate_center(gridmap, angle=rotation, interp=True, reshape=False, fill_value=0.0) return (lon, lat, gridmap)