def test_negative_alpha_with_vectors(self): x = array([[-1., -2., 3.]]) y = array([[3., 2., 1.]]) expected = [[4.5, 5., -3.5]] self.assertListEqual(axpy(-1.5, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_vectors_as_matrices(self): x = asmatrix(array([[1., 2., 3.]])) y = asmatrix(array([[3., 2., 1.]])) expected = [[5., 6., 7.]] self.assertListEqual(axpy(2, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_vectors_with_negatives_in_values(self): x = array([[-1., -2., 3.]]) y = array([[3., 2., 1.]]) expected = [[1., -2., 7.]] self.assertListEqual(axpy(2, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_alpha_equal_to_zero_with_vectors(self): x = array([[-1., -2., 3.]]) y = array([[3., 2., 1.]]) expected = [[3., 2., 1.]] self.assertListEqual(axpy(0, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_two_row_vectors_as_ndarrays(self): x = array([[1., 2., 3.]]) y = array([[3., 2., 1.]]) expected = [[5., 6., 7.]] self.assertListEqual(axpy(2, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_row_and_col_vectors_as_ndarrays(self): x = array([[1., 2., 3.]]) y = array([[3.], [2.], [1.]]) expected = [[5.], [6.], [7.]] self.assertListEqual(axpy(2, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_alpha_equal_to_zero_with_scalar(self): x = array([[1.]]) y = array([[2.]]) expected = [[2.]] self.assertListEqual(axpy(0, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_negative_alpha_with_scalar(self): x = array([[1.]]) y = array([[2.]]) expected = [[1.]] self.assertListEqual(axpy(-1, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_scalar_as_ndarray(self): x = array([[1.]]) y = array([[2.]]) expected = [[3.]] self.assertListEqual(axpy(1, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_unequal_strides(self): x = array([[1., 2., 3., 4., 5., 6.]]) y = array([[3., 2., 1.]]) expected = [[5., 8., 11.]] self.assertListEqual(axpy(2, x, y, inc_x=2, inc_y=1).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_strides_greater_than_length(self): x = array([[1., 2., 3.]]) y = array([[3., 2., 1.]]) expected = [[5., 2., 1.]] self.assertListEqual(axpy(2, x, y, inc_x=3, inc_y=3).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def passed_test(dtype, as_matrix, x_is_row, y_is_row, stride): """ Run one axpy operation 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 y_is_row: True to test a row vector as parameter y, 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 vectors to test alpha = uniform(SCAL_MIN, SCAL_MAX) x = random_vector(length, x_is_row, dtype, as_matrix) y = random_vector(length, y_is_row, dtype, as_matrix) # create views of x and y that can be used to calculate the expected result x_2 = x.T if x_is_row else x y_2 = y.T if y_is_row else y # compute the expected result if stride == 1: y_2 += alpha * x_2 else: for i in range(0, length, stride): y_2[i, 0] += alpha * x_2[i, 0] # get the actual result axpy(alpha, x, y, stride, stride) # if y is a row vector, make y_2 a row vector as well if y.shape[0] == 1: y_2 = y_2.T # compare the actual result to the expected result and return result of the test return allclose(y, y_2, RTOL, ATOL)
def test_float64_dtype(self): x = array([[1., 2., 3.]], dtype='float64') y = array([[3., 2., 1.]], dtype='float64') self.assertEqual(x.dtype, 'float64') self.assertEqual(y.dtype, 'float64') expected = [[5., 6., 7.]] self.assertListEqual(axpy(2, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)