Пример #1
0
def test_incorrect_arguments_raise_errors():
    f = get_somefunc()
    F = get_somefunc_from_static_method()

    w = np.random.uniform(-10, 10)
    x = random_math_array([2, 2])
    y = np.random.uniform(-10, 10)
    z = random_math_array([2])

    match = ("There was an error evaluating function {0}\(...\)"
             "\n1st input is ok: received a scalar as expected"
             "\n2nd input has an error: received a matrix of shape \(rows: 2, cols: 2\), "
             "expected a matrix of shape \(rows: 2, cols: 3\)"
             "\n3rd input has an error: received a scalar, expected a vector of length 3"
             "\n4th input is ok: received a vector of length 2 as expected")
    with raises(DomainError, match=match.format('somefunc')):
        f(w, x, y, z)
    with raises(DomainError, match=match.format('somefunc')):
        F(w, x, y, z)

    # Test dispaly name
    g = get_somefunc('puppy')
    G = get_somefunc_from_static_method('puppy')
    with raises(DomainError, match=match.format('puppy')):
        g(w, x, y, z)
    with raises(DomainError, match=match.format('puppy')):
        G(w, x, y, z)
Пример #2
0
def test_matrix_times_matrix_multiplication():
    A = MathArray([
        [5, 2, 3],
        [-4, 3, -1]
    ])
    B = MathArray([
        [0, 4, 5, 2],
        [5, -2, 6, -3],
        [-7, 5, 8, 4]
    ])
    C = MathArray([
        [-11, 31, 61, 16],
        [22, -27, -10, -21]
    ])
    assert equal_as_arrays(A*B, C)

    X = random_math_array([3, 5])
    Y = random_math_array([4, 2])

    match = ("Cannot multiply a matrix of shape \(rows: 3, cols: 5\) with a matrix "
             "of shape \(rows: 4, cols: 2\)")
    with raises(ShapeError, match=match):
        X*Y

    row = MathArray([[1, 2, 3]])
    col = MathArray([[1], [2], [3]])
    assert row*col == 14 and isinstance(row*col, Number)
Пример #3
0
def test_cross():
    cross = ARRAY_ONLY_FUNCTIONS['cross']
    a = MathArray([2, -1, 3.5])
    b = MathArray([1.5, 2.25, -1])
    a_cross_b = MathArray([-6.875, 7.25, 6.])

    assert equal_as_arrays(cross(a, b), a_cross_b)

    vec_3 = random_math_array((3, ))
    vec_4 = random_math_array((4, ))
    match = (
        r"There was an error evaluating function cross\(...\)\n"
        r"1st input is ok: received a vector of length 3 as expected\n"
        r"2nd input has an error: received a vector of length 4, expected "
        r"a vector of length 3")
    with raises(DomainError, match=match):
        cross(vec_3, vec_4)

    match = (
        r"There was an error evaluating function cross\(...\)\n"
        r"1st input has an error: received a vector of length 4, expected "
        r"a vector of length 3\n"
        r"2nd input is ok: received a vector of length 3 as expected")
    with raises(DomainError, match=match):
        cross(vec_4, vec_3)
Пример #4
0
def test_in_place_subtraction():

    amounts = [0, random_math_array([2, 2])]
    for amount in amounts:
        A = random_math_array([2, 2])
        A_copy = A.copy()
        A -= amount
        assert equal_as_arrays(A, A_copy - amount)
Пример #5
0
def test_in_place_multiplication():

    amounts = [5, random_math_array(tuple()), random_math_array([2, 2])]
    for amount in amounts:
        A = random_math_array([2, 2])
        A_copy = A.copy()
        A *= amount
        assert equal_as_arrays(A, A_copy * amount)
Пример #6
0
def test_random_math_array():
    matrix = random_math_array([3, 7])
    assert matrix.shape == (3, 7)
    assert isinstance(matrix, MathArray)

    tensor = random_math_array([3, 7, 5])
    assert tensor.shape == (3, 7, 5)
    assert isinstance(tensor, MathArray)
Пример #7
0
def test_correct_arguments_get_passed_to_function():
    f = get_somefunc()

    w = np.random.uniform(-10, 10)
    x = random_math_array([2, 3])
    y = random_math_array([3])
    z = random_math_array([2])

    assert equal_as_arrays(f(w, x, y, z), w*x*y + z)
Пример #8
0
def test_tensor_multiplication_not_supported():
    A = random_math_array([2, 3, 4])
    B = random_math_array([4, 2])

    match = "Multiplication of tensor arrays is not currently supported."
    with raises(MathArrayError, match=match):
        A*B
    with raises(MathArrayError, match=match):
        B*A
Пример #9
0
def test_array_functions_preserve_type():

    for name in ['re', 'im', 'conj']:
        func = ARRAY_FUNCTIONS[name]
        result = func(random_math_array((3, 3)))
        assert isinstance(result, MathArray)

    for name in ['trans', 'ctrans', 'adj']:
        func = ARRAY_ONLY_FUNCTIONS[name]
        result = func(random_math_array((3, 3)))
        assert isinstance(result, MathArray)
