static inline double gauss() { double u, v, x; do { v = nextDouble(); do u = nextDouble(); while (u == 0); x = 1.71552776992141359295 * (v - 0.5) / u; } while ( x * x > -4.0 * log(u) ); return x; }
int main(int argc,char *argv[]) { int n=4; //Solve an n x n system if (argc>1) n=atoi(argv[1]); int r,c; //Generate a random matrix and "unknown" x double *A=new double[n*n], *true_x=new double[n]; srand(2); for (r=0;r<n;r++) { for (c=0;c<n;c++) { if (c<r) A[r*n+c]=A[c*n+r]; //Symmetric copy else A[r*n+c]=nextDouble(); } true_x[r]=nextDouble(); } serial_comm comm(n,A); //Compute the target vector as b = A x double *b=new double[n], *x=new double[n]; comm.matrixVectorProduct(true_x,b); //Use solver to try to recover true_x ILSI_Param param; ILSI_Param_new(¶m); const double tolerance=1.0e-5; param.maxResidual=tolerance; printf("Solving...\n"); ILSI_CG_Solver(¶m,&comm, n,b,x); //Compare the true and estimated solutions double totalErr=0; for (r=0;r<n;r++) { double err=fabs(x[r]-true_x[r]); if (err>tolerance) { printf("Big difference %g found at position %d!\n",err,r); exit(1); } totalErr+=err; } printf("Solved: est. res=%g, err=%g, iter=%d\n", param.residual, totalErr, (int)param.iterations); exit(0); }
std::string TrampolineMgr::ArgumentWalker::next(char type) { std::stringstream ss; void* ptr; if (type == 'u') ss << unsigned(nextInt()); else if (type == 'i') ss << nextInt(); else if (type == 'q') ss << nextLL(); else if (type == 'f') ss << nextFloat(); else if (type == 'd') ss << nextDouble(); else if (type == 'p' || isupper(type)) ss << nextPointer(); else if (type == 'c') ss << char(nextInt()); else if (type == 's') { const char* s = (const char*) nextPointer(); ss << (void*)s; if (s) ss << " \"" << safeString(s) << '"'; } else if (type == 'v') ss << "(void)"; else ss << '?'; if (isupper(type)) m_pointers.push_back(std::make_pair(tolower(type), ptr)); return ss.str(); }
double Random::nextGauss(double mean, double standardDeviation) { return mean +( sqrt( -2.0*log(1.0 - nextDouble()) ) * cos( 6.283185307 * nextDouble() ) ) * standardDeviation; }
unsigned int Random::nextUInt(const unsigned int &min, const unsigned int &max) { return round(min + (max-min)*nextDouble()); }
double Random::nextDoubleRange( double lo, double hi ) { double range = hi - lo; return( lo + range * nextDouble() ); }
float Random::nextFloat() { return static_cast< float >( nextDouble() ); }
int Random::nextInt(int upperLimit) { return std::floor(nextDouble() * upperLimit); }
double Random::nextGaussian(double mean, double standardDeviation) { double standardNormalRandomNumber = sqrt( -2.0*log(1.0 - nextDouble()) ) * cos( 6.283185307 * nextDouble() ); return standardDeviation*standardNormalRandomNumber + mean; }