Algorithms_in_C++ 1.0.0
Set of 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

30 {
31 int i = 1; /// To find initial root or rough approximation
32
33 while (i * i <= radicand) {
34 i++;
35 }
36
37 i--; /// Real Initial value will be i-1 as loop stops on +1 value
38
39 double x0 = i; /// Storing previous value for comparison
40 double x1 =
41 (radicand / x0 + x0) / 2; /// Storing calculated value for comparison
42 double temp = NAN; /// Temp variable to x0 and x1
43
44 while (std::max(x0, x1) - std::min(x0, x1) < 0.0001) {
45 temp = (radicand / x1 + x1) / 2; /// Newly calculated root
46 x0 = x1;
47 x1 = temp;
48 }
49
50 return x1; /// Returning final root
51}
T max(T... args)
T min(T... args)
Here is the call graph for this function:

◆ 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

42 {
43 if (n == 1) {
44 return p; /// Base Case To return
45 }
46
47 double pi = 2 * asin(1.0); /// Declaring value of pi
48
50 cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega
51
52 auto *pe = new std::complex<double>[n / 2]; /// Coefficients of even power
53
54 auto *po = new std::complex<double>[n / 2]; /// Coefficients of odd power
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]; /// Assigning values of even Coefficients
60
61 } else {
62 po[k2++] = p[j]; /// Assigning value of odd Coefficients
63 }
64 }
65
67 FastFourierTransform(pe, n / 2); /// Recursive Call
68
70 FastFourierTransform(po, n / 2); /// Recursive Call
71
72 auto *y = new std::complex<double>[n]; /// Final value representation list
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]; /// Updating the first n/2 elements
79 y[i + n / 2] =
80 ye[k1] - pow(om, i) * yo[k2]; /// Updating the last n/2 elements
81
82 k1++;
83 k2++;
84 }
85
86 if (n != 2) {
87 delete[] pe;
88 delete[] po;
89 }
90
91 delete[] ye; /// Deleting dynamic array ye
92 delete[] yo; /// Deleting dynamic array 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.
Definition fast_fourier_transform.cpp:42
T pow(T... args)
Here is the call graph for this function:

◆ 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

35 {
36 if (n == 1) {
37 return p; /// Base Case To return
38 }
39
40 double pi = 2 * asin(1.0); /// Declaring value of pi
41
43 cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega
44
45 om.real(om.real() / n); /// One change in comparison with DFT
46 om.imag(om.imag() / n); /// One change in comparison with DFT
47
48 auto *pe = new std::complex<double>[n / 2]; /// Coefficients of even power
49
50 auto *po = new std::complex<double>[n / 2]; /// Coefficients of odd power
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]; /// Assigning values of even Coefficients
56
57 } else {
58 po[k2++] = p[j]; /// Assigning value of odd Coefficients
59 }
60 }
61
63 InverseFastFourierTransform(pe, n / 2); /// Recursive Call
64
66 InverseFastFourierTransform(po, n / 2); /// Recursive Call
67
68 auto *y = new std::complex<double>[n]; /// Final value representation list
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]; /// Updating the first n/2 elements
75 y[i + n / 2] =
76 ye[k1] - pow(om, i) * yo[k2]; /// Updating the last n/2 elements
77
78 k1++;
79 k2++;
80 }
81
82 if (n != 2) {
83 delete[] pe;
84 delete[] po;
85 }
86
87 delete[] ye; /// Deleting dynamic array ye
88 delete[] yo; /// Deleting dynamic array yo
89 return y;
90}
T imag(T... args)
std::complex< double > * InverseFastFourierTransform(std::complex< double > *p, uint8_t n)
InverseFastFourierTransform is a recursive function which returns list of complex numbers.
Definition inverse_fast_fourier_transform.cpp:34
T real(T... args)
Here is the call graph for this function: