def test_beta_ignored_if_no_y(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.]]) beta = 1.5 expected = [[5.], [11.]] self.assertListEqual(gemv(A, x, beta=beta).tolist(), expected)
def test_trans_conforms_correctly(self): A = array([[1., 2., 3.], [3., 4., 5.]]) x = array([[1.], [2.]]) y = array([[3.], [4.], [5.]]) expected = [[10.], [14.], [18.]] self.assertListEqual(gemv(A, x, y, trans_a='t').tolist(), expected)
def test_uppercase_trans_a(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) expected = [[10.], [14.]] self.assertListEqual(gemv(A, x, y, 'T').tolist(), expected)
def passed_test(dtype, as_matrix, x_is_row, y_is_row, provide_y, stride, trans): """ Run one general matrix-vector multiplication 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_y: True if y 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 trans: BLASpy trans 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) m = randint(N_MIN, N_MAX) n = randint(N_MIN, N_MAX) stride = randint(N_MIN, STRIDE_MAX) if stride is None else stride x_length = n if trans == 'n' else m y_length = m if trans == 'n' else n # create random scalars, vectors, and matrices to test alpha = uniform(SCAL_MIN, SCAL_MAX) beta = uniform(SCAL_MIN, SCAL_MAX) x = random_vector(x_length, x_is_row, dtype, as_matrix) y = random_vector(y_length, y_is_row, dtype, as_matrix) if provide_y else None A = random_matrix(m / stride + (m % stride > 0), n / stride + (n % stride > 0), dtype, as_matrix) # create copies/views of A, x, and y that can be used to calculate the expected result A_2 = A if trans == 'n' else A.T x_2 = x.T if x_is_row else x if y is None: y_2 = zeros((1, n)) else: y_2 = copy(y.T) if y_is_row else copy(y) # compute the expected result if stride == 1: y_2 = beta * y_2 + alpha * dot(A_2, x_2) else: for i in range(0, y_2.shape[0], stride): A_partition = A_2[i / stride, :] x_partition = x_2[:: stride, :] y_2[i, 0] = (beta * y_2[i, 0]) + (alpha * dot(A_partition, x_partition)) # get the actual result y = gemv(A, x, y, trans, alpha, beta, inc_x=stride, inc_y=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_trans_a_lowercase(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) expected = [[10.], [14.]] self.assertListEqual(gemv(A, x, y, trans_a='t').tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_strides_greater_than_length_provide_y(self): A = array([[3.]]) x = array([[1.], [2.], [3.], [4.]]) y = array([[3.], [4.], [5.], [6.]]) expected = [[6.], [4.], [5.], [6.]] self.assertListEqual(gemv(A, x, y, inc_x=4, inc_y=4).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_unequal_strides(self): A = array([[1.], [3.]]) x = array([[1.], [2.], [3.], [4.]]) y = array([[3.], [4.], [5.], [6.]]) expected = [[4.], [4.], [8.], [6.]] self.assertListEqual(gemv(A, x, y, inc_x=4, inc_y=2).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_col_scalar_row_as_ndarray_provide_y(self): A = array([[1.], [2.], [3.]]) x = array([[3.]]) y = array([[4., 5., 6.]]) expected = [[7., 11., 15.]] self.assertListEqual(gemv(A, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_strides_less_than_length_provide_y(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.], [3.], [4.]]) y = array([[3.], [4.], [5.], [6.]]) expected = [[10.], [4.], [20.], [6.]] self.assertListEqual(gemv(A, x, y, inc_x=2, inc_y=2).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_matrix_row_col_as_ndarray_provide_y(self): A = array([[1., 2.], [3., 4.]]) x = array([[1., 2.]]) y = array([[3.], [4.]]) expected = [[8.], [15.]] self.assertListEqual(gemv(A, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_matrix_col_col_as_matrix_mixed_provide_y(self): A = asmatrix(array([[1., 2.], [3., 4.]])) x = array([[1.], [2.]]) y = asmatrix(array([[3.], [4.]])) expected = [[8.], [15.]] self.assertListEqual(gemv(A, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_alpha(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) alpha = 2.5 expected = [[15.5], [31.5]] self.assertListEqual(gemv(A, x, y, alpha=alpha).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_beta(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) beta = -1.5 expected = [[0.5], [5.]] self.assertListEqual(gemv(A, x, y, beta=beta).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_alpha_and_beta(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.]]) y = array([[3.], [4.]]) alpha = 2.5 beta = -1.5 expected = [[8.], [21.5]] self.assertListEqual(gemv(A, x, y, alpha=alpha, beta=beta).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def test_float64_dtype(self): A = array([[1., 2.], [3., 4.]], 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 = [[8.], [15.]] self.assertListEqual(gemv(A, x, y).tolist(), expected) self.assertListEqual(y.tolist(), expected)
def passed_test(dtype, as_matrix, b_is_row, stride, uplo, trans_a, diag): """ Run one triangular solve 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 b_is_row: True to test a row vector as parameter b, False to test a column vector stride: stride of x and y to test; if None, a random stride is assigned uplo: BLASpy uplo parameter to test trans_a: BLASpy trans parameter to test diag: BLASpy diag 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 vectors and matrices to test b = random_vector(n, b_is_row, dtype, as_matrix) A = random_triangular_matrix(n_A, dtype, as_matrix, uplo, diag) A /= n_A # scale off-diagonal to avoid numerical issues # fill diagonal with 1 if unit-triangular, else fill diagonal with values between 1 and 2 if diag == 'u': fill_diagonal(A, 1) else: for i in range(n_A): A[i, i] = uniform(1, 2) # copy b for comparison later expected = copy(b.T) if b_is_row else copy(b) # solve for x trsv(A, b, uplo, trans_a, diag, inc_b=stride) b = b.T if b_is_row else b # compute actual result actual = gemv(A, b, zeros((n, 1), dtype=dtype), trans_a=trans_a, inc_x=stride, inc_y=stride) if stride != 1: for i in range(n): if i % stride != 0: # zero out all non-relevant entries in expected vector expected[i] = 0 # compare the actual result to the expected result and return result of the test return allclose(actual, expected, RTOL, ATOL)
def test_matrix_col_col_as_matrix_mixed_no_y(self): A = array([[1., 2.], [3., 4.]]) x = asmatrix(array([[1.], [2.]])) expected = [[5.], [11.]] self.assertListEqual(gemv(A, x).tolist(), expected)
def test_scalars_as_ndarray_provide_y(self): A = array([[1.]]) x = array([[2.]]) y = array([[3.]]) self.assertEqual(gemv(A, x, y), 5) self.assertEqual(y, 5)
def test_col_scalar_as_ndarray_no_y(self): A = array([[1.], [2.], [3.]]) x = array([[3.]]) expected = [[3., 6., 9.]] # scalar x is treated as row vector when creating similar zero vector self.assertListEqual(gemv(A, x).tolist(), expected)
def test_matrix_row_row_as_ndarray_no_y(self): A = array([[1., 2.], [3., 4.]]) x = array([[1., 2.]]) expected = [[5., 11.]] self.assertListEqual(gemv(A, x).tolist(), expected)
def test_strides_less_than_length_no_y(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.], [3.], [4.]]) expected = [[7.], [15.]] self.assertListEqual(gemv(A, x, inc_x=2).tolist(), expected)
def test_row_row_scalar_as_ndarray_no_y(self): A = array([[1., 2., 3.]]) x = array([[3., 2., 1.]]) self.assertEqual(gemv(A, x), 10)
def test_inc_y_ignored_if_no_y(self): A = array([[1., 2.], [3., 4.]]) x = array([[1.], [2.], [3.], [4.]]) expected = [[7.], [15.]] self.assertListEqual(gemv(A, x, inc_x=2, inc_y=2).tolist(), expected)
def test_row_row_scalar_as_ndarray_provide_y(self): A = array([[1., 2., 3.]]) x = array([[3., 2., 1.]]) y = array([[4.]]) self.assertEqual(gemv(A, x, y), 14) self.assertEqual(y, 14)
def test_scalars_as_ndarray_no_y(self): A = array([[1.]]) x = array([[2.]]) self.assertEqual(gemv(A, x), 2)