<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>scipy.linalg.qr_insert &mdash; SciPy v1.1.0.dev0+4e64658 Reference Guide</title>
    
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.1.0.dev0+4e64658',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/scipy-mathjax/MathJax.js?config=scipy-mathjax"></script>
    <script type="text/javascript" src="../_static/js/copybutton.js"></script>
    <link rel="index" title="Index" href="../genindex.html" >
    <link rel="search" title="Search" href="../search.html" >
    <link rel="top" title="SciPy v1.1.0.dev0+4e64658 Reference Guide" href="../index.html" >
    <link rel="up" title="Linear algebra (scipy.linalg)" href="../linalg.html" >
    <link rel="next" title="scipy.linalg.rq" href="scipy.linalg.rq.html" >
    <link rel="prev" title="scipy.linalg.qr_delete" href="scipy.linalg.qr_delete.html" > 
  </head>
  <body>

  <div class="container">
    <div class="header">
    </div>
  </div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
	
        <li class="active"><a href="../index.html">SciPy v1.1.0.dev0+4e64658 Reference Guide</a></li>
	
          <li class="active"><a href="../linalg.html" accesskey="U">Linear algebra (<code class="docutils literal"><span class="pre">scipy.linalg</span></code>)</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="../py-modindex.html" title="Python Module Index"
           >modules</a>
      </li>
      <li class="active">
        <a href="scipy.linalg.rq.html" title="scipy.linalg.rq"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="scipy.linalg.qr_delete.html" title="scipy.linalg.qr_delete"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/scipyshiny_small.png" alt="Logo">
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="scipy.linalg.qr_delete.html"
                        title="previous chapter">scipy.linalg.qr_delete</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="scipy.linalg.rq.html"
                        title="next chapter">scipy.linalg.rq</a></p>


        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="scipy-linalg-qr-insert">
