mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-25 14:23:03 +08:00
Documentation for a0227012ec
This commit is contained in:
10
df/db0/quadratic__equations__complex__numbers_8cpp__incl.map
Normal file
10
df/db0/quadratic__equations__complex__numbers_8cpp__incl.map
Normal file
@@ -0,0 +1,10 @@
|
||||
<map id="math/quadratic_equations_complex_numbers.cpp" name="math/quadratic_equations_complex_numbers.cpp">
|
||||
<area shape="rect" id="node1" title="Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0." alt="" coords="200,5,370,46"/>
|
||||
<area shape="rect" id="node2" title=" " alt="" coords="5,94,56,120"/>
|
||||
<area shape="rect" id="node3" title=" " alt="" coords="80,94,144,120"/>
|
||||
<area shape="rect" id="node4" title=" " alt="" coords="169,94,226,120"/>
|
||||
<area shape="rect" id="node5" title=" " alt="" coords="250,94,320,120"/>
|
||||
<area shape="rect" id="node6" title=" " alt="" coords="344,94,421,120"/>
|
||||
<area shape="rect" id="node7" title=" " alt="" coords="445,94,512,120"/>
|
||||
<area shape="rect" id="node8" title=" " alt="" coords="536,94,608,120"/>
|
||||
</map>
|
||||
@@ -0,0 +1 @@
|
||||
cdd96f9d5e8aea79954aa810ef6507b6
|
||||
127
df/db0/quadratic__equations__complex__numbers_8cpp__incl.svg
Normal file
127
df/db0/quadratic__equations__complex__numbers_8cpp__incl.svg
Normal file
@@ -0,0 +1,127 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
||||
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<!-- Generated by graphviz version 8.0.4 (20230421.1958)
|
||||
-->
|
||||
<!-- Title: math/quadratic_equations_complex_numbers.cpp Pages: 1 -->
|
||||
<svg width="460pt" height="94pt"
|
||||
viewBox="0.00 0.00 459.62 93.75" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 89.75)">
|
||||
<title>math/quadratic_equations_complex_numbers.cpp</title>
|
||||
<!-- Node1 -->
|
||||
<g id="node1" class="node">
|
||||
<title>Node1</title>
|
||||
<g id="a_node1"><a xlink:title="Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0.">
|
||||
<polygon fill="#999999" stroke="#666666" points="273.75,-85.75 146,-85.75 146,-55.25 273.75,-55.25 273.75,-85.75"/>
|
||||
<text text-anchor="start" x="154" y="-72.25" font-family="Helvetica,sans-Serif" font-size="10.00">math/quadratic_equations</text>
|
||||
<text text-anchor="middle" x="209.88" y="-61" font-family="Helvetica,sans-Serif" font-size="10.00">_complex_numbers.cpp</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node2 -->
|
||||
<g id="node2" class="node">
|
||||
<title>Node2</title>
|
||||
<g id="a_node2"><a xlink:title=" ">
|
||||
<polygon fill="#e0e0e0" stroke="#999999" points="37.75,-19.25 0,-19.25 0,0 37.75,0 37.75,-19.25"/>
|
||||
<text text-anchor="middle" x="18.88" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">array</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node1->Node2 -->
|
||||
<g id="edge1" class="edge">
|
||||
<title>Node1->Node2</title>
|
||||
<path fill="none" stroke="#63b8ff" d="M162.17,-54.8C126.35,-43.75 78.44,-28.99 48.11,-19.64"/>
|
||||
<polygon fill="#63b8ff" stroke="#63b8ff" points="49.38,-16.06 38.79,-16.46 47.32,-22.75 49.38,-16.06"/>
|
||||
</g>
|
||||
<!-- Node3 -->
|
||||
<g id="node3" class="node">
|
||||
<title>Node3</title>
|
||||
<g id="a_node3"><a xlink:title=" ">
|
||||
<polygon fill="#e0e0e0" stroke="#999999" points="104,-19.25 55.75,-19.25 55.75,0 104,0 104,-19.25"/>
|
||||
<text text-anchor="middle" x="79.88" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">cassert</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node1->Node3 -->
|
||||
<g id="edge2" class="edge">
|
||||
<title>Node1->Node3</title>
|
||||
<path fill="none" stroke="#63b8ff" d="M177.41,-54.8C156.66,-45.4 129.95,-33.3 109.73,-24.14"/>
|
||||
<polygon fill="#63b8ff" stroke="#63b8ff" points="111.36,-20.59 100.81,-19.65 108.48,-26.97 111.36,-20.59"/>
|
||||
</g>
|
||||
<!-- Node4 -->
|
||||
<g id="node4" class="node">
|
||||
<title>Node4</title>
|
||||
<g id="a_node4"><a xlink:title=" ">
|
||||
<polygon fill="#e0e0e0" stroke="#999999" points="165.38,-19.25 122.38,-19.25 122.38,0 165.38,0 165.38,-19.25"/>
|
||||
<text text-anchor="middle" x="143.88" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">cmath</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node1->Node4 -->
|
||||
<g id="edge3" class="edge">
|
||||
<title>Node1->Node4</title>
|
||||
<path fill="none" stroke="#63b8ff" d="M193.56,-54.95C183.99,-46.41 171.89,-35.62 162.02,-26.81"/>
|
||||
<polygon fill="#63b8ff" stroke="#63b8ff" points="164.84,-24.75 155.05,-20.7 160.18,-29.97 164.84,-24.75"/>
|
||||
</g>
|
||||
<!-- Node5 -->
|
||||
<g id="node5" class="node">
|
||||
<title>Node5</title>
|
||||
<g id="a_node5"><a xlink:title=" ">
|
||||
<polygon fill="#e0e0e0" stroke="#999999" points="236.25,-19.25 183.5,-19.25 183.5,0 236.25,0 236.25,-19.25"/>
|
||||
<text text-anchor="middle" x="209.88" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">complex</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node1->Node5 -->
|
||||
<g id="edge4" class="edge">
|
||||
<title>Node1->Node5</title>
|
||||
<path fill="none" stroke="#63b8ff" d="M209.88,-54.95C209.88,-47.62 209.88,-38.62 209.88,-30.66"/>
|
||||
<polygon fill="#63b8ff" stroke="#63b8ff" points="213.38,-30.7 209.88,-20.7 206.38,-30.7 213.38,-30.7"/>
|
||||
</g>
|
||||
<!-- Node6 -->
|
||||
<g id="node6" class="node">
|
||||
<title>Node6</title>
|
||||
<g id="a_node6"><a xlink:title=" ">
|
||||
<polygon fill="#e0e0e0" stroke="#999999" points="311.88,-19.25 253.88,-19.25 253.88,0 311.88,0 311.88,-19.25"/>
|
||||
<text text-anchor="middle" x="282.88" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">exception</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node1->Node6 -->
|
||||
<g id="edge5" class="edge">
|
||||
<title>Node1->Node6</title>
|
||||
<path fill="none" stroke="#63b8ff" d="M227.92,-54.95C238.61,-46.33 252.16,-35.4 263.13,-26.55"/>
|
||||
<polygon fill="#63b8ff" stroke="#63b8ff" points="265.03,-28.7 270.62,-19.7 260.64,-23.25 265.03,-28.7"/>
|
||||
</g>
|
||||
<!-- Node7 -->
|
||||
<g id="node7" class="node">
|
||||
<title>Node7</title>
|
||||
<g id="a_node7"><a xlink:title=" ">
|
||||
<polygon fill="#e0e0e0" stroke="#999999" points="379.75,-19.25 330,-19.25 330,0 379.75,0 379.75,-19.25"/>
|
||||
<text text-anchor="middle" x="354.88" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">iomanip</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node1->Node7 -->
|
||||
<g id="edge6" class="edge">
|
||||
<title>Node1->Node7</title>
|
||||
<path fill="none" stroke="#63b8ff" d="M246.09,-54.8C269.55,-45.27 299.85,-32.97 322.5,-23.77"/>
|
||||
<polygon fill="#63b8ff" stroke="#63b8ff" points="323.69,-26.66 331.64,-19.65 321.06,-20.17 323.69,-26.66"/>
|
||||
</g>
|
||||
<!-- Node8 -->
|
||||
<g id="node8" class="node">
|
||||
<title>Node8</title>
|
||||
<g id="a_node8"><a xlink:title=" ">
|
||||
<polygon fill="#e0e0e0" stroke="#999999" points="451.62,-19.25 398.12,-19.25 398.12,0 451.62,0 451.62,-19.25"/>
|
||||
<text text-anchor="middle" x="424.88" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">iostream</text>
|
||||
</a>
|
||||
</g>
|
||||
</g>
|
||||
<!-- Node1->Node8 -->
|
||||
<g id="edge7" class="edge">
|
||||
<title>Node1->Node8</title>
|
||||
<path fill="none" stroke="#63b8ff" d="M263.57,-54.8C302.07,-44.25 352.97,-30.32 387.2,-20.94"/>
|
||||
<polygon fill="#63b8ff" stroke="#63b8ff" points="387.98,-24.08 396.7,-18.07 386.13,-17.33 387.98,-24.08"/>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.5 KiB |
@@ -0,0 +1,317 @@
|
||||
<!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.6"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>Algorithms_in_C++: /Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp</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.6 -->
|
||||
<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('df/dcd/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2math_2quadratic_equations_complex_numbers_8cpp-example.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="headertitle"><div class="title">/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp</div></div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
<p>int <a class="el" href="../../da/d18/quadratic__equations__complex__numbers_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4" title="Main function.">main()</a> { using <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>; using <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex</a>; using <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a>;</p>
|
||||
<p>array<complex<long double, 2> solutions = quadraticEquation(1, 2, 1); cout << solutions[0] << " " << solutions[1] << "\n";</p>
|
||||
<p>solutions = quadraticEquation(1, 1, 1); // Reusing solutions. cout << solutions[0] << " " << solutions[1] << "\n"; return 0; }</p>
|
||||
<p>Output: (-1, 0) (-1, 0) (-0.5,0.866025) (-0.5,0.866025)</p>
|
||||
<div class="fragment"><div class="line"><span class="comment">/**</span></div>
|
||||
<div class="line"><span class="comment"> * @file</span></div>
|
||||
<div class="line"><span class="comment"> * @brief Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0.</span></div>
|
||||
<div class="line"><span class="comment"> *</span></div>
|
||||
<div class="line"><span class="comment"> * @author [Renjian-buchai](https://github.com/Renjian-buchai)</span></div>
|
||||
<div class="line"><span class="comment"> *</span></div>
|
||||
<div class="line"><span class="comment"> * @description Calculates any quadratic equation in form ax^2 + bx + c.</span></div>
|
||||
<div class="line"><span class="comment"> *</span></div>
|
||||
<div class="line"><span class="comment"> * Quadratic equation:</span></div>
|
||||
<div class="line"><span class="comment"> * x = (-b +/- sqrt(b^2 - 4ac)) / 2a</span></div>
|
||||
<div class="line"><span class="comment"> *</span></div>
|
||||
<div class="line"><span class="comment"> * @example</span></div>
|
||||
<div class="line"><span class="comment"> * int main() {</span></div>
|
||||
<div class="line"><span class="comment"> * using std::array;</span></div>
|
||||
<div class="line"><span class="comment"> * using std::complex;</span></div>
|
||||
<div class="line"><span class="comment"> * using std::cout;</span></div>
|
||||
<div class="line"><span class="comment"> *</span></div>
|
||||
<div class="line"><span class="comment"> * array<complex<long double, 2> solutions = quadraticEquation(1, 2, 1);</span></div>
|
||||
<div class="line"><span class="comment"> * cout << solutions[0] << " " << solutions[1] << "\n";</span></div>
|
||||
<div class="line"><span class="comment"> *</span></div>
|
||||
<div class="line"><span class="comment"> * solutions = quadraticEquation(1, 1, 1); // Reusing solutions.</span></div>
|
||||
<div class="line"><span class="comment"> * cout << solutions[0] << " " << solutions[1] << "\n";</span></div>
|
||||
<div class="line"><span class="comment"> * return 0;</span></div>
|
||||
<div class="line"><span class="comment"> * }</span></div>
|
||||
<div class="line"><span class="comment"> *</span></div>
|
||||
<div class="line"><span class="comment"> * Output:</span></div>
|
||||
<div class="line"><span class="comment"> * (-1, 0) (-1, 0)</span></div>
|
||||
<div class="line"><span class="comment"> * (-0.5,0.866025) (-0.5,0.866025)</span></div>
|
||||
<div class="line"><span class="comment"> */</span></div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"><span class="preprocessor">#include <array></span> <span class="comment">/// std::array</span></div>
|
||||
<div class="line"><span class="preprocessor">#include <cassert></span> <span class="comment">/// assert</span></div>
|
||||
<div class="line"><span class="preprocessor">#include <cmath></span> <span class="comment">/// std::sqrt, std::trunc, std::pow</span></div>
|
||||
<div class="line"><span class="preprocessor">#include <complex></span> <span class="comment">/// std::complex</span></div>
|
||||
<div class="line"><span class="preprocessor">#include <exception></span> <span class="comment">/// std::invalid_argument</span></div>
|
||||
<div class="line"><span class="preprocessor">#include <iomanip></span> <span class="comment">/// std::setprecision</span></div>
|
||||
<div class="line"><span class="preprocessor">#include <iostream></span> <span class="comment">/// std::cout</span></div>
|
||||
<div class="line"><span class="comment"></span> </div>
|
||||
<div class="line"><span class="comment">/**</span></div>
|
||||
<div class="line"><span class="comment"> * @namespace</span></div>
|
||||
<div class="line"><span class="comment"> * @brief Mathematical algorithms</span></div>
|
||||
<div class="line"><span class="comment"> */</span></div>
|
||||
<div class="line"><span class="keyword">namespace </span><a class="code hl_namespace" href="../../dd/d47/namespacemath.html">math</a> {</div>
|
||||
<div class="line"><span class="comment"></span> </div>
|
||||
<div class="line"><span class="comment">/**</span></div>
|
||||
<div class="line"><span class="comment"> * @brief Quadratic equation calculator.</span></div>
|
||||
<div class="line"><span class="comment"> * @param a quadratic coefficient.</span></div>
|
||||
<div class="line"><span class="comment"> * @param b linear coefficient.</span></div>
|
||||
<div class="line"><span class="comment"> * @param c constant</span></div>
|
||||
<div class="line"><span class="comment"> * @return Array containing the roots of quadratic equation, incl. complex</span></div>
|
||||
<div class="line"><span class="comment"> * root.</span></div>
|
||||
<div class="line"><span class="comment"> */</span></div>
|
||||
<div class="line"><a id="_a0" name="_a0"></a><a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::complex<long double></a>, 2> <a id="a1" name="a1"></a><a class="code hl_function" href="../../dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb">quadraticEquation</a>(<span class="keywordtype">long</span> <span class="keywordtype">double</span> a,</div>
|
||||
<div class="line"> <span class="keywordtype">long</span> <span class="keywordtype">double</span> b,</div>
|
||||
<div class="line"> <span class="keywordtype">long</span> <span class="keywordtype">double</span> c) {</div>
|
||||
<div class="line"> <span class="keywordflow">if</span> (a == 0) {</div>
|
||||
<div class="line"> <span class="keywordflow">throw</span> <a id="_a2" name="_a2"></a><a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/error/invalid_argument.html">std::invalid_argument</a>(<span class="stringliteral">"quadratic coefficient cannot be 0"</span>);</div>
|
||||
<div class="line"> }</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <span class="keywordtype">long</span> <span class="keywordtype">double</span> discriminant = b * b - 4 * a * c;</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::complex<long double></a>, 2> solutions{0, 0};</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <span class="keywordflow">if</span> (discriminant == 0) {</div>
|
||||
<div class="line"> solutions[0] = -b * 0.5 / a;</div>
|
||||
<div class="line"> solutions[1] = -b * 0.5 / a;</div>
|
||||
<div class="line"> <span class="keywordflow">return</span> solutions;</div>
|
||||
<div class="line"> }</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <span class="comment">// Complex root (discriminant < 0)</span></div>
|
||||
<div class="line"> <span class="comment">// Note that the left term (-b / 2a) is always real. The imaginary part</span></div>
|
||||
<div class="line"> <span class="comment">// appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So,</span></div>
|
||||
<div class="line"> <span class="comment">// the imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.</span></div>
|
||||
<div class="line"> <span class="keywordflow">if</span> (discriminant > 0) {</div>
|
||||
<div class="line"> <span class="comment">// Since discriminant > 0, there are only real roots. Therefore,</span></div>
|
||||
<div class="line"> <span class="comment">// imaginary component = 0.</span></div>
|
||||
<div class="line"> solutions[0] = <a id="_a3" name="_a3"></a><a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{</div>
|
||||
<div class="line"> (-b - <a id="a4" name="a4"></a><a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/sqrt.html">std::sqrt</a>(discriminant)) * 0.5 / a, 0};</div>
|
||||
<div class="line"> solutions[1] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{</div>
|
||||
<div class="line"> (-b + <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/sqrt.html">std::sqrt</a>(discriminant)) * 0.5 / a, 0};</div>
|
||||
<div class="line"> <span class="keywordflow">return</span> solutions;</div>
|
||||
<div class="line"> }</div>
|
||||
<div class="line"> <span class="comment">// Since b^2 - 4ac is < 0, for faster computation, -discriminant is</span></div>
|
||||
<div class="line"> <span class="comment">// enough to make it positive.</span></div>
|
||||
<div class="line"> solutions[0] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{</div>
|
||||
<div class="line"> -b * 0.5 / a, -<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/sqrt.html">std::sqrt</a>(-discriminant) * 0.5 / a};</div>
|
||||
<div class="line"> solutions[1] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{</div>
|
||||
<div class="line"> -b * 0.5 / a, <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/sqrt.html">std::sqrt</a>(-discriminant) * 0.5 / a};</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <span class="keywordflow">return</span> solutions;</div>
|
||||
<div class="line">}</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line">} <span class="comment">// namespace math</span></div>
|
||||
<div class="line"><span class="comment"></span> </div>
|
||||
<div class="line"><span class="comment">/**</span></div>
|
||||
<div class="line"><span class="comment"> * @brief Asserts an array of complex numbers.</span></div>
|
||||
<div class="line"><span class="comment"> * @param input Input array of complex numbers. .</span></div>
|
||||
<div class="line"><span class="comment"> * @param expected Expected array of complex numbers.</span></div>
|
||||
<div class="line"><span class="comment"> * @param precision Precision to be asserted. Default=10</span></div>
|
||||
<div class="line"><span class="comment"> */</span></div>
|
||||
<div class="line"><span class="keywordtype">void</span> <a id="a5" name="a5"></a><a class="code hl_function" href="../../da/d18/quadratic__equations__complex__numbers_8cpp.html#af7a6d4e3dc85a6288c8f1f7094830c5a">assertArray</a>(<a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a><<a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>, 2> input,</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a><<a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>, 2> expected,</div>
|
||||
<div class="line"> <span class="keywordtype">size_t</span> precision = 10) {</div>
|
||||
<div class="line"> <span class="keywordtype">long</span> <span class="keywordtype">double</span> exponent = <a id="a6" name="a6"></a><a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/pow.html">std::pow</a>(10, precision);</div>
|
||||
<div class="line"> input[0].real(<a id="a7" name="a7"></a><a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(input[0].real() * exponent));</div>
|
||||
<div class="line"> input[1].real(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(input[1].real() * exponent));</div>
|
||||
<div class="line"> input[0].imag(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(input[0].imag() * exponent));</div>
|
||||
<div class="line"> input[1].imag(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(input[1].imag() * exponent));</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> expected[0].real(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(expected[0].real() * exponent));</div>
|
||||
<div class="line"> expected[1].real(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(expected[1].real() * exponent));</div>
|
||||
<div class="line"> expected[0].imag(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(expected[0].imag() * exponent));</div>
|
||||
<div class="line"> expected[1].imag(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a>(expected[1].imag() * exponent));</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> assert(input == expected);</div>
|
||||
<div class="line">}</div>
|
||||
<div class="line"><span class="comment"></span> </div>
|
||||
<div class="line"><span class="comment">/**</span></div>
|
||||
<div class="line"><span class="comment"> * @brief Self-test implementations to test quadraticEquation function.</span></div>
|
||||
<div class="line"><span class="comment"> * @note There are 4 different types of solutions: Real and equal, real,</span></div>
|
||||
<div class="line"><span class="comment"> * complex, complex and equal.</span></div>
|
||||
<div class="line"><span class="comment"> */</span></div>
|
||||
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code hl_function" href="../../d6/d2c/caesar__cipher_8cpp.html#ae1a3968e7947464bee7714f6d43b7002">test</a>() {</div>
|
||||
<div class="line"> <span class="comment">// Values are equal and real.</span></div>
|
||||
<div class="line"> <a id="_a8" name="_a8"></a><a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Input: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"a=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"b=-2 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"c=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"Expected output: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"(1, 0), (1, 0)\n\n"</span>;</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::complex<long double></a>, 2> equalCase{</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{1, 0}, <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{1, 0}};</div>
|
||||
<div class="line"> assert(<a class="code hl_function" href="../../dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb">math::quadraticEquation</a>(1, -2, 1) == equalCase);</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <span class="comment">// Values are equal and complex.</span></div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Input: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"a=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"b=4 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"c=5 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"Expected output: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"(-2, -1), (-2, 1)\n\n"</span>;</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::complex<long double></a>, 2> complexCase{</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{-2, -1}, <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{-2, 1}};</div>
|
||||
<div class="line"> assert(<a class="code hl_function" href="../../dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb">math::quadraticEquation</a>(1, 4, 5) == complexCase);</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <span class="comment">// Values are real.</span></div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Input: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"a=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"b=5 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"c=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"Expected output: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"(-4.7912878475, 0), (-0.2087121525, 0)\n\n"</span>;</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::complex<long double></a>, 2> floatCase{</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{-4.7912878475, 0},</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{-0.2087121525, 0}};</div>
|
||||
<div class="line"> <a class="code hl_function" href="../../da/d18/quadratic__equations__complex__numbers_8cpp.html#af7a6d4e3dc85a6288c8f1f7094830c5a">assertArray</a>(<a class="code hl_function" href="../../dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb">math::quadraticEquation</a>(1, 5, 1), floatCase);</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <span class="comment">// Values are complex.</span></div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Input: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"a=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"b=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"c=1 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"Expected output: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"(-0.5, -0.8660254038), (-0.5, 0.8660254038)\n\n"</span>;</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::complex<long double></a>, 2> ifloatCase{</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{-0.5, -0.8660254038},</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex<long double></a>{-0.5, 0.8660254038}};</div>
|
||||
<div class="line"> <a class="code hl_function" href="../../da/d18/quadratic__equations__complex__numbers_8cpp.html#af7a6d4e3dc85a6288c8f1f7094830c5a">assertArray</a>(<a class="code hl_function" href="../../dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb">math::quadraticEquation</a>(1, 1, 1), ifloatCase);</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Exception test: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"Input: \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"a=0 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"b=0 \n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"c=0\n"</span></div>
|
||||
<div class="line"> <span class="stringliteral">"Expected output: Exception thrown \n"</span>;</div>
|
||||
<div class="line"> <span class="keywordflow">try</span> {</div>
|
||||
<div class="line"> <a class="code hl_function" href="../../dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb">math::quadraticEquation</a>(0, 0, 0);</div>
|
||||
<div class="line"> } <span class="keywordflow">catch</span> (<a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/error/invalid_argument.html">std::invalid_argument</a>& e) {</div>
|
||||
<div class="line"> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Exception thrown successfully \n"</span>;</div>
|
||||
<div class="line"> }</div>
|
||||
<div class="line">}</div>
|
||||
<div class="line"><span class="comment"></span> </div>
|
||||
<div class="line"><span class="comment">/**</span></div>
|
||||
<div class="line"><span class="comment"> * @brief Main function</span></div>
|
||||
<div class="line"><span class="comment"> * @returns 0 on exit</span></div>
|
||||
<div class="line"><span class="comment"> */</span></div>
|
||||
<div class="line"><span class="keywordtype">int</span> <a id="a9" name="a9"></a><a class="code hl_function" href="../../d3/d40/graph__coloring_8cpp.html#gae66f6b31b5ad750f1fe042a706a4e3d4">main</a>() {</div>
|
||||
<div class="line"> <a class="code hl_function" href="../../d6/d2c/caesar__cipher_8cpp.html#ae1a3968e7947464bee7714f6d43b7002">test</a>(); <span class="comment">// Run self-test implementation.</span></div>
|
||||
<div class="line"> <span class="keywordflow">return</span> 0;</div>
|
||||
<div class="line">}</div>
|
||||
<div class="ttc" id="aarray_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a></div></div>
|
||||
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
|
||||
<div class="ttc" id="acaesar__cipher_8cpp_html_ae1a3968e7947464bee7714f6d43b7002"><div class="ttname"><a href="../../d6/d2c/caesar__cipher_8cpp.html#ae1a3968e7947464bee7714f6d43b7002">test</a></div><div class="ttdeci">void test()</div><div class="ttdef"><b>Definition:</b> caesar_cipher.cpp:100</div></div>
|
||||
<div class="ttc" id="acomplex_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/complex.html">std::complex</a></div></div>
|
||||
<div class="ttc" id="agraph__coloring_8cpp_html_gae66f6b31b5ad750f1fe042a706a4e3d4"><div class="ttname"><a href="../../d3/d40/graph__coloring_8cpp.html#gae66f6b31b5ad750f1fe042a706a4e3d4">main</a></div><div class="ttdeci">int main()</div><div class="ttdoc">Main function.</div><div class="ttdef"><b>Definition:</b> graph_coloring.cpp:112</div></div>
|
||||
<div class="ttc" id="ainvalid_argument_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/error/invalid_argument.html">std::invalid_argument</a></div></div>
|
||||
<div class="ttc" id="anamespacemath_html"><div class="ttname"><a href="../../dd/d47/namespacemath.html">math</a></div><div class="ttdoc">for IO operations</div></div>
|
||||
<div class="ttc" id="anamespacemath_html_aacb1411ef2029e81f249c21e17c96fdb"><div class="ttname"><a href="../../dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb">math::quadraticEquation</a></div><div class="ttdeci">std::array< std::complex< long double >, 2 > quadraticEquation(long double a, long double b, long double c)</div><div class="ttdoc">Quadratic equation calculator.</div><div class="ttdef"><b>Definition:</b> quadratic_equations_complex_numbers.cpp:53</div></div>
|
||||
<div class="ttc" id="apow_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/math/pow.html">std::pow</a></div><div class="ttdeci">T pow(T... args)</div></div>
|
||||
<div class="ttc" id="aquadratic__equations__complex__numbers_8cpp_html_af7a6d4e3dc85a6288c8f1f7094830c5a"><div class="ttname"><a href="../../da/d18/quadratic__equations__complex__numbers_8cpp.html#af7a6d4e3dc85a6288c8f1f7094830c5a">assertArray</a></div><div class="ttdeci">void assertArray(std::array< std::complex< long double >, 2 > input, std::array< std::complex< long double >, 2 > expected, size_t precision=10)</div><div class="ttdoc">Asserts an array of complex numbers.</div><div class="ttdef"><b>Definition:</b> quadratic_equations_complex_numbers.cpp:100</div></div>
|
||||
<div class="ttc" id="around_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/math/round.html">std::round</a></div><div class="ttdeci">T round(T... args)</div></div>
|
||||
<div class="ttc" id="asqrt_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/math/sqrt.html">std::sqrt</a></div><div class="ttdeci">T sqrt(T... args)</div></div>
|
||||
</div><!-- fragment --> </div><!-- contents -->
|
||||
</div><!-- doc-content -->
|
||||
<!-- start footer part -->
|
||||
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
||||
<ul>
|
||||
<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.6 </li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user