def eulers_method_overapprox(s0, v0, a, total_time, step_count): trajectory = [s0] s = s0 v = v0 dt = total_time / step_count for _ in range(0, step_count): v = add(v, scale(dt, a)) # v is updated for current cycle s = add(s, scale(dt, v)) trajectory.append(s) return trajectory
def eulers_method(s0, v0, a, total_time, step_count): """Returns the trajectory of an object given the initial conditions s0, v0, assuming it will have a constant acceleration a, for the time from t0=0 to t=total_time splitting the intervals into the given step_count """ trajectory = [s0] s = s0 v = v0 dt = total_time / step_count for _ in range(0, step_count): s = add(s, scale(dt, v)) v = add(v, scale(dt, a)) # v is updated for the next cycle trajectory.append(s) return trajectory
def test_add_2_happy_path(self): u = (1, 2) v = (3, 4) add_actual_result = add(u, v) add_expected_result = (u[0] + v[0], u[1] + v[1]) self.assertEqual(add_actual_result, add_expected_result, 'add 2 vectors of same size should match')
def test_add_3_happy_path(self): u = (1, 2, 3) v = (4, 5, 6) w = (7, 8, 9) add_actual_result = add(u, v, w) add_expected_result = (u[0] + v[0] + w[0], u[1] + v[1] + w[1], u[2] + v[2] + w[2]) self.assertEqual(add_actual_result, add_expected_result, 'add 2 vectors of same size should match')
def transformed(self): # before rotation was: # return [add((self.x, self.y), v) for v in self.points] rotated_points = [ rotate2d(self.rotation_angle, point) for point in self.points ] return [ add((self.x, self.y), rotated_point) for rotated_point in rotated_points ]
def move(self, milliseconds): dx, dy = (self.vx * milliseconds / 1000, self.vy * milliseconds / 1000) self.x, self.y = add((self.x, self.y), (dx, dy)) # added in section 9.1.3 (teleportation when going offscreen) if self.x < -10: self.x += 20 if self.y < -10: self.y += 20 if self.x > 10: self.x -= 20 if self.y > 10: self.y -= 20
def new_function(t): return add(v1, scale(t, subtract(v2, v1)))
def linear_combination(scalars, *vectors): scaled = [scale(s, v) for s, v in zip(scalars, vectors)] return add(*scaled)
def new_function(v): return add(vector, v)
def test_add_single_vector(self): u = (1, 2) with self.assertRaises( TypeError, msg='should raise TypeError when passing only one vector'): add(u)
def test_add_no_args(self): with self.assertRaises( TypeError, msg='shoud raise TypeError when passing no args'): add()
def transformed(self): return [add((self.x, self.y), v) for v in self.points]
def add(self, other): # author used a one-liner, but these three lines make it easier to see what's going on coordinate_sum_as_tuple = add(self.coordinates, other.coordinates) sum_as_concrete_coordinate_vector = self.__class__( *coordinate_sum_as_tuple) return sum_as_concrete_coordinate_vector