transpose.py 1003 Bytes
Newer Older
rusty1s's avatar
rusty1s committed
1
import torch
rusty1s's avatar
rusty1s committed
2
from torch_sparse import to_scipy, from_scipy, coalesce
rusty1s's avatar
rusty1s committed
3
4


5
def transpose(index, value, m, nm coalesce=True):
rusty1s's avatar
rusty1s committed
6
    """Transposes dimensions 0 and 1 of a sparse tensor.
rusty1s's avatar
docs  
rusty1s committed
7
8
9
10

    Args:
        index (:class:`LongTensor`): The index tensor of sparse matrix.
        value (:class:`Tensor`): The value tensor of sparse matrix.
ekagra-ranjan's avatar
ekagra-ranjan committed
11
12
        m (int): The first dimension of corresponding dense matrix.
        n (int): The second dimension of corresponding dense matrix.
13
        coalesce (bool, optional): To return coalesced index and value or not (default: :obj:`True`) 
rusty1s's avatar
docs  
rusty1s committed
14
15
    :rtype: (:class:`LongTensor`, :class:`Tensor`)
    """
rusty1s's avatar
rusty1s committed
16

17
    if value.dim() == 1 and not value.is_cuda:
rusty1s's avatar
rusty1s committed
18
19
20
21
        mat = to_scipy(index, value, m, n).tocsc()
        (col, row), value = from_scipy(mat)
        index = torch.stack([row, col], dim=0)
        return index, value
22
23
24

    row, col = index
    index = torch.stack([col, row], dim=0)
25
26
    if coalesce:
        index, value = coalesce(index, value, n, m)
27
    return index, value