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
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
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))
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)))
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)
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))