TheAlgorithms-C/d7/d50/qr__eigen__values_8c.html

637 lines
52 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: numerical_methods/qr_eigen_values.c File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C
&#160;<span id="projectnumber">1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('d7/d50/qr__eigen__values_8c.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">qr_eigen_values.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<p>Compute real eigen values and eigen vectors of a symmetric matrix using <a href="https://en.wikipedia.org/wiki/QR_decomposition">QR decomposition</a> method.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;assert.h&gt;</code><br />
<code>#include &lt;math.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
<code>#include &quot;<a class="el" href="../../d4/d68/qr__decompose_8h_source.html">qr_decompose.h</a>&quot;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for qr_eigen_values.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d4/d15/qr__eigen__values_8c__incl.svg" width="459" height="202"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:aee57a411f07599034f5ceb8cc7d65b40"><td class="memItemLeft" align="right" valign="top"><a id="aee57a411f07599034f5ceb8cc7d65b40"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a>&#160;&#160;&#160;9</td></tr>
<tr class="memdesc:aee57a411f07599034f5ceb8cc7d65b40"><td class="mdescLeft">&#160;</td><td class="mdescRight">limit of range of matrix values <br /></td></tr>
<tr class="separator:aee57a411f07599034f5ceb8cc7d65b40"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a002b2f4894492820fe708b1b7e7c5e70"><td class="memItemLeft" align="right" valign="top"><a id="a002b2f4894492820fe708b1b7e7c5e70"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a002b2f4894492820fe708b1b7e7c5e70">EPSILON</a>&#160;&#160;&#160;1e-10</td></tr>
<tr class="memdesc:a002b2f4894492820fe708b1b7e7c5e70"><td class="mdescLeft">&#160;</td><td class="mdescRight">accuracy tolerance limit <br /></td></tr>
<tr class="separator:a002b2f4894492820fe708b1b7e7c5e70"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a7d96c5e4ae1bd6d29791bcc23a4cb2b0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a7d96c5e4ae1bd6d29791bcc23a4cb2b0">create_matrix</a> (double **A, int N)</td></tr>
<tr class="memdesc:a7d96c5e4ae1bd6d29791bcc23a4cb2b0"><td class="mdescLeft">&#160;</td><td class="mdescRight">create a square matrix of given size with random elements <a href="../../d7/d50/qr__eigen__values_8c.html#a7d96c5e4ae1bd6d29791bcc23a4cb2b0">More...</a><br /></td></tr>
<tr class="separator:a7d96c5e4ae1bd6d29791bcc23a4cb2b0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a741477692f001a805b0fea942c9dc2b9"><td class="memItemLeft" align="right" valign="top">double **&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a741477692f001a805b0fea942c9dc2b9">mat_mul</a> (double **A, double **B, double **OUT, int R1, int C1, int R2, int C2)</td></tr>
<tr class="memdesc:a741477692f001a805b0fea942c9dc2b9"><td class="mdescLeft">&#160;</td><td class="mdescRight">Perform multiplication of two matrices. <a href="../../d7/d50/qr__eigen__values_8c.html#a741477692f001a805b0fea942c9dc2b9">More...</a><br /></td></tr>
<tr class="separator:a741477692f001a805b0fea942c9dc2b9"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0d8ed79786d17df48396b333c09d05bb"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">eigen_values</a> (double **A, double *eigen_vals, int mat_size, char debug_print)</td></tr>
<tr class="memdesc:a0d8ed79786d17df48396b333c09d05bb"><td class="mdescLeft">&#160;</td><td class="mdescRight">Compute eigen values using iterative shifted QR decomposition algorithm as follows: <a href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">More...</a><br /></td></tr>
<tr class="separator:a0d8ed79786d17df48396b333c09d05bb"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a> ()</td></tr>
<tr class="memdesc:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="mdescLeft">&#160;</td><td class="mdescRight">test function to compute eigen values of a 2x2 matrix <a href="../../d7/d50/qr__eigen__values_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">More...</a><br /></td></tr>
<tr class="separator:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0283886819c7c140a023582b7269e2d0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a> ()</td></tr>
<tr class="memdesc:a0283886819c7c140a023582b7269e2d0"><td class="mdescLeft">&#160;</td><td class="mdescRight">test function to compute eigen values of a 2x2 matrix <a href="../../d7/d50/qr__eigen__values_8c.html#a0283886819c7c140a023582b7269e2d0">More...</a><br /></td></tr>
<tr class="separator:a0283886819c7c140a023582b7269e2d0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top"><a id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
<tr class="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft">&#160;</td><td class="mdescRight">main function <br /></td></tr>
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Compute real eigen values and eigen vectors of a symmetric matrix using <a href="https://en.wikipedia.org/wiki/QR_decomposition">QR decomposition</a> method. </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a7d96c5e4ae1bd6d29791bcc23a4cb2b0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7d96c5e4ae1bd6d29791bcc23a4cb2b0">&#9670;&nbsp;</a></span>create_matrix()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void create_matrix </td>
<td>(</td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>A</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>N</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>create a square matrix of given size with random elements </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[out]</td><td class="paramname">A</td><td>matrix to create (must be pre-allocated in memory) </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>matrix size </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160;{</div>
<div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160; <span class="keywordtype">int</span> i, j, tmp, lim2 = <a class="code" href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a> &gt;&gt; 1;</div>
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160; </div>
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; N; i++)</div>
<div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160; {</div>
<div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; A[i][i] = (rand() % <a class="code" href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a>) - lim2;</div>
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160; <span class="keywordflow">for</span> (j = i + 1; j &lt; N; j++)</div>
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160; {</div>
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160; tmp = (rand() % <a class="code" href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a>) - lim2;</div>
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; A[i][j] = tmp;</div>
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; A[j][i] = tmp;</div>
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160; }</div>
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; }</div>
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160;}</div>
<div class="ttc" id="aqr__eigen__values_8c_html_aee57a411f07599034f5ceb8cc7d65b40"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a></div><div class="ttdeci">#define LIMS</div><div class="ttdoc">limit of range of matrix values</div><div class="ttdef"><b>Definition:</b> qr_eigen_values.c:19</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a0d8ed79786d17df48396b333c09d05bb"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0d8ed79786d17df48396b333c09d05bb">&#9670;&nbsp;</a></span>eigen_values()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double eigen_values </td>
<td>(</td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>A</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>eigen_vals</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>mat_size</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char&#160;</td>
<td class="paramname"><em>debug_print</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Compute eigen values using iterative shifted QR decomposition algorithm as follows: </p>
<ol type="1">
<li>Use last diagonal element of A as eigen value approximation \(c\)</li>
<li>Shift diagonals of matrix \(A&#39; = A - cI\)</li>
<li>Decompose matrix \(A&#39;=QR\)</li>
<li>Compute next approximation \(A&#39;_1 = RQ \)</li>
<li>Shift diagonals back \(A_1 = A&#39;_1 + cI\)</li>
<li>Termination condition check: last element below diagonal is almost 0<ol type="a">
<li>If not 0, go back to step 1 with the new approximation \(A_1\)</li>
<li>If 0, continue to step 7</li>
</ol>
</li>
<li>Save last known \(c\) as the eigen value.</li>
<li>Are all eigen values found?<ol type="a">
<li>If not, remove last row and column of \(A_1\) and go back to step 1.</li>
<li>If yes, stop.</li>
</ol>
</li>
</ol>
<dl class="section note"><dt>Note</dt><dd>The matrix \(A\) gets modified</dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in,out]</td><td class="paramname">A</td><td>matrix to compute eigen values for </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">eigen_vals</td><td>resultant vector containing computed eigen values </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">mat_size</td><td>matrix size </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">debug_print</td><td>1 to print intermediate Q &amp; R matrices, 0 for not to </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>time for computation in seconds </dd></dl>
<div class="fragment"><div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160;{</div>
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160; <span class="keywordflow">if</span> (!eigen_vals)</div>
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160; {</div>
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>&#160; perror(<span class="stringliteral">&quot;Output eigen value vector cannot be NULL!&quot;</span>);</div>
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160; <span class="keywordflow">return</span> -1;</div>
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>&#160; }</div>
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160; <span class="keywordtype">double</span> **R = (<span class="keywordtype">double</span> **)malloc(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *) * mat_size);</div>
<div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160; <span class="keywordtype">double</span> **Q = (<span class="keywordtype">double</span> **)malloc(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *) * mat_size);</div>
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160; <span class="keywordflow">if</span> (!Q || !R)</div>
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; {</div>
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; perror(<span class="stringliteral">&quot;Unable to allocate memory for Q &amp; R!&quot;</span>);</div>
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>&#160; <span class="keywordflow">if</span> (Q)</div>
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>&#160; {</div>
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>&#160; free(Q);</div>
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>&#160; }</div>
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span>&#160; <span class="keywordflow">if</span> (R)</div>
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160; {</div>
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160; free(R);</div>
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span>&#160; }</div>
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160; <span class="keywordflow">return</span> -1;</div>
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>&#160; }</div>
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>&#160; </div>
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160; <span class="comment">/* allocate dynamic memory for matrices */</span></div>
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mat_size; i++)</div>
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>&#160; {</div>
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>&#160; R[i] = (<span class="keywordtype">double</span> *)malloc(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>) * mat_size);</div>
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>&#160; Q[i] = (<span class="keywordtype">double</span> *)malloc(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>) * mat_size);</div>
<div class="line"><a name="l00135"></a><span class="lineno"> 135</span>&#160; <span class="keywordflow">if</span> (!Q[i] || !R[i])</div>
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>&#160; {</div>
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span>&#160; perror(<span class="stringliteral">&quot;Unable to allocate memory for Q &amp; R.&quot;</span>);</div>
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>&#160; <span class="keywordflow">for</span> (; i &gt;= 0; i--)</div>
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>&#160; {</div>
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>&#160; free(R[i]);</div>
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>&#160; free(Q[i]);</div>
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>&#160; }</div>
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>&#160; free(Q);</div>
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>&#160; free(R);</div>
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160; <span class="keywordflow">return</span> -1;</div>
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160; }</div>
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160; }</div>
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>&#160; </div>
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>&#160; <span class="keywordflow">if</span> (debug_print)</div>
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>&#160; {</div>
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>&#160; <a class="code" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(A, mat_size, mat_size);</div>
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>&#160; }</div>
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span>&#160; </div>
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>&#160; <span class="keywordtype">int</span> rows = mat_size, columns = mat_size;</div>
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>&#160; <span class="keywordtype">int</span> counter = 0, num_eigs = rows - 1;</div>
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>&#160; <span class="keywordtype">double</span> last_eig = 0;</div>
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>&#160; </div>
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>&#160; clock_t t1 = clock();</div>
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>&#160; <span class="keywordflow">while</span> (num_eigs &gt; 0) <span class="comment">/* continue till all eigen values are found */</span></div>
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160; {</div>
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160; <span class="comment">/* iterate with QR decomposition */</span></div>
<div class="line"><a name="l00162"></a><span class="lineno"> 162</span>&#160; <span class="keywordflow">while</span> (fabs(A[num_eigs][num_eigs - 1]) &gt; <a class="code" href="../../d7/d50/qr__eigen__values_8c.html#a002b2f4894492820fe708b1b7e7c5e70">EPSILON</a>)</div>
<div class="line"><a name="l00163"></a><span class="lineno"> 163</span>&#160; {</div>
<div class="line"><a name="l00164"></a><span class="lineno"> 164</span>&#160; last_eig = A[num_eigs][num_eigs];</div>
<div class="line"><a name="l00165"></a><span class="lineno"> 165</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; rows; i++) A[i][i] -= last_eig; <span class="comment">/* A - cI */</span></div>
<div class="line"><a name="l00166"></a><span class="lineno"> 166</span>&#160; <a class="code" href="../../d4/d68/qr__decompose_8h.html#a45c7640d9d22c89c11beb1f567843c56">qr_decompose</a>(A, Q, R, rows, columns);</div>
<div class="line"><a name="l00167"></a><span class="lineno"> 167</span>&#160; </div>
<div class="line"><a name="l00168"></a><span class="lineno"> 168</span>&#160; <span class="keywordflow">if</span> (debug_print)</div>
<div class="line"><a name="l00169"></a><span class="lineno"> 169</span>&#160; {</div>
<div class="line"><a name="l00170"></a><span class="lineno"> 170</span>&#160; <a class="code" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(A, rows, columns);</div>
<div class="line"><a name="l00171"></a><span class="lineno"> 171</span>&#160; <a class="code" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(Q, rows, columns);</div>
<div class="line"><a name="l00172"></a><span class="lineno"> 172</span>&#160; <a class="code" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(R, columns, columns);</div>
<div class="line"><a name="l00173"></a><span class="lineno"> 173</span>&#160; printf(<span class="stringliteral">&quot;-------------------- %d ---------------------\n&quot;</span>,</div>
<div class="line"><a name="l00174"></a><span class="lineno"> 174</span>&#160; ++counter);</div>
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>&#160; }</div>
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>&#160; </div>
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>&#160; <a class="code" href="../../d7/d50/qr__eigen__values_8c.html#a741477692f001a805b0fea942c9dc2b9">mat_mul</a>(R, Q, A, columns, columns, rows, columns);</div>
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; rows; i++) A[i][i] += last_eig; <span class="comment">/* A + cI */</span></div>
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>&#160; }</div>
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>&#160; </div>
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>&#160; <span class="comment">/* store the converged eigen value */</span></div>
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>&#160; eigen_vals[num_eigs] = last_eig;</div>
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>&#160; </div>
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>&#160; <span class="keywordflow">if</span> (debug_print)</div>
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>&#160; {</div>
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>&#160; printf(<span class="stringliteral">&quot;========================\n&quot;</span>);</div>
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>&#160; printf(<span class="stringliteral">&quot;Eigen value: % g,\n&quot;</span>, last_eig);</div>
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>&#160; printf(<span class="stringliteral">&quot;========================\n&quot;</span>);</div>
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>&#160; }</div>
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>&#160; </div>
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>&#160; num_eigs--;</div>
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>&#160; rows--;</div>
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>&#160; columns--;</div>
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>&#160; }</div>
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>&#160; eigen_vals[0] = A[0][0];</div>
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>&#160; <span class="keywordtype">double</span> dtime = (double)(clock() - t1) / CLOCKS_PER_SEC;</div>
<div class="line"><a name="l00197"></a><span class="lineno"> 197</span>&#160; </div>
<div class="line"><a name="l00198"></a><span class="lineno"> 198</span>&#160; <span class="keywordflow">if</span> (debug_print)</div>
<div class="line"><a name="l00199"></a><span class="lineno"> 199</span>&#160; {</div>
<div class="line"><a name="l00200"></a><span class="lineno"> 200</span>&#160; <a class="code" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(R, mat_size, mat_size);</div>
<div class="line"><a name="l00201"></a><span class="lineno"> 201</span>&#160; <a class="code" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(Q, mat_size, mat_size);</div>
<div class="line"><a name="l00202"></a><span class="lineno"> 202</span>&#160; }</div>
<div class="line"><a name="l00203"></a><span class="lineno"> 203</span>&#160; </div>
<div class="line"><a name="l00204"></a><span class="lineno"> 204</span>&#160; <span class="comment">/* cleanup dynamic memory */</span></div>
<div class="line"><a name="l00205"></a><span class="lineno"> 205</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mat_size; i++)</div>
<div class="line"><a name="l00206"></a><span class="lineno"> 206</span>&#160; {</div>
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>&#160; free(R[i]);</div>
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>&#160; free(Q[i]);</div>
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>&#160; }</div>
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>&#160; free(R);</div>
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>&#160; free(Q);</div>
<div class="line"><a name="l00212"></a><span class="lineno"> 212</span>&#160; </div>
<div class="line"><a name="l00213"></a><span class="lineno"> 213</span>&#160; <span class="keywordflow">return</span> dtime;</div>
<div class="line"><a name="l00214"></a><span class="lineno"> 214</span>&#160;}</div>
<div class="ttc" id="aqr__decompose_8h_html_a45c7640d9d22c89c11beb1f567843c56"><div class="ttname"><a href="../../d4/d68/qr__decompose_8h.html#a45c7640d9d22c89c11beb1f567843c56">qr_decompose</a></div><div class="ttdeci">void qr_decompose(double **A, double **Q, double **R, int M, int N)</div><div class="ttdoc">Decompose matrix using Gram-Schmidt process.</div><div class="ttdef"><b>Definition:</b> qr_decompose.h:142</div></div>
<div class="ttc" id="aqr__decompose_8h_html_a90562ce8c3707401e9c5809dece68d6a"><div class="ttname"><a href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a></div><div class="ttdeci">void print_matrix(double **A, int M, int N)</div><div class="ttdoc">function to display matrix on stdout</div><div class="ttdef"><b>Definition:</b> qr_decompose.h:22</div></div>
<div class="ttc" id="aqr__eigen__values_8c_html_a002b2f4894492820fe708b1b7e7c5e70"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#a002b2f4894492820fe708b1b7e7c5e70">EPSILON</a></div><div class="ttdeci">#define EPSILON</div><div class="ttdoc">accuracy tolerance limit</div><div class="ttdef"><b>Definition:</b> qr_eigen_values.c:20</div></div>
<div class="ttc" id="aqr__eigen__values_8c_html_a741477692f001a805b0fea942c9dc2b9"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#a741477692f001a805b0fea942c9dc2b9">mat_mul</a></div><div class="ttdeci">double ** mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2, int C2)</div><div class="ttdoc">Perform multiplication of two matrices.</div><div class="ttdef"><b>Definition:</b> qr_eigen_values.c:59</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d7/d50/qr__eigen__values_8c_a0d8ed79786d17df48396b333c09d05bb_cgraph.svg" width="534" height="215"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a741477692f001a805b0fea942c9dc2b9"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a741477692f001a805b0fea942c9dc2b9">&#9670;&nbsp;</a></span>mat_mul()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double** mat_mul </td>
<td>(</td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>A</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>B</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>OUT</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>R1</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>C1</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>R2</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>C2</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Perform multiplication of two matrices. </p>
<ul>
<li>R2 must be equal to C1</li>
<li>Resultant matrix size should be R1xC2 <dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">A</td><td>first matrix to multiply </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">B</td><td>second matrix to multiply </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">OUT</td><td>output matrix (must be pre-allocated) </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">R1</td><td>number of rows of first matrix </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">C1</td><td>number of columns of first matrix </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">R2</td><td>number of rows of second matrix </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">C2</td><td>number of columns of second matrix </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to resultant matrix </dd></dl>
</li>
</ul>
<div class="fragment"><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160;{</div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; <span class="keywordflow">if</span> (C1 != R2)</div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; {</div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; perror(<span class="stringliteral">&quot;Matrix dimensions mismatch!&quot;</span>);</div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; <span class="keywordflow">return</span> OUT;</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; }</div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; </div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; <span class="keywordtype">int</span> i;</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160;<span class="preprocessor">#ifdef _OPENMP</span></div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160;<span class="preprocessor">#pragma omp for</span></div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; R1; i++)</div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; {</div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; C2; j++)</div>
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160; {</div>
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; OUT[i][j] = 0.f;</div>
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k &lt; C1; k++) OUT[i][j] += A[i][k] * B[k][j];</div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; }</div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; }</div>
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; <span class="keywordflow">return</span> OUT;</div>
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a1440a7779ac56f47a3f355ce4a8c7da0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1440a7779ac56f47a3f355ce4a8c7da0">&#9670;&nbsp;</a></span>test1()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test1 </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>test function to compute eigen values of a 2x2 matrix </p>
<p class="formulaDsp">
\[\begin{bmatrix} 5 &amp; 7\\ 7 &amp; 11 \end{bmatrix}\]
</p>
<p> which are approximately, {15.56158, 0.384227} </p>
<div class="fragment"><div class="line"><a name="l00225"></a><span class="lineno"> 225</span>&#160;{</div>
<div class="line"><a name="l00226"></a><span class="lineno"> 226</span>&#160; <span class="keywordtype">int</span> mat_size = 2;</div>
<div class="line"><a name="l00227"></a><span class="lineno"> 227</span>&#160; <span class="keywordtype">double</span> X[][2] = {{5, 7}, {7, 11}};</div>
<div class="line"><a name="l00228"></a><span class="lineno"> 228</span>&#160; <span class="keywordtype">double</span> y[] = {15.56158, 0.384227}; <span class="comment">// corresponding y-values</span></div>
<div class="line"><a name="l00229"></a><span class="lineno"> 229</span>&#160; <span class="keywordtype">double</span> eig_vals[2] = {0, 0};</div>
<div class="line"><a name="l00230"></a><span class="lineno"> 230</span>&#160; </div>
<div class="line"><a name="l00231"></a><span class="lineno"> 231</span>&#160; <span class="comment">// The following steps are to convert a &quot;double[][]&quot; to &quot;double **&quot;</span></div>
<div class="line"><a name="l00232"></a><span class="lineno"> 232</span>&#160; <span class="keywordtype">double</span> **A = (<span class="keywordtype">double</span> **)malloc(mat_size * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00233"></a><span class="lineno"> 233</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mat_size; i++) A[i] = X[i];</div>
<div class="line"><a name="l00234"></a><span class="lineno"> 234</span>&#160; </div>
<div class="line"><a name="l00235"></a><span class="lineno"> 235</span>&#160; printf(<span class="stringliteral">&quot;------- Test 1 -------\n&quot;</span>);</div>
<div class="line"><a name="l00236"></a><span class="lineno"> 236</span>&#160; </div>
<div class="line"><a name="l00237"></a><span class="lineno"> 237</span>&#160; <span class="keywordtype">double</span> dtime = <a class="code" href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">eigen_values</a>(A, eig_vals, mat_size, 0);</div>
<div class="line"><a name="l00238"></a><span class="lineno"> 238</span>&#160; </div>
<div class="line"><a name="l00239"></a><span class="lineno"> 239</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mat_size; i++)</div>
<div class="line"><a name="l00240"></a><span class="lineno"> 240</span>&#160; {</div>
<div class="line"><a name="l00241"></a><span class="lineno"> 241</span>&#160; printf(<span class="stringliteral">&quot;%d/5 Checking for %.3g --&gt; &quot;</span>, i + 1, y[i]);</div>
<div class="line"><a name="l00242"></a><span class="lineno"> 242</span>&#160; <span class="keywordtype">char</span> result = 0;</div>
<div class="line"><a name="l00243"></a><span class="lineno"> 243</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; mat_size &amp;&amp; !result; j++)</div>
<div class="line"><a name="l00244"></a><span class="lineno"> 244</span>&#160; {</div>
<div class="line"><a name="l00245"></a><span class="lineno"> 245</span>&#160; <span class="keywordflow">if</span> (fabs(y[i] - eig_vals[j]) &lt; 0.1)</div>
<div class="line"><a name="l00246"></a><span class="lineno"> 246</span>&#160; {</div>
<div class="line"><a name="l00247"></a><span class="lineno"> 247</span>&#160; result = 1;</div>
<div class="line"><a name="l00248"></a><span class="lineno"> 248</span>&#160; printf(<span class="stringliteral">&quot;(%.3g) &quot;</span>, eig_vals[j]);</div>
<div class="line"><a name="l00249"></a><span class="lineno"> 249</span>&#160; }</div>
<div class="line"><a name="l00250"></a><span class="lineno"> 250</span>&#160; }</div>
<div class="line"><a name="l00251"></a><span class="lineno"> 251</span>&#160; </div>
<div class="line"><a name="l00252"></a><span class="lineno"> 252</span>&#160; <span class="comment">// ensure that i^th expected eigen value was computed</span></div>
<div class="line"><a name="l00253"></a><span class="lineno"> 253</span>&#160; assert(result != 0);</div>
<div class="line"><a name="l00254"></a><span class="lineno"> 254</span>&#160; printf(<span class="stringliteral">&quot;found\n&quot;</span>);</div>
<div class="line"><a name="l00255"></a><span class="lineno"> 255</span>&#160; }</div>
<div class="line"><a name="l00256"></a><span class="lineno"> 256</span>&#160; printf(<span class="stringliteral">&quot;Test 1 Passed in %.3g sec\n\n&quot;</span>, dtime);</div>
<div class="line"><a name="l00257"></a><span class="lineno"> 257</span>&#160; free(A);</div>
<div class="line"><a name="l00258"></a><span class="lineno"> 258</span>&#160;}</div>
<div class="ttc" id="aqr__eigen__values_8c_html_a0d8ed79786d17df48396b333c09d05bb"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">eigen_values</a></div><div class="ttdeci">double eigen_values(double **A, double *eigen_vals, int mat_size, char debug_print)</div><div class="ttdoc">Compute eigen values using iterative shifted QR decomposition algorithm as follows:</div><div class="ttdef"><b>Definition:</b> qr_eigen_values.c:106</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d7/d50/qr__eigen__values_8c_a1440a7779ac56f47a3f355ce4a8c7da0_cgraph.svg" width="632" height="215"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a0283886819c7c140a023582b7269e2d0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0283886819c7c140a023582b7269e2d0">&#9670;&nbsp;</a></span>test2()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test2 </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>test function to compute eigen values of a 2x2 matrix </p>
<p class="formulaDsp">
\[\begin{bmatrix} -4&amp; 4&amp; 2&amp; 0&amp; -3\\ 4&amp; -4&amp; 4&amp; -3&amp; -1\\ 2&amp; 4&amp; 4&amp; 3&amp; -3\\ 0&amp; -3&amp; 3&amp; -1&amp;-1\\ -3&amp; -1&amp; -3&amp; -3&amp; 0 \end{bmatrix}\]
</p>
<p> which are approximately, {9.27648, -9.26948, 2.0181, -1.03516, -5.98994} </p>
<div class="fragment"><div class="line"><a name="l00272"></a><span class="lineno"> 272</span>&#160;{</div>
<div class="line"><a name="l00273"></a><span class="lineno"> 273</span>&#160; <span class="keywordtype">int</span> mat_size = 5;</div>
<div class="line"><a name="l00274"></a><span class="lineno"> 274</span>&#160; <span class="keywordtype">double</span> X[][5] = {{-4, 4, 2, 0, -3},</div>
<div class="line"><a name="l00275"></a><span class="lineno"> 275</span>&#160; {4, -4, 4, -3, -1},</div>
<div class="line"><a name="l00276"></a><span class="lineno"> 276</span>&#160; {2, 4, 4, 3, -3},</div>
<div class="line"><a name="l00277"></a><span class="lineno"> 277</span>&#160; {0, -3, 3, -1, -3},</div>
<div class="line"><a name="l00278"></a><span class="lineno"> 278</span>&#160; {-3, -1, -3, -3, 0}};</div>
<div class="line"><a name="l00279"></a><span class="lineno"> 279</span>&#160; <span class="keywordtype">double</span> y[] = {9.27648, -9.26948, 2.0181, -1.03516,</div>
<div class="line"><a name="l00280"></a><span class="lineno"> 280</span>&#160; -5.98994}; <span class="comment">// corresponding y-values</span></div>
<div class="line"><a name="l00281"></a><span class="lineno"> 281</span>&#160; <span class="keywordtype">double</span> eig_vals[5];</div>
<div class="line"><a name="l00282"></a><span class="lineno"> 282</span>&#160; </div>
<div class="line"><a name="l00283"></a><span class="lineno"> 283</span>&#160; <span class="comment">// The following steps are to convert a &quot;double[][]&quot; to &quot;double **&quot;</span></div>
<div class="line"><a name="l00284"></a><span class="lineno"> 284</span>&#160; <span class="keywordtype">double</span> **A = (<span class="keywordtype">double</span> **)malloc(mat_size * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
<div class="line"><a name="l00285"></a><span class="lineno"> 285</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mat_size; i++) A[i] = X[i];</div>
<div class="line"><a name="l00286"></a><span class="lineno"> 286</span>&#160; </div>
<div class="line"><a name="l00287"></a><span class="lineno"> 287</span>&#160; printf(<span class="stringliteral">&quot;------- Test 2 -------\n&quot;</span>);</div>
<div class="line"><a name="l00288"></a><span class="lineno"> 288</span>&#160; </div>
<div class="line"><a name="l00289"></a><span class="lineno"> 289</span>&#160; <span class="keywordtype">double</span> dtime = <a class="code" href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">eigen_values</a>(A, eig_vals, mat_size, 0);</div>
<div class="line"><a name="l00290"></a><span class="lineno"> 290</span>&#160; </div>
<div class="line"><a name="l00291"></a><span class="lineno"> 291</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mat_size; i++)</div>
<div class="line"><a name="l00292"></a><span class="lineno"> 292</span>&#160; {</div>
<div class="line"><a name="l00293"></a><span class="lineno"> 293</span>&#160; printf(<span class="stringliteral">&quot;%d/5 Checking for %.3g --&gt; &quot;</span>, i + 1, y[i]);</div>
<div class="line"><a name="l00294"></a><span class="lineno"> 294</span>&#160; <span class="keywordtype">char</span> result = 0;</div>
<div class="line"><a name="l00295"></a><span class="lineno"> 295</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; mat_size &amp;&amp; !result; j++)</div>
<div class="line"><a name="l00296"></a><span class="lineno"> 296</span>&#160; {</div>
<div class="line"><a name="l00297"></a><span class="lineno"> 297</span>&#160; <span class="keywordflow">if</span> (fabs(y[i] - eig_vals[j]) &lt; 0.1)</div>
<div class="line"><a name="l00298"></a><span class="lineno"> 298</span>&#160; {</div>
<div class="line"><a name="l00299"></a><span class="lineno"> 299</span>&#160; result = 1;</div>
<div class="line"><a name="l00300"></a><span class="lineno"> 300</span>&#160; printf(<span class="stringliteral">&quot;(%.3g) &quot;</span>, eig_vals[j]);</div>
<div class="line"><a name="l00301"></a><span class="lineno"> 301</span>&#160; }</div>
<div class="line"><a name="l00302"></a><span class="lineno"> 302</span>&#160; }</div>
<div class="line"><a name="l00303"></a><span class="lineno"> 303</span>&#160; </div>
<div class="line"><a name="l00304"></a><span class="lineno"> 304</span>&#160; <span class="comment">// ensure that i^th expected eigen value was computed</span></div>
<div class="line"><a name="l00305"></a><span class="lineno"> 305</span>&#160; assert(result != 0);</div>
<div class="line"><a name="l00306"></a><span class="lineno"> 306</span>&#160; printf(<span class="stringliteral">&quot;found\n&quot;</span>);</div>
<div class="line"><a name="l00307"></a><span class="lineno"> 307</span>&#160; }</div>
<div class="line"><a name="l00308"></a><span class="lineno"> 308</span>&#160; printf(<span class="stringliteral">&quot;Test 2 Passed in %.3g sec\n\n&quot;</span>, dtime);</div>
<div class="line"><a name="l00309"></a><span class="lineno"> 309</span>&#160; free(A);</div>
<div class="line"><a name="l00310"></a><span class="lineno"> 310</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d7/d50/qr__eigen__values_8c_a0283886819c7c140a023582b7269e2d0_cgraph.svg" width="632" height="215"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_9c6faab82c22511b50177aa2e38e2780.html">numerical_methods</a></li><li class="navelem"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html">qr_eigen_values.c</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1 </li>
</ul>
</div>
</body>
</html>