.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/linalg/qr/plot_01_householder_vs_gram_schmidt.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_qr_plot_01_householder_vs_gram_schmidt.py: Householder vs. Gram-Schmidt QR ==================================== Both factor ``A = Q R``, but Householder reflections stay orthogonal to machine precision even for ill-conditioned ``A``, while classical Gram-Schmidt can lose orthogonality badly -- modified Gram-Schmidt is a partial (but not complete) remedy. This is the textbook motivation for preferring Householder in production numerical software. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import numpy as np from mathematicskit.linalg import gram_schmidt_qr, householder_qr from mathematicskit.linalg.systems.qr import orthogonality_error .. GENERATED FROM PYTHON SOURCE LINES 19-21 A well-conditioned matrix: all three methods agree -------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-31 .. code-block:: Python rng = np.random.default_rng(0) A = rng.uniform(-2, 2, size=(6, 4)) for label, result in ( ("householder", householder_qr(A)), ("gram_schmidt (modified)", gram_schmidt_qr(A, modified=True)), ("gram_schmidt (classical)", gram_schmidt_qr(A, modified=False)), ): print(f"{label:>28s}: ||QR - A|| = {np.linalg.norm(result.Q @ result.R - A):.2e}, orthogonality error = {orthogonality_error(result.Q):.2e}") .. rst-class:: sphx-glr-script-out .. code-block:: none householder: ||QR - A|| = 2.36e-15, orthogonality error = 4.44e-16 gram_schmidt (modified): ||QR - A|| = 3.38e-16, orthogonality error = 2.22e-16 gram_schmidt (classical): ||QR - A|| = 5.12e-16, orthogonality error = 2.22e-16 .. GENERATED FROM PYTHON SOURCE LINES 32-36 An ill-conditioned matrix: orthogonality breaks down differently --------------------------------------------------------------------- Nearly-collinear columns (Trefethen & Bau's classic example) expose the gap between the three methods. .. GENERATED FROM PYTHON SOURCE LINES 36-45 .. code-block:: Python eps = 1e-8 A_ill = np.array([[1.0, 1.0, 1.0], [eps, 0.0, 0.0], [0.0, eps, 0.0], [0.0, 0.0, eps]]) for label, result in ( ("householder", householder_qr(A_ill)), ("gram_schmidt (modified)", gram_schmidt_qr(A_ill, modified=True)), ("gram_schmidt (classical)", gram_schmidt_qr(A_ill, modified=False)), ): print(f"{label:>28s}: orthogonality error = {orthogonality_error(result.Q):.3e}") .. rst-class:: sphx-glr-script-out .. code-block:: none householder: orthogonality error = 6.661e-16 gram_schmidt (modified): orthogonality error = 7.071e-09 gram_schmidt (classical): orthogonality error = 5.000e-01 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.001 seconds) .. _sphx_glr_download_api_gallery_linalg_qr_plot_01_householder_vs_gram_schmidt.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_householder_vs_gram_schmidt.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_householder_vs_gram_schmidt.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_householder_vs_gram_schmidt.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_