def passed_test(dtype, as_matrix, x_is_row, stride): """ Run one vector scaling test. Arguments: dtype: either 'float64' or 'float32', the NumPy dtype to test as_matrix: True to test a NumPy matrix, False to test a NumPy ndarray x_is_row: True to test a row vector as parameter x, False to test a column vector stride: stride of x and y to test; if None, a random stride is assigned Returns: True if the expected result is within the margin of error of the actual result, False otherwise. """ # generate random sizes for vector dimensions and vector stride (if necessary) length = randint(N_MIN, N_MAX) stride = randint(N_MIN, STRIDE_MAX) if stride is None else stride # create random scalar and vector to test alpha = uniform(SCAL_MIN, SCAL_MAX) x = random_vector(length, x_is_row, dtype, as_matrix) # create copy of x to hold the expected result x_2 = copy(x.T) if x_is_row else copy(x) # compute the expected result if stride == 1: x_2 *= alpha else: for i in range(0, length, stride): x_2[i, 0] *= alpha # get the actual result scal(alpha, x, stride) # if x is a row vector, make x_2 a row vector as well x_2 = x_2.T if x.shape[0] == 1 else x_2 # compare the actual result to the expected result and return result of the test return allclose(x, x_2)
def test_float64_dtype(self): x = array([[1., 2., 3.]], dtype='float64') self.assertEqual(x.dtype, 'float64') expected = [[2., 4., 6.]] self.assertListEqual(scal(2, x,).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_stride_greater_than_length(self): x = array([[1., 2., 3.]]) expected = [[2., 2., 3.]] self.assertListEqual(scal(2, x, inc_x=3).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_vector_as_matrix(self): x = asmatrix(array([[1., 2., 3.]])) expected = [[2., 4., 6.]] self.assertListEqual(scal(2, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_negative_alpha_with_vectors(self): x = array([[-1., -2., 3.]]) expected = [[1.5, 3., -4.5]] self.assertListEqual(scal(-1.5, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_alpha_equal_to_zero_with_vectors(self): x = array([[-1., -2., 3.]]) expected = [[0., 0., 0.]] self.assertListEqual(scal(0, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_vector_with_negatives_elements(self): x = array([[-1., -2., 3.]]) expected = [[-2., -4., 6.]] self.assertListEqual(scal(2, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_two_vector_as_ndarray(self): x = array([[1.], [2.], [3.]]) expected = [[2.], [4.], [6.]] self.assertListEqual(scal(2, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_row_vector_as_ndarray(self): x = array([[1., 2., 3.]]) expected = [[2., 4., 6.]] self.assertListEqual(scal(2, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_negative_alpha_with_scalar(self): x = array([[1.]]) expected = [[-1.]] self.assertListEqual(scal(-1, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_alpha_equal_to_zero_with_scalar(self): x = array([[1.]]) expected = [[0.]] self.assertListEqual(scal(0, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)
def test_scalar_as_ndarray(self): x = array([[1.]]) expected = [[2.]] self.assertListEqual(scal(2, x).tolist(), expected) self.assertListEqual(x.tolist(), expected)