{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "b49bc123",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "b5739650",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from io import BytesIO\nimport numpy as np\nfrom scipy.sparse import coo_array\nfrom scipy.io import mmwrite",
            "outputs": []
        },
        {
            "id": "9ab3ae6a",
            "cell_type": "markdown",
            "source": "Write a small NumPy array to a matrix market file.  The file will be\nwritten in the ``'array'`` format.\n",
            "metadata": {}
        },
        {
            "id": "c6f38665",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a = np.array([[1.0, 0, 0, 0], [0, 2.5, 0, 6.25]])\ntarget = BytesIO()\nmmwrite(target, a)\nprint(target.getvalue().decode('latin1'))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "%%MatrixMarket matrix array real general\n%\n2 4\n1\n0\n0\n2.5\n0\n0\n0\n6.25"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2175b0e6",
            "cell_type": "markdown",
            "source": "Add a comment to the output file, and set the precision to 3.\n",
            "metadata": {}
        },
        {
            "id": "96e123ec",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "target = BytesIO()\nmmwrite(target, a, comment='\\n Some test data.\\n', precision=3)\nprint(target.getvalue().decode('latin1'))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "%%MatrixMarket matrix array real general\n%\n% Some test data.\n%\n2 4\n1.00e+00\n0.00e+00\n0.00e+00\n2.50e+00\n0.00e+00\n0.00e+00\n0.00e+00\n6.25e+00"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "99f12baf",
            "cell_type": "markdown",
            "source": "Convert to a sparse matrix before calling ``mmwrite``.  This will\nresult in the output format being ``'coordinate'`` rather than\n``'array'``.\n",
            "metadata": {}
        },
        {
            "id": "b3f9994d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "target = BytesIO()\nmmwrite(target, coo_array(a), precision=3)\nprint(target.getvalue().decode('latin1'))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "%%MatrixMarket matrix coordinate real general\n%\n2 4 3\n1 1 1.00e+00\n2 2 2.50e+00\n2 4 6.25e+00"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f8cd1653",
            "cell_type": "markdown",
            "source": "Write a complex Hermitian array to a matrix market file.  Note that\nonly six values are actually written to the file; the other values\nare implied by the symmetry.\n",
            "metadata": {}
        },
        {
            "id": "9fdaf610",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "z = np.array([[3, 1+2j, 4-3j], [1-2j, 1, -5j], [4+3j, 5j, 2.5]])\nz",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 3. +0.j,  1. +2.j,  4. -3.j],\n       [ 1. -2.j,  1. +0.j, -0. -5.j],\n       [ 4. +3.j,  0. +5.j,  2.5+0.j]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a2cc7feb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "target = BytesIO()\nmmwrite(target, z, precision=2)\nprint(target.getvalue().decode('latin1'))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "%%MatrixMarket matrix array complex hermitian\n%\n3 3\n3.0e+00 0.0e+00\n1.0e+00 -2.0e+00\n4.0e+00 3.0e+00\n1.0e+00 0.0e+00\n0.0e+00 5.0e+00\n2.5e+00 0.0e+00"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c0fb82c5",
            "cell_type": "markdown",
            "source": "This method is threaded.\nThe default number of threads is equal to the number of CPUs in the system.\nUse [threadpoolctl](https://github.com/joblib/threadpoolctl) to override:\n",
            "metadata": {}
        },
        {
            "id": "e49a48ca",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import threadpoolctl\n\ntarget = BytesIO()\nwith threadpoolctl.threadpool_limits(limits=2):\n    mmwrite(target, a)",
            "outputs": []
        }
    ]
}