def test_scale_vector_arg(self): factor = 1.0 vector = 2.0 with self.assertRaises( TypeError, msg='should raise TypeError when not passing a vector'): scale(factor, vector)
def test_scale_vector_holding_other_than_numbers(self): factor = 1.0 vector = ('a', 'b') with self.assertRaises( TypeError, msg= 'should raise TypeError when passing vectors holding no numeric values' ): scale(factor, vector)
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_2d_scale_happy_path(self): factor = 1.0 vector = (2, 3) scale_actual_result = scale(factor, vector) scale_expected_result = (2, 3) self.assertEqual(scale_actual_result, scale_expected_result, 'scale in 2D should match')
def test_3d_scale_happy_path(self): vector = (1, 2, 3) factor = 4.5 scale_actual_result = scale(factor, vector) scale_expected_result = (vector[0] * factor, vector[1] * factor, vector[2] * factor) self.assertEqual(scale_actual_result, scale_expected_result, 'scale in 2D should match')
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 scale(factor, v)
def scale(self, scalar): # author used a one-liner, but these three lines make it easier to see what's going on # return self.__class__(*scale(scalar, self.coordinates)) coordinate_scalar_product_as_tuple = scale(scalar, self.coordinates) scalar_product_as_concrete_coordinate_vector = self.__class__( *coordinate_scalar_product_as_tuple) return scalar_product_as_concrete_coordinate_vector