dgl.sparse.sp_broadcast_vο
- dgl.sparse.sp_broadcast_v(A: SparseMatrix, v: Tensor, op: str) SparseMatrix [source]ο
Broadcast operator for sparse matrix and vector.
v
is broadcasted to the shape ofA
and then the operator is applied on the non-zero values ofA
.There are two cases regarding the shape of v:
1.
v
is a vector of shape(1, A.shape[1])
or(A.shape[1])
. In this case,v
is broadcasted on the row dimension ofA
.2.
v
is a vector of shape(A.shape[0], 1)
. In this case,v
is broadcasted on the column dimension ofA
.If
A.val
takes shape(nnz, D)
, thenv
will be broadcasted on theD
dimension.- Parameters:
A (SparseMatrix) β Sparse matrix
v (torch.Tensor) β Vector
op (str) β Operator in [βaddβ, βsubβ, βmulβ, βtruedivβ]
- Returns:
Sparse matrix
- Return type:
Examples
>>> indices = torch.tensor([[1, 0, 2], [0, 3, 2]]) >>> val = torch.tensor([10, 20, 30]) >>> A = dglsp.spmatrix(indices, val, shape=(3, 4)) >>> v = torch.tensor([1, 2, 3, 4]) >>> dglsp.sp_broadcast_v(A, v, "add") SparseMatrix(indices=tensor([[1, 0, 2], [0, 3, 2]]), values=tensor([11, 24, 33]), shape=(3, 4), nnz=3)
>>> v = torch.tensor([1, 2, 3]).view(-1, 1) >>> dglsp.sp_broadcast_v(A, v, "add") SparseMatrix(indices=tensor([[1, 0, 2], [0, 3, 2]]), values=tensor([12, 21, 33]), shape=(3, 4), nnz=3)
>>> indices = torch.tensor([[1, 0, 2], [0, 3, 2]]) >>> val = torch.tensor([[10, 20], [30, 40], [50, 60]]) >>> A = dglsp.spmatrix(indices, val, shape=(3, 4)) >>> v = torch.tensor([1, 2, 3]).view(-1, 1) >>> dglsp.sp_broadcast_v(A, v, "sub") SparseMatrix(indices=tensor([[1, 0, 2], [0, 3, 2]]), values=tensor([[ 8, 18], [29, 39], [47, 57]]), shape=(3, 4), nnz=3, val_size=(2,))