def _make_check_roi_2d_poly(xvert, yvert): xvert = xvert.astype(np.float) yvert = yvert.astype(np.float) _xmin = xvert.min() _xmax = xvert.max() _ymin = yvert.min() _ymax = yvert.max() @nb.vectorize([nb.b1(nb.f4, nb.f4)], target="parallel") def _check_roi_2d_poly(xpoint, ypoint): bbox = _check_roi_2d_rect(xpoint, _xmin, _xmax, ypoint, _ymin, _ymax) inside = False for i, j in zip(range(xvert.size), range(-1, xvert.size-1)): if bbox: if (yvert[i] > ypoint) != (yvert[j] > ypoint): if xpoint < (xvert[j]-xvert[i])*(ypoint-yvert[i])/(yvert[j]-yvert[i]) + xvert[i]: inside = not inside return inside return _check_roi_2d_poly
# from global_settings import COORD2INT_FACTOR, INT2COORD_FACTOR, MAX_HAVERSINE_DISTANCE dtype_3float_tuple = typeof((1.0, 1.0, 1.0)) dtype_2float_tuple = typeof((1.0, 1.0)) dtype_2int_tuple = typeof((1, 1)) # TODO Ahead-Of-Time Compilation: # cc = CC('precompiled_helpers', ) # # Uncomment the following line to print out the compilation steps # cc.verbose = True # TODO 'skip' decorator when numba is not installed and combine both helper files # njit is equal to @jit(nopython=True) # @cc.export('inside_polygon', 'b1(i4, i4, i4[:, :])') @njit(b1(i4, i4, i4[:, :]), cache=True) def inside_polygon(x, y, coordinates): """ Implementing the ray casting point in polygon test algorithm cf. https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm :param x: :param y: :param coordinates: a polygon represented by a list containing two lists (x and y coordinates): [ [x1,x2,x3...], [y1,y2,y3...]] those lists are actually numpy arrays which are bei ng read directly from a binary file :return: true if the point (x,y) lies within the polygon Some overflow considerations for the critical part of comparing the line segment slopes: (y2 - y) * (x2 - x1) <= delta_y_max * delta_x_max
exits, volume, accumulate): """Calculate portfolio value using signals.""" return portfolio_np(ts, investment, slippage, commission, signals_order_func_np, entries, exits, volume, accumulate) @njit(UniTuple(f8[:, :], 2)(f8[:, :], f8, f8, f8, f8[:, :], b1), cache=True) def portfolio_from_orders_np(ts, investment, slippage, commission, orders, is_target): """Calculate portfolio value using orders.""" return portfolio_np(ts, investment, slippage, commission, orders_order_func_np, orders, is_target) @njit(b1(f8[:]), cache=True) def detect_order_accumulation_1d_nb(trades): """Detect accumulation of orders, that is, position is being increased/decreased gradually. When it happens, it's not easy to calculate P/L of a position anymore.""" entry_i = -1 position = False for i in range(trades.shape[0]): if trades[i] > 0: if position: return True entry_i = i position = True elif trades[i] < 0: if not position: return True
if overlap: overlaps[angleidx1, angleidx2, anchoridx1, anchoridx2] = True overlaps[angleidx2, angleidx1, anchoridx2, anchoridx1] = True return overlaps @nb.njit(nb.void(nb.b1[:, :, :], nb.b1[:, :, :])) def prepRough(grid, roughX): xc, yc, zc = grid.shape roughX[:] = False for x, y, z in np.ndindex(xc, yc, zc): roughX[x // 3, y // 3, z // 3] |= grid[x, y, z] @nb.njit( nb.b1(nb.b1[:, :, :], nb.b1[:, :, :], nb.i8, nb.i8, nb.i8, nb.i8, nb.i8, nb.i8)) def splitGrid(grid, roughgrid, x1, y1, z1, x2, y2, z2): #x1,x2,y1,y2,z1,z2 = split largex1 = x1 // 3 smallx1 = largex1 if x1 == largex1 * 3 else largex1 + 1 smallx2 = x2 // 3 largex2 = smallx2 if x2 == smallx2 * 3 else smallx2 + 1 largey1 = y1 // 3 smally1 = largey1 if y1 == largey1 * 3 else largey1 + 1 smally2 = y2 // 3 largey2 = smally2 if y2 == smally2 * 3 else smally2 + 1 largez1 = z1 // 3 smallz1 = largez1 if z1 == largez1 * 3 else largez1 + 1 smallz2 = z2 // 3 largez2 = smallz2 if z2 == smallz2 * 3 else smallz2 + 1 if np.any(roughgrid[smallx1:smallx2, smally1:smally2, smallz1:smallz2]):
from numba import b1, f8, i2, i4, jit, typeof, u2, u8 # # for Ahead-Of-Time Compilation: # from numba.pycc import CC # cc = CC('compiled_helpers', ) # # Uncomment the following line to print out the compilation steps # # cc.verbose = True TIMEZONE_NAMES_FILE = 'timezone_names.json' dtype_3floattuple = typeof((1.0, 1.0, 1.0)) dtype_2floattuple = typeof((1.0, 1.0)) # @cc.export('inside_polygon', 'b1(i4, i4, i4[:, :])') @jit(b1(i4, i4, i4[:, :]), nopython=True, cache=True) def inside_polygon(x, y, coordinates): """ Implementing the ray casting point in polygon test algorithm cf. https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm :param x: :param y: :param coordinates: a polygon represented by a list containing two lists (x and y coordinates): [ [x1,x2,x3...], [y1,y2,y3...]] those lists are actually numpy arrays which are being read directly from a binary file :return: true if the point (x,y) lies within the polygon Some overflow considerations for the critical part of comparing the line segment slopes: (y2 - y) * (x2 - x1) <= delta_y_max * delta_x_max (y2 - y1) * (x2 - x) <= delta_y_max * delta_x_max
z = int(mathfloor(grndpt[2] / anchorstep[2])) - anchorstart[2] if z < 0 or z >= anchorlen[2]: continue for angle in xrange(anchornangles): xf = anchorcossins[angle, 0] * grndpt[0] + anchorcossins[ angle, 1] * grndpt[1] x = int(mathfloor(xf / anchorstep[0])) + localgridlen[0] // 2 yf = anchorcossins[angle, 0] * grndpt[1] - anchorcossins[ angle, 1] * grndpt[0] y = int(mathfloor(yf / anchorstep[1])) + localgridlen[1] // 2 if x >= 0 and x < localgridlen[ 0] and y >= 0 and y < localgridlen[1]: grid[angle, x, y, z] = True @nb.njit( nb.b1(nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8)) def rectOverlap(x1, y1, c1, s1, l1, w1, x2, y2, c2, s2, l2, w2, overlap_buffer): x2in1 = (x2 - x1) * c1 + (y2 - y1) * s1 y2in1 = (y2 - y1) * c1 - (x2 - x1) * s1 x1in2 = (x1 - x2) * c2 + (y1 - y2) * s2 y1in2 = (y1 - y2) * c2 - (x1 - x2) * s2 cos = abs(c1 * c2 + s1 * s2) sin = abs(c1 * s2 - c2 * s1) return not (l1 + l2 * cos + w2 * sin - abs(x2in1) < overlap_buffer or w1 + l2 * sin + w2 * cos - abs(y2in1) < overlap_buffer or l2 + l1 * cos + w1 * sin - abs(x1in2) < overlap_buffer or w2 + l1 * sin + w1 * cos - abs(y1in2) < overlap_buffer) # return not (x2in1 + l2*cos + w2*sin + l1 < overlap_buffer or
find differences in position and angle of rectangles use simple version of separating line theorem for convex polygons: if all corners in (rotated) x axis are past other rectangle's length or all corners in y axis are past other rectangle's width the symmetry of rectangles allows for some speedups on top of this """ from math import cos, sin, hypot import numpy as np import numba Vlen = 2.5 Vwid = 1. maxdistance = hypot(Vlen, Vwid) * 2 @numba.jit(numba.b1(numba.f8[:], numba.f8[:]), nopython=True) def collisionCheck(veh1, veh2): off_x = veh2[0] - veh1[0] off_y = veh2[1] - veh1[1] c1 = cos(veh1[2]) s1 = sin(veh1[2]) c2 = cos(veh2[2]) s2 = sin(veh2[2]) # because given point is front and center off_x += c1 * Vlen - c2 * Vlen off_y += s1 * Vlen - s2 * Vlen if hypot(off_x, off_y) > maxdistance: return False off_c = abs(c1 * c2 + s1 * s2) off_s = abs(c1 * s2 - s1 * c2)
dtype_3float_tuple = typeof((1.0, 1.0, 1.0)) dtype_2float_tuple = typeof((1.0, 1.0)) dtype_2int_tuple = typeof((1, 1)) # TODO Ahead-Of-Time Compilation: # cc = CC('precompiled_helpers', ) # # Uncomment the following line to print out the compilation steps # cc.verbose = True # TODO 'skip' decorator when numba is not installed and combine both helper files # njit is equal to @jit(nopython=True) # @cc.export('inside_polygon', 'b1(i4, i4, i4[:, :])') @njit(b1(i4, i4, i4[:, :]), cache=True) def inside_polygon(x, y, coordinates): """ Implementing the ray casting point in polygon test algorithm cf. https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm :param x: :param y: :param coordinates: a polygon represented by a list containing two lists (x and y coordinates): [ [x1,x2,x3...], [y1,y2,y3...]] those lists are actually numpy arrays which are bei ng read directly from a binary file :return: true if the point (x,y) lies within the polygon Some overflow considerations for the critical part of comparing the line segment slopes: (y2 - y) * (x2 - x1) <= delta_y_max * delta_x_max
#z = int(mathfloor(pt[2]*8)) - 1 # taken care of in tilePoints #if z >= 0 and z < 20: x = int(mathfloor(cos8 * pt[0] + sin8 * pt[1] + offx)) y = int(mathfloor(cos8 * pt[1] - sin8 * pt[0] + offy)) if x >= 0 and x < 72 and y >= 0 and y < 72: z = int(pt[2]) grid[x, y, z] = 1 lib = CDLL('./cumsum3d.so', RTLD_GLOBAL) makeIntegralGrid = lib.cumsum3d makeIntegralGrid.argtypes = [c_void_p, c_void_p] @nb.njit( nb.b1(nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8)) def rectOverlap(x1, y1, c1, s1, l1, w1, x2, y2, c2, s2, l2, w2, overlap_buffer): x2in1 = (x2 - x1) * c1 + (y2 - y1) * s1 y2in1 = (y2 - y1) * c1 - (x2 - x1) * s1 x1in2 = (x1 - x2) * c2 + (y1 - y2) * s2 y1in2 = (y1 - y2) * c2 - (x1 - x2) * s2 cos = abs(c1 * c2 + s1 * s2) sin = abs(c1 * s2 - c2 * s1) return not (l1 + l2 * cos + w2 * sin - abs(x2in1) < overlap_buffer or w1 + l2 * sin + w2 * cos - abs(y2in1) < overlap_buffer or l2 + l1 * cos + w1 * sin - abs(x1in2) < overlap_buffer or w2 + l1 * sin + w1 * cos - abs(y1in2) < overlap_buffer) @nb.njit(nb.b1[:, :, :, :, :, ::1]())
# if abs(obj1[1]-obj2[1])>.1: return False # if abs(obj1[2]-obj2[2])>.1: return False # if abs(obj1[3]-obj2[3])>.2: return False # if abs(obj1[4]-obj2[4])>.1: return False # if abs(obj1[5]-obj2[5])>.01: return False # if abs(obj1[6]-obj2[6])>.3: return False # if abs(obj1[7]-obj2[7])>.5: return False # if abs(obj1[8]-obj2[8])>.5: return False # if abs(obj1[15]-obj2[15])>.5: return False # if abs(obj1[23]-obj2[23])>.1: return False # if abs(obj1[47]-obj2[47])>.01: return False # if abs(obj1[55]-obj2[55])>1.: return False # return True @nb.njit(nb.b1(nb.f8[:], nb.f8[:])) def objectsSame(obj1, obj2): if abs(obj1[0] - obj2[0]) > .25: return False if abs(obj1[1] - obj2[1]) > .25: return False if abs(obj1[2] - obj2[2]) > .2: return False if abs(obj1[3] - obj2[3]) > .5: return False if abs(obj1[4] - obj2[4]) > .3: return False if abs(obj1[5] - obj2[5]) > .02: return False if abs(obj1[6] - obj2[6]) > .6: return False if abs(obj1[8] - obj2[8]) > 1: return False if abs(obj1[9] - obj2[9]) > 1: return False if abs(obj1[17] - obj2[17]) > 1: return False if abs(obj1[26] - obj2[26]) > .2: return False if abs(obj1[53] - obj2[53]) > .01: return False if abs(obj1[62] - obj2[62]) > 2.: return False return True
import numpy as np from numba import njit, b1, i1, int64, float64 @njit(b1(i1[:, :], i1, i1)) def was_winning_move(board, row, col): if col == -1: return False player = board[row, col] player_pieces = board == player win_len = 4 row_win = player_pieces[row, :] for i in range(row_win.size - win_len + 1): if row_win[i:i + win_len].all(): return True diag_win1 = np.diag(player_pieces, col - row) for i in range(diag_win1.size - win_len + 1): if diag_win1[i:i + win_len].all(): return True new_col = 6 - col diag_win2 = np.diag(player_pieces[:, ::-1], new_col - row) for i in range(diag_win2.size - win_len + 1): if diag_win2[i:i + win_len].all(): return True if row < 3: col_win = player_pieces[row:, col] for i in range(col_win.size - win_len + 1): if col_win[i:i + win_len].all():
# -*- coding: utf-8 -*- """ Created on Sat Oct 27 13:59:08 2018 @author: m2 """ from bisect import bisect from math import atan2, hypot, cos, sin, pi import numba as nb inf = 1e10 visual_resolution = .005 # radians, lines or gaps smaller than this are ignored twopi = pi * 2 @nb.jit(nb.b1(nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8), nopython=True) def ccw(Ax, Ay, Bx, By, Cx, Cy): """Tests whether the turn formed by A, B, and C is ccw""" return (Bx - Ax) * (Cy - Ay) > (By - Ay) * (Cx - Ax) @nb.jit(nb.b1(nb.f8, nb.f8, nb.f8, nb.f8), nopython=True) def ccw0(Bx, By, Cx, Cy): """Whether the turn formed by (0,0), B, and C is ccw""" return Bx * Cy > By * Cx @nb.jit(nb.b1(nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8, nb.f8), nopython=True) def doLinesIntersect(A1x, A1y, A2x, A2y, B1x, B1y, B2x, B2y): return ccw(A1x,A1y, A2x,A2y, B1x,B1y) != ccw(A1x,A1y, A2x,A2y, B2x,B2y) and\
for poly_nr in range(nr_polynomials): coeffs_single = coefficients[:, poly_nr] results_placeholder[point_nr, poly_nr] = single_eval( coeffs_single, monomial_vals_placeholder) @njit(void(F_2D, F_2D, I_2D), cache=True) def compute_vandermonde_n2c(V_n2c, nodes, exponents): num_monomials, spatial_dimension = exponents.shape for i in range(num_monomials): for j in range(1, num_monomials): for d in range(spatial_dimension): V_n2c[i, j] *= nodes[i, d]**exponents[j, d] @njit(b1(I_1D, I_1D), cache=True) def lex_smaller_or_equal(index1: np.ndarray, index2: np.ndarray) -> bool: """ tells weather multi-index 1 is lexicographically smaller than or equal to index 2 """ spatial_dimension = len(index1) for m in range(spatial_dimension - 1, -1, -1): # from last to first dimension if index1[m] > index2[m]: return False if index1[m] < index2[m]: return True return True # all equal @njit(B_TYPE(I_2D), cache=True) def have_lexicographical_ordering(indices: np.ndarray) -> bool: