TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
numerical_methods Namespace Reference

for assert More...

Functions

double babylonian_method (double radicand)
 Babylonian methods is an iterative function which returns square root of radicand.
 
std::complex< double > * FastFourierTransform (std::complex< double > *p, uint8_t n)
 FastFourierTransform is a recursive function which returns list of complex numbers.
 
std::complex< double > * InverseFastFourierTransform (std::complex< double > *p, uint8_t n)
 InverseFastFourierTransform is a recursive function which returns list of complex numbers.
 

Detailed Description

for assert

Numerical Methods.

for std::map container

for storing points and coefficents

for io operations

for math functions

for IO operations

Numerical algorithms/methods

for assert for integer allocation for std::atof for std::function for IO operations for std::map container

Numerical algorithms/methods

for math operations

Numerical methods

for assert for mathematical-related functions for IO operations for std::vector

Numerical algorithms/methods

for std::array for assert for fabs

Numerical Methods algorithms

for assert for math functions for integer allocation for std::atof for std::function for IO operations

Numerical algorithms/methods

Function Documentation

◆ babylonian_method()

double numerical_methods::babylonian_method ( double radicand)

Babylonian methods is an iterative function which returns square root of radicand.

Parameters
radicandis the radicand
Returns
x1 the square root of radicand

To find initial root or rough approximation

Real Initial value will be i-1 as loop stops on +1 value

Storing previous value for comparison

Storing calculated value for comparison

Temp variable to x0 and x1

Newly calculated root

Returning final root

Definition at line 30 of file babylonian_method.cpp.

30 {
31 int i = 1;
32
33 while (i * i <= radicand) {
34 i++;
35 }
36
37 i--;
38
39 double x0 = i;
40 double x1 =
41 (radicand / x0 + x0) / 2;
42 double temp = NAN;
43
44 while (std::max(x0, x1) - std::min(x0, x1) < 0.0001) {
45 temp = (radicand / x1 + x1) / 2;
46 x0 = x1;
47 x1 = temp;
48 }
49
50 return x1;
51}

◆ FastFourierTransform()

std::complex< double > * numerical_methods::FastFourierTransform ( std::complex< double > * p,
uint8_t n )

FastFourierTransform is a recursive function which returns list of complex numbers.

Parameters
pList of Coefficents in form of complex numbers
nCount of elements in list p
Returns
p if n==1
y if n!=1

Base Case To return

Declaring value of pi

Calculating value of omega

Coefficients of even power

Coefficients of odd power

Assigning values of even Coefficients

Assigning value of odd Coefficients

Recursive Call

Recursive Call

Final value representation list

Updating the first n/2 elements

Updating the last n/2 elements

Deleting dynamic array ye

Deleting dynamic array yo

Definition at line 42 of file fast_fourier_transform.cpp.

42 {
43 if (n == 1) {
44 return p;
45 }
46
47 double pi = 2 * asin(1.0);
48
49 std::complex<double> om = std::complex<double>(
50 cos(2 * pi / n), sin(2 * pi / n));
51
52 auto *pe = new std::complex<double>[n / 2];
53
54 auto *po = new std::complex<double>[n / 2];
55
56 int k1 = 0, k2 = 0;
57 for (int j = 0; j < n; j++) {
58 if (j % 2 == 0) {
59 pe[k1++] = p[j];
60
61 } else {
62 po[k2++] = p[j];
63 }
64 }
65
66 std::complex<double> *ye =
67 FastFourierTransform(pe, n / 2);
68
69 std::complex<double> *yo =
70 FastFourierTransform(po, n / 2);
71
72 auto *y = new std::complex<double>[n];
73
74 k1 = 0, k2 = 0;
75
76 for (int i = 0; i < n / 2; i++) {
77 y[i] =
78 ye[k1] + pow(om, i) * yo[k2];
79 y[i + n / 2] =
80 ye[k1] - pow(om, i) * yo[k2];
81
82 k1++;
83 k2++;
84 }
85
86 if (n != 2) {
87 delete[] pe;
88 delete[] po;
89 }
90
91 delete[] ye;
92 delete[] yo;
93 return y;
94}
std::complex< double > * FastFourierTransform(std::complex< double > *p, uint8_t n)
FastFourierTransform is a recursive function which returns list of complex numbers.

◆ InverseFastFourierTransform()

std::complex< double > * numerical_methods::InverseFastFourierTransform ( std::complex< double > * p,
uint8_t n )

InverseFastFourierTransform is a recursive function which returns list of complex numbers.

Parameters
pList of Coefficents in form of complex numbers
nCount of elements in list p
Returns
p if n==1
y if n!=1

Base Case To return

Declaring value of pi

Calculating value of omega

One change in comparison with DFT

One change in comparison with DFT

Coefficients of even power

Coefficients of odd power

Assigning values of even Coefficients

Assigning value of odd Coefficients

Recursive Call

Recursive Call

Final value representation list

Updating the first n/2 elements

Updating the last n/2 elements

Deleting dynamic array ye

Deleting dynamic array yo

Definition at line 34 of file inverse_fast_fourier_transform.cpp.

35 {
36 if (n == 1) {
37 return p;
38 }
39
40 double pi = 2 * asin(1.0);
41
42 std::complex<double> om = std::complex<double>(
43 cos(2 * pi / n), sin(2 * pi / n));
44
45 om.real(om.real() / n);
46 om.imag(om.imag() / n);
47
48 auto *pe = new std::complex<double>[n / 2];
49
50 auto *po = new std::complex<double>[n / 2];
51
52 int k1 = 0, k2 = 0;
53 for (int j = 0; j < n; j++) {
54 if (j % 2 == 0) {
55 pe[k1++] = p[j];
56
57 } else {
58 po[k2++] = p[j];
59 }
60 }
61
62 std::complex<double> *ye =
64
65 std::complex<double> *yo =
67
68 auto *y = new std::complex<double>[n];
69
70 k1 = 0, k2 = 0;
71
72 for (int i = 0; i < n / 2; i++) {
73 y[i] =
74 ye[k1] + pow(om, i) * yo[k2];
75 y[i + n / 2] =
76 ye[k1] - pow(om, i) * yo[k2];
77
78 k1++;
79 k2++;
80 }
81
82 if (n != 2) {
83 delete[] pe;
84 delete[] po;
85 }
86
87 delete[] ye;
88 delete[] yo;
89 return y;
90}
std::complex< double > * InverseFastFourierTransform(std::complex< double > *p, uint8_t n)
InverseFastFourierTransform is a recursive function which returns list of complex numbers.