def test_col_as_ndarray_no_A(self): x = array([[1.], [2.]]) expected = [[1., 2.], [0., 4.]] self.assertListEqual(syr(x).tolist(), expected)
def test_as_matrix_all_no_A(self): x = asmatrix(array([[1.], [2.]])) expected = [[1., 2.], [0., 4.]] self.assertListEqual(syr(x).tolist(), expected)
def test_stride_greater_than_length_no_A(self): x = array([[1.], [2.], [3.], [4.]]) expected = [[1.]] self.assertListEqual(syr(x, inc_x=4).tolist(), expected)
def test_upper_triangle_ignored_with_uplo_L(self): A = array([[1., 55.], [2., 3.]]) x = array([[1.], [2.]]) expected = [[2., 55.], [4., 7.]] self.assertListEqual(syr(x, A, uplo='L').tolist(), expected)
def test_lower_triangle_ignored_with_uplo_U(self): A = array([[1., 2.], [-100., 3.]]) x = array([[1.], [2.]]) expected = [[2., 4.], [-100., 7.]] self.assertListEqual(syr(x, A, uplo='U').tolist(), expected)
def test_row_as_ndarray_provide_A(self): A = array([[1., 2.], [2., 3.]]) x = array([[1., 2.]]) expected = [[2., 4.], [2., 7.]] # syr only updates one triangle of A, upper by default self.assertListEqual(syr(x, A).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_stride_less_than_length_no_A(self): x = array([[1.], [2.], [3.], [4.]]) expected = [[1., 3.], [0., 9.]] self.assertListEqual(syr(x, inc_x=2).tolist(), expected)
def test_col_as_ndarray_provide_A(self): A = array([[1., 2.], [2., 3.]]) x = array([[1.], [2.]]) expected = [[2., 4.], [2., 7.]] self.assertListEqual(syr(x, A).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_as_matrix_mixed(self): A = asmatrix(array([[1., 2.], [2., 3.]])) x = array([[1.], [2.]]) expected = [[2., 4.], [2., 7.]] self.assertListEqual(syr(x, A).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_alpha(self): A = array([[1., 2.], [2., 3.]]) x = array([[1.], [2.]]) alpha = 2.5 expected = [[3.5, 7.], [2., 13.]] self.assertListEqual(syr(x, A, alpha=alpha).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') self.assertEqual(A.dtype, 'float64') self.assertEqual(x.dtype, 'float64') expected = [[2., 4.], [2., 7.]] self.assertListEqual(syr(x, A).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_stride_less_than_length_provide_A(self): A = array([[1., 2.], [2., 3.]]) x = array([[1.], [2.], [3.], [4.]]) expected = [[2., 5.], [2., 12.]] self.assertListEqual(syr(x, A, inc_x=2).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def passed_test(dtype, as_matrix, x_is_row, provide_A, stride, uplo): """ Run one symmetric rank-1 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 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) A = random_symmetric_matrix(n_A, dtype, as_matrix) if provide_A else None # create copies/views of A and x that can be used to calculate the expected result x_2 = x.T if x_is_row else x 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, 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] * x_2[j * stride, 0]) # get the actual result A = syr(x, A, uplo, alpha, inc_x=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_scalars_as_ndarray_no_A(self): x = array([[2.]]) self.assertEqual(syr(x), 4)
def test_scalars_as_ndarray_provide_A(self): A = array([[1.]]) x = array([[2.]]) self.assertEqual(syr(x, A), 5) self.assertEqual(A, 5)