.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/linalg/cholesky/plot_01_cholesky_solve.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_linalg_cholesky_plot_01_cholesky_solve.py: Cholesky decomposition for SPD systems ========================================== Cholesky factors a symmetric positive-definite ``A = L L^T`` at roughly half the cost of LU, and the factorization succeeding at all (every ``sqrt`` argument staying positive) is itself a certificate of positive-definiteness. .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: Python import numpy as np from mathematicskit.linalg import cholesky_decompose, cholesky_solve, is_symmetric_positive_definite, random_spd_matrix .. GENERATED FROM PYTHON SOURCE LINES 17-19 Factor and solve -------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-30 .. code-block:: Python A = random_spd_matrix(5, seed=1) result = cholesky_decompose(A) print("L @ L.T == A:", np.allclose(result.L @ result.L.T, A)) x_true = np.arange(1.0, 6.0) b = A @ x_true x = cholesky_solve(result, b) print("recovered x:", np.round(x, 8)) print("matches x_true:", np.allclose(x, x_true, atol=1e-8)) .. rst-class:: sphx-glr-script-out .. code-block:: none L @ L.T == A: True recovered x: [1. 2. 3. 4. 5.] matches x_true: True .. GENERATED FROM PYTHON SOURCE LINES 31-33 Positive-definiteness as a byproduct of the factorization succeeding -------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: Python print("is_symmetric_positive_definite(A):", is_symmetric_positive_definite(A)) print("is_symmetric_positive_definite(non-SPD):", is_symmetric_positive_definite(np.array([[1.0, 2.0], [2.0, 1.0]]))) .. rst-class:: sphx-glr-script-out .. code-block:: none is_symmetric_positive_definite(A): True is_symmetric_positive_definite(non-SPD): False .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.001 seconds) .. _sphx_glr_download_api_gallery_linalg_cholesky_plot_01_cholesky_solve.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_cholesky_solve.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_cholesky_solve.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_cholesky_solve.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_