mul.py 2.96 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
from .scatter import scatter
from .utils import gen_output


def scatter_mul_(output, index, input, dim=0):
rusty1s's avatar
rusty1s committed
6
7
8
9
10
11
12
13
14
15
16
17
    r"""
    |

    .. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/
            master/docs/source/_figures/mul.svg?sanitize=true
        :align: center
        :width: 400px

    |

    Multiplies all values from the :attr:`input` tensor into :attr:`output` at
    the indices specified in the :attr:`index` tensor along an given axis
rusty1s's avatar
rusty1s committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    :attr:`dim`. If multiple indices reference the same location, their
    **contributions multiply** (`cf.` :meth:`~torch_scatter.scatter_add_`).

    For one-dimensional tensors, the operation computes

    .. math::
        \mathrm{output}_i = \mathrm{output}_i \cdot \prod_j \mathrm{input}_j

    where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`.

    Args:
        output (Tensor): The destination tensor
        index (LongTensor): The indices of elements to scatter
        input (Tensor): The source tensor
        dim (int, optional): The axis along which to index

    :rtype: :class:`Tensor`

    .. testsetup::

        import torch

    .. testcode::

        from torch_scatter import scatter_mul_
        input =     torch.Tensor([[2, 0, 3, 4, 3], [2, 3, 4, 2, 4]])
        index = torch.LongTensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
        output = torch.ones(2, 6)
        scatter_mul_(output, index, input, dim=1)
        print(output)

    .. testoutput::

        1  1  4  3  6  0
        6  4  8  1  1  1
       [torch.FloatTensor of size 2x6]
    """
rusty1s's avatar
rusty1s committed
55
56
57
    return scatter('mul', dim, output, index, input)


rusty1s's avatar
rename  
rusty1s committed
58
def scatter_mul(index, input, dim=0, size=None, fill_value=1):
rusty1s's avatar
rusty1s committed
59
60
61
62
63
64
65
66
67
68
    r"""Multiplies all values from the :attr:`input` tensor at the indices
    specified in the :attr:`index` tensor along an given axis :attr:`dim`
    (`cf.` :meth:`~torch_scatter.scatter_mul_` and
    :meth:`~torch_scatter.scatter_add`).

    For one-dimensional tensors, the operation computes

    .. math::
        \mathrm{output}_i = \mathrm{fill\_value} \cdot \prod_j \mathrm{input}_j

rusty1s's avatar
rusty1s committed
69
    where prod is over :math:`j` such that :math:`\mathrm{index}_j = i`.
rusty1s's avatar
rusty1s committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    Args:
        index (LongTensor): The indices of elements to scatter
        input (Tensor): The source tensor
        dim (int, optional): The axis along which to index
        size (int, optional): Output size at dimension :attr:`dim`
        fill_value (int, optional): Initial filling of output tensor

    :rtype: :class:`Tensor`

    .. testsetup::

        import torch

    .. testcode::

        from torch_scatter import scatter_mul
        input =     torch.Tensor([[2, 0, 3, 4, 3], [2, 3, 4, 2, 4]])
        index = torch.LongTensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
        output = scatter_mul(index, input, dim=1)
        print(output)

    .. testoutput::

        1  1  4  3  6  0
        6  4  8  1  1  1
       [torch.FloatTensor of size 2x6]
    """
rusty1s's avatar
rename  
rusty1s committed
98
    output = gen_output(index, input, dim, size, fill_value)
rusty1s's avatar
rusty1s committed
99
    return scatter_mul_(output, index, input, dim)