From 8fdd50d476dfab97296deea4181b66e0350bbd3f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 28 Apr 2023 19:58:46 +0000 Subject: [PATCH] Documentation for a0227012ec1a38bbf224838abe8d200b04c54cee --- d3/d40/graph__coloring_8cpp.html | 2 +- ...tic__equations__complex__numbers_8cpp.html | 359 ++++++++++++++++++ ...ratic__equations__complex__numbers_8cpp.js | 7 + ...8dca7b867074164d5f45b0f3851269d_cgraph.map | 8 + ...8dca7b867074164d5f45b0f3851269d_cgraph.md5 | 1 + ...8dca7b867074164d5f45b0f3851269d_cgraph.svg | 96 +++++ ...66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map | 9 + ...66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 | 1 + ...66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg | 111 ++++++ ...7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.map | 5 + ...7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.md5 | 1 + ...7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.svg | 51 +++ dd/d47/namespacemath.html | 96 +++++ dd/d47/namespacemath.js | 1 + ...cb1411ef2029e81f249c21e17c96fdb_cgraph.map | 4 + ...cb1411ef2029e81f249c21e17c96fdb_cgraph.md5 | 1 + ...cb1411ef2029e81f249c21e17c96fdb_cgraph.svg | 36 ++ ...equations__complex__numbers_8cpp__incl.map | 10 + ...equations__complex__numbers_8cpp__incl.md5 | 1 + ...equations__complex__numbers_8cpp__incl.svg | 127 +++++++ ...quations_complex_numbers_8cpp-example.html | 317 ++++++++++++++++ dir_296d53ceaeaa7e099814a6def439fe8a.html | 3 + dir_296d53ceaeaa7e099814a6def439fe8a.js | 1 + examples.html | 1 + examples.js | 1 + files.html | 17 +- globals_a.html | 1 + globals_func_a.html | 1 + globals_func_m.html | 2 +- globals_func_t.html | 2 +- globals_m.html | 2 +- globals_t.html | 2 +- namespacemembers.html | 1 + namespacemembers_func.html | 1 + navtreedata.js | 8 +- navtreeindex0.js | 56 +-- navtreeindex1.js | 30 +- navtreeindex10.js | 12 +- navtreeindex11.js | 62 +-- navtreeindex12.js | 32 +- navtreeindex13.js | 23 +- navtreeindex2.js | 22 +- navtreeindex3.js | 2 +- navtreeindex4.js | 18 +- navtreeindex5.js | 18 +- navtreeindex6.js | 28 +- navtreeindex7.js | 30 +- navtreeindex8.js | 34 +- navtreeindex9.js | 46 +-- search/all_12.js | 46 +-- search/all_15.js | 6 +- search/all_2.js | 99 ++--- search/all_a.js | 67 ++-- search/all_e.js | 265 +++++++------ search/files_11.js | 11 +- search/functions_1.js | 87 ++--- search/functions_11.js | 19 +- search/functions_14.js | 2 +- search/functions_9.js | 4 +- search/functions_d.js | 2 +- 60 files changed, 1787 insertions(+), 521 deletions(-) create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp.html create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp.js create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.map create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.md5 create mode 100644 da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.svg create mode 100644 dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.map create mode 100644 dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.md5 create mode 100644 dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.svg create mode 100644 df/db0/quadratic__equations__complex__numbers_8cpp__incl.map create mode 100644 df/db0/quadratic__equations__complex__numbers_8cpp__incl.md5 create mode 100644 df/db0/quadratic__equations__complex__numbers_8cpp__incl.svg create mode 100644 df/dcd/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2math_2quadratic_equations_complex_numbers_8cpp-example.html diff --git a/d3/d40/graph__coloring_8cpp.html b/d3/d40/graph__coloring_8cpp.html index 0bd789eef..916476b9b 100644 --- a/d3/d40/graph__coloring_8cpp.html +++ b/d3/d40/graph__coloring_8cpp.html @@ -333,7 +333,7 @@ Here is the call graph for this function:

Main function.

Returns
0 on exit

Driver Code

