mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
743 lines
60 KiB
HTML
743 lines
60 KiB
HTML
<!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" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.8"/>
|
|
<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 id="projectrow">
|
|
<td id="projectalign">
|
|
<div id="projectname">Algorithms_in_C<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.8 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../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:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(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:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(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">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#define-members">Macros</a> |
|
|
<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" target="_blank">QR decomposition</a> method.
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <assert.h></code><br />
|
|
<code>#include <math.h></code><br />
|
|
<code>#include <stdio.h></code><br />
|
|
<code>#include <stdlib.h></code><br />
|
|
<code>#include <time.h></code><br />
|
|
<code>#include "<a class="el" href="../../d4/d68/qr__decompose_8h_source.html">qr_decompose.h</a>"</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="455" height="199"><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 id="define-members" name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:aee57a411f07599034f5ceb8cc7d65b40" id="r_aee57a411f07599034f5ceb8cc7d65b40"><td class="memItemLeft" align="right" valign="top"><a id="aee57a411f07599034f5ceb8cc7d65b40" name="aee57a411f07599034f5ceb8cc7d65b40"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>LIMS</b>   9</td></tr>
|
|
<tr class="memdesc:aee57a411f07599034f5ceb8cc7d65b40"><td class="mdescLeft"> </td><td class="mdescRight">limit of range of matrix values <br /></td></tr>
|
|
<tr class="separator:aee57a411f07599034f5ceb8cc7d65b40"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a002b2f4894492820fe708b1b7e7c5e70" id="r_a002b2f4894492820fe708b1b7e7c5e70"><td class="memItemLeft" align="right" valign="top"><a id="a002b2f4894492820fe708b1b7e7c5e70" name="a002b2f4894492820fe708b1b7e7c5e70"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>EPSILON</b>   1e-10</td></tr>
|
|
<tr class="memdesc:a002b2f4894492820fe708b1b7e7c5e70"><td class="mdescLeft"> </td><td class="mdescRight">accuracy tolerance limit <br /></td></tr>
|
|
<tr class="separator:a002b2f4894492820fe708b1b7e7c5e70"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:a7d96c5e4ae1bd6d29791bcc23a4cb2b0" id="r_a7d96c5e4ae1bd6d29791bcc23a4cb2b0"><td class="memItemLeft" align="right" valign="top">void </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"> </td><td class="mdescRight">create a square matrix of given size with random elements <br /></td></tr>
|
|
<tr class="separator:a7d96c5e4ae1bd6d29791bcc23a4cb2b0"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a5835240036dc02ee9a13e65c559fb907" id="r_a5835240036dc02ee9a13e65c559fb907"><td class="memItemLeft" align="right" valign="top">double ** </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/qr__eigen__values_8c.html#a5835240036dc02ee9a13e65c559fb907">mat_mul</a> (double **A, double **B, double **OUT, int <a class="el" href="../../d7/d3b/group__hash.html#ga725275fda6544f05ec52cd3c0599de70">R1</a>, int C1, int <a class="el" href="../../d7/d3b/group__hash.html#ga636ffdae3b9f1559ae5419320d10a901">R2</a>, int C2)</td></tr>
|
|
<tr class="memdesc:a5835240036dc02ee9a13e65c559fb907"><td class="mdescLeft"> </td><td class="mdescRight">Perform multiplication of two matrices. <br /></td></tr>
|
|
<tr class="separator:a5835240036dc02ee9a13e65c559fb907"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a0d8ed79786d17df48396b333c09d05bb" id="r_a0d8ed79786d17df48396b333c09d05bb"><td class="memItemLeft" align="right" valign="top">double </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"> </td><td class="mdescRight">Compute eigen values using iterative shifted QR decomposition algorithm as follows: <br /></td></tr>
|
|
<tr class="separator:a0d8ed79786d17df48396b333c09d05bb"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a1440a7779ac56f47a3f355ce4a8c7da0" id="r_a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memItemLeft" align="right" valign="top">void </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"> </td><td class="mdescRight">test function to compute eigen values of a 2x2 matrix <br /></td></tr>
|
|
<tr class="separator:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a0283886819c7c140a023582b7269e2d0" id="r_a0283886819c7c140a023582b7269e2d0"><td class="memItemLeft" align="right" valign="top">void </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"> </td><td class="mdescRight">test function to compute eigen values of a 2x2 matrix <br /></td></tr>
|
|
<tr class="separator:a0283886819c7c140a023582b7269e2d0"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627" id="r_a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int </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"> </td><td class="mdescRight">main function <br /></td></tr>
|
|
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2"> </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" target="_blank">QR decomposition</a> method. </p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
|
|
</div><h2 class="groupheader">Function Documentation</h2>
|
|
<a id="a7d96c5e4ae1bd6d29791bcc23a4cb2b0" name="a7d96c5e4ae1bd6d29791bcc23a4cb2b0"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a7d96c5e4ae1bd6d29791bcc23a4cb2b0">◆ </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 ** </td>
|
|
<td class="paramname"><em>A</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>N</em> </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"><span class="lineno"> 28</span>{</div>
|
|
<div class="line"><span class="lineno"> 29</span> <span class="keywordtype">int</span> i, j, tmp, lim2 = <a class="code hl_define" href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a> >> 1;</div>
|
|
<div class="line"><span class="lineno"> 30</span> </div>
|
|
<div class="line"><span class="lineno"> 31</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 32</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 33</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 34</span> <span class="keywordflow">for</span> (i = 0; i < N; i++)</div>
|
|
<div class="line"><span class="lineno"> 35</span> {</div>
|
|
<div class="line"><span class="lineno"> 36</span> A[i][i] = (rand() % <a class="code hl_define" href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a>) - lim2;</div>
|
|
<div class="line"><span class="lineno"> 37</span> <span class="keywordflow">for</span> (j = i + 1; j < N; j++)</div>
|
|
<div class="line"><span class="lineno"> 38</span> {</div>
|
|
<div class="line"><span class="lineno"> 39</span> tmp = (rand() % <a class="code hl_define" href="../../d7/d50/qr__eigen__values_8c.html#aee57a411f07599034f5ceb8cc7d65b40">LIMS</a>) - lim2;</div>
|
|
<div class="line"><span class="lineno"> 40</span> A[i][j] = tmp;</div>
|
|
<div class="line"><span class="lineno"> 41</span> A[j][i] = tmp;</div>
|
|
<div class="line"><span class="lineno"> 42</span> }</div>
|
|
<div class="line"><span class="lineno"> 43</span> }</div>
|
|
<div class="line"><span class="lineno"> 44</span>}</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" name="a0d8ed79786d17df48396b333c09d05bb"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a0d8ed79786d17df48396b333c09d05bb">◆ </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 ** </td>
|
|
<td class="paramname"><em>A</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double * </td>
|
|
<td class="paramname"><em>eigen_vals</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>mat_size</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">char </td>
|
|
<td class="paramname"><em>debug_print</em> </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' = A - cI\)</li>
|
|
<li>Decompose matrix \(A'=QR\)</li>
|
|
<li>Compute next approximation \(A'_1 = RQ \)</li>
|
|
<li>Shift diagonals back \(A_1 = A'_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 & 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"><span class="lineno"> 108</span>{</div>
|
|
<div class="line"><span class="lineno"> 109</span> <span class="keywordflow">if</span> (!eigen_vals)</div>
|
|
<div class="line"><span class="lineno"> 110</span> {</div>
|
|
<div class="line"><span class="lineno"> 111</span> perror(<span class="stringliteral">"Output eigen value vector cannot be NULL!"</span>);</div>
|
|
<div class="line"><span class="lineno"> 112</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 113</span> }</div>
|
|
<div class="line"><span class="lineno"> 114</span> <span class="keywordtype">double</span> **R = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *) * mat_size);</div>
|
|
<div class="line"><span class="lineno"> 115</span> <span class="keywordtype">double</span> **Q = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *) * mat_size);</div>
|
|
<div class="line"><span class="lineno"> 116</span> <span class="keywordflow">if</span> (!Q || !R)</div>
|
|
<div class="line"><span class="lineno"> 117</span> {</div>
|
|
<div class="line"><span class="lineno"> 118</span> perror(<span class="stringliteral">"Unable to allocate memory for Q & R!"</span>);</div>
|
|
<div class="line"><span class="lineno"> 119</span> <span class="keywordflow">if</span> (Q)</div>
|
|
<div class="line"><span class="lineno"> 120</span> {</div>
|
|
<div class="line"><span class="lineno"> 121</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(Q);</div>
|
|
<div class="line"><span class="lineno"> 122</span> }</div>
|
|
<div class="line"><span class="lineno"> 123</span> <span class="keywordflow">if</span> (R)</div>
|
|
<div class="line"><span class="lineno"> 124</span> {</div>
|
|
<div class="line"><span class="lineno"> 125</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(R);</div>
|
|
<div class="line"><span class="lineno"> 126</span> }</div>
|
|
<div class="line"><span class="lineno"> 127</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 128</span> }</div>
|
|
<div class="line"><span class="lineno"> 129</span> </div>
|
|
<div class="line"><span class="lineno"> 130</span> <span class="comment">/* allocate dynamic memory for matrices */</span></div>
|
|
<div class="line"><span class="lineno"> 131</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < mat_size; i++)</div>
|
|
<div class="line"><span class="lineno"> 132</span> {</div>
|
|
<div class="line"><span class="lineno"> 133</span> R[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>) * mat_size);</div>
|
|
<div class="line"><span class="lineno"> 134</span> Q[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>) * mat_size);</div>
|
|
<div class="line"><span class="lineno"> 135</span> <span class="keywordflow">if</span> (!Q[i] || !R[i])</div>
|
|
<div class="line"><span class="lineno"> 136</span> {</div>
|
|
<div class="line"><span class="lineno"> 137</span> perror(<span class="stringliteral">"Unable to allocate memory for Q & R."</span>);</div>
|
|
<div class="line"><span class="lineno"> 138</span> <span class="keywordflow">for</span> (; i >= 0; i--)</div>
|
|
<div class="line"><span class="lineno"> 139</span> {</div>
|
|
<div class="line"><span class="lineno"> 140</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(R[i]);</div>
|
|
<div class="line"><span class="lineno"> 141</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(Q[i]);</div>
|
|
<div class="line"><span class="lineno"> 142</span> }</div>
|
|
<div class="line"><span class="lineno"> 143</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(Q);</div>
|
|
<div class="line"><span class="lineno"> 144</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(R);</div>
|
|
<div class="line"><span class="lineno"> 145</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 146</span> }</div>
|
|
<div class="line"><span class="lineno"> 147</span> }</div>
|
|
<div class="line"><span class="lineno"> 148</span> </div>
|
|
<div class="line"><span class="lineno"> 149</span> <span class="keywordflow">if</span> (debug_print)</div>
|
|
<div class="line"><span class="lineno"> 150</span> {</div>
|
|
<div class="line"><span class="lineno"> 151</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(A, mat_size, mat_size);</div>
|
|
<div class="line"><span class="lineno"> 152</span> }</div>
|
|
<div class="line"><span class="lineno"> 153</span> </div>
|
|
<div class="line"><span class="lineno"> 154</span> <span class="keywordtype">int</span> rows = mat_size, columns = mat_size;</div>
|
|
<div class="line"><span class="lineno"> 155</span> <span class="keywordtype">int</span> counter = 0, num_eigs = rows - 1;</div>
|
|
<div class="line"><span class="lineno"> 156</span> <span class="keywordtype">double</span> last_eig = 0;</div>
|
|
<div class="line"><span class="lineno"> 157</span> </div>
|
|
<div class="line"><span class="lineno"> 158</span> clock_t t1 = clock();</div>
|
|
<div class="line"><span class="lineno"> 159</span> <span class="keywordflow">while</span> (num_eigs > 0) <span class="comment">/* continue till all eigen values are found */</span></div>
|
|
<div class="line"><span class="lineno"> 160</span> {</div>
|
|
<div class="line"><span class="lineno"> 161</span> <span class="comment">/* iterate with QR decomposition */</span></div>
|
|
<div class="line"><span class="lineno"> 162</span> <span class="keywordflow">while</span> (fabs(A[num_eigs][num_eigs - 1]) > <a class="code hl_define" href="../../d7/d50/qr__eigen__values_8c.html#a002b2f4894492820fe708b1b7e7c5e70">EPSILON</a>)</div>
|
|
<div class="line"><span class="lineno"> 163</span> {</div>
|
|
<div class="line"><span class="lineno"> 164</span> last_eig = A[num_eigs][num_eigs];</div>
|
|
<div class="line"><span class="lineno"> 165</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < rows; i++) A[i][i] -= last_eig; <span class="comment">/* A - cI */</span></div>
|
|
<div class="line"><span class="lineno"> 166</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a45c7640d9d22c89c11beb1f567843c56">qr_decompose</a>(A, Q, R, rows, columns);</div>
|
|
<div class="line"><span class="lineno"> 167</span> </div>
|
|
<div class="line"><span class="lineno"> 168</span> <span class="keywordflow">if</span> (debug_print)</div>
|
|
<div class="line"><span class="lineno"> 169</span> {</div>
|
|
<div class="line"><span class="lineno"> 170</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(A, rows, columns);</div>
|
|
<div class="line"><span class="lineno"> 171</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(Q, rows, columns);</div>
|
|
<div class="line"><span class="lineno"> 172</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(R, columns, columns);</div>
|
|
<div class="line"><span class="lineno"> 173</span> printf(<span class="stringliteral">"-------------------- %d ---------------------\n"</span>,</div>
|
|
<div class="line"><span class="lineno"> 174</span> ++counter);</div>
|
|
<div class="line"><span class="lineno"> 175</span> }</div>
|
|
<div class="line"><span class="lineno"> 176</span> </div>
|
|
<div class="line"><span class="lineno"> 177</span> <a class="code hl_function" href="../../d7/d50/qr__eigen__values_8c.html#a5835240036dc02ee9a13e65c559fb907">mat_mul</a>(R, Q, A, columns, columns, rows, columns);</div>
|
|
<div class="line"><span class="lineno"> 178</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < rows; i++) A[i][i] += last_eig; <span class="comment">/* A + cI */</span></div>
|
|
<div class="line"><span class="lineno"> 179</span> }</div>
|
|
<div class="line"><span class="lineno"> 180</span> </div>
|
|
<div class="line"><span class="lineno"> 181</span> <span class="comment">/* store the converged eigen value */</span></div>
|
|
<div class="line"><span class="lineno"> 182</span> eigen_vals[num_eigs] = last_eig;</div>
|
|
<div class="line"><span class="lineno"> 183</span> </div>
|
|
<div class="line"><span class="lineno"> 184</span> <span class="keywordflow">if</span> (debug_print)</div>
|
|
<div class="line"><span class="lineno"> 185</span> {</div>
|
|
<div class="line"><span class="lineno"> 186</span> printf(<span class="stringliteral">"========================\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 187</span> printf(<span class="stringliteral">"Eigen value: % g,\n"</span>, last_eig);</div>
|
|
<div class="line"><span class="lineno"> 188</span> printf(<span class="stringliteral">"========================\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 189</span> }</div>
|
|
<div class="line"><span class="lineno"> 190</span> </div>
|
|
<div class="line"><span class="lineno"> 191</span> num_eigs--;</div>
|
|
<div class="line"><span class="lineno"> 192</span> rows--;</div>
|
|
<div class="line"><span class="lineno"> 193</span> columns--;</div>
|
|
<div class="line"><span class="lineno"> 194</span> }</div>
|
|
<div class="line"><span class="lineno"> 195</span> eigen_vals[0] = A[0][0];</div>
|
|
<div class="line"><span class="lineno"> 196</span> <span class="keywordtype">double</span> dtime = (double)(clock() - t1) / CLOCKS_PER_SEC;</div>
|
|
<div class="line"><span class="lineno"> 197</span> </div>
|
|
<div class="line"><span class="lineno"> 198</span> <span class="keywordflow">if</span> (debug_print)</div>
|
|
<div class="line"><span class="lineno"> 199</span> {</div>
|
|
<div class="line"><span class="lineno"> 200</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(R, mat_size, mat_size);</div>
|
|
<div class="line"><span class="lineno"> 201</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(Q, mat_size, mat_size);</div>
|
|
<div class="line"><span class="lineno"> 202</span> }</div>
|
|
<div class="line"><span class="lineno"> 203</span> </div>
|
|
<div class="line"><span class="lineno"> 204</span> <span class="comment">/* cleanup dynamic memory */</span></div>
|
|
<div class="line"><span class="lineno"> 205</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < mat_size; i++)</div>
|
|
<div class="line"><span class="lineno"> 206</span> {</div>
|
|
<div class="line"><span class="lineno"> 207</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(R[i]);</div>
|
|
<div class="line"><span class="lineno"> 208</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(Q[i]);</div>
|
|
<div class="line"><span class="lineno"> 209</span> }</div>
|
|
<div class="line"><span class="lineno"> 210</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(R);</div>
|
|
<div class="line"><span class="lineno"> 211</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(Q);</div>
|
|
<div class="line"><span class="lineno"> 212</span> </div>
|
|
<div class="line"><span class="lineno"> 213</span> <span class="keywordflow">return</span> dtime;</div>
|
|
<div class="line"><span class="lineno"> 214</span>}</div>
|
|
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition</b> malloc_dbg.h:18</div></div>
|
|
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition</b> malloc_dbg.h:26</div></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_a5835240036dc02ee9a13e65c559fb907"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#a5835240036dc02ee9a13e65c559fb907">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="531" height="208"><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="a3c04138a5bfe5d72780bb7e82a18e627" name="a3c04138a5bfe5d72780bb7e82a18e627"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>argc</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">char ** </td>
|
|
<td class="paramname"><em>argv</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>main function </p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 316</span>{</div>
|
|
<div class="line"><span class="lineno"> 317</span> srand(time(NULL));</div>
|
|
<div class="line"><span class="lineno"> 318</span> </div>
|
|
<div class="line"><span class="lineno"> 319</span> <span class="keywordtype">int</span> mat_size = 5;</div>
|
|
<div class="line"><span class="lineno"> 320</span> <span class="keywordflow">if</span> (argc == 2)</div>
|
|
<div class="line"><span class="lineno"> 321</span> {</div>
|
|
<div class="line"><span class="lineno"> 322</span> mat_size = atoi(argv[1]);</div>
|
|
<div class="line"><span class="lineno"> 323</span> }</div>
|
|
<div class="line"><span class="lineno"> 324</span> <span class="keywordflow">else</span></div>
|
|
<div class="line"><span class="lineno"> 325</span> { <span class="comment">// if invalid input argument is given run tests</span></div>
|
|
<div class="line"><span class="lineno"> 326</span> <a class="code hl_function" href="../../d7/d50/qr__eigen__values_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a>();</div>
|
|
<div class="line"><span class="lineno"> 327</span> <a class="code hl_function" href="../../d7/d50/qr__eigen__values_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a>();</div>
|
|
<div class="line"><span class="lineno"> 328</span> printf(<span class="stringliteral">"Usage: ./qr_eigen_values [mat_size]\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 329</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 330</span> }</div>
|
|
<div class="line"><span class="lineno"> 331</span> </div>
|
|
<div class="line"><span class="lineno"> 332</span> <span class="keywordflow">if</span> (mat_size < 2)</div>
|
|
<div class="line"><span class="lineno"> 333</span> {</div>
|
|
<div class="line"><span class="lineno"> 334</span> fprintf(stderr, <span class="stringliteral">"Matrix size should be > 2\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 335</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 336</span> }</div>
|
|
<div class="line"><span class="lineno"> 337</span> </div>
|
|
<div class="line"><span class="lineno"> 338</span> <span class="keywordtype">int</span> i;</div>
|
|
<div class="line"><span class="lineno"> 339</span> </div>
|
|
<div class="line"><span class="lineno"> 340</span> <span class="keywordtype">double</span> **A = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *) * mat_size);</div>
|
|
<div class="line"><span class="lineno"> 341</span> <span class="comment">/* number of eigen values = matrix size */</span></div>
|
|
<div class="line"><span class="lineno"> 342</span> <span class="keywordtype">double</span> *eigen_vals = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>) * mat_size);</div>
|
|
<div class="line"><span class="lineno"> 343</span> <span class="keywordflow">if</span> (!eigen_vals)</div>
|
|
<div class="line"><span class="lineno"> 344</span> {</div>
|
|
<div class="line"><span class="lineno"> 345</span> perror(<span class="stringliteral">"Unable to allocate memory for eigen values!"</span>);</div>
|
|
<div class="line"><span class="lineno"> 346</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(A);</div>
|
|
<div class="line"><span class="lineno"> 347</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 348</span> }</div>
|
|
<div class="line"><span class="lineno"> 349</span> <span class="keywordflow">for</span> (i = 0; i < mat_size; i++)</div>
|
|
<div class="line"><span class="lineno"> 350</span> {</div>
|
|
<div class="line"><span class="lineno"> 351</span> A[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>) * mat_size);</div>
|
|
<div class="line"><span class="lineno"> 352</span> eigen_vals[i] = 0.f;</div>
|
|
<div class="line"><span class="lineno"> 353</span> }</div>
|
|
<div class="line"><span class="lineno"> 354</span> </div>
|
|
<div class="line"><span class="lineno"> 355</span> <span class="comment">/* create a random matrix */</span></div>
|
|
<div class="line"><span class="lineno"> 356</span> <a class="code hl_function" href="../../d7/d50/qr__eigen__values_8c.html#a7d96c5e4ae1bd6d29791bcc23a4cb2b0">create_matrix</a>(A, mat_size);</div>
|
|
<div class="line"><span class="lineno"> 357</span> </div>
|
|
<div class="line"><span class="lineno"> 358</span> <a class="code hl_function" href="../../d4/d68/qr__decompose_8h.html#a90562ce8c3707401e9c5809dece68d6a">print_matrix</a>(A, mat_size, mat_size);</div>
|
|
<div class="line"><span class="lineno"> 359</span> </div>
|
|
<div class="line"><span class="lineno"> 360</span> <span class="keywordtype">double</span> dtime = <a class="code hl_function" href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">eigen_values</a>(A, eigen_vals, mat_size, 0);</div>
|
|
<div class="line"><span class="lineno"> 361</span> printf(<span class="stringliteral">"Eigen vals: "</span>);</div>
|
|
<div class="line"><span class="lineno"> 362</span> <span class="keywordflow">for</span> (i = 0; i < mat_size; i++) printf(<span class="stringliteral">"% 9.4g\t"</span>, eigen_vals[i]);</div>
|
|
<div class="line"><span class="lineno"> 363</span> printf(<span class="stringliteral">"\nTime taken to compute: % .4g sec\n"</span>, dtime);</div>
|
|
<div class="line"><span class="lineno"> 364</span> </div>
|
|
<div class="line"><span class="lineno"> 365</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < mat_size; i++) <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(A[i]);</div>
|
|
<div class="line"><span class="lineno"> 366</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(A);</div>
|
|
<div class="line"><span class="lineno"> 367</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(eigen_vals);</div>
|
|
<div class="line"><span class="lineno"> 368</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 369</span>}</div>
|
|
<div class="ttc" id="aqr__eigen__values_8c_html_a0283886819c7c140a023582b7269e2d0"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a></div><div class="ttdeci">void test2()</div><div class="ttdoc">test function to compute eigen values of a 2x2 matrix</div><div class="ttdef"><b>Definition</b> qr_eigen_values.c:271</div></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 class="ttc" id="aqr__eigen__values_8c_html_a1440a7779ac56f47a3f355ce4a8c7da0"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a></div><div class="ttdeci">void test1()</div><div class="ttdoc">test function to compute eigen values of a 2x2 matrix</div><div class="ttdef"><b>Definition</b> qr_eigen_values.c:224</div></div>
|
|
<div class="ttc" id="aqr__eigen__values_8c_html_a7d96c5e4ae1bd6d29791bcc23a4cb2b0"><div class="ttname"><a href="../../d7/d50/qr__eigen__values_8c.html#a7d96c5e4ae1bd6d29791bcc23a4cb2b0">create_matrix</a></div><div class="ttdeci">void create_matrix(double **A, int N)</div><div class="ttdoc">create a square matrix of given size with random elements</div><div class="ttdef"><b>Definition</b> qr_eigen_values.c:27</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d7/d50/qr__eigen__values_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="100%" height="496"><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>
|
|
<a id="a5835240036dc02ee9a13e65c559fb907" name="a5835240036dc02ee9a13e65c559fb907"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a5835240036dc02ee9a13e65c559fb907">◆ </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 ** </td>
|
|
<td class="paramname"><em>A</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double ** </td>
|
|
<td class="paramname"><em>B</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double ** </td>
|
|
<td class="paramname"><em>OUT</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>R1</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>C1</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>R2</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>C2</em> </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"><span class="lineno"> 61</span>{</div>
|
|
<div class="line"><span class="lineno"> 62</span> <span class="keywordflow">if</span> (C1 != <a class="code hl_variable" href="../../d7/d3b/group__hash.html#ga636ffdae3b9f1559ae5419320d10a901">R2</a>)</div>
|
|
<div class="line"><span class="lineno"> 63</span> {</div>
|
|
<div class="line"><span class="lineno"> 64</span> perror(<span class="stringliteral">"Matrix dimensions mismatch!"</span>);</div>
|
|
<div class="line"><span class="lineno"> 65</span> <span class="keywordflow">return</span> OUT;</div>
|
|
<div class="line"><span class="lineno"> 66</span> }</div>
|
|
<div class="line"><span class="lineno"> 67</span> </div>
|
|
<div class="line"><span class="lineno"> 68</span> <span class="keywordtype">int</span> i;</div>
|
|
<div class="line"><span class="lineno"> 69</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 70</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 71</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 72</span> <span class="keywordflow">for</span> (i = 0; i < <a class="code hl_variable" href="../../d7/d3b/group__hash.html#ga725275fda6544f05ec52cd3c0599de70">R1</a>; i++)</div>
|
|
<div class="line"><span class="lineno"> 73</span> {</div>
|
|
<div class="line"><span class="lineno"> 74</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < C2; j++)</div>
|
|
<div class="line"><span class="lineno"> 75</span> {</div>
|
|
<div class="line"><span class="lineno"> 76</span> OUT[i][j] = 0.f;</div>
|
|
<div class="line"><span class="lineno"> 77</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k < C1; k++) OUT[i][j] += A[i][k] * B[k][j];</div>
|
|
<div class="line"><span class="lineno"> 78</span> }</div>
|
|
<div class="line"><span class="lineno"> 79</span> }</div>
|
|
<div class="line"><span class="lineno"> 80</span> <span class="keywordflow">return</span> OUT;</div>
|
|
<div class="line"><span class="lineno"> 81</span>}</div>
|
|
<div class="ttc" id="agroup__hash_html_ga636ffdae3b9f1559ae5419320d10a901"><div class="ttname"><a href="../../d7/d3b/group__hash.html#ga636ffdae3b9f1559ae5419320d10a901">R2</a></div><div class="ttdeci">static const uint8_t R2</div><div class="ttdoc">Rotation constant 2 for mixing function G.</div><div class="ttdef"><b>Definition</b> hash_blake2b.c:84</div></div>
|
|
<div class="ttc" id="agroup__hash_html_ga725275fda6544f05ec52cd3c0599de70"><div class="ttname"><a href="../../d7/d3b/group__hash.html#ga725275fda6544f05ec52cd3c0599de70">R1</a></div><div class="ttdeci">static const uint8_t R1</div><div class="ttdoc">Rotation constant 1 for mixing function G.</div><div class="ttdef"><b>Definition</b> hash_blake2b.c:83</div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a1440a7779ac56f47a3f355ce4a8c7da0" name="a1440a7779ac56f47a3f355ce4a8c7da0"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a1440a7779ac56f47a3f355ce4a8c7da0">◆ </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 & 7\\
|
|
7 & 11
|
|
\end{bmatrix}\]
|
|
</p>
|
|
<p> which are approximately, {15.56158, 0.384227} </p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 225</span>{</div>
|
|
<div class="line"><span class="lineno"> 226</span> <span class="keywordtype">int</span> mat_size = 2;</div>
|
|
<div class="line"><span class="lineno"> 227</span> <span class="keywordtype">double</span> X[][2] = {{5, 7}, {7, 11}};</div>
|
|
<div class="line"><span class="lineno"> 228</span> <span class="keywordtype">double</span> y[] = {15.56158, 0.384227}; <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><span class="lineno"> 229</span> <span class="keywordtype">double</span> eig_vals[2] = {0, 0};</div>
|
|
<div class="line"><span class="lineno"> 230</span> </div>
|
|
<div class="line"><span class="lineno"> 231</span> <span class="comment">// The following steps are to convert a "double[][]" to "double **"</span></div>
|
|
<div class="line"><span class="lineno"> 232</span> <span class="keywordtype">double</span> **A = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(mat_size * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><span class="lineno"> 233</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < mat_size; i++) A[i] = X[i];</div>
|
|
<div class="line"><span class="lineno"> 234</span> </div>
|
|
<div class="line"><span class="lineno"> 235</span> printf(<span class="stringliteral">"------- Test 1 -------\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 236</span> </div>
|
|
<div class="line"><span class="lineno"> 237</span> <span class="keywordtype">double</span> dtime = <a class="code hl_function" href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">eigen_values</a>(A, eig_vals, mat_size, 0);</div>
|
|
<div class="line"><span class="lineno"> 238</span> </div>
|
|
<div class="line"><span class="lineno"> 239</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < mat_size; i++)</div>
|
|
<div class="line"><span class="lineno"> 240</span> {</div>
|
|
<div class="line"><span class="lineno"> 241</span> printf(<span class="stringliteral">"%d/5 Checking for %.3g --> "</span>, i + 1, y[i]);</div>
|
|
<div class="line"><span class="lineno"> 242</span> <span class="keywordtype">char</span> result = 0;</div>
|
|
<div class="line"><span class="lineno"> 243</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < mat_size && !result; j++)</div>
|
|
<div class="line"><span class="lineno"> 244</span> {</div>
|
|
<div class="line"><span class="lineno"> 245</span> <span class="keywordflow">if</span> (fabs(y[i] - eig_vals[j]) < 0.1)</div>
|
|
<div class="line"><span class="lineno"> 246</span> {</div>
|
|
<div class="line"><span class="lineno"> 247</span> result = 1;</div>
|
|
<div class="line"><span class="lineno"> 248</span> printf(<span class="stringliteral">"(%.3g) "</span>, eig_vals[j]);</div>
|
|
<div class="line"><span class="lineno"> 249</span> }</div>
|
|
<div class="line"><span class="lineno"> 250</span> }</div>
|
|
<div class="line"><span class="lineno"> 251</span> </div>
|
|
<div class="line"><span class="lineno"> 252</span> <span class="comment">// ensure that i^th expected eigen value was computed</span></div>
|
|
<div class="line"><span class="lineno"> 253</span> assert(result != 0);</div>
|
|
<div class="line"><span class="lineno"> 254</span> printf(<span class="stringliteral">"found\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 255</span> }</div>
|
|
<div class="line"><span class="lineno"> 256</span> printf(<span class="stringliteral">"Test 1 Passed in %.3g sec\n\n"</span>, dtime);</div>
|
|
<div class="line"><span class="lineno"> 257</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(A);</div>
|
|
<div class="line"><span class="lineno"> 258</span>}</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="628" height="208"><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" name="a0283886819c7c140a023582b7269e2d0"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a0283886819c7c140a023582b7269e2d0">◆ </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& 4& 2& 0& -3\\
|
|
4& -4& 4& -3& -1\\
|
|
2& 4& 4& 3& -3\\
|
|
0& -3& 3& -1&-1\\
|
|
-3& -1& -3& -3& 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"><span class="lineno"> 272</span>{</div>
|
|
<div class="line"><span class="lineno"> 273</span> <span class="keywordtype">int</span> mat_size = 5;</div>
|
|
<div class="line"><span class="lineno"> 274</span> <span class="keywordtype">double</span> X[][5] = {{-4, 4, 2, 0, -3},</div>
|
|
<div class="line"><span class="lineno"> 275</span> {4, -4, 4, -3, -1},</div>
|
|
<div class="line"><span class="lineno"> 276</span> {2, 4, 4, 3, -3},</div>
|
|
<div class="line"><span class="lineno"> 277</span> {0, -3, 3, -1, -3},</div>
|
|
<div class="line"><span class="lineno"> 278</span> {-3, -1, -3, -3, 0}};</div>
|
|
<div class="line"><span class="lineno"> 279</span> <span class="keywordtype">double</span> y[] = {9.27648, -9.26948, 2.0181, -1.03516,</div>
|
|
<div class="line"><span class="lineno"> 280</span> -5.98994}; <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><span class="lineno"> 281</span> <span class="keywordtype">double</span> eig_vals[5];</div>
|
|
<div class="line"><span class="lineno"> 282</span> </div>
|
|
<div class="line"><span class="lineno"> 283</span> <span class="comment">// The following steps are to convert a "double[][]" to "double **"</span></div>
|
|
<div class="line"><span class="lineno"> 284</span> <span class="keywordtype">double</span> **A = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(mat_size * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><span class="lineno"> 285</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < mat_size; i++) A[i] = X[i];</div>
|
|
<div class="line"><span class="lineno"> 286</span> </div>
|
|
<div class="line"><span class="lineno"> 287</span> printf(<span class="stringliteral">"------- Test 2 -------\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 288</span> </div>
|
|
<div class="line"><span class="lineno"> 289</span> <span class="keywordtype">double</span> dtime = <a class="code hl_function" href="../../d7/d50/qr__eigen__values_8c.html#a0d8ed79786d17df48396b333c09d05bb">eigen_values</a>(A, eig_vals, mat_size, 0);</div>
|
|
<div class="line"><span class="lineno"> 290</span> </div>
|
|
<div class="line"><span class="lineno"> 291</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < mat_size; i++)</div>
|
|
<div class="line"><span class="lineno"> 292</span> {</div>
|
|
<div class="line"><span class="lineno"> 293</span> printf(<span class="stringliteral">"%d/5 Checking for %.3g --> "</span>, i + 1, y[i]);</div>
|
|
<div class="line"><span class="lineno"> 294</span> <span class="keywordtype">char</span> result = 0;</div>
|
|
<div class="line"><span class="lineno"> 295</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < mat_size && !result; j++)</div>
|
|
<div class="line"><span class="lineno"> 296</span> {</div>
|
|
<div class="line"><span class="lineno"> 297</span> <span class="keywordflow">if</span> (fabs(y[i] - eig_vals[j]) < 0.1)</div>
|
|
<div class="line"><span class="lineno"> 298</span> {</div>
|
|
<div class="line"><span class="lineno"> 299</span> result = 1;</div>
|
|
<div class="line"><span class="lineno"> 300</span> printf(<span class="stringliteral">"(%.3g) "</span>, eig_vals[j]);</div>
|
|
<div class="line"><span class="lineno"> 301</span> }</div>
|
|
<div class="line"><span class="lineno"> 302</span> }</div>
|
|
<div class="line"><span class="lineno"> 303</span> </div>
|
|
<div class="line"><span class="lineno"> 304</span> <span class="comment">// ensure that i^th expected eigen value was computed</span></div>
|
|
<div class="line"><span class="lineno"> 305</span> assert(result != 0);</div>
|
|
<div class="line"><span class="lineno"> 306</span> printf(<span class="stringliteral">"found\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 307</span> }</div>
|
|
<div class="line"><span class="lineno"> 308</span> printf(<span class="stringliteral">"Test 2 Passed in %.3g sec\n\n"</span>, dtime);</div>
|
|
<div class="line"><span class="lineno"> 309</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(A);</div>
|
|
<div class="line"><span class="lineno"> 310</span>}</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="628" height="208"><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.8 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|