def matmul(a: AbstractMatrix, b: Constant, tr_a=False, tr_b=False): _assert_composable(a, b, tr_a=tr_a, tr_b=tr_b) a = _tr(a, tr_a) b = _tr(b, tr_b) return LowRank( B.expand_dims(B.sum(a, axis=1), axis=1), B.ones(B.dtype(b), b.cols, 1), B.fill_diag(b.const, 1), )
def constant_to_lowrank(a): dtype = B.dtype(a) rows, cols = B.shape(a) middle = B.fill_diag(a.const, 1) if rows == cols: return LowRank(B.ones(dtype, rows, 1), middle=middle) else: return LowRank(B.ones(dtype, rows, 1), B.ones(dtype, cols, 1), middle=middle)
def eye(a: AbstractMatrix): assert_square( a, "Can only construct identity matrices from square matrices.") return B.fill_diag(B.one(a), B.shape(a)[0])
def test_fill_diag(): approx(B.fill_diag(2, 3), np.diag(np.array([2, 2, 2])))