TheAlgorithms-C/da/d38/durand__kerner__roots_8c.html
2021-12-09 17:57:52 +00:00

487 lines
45 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">
<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.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: numerical_methods/durand_kerner_roots.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<span id="projectnumber">&#160;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.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'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&amp;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&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('da/d38/durand__kerner__roots_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">durand_kerner_roots.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Compute all possible approximate roots of any given polynomial using <a href="https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method" target="_blank">Durand Kerner algorithm</a>
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;complex.h&gt;</code><br />
<code>#include &lt;limits.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;string.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for durand_kerner_roots.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../db/dff/durand__kerner__roots_8c__incl.svg" width="616" height="127"><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:af270a96662132d0385cb6b4637c5a689"><td class="memItemLeft" align="right" valign="top"><a id="af270a96662132d0385cb6b4637c5a689" name="af270a96662132d0385cb6b4637c5a689"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>ACCURACY</b>&#160;&#160;&#160;1e-10</td></tr>
<tr class="memdesc:af270a96662132d0385cb6b4637c5a689"><td class="mdescLeft">&#160;</td><td class="mdescRight">maximum accuracy limit <br /></td></tr>
<tr class="separator:af270a96662132d0385cb6b4637c5a689"><td class="memSeparator" colspan="2">&#160;</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:a321f9781a9744ccdaf0aba89f35ec29c"><td class="memItemLeft" align="right" valign="top">long double complex&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d38/durand__kerner__roots_8c.html#a321f9781a9744ccdaf0aba89f35ec29c">poly_function</a> (long double *coeffs, unsigned int degree, long double complex x)</td></tr>
<tr class="memdesc:a321f9781a9744ccdaf0aba89f35ec29c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Evaluate the value of a polynomial with given coefficients. <a href="../../da/d38/durand__kerner__roots_8c.html#a321f9781a9744ccdaf0aba89f35ec29c">More...</a><br /></td></tr>
<tr class="separator:a321f9781a9744ccdaf0aba89f35ec29c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a46197e91f478e99a2229562aaaac311e"><td class="memItemLeft" align="right" valign="top">const char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a> (long double complex x)</td></tr>
<tr class="memdesc:a46197e91f478e99a2229562aaaac311e"><td class="mdescLeft">&#160;</td><td class="mdescRight">create a textual form of complex number <a href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">More...</a><br /></td></tr>
<tr class="separator:a46197e91f478e99a2229562aaaac311e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a26d5695ebed0818a3e7cf4b10aacab52"><td class="memItemLeft" align="right" valign="top">char&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d38/durand__kerner__roots_8c.html#a26d5695ebed0818a3e7cf4b10aacab52">check_termination</a> (long double delta)</td></tr>
<tr class="memdesc:a26d5695ebed0818a3e7cf4b10aacab52"><td class="mdescLeft">&#160;</td><td class="mdescRight">check for termination condition <a href="../../da/d38/durand__kerner__roots_8c.html#a26d5695ebed0818a3e7cf4b10aacab52">More...</a><br /></td></tr>
<tr class="separator:a26d5695ebed0818a3e7cf4b10aacab52"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d38/durand__kerner__roots_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</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 all possible approximate roots of any given polynomial using <a href="https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method" target="_blank">Durand Kerner algorithm</a> </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a></dd></dl>
<p>Test the algorithm online: <a href="https://gist.github.com/kvedala/27f1b0b6502af935f6917673ec43bcd7">https://gist.github.com/kvedala/27f1b0b6502af935f6917673ec43bcd7</a></p>
<p >Try the highly unstable Wilkinson's polynomial: </p><div class="fragment"><div class="line">./numerical_methods/durand_kerner_roots.c 1 -210 20615 -1256850 53327946</div>
<div class="line">-1672280820 40171771630 -756111184500 11310276995381 -135585182899530</div>
<div class="line">1307535010540395 -10142299865511450 63030812099294896 -311333643161390640</div>
<div class="line">1206647803780373360 -3599979517947607200 8037811822645051776</div>
<div class="line">-12870931245150988800 13803759753640704000 -8752948036761600000</div>
<div class="line">2432902008176640000</div>
</div><!-- fragment --><p> Sample implementation results to compute approximate roots of the equation \(x^4-1=0\):<br />
<img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/numerical_methods/durand_kerner_error.svg" alt="Error evolution during root approximations computed every
iteration." style="pointer-events: none;" width="400" class="inline"/> <img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/numerical_methods/durand_kerner_roots.svg" alt="Roots evolution - shows the initial approximation of the
roots and their convergence to a final approximation along with the iterative
approximations" style="pointer-events: none;" width="400" class="inline"/> </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a26d5695ebed0818a3e7cf4b10aacab52" name="a26d5695ebed0818a3e7cf4b10aacab52"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a26d5695ebed0818a3e7cf4b10aacab52">&#9670;&nbsp;</a></span>check_termination()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char check_termination </td>
<td>(</td>
<td class="paramtype">long double&#160;</td>
<td class="paramname"><em>delta</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>check for termination condition </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">delta</td><td>point at which to evaluate the polynomial </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>0 if termination not reached </dd>
<dd>
1 if termination reached </dd></dl>
<div class="fragment"><div class="line"><a id="l00084" name="l00084"></a><span class="lineno"> 84</span>{</div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"> 85</span> <span class="keyword">static</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> past_delta = INFINITY;</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span> <span class="keywordflow">if</span> (fabsl(past_delta - delta) &lt;= <a class="code hl_define" href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a> || delta &lt; <a class="code hl_define" href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a>)</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> <span class="keywordflow">return</span> 1;</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span> past_delta = delta;</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span>}</div>
<div class="ttc" id="adurand__kerner__roots_8c_html_af270a96662132d0385cb6b4637c5a689"><div class="ttname"><a href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a></div><div class="ttdeci">#define ACCURACY</div><div class="ttdoc">maximum accuracy limit</div><div class="ttdef"><b>Definition:</b> durand_kerner_roots.c:41</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a46197e91f478e99a2229562aaaac311e" name="a46197e91f478e99a2229562aaaac311e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a46197e91f478e99a2229562aaaac311e">&#9670;&nbsp;</a></span>complex_str()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">const char * complex_str </td>
<td>(</td>
<td class="paramtype">long double complex&#160;</td>
<td class="paramname"><em>x</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>create a textual form of complex number </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>point at which to evaluate the polynomial </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to converted string </dd></dl>
<div class="fragment"><div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span>{</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> <span class="keyword">static</span> <span class="keywordtype">char</span> msg[50];</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> <span class="keywordtype">double</span> r = creal(x);</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="keywordtype">double</span> c = cimag(x);</div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> </div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> sprintf(msg, <span class="stringliteral">&quot;% 7.04g%+7.04gj&quot;</span>, r, c);</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> </div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> <span class="keywordflow">return</span> msg;</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="a3c04138a5bfe5d72780bb7e82a18e627"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">&#9670;&nbsp;</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&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char **&#160;</td>
<td class="paramname"><em>argv</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p >store intermediate values to a CSV file</p>
<div class="fragment"><div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span>{</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> *coeffs = NULL;</div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex *s0 = NULL;</div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"> 99</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degree = 0;</div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno"> 100</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n, i;</div>
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno"> 101</span> </div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno"> 102</span> <span class="keywordflow">if</span> (argc &lt; 2)</div>
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno"> 103</span> {</div>
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno"> 104</span> printf(</div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno"> 105</span> <span class="stringliteral">&quot;Please pass the coefficients of the polynomial as commandline &quot;</span></div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"> 106</span> <span class="stringliteral">&quot;arguments.\n&quot;</span>);</div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno"> 107</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span> }</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span> </div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span> degree = argc - 1; <span class="comment">/* detected polynomial degree */</span></div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span> coeffs = (<span class="keywordtype">long</span> <span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(</div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</span> degree * <span class="keyword">sizeof</span>(<span class="keywordtype">long</span> <span class="keywordtype">double</span>)); <span class="comment">/* store all input coefficients */</span></div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> s0 = (<span class="keywordtype">long</span> <span class="keywordtype">double</span> complex *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> (degree - 1) *</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> <span class="keyword">sizeof</span>(<span class="keywordtype">long</span> <span class="keywordtype">double</span> complex)); <span class="comment">/* number of roots = degree-1 */</span></div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> </div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span> <span class="comment">/* initialize random seed: */</span></div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> srand(time(NULL));</div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span> </div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span> <span class="keywordflow">if</span> (!coeffs || !s0)</div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span> {</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span> perror(<span class="stringliteral">&quot;Unable to allocate memory!&quot;</span>);</div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span> <span class="keywordflow">if</span> (coeffs)</div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(coeffs);</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> <span class="keywordflow">if</span> (s0)</div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(s0);</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> <span class="keywordflow">return</span> EXIT_FAILURE;</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> }</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> </div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span><span class="comment"></span></div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span><span class="comment"> /**</span></div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span><span class="comment"> * store intermediate values to a CSV file</span></div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span><span class="comment"> */</span></div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> FILE *log_file = fopen(<span class="stringliteral">&quot;durand_kerner.log.csv&quot;</span>, <span class="stringliteral">&quot;wt&quot;</span>);</div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> <span class="keywordflow">if</span> (!log_file)</div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span> {</div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span> perror(<span class="stringliteral">&quot;Unable to create a storage log file!&quot;</span>);</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(coeffs);</div>
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno"> 139</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(s0);</div>
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno"> 140</span> <span class="keywordflow">return</span> EXIT_FAILURE;</div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span> }</div>
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno"> 142</span> fprintf(log_file, <span class="stringliteral">&quot;iter#,&quot;</span>);</div>
<div class="line"><a id="l00143" name="l00143"></a><span class="lineno"> 143</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00144" name="l00144"></a><span class="lineno"> 144</span> </div>
<div class="line"><a id="l00145" name="l00145"></a><span class="lineno"> 145</span> printf(<span class="stringliteral">&quot;Computing the roots for:\n\t&quot;</span>);</div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span> <span class="keywordflow">for</span> (n = 0; n &lt; degree; n++)</div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span> {</div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span> coeffs[n] = strtod(argv[n + 1], NULL);</div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span> <span class="keywordflow">if</span> (n &lt; degree - 1 &amp;&amp; coeffs[n] != 0)</div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> printf(<span class="stringliteral">&quot;(%Lg) x^%d + &quot;</span>, coeffs[n], degree - n - 1);</div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (coeffs[n] != 0)</div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> printf(<span class="stringliteral">&quot;(%Lg) x^%d = 0\n&quot;</span>, coeffs[n], degree - n - 1);</div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> </div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> <span class="keywordtype">double</span> tmp;</div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span> <span class="keywordflow">if</span> (n &gt; 0)</div>
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span> coeffs[n] /= tmp; <span class="comment">/* numerical errors less when the first</span></div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span><span class="comment"> coefficient is &quot;1&quot; */</span></div>
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> <span class="keywordflow">else</span></div>
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> {</div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> tmp = coeffs[0];</div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> coeffs[0] = 1;</div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> }</div>
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> </div>
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span> <span class="comment">/* initialize root approximations with random values */</span></div>
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span> <span class="keywordflow">if</span> (n &lt; degree - 1)</div>
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> {</div>
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span> s0[n] = (<span class="keywordtype">long</span> double)rand() + (<span class="keywordtype">long</span> double)rand() * I;</div>
<div class="line"><a id="l00168" name="l00168"></a><span class="lineno"> 168</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a id="l00169" name="l00169"></a><span class="lineno"> 169</span> fprintf(log_file, <span class="stringliteral">&quot;root_%d,&quot;</span>, n);</div>
<div class="line"><a id="l00170" name="l00170"></a><span class="lineno"> 170</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00171" name="l00171"></a><span class="lineno"> 171</span> }</div>
<div class="line"><a id="l00172" name="l00172"></a><span class="lineno"> 172</span> }</div>
<div class="line"><a id="l00173" name="l00173"></a><span class="lineno"> 173</span> </div>
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span> fprintf(log_file, <span class="stringliteral">&quot;avg. correction&quot;</span>);</div>
<div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span> fprintf(log_file, <span class="stringliteral">&quot;\n0,&quot;</span>);</div>
<div class="line"><a id="l00177" name="l00177"></a><span class="lineno"> 177</span> <span class="keywordflow">for</span> (n = 0; n &lt; degree - 1; n++)</div>
<div class="line"><a id="l00178" name="l00178"></a><span class="lineno"> 178</span> fprintf(log_file, <span class="stringliteral">&quot;%s,&quot;</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
<div class="line"><a id="l00179" name="l00179"></a><span class="lineno"> 179</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00180" name="l00180"></a><span class="lineno"> 180</span> </div>
<div class="line"><a id="l00181" name="l00181"></a><span class="lineno"> 181</span> <span class="keywordtype">double</span> tol_condition = 1;</div>
<div class="line"><a id="l00182" name="l00182"></a><span class="lineno"> 182</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> iter = 0;</div>
<div class="line"><a id="l00183" name="l00183"></a><span class="lineno"> 183</span> </div>
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"> 184</span> clock_t end_time, start_time = clock();</div>
<div class="line"><a id="l00185" name="l00185"></a><span class="lineno"> 185</span> <span class="keywordflow">while</span> (!<a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a26d5695ebed0818a3e7cf4b10aacab52">check_termination</a>(tol_condition) &amp;&amp; iter &lt; INT_MAX)</div>
<div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span> {</div>
<div class="line"><a id="l00187" name="l00187"></a><span class="lineno"> 187</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex delta = 0;</div>
<div class="line"><a id="l00188" name="l00188"></a><span class="lineno"> 188</span> tol_condition = 0;</div>
<div class="line"><a id="l00189" name="l00189"></a><span class="lineno"> 189</span> iter++;</div>
<div class="line"><a id="l00190" name="l00190"></a><span class="lineno"> 190</span> </div>
<div class="line"><a id="l00191" name="l00191"></a><span class="lineno"> 191</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a id="l00192" name="l00192"></a><span class="lineno"> 192</span> fprintf(log_file, <span class="stringliteral">&quot;\n%ld,&quot;</span>, iter);</div>
<div class="line"><a id="l00193" name="l00193"></a><span class="lineno"> 193</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00194" name="l00194"></a><span class="lineno"> 194</span> </div>
<div class="line"><a id="l00195" name="l00195"></a><span class="lineno"> 195</span> <span class="keywordflow">for</span> (n = 0; n &lt; degree - 1; n++)</div>
<div class="line"><a id="l00196" name="l00196"></a><span class="lineno"> 196</span> {</div>
<div class="line"><a id="l00197" name="l00197"></a><span class="lineno"> 197</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex numerator =</div>
<div class="line"><a id="l00198" name="l00198"></a><span class="lineno"> 198</span> <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a321f9781a9744ccdaf0aba89f35ec29c">poly_function</a>(coeffs, degree, s0[n]);</div>
<div class="line"><a id="l00199" name="l00199"></a><span class="lineno"> 199</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex denominator = 1.0;</div>
<div class="line"><a id="l00200" name="l00200"></a><span class="lineno"> 200</span> <span class="keywordflow">for</span> (i = 0; i &lt; degree - 1; i++)</div>
<div class="line"><a id="l00201" name="l00201"></a><span class="lineno"> 201</span> <span class="keywordflow">if</span> (i != n)</div>
<div class="line"><a id="l00202" name="l00202"></a><span class="lineno"> 202</span> denominator *= s0[n] - s0[i];</div>
<div class="line"><a id="l00203" name="l00203"></a><span class="lineno"> 203</span> </div>
<div class="line"><a id="l00204" name="l00204"></a><span class="lineno"> 204</span> delta = numerator / denominator;</div>
<div class="line"><a id="l00205" name="l00205"></a><span class="lineno"> 205</span> </div>
<div class="line"><a id="l00206" name="l00206"></a><span class="lineno"> 206</span> <span class="keywordflow">if</span> (isnan(cabsl(delta)) || isinf(cabsl(delta)))</div>
<div class="line"><a id="l00207" name="l00207"></a><span class="lineno"> 207</span> {</div>
<div class="line"><a id="l00208" name="l00208"></a><span class="lineno"> 208</span> printf(<span class="stringliteral">&quot;\n\nOverflow/underrun error - got value = %Lg&quot;</span>,</div>
<div class="line"><a id="l00209" name="l00209"></a><span class="lineno"> 209</span> cabsl(delta));</div>
<div class="line"><a id="l00210" name="l00210"></a><span class="lineno"> 210</span> <span class="keywordflow">goto</span> end;</div>
<div class="line"><a id="l00211" name="l00211"></a><span class="lineno"> 211</span> }</div>
<div class="line"><a id="l00212" name="l00212"></a><span class="lineno"> 212</span> </div>
<div class="line"><a id="l00213" name="l00213"></a><span class="lineno"> 213</span> s0[n] -= delta;</div>
<div class="line"><a id="l00214" name="l00214"></a><span class="lineno"> 214</span> </div>
<div class="line"><a id="l00215" name="l00215"></a><span class="lineno"> 215</span> tol_condition = fmaxl(tol_condition, fabsl(cabsl(delta)));</div>
<div class="line"><a id="l00216" name="l00216"></a><span class="lineno"> 216</span> </div>
<div class="line"><a id="l00217" name="l00217"></a><span class="lineno"> 217</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a id="l00218" name="l00218"></a><span class="lineno"> 218</span> fprintf(log_file, <span class="stringliteral">&quot;%s,&quot;</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
<div class="line"><a id="l00219" name="l00219"></a><span class="lineno"> 219</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00220" name="l00220"></a><span class="lineno"> 220</span> }</div>
<div class="line"><a id="l00221" name="l00221"></a><span class="lineno"> 221</span> <span class="comment">// tol_condition /= (degree - 1);</span></div>
<div class="line"><a id="l00222" name="l00222"></a><span class="lineno"> 222</span> </div>
<div class="line"><a id="l00223" name="l00223"></a><span class="lineno"> 223</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a id="l00224" name="l00224"></a><span class="lineno"> 224</span> <span class="keywordflow">if</span> (iter % 500 == 0)</div>
<div class="line"><a id="l00225" name="l00225"></a><span class="lineno"> 225</span> {</div>
<div class="line"><a id="l00226" name="l00226"></a><span class="lineno"> 226</span> printf(<span class="stringliteral">&quot;Iter: %lu\t&quot;</span>, iter);</div>
<div class="line"><a id="l00227" name="l00227"></a><span class="lineno"> 227</span> <span class="keywordflow">for</span> (n = 0; n &lt; degree - 1; n++) printf(<span class="stringliteral">&quot;\t%s&quot;</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
<div class="line"><a id="l00228" name="l00228"></a><span class="lineno"> 228</span> printf(<span class="stringliteral">&quot;\t\tabsolute average change: %.4g\n&quot;</span>, tol_condition);</div>
<div class="line"><a id="l00229" name="l00229"></a><span class="lineno"> 229</span> }</div>
<div class="line"><a id="l00230" name="l00230"></a><span class="lineno"> 230</span> </div>
<div class="line"><a id="l00231" name="l00231"></a><span class="lineno"> 231</span> fprintf(log_file, <span class="stringliteral">&quot;%.4g&quot;</span>, tol_condition);</div>
<div class="line"><a id="l00232" name="l00232"></a><span class="lineno"> 232</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00233" name="l00233"></a><span class="lineno"> 233</span> }</div>
<div class="line"><a id="l00234" name="l00234"></a><span class="lineno"> 234</span>end:</div>
<div class="line"><a id="l00235" name="l00235"></a><span class="lineno"> 235</span> </div>
<div class="line"><a id="l00236" name="l00236"></a><span class="lineno"> 236</span> end_time = clock();</div>
<div class="line"><a id="l00237" name="l00237"></a><span class="lineno"> 237</span> </div>
<div class="line"><a id="l00238" name="l00238"></a><span class="lineno"> 238</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a id="l00239" name="l00239"></a><span class="lineno"> 239</span> fclose(log_file);</div>
<div class="line"><a id="l00240" name="l00240"></a><span class="lineno"> 240</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00241" name="l00241"></a><span class="lineno"> 241</span> </div>
<div class="line"><a id="l00242" name="l00242"></a><span class="lineno"> 242</span> printf(<span class="stringliteral">&quot;\nIterations: %lu\n&quot;</span>, iter);</div>
<div class="line"><a id="l00243" name="l00243"></a><span class="lineno"> 243</span> <span class="keywordflow">for</span> (n = 0; n &lt; degree - 1; n++) printf(<span class="stringliteral">&quot;\t%s\n&quot;</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
<div class="line"><a id="l00244" name="l00244"></a><span class="lineno"> 244</span> printf(<span class="stringliteral">&quot;absolute average change: %.4g\n&quot;</span>, tol_condition);</div>
<div class="line"><a id="l00245" name="l00245"></a><span class="lineno"> 245</span> printf(<span class="stringliteral">&quot;Time taken: %.4g sec\n&quot;</span>,</div>
<div class="line"><a id="l00246" name="l00246"></a><span class="lineno"> 246</span> (end_time - start_time) / (<span class="keywordtype">double</span>)CLOCKS_PER_SEC);</div>
<div class="line"><a id="l00247" name="l00247"></a><span class="lineno"> 247</span> </div>
<div class="line"><a id="l00248" name="l00248"></a><span class="lineno"> 248</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(coeffs);</div>
<div class="line"><a id="l00249" name="l00249"></a><span class="lineno"> 249</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(s0);</div>
<div class="line"><a id="l00250" name="l00250"></a><span class="lineno"> 250</span> </div>
<div class="line"><a id="l00251" name="l00251"></a><span class="lineno"> 251</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00252" name="l00252"></a><span class="lineno"> 252</span>}</div>
<div class="ttc" id="adurand__kerner__roots_8c_html_a26d5695ebed0818a3e7cf4b10aacab52"><div class="ttname"><a href="../../da/d38/durand__kerner__roots_8c.html#a26d5695ebed0818a3e7cf4b10aacab52">check_termination</a></div><div class="ttdeci">char check_termination(long double delta)</div><div class="ttdoc">check for termination condition</div><div class="ttdef"><b>Definition:</b> durand_kerner_roots.c:83</div></div>
<div class="ttc" id="adurand__kerner__roots_8c_html_a321f9781a9744ccdaf0aba89f35ec29c"><div class="ttname"><a href="../../da/d38/durand__kerner__roots_8c.html#a321f9781a9744ccdaf0aba89f35ec29c">poly_function</a></div><div class="ttdeci">long double complex poly_function(long double *coeffs, unsigned int degree, long double complex x)</div><div class="ttdoc">Evaluate the value of a polynomial with given coefficients.</div><div class="ttdef"><b>Definition:</b> durand_kerner_roots.c:50</div></div>
<div class="ttc" id="adurand__kerner__roots_8c_html_a46197e91f478e99a2229562aaaac311e"><div class="ttname"><a href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a></div><div class="ttdeci">const char * complex_str(long double complex x)</div><div class="ttdoc">create a textual form of complex number</div><div class="ttdef"><b>Definition:</b> durand_kerner_roots.c:66</div></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><!-- 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="../../da/d38/durand__kerner__roots_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="238" height="139"><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="a321f9781a9744ccdaf0aba89f35ec29c" name="a321f9781a9744ccdaf0aba89f35ec29c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a321f9781a9744ccdaf0aba89f35ec29c">&#9670;&nbsp;</a></span>poly_function()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">long double complex poly_function </td>
<td>(</td>
<td class="paramtype">long double *&#160;</td>
<td class="paramname"><em>coeffs</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">unsigned int&#160;</td>
<td class="paramname"><em>degree</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">long double complex&#160;</td>
<td class="paramname"><em>x</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Evaluate the value of a polynomial with given coefficients. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">coeffs</td><td>coefficients of the polynomial </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">degree</td><td>degree of polynomial </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>point at which to evaluate the polynomial </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>\(f(x)\) </dd></dl>
<div class="fragment"><div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span>{</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex out = 0.;</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n;</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> </div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> <span class="keywordflow">for</span> (n = 0; n &lt; degree; n++) out += coeffs[n] * cpow(x, degree - n - 1);</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span> </div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> <span class="keywordflow">return</span> out;</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span>}</div>
</div><!-- fragment -->
</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="../../da/d38/durand__kerner__roots_8c.html">durand_kerner_roots.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.2 </li>
</ul>
</div>
</body>
</html>