-
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/numerical_methods/rungekutta.cpp, and /Users/runner/work/C-Plus-Plus/C-Plus-Plus/sorting/wiggle_sort.cpp.
+
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp, /Users/runner/work/C-Plus-Plus/C-Plus-Plus/numerical_methods/rungekutta.cpp, and /Users/runner/work/C-Plus-Plus/C-Plus-Plus/sorting/wiggle_sort.cpp.
112 {
113 // Create following graph and test whether it is 3 colorable
diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp.html b/da/d18/quadratic__equations__complex__numbers_8cpp.html new file mode 100644 index 000000000..9cc6c65dc --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp.html @@ -0,0 +1,359 @@ + + + + + + + +Algorithms_in_C++: math/quadratic_equations_complex_numbers.cpp File Reference + + + + + + + + + + + + + + + +
+
+ + + + + + +
+
Algorithms_in_C++ 1.0.0 +
+
Set of algorithms implemented in C++.
+
+
+ + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+ +
quadratic_equations_complex_numbers.cpp File Reference
+
+
+ +

Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0. +More...

+
#include <array>
+#include <cassert>
+#include <cmath>
+#include <complex>
+#include <exception>
+#include <iomanip>
+#include <iostream>
+
+Include dependency graph for quadratic_equations_complex_numbers.cpp:
+
+
+
+
+
+ + + + +

+Namespaces

namespace  math
 for IO operations
 
+ + + + + + + + + + + + + +

+Functions

std::array< std::complex< long double >, 2 > math::quadraticEquation (long double a, long double b, long double c)
 Quadratic equation calculator.
 
void assertArray (std::array< std::complex< long double >, 2 > input, std::array< std::complex< long double >, 2 > expected, size_t precision=10)
 Asserts an array of complex numbers.
 
static void test ()
 Self-test implementations to test quadraticEquation function.
 
int main ()
 Main function.
 
+

Detailed Description

+

Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0.

+
Author
Renjian-buchai
+

@description Calculates any quadratic equation in form ax^2 + bx + c.

+

Quadratic equation: x = (-b +/- sqrt(b^2 - 4ac)) / 2a

+

Function Documentation

+ +

◆ assertArray()

+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + +
void assertArray (std::array< std::complex< long double >, 2 > input,
std::array< std::complex< long double >, 2 > expected,
size_t precision = 10 
)
+
+ +

Asserts an array of complex numbers.

+
Parameters
+ + + + +
inputInput array of complex numbers. .
expectedExpected array of complex numbers.
precisionPrecision to be asserted. Default=10
+
+
+
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp.
+
+
102 {
+
103 long double exponent = std::pow(10, precision);
+
104 input[0].real(std::round(input[0].real() * exponent));
+
105 input[1].real(std::round(input[1].real() * exponent));
+
106 input[0].imag(std::round(input[0].imag() * exponent));
+
107 input[1].imag(std::round(input[1].imag() * exponent));
+
108
+
109 expected[0].real(std::round(expected[0].real() * exponent));
+
110 expected[1].real(std::round(expected[1].real() * exponent));
+
111 expected[0].imag(std::round(expected[0].imag() * exponent));
+
112 expected[1].imag(std::round(expected[1].imag() * exponent));
+
113
+
114 assert(input == expected);
+
115}
+
T pow(T... args)
+
T round(T... args)
+
+Here is the call graph for this function:
+
+
+
+
+ +
+
+ +

◆ main()

+ +
+
+ + + + + + + + +
int main (void )
+
+ +

Main function.

+
Returns
0 on exit
+
186 {
+
187 test(); // Run self-test implementation.
+
188 return 0;
+
189}
+
static void test()
Self-test implementations to test quadraticEquation function.
Definition: quadratic_equations_complex_numbers.cpp:122
+
+Here is the call graph for this function:
+
+
+
+
+ +
+
+ +

◆ test()

+ +
+
+ + + + + +
+ + + + + + + +
static void test ()
+
+static
+
+ +

Self-test implementations to test quadraticEquation function.

+
Note
There are 4 different types of solutions: Real and equal, real, complex, complex and equal.
+
122 {
+
123 // Values are equal and real.
+
124 std::cout << "Input: \n"
+
125 "a=1 \n"
+
126 "b=-2 \n"
+
127 "c=1 \n"
+
128 "Expected output: \n"
+
129 "(1, 0), (1, 0)\n\n";
+ + +
132 assert(math::quadraticEquation(1, -2, 1) == equalCase);
+
133
+
134 // Values are equal and complex.
+
135 std::cout << "Input: \n"
+
136 "a=1 \n"
+
137 "b=4 \n"
+
138 "c=5 \n"
+
139 "Expected output: \n"
+
140 "(-2, -1), (-2, 1)\n\n";
+ + +
143 assert(math::quadraticEquation(1, 4, 5) == complexCase);
+
144
+
145 // Values are real.
+
146 std::cout << "Input: \n"
+
147 "a=1 \n"
+
148 "b=5 \n"
+
149 "c=1 \n"
+
150 "Expected output: \n"
+
151 "(-4.7912878475, 0), (-0.2087121525, 0)\n\n";
+ +
153 std::complex<long double>{-4.7912878475, 0},
+
154 std::complex<long double>{-0.2087121525, 0}};
+
155 assertArray(math::quadraticEquation(1, 5, 1), floatCase);
+
156
+
157 // Values are complex.
+
158 std::cout << "Input: \n"
+
159 "a=1 \n"
+
160 "b=1 \n"
+
161 "c=1 \n"
+
162 "Expected output: \n"
+
163 "(-0.5, -0.8660254038), (-0.5, 0.8660254038)\n\n";
+ +
165 std::complex<long double>{-0.5, -0.8660254038},
+
166 std::complex<long double>{-0.5, 0.8660254038}};
+
167 assertArray(math::quadraticEquation(1, 1, 1), ifloatCase);
+
168
+
169 std::cout << "Exception test: \n"
+
170 "Input: \n"
+
171 "a=0 \n"
+
172 "b=0 \n"
+
173 "c=0\n"
+
174 "Expected output: Exception thrown \n";
+
175 try {
+ +
177 } catch (std::invalid_argument& e) {
+
178 std::cout << "Exception thrown successfully \n";
+
179 }
+
180}
+ + + + +
std::array< std::complex< long double >, 2 > quadraticEquation(long double a, long double b, long double c)
Quadratic equation calculator.
Definition: quadratic_equations_complex_numbers.cpp:53
+
void assertArray(std::array< std::complex< long double >, 2 > input, std::array< std::complex< long double >, 2 > expected, size_t precision=10)
Asserts an array of complex numbers.
Definition: quadratic_equations_complex_numbers.cpp:100
+
+Here is the call graph for this function:
+
+
+
+
+ +
+
+
+
+ + + + diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp.js b/da/d18/quadratic__equations__complex__numbers_8cpp.js new file mode 100644 index 000000000..0d3534067 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp.js @@ -0,0 +1,7 @@ +var quadratic__equations__complex__numbers_8cpp = +[ + [ "assertArray", "da/d18/quadratic__equations__complex__numbers_8cpp.html#af7a6d4e3dc85a6288c8f1f7094830c5a", null ], + [ "main", "da/d18/quadratic__equations__complex__numbers_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ], + [ "quadraticEquation", "da/d18/quadratic__equations__complex__numbers_8cpp.html#aacb1411ef2029e81f249c21e17c96fdb", null ], + [ "test", "da/d18/quadratic__equations__complex__numbers_8cpp.html#aa8dca7b867074164d5f45b0f3851269d", null ] +]; \ No newline at end of file diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map b/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map new file mode 100644 index 000000000..f78199828 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 b/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 new file mode 100644 index 000000000..f87c3e2cd --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 @@ -0,0 +1 @@ +9b34f7cb7af932f21f53659c0df0ff73 \ No newline at end of file diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg b/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg new file mode 100644 index 000000000..b9b2ce081 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg @@ -0,0 +1,96 @@ + + + + + + +test + + +Node1 + + +test + + + + + +Node2 + + +assertArray + + + + + +Node1->Node2 + + + + + +Node5 + + +math::quadraticEquation + + + + + +Node1->Node5 + + + + + +Node3 + + +std::pow + + + + + +Node2->Node3 + + + + + +Node4 + + +std::round + + + + + +Node2->Node4 + + + + + +Node6 + + +std::sqrt + + + + + +Node5->Node6 + + + + + diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map b/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map new file mode 100644 index 000000000..bd1adf22b --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 b/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 new file mode 100644 index 000000000..077840e96 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 @@ -0,0 +1 @@ +da09afc8caf114a7bf5d8c4ea9d3dbd9 \ No newline at end of file diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg b/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg new file mode 100644 index 000000000..76210d5c6 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg @@ -0,0 +1,111 @@ + + + + + + +main + + +Node1 + + +main + + + + + +Node2 + + +test + + + + + +Node1->Node2 + + + + + +Node3 + + +assertArray + + + + + +Node2->Node3 + + + + + +Node6 + + +math::quadraticEquation + + + + + +Node2->Node6 + + + + + +Node4 + + +std::pow + + + + + +Node3->Node4 + + + + + +Node5 + + +std::round + + + + + +Node3->Node5 + + + + + +Node7 + + +std::sqrt + + + + + +Node6->Node7 + + + + + diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.map b/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.map new file mode 100644 index 000000000..8c3ac1d69 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.map @@ -0,0 +1,5 @@ + + + + + diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.md5 b/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.md5 new file mode 100644 index 000000000..5c291ae83 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.md5 @@ -0,0 +1 @@ +d73edad244cdfafe5755d59e3b8c8aa3 \ No newline at end of file diff --git a/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.svg b/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.svg new file mode 100644 index 000000000..2b75ef982 --- /dev/null +++ b/da/d18/quadratic__equations__complex__numbers_8cpp_af7a6d4e3dc85a6288c8f1f7094830c5a_cgraph.svg @@ -0,0 +1,51 @@ + + + + + + +assertArray + + +Node1 + + +assertArray + + + + + +Node2 + + +std::pow + + + + + +Node1->Node2 + + + + + +Node3 + + +std::round + + + + + +Node1->Node3 + + + + + diff --git a/dd/d47/namespacemath.html b/dd/d47/namespacemath.html index 08b4e767b..aa8b47b92 100644 --- a/dd/d47/namespacemath.html +++ b/dd/d47/namespacemath.html @@ -211,6 +211,9 @@ Functions int power_of_two (int n)  This function finds whether a number is power of 2 or not.
  +std::array< std::complex< long double >, 2 > quadraticEquation (long double a, long double b, long double c) + Quadratic equation calculator.
+  uint64_t binomialCoeffSum (uint64_t n)   template<typename T > @@ -1365,6 +1368,99 @@ template<typename T >
81 return length * width * height / 3;
82}
+ + + +

◆ quadraticEquation()

+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + +
std::array< std::complex< long double >, 2 > math::quadraticEquation (long double a,
long double b,
long double c 
)
+
+ +

Quadratic equation calculator.

+
Parameters
+ + + + +
aquadratic coefficient.
blinear coefficient.
cconstant
+
+
+
Returns
Array containing the roots of quadratic equation, incl. complex root.
+
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp.
+
+
55 {
+
56 if (a == 0) {
+
57 throw std::invalid_argument("quadratic coefficient cannot be 0");
+
58 }
+
59
+
60 long double discriminant = b * b - 4 * a * c;
+
61 std::array<std::complex<long double>, 2> solutions{0, 0};
+
62
+
63 if (discriminant == 0) {
+
64 solutions[0] = -b * 0.5 / a;
+
65 solutions[1] = -b * 0.5 / a;
+
66 return solutions;
+
67 }
+
68
+
69 // Complex root (discriminant < 0)
+
70 // Note that the left term (-b / 2a) is always real. The imaginary part
+
71 // appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So,
+
72 // the imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.
+
73 if (discriminant > 0) {
+
74 // Since discriminant > 0, there are only real roots. Therefore,
+
75 // imaginary component = 0.
+
76 solutions[0] = std::complex<long double>{
+
77 (-b - std::sqrt(discriminant)) * 0.5 / a, 0};
+
78 solutions[1] = std::complex<long double>{
+
79 (-b + std::sqrt(discriminant)) * 0.5 / a, 0};
+
80 return solutions;
+
81 }
+
82 // Since b^2 - 4ac is < 0, for faster computation, -discriminant is
+
83 // enough to make it positive.
+
84 solutions[0] = std::complex<long double>{
+
85 -b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
+
86 solutions[1] = std::complex<long double>{
+
87 -b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
+
88
+
89 return solutions;
+
90}
+ + + +
T sqrt(T... args)
+
+Here is the call graph for this function:
+
+
+
+
+
diff --git a/dd/d47/namespacemath.js b/dd/d47/namespacemath.js index 551ef73b9..a6ec41bdb 100644 --- a/dd/d47/namespacemath.js +++ b/dd/d47/namespacemath.js @@ -25,6 +25,7 @@ var namespacemath = [ "power_of_two", "dd/d47/namespacemath.html#a8a48be4d7f14e34c5c92925bc1cbf3bb", null ], [ "print_primes", "dd/d47/namespacemath.html#ad09d59850865012a6fd95d89954c82e4", null ], [ "pyramid_volume", "dd/d47/namespacemath.html#a94db02b3c9e55a69ac1696f30e2f761c", null ], + [ "quadraticEquation", "dd/d47/namespacemath.html#aacb1411ef2029e81f249c21e17c96fdb", null ], [ "rect_area", "dd/d47/namespacemath.html#ab31d141f7c5b551746b1eee0eb4dedca", null ], [ "rect_perimeter", "dd/d47/namespacemath.html#a428769a16e9525e56588d7c7709d25a6", null ], [ "rect_prism_volume", "dd/d47/namespacemath.html#a3fdc74c24697ec5bb5c3698c96117c12", null ], diff --git a/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.map b/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.map new file mode 100644 index 000000000..c7931f277 --- /dev/null +++ b/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.map @@ -0,0 +1,4 @@ + + + + diff --git a/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.md5 b/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.md5 new file mode 100644 index 000000000..30f547313 --- /dev/null +++ b/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.md5 @@ -0,0 +1 @@ +dd24a2d6daec40a8c64c7af07100daa6 \ No newline at end of file diff --git a/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.svg b/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.svg new file mode 100644 index 000000000..f9c852dfd --- /dev/null +++ b/dd/d47/namespacemath_aacb1411ef2029e81f249c21e17c96fdb_cgraph.svg @@ -0,0 +1,36 @@ + + + + + + +math::quadraticEquation + + +Node1 + + +math::quadraticEquation + + + + + +Node2 + + +std::sqrt + + + + + +Node1->Node2 + + + + + diff --git a/df/db0/quadratic__equations__complex__numbers_8cpp__incl.map b/df/db0/quadratic__equations__complex__numbers_8cpp__incl.map new file mode 100644 index 000000000..93975412c --- /dev/null +++ b/df/db0/quadratic__equations__complex__numbers_8cpp__incl.map @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/df/db0/quadratic__equations__complex__numbers_8cpp__incl.md5 b/df/db0/quadratic__equations__complex__numbers_8cpp__incl.md5 new file mode 100644 index 000000000..3546aafc9 --- /dev/null +++ b/df/db0/quadratic__equations__complex__numbers_8cpp__incl.md5 @@ -0,0 +1 @@ +cdd96f9d5e8aea79954aa810ef6507b6 \ No newline at end of file diff --git a/df/db0/quadratic__equations__complex__numbers_8cpp__incl.svg b/df/db0/quadratic__equations__complex__numbers_8cpp__incl.svg new file mode 100644 index 000000000..ef43dbfc6 --- /dev/null +++ b/df/db0/quadratic__equations__complex__numbers_8cpp__incl.svg @@ -0,0 +1,127 @@ + + + + + + +math/quadratic_equations_complex_numbers.cpp + + +Node1 + + +math/quadratic_equations +_complex_numbers.cpp + + + + + +Node2 + + +array + + + + + +Node1->Node2 + + + + + +Node3 + + +cassert + + + + + +Node1->Node3 + + + + + +Node4 + + +cmath + + + + + +Node1->Node4 + + + + + +Node5 + + +complex + + + + + +Node1->Node5 + + + + + +Node6 + + +exception + + + + + +Node1->Node6 + + + + + +Node7 + + +iomanip + + + + + +Node1->Node7 + + + + + +Node8 + + +iostream + + + + + +Node1->Node8 + + + + + diff --git a/df/dcd/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2math_2quadratic_equations_complex_numbers_8cpp-example.html b/df/dcd/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2math_2quadratic_equations_complex_numbers_8cpp-example.html new file mode 100644 index 000000000..b10603c9a --- /dev/null +++ b/df/dcd/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2math_2quadratic_equations_complex_numbers_8cpp-example.html @@ -0,0 +1,317 @@ + + + + + + + +Algorithms_in_C++: /Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp + + + + + + + + + + + + + + + +
+
+ + + + + + +
+
Algorithms_in_C++ 1.0.0 +
+
Set of algorithms implemented in C++.
+
+
+ + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp
+
+
+

int main() { using std::array; using std::complex; using std::cout;

+

array<complex<long double, 2> solutions = quadraticEquation(1, 2, 1); cout << solutions[0] << " " << solutions[1] << "\n";

+

solutions = quadraticEquation(1, 1, 1); // Reusing solutions. cout << solutions[0] << " " << solutions[1] << "\n"; return 0; }

+

Output: (-1, 0) (-1, 0) (-0.5,0.866025) (-0.5,0.866025)

+
/**
+
* @file
+
* @brief Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0.
+
*
+
* @author [Renjian-buchai](https://github.com/Renjian-buchai)
+
*
+
* @description Calculates any quadratic equation in form ax^2 + bx + c.
+
*
+
* Quadratic equation:
+
* x = (-b +/- sqrt(b^2 - 4ac)) / 2a
+
*
+
* @example
+
* int main() {
+
* using std::array;
+
* using std::complex;
+
* using std::cout;
+
*
+
* array<complex<long double, 2> solutions = quadraticEquation(1, 2, 1);
+
* cout << solutions[0] << " " << solutions[1] << "\n";
+
*
+
* solutions = quadraticEquation(1, 1, 1); // Reusing solutions.
+
* cout << solutions[0] << " " << solutions[1] << "\n";
+
* return 0;
+
* }
+
*
+
* Output:
+
* (-1, 0) (-1, 0)
+
* (-0.5,0.866025) (-0.5,0.866025)
+
*/
+
+
#include <array> /// std::array
+
#include <cassert> /// assert
+
#include <cmath> /// std::sqrt, std::trunc, std::pow
+
#include <complex> /// std::complex
+
#include <exception> /// std::invalid_argument
+
#include <iomanip> /// std::setprecision
+
#include <iostream> /// std::cout
+
+
/**
+
* @namespace
+
* @brief Mathematical algorithms
+
*/
+
namespace math {
+
+
/**
+
* @brief Quadratic equation calculator.
+
* @param a quadratic coefficient.
+
* @param b linear coefficient.
+
* @param c constant
+
* @return Array containing the roots of quadratic equation, incl. complex
+
* root.
+
*/
+ +
long double b,
+
long double c) {
+
if (a == 0) {
+
throw std::invalid_argument("quadratic coefficient cannot be 0");
+
}
+
+
long double discriminant = b * b - 4 * a * c;
+ +
+
if (discriminant == 0) {
+
solutions[0] = -b * 0.5 / a;
+
solutions[1] = -b * 0.5 / a;
+
return solutions;
+
}
+
+
// Complex root (discriminant < 0)
+
// Note that the left term (-b / 2a) is always real. The imaginary part
+
// appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So,
+
// the imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.
+
if (discriminant > 0) {
+
// Since discriminant > 0, there are only real roots. Therefore,
+
// imaginary component = 0.
+
solutions[0] = std::complex<long double>{
+
(-b - std::sqrt(discriminant)) * 0.5 / a, 0};
+
solutions[1] = std::complex<long double>{
+
(-b + std::sqrt(discriminant)) * 0.5 / a, 0};
+
return solutions;
+
}
+
// Since b^2 - 4ac is < 0, for faster computation, -discriminant is
+
// enough to make it positive.
+
solutions[0] = std::complex<long double>{
+
-b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
+
solutions[1] = std::complex<long double>{
+
-b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
+
+
return solutions;
+
}
+
+
} // namespace math
+
+
/**
+
* @brief Asserts an array of complex numbers.
+
* @param input Input array of complex numbers. .
+
* @param expected Expected array of complex numbers.
+
* @param precision Precision to be asserted. Default=10
+
*/
+ + +
size_t precision = 10) {
+
long double exponent = std::pow(10, precision);
+
input[0].real(std::round(input[0].real() * exponent));
+
input[1].real(std::round(input[1].real() * exponent));
+
input[0].imag(std::round(input[0].imag() * exponent));
+
input[1].imag(std::round(input[1].imag() * exponent));
+
+
expected[0].real(std::round(expected[0].real() * exponent));
+
expected[1].real(std::round(expected[1].real() * exponent));
+
expected[0].imag(std::round(expected[0].imag() * exponent));
+
expected[1].imag(std::round(expected[1].imag() * exponent));
+
+
assert(input == expected);
+
}
+
+
/**
+
* @brief Self-test implementations to test quadraticEquation function.
+
* @note There are 4 different types of solutions: Real and equal, real,
+
* complex, complex and equal.
+
*/
+
static void test() {
+
// Values are equal and real.
+
std::cout << "Input: \n"
+
"a=1 \n"
+
"b=-2 \n"
+
"c=1 \n"
+
"Expected output: \n"
+
"(1, 0), (1, 0)\n\n";
+ + +
assert(math::quadraticEquation(1, -2, 1) == equalCase);
+
+
// Values are equal and complex.
+
std::cout << "Input: \n"
+
"a=1 \n"
+
"b=4 \n"
+
"c=5 \n"
+
"Expected output: \n"
+
"(-2, -1), (-2, 1)\n\n";
+ + +
assert(math::quadraticEquation(1, 4, 5) == complexCase);
+
+
// Values are real.
+
std::cout << "Input: \n"
+
"a=1 \n"
+
"b=5 \n"
+
"c=1 \n"
+
"Expected output: \n"
+
"(-4.7912878475, 0), (-0.2087121525, 0)\n\n";
+ +
std::complex<long double>{-4.7912878475, 0},
+
std::complex<long double>{-0.2087121525, 0}};
+
assertArray(math::quadraticEquation(1, 5, 1), floatCase);
+
+
// Values are complex.
+
std::cout << "Input: \n"
+
"a=1 \n"
+
"b=1 \n"
+
"c=1 \n"
+
"Expected output: \n"
+
"(-0.5, -0.8660254038), (-0.5, 0.8660254038)\n\n";
+ +
std::complex<long double>{-0.5, -0.8660254038},
+
std::complex<long double>{-0.5, 0.8660254038}};
+
assertArray(math::quadraticEquation(1, 1, 1), ifloatCase);
+
+
std::cout << "Exception test: \n"
+
"Input: \n"
+
"a=0 \n"
+
"b=0 \n"
+
"c=0\n"
+
"Expected output: Exception thrown \n";
+
try {
+ +
} catch (std::invalid_argument& e) {
+
std::cout << "Exception thrown successfully \n";
+
}
+
}
+
+
/**
+
* @brief Main function
+
* @returns 0 on exit
+
*/
+
int main() {
+
test(); // Run self-test implementation.
+
return 0;
+
}
+ + +
void test()
Definition: caesar_cipher.cpp:100
+ +
int main()
Main function.
Definition: graph_coloring.cpp:112
+ +
for IO operations
+
std::array< std::complex< long double >, 2 > quadraticEquation(long double a, long double b, long double c)
Quadratic equation calculator.
Definition: quadratic_equations_complex_numbers.cpp:53
+
T pow(T... args)
+
void assertArray(std::array< std::complex< long double >, 2 > input, std::array< std::complex< long double >, 2 > expected, size_t precision=10)
Asserts an array of complex numbers.
Definition: quadratic_equations_complex_numbers.cpp:100
+
T round(T... args)
+
T sqrt(T... args)
+
+
+ + + + diff --git a/dir_296d53ceaeaa7e099814a6def439fe8a.html b/dir_296d53ceaeaa7e099814a6def439fe8a.html index 807dd4ed0..ef2124f1b 100644 --- a/dir_296d53ceaeaa7e099814a6def439fe8a.html +++ b/dir_296d53ceaeaa7e099814a6def439fe8a.html @@ -250,6 +250,9 @@ Files file  primes_up_to_billion.cpp  Compute prime numbers upto 1 billion.
  +file  quadratic_equations_complex_numbers.cpp + Calculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0.
+  file  realtime_stats.cpp  Compute statistics for data entered in rreal-time.
  diff --git a/dir_296d53ceaeaa7e099814a6def439fe8a.js b/dir_296d53ceaeaa7e099814a6def439fe8a.js index 96120c623..39b87d61e 100644 --- a/dir_296d53ceaeaa7e099814a6def439fe8a.js +++ b/dir_296d53ceaeaa7e099814a6def439fe8a.js @@ -49,6 +49,7 @@ var dir_296d53ceaeaa7e099814a6def439fe8a = [ "prime_factorization.cpp", "db/d0d/prime__factorization_8cpp.html", "db/d0d/prime__factorization_8cpp" ], [ "prime_numbers.cpp", "de/d9b/prime__numbers_8cpp.html", "de/d9b/prime__numbers_8cpp" ], [ "primes_up_to_billion.cpp", "d4/d9c/primes__up__to__billion_8cpp.html", "d4/d9c/primes__up__to__billion_8cpp" ], + [ "quadratic_equations_complex_numbers.cpp", "da/d18/quadratic__equations__complex__numbers_8cpp.html", "da/d18/quadratic__equations__complex__numbers_8cpp" ], [ "realtime_stats.cpp", "d0/d08/realtime__stats_8cpp.html", "d0/d08/realtime__stats_8cpp" ], [ "sieve_of_eratosthenes.cpp", "d8/ddf/sieve__of__eratosthenes_8cpp.html", "d8/ddf/sieve__of__eratosthenes_8cpp" ], [ "sqrt_double.cpp", "da/d24/sqrt__double_8cpp.html", "da/d24/sqrt__double_8cpp" ], diff --git a/examples.html b/examples.html index 6d4133921..451eb10aa 100644 --- a/examples.html +++ b/examples.html @@ -102,6 +102,7 @@ $(document).ready(function(){initNavTree('examples.html',''); initResizable(); }
Here is a list of all examples:
diff --git a/examples.js b/examples.js index 254788db5..45ea3fb2d 100644 --- a/examples.js +++ b/examples.js @@ -1,6 +1,7 @@ var examples = [ [ "()", "db/d03/_s_t-example.html", null ], + [ "/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp", "df/dcd/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2math_2quadratic_equations_complex_numbers_8cpp-example.html", null ], [ "/Users/runner/work/C-Plus-Plus/C-Plus-Plus/numerical_methods/rungekutta.cpp", "dc/dc4/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2numerical_methods_2rungekutta_8cpp-example.html", null ], [ "/Users/runner/work/C-Plus-Plus/C-Plus-Plus/sorting/wiggle_sort.cpp", "dd/db0/_2_users_2runner_2work_2_c-_plus-_plus_2_c-_plus-_plus_2sorting_2wiggle_sort_8cpp-example.html", null ] ]; \ No newline at end of file diff --git a/files.html b/files.html index cb273de04..a2279e4c5 100644 --- a/files.html +++ b/files.html @@ -270,15 +270,16 @@ solve-a-rat-in-a-maze-c-java-pytho/" target="_blank">Rat in a Maze algorithm  prime_factorization.cppPrime factorization of positive integers  prime_numbers.cppGet list of prime numbers  primes_up_to_billion.cppCompute prime numbers upto 1 billion - realtime_stats.cppCompute statistics for data entered in rreal-time - sieve_of_eratosthenes.cppGet list of prime numbers using Sieve of Eratosthenes - sqrt_double.cppCalculate the square root of any positive real number in \(O(\log + quadratic_equations_complex_numbers.cppCalculate quadratic equation with complex roots, i.e. b^2 - 4ac < 0 + realtime_stats.cppCompute statistics for data entered in rreal-time + sieve_of_eratosthenes.cppGet list of prime numbers using Sieve of Eratosthenes + sqrt_double.cppCalculate the square root of any positive real number in \(O(\log N)\) time, with precision fixed using bisection method of root-finding - string_fibonacci.cppThis Programme returns the Nth fibonacci as a string - sum_of_binomial_coefficient.cppAlgorithm to find sum of binomial coefficients of a given positive integer - sum_of_digits.cppA C++ Program to find the Sum of Digits of input integer - vector_cross_product.cppCalculates the Cross Product and the magnitude of two mathematical 3D vectors - volume.cppImplmentations for the volume of various 3D shapes + string_fibonacci.cppThis Programme returns the Nth fibonacci as a string + sum_of_binomial_coefficient.cppAlgorithm to find sum of binomial coefficients of a given positive integer + sum_of_digits.cppA C++ Program to find the Sum of Digits of input integer + vector_cross_product.cppCalculates the Cross Product and the magnitude of two mathematical 3D vectors + volume.cppImplmentations for the volume of various 3D shapes   numerical_methods  babylonian_method.cppA babylonian method (BM) is an algorithm that computes the square root  bisection_method.cppSolve the equation \(f(x)=0\) using bisection method diff --git a/globals_a.html b/globals_a.html index ab2ff2d2c..c1c8dc475 100644 --- a/globals_a.html +++ b/globals_a.html @@ -107,6 +107,7 @@ $(document).ready(function(){initNavTree('globals_a.html',''); initResizable();
  • addition_rule_independent() : addition_rule.cpp
  • ans() : matrix_exponentiation.cpp
  • are_amicable() : check_amicable_pair.cpp
  • +
  • assertArray() : quadratic_equations_complex_numbers.cpp
  • AvailArray : linkedlist_implentation_usingarray.cpp
  • diff --git a/globals_func_a.html b/globals_func_a.html index 05710c256..38a8b049a 100644 --- a/globals_func_a.html +++ b/globals_func_a.html @@ -105,6 +105,7 @@ $(document).ready(function(){initNavTree('globals_func_a.html',''); initResizabl
  • addition_rule_independent() : addition_rule.cpp
  • ans() : matrix_exponentiation.cpp
  • are_amicable() : check_amicable_pair.cpp
  • +
  • assertArray() : quadratic_equations_complex_numbers.cpp
  • diff --git a/globals_func_m.html b/globals_func_m.html index 24fe32836..403d06303 100644 --- a/globals_func_m.html +++ b/globals_func_m.html @@ -100,7 +100,7 @@ $(document).ready(function(){initNavTree('globals_func_m.html',''); initResizabl  

    - m -