def distRect_factory(nb_metric): """Can be passed a numba jit'd distance function and will return a jit'd function for computing all pairwise distances using that function""" @nb.jit(nb.boolean(nb.float64[:, :], nb.int8[:, :], nb.int8[:, :], nb.float64[:, :], nb.boolean), nopython=True) def nb_distRect(pwdist, rows, cols, substMat, symetric): n = rows.shape[0] m = cols.shape[0] for i in range(n): for j in range(m): if not symetric: pwdist[i, j] = nb_seq_similarity(rows[i, :], cols[j, :], substMat=substMat, normed=False, asDistance=True) else: if j <= i: pwdist[i, j] = nb_seq_similarity(rows[i, :], cols[j, :], substMat=substMat, normed=False, asDistance=True) pwdist[j, i] = pwdist[i, j] return True return nb_distRect
def test_call_extern_c_fn_twice(self): global memcmp memcmp = cffi_support.ExternCFunction('memcmp', 'int memcmp ( const uint8_t * ptr1, const uint8_t * ptr2, size_t num )') @udf(boolean(FunctionContext, StringVal, StringVal)) def fn(context, a, b): c = memcmp(a.ptr, a.ptr, a.len) == 0 d = memcmp(a.ptr, b.ptr, a.len) == 0 return c or d
def all_short_circuit_merges(MERGE=None): """Determine if all MERGE flags are can be skipped if data is None. This allows us to skip the data merge step if any of the contributing data is None. However, if any of the columns are True, then we must propagate all data columns. """ if MERGE is None: return numba.boolean(DATA_DEFAULT & DATA_NANS_PROPAGATE) for M in MERGE: if not (M & DATA_NANS_PROPAGATE): return False return True
def distRect_factory(nb_metric): """Can be passed a numba jit'd distance function and will return a jit'd function for computing all pairwise distances using that function""" @nb.jit(nb.boolean(nb.float64[:,:], nb.int8[:,:], nb.int8[:,:], nb.float64[:,:], nb.boolean), nopython=True) def nb_distRect(pwdist, rows, cols, substMat, symetric): n = rows.shape[0] m = cols.shape[0] for i in range(n): for j in range(m): if not symetric: pwdist[i, j] = nb_seq_similarity(rows[i,:], cols[j,:], substMat=substMat, normed=False, asDistance=True) else: if j<=i: pwdist[i, j] = nb_seq_similarity(rows[i,:], cols[j,:], substMat=substMat, normed=False, asDistance=True) pwdist[j, i] = pwdist[i, j] return True return nb_distRect
class Methods: @staticmethod def apply(function, args, output): if len(args) == 3: if len(output) == 3: Methods._apply_f_3_3(function, *args, *output) else: raise NotImplementedError() else: raise NotImplementedError() @staticmethod @numba.njit(boolean(int64[:]), **{**conf.JIT_FLAGS, **{'parallel': False}}) def first_element_is_zero(array): return array[0] == 0 @staticmethod @numba.njit(**{**conf.JIT_FLAGS, **{'parallel': False, 'cache': False}}) def _apply_f_3_3(function, arg0, arg1, arg2, output0, output1, output2): for i in range(output0.shape[0]): output0[i], output1[i], output2[i] = function(arg0[i], arg1[i], arg2[i])
def inner(func): func_name = func.__name__ sig = numba.boolean( numba.types.uint64, numba.types.uint64, numba.types.CPointer(arg_type._numba_t), numba.types.CPointer(arg_type._numba_t), ) jitfunc = numba.jit(func, nopython=True) @numba.cfunc(sig, nopython=True) def wrapper(i, j, x, t): # pragma: no cover return jitfunc(i, j, x[0], t[0]) out = core_ffi.new("GxB_SelectOp*") lib.GxB_SelectOp_new( out, core_ffi.cast("GxB_select_function", wrapper.address), arg_type._gb_type, thunk_type, ) return SelectOp(func_name, out[0])
if i + 1 < n_sites: h_values.append(t) h_terms.append([ OP_C + i * SITE + s * SPIN, OP_D + (i + 1) * SITE + s * SPIN, -1, -1 ]) h_values.append(0.5 * u) h_terms.append([ OP_C + i * SITE + s * SPIN, OP_C + i * SITE + (1 - s) * SPIN, OP_D + i * SITE + (1 - s) * SPIN, OP_D + i * SITE + s * SPIN ]) return np.array(h_values, dtype=np.float64), np.array(h_terms, dtype=np.int32) @nb.njit(nb.boolean(nb.int32, nb.int32, nb.int32, nb.int32)) def qc_hamil_partition(i, j, k, l): if j == -1: return mrank == i % msize elif k == -1: if i <= j: return mrank == (j * (j + 1) // 2 + i) % msize else: return mrank == (i * (i + 1) // 2 + j) % msize else: x = np.array([i, j, k, l], dtype=np.int32) x.sort() if x[1] == x[2]: return mrank == x[1] % msize else: i, j = x[0], x[1]
import numpy as np import numba as nb import gym @nb.jit(nb.boolean(nb.i4[:, :], nb.i4, nb.i4)) def valid(board, x, y): board_size = board.shape[0] def _in_range(v): return (0 <= v) and (v < board_size) return _in_range(x) and _in_range(y) @nb.jit(nb.boolean(nb.i4[:, :], nb.i4, nb.i4)) def available(board, x, y): return valid(board, x, y) and board[x][y] == -1 # Or: @nb.jit(nb.boolean(nb.i4[:, :], nb.types.UniTuple(nb.i4, 2), nb.i4)) # Or: @nb.jit(nb.boolean(nb.i4[:, :], nb.typeof((1, 1)), nb.i4)) @nb.jit('boolean(i4[:, :], UniTuple(i4, 2), i4)') def check_win(board, pos, num_win): def _check_lines(pos, delta, player): x, y = pos dx, dy = delta count = 0 while valid(board, x, y): if board[x][y] == player: x += dx y += dy
@nb.jitclass(move_spec) class Move: """ Represents a move from a square to a square and possibly the promotion piece type. """ def __init__(self, from_square, to_square, promotion): self.from_square = from_square self.to_square = to_square self.promotion = promotion @njit(boolean(Move.class_type.instance_type, Move.class_type.instance_type)) def move_objects_equal(move1, move2): """ Checks if the two given move objects are equal. """ if move1.from_square == move2.from_square and move1.to_square == move2.to_square and move1.promotion == move2.promotion: return True return False @njit(Move.class_type.instance_type(uint8, uint8)) def create_move(from_square, to_square): """ For use when not using promotions """
[power] -> distance weighting factor: 1/distance**[power] [copy] -> False: a bit faster, but modifies 'grid' and 'mask' ''' assert grid.shape==mask.shape, 'grid and mask shape are different' border = np.zeros(shape=mask.shape, dtype=np.bool) if copy: #copy mask as well because if will be modified later: mask = mask.copy() grid = grid.copy() return _calc(grid, mask, border, kernel, power, maxIter) @jit(boolean(boolean[:,:], boolean[:,:]), nopython=True) def _createBorder(mask, border): #create a 1px-width-border around all mask=True values gx = mask.shape[0] gy = mask.shape[1] last_val = mask[0,0] val = last_val any_border = False for i in xrange(gx): for j in xrange(gy): val = mask[i,j] if val != last_val: if val: border[i,j] = True else:
np.sum(np.multiply(np.square(x_vals - mu_0), z_0_prob)) / n_0) if n_1 == 0.0: mu_1 = np.amax(x_vals) sigma_1 = 0.01 else: mu_1 = np.sum(np.multiply(x_vals, z_1_prob)) / n_1 sigma_1 = np.sqrt( np.sum(np.multiply(np.square(x_vals - mu_1), z_1_prob)) / n_1) pi_0 = n_0 / x_vals.size pi_1 = n_1 / x_vals.size return pi_0, pi_1, mu_0, mu_1, sigma_0, sigma_1 @jit(boolean(float64, float64, float64), nopython=True) def isclose(a, b, error): return error > np.abs(a - b) @jit(UniTuple(float64, 4)(float64[:], float64, float64, float64, float64, float64, float64), nopython=True) def em_algorithm_fixed_pi_numba(x_vals, pi_0, pi_1, mu_0, mu_1, sigma_0, sigma_1): mu_0_new, mu_1_new, sigma_0_new, sigma_1_new = 1.0, 1.0, 1.0, 1.0 iteration = 0 while not (isclose(mu_0, mu_0_new, .01) and isclose(mu_1, mu_1_new, .01) and isclose(sigma_0, sigma_0_new, .01) and isclose(sigma_1, sigma_1_new, .01) or iteration == 500): if iteration > 0:
recB = max(arr1[irec1, 0], arr2[irec2, 0] - i) recE = min(arr1[irec1, 1], arr2[irec2, 1] - i) cor_rec[lagN + i] += recE - recB for irec2 in range(N2): if arr1[irec1, 0] >= arr2[irec2, 1] + i: continue if arr1[irec1, 1] <= arr2[irec2, 0] + i: break recB = max(arr1[irec1, 0], arr2[irec2, 0] + i) recE = min(arr1[irec1, 1], arr2[irec2, 1] + i) cor_rec[lagN - i] += recE - recB cor_rec[lagN] /= 2 return cor_rec @jit(boolean(int32, float32, float32[:], float32), nopython=True) def _CheckPrecNoise(lagN, dt, data, dist): """check precursory noise """ # trailing noise window Ndis = np.int32(np.floor((dist / 0.8 + 50.) / dt + 0.5)) if Ndis > lagN: return False Nb = max(np.int32(lagN * 4 / 5), Ndis) noiseSum = np.float32(0.) noiseN = lagN - Nb + 1 for i in range(noiseN): noiseSum += (data[i + lagN + Nb])**2 noiseSum += (data[lagN - i - Nb])**2 noiseSum = np.sqrt(noiseSum / (2 * (noiseN))) # precursory noise window
by Numba's JIT. The input 2D matrix with dimensions M x N expands to M x N*4. Dependencies: numpy, numba @author: Dr. Pranay S. Yadav """ # Import calls import numpy as np import numba as nb import ChaosFEX.chaotic_sampler as cs from ChaosFEX.input_validator import validate # Pure python func with typing to check inequality for compiling as numpy ufunc @nb.vectorize([nb.boolean(nb.float64, nb.float64, nb.float64)]) def _compare(value1, value2, value3): """ This function calculates absolute distance (L1), checks whether it is less than epsilon and returns a corresponding boolean. It operates over scalar floats and is used by _compute_match_idx for speedy iteration. Parameters ---------- value1 : scalar, float64 A single value from the feature matrix. value2 : scalar, float64 A single element from the trajectory array. value3 : scalar, float64 The value epsilon.
records.stack = resize(records.stack, records.capacity) stack_top = records.stack[records.top] stack_top["parent"] = parent stack_top["depth"] = depth stack_top["is_left"] = is_left stack_top["impurity"] = impurity stack_top["start_train"] = start_train stack_top["end_train"] = end_train stack_top["start_valid"] = start_valid stack_top["end_valid"] = end_valid records.top += 1 @jit( boolean(RecordsType), nopython=NOPYTHON, nogil=NOGIL, boundscheck=BOUNDSCHECK, fastmath=FASTMATH, ) def has_records(records): """Tests if the stack of records contain remaining records. Parameters ---------- records : Records A records dataclass containing the stack of node records Returns -------
accdFrac = (tset-pct) / denom accdCpn = accdFrac * couponAmounts[i] return accdCpn # TODO: NEED TO REVISIT THIS TODO return 0.0 print("t", tset) print("CPN TIMES", coupon_times) print("CPN AMNTS", couponAmounts) raise FinError("Failed to calculate accrued") ############################################################################### @njit(boolean(int64), fastmath=True, cache=True) def is_leap_year(y: int): """ Test whether year y is a leap year - if so return True, else False """ leap_year = ((y % 4 == 0) and (y % 100 != 0) or (y % 400 == 0)) return leap_year ############################################################################### @njit(float64[:](float64[:], float64), fastmath=True, cache=True) def scale(x: np.ndarray, factor: float): """ Scale all of the elements of an array by the same amount factor. """ x_scale = np.empty(len(x)) for i in range(0, len(x)): x_scale[i] = x[i] * factor
default_radius = 2. def random_from(pt, new_pt): # random - random is most likely to be 0 and falls off toward +-1 angle = (random() - random()) * pi angle_to_origin = pi + atan2(pt[Y], pt[X]) new_pt[X] = pt[X] + (new_pt[R] + pt[R]) * cos(angle_to_origin + angle) new_pt[Y] = pt[Y] + (new_pt[R] + pt[R]) * sin(angle_to_origin + angle) margin = 0.01 max_tries = 10 @jit(boolean(int64, float64[:])) def add_point(n, points): new_hit = True tries = 0 while new_hit and tries < max_tries: tries += 1 random_from(points[n - 1], points[n]) new_hit = False for i in range(n): dist = np.sqrt(np.sum((points[i][:R] - points[n][:R])**2)) if dist + margin < points[i][R] + points[n][R]: new_hit = True break return tries < max_tries
px+-kernel [power] -> distance weighting factor: 1/distance**[power] [copy] -> False: a bit faster, but modifies 'grid' and 'mask' ''' assert grid.shape == mask.shape, 'grid and mask shape are different' border = np.zeros(shape=mask.shape, dtype=np.bool) if copy: # copy mask as well because if will be modified later: mask = mask.copy() grid = grid.copy() return _calc(grid, mask, border, kernel, power, maxIter) @njit(boolean(boolean[:, :], boolean[:, :])) def _createBorder(mask, border): # create a 1px-width-border around all mask=True values gx = mask.shape[0] gy = mask.shape[1] last_val = mask[0, 0] val = last_val any_border = False for i in range(gx): for j in range(gy): val = mask[i, j] if val != last_val: if val: border[i, j] = True else:
class SpecialMethods: @staticmethod @numba.njit(int64(int64[:], int64[:], int64), **{**conf.JIT_FLAGS, **{'parallel': False}}) def remove_zeros(data, idx, length) -> int: new_length = 0 for i in range(length): if idx[i] == len(idx) or data[idx[i]] == 0: idx[i] = len(idx) else: new_length += 1 idx[:length].sort() return new_length @staticmethod @numba.njit(void(int64[:, :], float64[:, :], int64[:], int64, int64[:])) def flag_precipitated(cell_origin, position_in_cell, idx, length, healthy): for i in range(length): if cell_origin[i, -1] == 0 and position_in_cell[i, -1] < 0: idx[i] = len(idx) healthy[0] = 0 @staticmethod @numba.njit(void(int64[:], float64[:], int64[:], int64, float64[:, :], float64[:, :], float64[:], int64[:]), **{**conf.JIT_FLAGS, **{'parallel': False}}) # TODO: waits for https://github.com/numba/numba/issues/5279 def coalescence(n, volume, idx, length, intensive, extensive, gamma, healthy): for i in prange(length - 1): if gamma[i] == 0: continue j = idx[i] k = idx[i + 1] if n[j] < n[k]: j, k = k, j g = min(gamma[i], n[j] // n[k]) if g == 0: continue # note: extensive must be modified after intensive (as it is used as weights) new_n = n[j] - g * n[k] if new_n > 0: n[j] = new_n intensive[:, k] = (intensive[:, k] * volume[k] + intensive[:, j] * g * volume[j]) / (volume[k] + g * volume[j]) extensive[:, k] += g * extensive[:, j] else: # new_n == 0 n[j] = n[k] // 2 n[k] = n[k] - n[j] intensive[:, j] = (intensive[:, k] * volume[k] + intensive[:, j] * g * volume[j]) / (volume[k] + g * volume[j]) intensive[:, k] = intensive[:, j] extensive[:, j] = g * extensive[:, j] + extensive[:, k] extensive[:, k] = extensive[:, j] if n[k] == 0 or n[j] == 0: healthy[0] = 0 @staticmethod @numba.njit(void(float64[:], float64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def sum_pair(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = (data_in[idx[i]] + data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 @staticmethod @numba.njit(void(float64[:], float64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def distance_pair(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = np.abs(data_in[idx[i]] - data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 @staticmethod @numba.njit(void(float64[:], int64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def max_pair(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = max(data_in[idx[i]], data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 # TODO comment @staticmethod @numba.njit(**conf.JIT_FLAGS) def compute_gamma(prob, rand): for i in prange(len(prob)): prob[i] *= -1. prob[i] += rand[i//2] prob[i] = -np.floor(prob[i]) @staticmethod @numba.njit(boolean(int64[:]), **{**conf.JIT_FLAGS, **{'parallel': False}}) def first_element_is_zero(arr): return arr[0] == 0 @staticmethod #@numba.njit(**conf.JIT_FLAGS) # TODO: "np.dot() only supported on float and complex arrays" def cell_id(cell_id, cell_origin, strides): cell_id[:] = np.dot(strides, cell_origin.T) @staticmethod @numba.njit(void(int64[:], int64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def find_pairs(cell_start, is_first_in_pair, cell_id, idx, sd_num): for i in prange(sd_num - 1): is_first_in_pair[i] = ( cell_id[idx[i]] == cell_id[idx[i+1]] and (i - cell_start[cell_id[idx[i]]]) % 2 == 0 ) @staticmethod @numba.njit(**{**conf.JIT_FLAGS, **{'parallel': False}}) def calculate_displacement(dim, scheme, displacement, courant, cell_origin, position_in_cell): length = displacement.shape[0] for droplet in prange(length): # Arakawa-C grid _l = (cell_origin[droplet, 0], cell_origin[droplet, 1]) _r = (cell_origin[droplet, 0] + 1 * (dim == 0), cell_origin[droplet, 1] + 1 * (dim == 1)) omega = position_in_cell[droplet, dim] displacement[droplet, dim] = scheme(omega, courant[_l], courant[_r]) @staticmethod @numba.njit(**conf.JIT_FLAGS) def moments(moment_0, moments, n, attr, cell_id, idx, length, specs_idx, specs_rank, min_x, max_x, x_id): moment_0[:] = 0 moments[:, :] = 0 for i in idx[:length]: if min_x < attr[x_id][i] < max_x: moment_0[cell_id[i]] += n[i] for k in range(specs_idx.shape[0]): moments[k, cell_id[i]] += n[i] * attr[specs_idx[k], i] ** specs_rank[k] moments[:, :] /= moment_0 @staticmethod @numba.njit(**{**conf.JIT_FLAGS, **{'parallel': False}}) def normalize(prob, cell_id, cell_start, norm_factor, dt_div_dv): n_cell = cell_start.shape[0] # TODO: isn't it n_cell_plus_one? for i in range(n_cell - 1): sd_num = cell_start[i + 1] - cell_start[i] if sd_num < 2: norm_factor[i] = 0 else: norm_factor[i] = dt_div_dv * sd_num * (sd_num - 1) / 2 / (sd_num // 2) for d in range(prob.shape[0]): prob[d] *= norm_factor[cell_id[d]] @staticmethod @numba.njit(**{**conf.JIT_FLAGS, **{'parallel': False}}) def apply_f_3_3(function, arg0, arg1, arg2, output0, output1, output2): for i in range(output0.shape[0]): output0[i], output1[i], output2[i] = function(arg0[i], arg1[i], arg2[i]) @staticmethod def apply(function, args, output): if len(args) == 3: if len(output) == 3: SpecialMethods.apply_f_3_3(function, *args, *output) else: raise NotImplementedError() else: raise NotImplementedError() @staticmethod @numba.njit(void(int64[:], int64[:], int64[:], int64, int64[:]), **conf.JIT_FLAGS) def countsort_by_cell_id(new_idx, idx, cell_id, length, cell_start): cell_end = cell_start cell_end[:] = 0 for i in range(length): cell_end[cell_id[idx[i]]] += 1 for i in range(1, len(cell_end)): # TODO: if len(cell_end) != n_cell+1 silently does wrong thing... cell_end[i] += cell_end[i - 1] for i in range(length-1, -1, -1): cell_end[cell_id[idx[i]]] -= 1 new_idx[cell_end[cell_id[idx[i]]]] = idx[i] @staticmethod @numba.njit(void(int64[:], int64[:], int64[:], int64, int64[:], int64[:, :]), parallel=True) def countsort_by_cell_id_parallel(new_idx, idx, cell_id, length, cell_start, cell_start_p): cell_end_thread = cell_start_p thread_num = cell_end_thread.shape[0] for t in prange(thread_num): cell_end_thread[t, :] = 0 for i in range(t * length // thread_num, (t + 1) * length // thread_num if t < thread_num - 1 else length): cell_end_thread[t, cell_id[idx[i]]] += 1 cell_start[:] = np.sum(cell_end_thread, axis=0) for i in range(1, len(cell_start)): # TODO: if len(cell_end) != n_cell+1 silently does wrong thing... cell_start[i] += cell_start[i - 1] tmp = cell_end_thread[0, :] tmp[:] = cell_end_thread[thread_num - 1, :] cell_end_thread[thread_num - 1, :] = cell_start[:] for t in range(thread_num - 2, -1, -1): cell_start[:] = cell_end_thread[t + 1, :] - tmp[:] tmp[:] = cell_end_thread[t, :] cell_end_thread[t, :] = cell_start[:] for t in prange(thread_num): for i in range((t + 1) * length // thread_num - 1 if t < thread_num - 1 else length - 1, t * length // thread_num - 1, -1): cell_end_thread[t, cell_id[idx[i]]] -= 1 new_idx[cell_end_thread[t, cell_id[idx[i]]]] = idx[i] cell_start[:] = cell_end_thread[0, :] @staticmethod def condensation( solver, n_cell, cell_start_arg, v, particle_temperatures, n, vdry, idx, rhod, thd, qv, dv, prhod, pthd, pqv, kappa, rtol_x, rtol_thd, dt, substeps, cell_order ): n_threads = min(numba.config.NUMBA_NUM_THREADS, n_cell) SpecialMethods._condensation( solver, n_threads, n_cell, cell_start_arg, v, particle_temperatures, n, vdry, idx, rhod, thd, qv, dv, prhod, pthd, pqv, kappa, rtol_x, rtol_thd, dt, substeps, cell_order ) @staticmethod @numba.njit(**conf.JIT_FLAGS) def _condensation( solver, n_threads, n_cell, cell_start_arg, v, particle_temperatures, n, vdry, idx, rhod, thd, qv, dv, prhod, pthd, pqv, kappa, rtol_x, rtol_thd, dt, substeps, cell_order ): for thread_id in numba.prange(n_threads): for i in range(thread_id, n_cell, n_threads): # TODO: at least show that it is not slower :) cell_id = cell_order[i] cell_start = cell_start_arg[cell_id] cell_end = cell_start_arg[cell_id + 1] n_sd_in_cell = cell_end - cell_start if n_sd_in_cell == 0: continue dthd_dt = (pthd[cell_id] - thd[cell_id]) / dt dqv_dt = (pqv[cell_id] - qv[cell_id]) / dt md_new = prhod[cell_id] * dv md_old = rhod[cell_id] * dv md_mean = (md_new + md_old) / 2 rhod_mean = (prhod[cell_id] + rhod[cell_id]) / 2 qv_new, thd_new, substeps_hint = solver( v, particle_temperatures, n, vdry, idx[cell_start:cell_end], # TODO kappa, thd[cell_id], qv[cell_id], dthd_dt, dqv_dt, md_mean, rhod_mean, rtol_x, rtol_thd, dt, substeps[cell_id] ) substeps[cell_id] = substeps_hint pqv[cell_id] = qv_new pthd[cell_id] = thd_new
is_left_value = is_left(poly[k], poly[k + 1], point) if is_left_value >= 0: # P left of edge wn += 1 # have a valid up intersect else: # start y > P[1] (no test needed) if poly[k + 1][1] <= point[1]: # a downward crossing is_left_value = is_left(poly[k], poly[k + 1], point) if is_left_value <= 0: # P right of edge wn -= 1 # have a valid down intersect out[i] = wn return out @njit(nb.boolean(nb.double, nb.double, nb.double[:, :])) def point_inside_polygon(x, y, poly): n = len(poly) # determine if a point is inside a given polygon or not # Polygon is a list of (x,y) pairs. p1x = 0.0 p1y = 0.0 p2x = 0.0 p2y = 0.0 xinters = 0.0 plx = 0.0 ply = 0.0 idx = 0 inside = False # p1x, p1y = poly[0]
elif issubclass(type(data), sp_sparse.spmatrix): data = data.data elif isinstance(data, pd.DataFrame): data = data.to_numpy() else: raise TypeError("data type not understood") n = len(data) inds = np.random.permutation(n)[:20] check = data.flat[inds] return ~np.any(_is_not_count(check)) @vectorize( [ boolean(int32), boolean(int64), boolean(float32), boolean(float64), ], target="parallel", cache=True, ) def _is_not_count(d): return d < 0 or d % 1 != 0 def _get_batch_mask_protein_data(adata: anndata.AnnData, protein_expression_obsm_key: str, batch_key: str): """
from numba import double, boolean from numba.decorators import jit @jit(boolean(double[:], double[:], double)) def dist_sq_lt(u, v, b): ''' Does a partial distance search to see if dist(u, v)**2<b, for l2 distances. ''' d = 0 for x in range(u.size): d += (u[x] - v[x])**2 if d>=b: return False return True
from sys import argv from math import sqrt, trunc from numpy import zeros, sum, ndarray import time from numba import jit, float32, boolean, int32, float64, int64 compile_start = time.time() @jit([boolean()]) def check_prime(num): has_divisor = False if (num == 2): return True #Check 2 specially if num % 2 == 0: # Don't need to check any further - return right now return False for i in range(3, trunc(sqrt(num)) + 1, 2): if num % i == 0: has_divisor = True # Don't need to check any further - at least one divisor exists break #Either we found a divisor, or there are none return not has_divisor compile_end = time.time() print("Compilation of check_prime took {} seconds".format(compile_end -
return isfinite(_sum(asarray(arr))) def _is_crescent(arr): i = 0 while i < arr.shape[0] - 1: if arr[i] > arr[i + 1]: return False i += 1 return True if _NUMBA: signature = jit( [ boolean(float64[:]), boolean(int64[:]), boolean(char[:]), boolean(int32[:]) ], nogil=True, nopython=True, cache=True, ) _is_crescent = signature(_is_crescent) def _is_all_equal(arr): arr = arr.ravel() v = arr[0] i = 1
import numba import numpy as np @numba.jit( # error: Any? not callable numba.boolean(numba.int64[:]), # type: ignore[misc] nopython=True, nogil=True, parallel=False, ) def is_monotonic_increasing(bounds: np.ndarray) -> bool: """Check if int64 values are monotonically increasing.""" n = len(bounds) if n < 2: return True prev = bounds[0] for i in range(1, n): cur = bounds[i] if cur < prev: return False prev = cur return True
"""Positions of the center of mass, left- and right shoulders. Args: agents (ndarray): Numpy array of datatype ``dtype=agent_type_three_circle``. """ for agent, m in zip(agents, mask): if not m: continue tangent = rotate270(unit_vector(agent['orientation'])) offset = tangent * agent['r_ts'] agent['position_ls'][:] = agent['position'] - offset agent['position_rs'][:] = agent['position'] + offset @numba.jit([boolean(typeof(agent_type_circular)[:], float64[:], float64)], nopython=True, nogil=True, cache=True) def overlapping_circles(agents, x, r): """Test if two circles are overlapping. Args: agents: x: Position of agent that is tested r: Radius of agent that is tested Returns: bool: """ for agent in agents:
records.capacity *= 2 records.stack = resize(records.stack, records.capacity) stack_top = records.stack[records.top] stack_top["parent"] = parent stack_top["depth"] = depth stack_top["is_left"] = is_left stack_top["impurity"] = impurity stack_top["start_train"] = start_train stack_top["end_train"] = end_train stack_top["start_valid"] = start_valid stack_top["end_valid"] = end_valid records.top += 1 @jit(boolean(RecordsType), nopython=True, nogil=True) def has_records(records): """Tests if the stack of records contain remaining records. Parameters ---------- records : Records A records dataclass containing the stack of node records Returns ------- output : bool Returns True if there are remaining records in the stack, False otherwise """ return records.top <= 0
""" Created at 12.03.2020 """ import numba import numpy as np MAX_DIM_NUM = 2 f_i = 0 f_j = f_i + 1 f_d = f_j + 1 @numba.njit([numba.boolean(numba.float64), numba.boolean(numba.int64)]) def _is_integral(n): return int(n * 2.) % 2 == 0 @numba.njit() def at_1d(focus, arr, i, _): return arr[focus[f_i] + i] @numba.njit() def at_2d_axis0(focus, arr, i, j): return arr[focus[f_i] + i, focus[f_j] + j] @numba.njit() def at_2d_axis1(focus, arr, i, j):
z = sgn(np.dot(i_cells,j_genes.T)) inv_z = 1.0-z #compute the loglikelihood of the expression data p_x = np.sum(np.multiply(z,prob_matrix_a) + np.multiply(inv_z,prob_matrix_b)) z_total = np.sum(z ,0) * (1.0/(i_cells.shape[0])) p_y = sum([trunc_norm_logpdf(z_total[index],y[index],sigma_y) for index in range(y.size)]) return p_x+y_scale*p_y @jit(boolean(float64[:,:],float64[:,:],int64,int64,float64[:,:],float64[:,:],float64[:],float64,float64,float64[:],float64[:,:]),nopython=True) def opt_jk_numba_single(i_cells:np.ndarray,j_genes:np.ndarray,j:int,k:int, prob_matrix_a:np.ndarray, prob_matrix_b:np.ndarray, y: np.ndarray, sigma_y:float, y_scale: float, z_arr:np.ndarray, z_count: np.ndarray): denom = i_cells.shape[0] val = j_genes[j,k] z = sgn(z_count[:, j]) x_stay_log_likelihood = np.sum(np.multiply(z, prob_matrix_a[:, j])) + \ np.sum(np.multiply(1.0 - z, prob_matrix_b[:, j])) if val == 0.0: #case of changing 0 to 1 changed_z_count = i_cells[:, k] + z_count[:, j] num_changed = np.sum(np.logical_and(z_count[:, j] == 0.0, changed_z_count == 1.0))
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # import unittest from numba import vectorize, int64, boolean, short from test_helper import start_jvm start_jvm() from deephaven import empty_table from deephaven.html import to_html @vectorize([boolean(int64, int64)]) def vectorized_func(x, y): return x % 2 > y % 5 @vectorize([short(int64, int64)]) def vectorized_func_wrong_return_type(x, y): return x % 2 > y % 5 class TestClass(unittest.TestCase): def test_wrong_return_type(self): with self.assertRaises(Exception): t = empty_table(10).view(formulas=["I=ii", "J=(ii * 2)"])\ .where("vectorized_func_wrong_return_type(I, J)") def test_filter(self):
bank_col = pdf_wupdates.bank.values table_col = pdf_wupdates.table.values row_col = pdf_wupdates.row.values (banks, tables, rows) = _get_geo_cols(brn_idx, wupdate_col, brn_col, bank_col, table_col, row_col) return _accept(self.filt, brn.pc, brn.tgt, brn.indirect, brn.uncond, banks, tables, rows) # Accept or reject a branch based on the filter # Note that this being a standalone function is noticeably faster than being a member of the BranchFilter class because # of jitclass performance limitations. @numba.jit(numba.boolean(BranchFilter.class_type.instance_type, numba.uint64, numba.uint64, numba.boolean, numba.boolean, numba.int32[:], numba.int32[:], numba.int32[:]), nopython=True, nogil=True) def _accept(self, pc, target, indirect, uncond, banks, tables, rows): if self._no_filtering: # early out return True # Filtering on banks,tables,rows works by finding any one weight update that matches all the filter criteria. # i.e.: at least 1 single index across `banks`, `tables`, and `rows` columns must match all filter criteria. # # Note: assuming banks,rows,tables all the same length because they should be. j = 0 while j < len(tables): if len(self.incl_banks) > 0: i = 0
shortMonthNames = [ 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC' ] longMonthNames = [ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER' ] monthDaysNotLeapYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] monthDaysLeapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ############################################################################### @njit(boolean(int64), fastmath=True, cache=True) def isLeapYear(y: int): ''' Test whether year y is a leap year - if so return True, else False ''' leapYear = ((y % 4 == 0) and (y % 100 != 0) or (y % 400 == 0)) return leapYear ############################################################################### def parse_date(dateStr, dateFormat): dt_obj = datetime.datetime.strptime(dateStr, dateFormat) return dt_obj.day, dt_obj.month, dt_obj.year ###############################################################################
class SpecialMethods: @staticmethod @numba.njit(int64(int64[:], int64[:], int64), **{**conf.JIT_FLAGS, **{'parallel': False}}) def remove_zeros(data, idx, length) -> int: new_length = 0 for i in range(length): if data[idx[i]] == 0: idx[i] = len(idx) else: new_length += 1 idx[:length].sort() return new_length @staticmethod @numba.njit(void(int64[:], int64[:], int64, float64[:, :], float64[:, :], float64[:], int64[:]), **conf.JIT_FLAGS) def coalescence(n, idx, length, intensive, extensive, gamma, healthy): for i in prange(length - 1): if gamma[i] == 0: continue j = idx[i] k = idx[i + 1] if n[j] < n[k]: j, k = k, j g = min(gamma[i], n[j] // n[k]) if g == 0: continue new_n = n[j] - g * n[k] if new_n > 0: n[j] = new_n extensive[:, k] += g * extensive[:, j] else: # new_n == 0 n[j] = n[k] // 2 n[k] = n[k] - n[j] extensive[:, j] = g * extensive[:, j] + extensive[:, k] extensive[:, k] = extensive[:, j] if n[k] == 0 or n[j] == 0: healthy[0] = 0 @staticmethod @numba.njit(void(float64[:], float64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def sum_pair(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = (data_in[idx[i]] + data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 @staticmethod @numba.njit(void(float64[:], float64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def distance_pair(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = np.abs(data_in[idx[i]] - data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 @staticmethod @numba.njit(void(float64[:], int64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def max_pair(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = max(data_in[idx[i]], data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 # TODO comment @staticmethod @numba.njit(**conf.JIT_FLAGS) def compute_gamma(prob, rand): for i in prange(len(prob)): prob[i] *= -1. prob[i] += rand[i//2] prob[i] = -np.floor(prob[i]) @staticmethod @numba.njit(boolean(int64[:]), **{**conf.JIT_FLAGS, **{'parallel': False}}) def first_element_is_zero(arr): return arr[0] == 0 @staticmethod #@numba.njit(**conf.JIT_FLAGS) # TODO: "np.dot() only supported on float and complex arrays" def cell_id(cell_id, cell_origin, strides): cell_id[:] = np.dot(strides, cell_origin.T) @staticmethod @numba.njit(void(int64[:], int64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def find_pairs(cell_start, is_first_in_pair, cell_id, idx, sd_num): for i in prange(sd_num - 1): is_first_in_pair[i] = ( cell_id[idx[i]] == cell_id[idx[i+1]] and (i - cell_start[cell_id[idx[i]]]) % 2 == 0 ) @staticmethod @numba.njit(**{**conf.JIT_FLAGS, **{'parallel': False}}) def calculate_displacement(dim, scheme, displacement, courant, cell_origin, position_in_cell): length = displacement.shape[0] for droplet in prange(length): # Arakawa-C grid _l = (cell_origin[droplet, 0], cell_origin[droplet, 1]) _r = (cell_origin[droplet, 0] + 1 * (dim == 0), cell_origin[droplet, 1] + 1 * (dim == 1)) omega = position_in_cell[droplet, dim] displacement[droplet, dim] = scheme(omega, courant[_l], courant[_r]) @staticmethod @numba.njit(**conf.JIT_FLAGS) def moments(moment_0, moments, n, attr, cell_id, idx, length, specs_idx, specs_rank, min_x, max_x, x_id): moment_0[:] = 0 moments[:, :] = 0 for i in idx[:length]: if min_x < attr[x_id][i] < max_x: moment_0[cell_id[i]] += n[i] for k in range(specs_idx.shape[0]): moments[k, cell_id[i]] += n[i] * attr[specs_idx[k], i] ** specs_rank[k] moments[:, :] /= moment_0 # TODO: should we divide or not... @staticmethod @numba.njit(**{**conf.JIT_FLAGS, **{'parallel': False}}) def normalize(prob, cell_id, cell_start, norm_factor, dt_div_dv): n_cell = cell_start.shape[0] # TODO: isn't it n_cell_plus_one? for i in range(n_cell - 1): sd_num = cell_start[i + 1] - cell_start[i] if sd_num < 2: norm_factor[i] = 0 else: norm_factor[i] = dt_div_dv * sd_num * (sd_num - 1) / 2 / (sd_num // 2) for d in range(prob.shape[0]): prob[d] *= norm_factor[cell_id[d]] @staticmethod @numba.njit(**{**conf.JIT_FLAGS, **{'parallel': False}}) def apply_f_3_3(function, arg0, arg1, arg2, output0, output1, output2): for i in range(output0.shape[0]): output0[i], output1[i], output2[i] = function(arg0[i], arg1[i], arg2[i]) @staticmethod def apply(function, args, output): if len(args) == 3: if len(output) == 3: SpecialMethods.apply_f_3_3(function, *args, *output) else: raise NotImplementedError() else: raise NotImplementedError() @staticmethod @numba.njit(void(int64[:], int64[:], int64[:], int64, int64[:]), **conf.JIT_FLAGS) def countsort_by_cell_id(new_idx, idx, cell_id, length, cell_start): cell_end = cell_start cell_end[:] = 0 for i in range(length): cell_end[cell_id[idx[i]]] += 1 for i in range(1, len(cell_end)): # TODO: if len(cell_end) != n_cell+1 silently does wrong thing... cell_end[i] += cell_end[i - 1] for i in range(length-1, -1, -1): cell_end[cell_id[idx[i]]] -= 1 new_idx[cell_end[cell_id[idx[i]]]] = idx[i] @staticmethod @numba.njit(void(int64[:], int64[:], int64[:], int64, int64[:], int64[:, :]), parallel=True) def countsort_by_cell_id_parallel(new_idx, idx, cell_id, length, cell_start, cell_start_p): cell_end_thread = cell_start_p thread_num = cell_end_thread.shape[0] for t in prange(thread_num): cell_end_thread[t, :] = 0 for i in range(t * length // thread_num, (t + 1) * length // thread_num if t < thread_num - 1 else length): cell_end_thread[t, cell_id[idx[i]]] += 1 cell_start[:] = np.sum(cell_end_thread, axis=0) for i in range(1, len(cell_start)): # TODO: if len(cell_end) != n_cell+1 silently does wrong thing... cell_start[i] += cell_start[i - 1] tmp = cell_end_thread[0, :] tmp[:] = cell_end_thread[thread_num - 1, :] cell_end_thread[thread_num - 1, :] = cell_start[:] for t in range(thread_num - 2, -1, -1): cell_start[:] = cell_end_thread[t + 1, :] - tmp[:] tmp[:] = cell_end_thread[t, :] cell_end_thread[t, :] = cell_start[:] for t in prange(thread_num): for i in range((t + 1) * length // thread_num - 1 if t < thread_num - 1 else length - 1, t * length // thread_num - 1, -1): cell_end_thread[t, cell_id[idx[i]]] -= 1 new_idx[cell_end_thread[t, cell_id[idx[i]]]] = idx[i] cell_start[:] = cell_end_thread[0, :]
2285^2 + 20^3 2223^2 + 66^3 1810^2 + 125^3 1197^2 + 156^3 Find the sum of the five smallest such palindromic numbers. ''' import numpy as np from itertools import product from collections import defaultdict, Counter from time import time from numba import jit, vectorize, boolean, uint64 @vectorize([boolean(uint64)]) def is_palindrome(n): rev = 0 a = n while a > 0: r = a % 10 rev = (rev * 10) + r a = (a - r) // 10 return n == rev @jit def solve(): A = 3 * (10**4) B = 10**3 numbers_a = np.arange(A, dtype="uint64")
import time import numba as nb import math import primes @nb.jit(nb.boolean(nb.int32), nopython=True, locals={'i': nb.int32, 'upper': nb.int32}) def isPrime(n): i = 2 upper = int(math.sqrt(n)) + 1 while i < upper: if n % i == 0: return False i += 1 return True n0 = 30000 n1 = 500000 t0 = time.perf_counter() # Numba function: a = [p for p in range(1, n1) if isPrime(p)] t1 = time.perf_counter() # Cython function: b = [p for p in range(1, n1) if primes.isPrime(p)]