Exemplo n.º 1
0
def test_equality():
    # Test `Dense.`
    a = Dense(np.random.randn(4, 2))
    yield assert_allclose, a == a, dense(a) == dense(a)

    # Test `Diagonal`.
    d = Diagonal(np.random.randn(4))
    yield assert_allclose, d == d, B.diag(d) == B.diag(d)

    # Test `LowRank`.
    lr = LowRank(left=np.random.randn(4, 2), middle=np.random.randn(2, 2))
    yield assert_allclose, lr == lr, (lr.l == lr.l, lr.m == lr.m, lr.r == lr.r)

    # Test `Woodbury`.
    yield assert_allclose, (lr + d) == (lr + d), (lr == lr, d == d)

    # Test `Constant`.
    c1 = Constant.from_(1, a)
    c1_2 = Constant(1, 4, 3)
    c2 = Constant.from_(2, a)
    yield eq, c1, c1
    yield neq, c1, c1_2
    yield neq, c1, c2

    # Test `One`.
    one1 = One(np.float64, 4, 2)
    one2 = One(np.float64, 4, 3)
    yield eq, one1, one1
    yield neq, one1, one2

    # Test `Zero`.
    zero1 = Zero(np.float64, 4, 2)
    zero2 = Zero(np.float64, 4, 3)
    yield eq, zero1, zero1
    yield neq, zero1, zero2
Exemplo n.º 2
0
def test_dtype():
    # Test `Dense`.
    yield eq, B.dtype(Dense(np.array([[1]]))), int
    yield eq, B.dtype(Dense(np.array([[1.0]]))), float

    # Test `Diagonal`.
    diag_int = Diagonal(np.array([1]))
    diag_float = Diagonal(np.array([1.0]))
    yield eq, B.dtype(diag_int), int
    yield eq, B.dtype(diag_float), float

    # Test `LowRank`.
    lr_int = LowRank(left=np.array([[1]]),
                     right=np.array([[2]]),
                     middle=np.array([[3]]))
    lr_float = LowRank(left=np.array([[1.0]]),
                       right=np.array([[2.0]]),
                       middle=np.array([[3.0]]))
    yield eq, B.dtype(lr_int), int
    yield eq, B.dtype(lr_float), float

    # Test `Constant`.
    yield eq, B.dtype(Constant(1, rows=1)), int
    yield eq, B.dtype(Constant(1.0, rows=1)), float

    # Test `Woodbury`.
    yield eq, B.dtype(Woodbury(diag_int, lr_int)), int
    yield eq, B.dtype(Woodbury(diag_float, lr_float)), float
Exemplo n.º 3
0
def test_equality():
    # Test `Dense.`
    a = Dense(np.random.randn(4, 2))
    allclose(a == a, to_np(a) == to_np(a))

    # Test `Diagonal`.
    d = Diagonal(np.random.randn(4))
    allclose(d == d, B.diag(d) == B.diag(d))

    # Test `LowRank`.
    lr = LowRank(left=np.random.randn(4, 2), middle=np.random.randn(2, 2))
    allclose(lr == lr, (lr.l == lr.l, lr.m == lr.m, lr.r == lr.r))

    # Test `Woodbury`.
    allclose((lr + d) == (lr + d), (lr == lr, d == d))

    # Test `Constant`.
    c1 = Constant.from_(1, a)
    c1_2 = Constant(1, 4, 3)
    c2 = Constant.from_(2, a)
    assert c1 == c1
    assert c1 != c1_2
    assert c1 != c2

    # Test `One`.
    one1 = One(np.float64, 4, 2)
    one2 = One(np.float64, 4, 3)
    assert one1 == one1
    assert one1 != one2

    # Test `Zero`.
    zero1 = Zero(np.float64, 4, 2)
    zero2 = Zero(np.float64, 4, 3)
    assert zero1 == zero1
    assert zero1 != zero2
Exemplo n.º 4
0
def test_dtype():
    # Test `Dense`.
    assert B.dtype(Dense(np.array([[1]]))) == np.int64
    assert B.dtype(Dense(np.array([[1.0]]))) == np.float64

    # Test `Diagonal`.
    diag_int = Diagonal(np.array([1]))
    diag_float = Diagonal(np.array([1.0]))
    assert B.dtype(diag_int) == np.int64
    assert B.dtype(diag_float) == np.float64

    # Test `LowRank`.
    lr_int = LowRank(left=np.array([[1]]),
                     right=np.array([[2]]),
                     middle=np.array([[3]]))
    lr_float = LowRank(left=np.array([[1.0]]),
                       right=np.array([[2.0]]),
                       middle=np.array([[3.0]]))
    assert B.dtype(lr_int) == np.int64
    assert B.dtype(lr_float) == np.float64

    # Test `Constant`.
    assert B.dtype(Constant(1, rows=1)) == int
    assert B.dtype(Constant(1.0, rows=1)) == float

    # Test `Woodbury`.
    assert B.dtype(Woodbury(diag_int, lr_int)) == np.int64
    assert B.dtype(Woodbury(diag_float, lr_float)) == np.float64
Exemplo n.º 5
0
def test_constant_zero_one():
    a = np.random.randn(4, 2)
    b = Dense(a)

    yield assert_allclose, Zero.from_(a), np.zeros((4, 2))
    yield assert_allclose, Zero.from_(b), np.zeros((4, 2))

    yield assert_allclose, One.from_(a), np.ones((4, 2))
    yield assert_allclose, One.from_(b), np.ones((4, 2))

    yield assert_allclose, Constant.from_(2, a), 2 * np.ones((4, 2))
    yield assert_allclose, Constant.from_(2, b), 2 * np.ones((4, 2))
