matrix_ops.h 1.91 KB
Newer Older
czkkkkkk's avatar
czkkkkkk committed
1
2
3
4
5
6
7
8
9
/**
 *  Copyright (c) 2023 by Contributors
 * @file sparse/matrix_ops.h
 * @brief DGL C++ sparse matrix operators.
 */
#ifndef SPARSE_MATRIX_OPS_H_
#define SPARSE_MATRIX_OPS_H_

#include <sparse/sparse_format.h>
xiangyuzhi's avatar
xiangyuzhi committed
10
#include <sparse/sparse_matrix.h>
czkkkkkk's avatar
czkkkkkk committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <tuple>

namespace dgl {
namespace sparse {

/**
 * @brief Compute the intersection of two COO matrices. Return the intersection
 * matrix, and the indices of the intersection in the left-hand-side and
 * right-hand-side matrices.
 *
 * @param lhs The left-hand-side COO matrix.
 * @param rhs The right-hand-side COO matrix.
 *
 * @return A tuple of COO matrix, lhs indices, and rhs indices.
 */
std::tuple<std::shared_ptr<COO>, torch::Tensor, torch::Tensor> COOIntersection(
    const std::shared_ptr<COO>& lhs, const std::shared_ptr<COO>& rhs);

xiangyuzhi's avatar
xiangyuzhi committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * @brief Compact sparse matrix by removing rows or columns without non-zero
 * elements in the sparse matrix and relabeling indices of the dimension.
 *
 * This function serves a dual purpose: it allows you to reorganize the
 * indices within a specific dimension (rows or columns) of the sparse matrix
 * and, if needed, place certain 'leading_indices' at the beginning of the
 * compact dimension.
 *
 * @param mat The sparse matrix to be compacted.
 * @param dim The dimension to compact. Should be 0 or 1. Use 0 for row-wise
 *        compaction and 1 for column-wise compaction.
 * @param leading_indices An optional tensor containing row or column ids that
 *        should be placed at the beginning of the compact dimension.
 *
 * @return A tuple containing the compacted sparse matrix and the index mapping
 *         of the compact dimension from the new index to the original index.
 */
std::tuple<c10::intrusive_ptr<SparseMatrix>, torch::Tensor> Compact(
    const c10::intrusive_ptr<SparseMatrix>& mat, int64_t dim,
    torch::Tensor leading_indices);

czkkkkkk's avatar
czkkkkkk committed
52
53
54
55
}  // namespace sparse
}  // namespace dgl

#endif  // SPARSE_MATRIX_OPS_H_