diff --git a/README.md b/README.md index a1c8128..6612ca6 100644 --- a/README.md +++ b/README.md @@ -10,21 +10,25 @@ pip install matrepr Sparse matrix string, HTML, and LaTeX representation with Jupyter integration. -Supports: - * **SciPy** - sparse matrices and arrays like `csr_matrix` and `coo_array` * **[Python-graphblas](https://github.com/python-graphblas/python-graphblas)** - `gb.Matrix` and `gb.Vector` [(demo)](doc/demo-python-graphblas.ipynb) * **[PyData/Sparse](https://sparse.pydata.org/)** - `COO`, `DOK`, `GCXS` [(demo)](doc/demo-pydata-sparse.ipynb) * **NumPy** - `ndarray` * `list`, `tuple`, including multi-dimensional and jagged -Use MatRepr to turn this opaque string: -``` -<1000x1000 sparse matrix of type '' - with 212345 stored elements in COOrdinate format> -``` +Features: +* Jupyter extension to format matrices in cell outputs. +* A `__repr__` monkey patch to format matrices in the Python shell. +* Nested sub-matrices of any supported type, including mixing packages. +* Configurable float precision or format string. +* Row and column labels are both toggleable and customizable. +* String output can optionally detect terminal width. +* Methods to directly display a matrix (`mprint`, `mdisplay` for Jupyter) +* Methods to convert to string (`to_html`, `to_latex`, `to_str`). +* Configurable globally and/or per method call. +* Fast. -To one of these: +See [Jupyter notebook with examples.](doc/demo.ipynb) ### String @@ -81,20 +85,22 @@ Methods: ## Jupyter Extension -MatRepr can integrate with [Jupyter's formatter](https://ipython.readthedocs.io/en/stable/config/integrating.html) -to format SciPy, GraphBLAS, and PyData/Sparse with MatRepr. Simply: +MatRepr's Jupyter extension registers with [Jupyter's formatter](https://ipython.readthedocs.io/en/stable/config/integrating.html) +to format supported matrices with MatRepr. Simply: ```jupyter %load_ext matrepr ``` -Jupyter extension screenshot - -If you prefer LaTeX: +Or if you prefer LaTeX: ```jupyter %load_ext matrepr.latex ``` +Example: + +Jupyter extension screenshot + ## Interactive Python: Monkey Patching `__repr__` The interactive Python REPL does not have a nice way to register a formatter. diff --git a/doc/demo-edgecases.ipynb b/doc/demo-edgecases.ipynb index 8947e47..2df3b6d 100644 --- a/doc/demo-edgecases.ipynb +++ b/doc/demo-edgecases.ipynb @@ -16,13 +16,13 @@ "cell_type": "code", "execution_count": 1, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.537004Z", "start_time": "2023-08-28T20:00:35.528039Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [], @@ -55,13 +55,13 @@ "cell_type": "code", "execution_count": 2, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.742269Z", "start_time": "2023-08-28T20:00:35.538362Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [], @@ -78,20 +78,180 @@ "cell_type": "code", "execution_count": 3, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.750824Z", "start_time": "2023-08-28T20:00:35.745049Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [ { "data": { - "text/plain": "<5x5 sparse array of type ''\n\twith 9 stored elements in COOrdinate format>", - "text/html": "
\n\n

5×5, 9 'float64' elements, coo

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
01234
011.2e+35
11e-06
22.1
2.2
33.1
3.2
3.3
40
\n
" + "text/html": [ + "
\n", + "\n", + "

5×5, 9 'float64' elements, coo

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
011.2e+35
11e-06
22.1
2.2
33.1
3.2
3.3
40
\n", + "
" + ], + "text/plain": [ + "<5x5 sparse array of type ''\n", + "\twith 9 stored elements in COOrdinate format>" + ] }, "execution_count": 3, "metadata": {}, @@ -106,20 +266,37 @@ "cell_type": "code", "execution_count": 4, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.758651Z", "start_time": "2023-08-28T20:00:35.752606Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [ { "data": { - "text/plain": "", - "text/latex": "$\\begin{bmatrix}\n 1 & 1.2 \\times 10^{35} & & & \\\\\n 1 \\times 10^{-6} & & & & \\\\\n & & \\begin{Bmatrix}\n 2.1 \\\\\n 2.2\n\\end{Bmatrix} & & \\\\\n & & & \\begin{Bmatrix}\n 3.1 \\\\\n 3.2 \\\\\n 3.3\n\\end{Bmatrix} & \\\\\n & & & & 0\n\\end{bmatrix}$" + "text/latex": [ + "$\\begin{bmatrix}\n", + " 1 & 1.2 \\times 10^{35} & & & \\\\\n", + " 1 \\times 10^{-6} & & & & \\\\\n", + " & & \\begin{Bmatrix}\n", + " 2.1 \\\\\n", + " 2.2\n", + "\\end{Bmatrix} & & \\\\\n", + " & & & \\begin{Bmatrix}\n", + " 3.1 \\\\\n", + " 3.2 \\\\\n", + " 3.3\n", + "\\end{Bmatrix} & \\\\\n", + " & & & & 0\n", + "\\end{bmatrix}$" + ], + "text/plain": [ + "" + ] }, "metadata": {}, "output_type": "display_data" @@ -133,19 +310,29 @@ "cell_type": "code", "execution_count": 5, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.776298Z", "start_time": "2023-08-28T20:00:35.763039Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [ { "data": { - "text/plain": "5×5, 9 'float64' elements, coo\n 0 1 2 3 4\n ┌ ┐\n 0 │ 1 1.2e+35 │\n 1 │ 1e-06 │\n 2 │ [2.1, 2.2] │\n 3 │ [3.1, 3.2, 3.3] │\n 4 │ 0 │\n └ ┘" + "text/plain": [ + "5×5, 9 'float64' elements, coo\n", + " 0 1 2 3 4\n", + " ┌ ┐\n", + " 0 │ 1 1.2e+35 │\n", + " 1 │ 1e-06 │\n", + " 2 │ [2.1, 2.2] │\n", + " 3 │ [3.1, 3.2, 3.3] │\n", + " 4 │ 0 │\n", + " └ ┘" + ] }, "metadata": {}, "output_type": "display_data" @@ -180,13 +367,13 @@ "cell_type": "code", "execution_count": 6, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.852562Z", "start_time": "2023-08-28T20:00:35.845394Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [], @@ -215,20 +402,247 @@ "cell_type": "code", "execution_count": 7, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.861964Z", "start_time": "2023-08-28T20:00:35.854241Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [ { "data": { - "text/plain": "", - "text/html": "
\n\n

6×5, 18 elements

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
onetwothreefourfive
sci01.2e+351e-061.235e+08
complex1+2i1.235e05 + 0.1235i
single1
nested\n \n \n \n \n \n \n
1
\n \n \n \n \n \n \n \n \n \n \n \n
12
34
\n \n \n \n \n \n \n \n
2.12.2
\n \n \n \n \n \n \n \n \n \n \n \n
1.12.2
3.34.4
stringsmultiline
string
<escape!>\\begin{escape!}{'a Python set'}
numpy(False, 2)\n \n \n \n \n \n \n \n \n
123
\n \n \n \n \n \n \n \n \n \n \n \n
1122
3344
\n
" + "text/html": [ + "
\n", + "\n", + "

6×5, 18 elements

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
onetwothreefourfive
sci01.2e+351e-061.235e+08
complex1+2i1.235e05 + 0.1235i
single1
nested\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
1
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
12
34
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
2.12.2
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
1.12.2
3.34.4
stringsmultiline
string
<escape!>\\begin{escape!}{'a Python set'}
numpy(False, 2)\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
123
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
1122
3344
\n", + "
" + ], + "text/plain": [ + "" + ] }, "metadata": {}, "output_type": "display_data" @@ -242,20 +656,49 @@ "cell_type": "code", "execution_count": 8, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.869589Z", "start_time": "2023-08-28T20:00:35.862986Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [ { "data": { - "text/plain": "", - "text/latex": "$\\begin{bmatrix}\n 0 & 1.2 \\times 10^{35} & 1 \\times 10^{-6} & & 1.235 \\times 10^{8} \\\\\n 1+2i & 1.235 \\times 10^{5}+0.1235i & & & \\\\\n 1 & & & & \\\\\n \\begin{bmatrix}\n 1\n\\end{bmatrix} & \\begin{bmatrix}\n \\textrm{1} & \\textrm{2} \\\\\n \\textrm{3} & \\textrm{4}\n\\end{bmatrix} & \\begin{bmatrix}\n 2.1 & 2.2\n\\end{bmatrix} & \\begin{bmatrix}\n 1.1 & 2.2 \\\\\n 3.3 & 4.4\n\\end{bmatrix} & \\\\\n \\begin{matrix}\n \\textrm{multiline} \\\\\n \\textrm{string}\n\\end{matrix} & \\textrm{} & \\textrm{\\\\begin\\{escape!\\}} & \\textrm{\\{'a Python set'\\}} & \\\\\n \\textrm{(False, 2)} & \\begin{bmatrix}\n \\textrm{1} & \\textrm{2} & \\textrm{3}\n\\end{bmatrix} & \\begin{bmatrix}\n \\textrm{11} & \\textrm{22} \\\\\n \\textrm{33} & \\textrm{44}\n\\end{bmatrix} & &\n\\end{bmatrix}$" + "text/latex": [ + "$\\begin{bmatrix}\n", + " 0 & 1.2 \\times 10^{35} & 1 \\times 10^{-6} & & 1.235 \\times 10^{8} \\\\\n", + " 1+2i & 1.235 \\times 10^{5}+0.1235i & & & \\\\\n", + " 1 & & & & \\\\\n", + " \\begin{bmatrix}\n", + " 1\n", + "\\end{bmatrix} & \\begin{bmatrix}\n", + " \\textrm{1} & \\textrm{2} \\\\\n", + " \\textrm{3} & \\textrm{4}\n", + "\\end{bmatrix} & \\begin{bmatrix}\n", + " 2.1 & 2.2\n", + "\\end{bmatrix} & \\begin{bmatrix}\n", + " 1.1 & 2.2 \\\\\n", + " 3.3 & 4.4\n", + "\\end{bmatrix} & \\\\\n", + " \\begin{matrix}\n", + " \\textrm{multiline} \\\\\n", + " \\textrm{string}\n", + "\\end{matrix} & \\textrm{} & \\textrm{\\\\begin\\{escape!\\}} & \\textrm{\\{'a Python set'\\}} & \\\\\n", + " \\textrm{(False, 2)} & \\begin{bmatrix}\n", + " \\textrm{1} & \\textrm{2} & \\textrm{3}\n", + "\\end{bmatrix} & \\begin{bmatrix}\n", + " \\textrm{11} & \\textrm{22} \\\\\n", + " \\textrm{33} & \\textrm{44}\n", + "\\end{bmatrix} & &\n", + "\\end{bmatrix}$" + ], + "text/plain": [ + "" + ] }, "metadata": {}, "output_type": "display_data" @@ -269,19 +712,30 @@ "cell_type": "code", "execution_count": 9, "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - }, "ExecuteTime": { "end_time": "2023-08-28T20:00:35.881033Z", "start_time": "2023-08-28T20:00:35.871540Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false } }, "outputs": [ { "data": { - "text/plain": "6×5, 18 elements\n one two three four five\n ┌ ┐\n sci │ 0 1.2e+35 1e-06 1.235e+08 │\n complex │ 1+2i 1.235e05 + 0.1235i │\n single │ 1 │\n nested │ [1] [[1, 2], [3, 4]] [2.1, 2.2] [[1.1, 2.2], [3.3, 4.4]] │\n strings │ 'multiline\\nstring' '' '\\\\begin{escape!}' {'a Python set'} │\n numpy │ (False, 2) [1 2 3] [[11 22] [33 44]] │\n └ ┘" + "text/plain": [ + "6×5, 18 elements\n", + " one two three four five\n", + " ┌ ┐\n", + " sci │ 0 1.2e+35 1e-06 1.235e+08 │\n", + " complex │ 1+2i 1.235e05 + 0.1235i │\n", + " single │ 1 │\n", + " nested │ [1] [[1, 2], [3, 4]] [2.1, 2.2] [[1.1, 2.2], [3.3, 4.4]] │\n", + " strings │ 'multiline\\nstring' '' '\\\\begin{escape!}' {'a Python set'} │\n", + " numpy │ (False, 2) [1 2 3] [[11 22] [33 44]] │\n", + " └ ┘" + ] }, "metadata": {}, "output_type": "display_data" diff --git a/doc/demo.ipynb b/doc/demo.ipynb new file mode 100644 index 0000000..5e35725 --- /dev/null +++ b/doc/demo.ipynb @@ -0,0 +1,1494 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "# MatRepr\n", + "\n", + "Render sparse and dense matrices to HTML and Latex, with a Jupyter extension." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:41.955312Z", + "start_time": "2023-08-28T23:01:41.784979Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "import scipy.sparse\n", + "import numpy as np\n", + "np.random.seed(123)\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": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "### Load MatRepr Jupyter extension" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:41.962970Z", + "start_time": "2023-08-28T23:01:41.960020Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "%load_ext matrepr" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "### SciPy sparse matrix" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:41.971947Z", + "start_time": "2023-08-28T23:01:41.964509Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "

5×5, 12 'float64' elements, coo

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234
00.53160.7245
10.3230.5318
20.72240.6310.6344
30.22830.6110.8494
40.36180.2937
\n", + "
" + ], + "text/plain": [ + "<5x5 sparse matrix of type ''\n", + "\twith 12 stored elements in COOrdinate format>" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scipy.sparse.random(5, 5, density=0.5)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "### 2D NumPy array with row and column labels" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:41.993321Z", + "start_time": "2023-08-28T23:01:41.973073Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "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", + "
BostonBuffaloChicagoClevelandDallasDenver
Boston45798363918151991
Buffalo45753619213871561
Chicago9835363449311050
Cleveland63919234412051369
Dallas181513879311205801
Denver1991156110501369801
\n", + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "cities = [\"Boston\", \"Buffalo\", \"Chicago\", \"Cleveland\", \"Dallas\", \"Denver\"]\n", + "distances = np.array([\n", + " [None, 457, 983, 639, 1815, 1991],\n", + " [457, None, 536, 192, 1387, 1561],\n", + " [983, 536, None, 344, 931, 1050],\n", + " [639, 192, 344, None, 1205, 1369],\n", + " [1815, 1387, 931, 1205, None, 801],\n", + " [1991, 1561, 1050, 1369, 801, None],\n", + "])\n", + "\n", + "mdisplay(distances, title=None, row_labels=cities, col_labels=cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "### Nested sub-matrices" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:05:16.838517Z", + "start_time": "2023-08-28T23:05:16.835633Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "

2×2, 4 elements

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01
0\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0.1739
0.02798
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
12
34
1\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
12
34
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0.4264
0.3123
\n", + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# You may mix types if the datastructure allows, as a Python list does\n", + "mat = [\n", + " [scipy.sparse.random(2, 2, density=0.6), [[1, 2], [3, 4]]],\n", + " [np.array([[1, 2], [3, 4]]), scipy.sparse.random(2, 2, density=0.6)]\n", + "]\n", + "\n", + "mdisplay(mat)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "### Large matrices" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:42.683137Z", + "start_time": "2023-08-28T23:01:41.994819Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "r = scipy.sparse.random(10000, 10000, density=0.23421, format=\"csr\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:42.687036Z", + "start_time": "2023-08-28T23:01:42.684322Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "

10000×10000, 23421000 'float64' elements, csr

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01234569993999499959996999799989999
00.26360.54150.62790.6977
10.85830.95480.16160.16830.01884
20.73110.1257
30.16560.7769
40.4144
99950.68080.62610.64020.03003
99960.530.25350.37320.81990.51950.003138
99970.38840.1740.38850.61270.73060.3807
99980.68660.5196
99990.47880.20060.35530.6836
\n", + "
" + ], + "text/plain": [ + "<10000x10000 sparse matrix of type ''\n", + "\twith 23421000 stored elements in Compressed Sparse Row format>" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:42.722242Z", + "start_time": "2023-08-28T23:01:42.687623Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "

100000000×100000000, 100000000 'float64' elements, csr

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
012345699999993999999949999999599999996999999979999999899999999
01
11
21
31
41
999999951
999999961
999999971
999999981
999999991
\n", + "
" + ], + "text/plain": [ + "<100000000x100000000 sparse matrix of type ''\n", + "\twith 100000000 stored elements in Compressed Sparse Row format>" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scipy.sparse.eye(100_000_000, format=\"csr\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2023-08-28T23:01:42.722341Z", + "start_time": "2023-08-28T23:01:42.717999Z" + }, + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "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 +}