add complex random

This commit is contained in:
张壹 2024-09-22 12:11:17 +08:00
parent 7afca35c73
commit 00794ec956
2 changed files with 28 additions and 4 deletions

View File

@ -38,6 +38,21 @@ double gctl::random(double low, double hig)
return f*(hig-low) + low; return f*(hig-low) + low;
} }
std::complex<double> random(std::complex<double> low, std::complex<double> hig)
{
std::complex<double> c;
double r = (double) rand()/RAND_MAX;
double i = (double) rand()/RAND_MAX;
double rh = std::max(hig.real(), low.real());
double rl = std::min(hig.real(), low.real());
double ih = std::max(hig.imag(), low.imag());
double il = std::min(hig.imag(), low.imag());
c.real(r*(rh - rl) + rl);
c.imag(i*(ih - il) + il);
return c;
}
bool gctl::isequal(double f, double v, double eps) bool gctl::isequal(double f, double v, double eps)
{ {
return fabs(f - v) < eps; return fabs(f - v) < eps;

View File

@ -45,10 +45,9 @@ namespace gctl
* *
* @note Call srand(seed) to initiate the random squence before using this function. * @note Call srand(seed) to initiate the random squence before using this function.
* *
* @tparam T Value type
* @param low Lower bound * @param low Lower bound
* @param hig Higher bound * @param hig Higher bound
* @return T Random value * @return Random value
*/ */
int random(int low, int hig); int random(int low, int hig);
@ -57,13 +56,23 @@ namespace gctl
* *
* @note Call srand(seed) to initiate the random squence before using this function. * @note Call srand(seed) to initiate the random squence before using this function.
* *
* @tparam T Value type
* @param low Lower bound * @param low Lower bound
* @param hig Higher bound * @param hig Higher bound
* @return T Random value * @return Random value
*/ */
double random(double low, double hig); double random(double low, double hig);
/**
* @brief Return a random number in the range [low, hig]
*
* @note Call srand(seed) to initiate the random squence before using this function.
*
* @param low Lower bound
* @param hig Higher bound
* @return Random value
*/
std::complex<double> random(std::complex<double> low, std::complex<double> hig);
/** /**
* @brief eps eps 1e-109 * @brief eps eps 1e-109
* *