def test_row_row_as_ndarray_no_A(self): x = array([[1., 2.]]) y = array([[3., 4.]]) expected = [[6., 10.], [0., 16.]] self.assertListEqual(syr2(x, y).tolist(), expected)
def test_matrix_col_col_as_ndarray_no_A(self): x = array([[1.], [2.]]) y = array([[3.], [4.]]) expected = [[6., 10.], [0., 16.]] self.assertListEqual(syr2(x, y).tolist(), expected)
def test_as_matrix_mixed_no_A(self): x = asmatrix(array([[1.], [2.]])) y = array([[3.], [4.]]) expected = [[6., 10.], [0., 16.]] self.assertListEqual(syr2(x, y).tolist(), expected)
def test_row_row_as_ndarray_provide_A(self): A = array([[1., 2.], [2., 3.]]) x = array([[1., 2.]]) y = array([[3., 4.]]) expected = [[7., 12.], [2., 19.]] self.assertListEqual(syr2(x, y, A).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def passed_test(dtype, as_matrix, x_is_row, y_is_row, provide_A, stride, uplo): """ Run one symmetric rank-2 update 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 provide_A: True if A is to be provided to the BLASpy function, False otherwise stride: stride of x and y to test; if None, a random stride is assigned uplo: BLASpy uplo parameter to test Returns: True if the expected result is within the margin of error of the actual result, False otherwise. """ # generate random sizes for matrix/vector dimensions and vector stride (if necessary) n = randint(N_MIN, N_MAX) stride = randint(N_MIN, STRIDE_MAX) if stride is None else stride n_A = n / stride + (n % stride > 0) # create random scalars, vectors, and matrices to test alpha = uniform(SCAL_MIN, SCAL_MAX) x = random_vector(n, x_is_row, dtype, as_matrix) y = random_vector(n, y_is_row, dtype, as_matrix) A = random_symmetric_matrix(n_A, dtype, as_matrix) if provide_A else None # create copies/views of A, 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 A_2 = zeros((n_A, n_A)) if A is None else copy(A) # compute the expected result if stride == 1: A_2 += alpha * dot(x_2, y_2.T) A_2 += alpha * dot(y_2, x_2.T) else: for i in range(0, n_A): for j in range(0, n_A): A_2[i, j] += alpha * (x_2[i * stride, 0] * y_2[j * stride, 0]) A_2[i, j] += alpha * (y_2[i * stride, 0] * x_2[j * stride, 0]) # get the actual result A = syr2(x, y, A, uplo, alpha, inc_x=stride, inc_y=stride) # make A and A_2 triangular so that they can be compared A = triu(A) if uplo == 'u' else tril(A) A_2 = triu(A_2) if uplo == 'u' else tril(A_2) # compare the actual result to the expected result and return result of the test return allclose(A, A_2, RTOL, ATOL)
def test_lower_triangle_ignored_with_uplo_u(self): A = array([[1., 2.], [-100., 3.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) expected = [[7., 12.], [-100., 19.]] self.assertListEqual(syr2(x, y, A, uplo='u').tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_upper_triangle_ignored_with_uplo_L(self): A = array([[1., 55.], [2., 3.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) expected = [[7., 55.], [12., 19.]] self.assertListEqual(syr2(x, y, A, uplo='L').tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_strides_less_than_length_no_A(self): x = array([[1.], [2.], [3.], [4.]]) y = array([[3.], [4.], [5.], [6.]]) expected = [[6., 14.], [0., 30.]] self.assertListEqual(syr2(x, y, inc_x=2, inc_y=2).tolist(), expected)
def test_alpha(self): A = array([[1., 2.], [2., 3.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) alpha = 2.5 expected = [[16., 27.], [2., 43.]] self.assertListEqual(syr2(x, y, A, alpha=alpha).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_unequal_strides(self): A = array([[1., 2.], [2., 3.]]) x = array([[1.], [2.], [3.], [4.]]) y = array([[3.], [5.]]) expected = [[7., 16.], [2., 33.]] self.assertListEqual(syr2(x, y, A, inc_x=2, inc_y=1).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_strides_greater_than_length_provide_A(self): A = array([[3.]]) x = array([[1.], [2.], [3.], [4.]]) y = array([[3.], [4.], [5.], [6.]]) expected = [[9.]] self.assertListEqual(syr2(x, y, A, inc_x=4, inc_y=4).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_float64_dtype(self): A = array([[1., 2.], [2., 3.]], dtype='float64') x = array([[1.], [2.]], dtype='float64') y = array([[3.], [4.]], dtype='float64') self.assertEqual(A.dtype, 'float64') self.assertEqual(x.dtype, 'float64') self.assertEqual(y.dtype, 'float64') expected = [[7., 12.], [2., 19.]] self.assertListEqual(syr2(x, y, A).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_strides_less_than_length_provide_A(self): A = array([[1., 2.], [2., 3.]]) x = array([[1.], [2.], [3.], [4.]]) y = array([[3.], [4.], [5.], [6.]]) expected = [[7., 16.], [2., 33.]] self.assertListEqual(syr2(x, y, A, inc_x=2, inc_y=2).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_scalars_as_ndarray_no_A(self): x = array([[2.]]) y = array([[3.]]) self.assertEqual(syr2(x, y), 12)
def test_scalars_as_ndarray_provide_A(self): A = array([[1.]]) x = array([[2.]]) y = array([[3.]]) self.assertEqual(syr2(x, y, A), 13) self.assertEqual(A, 13)