def passed_test(dtype, as_matrix, x_is_row, stride): """ Run one amax 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 vector to test x = random_vector(length, x_is_row, dtype, as_matrix) # create view of x that can be used to calculate the expected result x_2 = x.T if x_is_row else x # compute the expected result if stride == 1: expected = argmax(absolute(x)) else: expected = -1 current_max = -1 for i in range(0, length, stride): if abs(x_2[i, 0]) > current_max: current_max = abs(x_2[i, 0]) expected = i / stride # get the actual result actual = amax(x, stride) # compare the actual result to the expected result and return result of the test return actual == expected
def test_stride_greater_than_length(self): x = array([[1., 2., 3., 4.]]) self.assertEqual(amax(x, 5), 0) # 1 is at index 0
def test_float64_dtype(self): x = array([[1., 2., 3.]], dtype='float64') self.assertEqual(x.dtype, 'float64') self.assertEqual(amax(x), 2)
def test_vector_as_matrix(self): x = asmatrix(array([[1., 2., 3.]])) self.assertEqual(amax(x), 2)
def test_stride_less_than_length(self): x = array([[1., 2., 3., 4.]]) self.assertEqual(amax(x, 2), 1) # 3. is at index 1
def test_negative_element(self): x = x = array([[5., -6.5, 3.]]) self.assertEqual(amax(x), 1)
def test_column_vector_as_ndarray(self): x = array([[1.], [2.], [3.]]) self.assertEqual(amax(x), 2)
def test_last_element(self): x = x = array([[5., 2., 10.]]) self.assertEqual(amax(x), 2)
def test_middle_element(self): x = x = array([[5., 8., 3.]]) self.assertEqual(amax(x), 1)
def test_first_element(self): x = x = array([[5., 2., 3.]]) self.assertEqual(amax(x), 0)
def test_row_vector_as_ndarray(self): x = array([[1., 2., 3.]]) self.assertEqual(amax(x), 2)
def test_scalar_as_ndarray(self): x = array([[1.]]) self.assertEqual(amax(x), 0)