def test_row_and_col_vectors_as_ndarrays_provide_A(self): A = array([[1.0, 0.0, 1.0], [-1.0, 3.0, 2.0], [0.0, 1.0, 0.0]]) x = array([[1.0, 2.0, 3.0]]) y = array([[3.0], [2.0], [1.0]]) expected = [[4.0, 2.0, 2.0], [5.0, 7.0, 4.0], [9.0, 7.0, 3.0]] self.assertListEqual(ger(x, y, A).tolist(), expected) self.assertListEqual(A.tolist(), expected)
def test_float64_dtype(self): x = array([[1.0], [2.0], [3.0]], dtype="float64") y = array([[3.0, 2.0, 1.0]], dtype="float64") expected = [[3.0, 2.0, 1.0], [6.0, 4.0, 2.0], [9.0, 6.0, 3.0]] self.assertEqual(x.dtype, "float64") self.assertEqual(y.dtype, "float64") self.assertListEqual(ger(x, y).tolist(), expected)
def test_as_matrices_provide_A(self): A = asmatrix(array([[1.0, 0.0, 1.0], [-1.0, 3.0, 2.0], [0.0, 1.0, 0.0]])) x = asmatrix(array([[1.0], [2.0], [3.0]])) y = asmatrix(array([[3.0, 2.0, 1.0]])) expected = [[4.0, 2.0, 2.0], [5.0, 7.0, 4.0], [9.0, 7.0, 3.0]] self.assertListEqual(ger(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): """ Run one general 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 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 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_x = randint(N_MIN, STRIDE_MAX) if stride is None else stride stride_y = randint(N_MIN, STRIDE_MAX) if stride is None else stride m_A = m / stride_x + (m % stride_x > 0) n_A = n / stride_y + (n % stride_y > 0) # create random scalars, vectors, and matrices to test alpha = uniform(SCAL_MIN, SCAL_MAX) x = random_vector(m, x_is_row, dtype, as_matrix) y = random_vector(n, y_is_row, dtype, as_matrix) A = random_matrix(m_A, 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 if y_is_row else y.T A_2 = zeros((m_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) else: for i in range(0, m_A): for j in range(0, n_A): A_2[i, j] += alpha * x_2[i * stride_x, 0] * y_2[0, j * stride_y] # get the actual result A = ger(x, y, A, alpha, inc_x=stride_x, inc_y=stride_y) # compare the actual result to the expected result and return result of the test return allclose(A, A_2, RTOL, ATOL)
def test_vectors_with_negatives_in_values(self): x = array([[-1.0], [-2.0], [3.0]]) y = array([[3.0, 2.0, 1.0]]) expected = [[-3.0, -2.0, -1.0], [-6.0, -4.0, -2.0], [9.0, 6.0, 3.0]] self.assertListEqual(ger(x, y).tolist(), expected)
def test_row_and_col_vectors_as_ndarrays_no_A(self): x = array([[1.0, 2.0, 3.0]]) y = array([[3.0], [2.0], [1.0]]) expected = [[3.0, 2.0, 1.0], [6.0, 4.0, 2.0], [9.0, 6.0, 3.0]] self.assertListEqual(ger(x, y).tolist(), expected)
def test_scalars_as_ndarray_no_A(self): x = array([[2.0]]) y = array([[3.0]]) self.assertEqual(ger(x, y), 6)
def test_scalars_as_ndarray_provide_A(self): A = array([[1.0]]) x = array([[2.0]]) y = array([[3.0]]) self.assertEqual(ger(x, y, A), 7) self.assertEqual(A, 7)
def test_alpha(self): x = array([[1.0], [2.0], [3.0]]) y = array([[3.0, 2.0, 1.0]]) expected = [[4.5, 3.0, 1.5], [9.0, 6.0, 3.0], [13.5, 9.0, 4.5]] self.assertListEqual(ger(x, y, alpha=1.5).tolist(), expected)
def test_unequal_strides(self): x = array([[1.0], [2.0], [3.0]]) y = array([[3.0, 2.0, 1.0]]) expected = [[3.0], [9.0]] self.assertListEqual(ger(x, y, inc_x=2, inc_y=3).tolist(), expected)
def test_strides_greater_than_length(self): x = array([[1.0], [2.0], [3.0]]) y = array([[3.0, 2.0, 1.0]]) expected = [[3.0]] self.assertListEqual(ger(x, y, inc_x=3, inc_y=3).tolist(), expected)
def test_strides_less_than_length(self): x = array([[1.0], [2.0], [3.0]]) y = array([[3.0, 2.0, 1.0]]) expected = [[3.0, 1.0], [9.0, 3.0]] self.assertListEqual(ger(x, y, inc_x=2, inc_y=2).tolist(), expected)
def test_vectors_as_mixed_matrices_and_ndarrays(self): x = asmatrix(array([[1.0], [2.0], [3.0]])) y = array([[3.0, 2.0, 1.0]]) expected = [[3.0, 2.0, 1.0], [6.0, 4.0, 2.0], [9.0, 6.0, 3.0]] self.assertListEqual(ger(x, y).tolist(), expected)
def test_as_matrices_no_A(self): x = asmatrix(array([[1.0], [2.0], [3.0]])) y = asmatrix(array([[3.0, 2.0, 1.0]])) expected = [[3.0, 2.0, 1.0], [6.0, 4.0, 2.0], [9.0, 6.0, 3.0]] self.assertListEqual(ger(x, y).tolist(), expected)