{ "cells": [ { "cell_type": "markdown", "source": [ "# Preconditioners overview\n", "\n", "This page illustrates some of the method(s) in the Julia package\n", "[`Preconditioners.jl`](https://github.com/JuliaLinearAlgebra/Preconditioners.jl).\n", "\n", "This page was generated from a single Julia file:\n", "[1-overview.jl](https://github.com/JuliaLinearAlgebra/Preconditioners.jl/blob/master/docs/lit/examples/1-overview.jl)." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Setup" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Packages needed here." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using Preconditioners: DiagonalPreconditioner, CholeskyPreconditioner\n", "using InteractiveUtils: versioninfo\n", "using SparseArrays: sprand\n", "using LinearAlgebra: I, cond" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "## Overview\n", "\n", "Preconditioning is useful\n", "for accelerating solutions\n", "to systems of equations\n", "and optimization problems.\n", "\n", "\n", "## Examples" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "n = 1000\n", "A = sprand(n, n, 0.01)\n", "A = A + A' + 10I" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Examine conditioning:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "cA = cond(Matrix(A), 2)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Diagonal preconditioner" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "Dp = DiagonalPreconditioner(A)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Apply preconditioner" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "DA = Dp \\ A" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Examine effect on condition number" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "cd = cond(Matrix(DA), 2)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Incomplete Cholesky preconditioner with cut-off level 2" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "Pc = CholeskyPreconditioner(A, 2)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Here is a quick way to handle a matrix argument.\n", "This could be done more efficiently eventually; see Issue #33." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "import Base.\\\n", "\\(C::CholeskyPreconditioner, A::AbstractMatrix) = reduce(hcat, [C \\ c for c in eachcol(A)])" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "apply preconditioner" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "CA = Pc \\ A" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "examine effect on condition number" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "cc = cond(Matrix(CA), 2)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Reproducibility" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "This page was generated with the following version of Julia:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\\n')" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "And with the following package versions" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "import Pkg; Pkg.status()" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.9.3" }, "kernelspec": { "name": "julia-1.9", "display_name": "Julia 1.9.3", "language": "julia" } }, "nbformat": 4 }