Пример #10
0
def test_vector_times_matrix_multiplication():
    u = MathArray([1, -2, 3])
    A = MathArray([[2, 4], [4, -3], [-1, 0]])
    b = MathArray([-9, 10])
    assert equal_as_arrays(u * A, b)
    assert b.ndim == 1  # result is a vector

    X = random_math_array(5)
    Y = random_math_array([4, 3])

    match = (r"Cannot multiply a vector of length 5 with a matrix of shape "
             r"\(rows: 4, cols: 3\)")
    with raises(ShapeError, match=match):
        X * Y
Пример #11
0
def test_det_and_tr_raise_error_if_not_square():

    det = ARRAY_ONLY_FUNCTIONS['det']
    match = (r"There was an error evaluating function det\(...\)\n"
             r"1st input has an error: received a matrix of shape "
             r"\(rows: 2, cols: 3\), expected a square matrix")
    with raises(DomainError, match=match):
        det(random_math_array((2, 3)))

    tr = ARRAY_ONLY_FUNCTIONS['trace']
    match = (r"There was an error evaluating function trace\(...\)\n"
             r"1st input has an error: received a matrix of shape "
             r"\(rows: 2, cols: 3\), expected a square matrix")
    with raises(DomainError, match=match):
        tr(random_math_array((2, 3)))
Пример #12
0
def test_matrix_times_vector_multiplication():
    u = MathArray([1, -2, 3])
    A = MathArray([[2, 4, -1], [4, -3, 0]])
    b = MathArray([-9, 10])
    assert equal_as_arrays(A * u, b)
    assert b.ndim == 1  # result is a vector

    X = random_math_array([4, 3])
    Y = random_math_array(5)

    match = (
        r"Cannot multiply a matrix of shape \(rows: 4, cols: 3\) with a vector "
        r"of length 5.")
    with raises(ShapeError, match=match):
        X * Y
Пример #13
0
def test_in_place_powers():
    amounts = [2, -3.0, MathArray(5)]
    for amount in amounts:
        A = random_math_array([2, 2])
        A_copy = A.copy()
        A **= amount
        assert equal_as_arrays(A, A_copy ** amount)
Пример #14
0
def test_in_place_division():

    amounts = [5, MathArray(5)]
    for amount in amounts:
        A = random_math_array([2, 2])
        A_copy = A.copy()
        A /= amount
        assert equal_as_arrays(A, A_copy / amount)
Пример #15
0
def test_multiplication_with_unexpected_types_raises_TypeError():
    A = random_math_array([4, 2])
    B = [1, 2, 3]
    match = "Cannot multiply a matrix with object of <type 'list'>"
    with raises(TypeError, match=match):
        A*B

    match = "Cannot multiply object of type <type 'list'> with a matrix."
    with raises(TypeError, match=match):
        B*A
Пример #16
0
def test_multiplication_with_unexpected_types_raises_TypeError():
    A = random_math_array([4, 2])
    B = [1, 2, 3]
    match = "Cannot multiply a matrix with object of {list_type}".format(
        list_type=list)
    with raises(TypeError, match=match):
        A * B

    match = "Cannot multiply object of type {list_type} with a matrix.".format(
        list_type=list)
    with raises(TypeError, match=match):
        B * A
Пример #17
0
def test_power_error_messages():
    A = random_math_array([3, 3])
    B = random_math_array([3, 4])
    u = random_math_array([5])

    # Plausible case with MathArray on the right.
    match = 'Cannot raise a scalar to power of a matrix.'
    with raises(ShapeError, match=match):
        2**A

    # Weird case with MathArray on the right.
    match = "Cannot raise {list_type} to power of matrix.".format(
        list_type=list)
    with raises(TypeError, match=match):
        [1, 2, 3]**A

    # with MathArrays on the left:

    match = 'Cannot raise a vector to powers.'
    with raises(ShapeError, match=match):
        u**2
    with raises(ShapeError, match=match):
        u**A

    match = "Cannot raise a matrix to non-integer powers"
    with raises(MathArrayError, match=match):
        A**2.4

    match = "Cannot raise a non-square matrix to powers."
    with raises(ShapeError, match=match):
        B**2

    match = "Cannot raise a matrix to matrix powers."
    with raises(ShapeError, match=match):
        A**B

    match = "Cannot raise matrix to power of {list_type}.".format(
        list_type=list)
    with raises(TypeError, match=match):
        A**[1, 2, 3]
Пример #18
0
def test_matrix_power_works_with_floats_and_scalars_if_integral():
    A = random_math_array([3, 3])
    assert equal_as_arrays(A**2, A**2.0)
    assert equal_as_arrays(A**2, A**MathArray(2.0))