{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "# MatRepr PyData/Sparse\n", "\n", "Demonstrate:\n", "* 2D matrix\n", "* 1D vector\n", "* multidimensional\n", "\n", "With rendering:\n", "* native pydata/sparse\n", "* with MatRepr Jupyter integration, both default and a more compact size\n", "* MatRepr LaTeX output\n", "* MatRepr string output" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:06.015615Z", "start_time": "2023-09-08T18:46:05.587384Z" } }, "outputs": [], "source": [ "import sparse\n", "\n", "# so matrepr can be imported from the source tree.\n", "import sys\n", "sys.path.insert(0, '..')\n", "\n", "from matrepr import mdisplay" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:06.529548Z", "start_time": "2023-09-08T18:46:06.032629Z" } }, "outputs": [], "source": [ "import numpy\n", "numpy.random.seed(1234)\n", "\n", "A = sparse.random((100, 100), density=0.21234)\n", "# v = sparse.COO([0, 1, 2, 3, 99], data=[1000, 1001, 1002, 1003, 1099], shape=(100,))\n", "v = sparse.random((1000,), density=0.5)\n", "# T = sparse.COO(coords=[[0, 1], [3, 2], [1, 3]], data=[111, 222], shape=(5, 5, 5))\n", "T = sparse.random((1000, 100, 100), density=0.21234)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PyData/Sparse native formatting" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:06.534313Z", "start_time": "2023-09-08T18:46:06.533505Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
Formatcoo
Data Typefloat64
Shape(100, 100)
nnz2123
Density0.2123
Read-onlyTrue
Size49.8K
Storage ratio0.6
" }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-09-08T18:46:06.538771Z", "start_time": "2023-09-08T18:46:06.535786Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
Formatcoo
Data Typefloat64
Shape(1000,)
nnz500
Density0.5
Read-onlyTrue
Size7.8K
Storage ratio1.0
" }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:06.540766Z", "start_time": "2023-09-08T18:46:06.538120Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
Formatcoo
Data Typefloat64
Shape(1000, 100, 100)
nnz2123400
Density0.21234
Read-onlyTrue
Size64.8M
Storage ratio0.8
" }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MatRepr default" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:06.543428Z", "start_time": "2023-09-08T18:46:06.541726Z" } }, "outputs": [], "source": [ "%load_ext matrepr" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-09-08T18:46:07.460671Z", "start_time": "2023-09-08T18:46:06.544141Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
100×100, 2123 'float64' elements, coo, fill_value=0.0, read-only, 49.8KiB
012345693949596979899
00.49770.4427
10.81350.6466
20.047430.9950.03235
30.32820.011630.07154
40.1318
950.33150.77080.0044160.9238
960.94550.59610.2370.6584
97
980.9720.4620.37260.3856
990.82630.01302
\n
" }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-09-08T18:46:07.464136Z", "start_time": "2023-09-08T18:46:07.460851Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
length=1000, 500 'float64' elements, coo, fill_value=0.0, read-only, 7.8KiB
0123456993994995996997998999
0.98730.94950.75220.98180.23770.9387
\n
" }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.467286Z", "start_time": "2023-09-08T18:46:07.463926Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
shape=(1000,100,100), 2123400 'float64' elements, coo, fill_value=0.0, read-only, 64.8MiB
012val
00040.8811
100100.2228
200110.5559
300210.5681
400250.5452
212339599999770.2487
212339699999820.7877
212339799999840.4994
212339899999880.3997
212339999999930.7297
\n
" }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## LaTeX" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.472490Z", "start_time": "2023-09-08T18:46:07.469101Z" } }, "outputs": [ { "data": { "text/plain": "", "text/latex": "$\\left[\\begin{array}{ccccccccccccccc}\n & 0.4977 & & & & & & \\dots & 0.4427 & & & & & & \\\\\n & & & & & & & \\dots & & 0.8135 & & & & & 0.6466 \\\\\n & 0.04743 & & & 0.995 & & & \\dots & & & & & & 0.03235 & \\\\\n & & & & & 0.3282 & & \\dots & 0.01163 & & & & 0.07154 & & \\\\\n & & & & & & & \\dots & & & & & & & 0.1318 \\\\\n \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\ddots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots \\\\\n & & & & & 0.3315 & & \\dots & & 0.7708 & 0.004416 & 0.9238 & & & \\\\\n & 0.9455 & & & & & 0.5961 & \\dots & 0.237 & & 0.6584 & & & & \\\\\n & & & & & & & \\dots & & & & & & & \\\\\n & & & 0.972 & & 0.462 & 0.3726 & \\dots & & & & & & & 0.3856 \\\\\n & & & & 0.8263 & & & \\dots & & & & 0.01302 & & & \n\\end{array}\\right]$" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(A, \"latex\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-09-08T18:46:07.475667Z", "start_time": "2023-09-08T18:46:07.472916Z" } }, "outputs": [ { "data": { "text/plain": "", "text/latex": "$\\left[\\begin{array}{ccccccccccccccc}\n & 0.9873 & & & & 0.9495 & 0.7522 & \\dots & & & & 0.9818 & 0.2377 & & 0.9387\n\\end{array}\\right]$" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(v, \"latex\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.479544Z", "start_time": "2023-09-08T18:46:07.476297Z" } }, "outputs": [ { "data": { "text/plain": "", "text/latex": "$\\begin{array}{rrrc}\n 0 & 0 & 4 & 0.8811 \\\\\n 0 & 0 & 10 & 0.2228 \\\\\n 0 & 0 & 11 & 0.5559 \\\\\n 0 & 0 & 21 & 0.5681 \\\\\n 0 & 0 & 25 & 0.5452 \\\\\n \\vdots & \\vdots & \\vdots & \\vdots \\\\\n 999 & 99 & 77 & 0.2487 \\\\\n 999 & 99 & 82 & 0.7877 \\\\\n 999 & 99 & 84 & 0.4994 \\\\\n 999 & 99 & 88 & 0.3997 \\\\\n 999 & 99 & 93 & 0.7297\n\\end{array}$" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(T, \"latex\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "## String" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.509354Z", "start_time": "2023-09-08T18:46:07.478743Z" } }, "outputs": [ { "data": { "text/plain": "<100×100, 2123 'float64' elements, coo, fill_value=0.0, read-only, 49.8KiB>\n 0 1 2 3 4 96 97 98 99\n ┌ ┐\n 0 │ 0.4977 ... │\n 1 │ ... 0.6466 │\n 2 │ 0.04743 ... 0.03235 │\n 3 │ ... 0.07154 │\n 4 │ ... 0.1318 │\n │ : : : : : ... : : : : │\n95 │ ... 0.9238 │\n96 │ 0.9455 0.9514 ... │\n97 │ ... │\n98 │ 0.972 0.3444 ... 0.3856 │\n99 │ ... 0.01302 │\n └ ┘" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(A, \"str\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.512901Z", "start_time": "2023-09-08T18:46:07.508942Z" } }, "outputs": [ { "data": { "text/plain": "\n 0 1 2 3 4 5 995 996 997 998 999\n[ 0.9873 ... 0.9818 0.2377 0.9387]" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(v, \"str\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.514738Z", "start_time": "2023-09-08T18:46:07.510095Z" } }, "outputs": [ { "data": { "text/plain": "[, 0.9873, , , , 0.9495, 0.7522, , ..., 0.1255, , , , 0.9818, 0.2377, , 0.9387]" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(v, \"str\", indices=False, title=False)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.518574Z", "start_time": "2023-09-08T18:46:07.515274Z" } }, "outputs": [ { "data": { "text/plain": "\n 0 1 2 val\n 0 ( 0, 0, 4, 0.8811)\n 1 ( 0, 0, 10, 0.2228)\n 2 ( 0, 0, 11, 0.5559)\n 3 ( 0, 0, 21, 0.5681)\n 4 ( 0, 0, 25, 0.5452)\n ( :, :, :, : )\n2123395 (999, 99, 77, 0.2487)\n2123396 (999, 99, 82, 0.7877)\n2123397 (999, 99, 84, 0.4994)\n2123398 (999, 99, 88, 0.3997)\n2123399 (999, 99, 93, 0.7297)" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(T, \"str\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More compact size" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.520782Z", "start_time": "2023-09-08T18:46:07.519054Z" } }, "outputs": [], "source": [ "import matrepr\n", "matrepr.params.max_rows = 10\n", "matrepr.params.max_cols = 7\n", "matrepr.params.num_after_dots = 0\n", "matrepr.params.indices = False" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.562855Z", "start_time": "2023-09-08T18:46:07.522473Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
100×100, 2123 'float64' elements, coo, fill_value=0.0, read-only, 49.8KiB
0.4977
0.047430.995
0.3282
0.4119
0.93750.61340.6993
0.6578
0.05845
\n
" }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.568140Z", "start_time": "2023-09-08T18:46:07.525421Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n
length=1000, 500 'float64' elements, coo, fill_value=0.0, read-only, 7.8KiB
0.98730.9495
\n
" }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.568335Z", "start_time": "2023-09-08T18:46:07.529931Z" } }, "outputs": [ { "data": { "text/plain": "", "text/html": "
\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
shape=(1000,100,100), 2123400 'float64' elements, coo, fill_value=0.0, read-only, 64.8MiB
0040.8811
00100.2228
00110.5559
00210.5681
00250.5452
00370.4053
00380.8414
00460.638
00480.9332
\n
" }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-08T18:46:07.568464Z", "start_time": "2023-09-08T18:46:07.533260Z" } }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 4 }