Пример #1
0
    def test_vectors_as_mixed_matrices_and_ndarrays(self):
        x = array([[1., 2., 3.]])
        y = asmatrix(array([[4., 5., 6.]]))

        expected_x = y.tolist()
        expected_y = x.tolist()
        self.assertListEqual(swap(x, y).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
Пример #2
0
    def test_strides_less_than_length(self):
        x = array([[1., 2., 3.]])
        y = array([[4., 5., 6.]])

        expected_x = [[4., 2., 6.]]
        expected_y = [[1., 5., 3.]]
        self.assertListEqual(swap(x, y, inc_x=2, inc_y=2).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
Пример #3
0
    def test_row_and_col_vectors_as_ndarrays(self):
        x = array([[1., 2., 3.]])
        y = array([[4.], [5.], [6.]])

        expected_x = [[4., 5., 6.]]
        expected_y = [[1.], [2.], [3.]]
        self.assertListEqual(swap(x, y).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
Пример #4
0
    def test_negative_element(self):
        x = array([[1., -2., 3.]])
        y = array([[4., 5., 6.]])

        expected_x = y.tolist()
        expected_y = x.tolist()
        self.assertListEqual(swap(x, y).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
Пример #5
0
    def test_two_column_vectors_as_ndarrays(self):
        x = array([[1.], [2.], [3.]])
        y = array([[4.], [5.], [6.]])

        expected_x = y.tolist()
        expected_y = x.tolist()
        self.assertListEqual(swap(x, y).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
Пример #6
0
    def test_two_row_vectors_as_ndarrays(self):
        x = array([[1., 2., 3.]])
        y = array([[4., 5., 6.]])

        expected_x = y.tolist()
        expected_y = x.tolist()
        self.assertListEqual(swap(x, y).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
Пример #7
0
    def test_scalar_as_ndarray(self):
        x = array([[1.]])
        y = array([[2.]])

        expected_x = y.tolist()
        expected_y = x.tolist()
        self.assertListEqual(swap(x, y).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
Пример #8
0
    def test_unequal_strides(self):
        x = array([[1., 2., 3., 4., 5., 6.]])
        y = array([[4., 5., 6.]])

        expected_x = [[4., 2., 5., 4., 6., 6.]]
        expected_y = [[1., 3., 5.]]
        self.assertListEqual(swap(x, y, inc_x=2, inc_y=1).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)
def passed_test(dtype, as_matrix, x_is_row, y_is_row, stride):
    """
    Run one vector swap 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 vectors to test
    x = random_vector(length, x_is_row, dtype, as_matrix)
    y = random_vector(length, y_is_row, dtype, as_matrix)

    # compute the expected result
    if stride == 1:
        x_2 = copy(y.T) if y_is_row else copy(y)
        y_2 = copy(x.T) if x_is_row else copy(x)
    else:
        x_2 = copy(x.T) if x_is_row else copy(x)
        y_2 = copy(y.T) if y_is_row else copy(y)
        for i in range(0, length, stride):
            temp = x_2[i, 0]
            x_2[i, 0] = y_2[i, 0]
            y_2[i, 0] = temp

    # get the actual result
    swap(x, y, stride, stride)

    # compare the actual result to the expected result and return result of the test
    passed_x = allclose(x.T, x_2) if x_is_row else allclose(x, x_2)
    passed_y = allclose(y.T, y_2) if y_is_row else allclose(y, y_2)
    return passed_x and passed_y
Пример #10
0
    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_x = y.tolist()
        expected_y = x.tolist()
        self.assertListEqual(swap(x, y).tolist(), expected_y)
        self.assertListEqual(x.tolist(), expected_x)
        self.assertListEqual(y.tolist(), expected_y)