예제 #1
0
    def test_matrix_row_row_as_ndarray_no_y(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1., 2.]])

        expected = [[5., 8.]]
        self.assertListEqual(symv(A, x).tolist(), expected)
예제 #2
0
    def test_upper_triangle_ignored_with_uplo_L(self):
        A = array([[1., 55.],
                   [2., 3.]])
        x = array([[1.],
                   [2.]])

        expected = [[5.],
                    [8.]]
        self.assertListEqual(symv(A, x, uplo='L').tolist(), expected)
예제 #3
0
    def test_matrix_row_row_as_ndarray_provide_y(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1., 2.]])
        y = array([[3., 4.]])

        expected = [[8., 12.]]
        self.assertListEqual(symv(A, x, y).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #4
0
    def test_lower_triangle_ingnored_by_default(self):
        A = array([[1., 2.],
                   [-100., 3.]])
        x = array([[1.],
                   [2.]])

        expected = [[5.],
                    [8.]]
        self.assertListEqual(symv(A, x).tolist(), expected)
예제 #5
0
    def test_as_matrix_mixed_no_y(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = asmatrix(array([[1.],
                            [2.]]))

        expected = [[5.],
                    [8.]]
        self.assertListEqual(symv(A, x).tolist(), expected)
예제 #6
0
    def test_lower_triangle_ignored_with_uplo_U(self):
        A = array([[1., 2.],
                   [-100., 3.]])
        x = array([[1.],
                   [2.]])

        expected = [[5.],
                    [8.]]
        self.assertListEqual(symv(A, x, uplo='U').tolist(), expected)
def passed_test(dtype, as_matrix, x_is_row, y_is_row, provide_y, stride, uplo):
    """
    Run one symmetric 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
        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)
    beta = 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) if provide_y else None
    A = random_symmetric_matrix(n_A, dtype, as_matrix)

    # create copies/views of x and y that can be used to calculate the expected result
    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, x_2)
    else:
        for i in range(0, y_2.shape[0], stride):
            A_partition = A[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
    A = (triu(A) if uplo == 'u' else tril(A)).astype(dtype)
    y = symv(A, x, y, uplo, 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)
예제 #8
0
    def test_inc_y_ignored_if_no_y(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.],
                   [3.],
                   [4.]])

        expected = [[7.], [11.]]
        self.assertListEqual(symv(A, x, inc_x=2, inc_y=2).tolist(), expected)
예제 #9
0
    def test_beta_ignored_if_no_y(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.]])
        beta = 1.5

        expected = [[5.],
                    [8.]]
        self.assertListEqual(symv(A, x, beta=beta).tolist(), expected)
예제 #10
0
    def test_strides_less_than_length_no_y(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.],
                   [3.],
                   [4.]])

        expected = [[7.],
                    [11.]]
        self.assertListEqual(symv(A, x, inc_x=2).tolist(), expected)
예제 #11
0
    def test_as_matrix_mixed_provide_y(self):
        A = asmatrix(array([[1., 2.],
                            [2., 3.]]))
        x = array([[1.],
                   [2.]])
        y = asmatrix(array([[3.],
                            [4.]]))

        expected = [[8.],
                    [12.]]
        self.assertListEqual(symv(A, x, y).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #12
0
    def test_alpha(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.]])
        y = array([[3.],
                   [4.]])
        alpha = 2.5

        expected = [[15.5],
                    [24.]]
        self.assertListEqual(symv(A, x, y, alpha=alpha).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #13
0
    def test_beta(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.]])
        y = array([[3.],
                   [4.]])
        beta = -1.5

        expected = [[0.5],
                    [2.]]
        self.assertListEqual(symv(A, x, y, beta=beta).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #14
0
    def test_alpha_and_beta(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.]])
        y = array([[3.],
                   [4.]])
        alpha = 2.5
        beta = -1.5

        expected = [[8.],
                    [14.]]
        self.assertListEqual(symv(A, x, y, alpha=alpha, beta=beta).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #15
0
    def test_unequal_strides(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.],
                   [3.],
                   [4.]])
        y = array([[3.],
                   [4.]])

        expected = [[10.],
                    [15.]]
        self.assertListEqual(symv(A, x, y, inc_x=2, inc_y=1).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #16
0
    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 = [[8.],
                    [12.]]
        self.assertListEqual(symv(A, x, y).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #17
0
    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(symv(A, x, y, inc_x=4, inc_y=4).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #18
0
    def test_strides_less_than_length_provide_y(self):
        A = array([[1., 2.],
                   [2., 3.]])
        x = array([[1.],
                   [2.],
                   [3.],
                   [4.]])
        y = array([[3.],
                   [4.],
                   [5.],
                   [6.]])

        expected = [[10.],
                    [4.],
                    [16.],
                    [6.]]
        self.assertListEqual(symv(A, x, y, inc_x=2, inc_y=2).tolist(), expected)
        self.assertListEqual(y.tolist(), expected)
예제 #19
0
 def test_scalars_as_ndarray_no_y(self):
     A = array([[1.]])
     x = array([[2.]])
     self.assertEqual(symv(A, x), 2)
예제 #20
0
 def test_scalars_as_ndarray_provide_y(self):
     A = array([[1.]])
     x = array([[2.]])
     y = array([[3.]])
     self.assertEqual(symv(A, x, y), 5)
     self.assertEqual(y, 5)