You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
#pragma once
|
|
|
|
#include <complex>
|
|
#include <random>
|
|
|
|
using dbl = std::complex<double>;
|
|
using dbl_complex = std::complex<double>;
|
|
|
|
inline std::complex<double> operator*(unsigned i, std::complex<double> z)
|
|
{
|
|
z *= i;
|
|
return z;
|
|
}
|
|
|
|
inline bool isnan(std::complex<double> const& z)
|
|
{
|
|
using std::isnan;
|
|
return isnan(z.real()) || isnan(z.imag());
|
|
}
|
|
|
|
inline double rand_real()
|
|
{
|
|
static std::default_random_engine generator;
|
|
static std::uniform_real_distribution<double> distribution(-1.0, 1.0);
|
|
return distribution(generator);
|
|
}
|
|
|
|
inline dbl pow(const dbl& z, int power)
|
|
{
|
|
if (power < 0) {
|
|
return pow(1. / z, -power);
|
|
} else if (power == 0)
|
|
return dbl(1, 0);
|
|
else if (power == 1)
|
|
return z;
|
|
else if (power == 2)
|
|
return z * z;
|
|
else if (power == 3)
|
|
return z * z * z;
|
|
else {
|
|
unsigned int p(power);
|
|
dbl result(1, 0), z_to_the_current_power_of_two = z;
|
|
// fast powering algorithm
|
|
do {
|
|
if ((p & 1) == 1) result *= z_to_the_current_power_of_two;
|
|
z_to_the_current_power_of_two *= z_to_the_current_power_of_two;
|
|
} while (p >>= 1);
|
|
|
|
return result;
|
|
}
|
|
}
|