{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "cfb7b2cb",
            "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": "cfac9aeb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.sparse import csc_array\nfrom scipy.sparse.linalg import lsmr\nA = csc_array([[1., 0.], [1., 1.], [0., 1.]], dtype=float)",
            "outputs": []
        },
        {
            "id": "4eef9baa",
            "cell_type": "markdown",
            "source": "The first example has the trivial solution ``[0, 0]``\n",
            "metadata": {}
        },
        {
            "id": "a662e544",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b = np.array([0., 0., 0.], dtype=float)\nx, istop, itn, normr = lsmr(A, b)[:4]\nistop",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c604a6d9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0., 0.])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "88db6b14",
            "cell_type": "markdown",
            "source": "The stopping code ``istop=0`` returned indicates that a vector of zeros was\nfound as a solution. The returned solution `x` indeed contains\n``[0., 0.]``. The next example has a non-trivial solution:\n",
            "metadata": {}
        },
        {
            "id": "b0c5eba9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b = np.array([1., 0., -1.], dtype=float)\nx, istop, itn, normr = lsmr(A, b)[:4]\nistop",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b581cefc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 1., -1.])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ffca8c61",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "itn",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d5e112bf",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "normr",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "4.440892098500627e-16"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "55df9240",
            "cell_type": "markdown",
            "source": "As indicated by ``istop=1``, `lsmr` found a solution obeying the tolerance\nlimits. The given solution ``[1., -1.]`` obviously solves the equation. The\nremaining return values include information about the number of iterations\n(`itn=1`) and the remaining difference of left and right side of the solved\nequation.\nThe final example demonstrates the behavior in the case where there is no\nsolution for the equation:\n",
            "metadata": {}
        },
        {
            "id": "af8bca6b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b = np.array([1., 0.01, -1.], dtype=float)\nx, istop, itn, normr = lsmr(A, b)[:4]\nistop",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ae39c266",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 1.00333333, -0.99666667])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c8fe3877",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "A.dot(x)-b",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 0.00333333, -0.00333333,  0.00333333])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "480a3d8d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "normr",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.005773502691896255"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f64e9268",
            "cell_type": "markdown",
            "source": "`istop` indicates that the system is inconsistent and thus `x` is rather an\napproximate solution to the corresponding least-squares problem. `normr`\ncontains the minimal distance that was found.",
            "metadata": {}
        }
    ]
}