TheAlgorithms-C/da/d38/durand__kerner__roots_8c.html
2020-08-26 16:14:44 +00:00

486 lines
42 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=9"/>
<meta name="generator" content="Doxygen 1.8.19"/>
<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
&#160;<span id="projectnumber">1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.19 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('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">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 name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:af270a96662132d0385cb6b4637c5a689"><td class="memItemLeft" align="right" valign="top"><a id="af270a96662132d0385cb6b4637c5a689"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a>&#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 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:afa5b04ce11475d67049cba8273741fb7"><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#afa5b04ce11475d67049cba8273741fb7">complex_str</a> (long double complex x)</td></tr>
<tr class="memdesc:afa5b04ce11475d67049cba8273741fb7"><td class="mdescLeft">&#160;</td><td class="mdescRight">create a textual form of complex number <a href="../../da/d38/durand__kerner__roots_8c.html#afa5b04ce11475d67049cba8273741fb7">More...</a><br /></td></tr>
<tr class="separator:afa5b04ce11475d67049cba8273741fb7"><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">Durand Kerner algorithm</a> </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">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"></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 name="l00084"></a><span class="lineno"> 84</span>&#160;{</div>
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; <span class="keyword">static</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> past_delta = INFINITY;</div>
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; <span class="keywordflow">if</span> (fabsl(past_delta - delta) &lt;= <a class="code" href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a> || delta &lt; <a class="code" href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a>)</div>
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; <span class="keywordflow">return</span> 1;</div>
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160; past_delta = delta;</div>
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160; <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="afa5b04ce11475d67049cba8273741fb7"></a>
<h2 class="memtitle"><span class="permalink"><a href="#afa5b04ce11475d67049cba8273741fb7">&#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 name="l00067"></a><span class="lineno"> 67</span>&#160;{</div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; <span class="keyword">static</span> <span class="keywordtype">char</span> msg[50];</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; <span class="keywordtype">double</span> r = creal(x);</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; <span class="keywordtype">double</span> c = cimag(x);</div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; </div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; sprintf(msg, <span class="stringliteral">&quot;% 7.04g%+7.04gj&quot;</span>, r, c);</div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; </div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; <span class="keywordflow">return</span> msg;</div>
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="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 name="l00096"></a><span class="lineno"> 96</span>&#160;{</div>
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160; <span class="keywordtype">long</span> <span class="keywordtype">double</span> *coeffs = NULL;</div>
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160; <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex *s0 = NULL;</div>
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>&#160; <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degree = 0;</div>
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160; <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n, i;</div>
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>&#160; </div>
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160; <span class="keywordflow">if</span> (argc &lt; 2)</div>
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>&#160; {</div>
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160; printf(</div>
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160; <span class="stringliteral">&quot;Please pass the coefficients of the polynomial as commandline &quot;</span></div>
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160; <span class="stringliteral">&quot;arguments.\n&quot;</span>);</div>
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160; <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160; }</div>
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160; </div>
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160; degree = argc - 1; <span class="comment">/* detected polynomial degree */</span></div>
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>&#160; coeffs = (<span class="keywordtype">long</span> <span class="keywordtype">double</span> *)malloc(</div>
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160; 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 name="l00113"></a><span class="lineno"> 113</span>&#160; s0 = (<span class="keywordtype">long</span> <span class="keywordtype">double</span> complex *)malloc(</div>
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160; (degree - 1) *</div>
<div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160; <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 name="l00116"></a><span class="lineno"> 116</span>&#160; </div>
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; <span class="comment">/* initialize random seed: */</span></div>
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; srand(time(NULL));</div>
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>&#160; </div>
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>&#160; <span class="keywordflow">if</span> (!coeffs || !s0)</div>
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>&#160; {</div>
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>&#160; perror(<span class="stringliteral">&quot;Unable to allocate memory!&quot;</span>);</div>
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span>&#160; <span class="keywordflow">if</span> (coeffs)</div>
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160; free(coeffs);</div>
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160; <span class="keywordflow">if</span> (s0)</div>
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span>&#160; free(s0);</div>
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160; <span class="keywordflow">return</span> EXIT_FAILURE;</div>
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>&#160; }</div>
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>&#160; </div>
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160;<span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span>&#160;<span class="comment"> /**</span></div>
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>&#160;<span class="comment"> * store intermediate values to a CSV file</span></div>
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>&#160; 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 name="l00135"></a><span class="lineno"> 135</span>&#160; <span class="keywordflow">if</span> (!log_file)</div>
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>&#160; {</div>
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span>&#160; perror(<span class="stringliteral">&quot;Unable to create a storage log file!&quot;</span>);</div>
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>&#160; free(coeffs);</div>
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>&#160; free(s0);</div>
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>&#160; <span class="keywordflow">return</span> EXIT_FAILURE;</div>
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>&#160; }</div>
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;iter#,&quot;</span>);</div>
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>&#160; </div>
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160; printf(<span class="stringliteral">&quot;Computing the roots for:\n\t&quot;</span>);</div>
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160; <span class="keywordflow">for</span> (n = 0; n &lt; degree; n++)</div>
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160; {</div>
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>&#160; coeffs[n] = strtod(argv[n + 1], NULL);</div>
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>&#160; <span class="keywordflow">if</span> (n &lt; degree - 1 &amp;&amp; coeffs[n] != 0)</div>
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>&#160; printf(<span class="stringliteral">&quot;(%Lg) x^%d + &quot;</span>, coeffs[n], degree - n - 1);</div>
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (coeffs[n] != 0)</div>
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>&#160; printf(<span class="stringliteral">&quot;(%Lg) x^%d = 0\n&quot;</span>, coeffs[n], degree - n - 1);</div>
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span>&#160; </div>
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>&#160; <span class="keywordtype">double</span> tmp;</div>
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>&#160; <span class="keywordflow">if</span> (n &gt; 0)</div>
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>&#160; coeffs[n] /= tmp; <span class="comment">/* numerical errors less when the first</span></div>
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>&#160;<span class="comment"> coefficient is &quot;1&quot; */</span></div>
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>&#160; {</div>
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160; tmp = coeffs[0];</div>
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160; coeffs[0] = 1;</div>
<div class="line"><a name="l00162"></a><span class="lineno"> 162</span>&#160; }</div>
<div class="line"><a name="l00163"></a><span class="lineno"> 163</span>&#160; </div>
<div class="line"><a name="l00164"></a><span class="lineno"> 164</span>&#160; <span class="comment">/* initialize root approximations with random values */</span></div>
<div class="line"><a name="l00165"></a><span class="lineno"> 165</span>&#160; <span class="keywordflow">if</span> (n &lt; degree - 1)</div>
<div class="line"><a name="l00166"></a><span class="lineno"> 166</span>&#160; {</div>
<div class="line"><a name="l00167"></a><span class="lineno"> 167</span>&#160; s0[n] = (<span class="keywordtype">long</span> double)rand() + (<span class="keywordtype">long</span> double)rand() * I;</div>
<div class="line"><a name="l00168"></a><span class="lineno"> 168</span>&#160;<span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a name="l00169"></a><span class="lineno"> 169</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;root_%d,&quot;</span>, n);</div>
<div class="line"><a name="l00170"></a><span class="lineno"> 170</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00171"></a><span class="lineno"> 171</span>&#160; }</div>
<div class="line"><a name="l00172"></a><span class="lineno"> 172</span>&#160; }</div>
<div class="line"><a name="l00173"></a><span class="lineno"> 173</span>&#160; </div>
<div class="line"><a name="l00174"></a><span class="lineno"> 174</span>&#160;<span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;avg. correction&quot;</span>);</div>
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;\n0,&quot;</span>);</div>
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>&#160; <span class="keywordflow">for</span> (n = 0; n &lt; degree - 1; n++)</div>
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;%s,&quot;</span>, <a class="code" href="../../da/d38/durand__kerner__roots_8c.html#afa5b04ce11475d67049cba8273741fb7">complex_str</a>(s0[n]));</div>
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>&#160; </div>
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>&#160; <span class="keywordtype">double</span> tol_condition = 1;</div>
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>&#160; <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> iter = 0;</div>
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>&#160; </div>
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>&#160; clock_t end_time, start_time = clock();</div>
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>&#160; <span class="keywordflow">while</span> (!<a class="code" 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 name="l00186"></a><span class="lineno"> 186</span>&#160; {</div>
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>&#160; <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex delta = 0;</div>
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>&#160; tol_condition = 0;</div>
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>&#160; iter++;</div>
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>&#160; </div>
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>&#160;<span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;\n%ld,&quot;</span>, iter);</div>
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>&#160; </div>
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>&#160; <span class="keywordflow">for</span> (n = 0; n &lt; degree - 1; n++)</div>
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>&#160; {</div>
<div class="line"><a name="l00197"></a><span class="lineno"> 197</span>&#160; <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex numerator =</div>
<div class="line"><a name="l00198"></a><span class="lineno"> 198</span>&#160; <a class="code" href="../../da/d38/durand__kerner__roots_8c.html#a321f9781a9744ccdaf0aba89f35ec29c">poly_function</a>(coeffs, degree, s0[n]);</div>
<div class="line"><a name="l00199"></a><span class="lineno"> 199</span>&#160; <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex denominator = 1.0;</div>
<div class="line"><a name="l00200"></a><span class="lineno"> 200</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; degree - 1; i++)</div>
<div class="line"><a name="l00201"></a><span class="lineno"> 201</span>&#160; <span class="keywordflow">if</span> (i != n)</div>
<div class="line"><a name="l00202"></a><span class="lineno"> 202</span>&#160; denominator *= s0[n] - s0[i];</div>
<div class="line"><a name="l00203"></a><span class="lineno"> 203</span>&#160; </div>
<div class="line"><a name="l00204"></a><span class="lineno"> 204</span>&#160; delta = numerator / denominator;</div>
<div class="line"><a name="l00205"></a><span class="lineno"> 205</span>&#160; </div>
<div class="line"><a name="l00206"></a><span class="lineno"> 206</span>&#160; <span class="keywordflow">if</span> (isnan(cabsl(delta)) || isinf(cabsl(delta)))</div>
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>&#160; {</div>
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>&#160; printf(<span class="stringliteral">&quot;\n\nOverflow/underrun error - got value = %Lg&quot;</span>,</div>
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>&#160; cabsl(delta));</div>
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>&#160; <span class="keywordflow">goto</span> end;</div>
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>&#160; }</div>
<div class="line"><a name="l00212"></a><span class="lineno"> 212</span>&#160; </div>
<div class="line"><a name="l00213"></a><span class="lineno"> 213</span>&#160; s0[n] -= delta;</div>
<div class="line"><a name="l00214"></a><span class="lineno"> 214</span>&#160; </div>
<div class="line"><a name="l00215"></a><span class="lineno"> 215</span>&#160; tol_condition = fmaxl(tol_condition, fabsl(cabsl(delta)));</div>
<div class="line"><a name="l00216"></a><span class="lineno"> 216</span>&#160; </div>
<div class="line"><a name="l00217"></a><span class="lineno"> 217</span>&#160;<span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a name="l00218"></a><span class="lineno"> 218</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;%s,&quot;</span>, <a class="code" href="../../da/d38/durand__kerner__roots_8c.html#afa5b04ce11475d67049cba8273741fb7">complex_str</a>(s0[n]));</div>
<div class="line"><a name="l00219"></a><span class="lineno"> 219</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00220"></a><span class="lineno"> 220</span>&#160; }</div>
<div class="line"><a name="l00221"></a><span class="lineno"> 221</span>&#160; <span class="comment">// tol_condition /= (degree - 1);</span></div>
<div class="line"><a name="l00222"></a><span class="lineno"> 222</span>&#160; </div>
<div class="line"><a name="l00223"></a><span class="lineno"> 223</span>&#160;<span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a name="l00224"></a><span class="lineno"> 224</span>&#160; <span class="keywordflow">if</span> (iter % 500 == 0)</div>
<div class="line"><a name="l00225"></a><span class="lineno"> 225</span>&#160; {</div>
<div class="line"><a name="l00226"></a><span class="lineno"> 226</span>&#160; printf(<span class="stringliteral">&quot;Iter: %lu\t&quot;</span>, iter);</div>
<div class="line"><a name="l00227"></a><span class="lineno"> 227</span>&#160; <span class="keywordflow">for</span> (n = 0; n &lt; degree - 1; n++) printf(<span class="stringliteral">&quot;\t%s&quot;</span>, <a class="code" href="../../da/d38/durand__kerner__roots_8c.html#afa5b04ce11475d67049cba8273741fb7">complex_str</a>(s0[n]));</div>
<div class="line"><a name="l00228"></a><span class="lineno"> 228</span>&#160; printf(<span class="stringliteral">&quot;\t\tabsolute average change: %.4g\n&quot;</span>, tol_condition);</div>
<div class="line"><a name="l00229"></a><span class="lineno"> 229</span>&#160; }</div>
<div class="line"><a name="l00230"></a><span class="lineno"> 230</span>&#160; </div>
<div class="line"><a name="l00231"></a><span class="lineno"> 231</span>&#160; fprintf(log_file, <span class="stringliteral">&quot;%.4g&quot;</span>, tol_condition);</div>
<div class="line"><a name="l00232"></a><span class="lineno"> 232</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00233"></a><span class="lineno"> 233</span>&#160; }</div>
<div class="line"><a name="l00234"></a><span class="lineno"> 234</span>&#160;end:</div>
<div class="line"><a name="l00235"></a><span class="lineno"> 235</span>&#160; </div>
<div class="line"><a name="l00236"></a><span class="lineno"> 236</span>&#160; end_time = clock();</div>
<div class="line"><a name="l00237"></a><span class="lineno"> 237</span>&#160; </div>
<div class="line"><a name="l00238"></a><span class="lineno"> 238</span>&#160;<span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><a name="l00239"></a><span class="lineno"> 239</span>&#160; fclose(log_file);</div>
<div class="line"><a name="l00240"></a><span class="lineno"> 240</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00241"></a><span class="lineno"> 241</span>&#160; </div>
<div class="line"><a name="l00242"></a><span class="lineno"> 242</span>&#160; printf(<span class="stringliteral">&quot;\nIterations: %lu\n&quot;</span>, iter);</div>
<div class="line"><a name="l00243"></a><span class="lineno"> 243</span>&#160; <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" href="../../da/d38/durand__kerner__roots_8c.html#afa5b04ce11475d67049cba8273741fb7">complex_str</a>(s0[n]));</div>
<div class="line"><a name="l00244"></a><span class="lineno"> 244</span>&#160; printf(<span class="stringliteral">&quot;absolute average change: %.4g\n&quot;</span>, tol_condition);</div>
<div class="line"><a name="l00245"></a><span class="lineno"> 245</span>&#160; printf(<span class="stringliteral">&quot;Time taken: %.4g sec\n&quot;</span>,</div>
<div class="line"><a name="l00246"></a><span class="lineno"> 246</span>&#160; (end_time - start_time) / (<span class="keywordtype">double</span>)CLOCKS_PER_SEC);</div>
<div class="line"><a name="l00247"></a><span class="lineno"> 247</span>&#160; </div>
<div class="line"><a name="l00248"></a><span class="lineno"> 248</span>&#160; free(coeffs);</div>
<div class="line"><a name="l00249"></a><span class="lineno"> 249</span>&#160; free(s0);</div>
<div class="line"><a name="l00250"></a><span class="lineno"> 250</span>&#160; </div>
<div class="line"><a name="l00251"></a><span class="lineno"> 251</span>&#160; <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00252"></a><span class="lineno"> 252</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../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"></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 name="l00052"></a><span class="lineno"> 52</span>&#160;{</div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex out = 0.;</div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n;</div>
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; </div>
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160; <span class="keywordflow">for</span> (n = 0; n &lt; degree; n++) out += coeffs[n] * cpow(x, degree - n - 1);</div>
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; </div>
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160; <span class="keywordflow">return</span> out;</div>
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div class="ttc" id="adurand__kerner__roots_8c_html_afa5b04ce11475d67049cba8273741fb7"><div class="ttname"><a href="../../da/d38/durand__kerner__roots_8c.html#afa5b04ce11475d67049cba8273741fb7">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="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_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>
<!-- 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="http://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.19 </li>
</ul>
</div>
</body>
</html>