def not_null(typingctx, src): sig = types.boolean(src) def codegen(context, builder, signature, args): instance, = args # TODO: probably a more general way to do this second_element = builder.extract_value(instance, [1]) result = cgutils.is_not_null(builder, second_element) return result if isinstance(src, types.ClassInstanceType): return sig, codegen raise TypeError()
def gen_hash_compare_ops(key_type): deref_voidptr = gen_deref_voidptr(key_type) c_sig_hash = types.uintp(types.voidptr) c_sig_eq = types.boolean(types.voidptr, types.voidptr) @cfunc(c_sig_hash) def hash_func_adaptor(voidptr_to_data): obj = deref_voidptr(voidptr_to_data) return hash(obj) @cfunc(c_sig_eq) def eq_func_adaptor(lhs_ptr, rhs_ptr): lhs_str = deref_voidptr(lhs_ptr) rhs_str = deref_voidptr(rhs_ptr) return lhs_str == rhs_str hasher_ptr = hash_func_adaptor.address eq_ptr = eq_func_adaptor.address return hasher_ptr, eq_ptr
def check_predicate_func(self, pyfunc): self.run_unary( pyfunc, [types.boolean(tp) for tp in (types.complex128, types.complex64)], self.basic_values())
''' This file is meant for generating ahead of time compiled functions that accelerate code performance by reducing constants for simple functions that are invoked for roughly every letter in a document For instance, some characters such as inverted commas are handled differently by docx files. ''' from numba.pycc import CC from numba import types cc = CC('check_letter') cc.verbose = True @cc.export('check_inv', types.boolean(types.string)) def check_inv(letter): if letter == '‘' or letter == '’': return True else: return False @cc.export('check_dinv', types.boolean(types.string)) def check_dinv(letter): if letter == '“' or letter == '”': return True else: return False @cc.export('check_hyphen', types.boolean(types.string)) def check_hyphen(letter): if letter == '-' or letter == '–':
@njit(void(TreeClassifier.class_type.instance_type, uint32, uint8)) def node_update_depth(tree, idx_node, depth): depth += 1 tree.nodes.depth[idx_node] = depth if tree.nodes.is_leaf[idx_node]: return else: left = tree.nodes.left[idx_node] right = tree.nodes.right[idx_node] node_update_depth(tree, left, depth) node_update_depth(tree, right, depth) @njit(boolean(TreeClassifier.class_type.instance_type, uint32, float32)) def node_is_dirac(tree, idx_node, y_t): c = uint8(y_t) n_samples = tree.nodes.n_samples[idx_node] count = tree.nodes.counts[idx_node, c] return n_samples == count @njit(uint32(TreeClassifier.class_type.instance_type, uint32, float32[::1])) def node_get_child(tree, idx_node, x_t): feature = tree.nodes.feature[idx_node] threshold = tree.nodes.threshold[idx_node] if x_t[feature] <= threshold: return tree.nodes.left[idx_node] else: return tree.nodes.right[idx_node]
def check_predicate_func(self, pyfunc): self.run_unary(pyfunc, [types.boolean(tp) for tp in (types.complex128, types.complex64)], self.basic_values())
elif face_idx == 5: temp = state[0, :, 0].copy() if rotate_direction == 0: state[0, :, 0] = state[1, 0, ::-1] state[1, 0, :] = state[4, :, 2] state[4, :, 2] = state[3, 2, ::-1] state[3, 2, :] = temp else: state[0, :, 0] = state[3, 2, :] state[3, 2, :] = state[4, ::-1, 2] state[4, :, 2] = state[1, 0, :] state[1, 0, :] = temp[::-1] @numba.njit(nt.boolean(numba.uint8[:, :, ::1]), cache=True) def cube_is_solved(state: np.ndarray) -> bool: """ Check if cube is in the solved state """ return np.all(state == state[:, 0:1, 0:1]) def rubiks_cube_play_original(state: np.ndarray, action_idx: int): """ Most important function in this module. How does rotating each face affect the cube?""" face_idx = action_idx // 2 # Rotate direction 0 - clockwise # Rotate direction 1 - counterclockwise rotate_direction = action_idx % 2 # Parameter for np.rot90 numpy_rot_k = 1 if rotate_direction == 1 else 3
import numba from numba.types import f8, boolean from numba.typing.typeof import typeof from crowddynamics.core.geom2D import line_intersect from crowddynamics.core.structures import obstacle_type_linear @numba.jit([boolean(f8[:], f8[:], typeof(obstacle_type_linear)[:])], nopython=True, nogil=True, cache=True) def is_obstacle_between_points(p0, p1, obstacles): """Tests if there is obstacles between the two points p0 and p1.""" for obstacle in obstacles: if line_intersect(p0, p1, obstacle['p0'], obstacle['p1']): return True return False
primary_dtype = sig.args[0] suffix = GrB_Type.lookup_name(primary_dtype) typed_name = f'{funcname}_{suffix}' jitted_func = njit(sig)(func) setattr(self, typed_name, jitted_func) return ncompiler GrB_UnaryOp = OpContainer() GrB_BinaryOp = OpContainer() ################################## # Useful collections of signatures ################################## _unary_bool = [nt.boolean(nt.boolean)] _unary_int = [ nt.uint8(nt.uint8), nt.int8(nt.int8), nt.uint16(nt.uint16), nt.int16(nt.int16), nt.uint32(nt.uint32), nt.int32(nt.int32), nt.uint64(nt.uint64), nt.int64(nt.int64) ] _unary_float = [nt.float32(nt.float32), nt.float64(nt.float64)] _unary_all = _unary_bool + _unary_int + _unary_float _binary_bool = [nt.boolean(nt.boolean, nt.boolean)] _binary_int = [