# Algorithms_in_C++: numerical_methods Namespace Reference

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. More...
std::complex< double > * | FastFourierTransform (std::complex< double > *p, uint8_t n)
| FastFourierTransform is a recursive function which returns list of complex numbers. More...
std::complex< double > * | InverseFastFourierTransform (std::complex< double > *p, uint8_t n)
| InverseFastFourierTransform is a recursive function which returns list of complex numbers. More...

## Detailed Description

for assert

Numerical Methods.

for std::map container

for std::vector

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 storing points and coefficents for IO operations

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

  radicand | is 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

{
int i = 1; /// To find initial root or rough approximation
while (i * i <= radicand) {
i++;
}
i--; /// Real Initial value will be i-1 as loop stops on +1 value
double x0 = i; /// Storing previous value for comparison
double x1 =
(radicand / x0 + x0) / 2; /// Storing calculated value for comparison
double temp = NAN; /// Temp variable to x0 and x1
while (std::max(x0, x1) - std::min(x0, x1) < 0.0001) {
temp = (radicand / x1 + x1) / 2; /// Newly calculated root
x0 = x1;
x1 = temp;
}
return x1; /// Returning final root
}
std::max
T max(T... args)
std::min
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

  p | List of Coefficents in form of complex numbers
  n | Count 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

{
if (n == 1) {
return p; /// Base Case To return
}
double pi = 2 * asin(1.0); /// Declaring value of pi
std::complex<double> om = std::complex<double>(
cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega
auto *pe = new std::complex<double>[n / 2]; /// Coefficients of even power
auto *po = new std::complex<double>[n / 2]; /// Coefficients of odd power
int k1 = 0, k2 = 0;
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
pe[k1++] = p[j]; /// Assigning values of even Coefficients
} else {
po[k2++] = p[j]; /// Assigning value of odd Coefficients
}
}
std::complex<double> *ye =
FastFourierTransform(pe, n / 2); /// Recursive Call
std::complex<double> *yo =
FastFourierTransform(po, n / 2); /// Recursive Call
auto *y = new std::complex<double>[n]; /// Final value representation list
k1 = 0, k2 = 0;
for (int i = 0; i < n / 2; i++) {
y[i] =
ye[k1] + pow(om, i) * yo[k2]; /// Updating the first n/2 elements
y[i + n / 2] =
ye[k1] - pow(om, i) * yo[k2]; /// Updating the last n/2 elements
k1++;
k2++;
}
if (n != 2) {
delete[] pe;
delete[] po;
}
delete[] ye; /// Deleting dynamic array ye
delete[] yo; /// Deleting dynamic array yo
return y;
}
std::complex
numerical_methods::FastFourierTransform
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:41
std::pow
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

  p | List of Coefficents in form of complex numbers
  n | Count 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

{
if (n == 1) {
return p; /// Base Case To return
}
double pi = 2 * asin(1.0); /// Declaring value of pi
std::complex<double> om = std::complex<double>(
cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega
om.real(om.real() / 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 *po = new std::complex<double>[n / 2]; /// Coefficients of odd power
int k1 = 0, k2 = 0;
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
pe[k1++] = p[j]; /// Assigning values of even Coefficients
} else {
po[k2++] = p[j]; /// Assigning value of odd Coefficients
}
}
std::complex<double> *ye =
InverseFastFourierTransform(pe, n / 2); /// Recursive Call
std::complex<double> *yo =
InverseFastFourierTransform(po, n / 2); /// Recursive Call
auto *y = new std::complex<double>[n]; /// Final value representation list
k1 = 0, k2 = 0;
for (int i = 0; i < n / 2; i++) {
y[i] =
ye[k1] + pow(om, i) * yo[k2]; /// Updating the first n/2 elements
y[i + n / 2] =
ye[k1] - pow(om, i) * yo[k2]; /// Updating the last n/2 elements
k1++;
k2++;
}
if (n != 2) {
delete[] pe;
delete[] po;
}
delete[] ye; /// Deleting dynamic array ye
delete[] yo; /// Deleting dynamic array yo
return y;
}
std::complex::imag
T imag(T... args)
numerical_methods::InverseFastFourierTransform
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:33
std::complex::real
T real(T... args)
Here is the call graph for this function: