def test_dimensions(): v=Vector([1,2,3,4]) assert v.dims == 4
def test_make_vector(): v = Vector([1, 2, 3]) assert len(v.nums) == 3 #passes if true, flags if false
def test_norm(): v = Vector([3, 4]) assert v.norm == 5
from src.physics import physics from src.physicsObject import physicsObject from src.vector import Vector # projectile calculations obj = physics(vector=Vector('mag_ang', 50, 50)) print(obj.x_values, obj.y_values) obj.proj.graph('all') obj.proj.graph('ty') obj.proj.graph('tx') obj.proj.graph('xy') """ output {'X0': 0, 'X': 3.42, 'A': 0, 'V0': 30.0, 'V': 30.0, 'T': 0.114} {'V': -53.082, 'V0': 51.962, 'X': 0, 'X0': 6, 'T': 0.114,'A': -9.8} and the images in the folder """ # kinematics calculations kin = physics(V=5, V0=10, T=1.6) print(kin.values) """ output {'V': 5, 'V0': 10, 'X': 12.0, 'X0': 0, 'T': 1.6, 'A': -3.125} """ # forces calculations obj = physicsObject(15, Vector('composite', 100, 35), static_friction_coefficient=.5, kinetic_friction_coefficient=.3) print(obj) obj.graph_fbd()
def test_dimension(): v = Vector([1, 2, 3]) assert v.dims == 3
def test_Plane_init_nolabels(): plane1 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=5) assert isinstance(plane1, Plane) assert plane1.normal_vector == Vector([1, 1, 1]) assert plane1.constant_term == 5
def test_Plane_parallel(): plane1 = Plane(Vector([1, 1, 1]), 1) plane2 = Plane(Vector([1, 1, 1]), 2) assert plane1 != plane2 assert plane1.isParallel(plane2)
def test_Plane_parallel_identical(): plane1 = Plane(Vector([1, 1, 1]), 1) plane2 = Plane(Vector([1, 1, 1]), 1) assert plane1 == plane2 assert plane1.isParallel(plane2)
def test_Line_init_nolabels(): line1 = Line(Vector([1, 1]), 4) assert isinstance(line1, Line) assert line1.normal_vector == Vector([1, 1]) assert line1.constant_term == 4
def col_vector(self, index): """返回矩阵的第index个列向量""" return Vector([row[index] for row in self._values])
def test_null_Line(): line1 = Line() assert isinstance(line1, Line) assert line1.normal_vector == Vector([0, 0]) assert line1.constant_term == 0
def row_vector(self, index): """返回矩阵的第index个行向量""" return Vector(self._values[index])
from src.vector import Vector x1 = Vector([8.218, -9.341]) y1 = Vector([-1.129, 2.111]) x2 = Vector([7.119, 8.215]) y2 = Vector([-8.223, 0.878]) x3 = Vector([1.671, -1.012, -0.318]) y3 = 7.41 x4 = Vector([-0.221, 7.437]) x5 = Vector([8.813, -1.331, -6.247]) x6 = Vector([5.581, -2.136]) x7 = Vector([1.996, 3.108, -4.554]) x8 = Vector([7.887, 4.138]) y8 = Vector([-8.802, 6.776]) x9 = Vector([-5.955, -4.904, -1.874]) y9 = Vector([-4.496, -8.755, 7.103]) x10 = Vector([3.183, -7.627]) y10 = Vector([-2.668, 5.319]) x11 = Vector([7.35, 0.221, 5.188]) y11 = Vector([2.751, 8.259, 3.985]) x12 = Vector([-7.579, -7.88]) y12 = Vector([22.737, 23.64]) x13 = Vector([-2.029, 9.97, 4.172]) y13 = Vector([-9.231, -6.639, -7.245]) x14 = Vector([-2.328, -7.284, -1.214]) y14 = Vector([-1.821, 1.072, -2.94]) x15 = Vector([2.118, 4.4827]) y15 = Vector([0, 0]) v1 = Vector([3.039, 1.879]) b1 = Vector([0.825, 2.036]) v2 = Vector([-9.88, -3.264, -8.159]) b2 = Vector([-2.155, -9.353, -9.473])
def index_to_vector(self, px: int, py: int) -> Vector: result = Vector() result.x = px / self.width result.y = py / self.height return result
def test_intersect(): line1 = Line(Vector([1, 1]), 2) line2 = Line(Vector([1, -1]), 2) intersect = line1.intersection(line2) assert intersect.coordinates[0] == pytest.approx(2, abs=1e-3) assert intersect.coordinates[1] == pytest.approx(0, abs=1e-3)
def test_Line_init_labels(): line1 = Line(normal_vector=Vector([2, 2]), constant_term=3) assert isinstance(line1, Line) assert line1.normal_vector == Vector([2, 2]) assert line1.constant_term == 3
def test_null_Plane(): plane1 = Plane() assert isinstance(plane1, Plane) assert plane1.normal_vector == Vector([0, 0, 0]) assert plane1.constant_term == 0
def test_Line_eq(): line1 = Line(Vector([1, 1]), 1) line2 = Line(Vector([-3, -3]), -3) assert line1 == line2
def test_Plane_init_labels(): plane1 = Plane(normal_vector=Vector([2, 2, 2]), constant_term=3) assert isinstance(plane1, Plane) assert plane1.normal_vector == Vector([2, 2, 2]) assert plane1.constant_term == 3
def test_Line_ne(): line1 = Line(Vector([1, 1]), 1) line2 = Line(Vector([-3, -3]), 3) assert line1 != line2
def tester(): working = True composite_to_mag = [{ "given": [15, 25], "expected": [29.15475947, 1.030376827, 59.03624347] }, { "given": [-36, 18], "expected": [40.24922356, 2.677945045, 153.43494884685] }, { "given": [-42, -69], "expected": [80.77747211, 4.165600139, 238.6715071] }, { 'given': [13, -26], "expected": [29.06888371, 5.176036589, 296.5650512] }] mag_to_composite = [{ 'given': [35, 74, "degrees"], "expected": [9.647307454, 33.64415936, 1.29154] }, { 'given': [69, 123, "degrees"], "expected": [-37.58009342, 57.86826919, 2.14675] }, { 'given': [42, 186, "degrees"], "expected": [-41.76991961, -4.390195457, 3.24631] }, { 'given': [15, 346, "degrees"], "expected": [14.55443589, -3.628828434, 6.03884] }, { 'given': [35, 1.29154, "radians"], "expected": [9.647307454, 33.64415936, 74] }, { 'given': [69, 2.14675, "radians"], "expected": [-37.58009342, 57.86826919, 123] }, { 'given': [42, 3.24631, "radians"], "expected": [-41.76991961, -4.390195457, 186] }, { 'given': [15, 6.03884, "radians"], "expected": [14.55443589, -3.628828434, 346] }] print("\nVector Conversion Test (Composite => Mag_ang)\n") for i in composite_to_mag: obj = Vector("composite", *i['given']) if nums_are_equal(obj.magnitude, i['expected'][0]) and nums_are_equal( obj.radians, i['expected'][1]) and nums_are_equal( obj.degrees, i['expected'][2]): print(f"Conversion {composite_to_mag.index(i)}: SUCCESS") else: working = False print(f"Conversion {composite_to_mag.index(i)}: FAILURE") print( f"Expected: {i['expected']},\nReceived: {obj.magnitude, obj.radians, obj.degrees}" ) print("\nVector Conversion Test (Mag_ang => Composite)\n") for i in mag_to_composite: obj = Vector('mag_ang', *i['given']) if nums_are_equal(obj.x, i['expected'][0]) and nums_are_equal( obj.y, i['expected'][1]) and ( nums_are_equal(obj.degrees, i['expected'][2]) or nums_are_equal(obj.radians, i['expected'][2])): print(f"Conversion {mag_to_composite.index(i)}: SUCCESS") else: working = False print(f"Conversion {mag_to_composite.index(i)}: FAILURE") print( f"Expected: {i['expected']},\nReceived: {obj.x, obj.y, obj.degrees, obj.radians}" ) return working
def test_hyperplane_gaussian_elimination_infinite_solutions(): p1 = Hyperplane(normal_vector=Vector(['0', '1', '1']), constant_term='1') p2 = Hyperplane(normal_vector=Vector(['0', '-1', '-1']), constant_term='-1') s = LinearSystem([p1, p2]) r = s.gaussian_elimination()
def test_make_vector(): v = Vector([1, 2, 3]) assert len(v.nums) == 3
def test_Line_not_parallel(): line1 = Line(Vector([1, 1.1]), 1) line2 = Line(Vector([1, 1]), 2) assert line1 != line2 assert not line1.isParallel(line2)
def test_scale(): v = Vector([1, 2, 3]) assert v.scale(3) == Vector([3, 6, 9])
def test_intersect_parallel(): line1 = Line(Vector([2, 2]), 1) line2 = Line(Vector([1, 1]), 2) assert line1.intersection(line2) == None
def test_dimensions(): v = Vector([6, 7, 8, 9]) assert v.dims == 4
def test_intersect_parallel_identical(): line1 = Line(Vector([2, 2]), 2) line2 = Line(Vector([1, 1]), 1) assert str(line1.intersection(line2)) == "2x_1 + 2x_2 = 2"
def test_unit_vector(): v = Vector([5, 3, 1, 9, 5]) assert v.unit_vector().norm == 1
def get_regions(self, small_region_area_ratio): dirs = [Vector(0, -1), Vector(0, 1), Vector(-1, 0), Vector(1, 0)] # (x, y) small_region_area_limit = small_region_area_ratio * \ self.img_size[0] * self.img_size[1] / (self.w_n * self.h_n) mask = np.invert(np.array(self.mask, dtype=np.uint8)) self.region_cnt, self.region_mat, stats, centroids = \ cv2.connectedComponentsWithStats(mask, connectivity=4, ltype=cv2.CV_32S) stats = stats.tolist() # Remap region idx region_idx_map = -1 * np.ones(self.region_cnt, dtype=np.int32) region_new_cnt = 0 for i in range(1, self.region_cnt): if stats[i][4] < small_region_area_limit: region_idx_map[i] = -1 else: region_idx_map[i] = region_new_cnt region_new_cnt += 1 self.region_mat = region_idx_map[self.region_mat] print('\tRegion cnt final (raw): %d (%d)' % (region_new_cnt, self.region_cnt - 1)) self.region_cnt = region_new_cnt # Expand valid region to fill out the canvas bg_pts = np.transpose(np.nonzero(self.region_mat == -1)).tolist() self.region_mat = self.region_mat.tolist() que = [] for bg_pt in bg_pts: cur_p = Vector(bg_pt[1], bg_pt[0]) for dir in dirs: next_p = cur_p + dir if utils.check_outside(next_p.x, next_p.y, self.img_size[1], self.img_size[0]) or \ self.region_mat[next_p.y][next_p.x] == -1: continue que.append(next_p) while len(que) > 0: cur_p = que.pop(0) for dir in dirs: next_p = cur_p + dir if utils.check_outside(next_p.x, next_p.y, self.img_size[1], self.img_size[0]) or \ self.region_mat[next_p.y][next_p.x] != -1: continue self.region_mat[next_p.y][next_p.x] = self.region_mat[cur_p.y][ cur_p.x] que.append(next_p) # Check the region mat unlabel_pts = np.transpose( np.nonzero(np.ma.masked_equal(self.region_mat, -1).mask)) assert (unlabel_pts.size == 0)