{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "# MatRepr - edge cases" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.652355Z", "start_time": "2023-09-01T06:59:27.292068Z" } }, "outputs": [], "source": [ "# so matrepr can be imported from the source tree.\n", "import sys\n", "sys.path.insert(0, '..')\n", "\n", "from matrepr import mdisplay\n", "import matrepr\n", "matrepr.params.width_str = 115 # Narrow enough for GitHub's nb viewer" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "## SciPy\n", "\n", "* duplicate entries\n", "* explicit zero" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.668542Z", "start_time": "2023-09-01T06:59:27.305887Z" } }, "outputs": [], "source": [ "import scipy\n", "\n", "row = [0, 0, 1, 2, 2, 3, 3, 3, 4]\n", "col = [0, 1, 0, 2, 2, 3, 3, 3, 4]\n", "val = [1, 12e34, 1e-6, 2.1, 2.2, 3.1, 3.2, 3.3, 0]\n", "A = scipy.sparse.coo_array((val, (row, col)), shape=(5, 5))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677319Z", "start_time": "2023-09-01T06:59:28.090059Z" } }, "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
5×5, 9 'float64' elements, coo
01234
011.2e+35
11e-06
22.1
2.2
33.1
3.2
3.3
40
\n
" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(A, \"html\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677500Z", "start_time": "2023-09-01T06:59:28.090177Z" } }, "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}$" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(A, \"latex\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677607Z", "start_time": "2023-09-01T06:59:28.090214Z" } }, "outputs": [ { "data": { "text/plain": "<5×5, 9 'float64' elements, coo>\n 0 1 2 3 4\n ┌ ┐\n0 │ 1 1.2e+35 │\n1 │ 1e-06 │\n2 │ [2.1, 2.2] │\n3 │ [3.1, 3.2, 3.3] │\n4 │ 0 │\n └ ┘" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(A, \"str\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "## Lists\n", "\n", "* uneven row lengths\n", "* varying row types\n", "* explicit zero\n", "* contains nested lists\n", "* contains other supported matrix types, like a SciPy matrix\n", "* Strings elements with HTML and LaTeX control characters\n", "* Complex numbers\n", "* User-defined numpy dtypes\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677652Z", "start_time": "2023-09-01T06:59:28.090327Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "sps_mat = scipy.sparse.coo_array(([1, 2, 3, 4], ([0, 0, 1, 1], [0, 1, 0, 1])), shape=(2, 2))\n", "\n", "dtype_a = np.dtype([(\"x\", np.bool_), (\"y\", np.int64)], align=True)\n", "np_a = np.array([(False, 2)], dtype=dtype_a)[0]\n", "dtype_b = np.dtype(\"(3,)uint16\")\n", "np_b = np.array([(1, 2, 3)], dtype=dtype_b)[0]\n", "np_2d = np.array([[11, 22], [33, 44]])\n", "\n", "list_mat = [\n", " (0, 12e34, 1e-6, None, 123456789),\n", " 1,\n", " [complex(1, 2), complex(123456, 0.123456)],\n", " [[1], sps_mat, [2.1, 2.2], [[1.1, 2.2], [3.3, 4.4]]],\n", " [\"multiline\\nstring\", \"\", \"\\\\begin{escape!}\", {\"a Python set\"}],\n", " [np_a, np_b, np_2d]\n", "]\n", "\n", "row_labels = [\"sci\", \"single\", \"complex\", \"nested\", \"strings\", \"numpy\"]\n", "col_labels = [\"one\", \"two\", \"three\", \"four\", \"five\"]\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677730Z", "start_time": "2023-09-01T06:59:28.090358Z" } }, "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
6×5, 18 elements, list
onetwothreefourfive
sci01.2e+351e-061.235e+08
single1
complex1+2i1.235e05 + 0.1235i
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
" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(list_mat, \"html\", row_labels=row_labels, col_labels=col_labels)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677795Z", "start_time": "2023-09-01T06:59:28.090447Z" } }, "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 & & & & \\\\\n 1+2i & 1.235 \\times 10^{5}+0.1235i & & & \\\\\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}$" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(list_mat, \"latex\", row_labels=row_labels, col_labels=col_labels)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677897Z", "start_time": "2023-09-01T06:59:28.090630Z" } }, "outputs": [ { "data": { "text/plain": "<6×5, 18 elements, list>\n one two three four five\n ┌ ┐\n sci │ 0 1.2e+35 1e-06 1.235e+08 │\n single │ 1 │\ncomplex │ 1+2i 1.235e05 + 0.1235i │\n nested │ [1] [[1, 2], [3, 4]] [2.1, 2.2] [[1.1, 2.2], [3.3, 4.4]] │\nstrings │ 'multiline\\nstring' '' '\\\\begin{escape!}' {'a Python set'} │\n numpy │ (False, 2) [1 2 3] [[11 22] [33 44]] │\n └ ┘" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(list_mat, \"str\", row_labels=row_labels, col_labels=col_labels)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-09-01T06:59:28.677948Z", "start_time": "2023-09-01T06:59:28.606962Z" } }, "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 }