3 bool check_diff(
double x,
double y,
double frac_tolerance,
6 if(std::isnan(x))
return std::isnan(y);
7 return std::abs(x-y)<=(abs_tolerance+
8 frac_tolerance*std::min(std::abs(x), std::abs(y)));
11 bool check_diff(std::valarray<double> x, std::valarray<double> y,
12 std::valarray<double> frac_tolerance,
13 std::valarray<double> abs_tolerance)
16 for(
size_t i=0; i<x.size(); i++)
17 result=result && (std::abs(x[i]-y[i])<=
19 frac_tolerance[i]*std::min(std::abs(x[i]), std::abs(y[i]))));
24 return std::abs(a-b) < 1e-20;
27 double getError(
double predicted,
double actual) {
28 return (std::abs(actual - predicted)
30 std::max(std::abs(actual), std::abs(predicted)));
33 bool approxEqual(
double predicted,
double actual,
double thres)
35 if (
isEqual(predicted, actual))
return true;
36 return getError(predicted, actual) < thres;
51 return (max-min)*
static_cast<double>(std::rand())/RAND_MAX+min;
63 return (max-min)*rand()/RAND_MAX+min;
68 return rand()%(max-min+1)+min;
71 std::ostream &operator<<(
73 const std::valarray< std::valarray<double> > &poly_coef
76 for(
size_t age_i=0; age_i<poly_coef.size(); age_i++) {
77 for(
size_t mass_i=0; mass_i<poly_coef[age_i].size(); mass_i++) {
78 os << poly_coef[age_i][mass_i];
79 if(age_i) os <<
"*age";
80 if(age_i>1) os <<
"**" << age_i;
81 if(mass_i) os <<
"*m";
82 if(mass_i>1) os <<
"**" << mass_i;
83 if(age_i<poly_coef.size()-1 || mass_i<poly_coef.size()-1)
94 poly_coef.resize(3, std::valarray<double>(3));
96 for (
unsigned age_i=0; age_i < poly_coef.size(); age_i++) {
97 if (age_i > 1) age_fac *= age_i *
MAX_AGE;
99 for(
unsigned m_i = 0; m_i < poly_coef[age_i].size(); ++m_i) {
100 if(m_i>1) mass_fac *= m_i * max_mass;
101 poly_coef[age_i][m_i] = (
static_cast<double>(rand()) / RAND_MAX
110 std::valarray< std::valarray<double> > poly_coef(std::valarray<double>(3), 3);
116 const std::valarray< std::valarray<double> > &poly_coef,
119 std::valarray< std::valarray<double> > result(
120 std::valarray<double>(poly_coef[0].size()), poly_coef.size());
121 for(
size_t i=0; i<poly_coef.size(); i++)
122 for(
size_t j=0; j<poly_coef[0].size(); j++) {
123 double c_ij=poly_coef[i][j], offset_k=1.0;
125 for(
size_t k=0; k<=i; k++) {
126 result[i-k][j]+=c_ij*offset_k*binom_coef;
127 offset_k*=age_offset;
137 return (nCm*(n-m))/(m+1);
142 return 15.0 / (16.0 * M_PI * std::pow(10.0, lgQ));
150 (8.0 * M_PI * std::sqrt(1.0 + mass_ratio) * std::pow(10.0, lgQ))
bool approxEqual(double predicted, double actual, double thres=0.02)
double getError(double predicted, double actual)
Functions and classes of general use for all unit tests.
double rand_value(double min, double max)
A uniform random real value in the given range.
const double MAX_AGE
Most tests end at this age in Gyr.
double lag_from_lgQ(double lgQ)
Converts lg(Q) to a tidal phase lag.
const double solar_radius
Radius of the sun [m].
void rand_poly_coef(std::valarray< std::valarray< double > > &poly_coef, double max_mass=-1)
Fills the given valarray with a random set of polynomial coefficients.
double orbital_angmom_from_freq(double m1, double m2, double freq, double e)
The orbital angular momentum corresponding to the given frequency.
bool check_diff(double x, double y, double frac_tolerance, double abs_tolerance)
Returns true iff .
double uniform_rand(double min, double max)
Generates a uniformly distributed random number.
bool isEqual(double a, double b)
const double solar_mass
Mass of the sun [kg].
const double MAX_STELLAR_MASS
The highest stellar mass to use in tests in .
unsigned next_binom_coef(unsigned n, unsigned m, unsigned nCm)
Given n, m and (n)C(m) returns (n)C(m+1)
std::valarray< std::valarray< double > > offset_age(const std::valarray< std::valarray< double > > &poly_coef, double age_offset)
Returns new polynomial coefficienst such that output polynomial(mass, age+age_offset)=input polynomia...
const double G
Gravitational constant in SI.