"A DGL Diagonal Matrix can be initiate by `dglsp.diag()`.\n",
"### Initializing a DGL Diagonal Sparse Matrix\n",
"A DGL Diagonal Sparse Matrix can be initiate by `dglsp.diag()`.\n",
"\n",
"Identity Matrix is a special type of Diagonal Matrix, in which all the value on the diagonal are 1.0. Use `dglsp.identity()` to initiate a Diagonal Matrix."
"Identity Matrix is a special type of Diagonal Sparse Matrix, in which all the value on the diagonal are 1.0. Use `dglsp.identity()` to initiate a Diagonal Sparse Matrix."
],
"metadata": {
"id": "1CeCoE2Fgl_x"
...
...
@@ -400,40 +398,15 @@
{
"cell_type": "markdown",
"source": [
"### Attributes and methods of a DGL Diagonal Matrix"
],
"metadata": {
"id": "s-JpSHGLhWlm"
}
},
{
"cell_type": "code",
"source": [
"val = torch.tensor([1., 2., 3., 4.])\n",
"D = dglsp.diag(val)\n",
"\n",
"print(f\"Shape of sparse matrix: {D.shape}\")\n",
"print(f\"The number of nonzero elements of sparse matrix: {D.nnz}\")\n",
"print(f\"Datatype of sparse matrix: {D.dtype}\")\n",
"print(f\"Device sparse matrix is stored on: {D.device}\")\n",
"print(f\"Get the values of the nonzero elements: {D.val}\")"
],
"metadata": {
"id": "QMV0u-kQWsWd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Operations on Sparse Matrix and Diagonal Matrix\n",
"## Operations on Sparse Matrix\n",
"* Elementwise operations\n",
" * `A + B`\n",
" * `A - B`\n",
" * `A * B`\n",
" * `A / B`\n",
" * `A ** scalar`\n",
"* Broadcast operations\n",
" * `sp_<op>_v()`\n",
"* Reduce operations\n",
" * `reduce()`\n",
" * `sum()`\n",
...
...
@@ -443,9 +416,7 @@
"* Matrix transformations\n",
" * `SparseMatrix.transpose()` or `SparseMatrix.T`\n",
" * `SparseMatrix.neg()`\n",
" * `DiagMatrix.transpose()` or `DiagMatrix.T`\n",
" * `DiagMatrix.neg()`\n",
" * `DiagMatrix.inv()`\n",
" * `SparseMatrix.inv()`\n",
"* Matrix multiplication\n",
" * `matmul()`\n",
" * `sddmm()`\n",
...
...
@@ -471,13 +442,7 @@
"source": [
"**add(A, B), equivalent to A + B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **DiagMatrix**|**SparseMatrix**|**scalar**\n",
"Element-wise multiplication on two sparse matrices or on a sparse matrix and a scalar, returning a sparse matrix. If both `A` and `B` are sparse matrices, both of them must have the same sparsity. And the returned matrix has the same order of non-zero entries as `A`."
],
"metadata": {
"id": "Xb2RU6H4UBCs"
...
...
@@ -666,12 +627,20 @@
"i = torch.tensor([[1, 1, 2],\n",
" [0, 2, 0]])\n",
"val = torch.tensor([1., 2., 3.])\n",
"A = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"print(\"A:\")\n",
"print(A.to_dense())\n",
"A1 = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"print(\"A1:\")\n",
"print(A1.to_dense())\n",
"\n",
"print(\"A / 2:\")\n",
"print((A / 2).to_dense())\n",
"i = torch.tensor([[1, 2, 1],\n",
" [0, 0, 2]])\n",
"val = torch.tensor([1., 3., 2.])\n",
"A2 = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"\n",
"print(\"A1 / 2:\")\n",
"print((A1 / 2).to_dense())\n",
"\n",
"print(\"A1 / A2\")\n",
"print((A1 / A2).to_dense())\n",
"\n",
"val = torch.tensor([-1., -2., -3.])\n",
"D1 = dglsp.diag(val)\n",
...
...
@@ -700,13 +669,7 @@
"source": [
"**power(A, B), equivalent to A \\*\\* B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **DiagMatrix**|**SparseMatrix**|**scalar**\n",
"Element-wise power of a sparse matrix and a scalar, returning a sparse matrix."
],
"metadata": {
"id": "2lZbyTYUUgSi"
...
...
@@ -739,6 +702,69 @@
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### *Broadcast operations*"
],
"metadata": {
"id": "VXBz4j5x_wQ4"
}
},
{
"cell_type": "markdown",
"source": [
"**sp_\\<op\\>_v(A, v)**\n",
"\n",
"Broadcast operations on a sparse matrix and a vector, returning a sparse matrix. `v` is broadcasted to the shape of `A` and then the operator is applied on the non-zero values of `A`. `<op>` can be add, sub, mul, and div. \n",
"\n",
"There are two cases regarding the shape of `v`:\n",
"\n",
"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 of `A`.\n",
"\n",
"2. `v` is a vector of shape `(A.shape[0], 1)`. In this case, `v` is broadcasted on the column dimension of `A`."
],
"metadata": {
"id": "PtnyZdXHAZ6Z"
}
},
{
"cell_type": "code",
"source": [
"i = torch.tensor([[1, 0, 2], [0, 3, 2]])\n",
"val = torch.tensor([10, 20, 30])\n",
"A = dglsp.spmatrix(i, val, shape=(3, 4))\n",
"\n",
"v1 = torch.tensor([1, 2, 3, 4])\n",
"print(\"A:\")\n",
"print(A.to_dense())\n",
"\n",
"print(\"v1:\")\n",
"print(v1)\n",
"\n",
"print(\"sp_add_v(A, v1)\")\n",
"print(dglsp.sp_add_v(A, v1).to_dense())\n",
"\n",
"v2 = v1.reshape(1, -1)\n",
"print(\"v2:\")\n",
"print(v2)\n",
"\n",
"print(\"sp_add_v(A, v2)\")\n",
"print(dglsp.sp_add_v(A, v2).to_dense())\n",
"\n",
"v3 = torch.tensor([1, 2, 3]).reshape(-1, 1)\n",
"print(\"v3:\")\n",
"print(v3)\n",
"\n",
"print(\"sp_add_v(A, v3)\")\n",
"print(dglsp.sp_add_v(A, v3).to_dense())"
],
"metadata": {
"id": "xxf3s-uWBRR7"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
...
...
@@ -839,37 +865,6 @@
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"*Diagonal Matrix*"
],
"metadata": {
"id": "iE3ANjFolIJu"
}
},
{
"cell_type": "code",
"source": [
"val = torch.tensor([1., 2., 3., 4.])\n",
"D = dglsp.diag(val)\n",
"print(D.to_dense())\n",
"print(\"\")\n",
"\n",
"print(\"Get inverse of diagonal matrix:\")\n",
"print(D.inv().to_dense())\n",
"print(\"\")\n",
"\n",
"print(\"Get a diagonal matrix with the negation of the original nonzero values.\")\n",
"print(D.neg().to_dense())\n",
"print(\"\")"
],
"metadata": {
"id": "j9kjY9RdlGXx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
...
...
@@ -884,13 +879,7 @@
"source": [
"**matmul(A, B), equivalent to A @ B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **Tensor**|**DiagMatrix**|**SparseMatrix**\n",