<h1>scipy.linalg.qr_insert<a class="headerlink" href="#scipy-linalg-qr-insert" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="scipy.linalg.qr_insert">
<code class="descclassname">scipy.linalg.</code><code class="descname">qr_insert</code><span class="sig-paren">(</span><em>Q</em>, <em>R</em>, <em>u</em>, <em>k</em>, <em>which='row'</em>, <em>rcond=None</em>, <em>overwrite_qru=False</em>, <em>check_finite=True</em><span class="sig-paren">)</span><a class="headerlink" href="#scipy.linalg.qr_insert" title="Permalink to this definition">¶</a></dt>
<dd><p>QR update on row or column insertions</p>
<p>If <code class="docutils literal"><span class="pre">A</span> <span class="pre">=</span> <span class="pre">Q</span> <span class="pre">R</span></code> is the QR factorization of <code class="docutils literal"><span class="pre">A</span></code>, return the QR
factorization of <code class="docutils literal"><span class="pre">A</span></code> where rows or columns have been inserted starting
at row or column <code class="docutils literal"><span class="pre">k</span></code>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><p class="first"><strong>Q</strong> : (M, M) array_like</p>
<blockquote>
<div><p>Unitary/orthogonal matrix from the QR decomposition of A.</p>
</div></blockquote>
<p><strong>R</strong> : (M, N) array_like</p>
<blockquote>
<div><p>Upper triangular matrix from the QR decomposition of A.</p>
</div></blockquote>
<p><strong>u</strong> : (N,), (p, N), (M,), or (M, p) array_like</p>
<blockquote>
<div><p>Rows or columns to insert</p>
</div></blockquote>
<p><strong>k</strong> : int</p>
<blockquote>
<div><p>Index before which <em class="xref py py-obj">u</em> is to be inserted.</p>
</div></blockquote>
<p><strong>which: {‘row’, ‘col’}, optional</strong></p>
<blockquote>
<div><p>Determines if rows or columns will be inserted, defaults to ‘row’</p>
</div></blockquote>
<p><strong>rcond</strong> : float</p>
<blockquote>
<div><p>Lower bound on the reciprocal condition number of <code class="docutils literal"><span class="pre">Q</span></code> augmented with
<code class="docutils literal"><span class="pre">u/||u||</span></code> Only used when updating economic mode (thin, (M,N) (N,N))
decompositions.  If None, machine precision is used.  Defaults to
None.</p>
</div></blockquote>
<p><strong>overwrite_qru</strong> : bool, optional</p>
<blockquote>
<div><p>If True, consume Q, R, and u, if possible, while performing the update,
otherwise make copies as necessary. Defaults to False.</p>
</div></blockquote>
<p><strong>check_finite</strong> : bool, optional</p>
<blockquote>
<div><p>Whether to check that the input matrices contain only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
Default is True.</p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>Q1</strong> : ndarray</p>
<blockquote>
<div><p>Updated unitary/orthogonal factor</p>
</div></blockquote>
<p><strong>R1</strong> : ndarray</p>
<blockquote>
<div><p>Updated upper triangular factor</p>
</div></blockquote>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body"><p class="first"><strong>LinAlgError :</strong></p>
<blockquote class="last">
<div><p>If updating a (M,N) (N,N) factorization and the reciprocal condition
number of Q augmented with u/||u|| is smaller than rcond.</p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="scipy.linalg.qr.html#scipy.linalg.qr" title="scipy.linalg.qr"><code class="xref py py-obj docutils literal"><span class="pre">qr</span></code></a>, <a class="reference internal" href="scipy.linalg.qr_multiply.html#scipy.linalg.qr_multiply" title="scipy.linalg.qr_multiply"><code class="xref py py-obj docutils literal"><span class="pre">qr_multiply</span></code></a>, <a class="reference internal" href="scipy.linalg.qr_delete.html#scipy.linalg.qr_delete" title="scipy.linalg.qr_delete"><code class="xref py py-obj docutils literal"><span class="pre">qr_delete</span></code></a>, <a class="reference internal" href="scipy.linalg.qr_update.html#scipy.linalg.qr_update" title="scipy.linalg.qr_update"><code class="xref py py-obj docutils literal"><span class="pre">qr_update</span></code></a></p>
</div>
<p class="rubric">Notes</p>
<p>This routine does not guarantee that the diagonal entries of <code class="docutils literal"><span class="pre">R1</span></code> are
positive.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.16.0.</span></p>
</div>
<p class="rubric">References</p>
<table class="docutils citation" frame="void" id="r143" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[R143]</a></td><td>Golub, G. H. &amp; Van Loan, C. F. Matrix Computations, 3rd Ed.
(Johns Hopkins University Press, 1996).</td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="r144" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[R144]</a></td><td>Daniel, J. W., Gragg, W. B., Kaufman, L. &amp; Stewart, G. W.
Reorthogonalization and stable algorithms for updating the
Gram-Schmidt QR factorization. Math. Comput. 30, 772-795 (1976).</td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="r145" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[R145]</a></td><td>Reichel, L. &amp; Gragg, W. B. Algorithm 686: FORTRAN Subroutines for
Updating the QR Decomposition. ACM Trans. Math. Softw. 16, 369-377
(1990).</td></tr>
</tbody>
</table>
<p class="rubric">Examples</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">scipy</span> <span class="k">import</span> <span class="n">linalg</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span>  <span class="mf">3.</span><span class="p">,</span>  <span class="o">-</span><span class="mf">2.</span><span class="p">,</span>  <span class="o">-</span><span class="mf">2.</span><span class="p">],</span>
<span class="gp">... </span>              <span class="p">[</span>  <span class="mf">6.</span><span class="p">,</span>  <span class="o">-</span><span class="mf">7.</span><span class="p">,</span>   <span class="mf">4.</span><span class="p">],</span>
<span class="gp">... </span>              <span class="p">[</span>  <span class="mf">7.</span><span class="p">,</span>   <span class="mf">8.</span><span class="p">,</span>  <span class="o">-</span><span class="mf">6.</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">linalg</span><span class="o">.</span><span class="n">qr</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>
</div>
<p>Given this QR decomposition, update q and r when 2 rows are inserted.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span>  <span class="mf">6.</span><span class="p">,</span>  <span class="o">-</span><span class="mf">9.</span><span class="p">,</span>  <span class="o">-</span><span class="mf">3.</span><span class="p">],</span>
<span class="gp">... </span>              <span class="p">[</span> <span class="o">-</span><span class="mf">3.</span><span class="p">,</span>  <span class="mf">10.</span><span class="p">,</span>   <span class="mf">1.</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q1</span><span class="p">,</span> <span class="n">r1</span> <span class="o">=</span> <span class="n">linalg</span><span class="o">.</span><span class="n">qr_insert</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;row&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q1</span>
<span class="go">array([[-0.25445668,  0.02246245,  0.18146236, -0.72798806,  0.60979671],  # may vary (signs)</span>
<span class="go">       [-0.50891336,  0.23226178, -0.82836478, -0.02837033, -0.00828114],</span>
<span class="go">       [-0.50891336,  0.35715302,  0.38937158,  0.58110733,  0.35235345],</span>
<span class="go">       [ 0.25445668, -0.52202743, -0.32165498,  0.36263239,  0.65404509],</span>
<span class="go">       [-0.59373225, -0.73856549,  0.16065817, -0.0063658 , -0.27595554]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r1</span>
<span class="go">array([[-11.78982612,   6.44623587,   3.81685018],  # may vary (signs)</span>
<span class="go">       [  0.        , -16.01393278,   3.72202865],</span>
<span class="go">       [  0.        ,   0.        ,  -6.13010256],</span>
<span class="go">       [  0.        ,   0.        ,   0.        ],</span>
<span class="go">       [  0.        ,   0.        ,   0.        ]])</span>
</pre></div>
</div>
<p>The update is equivalent, but faster than the following.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a1</span>
<span class="go">array([[  3.,  -2.,  -2.],</span>
<span class="go">       [  6.,  -7.,   4.],</span>
<span class="go">       [  6.,  -9.,  -3.],</span>
<span class="go">       [ -3.,  10.,   1.],</span>
<span class="go">       [  7.,   8.,  -6.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q_direct</span><span class="p">,</span> <span class="n">r_direct</span> <span class="o">=</span> <span class="n">linalg</span><span class="o">.</span><span class="n">qr</span><span class="p">(</span><span class="n">a1</span><span class="p">)</span>
</pre></div>
</div>
<p>Check that we have equivalent results:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">q1</span><span class="p">,</span> <span class="n">r1</span><span class="p">)</span>
<span class="go">array([[  3.,  -2.,  -2.],</span>
<span class="go">       [  6.,  -7.,   4.],</span>
<span class="go">       [  6.,  -9.,  -3.],</span>
<span class="go">       [ -3.,  10.,   1.],</span>
<span class="go">       [  7.,   8.,  -6.]])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">q1</span><span class="p">,</span> <span class="n">r1</span><span class="p">),</span> <span class="n">a1</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>And the updated Q is still unitary:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">q1</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">q1</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2016, The Scipy community.
      </li>
      <li>
      Last updated on Sep 21, 2017.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.3.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>