mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: CodeQL warnings (#1827)
* fix: CodeQL warnings * clang-format and clang-tidy fixes for4d357c46
* clang-format and clang-tidy fixes for72322fb7
* accept suggestion * clang-format and clang-tidy fixes for9a4dc07c
Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Ayaan Khan <ayaankhan98@gmail.com>
This commit is contained in:
parent
b98dcdfd08
commit
e64e3df18f
@ -462,7 +462,7 @@ class uint128_t {
|
|||||||
tmp <<= left;
|
tmp <<= left;
|
||||||
uint128_t quotient(0);
|
uint128_t quotient(0);
|
||||||
uint128_t zero(0);
|
uint128_t zero(0);
|
||||||
while (left >= 0 && tmp2 >= p) {
|
while (tmp2 >= p) {
|
||||||
uint16_t shf = tmp2._lez() - tmp._lez();
|
uint16_t shf = tmp2._lez() - tmp._lez();
|
||||||
if (shf) {
|
if (shf) {
|
||||||
tmp >>= shf;
|
tmp >>= shf;
|
||||||
|
@ -429,7 +429,7 @@ class uint256_t {
|
|||||||
tmp <<= left;
|
tmp <<= left;
|
||||||
uint256_t quotient(0);
|
uint256_t quotient(0);
|
||||||
uint256_t zero(0);
|
uint256_t zero(0);
|
||||||
while (left >= 0 && tmp2 >= p) {
|
while (tmp2 >= p) {
|
||||||
uint16_t shf = tmp2._lez() - tmp._lez();
|
uint16_t shf = tmp2._lez() - tmp._lez();
|
||||||
if (shf) {
|
if (shf) {
|
||||||
tmp >>= shf;
|
tmp >>= shf;
|
||||||
|
@ -121,12 +121,10 @@ void list::reverseList() {
|
|||||||
* @returns the top element of the list
|
* @returns the top element of the list
|
||||||
*/
|
*/
|
||||||
int32_t list::top() {
|
int32_t list::top() {
|
||||||
try {
|
if (!isEmpty()) {
|
||||||
if (!isEmpty()) {
|
return head->val;
|
||||||
return head->val;
|
} else {
|
||||||
}
|
throw std::logic_error("List is empty");
|
||||||
} catch (const std::exception &e) {
|
|
||||||
std::cerr << "List is empty" << e.what() << '\n';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -134,16 +132,14 @@ int32_t list::top() {
|
|||||||
* @returns the last element of the list
|
* @returns the last element of the list
|
||||||
*/
|
*/
|
||||||
int32_t list::last() {
|
int32_t list::last() {
|
||||||
try {
|
if (!isEmpty()) {
|
||||||
if (!isEmpty()) {
|
Node *t = head;
|
||||||
Node *t = head;
|
while (t->next != nullptr) {
|
||||||
while (t->next != nullptr) {
|
t = t->next;
|
||||||
t = t->next;
|
|
||||||
}
|
|
||||||
return t->val;
|
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
return t->val;
|
||||||
std::cerr << "List is empty" << e.what() << '\n';
|
} else {
|
||||||
|
throw std::logic_error("List is empty");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -164,7 +160,7 @@ int32_t list::traverse(int index) {
|
|||||||
|
|
||||||
/* if we get to this line,the caller was asking for a non-existent element
|
/* if we get to this line,the caller was asking for a non-existent element
|
||||||
so we assert fail */
|
so we assert fail */
|
||||||
assert(0);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace linked_list
|
} // namespace linked_list
|
||||||
|
@ -9,11 +9,10 @@
|
|||||||
* @author [Ameya Chawla](https://github.com/ameyachawlaggsipu)
|
* @author [Ameya Chawla](https://github.com/ameyachawlaggsipu)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cassert> /// for assert
|
#include <cassert> /// for assert
|
||||||
|
#include <cmath>
|
||||||
#include <iostream> /// for IO operations
|
#include <iostream> /// for IO operations
|
||||||
|
|
||||||
#include "math.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @namespace numerical_methods
|
* @namespace numerical_methods
|
||||||
* @brief Numerical algorithms/methods
|
* @brief Numerical algorithms/methods
|
||||||
|
@ -35,8 +35,9 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cassert> /// for assert
|
#include <cassert> /// for assert
|
||||||
#include <cmath> /// for math functions
|
#include <cmath> /// for math functions
|
||||||
|
#include <cmath>
|
||||||
#include <cstdint> /// for integer allocation
|
#include <cstdint> /// for integer allocation
|
||||||
#include <cstdlib> /// for std::atof
|
#include <cstdlib> /// for std::atof
|
||||||
#include <functional> /// for std::function
|
#include <functional> /// for std::function
|
||||||
@ -64,13 +65,13 @@ namespace simpson_method {
|
|||||||
* @returns the result of the integration
|
* @returns the result of the integration
|
||||||
*/
|
*/
|
||||||
double evaluate_by_simpson(std::int32_t N, double h, double a,
|
double evaluate_by_simpson(std::int32_t N, double h, double a,
|
||||||
std::function<double(double)> func) {
|
const std::function<double(double)>& func) {
|
||||||
std::map<std::int32_t, double>
|
std::map<std::int32_t, double>
|
||||||
data_table; // Contains the data points. key: i, value: f(xi)
|
data_table; // Contains the data points. key: i, value: f(xi)
|
||||||
double xi = a; // Initialize xi to the starting point x0 = a
|
double xi = a; // Initialize xi to the starting point x0 = a
|
||||||
|
|
||||||
// Create the data table
|
// Create the data table
|
||||||
double temp;
|
double temp = NAN;
|
||||||
for (std::int32_t i = 0; i <= N; i++) {
|
for (std::int32_t i = 0; i <= N; i++) {
|
||||||
temp = func(xi);
|
temp = func(xi);
|
||||||
data_table.insert(
|
data_table.insert(
|
||||||
@ -82,12 +83,13 @@ double evaluate_by_simpson(std::int32_t N, double h, double a,
|
|||||||
// Remember: f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)
|
// Remember: f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)
|
||||||
double evaluate_integral = 0;
|
double evaluate_integral = 0;
|
||||||
for (std::int32_t i = 0; i <= N; i++) {
|
for (std::int32_t i = 0; i <= N; i++) {
|
||||||
if (i == 0 || i == N)
|
if (i == 0 || i == N) {
|
||||||
evaluate_integral += data_table.at(i);
|
evaluate_integral += data_table.at(i);
|
||||||
else if (i % 2 == 1)
|
} else if (i % 2 == 1) {
|
||||||
evaluate_integral += 4 * data_table.at(i);
|
evaluate_integral += 4 * data_table.at(i);
|
||||||
else
|
} else {
|
||||||
evaluate_integral += 2 * data_table.at(i);
|
evaluate_integral += 2 * data_table.at(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multiply by the coefficient h/3
|
// Multiply by the coefficient h/3
|
||||||
@ -170,7 +172,7 @@ int main(int argc, char** argv) {
|
|||||||
/// interval. MUST BE EVEN
|
/// interval. MUST BE EVEN
|
||||||
double a = 1, b = 3; /// Starting and ending point of the integration in
|
double a = 1, b = 3; /// Starting and ending point of the integration in
|
||||||
/// the real axis
|
/// the real axis
|
||||||
double h; /// Step, calculated by a, b and N
|
double h = NAN; /// Step, calculated by a, b and N
|
||||||
|
|
||||||
bool used_argv_parameters =
|
bool used_argv_parameters =
|
||||||
false; // If argv parameters are used then the assert must be omitted
|
false; // If argv parameters are used then the assert must be omitted
|
||||||
@ -180,18 +182,20 @@ int main(int argc, char** argv) {
|
|||||||
// displaying messages)
|
// displaying messages)
|
||||||
if (argc == 4) {
|
if (argc == 4) {
|
||||||
N = std::atoi(argv[1]);
|
N = std::atoi(argv[1]);
|
||||||
a = (double)std::atof(argv[2]);
|
a = std::atof(argv[2]);
|
||||||
b = (double)std::atof(argv[3]);
|
b = std::atof(argv[3]);
|
||||||
// Check if a<b else abort
|
// Check if a<b else abort
|
||||||
assert(a < b && "a has to be less than b");
|
assert(a < b && "a has to be less than b");
|
||||||
assert(N > 0 && "N has to be > 0");
|
assert(N > 0 && "N has to be > 0");
|
||||||
if (N < 16 || a != 1 || b != 3)
|
if (N < 16 || a != 1 || b != 3) {
|
||||||
used_argv_parameters = true;
|
used_argv_parameters = true;
|
||||||
|
}
|
||||||
std::cout << "You selected N=" << N << ", a=" << a << ", b=" << b
|
std::cout << "You selected N=" << N << ", a=" << a << ", b=" << b
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
} else
|
} else {
|
||||||
std::cout << "Default N=" << N << ", a=" << a << ", b=" << b
|
std::cout << "Default N=" << N << ", a=" << a << ", b=" << b
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
// Find the step
|
// Find the step
|
||||||
h = (b - a) / N;
|
h = (b - a) / N;
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
* discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT).
|
* discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT).
|
||||||
* @details
|
* @details
|
||||||
* This
|
* This
|
||||||
* algorithm has application in use case scenario where a user wants to find points of a
|
* algorithm has application in use case scenario where a user wants to find
|
||||||
|
points of a
|
||||||
* function
|
* function
|
||||||
* in a short time by just using the coefficients of the polynomial
|
* in a short time by just using the coefficients of the polynomial
|
||||||
* function.
|
* function.
|
||||||
@ -56,8 +57,9 @@ std::complex<double> *FastFourierTransform(std::complex<double> *p, uint8_t n) {
|
|||||||
if (j % 2 == 0) {
|
if (j % 2 == 0) {
|
||||||
pe[k1++] = p[j]; /// Assigning values of even Coefficients
|
pe[k1++] = p[j]; /// Assigning values of even Coefficients
|
||||||
|
|
||||||
} else
|
} else {
|
||||||
po[k2++] = p[j]; /// Assigning value of odd Coefficients
|
po[k2++] = p[j]; /// Assigning value of odd Coefficients
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::complex<double> *ye =
|
std::complex<double> *ye =
|
||||||
@ -79,12 +81,10 @@ std::complex<double> *FastFourierTransform(std::complex<double> *p, uint8_t n) {
|
|||||||
k1++;
|
k1++;
|
||||||
k2++;
|
k2++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(n!=2){
|
if (n != 2) {
|
||||||
|
|
||||||
delete[] pe;
|
delete[] pe;
|
||||||
delete[] po;
|
delete[] po;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete[] ye; /// Deleting dynamic array ye
|
delete[] ye; /// Deleting dynamic array ye
|
||||||
@ -123,9 +123,11 @@ static void test() {
|
|||||||
{10, 0}, {-2, -2}, {-2, 0}, {-2, 2}}; /// True Answer for test case 2
|
{10, 0}, {-2, -2}, {-2, 0}, {-2, 2}}; /// True Answer for test case 2
|
||||||
|
|
||||||
std::complex<double> *o1 = numerical_methods::FastFourierTransform(t1, n1);
|
std::complex<double> *o1 = numerical_methods::FastFourierTransform(t1, n1);
|
||||||
std::complex<double> *t3=o1; /// Temporary variable used to delete memory location of o1
|
std::complex<double> *t3 =
|
||||||
|
o1; /// Temporary variable used to delete memory location of o1
|
||||||
std::complex<double> *o2 = numerical_methods::FastFourierTransform(t2, n2);
|
std::complex<double> *o2 = numerical_methods::FastFourierTransform(t2, n2);
|
||||||
std::complex<double> *t4=o2; /// Temporary variable used to delete memory location of o2
|
std::complex<double> *t4 =
|
||||||
|
o2; /// Temporary variable used to delete memory location of o2
|
||||||
for (uint8_t i = 0; i < n1; i++) {
|
for (uint8_t i = 0; i < n1; i++) {
|
||||||
assert((r1[i].real() - o1->real() < 0.000000000001) &&
|
assert((r1[i].real() - o1->real() < 0.000000000001) &&
|
||||||
(r1[i].imag() - o1->imag() <
|
(r1[i].imag() - o1->imag() <
|
||||||
@ -141,8 +143,7 @@ static void test() {
|
|||||||
/// values for test case 2
|
/// values for test case 2
|
||||||
o2++;
|
o2++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
delete[] t1;
|
delete[] t1;
|
||||||
delete[] t2;
|
delete[] t2;
|
||||||
delete[] t3;
|
delete[] t3;
|
||||||
@ -160,6 +161,6 @@ static void test() {
|
|||||||
|
|
||||||
int main(int argc, char const *argv[]) {
|
int main(int argc, char const *argv[]) {
|
||||||
test(); // run self-test implementations
|
test(); // run self-test implementations
|
||||||
// with 2 defined test cases
|
// with 2 defined test cases
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,10 @@
|
|||||||
* (IFFT)](https://www.geeksforgeeks.org/python-inverse-fast-fourier-transformation/)
|
* (IFFT)](https://www.geeksforgeeks.org/python-inverse-fast-fourier-transformation/)
|
||||||
* is an algorithm that computes the inverse fourier transform.
|
* is an algorithm that computes the inverse fourier transform.
|
||||||
* @details
|
* @details
|
||||||
* This algorithm has an application in use case scenario where a user wants find coefficients of
|
* This algorithm has an application in use case scenario where a user wants
|
||||||
* a function in a short time by just using points generated by DFT.
|
* find coefficients of a function in a short time by just using points
|
||||||
* Time complexity
|
* generated by DFT. Time complexity this algorithm computes the IDFT in
|
||||||
* this algorithm computes the IDFT in O(nlogn) time in comparison to traditional O(n^2).
|
* O(nlogn) time in comparison to traditional O(n^2).
|
||||||
* @author [Ameya Chawla](https://github.com/ameyachawlaggsipu)
|
* @author [Ameya Chawla](https://github.com/ameyachawlaggsipu)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -23,14 +23,15 @@
|
|||||||
*/
|
*/
|
||||||
namespace numerical_methods {
|
namespace numerical_methods {
|
||||||
/**
|
/**
|
||||||
* @brief InverseFastFourierTransform is a recursive function which returns list of
|
* @brief InverseFastFourierTransform is a recursive function which returns list
|
||||||
* complex numbers
|
* of complex numbers
|
||||||
* @param p List of Coefficents in form of complex numbers
|
* @param p List of Coefficents in form of complex numbers
|
||||||
* @param n Count of elements in list p
|
* @param n Count of elements in list p
|
||||||
* @returns p if n==1
|
* @returns p if n==1
|
||||||
* @returns y if n!=1
|
* @returns y if n!=1
|
||||||
*/
|
*/
|
||||||
std::complex<double> *InverseFastFourierTransform(std::complex<double> *p, uint8_t n) {
|
std::complex<double> *InverseFastFourierTransform(std::complex<double> *p,
|
||||||
|
uint8_t n) {
|
||||||
if (n == 1) {
|
if (n == 1) {
|
||||||
return p; /// Base Case To return
|
return p; /// Base Case To return
|
||||||
}
|
}
|
||||||
@ -39,9 +40,9 @@ std::complex<double> *InverseFastFourierTransform(std::complex<double> *p, uint8
|
|||||||
|
|
||||||
std::complex<double> om = std::complex<double>(
|
std::complex<double> om = std::complex<double>(
|
||||||
cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega
|
cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega
|
||||||
|
|
||||||
om.real(om.real()/n); /// One change in comparison with DFT
|
om.real(om.real() / n); /// One change in comparison with DFT
|
||||||
om.imag(om.imag()/n); /// One change in comparison with DFT
|
om.imag(om.imag() / n); /// One change in comparison with DFT
|
||||||
|
|
||||||
auto *pe = new std::complex<double>[n / 2]; /// Coefficients of even power
|
auto *pe = new std::complex<double>[n / 2]; /// Coefficients of even power
|
||||||
|
|
||||||
@ -52,8 +53,9 @@ std::complex<double> *InverseFastFourierTransform(std::complex<double> *p, uint8
|
|||||||
if (j % 2 == 0) {
|
if (j % 2 == 0) {
|
||||||
pe[k1++] = p[j]; /// Assigning values of even Coefficients
|
pe[k1++] = p[j]; /// Assigning values of even Coefficients
|
||||||
|
|
||||||
} else
|
} else {
|
||||||
po[k2++] = p[j]; /// Assigning value of odd Coefficients
|
po[k2++] = p[j]; /// Assigning value of odd Coefficients
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::complex<double> *ye =
|
std::complex<double> *ye =
|
||||||
@ -75,12 +77,10 @@ std::complex<double> *InverseFastFourierTransform(std::complex<double> *p, uint8
|
|||||||
k1++;
|
k1++;
|
||||||
k2++;
|
k2++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(n!=2){
|
if (n != 2) {
|
||||||
|
|
||||||
delete[] pe;
|
delete[] pe;
|
||||||
delete[] po;
|
delete[] po;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete[] ye; /// Deleting dynamic array ye
|
delete[] ye; /// Deleting dynamic array ye
|
||||||
@ -118,16 +118,17 @@ static void test() {
|
|||||||
std::vector<std::complex<double>> r2 = {
|
std::vector<std::complex<double>> r2 = {
|
||||||
{1, 0}, {2, 0}, {3, 0}, {4, 0}}; /// True Answer for test case 2
|
{1, 0}, {2, 0}, {3, 0}, {4, 0}}; /// True Answer for test case 2
|
||||||
|
|
||||||
std::complex<double> *o1 = numerical_methods::InverseFastFourierTransform(t1, n1);
|
std::complex<double> *o1 =
|
||||||
|
numerical_methods::InverseFastFourierTransform(t1, n1);
|
||||||
std::complex<double> *o2 = numerical_methods::InverseFastFourierTransform(t2, n2);
|
|
||||||
|
std::complex<double> *o2 =
|
||||||
|
numerical_methods::InverseFastFourierTransform(t2, n2);
|
||||||
|
|
||||||
for (uint8_t i = 0; i < n1; i++) {
|
for (uint8_t i = 0; i < n1; i++) {
|
||||||
assert((r1[i].real() - o1[i].real() < 0.000000000001) &&
|
assert((r1[i].real() - o1[i].real() < 0.000000000001) &&
|
||||||
(r1[i].imag() - o1[i].imag() <
|
(r1[i].imag() - o1[i].imag() <
|
||||||
0.000000000001)); /// Comparing for both real and imaginary
|
0.000000000001)); /// Comparing for both real and imaginary
|
||||||
/// values for test case 1
|
/// values for test case 1
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint8_t i = 0; i < n2; i++) {
|
for (uint8_t i = 0; i < n2; i++) {
|
||||||
@ -135,10 +136,8 @@ static void test() {
|
|||||||
(r2[i].imag() - o2[i].imag() <
|
(r2[i].imag() - o2[i].imag() <
|
||||||
0.000000000001)); /// Comparing for both real and imaginary
|
0.000000000001)); /// Comparing for both real and imaginary
|
||||||
/// values for test case 2
|
/// values for test case 2
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
delete[] t1;
|
delete[] t1;
|
||||||
delete[] t2;
|
delete[] t2;
|
||||||
delete[] o1;
|
delete[] o1;
|
||||||
|
Loading…
Reference in New Issue
Block a user