mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
492 lines
39 KiB
HTML
492 lines
39 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/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 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('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">
|
|
<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">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 <complex.h></code><br />
|
|
<code>#include <limits.h></code><br />
|
|
<code>#include <math.h></code><br />
|
|
<code>#include <stdio.h></code><br />
|
|
<code>#include <stdlib.h></code><br />
|
|
<code>#include <string.h></code><br />
|
|
<code>#include <time.h></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="607" height="126"><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" id="r_af270a96662132d0385cb6b4637c5a689"><td class="memItemLeft" align="right" valign="top"><a id="af270a96662132d0385cb6b4637c5a689" name="af270a96662132d0385cb6b4637c5a689"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>ACCURACY</b>   1e-10</td></tr>
|
|
<tr class="memdesc:af270a96662132d0385cb6b4637c5a689"><td class="mdescLeft"> </td><td class="mdescRight">maximum accuracy limit <br /></td></tr>
|
|
<tr class="separator:af270a96662132d0385cb6b4637c5a689"><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:a321f9781a9744ccdaf0aba89f35ec29c" id="r_a321f9781a9744ccdaf0aba89f35ec29c"><td class="memItemLeft" align="right" valign="top">long double complex </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"> </td><td class="mdescRight">Evaluate the value of a polynomial with given coefficients. <br /></td></tr>
|
|
<tr class="separator:a321f9781a9744ccdaf0aba89f35ec29c"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a46197e91f478e99a2229562aaaac311e" id="r_a46197e91f478e99a2229562aaaac311e"><td class="memItemLeft" align="right" valign="top">const char * </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"> </td><td class="mdescRight">create a textual form of complex number <br /></td></tr>
|
|
<tr class="separator:a46197e91f478e99a2229562aaaac311e"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a26d5695ebed0818a3e7cf4b10aacab52" id="r_a26d5695ebed0818a3e7cf4b10aacab52"><td class="memItemLeft" align="right" valign="top">char </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"> </td><td class="mdescRight">check for termination condition <br /></td></tr>
|
|
<tr class="separator:a26d5695ebed0818a3e7cf4b10aacab52"><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="../../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"> </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">◆ </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 </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"><span class="lineno"> 84</span>{</div>
|
|
<div class="line"><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"><span class="lineno"> 86</span> <span class="keywordflow">if</span> (fabsl(past_delta - delta) <= <a class="code hl_define" href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a> || delta < <a class="code hl_define" href="../../da/d38/durand__kerner__roots_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a>)</div>
|
|
<div class="line"><span class="lineno"> 87</span> <span class="keywordflow">return</span> 1;</div>
|
|
<div class="line"><span class="lineno"> 88</span> past_delta = delta;</div>
|
|
<div class="line"><span class="lineno"> 89</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><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">◆ </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 </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"><span class="lineno"> 67</span>{</div>
|
|
<div class="line"><span class="lineno"> 68</span> <span class="keyword">static</span> <span class="keywordtype">char</span> msg[50];</div>
|
|
<div class="line"><span class="lineno"> 69</span> <span class="keywordtype">double</span> r = creal(x);</div>
|
|
<div class="line"><span class="lineno"> 70</span> <span class="keywordtype">double</span> c = cimag(x);</div>
|
|
<div class="line"><span class="lineno"> 71</span> </div>
|
|
<div class="line"><span class="lineno"> 72</span> sprintf(msg, <span class="stringliteral">"% 7.04g%+7.04gj"</span>, r, c);</div>
|
|
<div class="line"><span class="lineno"> 73</span> </div>
|
|
<div class="line"><span class="lineno"> 74</span> <span class="keywordflow">return</span> msg;</div>
|
|
<div class="line"><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">◆ </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>store intermediate values to a CSV file</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 96</span>{</div>
|
|
<div class="line"><span class="lineno"> 97</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> *coeffs = NULL;</div>
|
|
<div class="line"><span class="lineno"> 98</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex *s0 = NULL;</div>
|
|
<div class="line"><span class="lineno"> 99</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degree = 0;</div>
|
|
<div class="line"><span class="lineno"> 100</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n, i;</div>
|
|
<div class="line"><span class="lineno"> 101</span> </div>
|
|
<div class="line"><span class="lineno"> 102</span> <span class="keywordflow">if</span> (argc < 2)</div>
|
|
<div class="line"><span class="lineno"> 103</span> {</div>
|
|
<div class="line"><span class="lineno"> 104</span> printf(</div>
|
|
<div class="line"><span class="lineno"> 105</span> <span class="stringliteral">"Please pass the coefficients of the polynomial as commandline "</span></div>
|
|
<div class="line"><span class="lineno"> 106</span> <span class="stringliteral">"arguments.\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 107</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 108</span> }</div>
|
|
<div class="line"><span class="lineno"> 109</span> </div>
|
|
<div class="line"><span class="lineno"> 110</span> degree = argc - 1; <span class="comment">/* detected polynomial degree */</span></div>
|
|
<div class="line"><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"><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"><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"><span class="lineno"> 114</span> (degree - 1) *</div>
|
|
<div class="line"><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"><span class="lineno"> 116</span> </div>
|
|
<div class="line"><span class="lineno"> 117</span> <span class="comment">/* initialize random seed: */</span></div>
|
|
<div class="line"><span class="lineno"> 118</span> srand(time(NULL));</div>
|
|
<div class="line"><span class="lineno"> 119</span> </div>
|
|
<div class="line"><span class="lineno"> 120</span> <span class="keywordflow">if</span> (!coeffs || !s0)</div>
|
|
<div class="line"><span class="lineno"> 121</span> {</div>
|
|
<div class="line"><span class="lineno"> 122</span> perror(<span class="stringliteral">"Unable to allocate memory!"</span>);</div>
|
|
<div class="line"><span class="lineno"> 123</span> <span class="keywordflow">if</span> (coeffs)</div>
|
|
<div class="line"><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"><span class="lineno"> 125</span> <span class="keywordflow">if</span> (s0)</div>
|
|
<div class="line"><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"><span class="lineno"> 127</span> <span class="keywordflow">return</span> EXIT_FAILURE;</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="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span><span class="comment"></span></div>
|
|
<div class="line"><span class="lineno"> 131</span><span class="comment"> /**</span></div>
|
|
<div class="line"><span class="lineno"> 132</span><span class="comment"> * store intermediate values to a CSV file</span></div>
|
|
<div class="line"><span class="lineno"> 133</span><span class="comment"> */</span><span class="preprocessor"></span></div>
|
|
<div class="line"><span class="lineno"> 134</span> FILE *log_file = fopen(<span class="stringliteral">"durand_kerner.log.csv"</span>, <span class="stringliteral">"wt"</span>);</div>
|
|
<div class="line"><span class="lineno"> 135</span> <span class="keywordflow">if</span> (!log_file)</div>
|
|
<div class="line"><span class="lineno"> 136</span> {</div>
|
|
<div class="line"><span class="lineno"> 137</span> perror(<span class="stringliteral">"Unable to create a storage log file!"</span>);</div>
|
|
<div class="line"><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"><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"><span class="lineno"> 140</span> <span class="keywordflow">return</span> EXIT_FAILURE;</div>
|
|
<div class="line"><span class="lineno"> 141</span> }</div>
|
|
<div class="line"><span class="lineno"> 142</span> fprintf(log_file, <span class="stringliteral">"iter#,"</span>);</div>
|
|
<div class="line"><span class="lineno"> 143</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 144</span> </div>
|
|
<div class="line"><span class="lineno"> 145</span> printf(<span class="stringliteral">"Computing the roots for:\n\t"</span>);</div>
|
|
<div class="line"><span class="lineno"> 146</span> <span class="keywordflow">for</span> (n = 0; n < degree; n++)</div>
|
|
<div class="line"><span class="lineno"> 147</span> {</div>
|
|
<div class="line"><span class="lineno"> 148</span> coeffs[n] = strtod(argv[n + 1], NULL);</div>
|
|
<div class="line"><span class="lineno"> 149</span> <span class="keywordflow">if</span> (n < degree - 1 && coeffs[n] != 0)</div>
|
|
<div class="line"><span class="lineno"> 150</span> printf(<span class="stringliteral">"(%Lg) x^%d + "</span>, coeffs[n], degree - n - 1);</div>
|
|
<div class="line"><span class="lineno"> 151</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (coeffs[n] != 0)</div>
|
|
<div class="line"><span class="lineno"> 152</span> printf(<span class="stringliteral">"(%Lg) x^%d = 0\n"</span>, coeffs[n], degree - n - 1);</div>
|
|
<div class="line"><span class="lineno"> 153</span> </div>
|
|
<div class="line"><span class="lineno"> 154</span> <span class="keywordtype">double</span> tmp;</div>
|
|
<div class="line"><span class="lineno"> 155</span> <span class="keywordflow">if</span> (n > 0)</div>
|
|
<div class="line"><span class="lineno"> 156</span> coeffs[n] /= tmp; <span class="comment">/* numerical errors less when the first</span></div>
|
|
<div class="line"><span class="lineno"> 157</span><span class="comment"> coefficient is "1" */</span></div>
|
|
<div class="line"><span class="lineno"> 158</span> <span class="keywordflow">else</span></div>
|
|
<div class="line"><span class="lineno"> 159</span> {</div>
|
|
<div class="line"><span class="lineno"> 160</span> tmp = coeffs[0];</div>
|
|
<div class="line"><span class="lineno"> 161</span> coeffs[0] = 1;</div>
|
|
<div class="line"><span class="lineno"> 162</span> }</div>
|
|
<div class="line"><span class="lineno"> 163</span> </div>
|
|
<div class="line"><span class="lineno"> 164</span> <span class="comment">/* initialize root approximations with random values */</span></div>
|
|
<div class="line"><span class="lineno"> 165</span> <span class="keywordflow">if</span> (n < degree - 1)</div>
|
|
<div class="line"><span class="lineno"> 166</span> {</div>
|
|
<div class="line"><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"><span class="lineno"> 168</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
|
|
<div class="line"><span class="lineno"> 169</span> fprintf(log_file, <span class="stringliteral">"root_%d,"</span>, n);</div>
|
|
<div class="line"><span class="lineno"> 170</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 171</span> }</div>
|
|
<div class="line"><span class="lineno"> 172</span> }</div>
|
|
<div class="line"><span class="lineno"> 173</span> </div>
|
|
<div class="line"><span class="lineno"> 174</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
|
|
<div class="line"><span class="lineno"> 175</span> fprintf(log_file, <span class="stringliteral">"avg. correction"</span>);</div>
|
|
<div class="line"><span class="lineno"> 176</span> fprintf(log_file, <span class="stringliteral">"\n0,"</span>);</div>
|
|
<div class="line"><span class="lineno"> 177</span> <span class="keywordflow">for</span> (n = 0; n < degree - 1; n++)</div>
|
|
<div class="line"><span class="lineno"> 178</span> fprintf(log_file, <span class="stringliteral">"%s,"</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
|
|
<div class="line"><span class="lineno"> 179</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 180</span> </div>
|
|
<div class="line"><span class="lineno"> 181</span> <span class="keywordtype">double</span> tol_condition = 1;</div>
|
|
<div class="line"><span class="lineno"> 182</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> iter = 0;</div>
|
|
<div class="line"><span class="lineno"> 183</span> </div>
|
|
<div class="line"><span class="lineno"> 184</span> clock_t end_time, start_time = clock();</div>
|
|
<div class="line"><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) && iter < INT_MAX)</div>
|
|
<div class="line"><span class="lineno"> 186</span> {</div>
|
|
<div class="line"><span class="lineno"> 187</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex delta = 0;</div>
|
|
<div class="line"><span class="lineno"> 188</span> tol_condition = 0;</div>
|
|
<div class="line"><span class="lineno"> 189</span> iter++;</div>
|
|
<div class="line"><span class="lineno"> 190</span> </div>
|
|
<div class="line"><span class="lineno"> 191</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
|
|
<div class="line"><span class="lineno"> 192</span> fprintf(log_file, <span class="stringliteral">"\n%ld,"</span>, iter);</div>
|
|
<div class="line"><span class="lineno"> 193</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 194</span> </div>
|
|
<div class="line"><span class="lineno"> 195</span> <span class="keywordflow">for</span> (n = 0; n < degree - 1; n++)</div>
|
|
<div class="line"><span class="lineno"> 196</span> {</div>
|
|
<div class="line"><span class="lineno"> 197</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex numerator =</div>
|
|
<div class="line"><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"><span class="lineno"> 199</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex denominator = 1.0;</div>
|
|
<div class="line"><span class="lineno"> 200</span> <span class="keywordflow">for</span> (i = 0; i < degree - 1; i++)</div>
|
|
<div class="line"><span class="lineno"> 201</span> <span class="keywordflow">if</span> (i != n)</div>
|
|
<div class="line"><span class="lineno"> 202</span> denominator *= s0[n] - s0[i];</div>
|
|
<div class="line"><span class="lineno"> 203</span> </div>
|
|
<div class="line"><span class="lineno"> 204</span> delta = numerator / denominator;</div>
|
|
<div class="line"><span class="lineno"> 205</span> </div>
|
|
<div class="line"><span class="lineno"> 206</span> <span class="keywordflow">if</span> (isnan(cabsl(delta)) || isinf(cabsl(delta)))</div>
|
|
<div class="line"><span class="lineno"> 207</span> {</div>
|
|
<div class="line"><span class="lineno"> 208</span> printf(<span class="stringliteral">"\n\nOverflow/underrun error - got value = %Lg"</span>,</div>
|
|
<div class="line"><span class="lineno"> 209</span> cabsl(delta));</div>
|
|
<div class="line"><span class="lineno"> 210</span> <span class="keywordflow">goto</span> end;</div>
|
|
<div class="line"><span class="lineno"> 211</span> }</div>
|
|
<div class="line"><span class="lineno"> 212</span> </div>
|
|
<div class="line"><span class="lineno"> 213</span> s0[n] -= delta;</div>
|
|
<div class="line"><span class="lineno"> 214</span> </div>
|
|
<div class="line"><span class="lineno"> 215</span> tol_condition = fmaxl(tol_condition, fabsl(cabsl(delta)));</div>
|
|
<div class="line"><span class="lineno"> 216</span> </div>
|
|
<div class="line"><span class="lineno"> 217</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
|
|
<div class="line"><span class="lineno"> 218</span> fprintf(log_file, <span class="stringliteral">"%s,"</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
|
|
<div class="line"><span class="lineno"> 219</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 220</span> }</div>
|
|
<div class="line"><span class="lineno"> 221</span> <span class="comment">// tol_condition /= (degree - 1);</span></div>
|
|
<div class="line"><span class="lineno"> 222</span> </div>
|
|
<div class="line"><span class="lineno"> 223</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
|
|
<div class="line"><span class="lineno"> 224</span> <span class="keywordflow">if</span> (iter % 500 == 0)</div>
|
|
<div class="line"><span class="lineno"> 225</span> {</div>
|
|
<div class="line"><span class="lineno"> 226</span> printf(<span class="stringliteral">"Iter: %lu\t"</span>, iter);</div>
|
|
<div class="line"><span class="lineno"> 227</span> <span class="keywordflow">for</span> (n = 0; n < degree - 1; n++) printf(<span class="stringliteral">"\t%s"</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
|
|
<div class="line"><span class="lineno"> 228</span> printf(<span class="stringliteral">"\t\tabsolute average change: %.4g\n"</span>, tol_condition);</div>
|
|
<div class="line"><span class="lineno"> 229</span> }</div>
|
|
<div class="line"><span class="lineno"> 230</span> </div>
|
|
<div class="line"><span class="lineno"> 231</span> fprintf(log_file, <span class="stringliteral">"%.4g"</span>, tol_condition);</div>
|
|
<div class="line"><span class="lineno"> 232</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 233</span> }</div>
|
|
<div class="line"><span class="lineno"> 234</span>end:</div>
|
|
<div class="line"><span class="lineno"> 235</span> </div>
|
|
<div class="line"><span class="lineno"> 236</span> end_time = clock();</div>
|
|
<div class="line"><span class="lineno"> 237</span> </div>
|
|
<div class="line"><span class="lineno"> 238</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
|
|
<div class="line"><span class="lineno"> 239</span> fclose(log_file);</div>
|
|
<div class="line"><span class="lineno"> 240</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 241</span> </div>
|
|
<div class="line"><span class="lineno"> 242</span> printf(<span class="stringliteral">"\nIterations: %lu\n"</span>, iter);</div>
|
|
<div class="line"><span class="lineno"> 243</span> <span class="keywordflow">for</span> (n = 0; n < degree - 1; n++) printf(<span class="stringliteral">"\t%s\n"</span>, <a class="code hl_function" href="../../da/d38/durand__kerner__roots_8c.html#a46197e91f478e99a2229562aaaac311e">complex_str</a>(s0[n]));</div>
|
|
<div class="line"><span class="lineno"> 244</span> printf(<span class="stringliteral">"absolute average change: %.4g\n"</span>, tol_condition);</div>
|
|
<div class="line"><span class="lineno"> 245</span> printf(<span class="stringliteral">"Time taken: %.4g sec\n"</span>,</div>
|
|
<div class="line"><span class="lineno"> 246</span> (end_time - start_time) / (<span class="keywordtype">double</span>)CLOCKS_PER_SEC);</div>
|
|
<div class="line"><span class="lineno"> 247</span> </div>
|
|
<div class="line"><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"><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"><span class="lineno"> 250</span> </div>
|
|
<div class="line"><span class="lineno"> 251</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><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="236" height="135"><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">◆ </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 * </td>
|
|
<td class="paramname"><em>coeffs</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">unsigned int </td>
|
|
<td class="paramname"><em>degree</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">long double complex </td>
|
|
<td class="paramname"><em>x</em> </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"><span class="lineno"> 52</span>{</div>
|
|
<div class="line"><span class="lineno"> 53</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> complex out = 0.;</div>
|
|
<div class="line"><span class="lineno"> 54</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n;</div>
|
|
<div class="line"><span class="lineno"> 55</span> </div>
|
|
<div class="line"><span class="lineno"> 56</span> <span class="keywordflow">for</span> (n = 0; n < degree; n++) out += coeffs[n] * cpow(x, degree - n - 1);</div>
|
|
<div class="line"><span class="lineno"> 57</span> </div>
|
|
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">return</span> out;</div>
|
|
<div class="line"><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.8 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|