-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
quadratic_equations_complex_numbers.cpp
189 lines (172 loc) · 6.22 KB
/
quadratic_equations_complex_numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @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.
*/
std::array<std::complex<long double>, 2> quadraticEquation(long double a,
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;
std::array<std::complex<long double>, 2> solutions{0, 0};
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
*/
void assertArray(std::array<std::complex<long double>, 2> input,
std::array<std::complex<long double>, 2> expected,
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";
std::array<std::complex<long double>, 2> equalCase{
std::complex<long double>{1, 0}, std::complex<long double>{1, 0}};
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";
std::array<std::complex<long double>, 2> complexCase{
std::complex<long double>{-2, -1}, std::complex<long double>{-2, 1}};
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::array<std::complex<long double>, 2> floatCase{
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::array<std::complex<long double>, 2> ifloatCase{
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 {
math::quadraticEquation(0, 0, 0);
} 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;
}