Exemplo n.º 6
0
def test_constant_zero_one():
    a = np.random.randn(4, 2)
    b = Dense(a)

    allclose(Zero.from_(a), np.zeros((4, 2)))
    allclose(Zero.from_(b), np.zeros((4, 2)))

    allclose(One.from_(a), np.ones((4, 2)))
    allclose(One.from_(b), np.ones((4, 2)))

    allclose(Constant.from_(2, a), 2 * np.ones((4, 2)))
    allclose(Constant.from_(2, b), 2 * np.ones((4, 2)))
Exemplo n.º 7
0
def test_arithmetic_and_shapes():
    a = Dense(np.random.randn(4, 3))
    d = Diagonal(np.array([1.0, 2.0, 3.0]), rows=4, cols=3)
    lr = LowRank(left=np.random.randn(4, 2),
                 right=np.random.randn(3, 2),
                 middle=np.random.randn(2, 2))
    zero = Zero.from_(a)
    one = One.from_(a)
    constant = Constant.from_(2.0, a)
    wb = d + lr

    # Aggregate all matrices.
    candidates = [a, d, lr, wb, constant, one, zero, 2, 1, 0]

    # Check division.
    yield assert_allclose, a.__div__(5.0), dense(a) / 5.0
    yield assert_allclose, a.__truediv__(5.0), dense(a) / 5.0

    # Check shapes.
    for m in candidates:
        yield eq, B.shape(a), (4, 3)

    # Check interactions.
    for m1, m2 in product(candidates, candidates):
        yield assert_allclose, m1 * m2, dense(m1) * dense(m2)
        yield assert_allclose, m1 + m2, dense(m1) + dense(m2)
        yield assert_allclose, m1 - m2, dense(m1) - dense(m2)
Exemplo n.º 8
0
def test_diag():
    # Test `Dense`.
    a = np.random.randn(5, 3)
    yield assert_allclose, B.diag(Dense(a)), np.diag(a)

    # Test `Diagonal`.
    yield assert_allclose, B.diag(Diagonal([1, 2, 3])), [1, 2, 3]
    yield assert_allclose, B.diag(Diagonal([1, 2, 3], 2)), [1, 2]
    yield assert_allclose, B.diag(Diagonal([1, 2, 3], 4)), [1, 2, 3, 0]

    # Test `LowRank`.
    b = np.random.randn(10, 3)
    yield assert_allclose, B.diag(LowRank(left=a,
                                          right=a)), np.diag(a.dot(a.T))
    yield assert_allclose, B.diag(LowRank(left=a,
                                          right=b)), np.diag(a.dot(b.T))
    yield assert_allclose, B.diag(LowRank(left=b,
                                          right=b)), np.diag(b.dot(b.T))

    # Test `Constant`.
    yield assert_allclose, B.diag(Constant(1, rows=3, cols=5)), np.ones(3)

    # Test `Woodbury`.
    yield assert_allclose, \
          B.diag(Woodbury(Diagonal([1, 2, 3], rows=5, cols=10),
                          LowRank(left=a, right=b))), \
          np.diag(a.dot(b.T) + np.concatenate((np.diag([1, 2, 3, 0, 0]),
                                               np.zeros((5, 5))), axis=1))
Exemplo n.º 9
0
def test_arithmetic_and_shapes():
    a = Dense(np.random.randn(4, 3))
    d = Diagonal(np.array([1.0, 2.0, 3.0]), rows=4, cols=3)
    lr = LowRank(left=np.random.randn(4, 2),
                 right=np.random.randn(3, 2),
                 middle=np.random.randn(2, 2))
    zero = Zero.from_(a)
    one = One.from_(a)
    constant = Constant.from_(2.0, a)
    wb = d + lr

    # Aggregate all matrices.
    candidates = [a, d, lr, wb, constant, one, zero, 2, 1, 0, 2.0, 1.0, 0.0]

    # Check division.
    allclose(a.__div__(5.0), to_np(a) / 5.0)
    allclose(a.__rdiv__(5.0), 5.0 / to_np(a))
    allclose(a.__truediv__(5.0), to_np(a) / 5.0)
    allclose(a.__rtruediv__(5.0), 5.0 / to_np(a))
    allclose(B.divide(a, 5.0), to_np(a) / 5.0)
    allclose(B.divide(a, a), B.ones(to_np(a)))

    # Check shapes.
    for m in candidates:
        assert B.shape(a) == (4, 3)

    # Check interactions.
    for m1, m2 in product(candidates, candidates):
        allclose(m1 * m2, to_np(m1) * to_np(m2))
        allclose(m1 + m2, to_np(m1) + to_np(m2))
        allclose(m1 - m2, to_np(m1) - to_np(m2))
Exemplo n.º 10
0
def test_transposition():
    def compare(a):
        assert_allclose(B.transpose(a), dense(a).T)

    d = Diagonal([1, 2, 3], rows=5, cols=10)
    lr = LowRank(left=np.random.randn(5, 3), right=np.random.randn(10, 3))

    yield compare, Dense(np.random.randn(5, 5))
    yield compare, d
    yield compare, lr
    yield compare, d + lr
    yield compare, Constant(5, rows=4, cols=2)
Exemplo n.º 11
0
def test_transposition():
    def compare(a):
        allclose(B.transpose(a), to_np(a).T)

    d = Diagonal(np.array([1, 2, 3]), rows=5, cols=10)
    lr = LowRank(left=np.random.randn(5, 3), right=np.random.randn(10, 3))

    compare(Dense(np.random.randn(5, 5)))
    compare(d)
    compare(lr)
    compare(d + lr)
    compare(Constant(5, rows=4, cols=2))