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