def angle_between_ccw(road1: Segment, road2: Segment) -> float: """ Gets the angle between road1 and road2 in a counterclockwise direction """ if road1.start == road2.start: vect1 = vectors.sub(road1.end, road1.start) vect2 = vectors.sub(road2.end, road1.start) elif road1.start == road2.end: vect1 = vectors.sub(road1.end, road1.start) vect2 = vectors.sub(road2.start, road1.start) elif road1.end == road2.start: vect1 = vectors.sub(road1.start, road1.end) vect2 = vectors.sub(road2.end, road1.end) elif road1.end == road2.end: vect1 = vectors.sub(road1.start, road1.end) vect2 = vectors.sub(road2.start, road1.end) dot = vectors.dot(vect1, vect2) det = vectors.determinant(vect1, vect2) deg = math.degrees(math.atan2(det, dot)) if -180 <= deg <= 0: deg += 360 return deg
def pnt2line(pnt, start, end): line_vec = vec.vector(start, end) pnt_vec = vec.vector(start, pnt) line_len = vec.length(line_vec) line_unitvec = vec.unit(line_vec) pnt_vec_scaled = vec.scale(pnt_vec, 1.0 / line_len) t = vec.dot(line_unitvec, pnt_vec_scaled) if t < 0.0: t = 0.0 elif t > 1.0: t = 1.0 nearest = vec.scale(line_vec, t) dist = vec.distance(nearest, pnt_vec) nearest = vec.add(nearest, start) return (dist, nearest)
def calculate_score(input, wt, wh, wb): for query, query_data in input.items(): for url, doc in query_data.items(): qv = query.split() tft = doc['tf_title_vector'] tfh = doc['tf_header_vector'] tfb = doc['tf_body_vector'] tft = mul(tft, wt) tfh = mul(tfh, wh) tfb = mul(tfb, wb) t = add(tft, tfh) t = add(t, tfb) # ( ... ) t = dot(t, doc['idf']) score = t doc['score'] = score
def covariance(x, y): n = len(x) return dot(de_mean(x), de_mean(y)) / (n - 1)
def tcpa(s, v): if v.dot(s, v) < 0: return -(v.dot(s, v)) / v.sqv(v) else: return 0
def taumod(s, v, DMOD): if v.dot(s, v) < 0: return (DMOD**2 - v.sqv(s)) / v.dot(s, v) else: return -1
def transform_vector(v, components): return [dot(v, x) for w in components]
def multiply_matrix_vector(matrix, vector): return tuple(dot(row, vector) for row in matrix)
def tcpa(s,v): if v.dot(s,v) < 0: return -(v.dot(s,v))/v.sqv(v) else: return 0
def taumod(s,v,DMOD): if v.dot(s,v) < 0: return (DMOD**2 - v.sqv(s))/v.dot(s,v) else: return -1
def covariance(x, y): """covariance measures how two variables vary in tandem from their means""" n = len(x) return dot(de_mean(x), de_mean(y)) / (n -1)
def project(v, w): '''return the projection of v onto the directon w''' projection_length = dot(v, w) return scalar_mulltiply(projection_length, w)
def directional_variance_gradient_i(x_i, w): '''the contribution of row x_i to the grdient of the direction-w variance''' projection_length = dot(x_i, direction(w)) return [2 * projection_length * x_ij for x_ij in x_i]
def directional_variance_i(x_i, w): '''the variance of the data in the direction determined by w''' return dot(x_i, direction(w)) ** 2
def _collide(e1, e2): # orthogonally project the polygons onto the normal of each face. if all intersect, # the polygons intersect v = [e1.vertices(), e2.vertices()] col = True # currently colliding colfuture = False # will collide within one step minstep = 999999 # for a collision this frame, 0 <= step <= 1 # cut some corners if both objects are rectangular and axis-aligned aa = False if len(v[0]) == 4 and len( v[1] ) == 4 and False: # two boxes. remove the False when code is fixed a = sorted(v[0]) b = sorted(v[1]) for i in [a, b]: if i[0][0] != i[1][0] or a[2][0] != a[3][ 0]: # should be two unique x values (lists sorted by x val) break elif i[0][1] != i[2][1] or i[1][1] != i[3][ 1]: # should be two unique y values break else: # axis aligned xs1 = ys1 = xs2 = ys2 = [] for i in a: xs1 += i[0] ys1 += i[1] for i in b: xs2 += i[0] ys2 += i[1] coords = [(xs1, ys1), (xs2, ys2)] overx = False if min(xs1) > max(xs2): maxx, minx = xs1, xs2 maxxe, minxe = e1, e2 elif min(xs2) > max(xs1): maxx, minx = xs2, xs1 maxxe, minxe = e2, e1 else: # they're overlapping on x. what if the shape moves right past? overx = True overy = False if min(ys1) > max(ys2): maxy, miny = ys1, ys2 maxye, minye = e1, e2 elif min(ys2) > max(ys1): maxy, miny = ys2, ys1 maxye, minye = e2, e1 else: # they're overlapping on y overy = True if overx and overy: col = True colfuture = True minstep = 0 aa = True else: #needs work tx = (minxe.pos[0] - maxxe.pos[0]) / (maxxe.vel[0] - minxe.vel[0]) ty = (minye.pos[1] - maxye.pos[1]) / (maxye.vel[1] - maxye.vel[1]) # generic stuff j = 0 while j < 2 and not aa: i = 0 while i < len(v[j]): axis = vectors.normalize( (-(v[j][i][1] - v[j][i - 1][1]), v[j][i][0] - v[j][i - 1][0])) # axis of projection / normal # first poly min1 = max1 = vectors.dot( axis, v[0][0] ) # the magnitude of a projection onto a unit vector is just their dot product #min1p = max1p = v[0][0] n = 0 while n < len(v[0]): proj = vectors.dot(axis, v[0][n]) if proj > max1: max1 = proj #max1p = v[0][n] elif proj < min1: min1 = proj #min1p = v[0][n] n += 1 #off = vectors.dot(axis, e1.pos) #min1 += off #max1 += off # second poly min2 = max2 = vectors.dot(axis, v[1][0]) #min2p = max2p = v[1][0] n = 0 while n < len(v[1]): proj = vectors.dot(axis, v[1][n]) if proj > max2: max2 = proj #max2p = v[1][n] elif proj < min2: min2 = proj #min2p = v[1][n] n += 1 #off = vectors.dot(axis, e2.pos) #min2 += off #max2 += off #print "j:", j, "i:", i #print "axis:", axis, "normal of:", v[j][i], "and", v[j][i - 1] #print "\tmin1:", min1, "max2:", max2, "min2:", min2, "max1:", max1 #print "\tmin1:", min1p, "max2:", max2p, "min2:", min2p, "max1:", max1p if min1 > max2 or min2 > max1: # potential for collision!! col = False # they are not overlapping, so both values of one projection (a) are larger than both values for the other (b) if (min1 > max2): # min1 will touch max2 on collision a, b = min1, max2 ae, be = e1, e2 elif (min2 > max1): # min2 will touch max1 on collision a, b = min2, max1 ae, be = e2, e1 # multiply the axis' unit vector by the vector magnitudes to find their coordinates maxp, minp = (axis[0] * a, axis[1] * a), (axis[0] * b, axis[1] * b) #print "collision anticipated at step =" # prevent division by zero error. they are not moving or are moving at the same speed #if be.vel[1] - ae.vel[1]: # step = (axis[0] - maxp[1] + minp[1]) / (be.vel[1] - ae.vel[1]) # print "\t", step # if 0 <= step < minstep: # minstep = step #if ae.vel[0] - be.vel[0]: # step = (axis[1] - minp[0] + maxp[0]) / (ae.vel[0] - be.vel[0]) # print "\t", step # if 0 <= step < minstep: # minstep = step # N7NN # ~N77777D. # .D77777777.. # 8777777777$. # ,7$7777777777N. # NONNNND7777778$NNND... # .ONNNN.ND777M8$7777777N.. # NNN===++NIN777777777777M. # N=======N7777777777777777N # .D$N=+===ZN==777===I~$7777777$NNNN. # N$ND=+===+N7777=877N777777777777777N. # .O==++==+D7?=7=+777N7777777777777777. # .$NNNDNN77?=7==77N+===+?N777N777777+ # D7777777777?=77=$7D+++=====NO7777777. # .+777777N7777?=777=I:::====++N7777777D. # .777777N7777777777NNN=====+NN7777777M.. # N777777D777777777N. ..NNNNNNN777777. #.7777777N77777777N. 8N7777ZN777777N. #NZ777777D77777777 .NN777N77777$N #D$NO78NN777777777. .8==N7?+NN77$7DM. #M==N=MDN$777777N N~?N=+N=I=IZ+++?. #+=+N+N+=+N77Z7N. .ON. N+==++NN #.....N=D+==+N ... .D.NN. # .NN+==+N # ...: denom = (axis[0] * (ae.vel[0] - be.vel[0])) + (axis[1] * (ae.vel[1] - be.vel[1])) if denom: # "eggs"-planation: the magnitude of the step factor is the parameter value that brings two objects from different points on the # projection axis to the same point. the sign of the step factor is determined by the object whose projection is largest in # magnitude, as this object is always bound to 'a' and 'ae'. if this object lies below the point of intersection, # the step factor will be positive, as its value needs to increase to reach the point. likewise, if it lies below, # the movement is downwards, and the step factor will be negative. so we take the absolute value of the whole thing if we just # want the step size step = math.fabs((axis[0] * (minp[0] - maxp[0])) + (axis[1] * (minp[1] - maxp[1])) / denom) #print "\t", step if step < minstep: minstep = step if minstep <= 1: # who cares if the collision isn't happening this turn colfuture = True i += 1 j += 1 # if col: # print "axis:", axis # print "\tmin1:", min1, "max2:", max2, "min2:", min2, "max1:", max1 # print "\tmin1:", min1p, "max2:", max2p, "min2:", min2p, "max1:", max1p if col: minstep = 0 return (col, colfuture, minstep)
def linear_hypothesis(X, theta): return vectors.dot(theta, X)
def test_can_calculate_dot_product_of_two_vectors(self): self.assertEqual(vectors.dot([1, 2], [8, 2]), 12)
def _collide(e1, e2): # orthogonally project the polygons onto the normal of each face. if all intersect, # the polygons intersect v = [e1.vertices(), e2.vertices()] col = True # currently colliding colfuture = False # will collide within one step minstep = 999999 # for a collision this frame, 0 <= step <= 1 # cut some corners if both objects are rectangular and axis-aligned aa = False if len(v[0]) == 4 and len(v[1]) == 4 and False: # two boxes. remove the False when code is fixed a = sorted(v[0]) b = sorted(v[1]) for i in [a, b]: if i[0][0] != i[1][0] or a[2][0] != a[3][0]: # should be two unique x values (lists sorted by x val) break elif i[0][1] != i[2][1] or i[1][1] != i[3][1]: # should be two unique y values break else: # axis aligned xs1 = ys1 = xs2 = ys2 = [] for i in a: xs1 += i[0] ys1 += i[1] for i in b: xs2 += i[0] ys2 += i[1] coords = [(xs1, ys1), (xs2, ys2)] overx = False if min(xs1) > max(xs2): maxx, minx = xs1, xs2 maxxe, minxe = e1, e2 elif min(xs2) > max(xs1): maxx, minx = xs2, xs1 maxxe, minxe = e2, e1 else: # they're overlapping on x. what if the shape moves right past? overx = True overy = False if min(ys1) > max(ys2): maxy, miny = ys1, ys2 maxye, minye = e1, e2 elif min(ys2) > max(ys1): maxy, miny = ys2, ys1 maxye, minye = e2, e1 else: # they're overlapping on y overy = True if overx and overy: col = True colfuture = True minstep = 0 aa = True else: #needs work tx = (minxe.pos[0] - maxxe.pos[0]) / (maxxe.vel[0] - minxe.vel[0]) ty = (minye.pos[1] - maxye.pos[1]) / (maxye.vel[1] - maxye.vel[1]) # generic stuff j = 0 while j < 2 and not aa: i = 0 while i < len(v[j]): axis = vectors.normalize((-(v[j][i][1] - v[j][i-1][1]), v[j][i][0] - v[j][i-1][0])) # axis of projection / normal # first poly min1 = max1 = vectors.dot(axis, v[0][0]) # the magnitude of a projection onto a unit vector is just their dot product #min1p = max1p = v[0][0] n = 0 while n < len(v[0]): proj = vectors.dot(axis, v[0][n]) if proj > max1: max1 = proj #max1p = v[0][n] elif proj < min1: min1 = proj #min1p = v[0][n] n += 1 #off = vectors.dot(axis, e1.pos) #min1 += off #max1 += off # second poly min2 = max2 = vectors.dot(axis, v[1][0]) #min2p = max2p = v[1][0] n = 0 while n < len(v[1]): proj = vectors.dot(axis, v[1][n]) if proj > max2: max2 = proj #max2p = v[1][n] elif proj < min2: min2 = proj #min2p = v[1][n] n += 1 #off = vectors.dot(axis, e2.pos) #min2 += off #max2 += off #print "j:", j, "i:", i #print "axis:", axis, "normal of:", v[j][i], "and", v[j][i - 1] #print "\tmin1:", min1, "max2:", max2, "min2:", min2, "max1:", max1 #print "\tmin1:", min1p, "max2:", max2p, "min2:", min2p, "max1:", max1p if min1 > max2 or min2 > max1: # potential for collision!! col = False # they are not overlapping, so both values of one projection (a) are larger than both values for the other (b) if(min1 > max2): # min1 will touch max2 on collision a, b = min1, max2 ae, be = e1, e2 elif(min2 > max1): # min2 will touch max1 on collision a, b = min2, max1 ae, be = e2, e1 # multiply the axis' unit vector by the vector magnitudes to find their coordinates maxp, minp = (axis[0] * a, axis[1] * a), (axis[0] * b, axis[1] * b) #print "collision anticipated at step =" # prevent division by zero error. they are not moving or are moving at the same speed #if be.vel[1] - ae.vel[1]: # step = (axis[0] - maxp[1] + minp[1]) / (be.vel[1] - ae.vel[1]) # print "\t", step # if 0 <= step < minstep: # minstep = step #if ae.vel[0] - be.vel[0]: # step = (axis[1] - minp[0] + maxp[0]) / (ae.vel[0] - be.vel[0]) # print "\t", step # if 0 <= step < minstep: # minstep = step # N7NN # ~N77777D. # .D77777777.. # 8777777777$. # ,7$7777777777N. # NONNNND7777778$NNND... # .ONNNN.ND777M8$7777777N.. # NNN===++NIN777777777777M. # N=======N7777777777777777N # .D$N=+===ZN==777===I~$7777777$NNNN. # N$ND=+===+N7777=877N777777777777777N. # .O==++==+D7?=7=+777N7777777777777777. # .$NNNDNN77?=7==77N+===+?N777N777777+ # D7777777777?=77=$7D+++=====NO7777777. # .+777777N7777?=777=I:::====++N7777777D. # .777777N7777777777NNN=====+NN7777777M.. # N777777D777777777N. ..NNNNNNN777777. #.7777777N77777777N. 8N7777ZN777777N. #NZ777777D77777777 .NN777N77777$N #D$NO78NN777777777. .8==N7?+NN77$7DM. #M==N=MDN$777777N N~?N=+N=I=IZ+++?. #+=+N+N+=+N77Z7N. .ON. N+==++NN #.....N=D+==+N ... .D.NN. # .NN+==+N # ...: denom = (axis[0] * (ae.vel[0] - be.vel[0])) + (axis[1] * (ae.vel[1] - be.vel[1])) if denom: # "eggs"-planation: the magnitude of the step factor is the parameter value that brings two objects from different points on the # projection axis to the same point. the sign of the step factor is determined by the object whose projection is largest in # magnitude, as this object is always bound to 'a' and 'ae'. if this object lies below the point of intersection, # the step factor will be positive, as its value needs to increase to reach the point. likewise, if it lies below, # the movement is downwards, and the step factor will be negative. so we take the absolute value of the whole thing if we just # want the step size step = math.fabs((axis[0] * (minp[0] - maxp[0])) + (axis[1] * (minp[1] - maxp[1])) / denom) #print "\t", step if step < minstep: minstep = step if minstep <= 1: # who cares if the collision isn't happening this turn colfuture = True i += 1 j += 1 # if col: # print "axis:", axis # print "\tmin1:", min1, "max2:", max2, "min2:", min2, "max1:", max1 # print "\tmin1:", min1p, "max2:", max2p, "min2:", min2p, "max1:", max1p if col: minstep = 0 return (col, colfuture, minstep)
def covariance(x, y): """how two variables vary in tandem from their means""" n = len(x) return dot(de_mean(x), de_mean(y)) / (n - 1)