def test_normalisation(self): r = np.random.rand(100,3) n = norm_vect(r) d = dot_product(n,n) ones = np.ones((100)) assert_almost_equal(d, ones, 8, err_msg="Your vectors don't normalise too well")
def push(self, bullet_blob, strength): direction = vector.normalize( vector.substract(self.position, bullet_blob.get_position())) bullet_force = bullet_blob.get_force() self_force_strength = vector.dot_product(direction, bullet_force) * strength self.add_force(vector.multiply(direction, self_force_strength)) bullet_blob.set_force(vector.substract(bullet_force, self.force))
def covariance( list_a, list_b, offset=0 ): #sometimes counting starts at 0, which results in the covariance to be divided by n-1 if (len(list_a) != len(list_b)): raise Exception("lists must be same length") n = len(list_a) return dot_product([sub_mean(list_a), sub_mean(list_b)]) / n - offset
def ray_colour(r, s): normal = sphere.normal(s, r[0]) # calculate angle to ray dot = abs(vector.dot_product(r[1], normal) / 2.0) # apply specular dot = dot * s[3] # scale colour by the brightness colour = vector.scale(s[2], dot) # clamp to 255 colour = [min(colour[0], 255), min(colour[1], 255), min(colour[2], 255)] return colour
def test_block_vect(self): r = np.random.rand(100, 3) d = dot_product(r, r) r_sqar = np.power(r[:,:], 2) r_sum = np.sum(r_sqar[:,:], axis=1) assert_equal(np.shape(d), np.shape(r_sum), err_msg="The shape of the final dot product is not right") assert_almost_equal(d[:], r_sum[:], 8, err_msg="The dot_product sum does not add up (funny, huh?)")
def dihedral_calc(r1, r2, r3, r4): """ This function calculates the angle made by the vectors normal to the planes made by r1, r2, r3 and r2, r3, r4. Each of the positions is assumed to be a 3 by n array for n lists of dihedral sets r1 \ \ r2---r3 \ \ r4 The angle for dihedral will go between +180 and -180 degrees """ r12 = vect_calc(r1, r2) r23 = vect_calc(r2, r3) r34 = vect_calc(r3, r4) r123 = np.cross(r12[:], r23[:]) r234 = np.cross(r23[:], r34[:]) n123 = norm_vect(r123) n234 = norm_vect(r234) axis_2 = np.cross(n123[:], r23[:]) axis_2_norm = norm_vect(axis_2) x = dot_product(n123[:], n234[:]) y = dot_product(axis_2_norm[:], n234[:]) dih_angle = np.arctan2(y, x) return dih_angle
def point_at(pos: Vector3, target: Vector3, up: Vector3): # Calculate new forward direction new_forward = vector.subtract(target, pos) new_forward.normalise() # Calculate new Up direction a: Vector3 = vector.multiply(new_forward, vector.dot_product(up, new_forward)) new_up: Vector3 = vector.subtract(up, a) new_up.normalise() # New Right direction is easy, its just cross product new_right: vector.Vector3 = vector.cross_product(new_up, new_forward) # Construct Dimensioning and Translation Matrix matrix = Matrix4() matrix.m[0][0] = new_right.x; matrix.m[0][1] = new_right.y; matrix.m[0][2] = new_right.z; matrix.m[0][3] = 0 matrix.m[1][0] = new_up.x; matrix.m[1][1] = new_up.y; matrix.m[1][2] = new_up.z; matrix.m[1][3] = 0 matrix.m[2][0] = new_forward.x; matrix.m[2][1] = new_forward.y; matrix.m[2][2] = new_forward.z; matrix.m[2][3] = 0 matrix.m[3][0] = pos.x; matrix.m[3][1] = pos.y; matrix.m[3][2] = pos.z; matrix.m[3][3] = 1 return matrix
def dist(p: Vector3): # Return shortest distance from point to plane, plane normal must be normalised return (plane_n.x * p.x + plane_n.y * p.y + plane_n.z * p.z - vector.dot_product(plane_n, plane_p))
def test_sum_vect(self): r = np.array(([[1,2,3]])) d = dot_product(r,r) assert_equal(d, 14, err_msg="sum in dot_product is broken")
def _dim_vect(self,r): d = dot_product(r,r) assert_equal( d, 4, err_msg="dot_product is in 1 dimension broken")
def covariance(xs: List[float], ys: List[float]) -> float: "Measure of how two variables vary in tandem from their means" assert len(xs) == len(ys), "xs and ys must have same number of elements" return dot_product(delta_mean(xs), delta_mean(ys)) / (len(xs) - 1)