Esempio n. 1
0
def test_assert_matrix_equals_4(x):
    dims = randrange(1, 100), randrange(1, 100), randrange(1, 100)
    A = np.random.random_sample(dims)

    with pytest.raises(ValueError):
        assert_matrix_equals(A, A)
    return "A matrix with shape of {} is invalid input".format(A.shape)
def test_eigen_2():
    A = gen_matrix(100, 100, square=True)
    eigenvalues, eigenvectors = np.linalg.eig(A)
    # assert the property holds for each vector and value pair
    Ax = np.dot(A, eigenvectors)
    lx = eigenvalues.reshape(
        1, A.shape[0]
    ) * eigenvectors  # multiply each eigenvalue by corresponding vector
    assert_matrix_equals(Ax, lx)
def test_eigen_1():
    A = gen_matrix(100, 100, square=True)
    # calculate the eigenvalues and sort them so we can compare (as eigenvalue are given in not necessarily the same order)
    A_eigenvalues = np.sort(np.linalg.eig(A)[0])
    AT_eigenvalues = np.sort(np.linalg.eig(A.T)[0])

    # reshape into a 2 dimensional matrix so we can use assert_matrix_equals to compare
    shape = [A.shape[0], 1]
    assert_matrix_equals(A_eigenvalues.reshape(shape),
                         AT_eigenvalues.reshape(shape))
def test_addition_2():
    A = gen_matrix(5, 5)
    B = random() * A + random()  # make B based on A so that dimensions fit
    add1 = np.add(A, B)
    add2 = np.add(B, A)
    assert_matrix_equals(add1, add2)
    # some test functions return output of the correct test case
    # so we can use it as evidence in our written Report
    return """A =\n{}\nB =\n{}\nA + B =\n{}\nB + A =\n{}\n
Addition is commutative so test passed""".format(A, B, add1, add2)
Esempio n. 5
0
def test_assert_matrix_equals_3(x):
    dims_a = randrange(1, 50), randrange(1, 50)
    dims_b = randrange(50, 100), randrange(50, 100)
    A = np.random.random_sample(dims_a)
    B = np.random.random_sample(dims_b)

    with pytest.raises(ValueError):
        assert_matrix_equals(A, B)
    return "A matrix of shape {} is not equal to matrix of shape {}".format(
        A.shape, B.shape)
def test_pseudo_inverse_4():
    A = gen_matrix(
        5, 5,
        non_square=True)  # force it to be non square for reporting purposes
    B = np.linalg.pinv(A)
    AB = np.dot(A, B)
    assert_matrix_equals(AB, AB.T)
    return """A =\n{}\nA_pseudoinverse =\n{}\nA*A_pinv =\n{}\nTranspose of A*A_pinv =\n{}\n
i.e. A*A_pinv is hermitian (equal to it's own transpose), a required property of
the psuedo inverse, so the test passes""".format(A, B, AB, AB.T)
def test_solve_1():
    A = gen_matrix(5, 5, square=True)
    I = np.identity(A.shape[0])
    X1 = np.linalg.solve(A, I)
    X2 = np.linalg.solve(I, A)
    X2_inv = np.linalg.inv(X2)
    assert_matrix_equals(X1, X2_inv)
    return """A = \n{}\nI = \n{}
Solution of Ax = I:\n{}\nSolution of Ix = A:\n{}\nInverse of solution to Ix = A:\n{}\n
The solution to the first and inverse solution to the second are equal hence test passes""".format(
        A, I, X1, X2, X2_inv)
def test_transpose_3():
    A = np.identity(randrange(1, 100)) * random()
    assert_matrix_equals(A, A.T)
Esempio n. 9
0
def test_assert_matrix_equals_2(x):
    dims = randrange(1, 100), randrange(1, 100)
    A = np.random.random_sample(dims)
    assert_matrix_equals(A, A)
def test_multiplication_4():
    A = gen_matrix(100, 100, non_square=True, non_singular=True)
    A_inv = np.linalg.pinv(A)
    dot = np.dot(A_inv, np.dot(A, A.T))  # note all matrices are non square
    assert_matrix_equals(dot, A.T)
def test_addition_1():
    A = gen_matrix(100, 100)
    O = np.zeros(
        A.shape)  # additive inverse (zero matrix of the same dimensions as A)
    result = np.add(A, -A)
    assert_matrix_equals(result, O)
def test_solve_3():
    A = gen_matrix(100, 100, square=True)
    Ainv = np.linalg.inv(A)
    X = np.linalg.solve(A, Ainv)
    assert_matrix_equals(X, np.dot(Ainv, Ainv))
def test_pseudo_inverse_2():
    A = gen_matrix(100, 100)
    B = np.linalg.pinv(A)
    assert_matrix_equals(A, np.dot(np.dot(A, B), A))
def test_pseudo_inverse_1():
    A = gen_matrix(100, 100, square=True, non_singular=True)
    B = np.linalg.pinv(np.linalg.pinv(A))
    assert_matrix_equals(A, B)
def test_inverse_4():
    A = np.identity(randrange(1, 100))
    B = np.linalg.inv(A)
    assert_matrix_equals(A, B)
def test_solve_2():
    vector = gen_matrix(
        100, 2)  # generates a random vector with size randrange(1, 100), 1
    X = np.linalg.solve(np.identity(vector.shape[0]), vector)
    assert_matrix_equals(X, vector)
def test_pseudo_inverse_3():
    A = gen_matrix(100, 100)
    B = np.linalg.pinv(A)
    assert_matrix_equals(B, np.dot(np.dot(B, A), B))
def test_multiplication_1():
    A = gen_matrix(100, 100, square=True)
    B = np.linalg.inv(A)
    dot = np.dot(A, B)
    assert_matrix_equals(dot, np.identity(A.shape[0]))
def test_pseudo_inverse_5():
    A = gen_matrix(100, 100)
    B = np.linalg.pinv(A)
    BA = np.dot(B, A)
    assert_matrix_equals(BA, BA.T)
def test_multiplication_3():
    A = gen_matrix(100, 100, square=True)
    I = np.identity(A.shape[0])
    dot = np.dot(I, np.dot(A, I))  # IAI should = A
    assert_matrix_equals(dot, A)
def test_transpose_1():
    A = gen_matrix(100, 100)
    assert_matrix_equals(A, A.T.T)
def test_inverse_1():
    A = gen_matrix(
        100, 100, square=True,
        non_singular=True)  # matrix needs to be non_singular to inver
    B = np.linalg.inv(np.linalg.inv(A))
    assert_matrix_equals(A, B)
Esempio n. 23
0
def test_assert_matrix_equals_1(x):
    A = gen_matrix(100, 100)
    B = A - randrange(1, 500)

    with pytest.raises(AssertionError):
        assert_matrix_equals(A, B)