void sphereVolSample(vector<float>& cen, float rad, vector<float>& sample) { float dx, dy, dz; while(1) { dx = (ran01() * 2 - 1) * rad; dy = (ran01() * 2 - 1) * rad; dz = rad*rad - dx*dx - dy*dy; if(dz < 0) continue; dz = sqrt(dz); if(ran01() > 0.5) dz *= -1; sample[0] = cen[0] + dx; sample[1] = cen[1] + dy; sample[2] = cen[2] + dz; return; } }
// generates a random normalized vector // is uniformly distributed over sphere surface // taken from sphere point picking in mathworld void randomNormalVector(vector<float> & v) { // while(1) { // v[0] = -1. + 2.* ran01(); // v[1] = -1. + 2.* ran01(); // v[2] = 1 - v[0]*v[0] + v[1]*v[1]; // if(v[2] < 0) continue; // v[2] = sqrt(v[2]); // return; // } float theta = 2. * M_PI * ran01(); float phi = acos(2. * ran01() - 1); v[0] = sin(phi) * cos(theta); v[1] = sin(phi) * sin(theta); v[2] = cos(phi); }
long int roulette(double* elments, long element_size) { int i=0; double sum=0.0; double rnd=0.0; double* prob=malloc( element_size*sizeof(double) ); rnd = ran01( &seed ); for(i=0;i<element_size; i++) { sum += elments[i]; } for(i=0;i<element_size; i++) { prob[i]=elments[i]/sum; } i=0; sum=prob[i]; while(sum<=rnd) { i++; sum+=prob[i]; } free(prob); return i; }
// given a vector X, find normal vectors Y,Z such that X,Y,Z are mutually perpendicular void findYZgivenX( vector<float> & givenX, vector<float> & Y, vector<float> & Z ) { vector<float> X(3); normalize_point(X, givenX); int a=0, b=1, c=2; if( fabs(X[a]) < 1e-2 ) { a=1; b=2; c=0; } if( fabs(X[a]) < 1e-2 ) { a=2; b=0; c=1; } assert ( fabs(X[a]) > 1e-2 ); Y[b] = -1. + (2.*ran01()); Y[c] = -1. + (2.*ran01()); //cout << "rands " << Y[b] << "\nrands " << Y[c] << endl; Y[a] = -1 * (X[b]*Y[b] + X[c]*Y[c]) / X[a]; normalize_point(Y, Y); cross_product(Z, X, Y); normalize_point(Z, Z); }
int rnd(int low,int high) /* Pick a random integer between low and high */ { int i; if(low >= high) i = low; else { i = (int)((ran01(&seed) * (high - low + 1)) + low); if(i > high) i = high; } return(i); }
int find_var_valueno(int vno) { double q , sum ,ranno; int valueno, nmax; find_prob(vno); q=ran01(&seed); ranno=ran01(&seed); valueno = find_no_of_max( vno, &nmax ); /*printf(" for %d vno, q= %f ,ranno= %f , nmax =%d valueno= %d \n",vno,q,ranno,nmax,valueno);*/ if((q<q0) && (nmax==1)) { #if (localupdate==1) (var[vno]).trail[valueno] *= (1-gamma); #endif return valueno; } else { sum=0.0; for(valueno=0; valueno -1 < var[vno].nvalues && sum < ranno ; valueno++) { sum += (var[vno]).prob[valueno] ; }//endfo valueno loop #if (localupdate==1) if(valueno > 0 && valueno -1 < var[vno].nvalues) { (var[vno]).trail[valueno-1] *= (1-rho); } #endif return (valueno-1); }//end of else }//end of find valueno
int sample_from_probs(double *probs, int num_elements){ double sum=0; int i; for(i=0; i<num_elements; i++) sum+= probs[i]; double randnum = ran01(&seed) * sum; sum = 0.0; for(i=0; i<num_elements; i++){ sum+= probs[i]; if( randnum <= sum ) break; } if( i>= num_elements ){ printf("Error in sampling, didn't sample any of the values, sum=%lf\n, randnum=%lf", sum, randnum); exit(-1); } return i; }
// if fwd, C,N,CA are C,N,CA, else assumed to be N,C,CA void InformedPhipsiSampler::sample( vector<float>& curC, vector<float>& curN, vector<float>& curCA, float & phi, float & psi, float & omega, float & r, float & a, float & t) { float d0 = calcDist(target, curCA); float cisdist = 2.8, transdist = 3.81; bool cis = false, trans = false; if(cisdist > d0 - targetTol && cisdist < d0 + targetTol) cis = true; if(transdist > d0 - targetTol && transdist < d0 + targetTol) trans = true; if(cis && trans) { // if both cis and trans are possible, choose one if(ran01() > 0.95) trans = false; } else if(!cis && !trans) { cout << "target unreachable " <<curCA[0]<<" "<<curCA[1]<<" "<<curCA[2] << " : " << d0 <<" : "<< targetTol <<" : "<< target[0]<<" "<<target[1]<<" "<<target[2] << endl; exit(0); } // now either cis or trans is true float interCA; if(cis) { omega = 0; interCA = cisdist; } if(trans) { omega = -180; interCA = transdist; } phi = -999; psi = -999; // find a,t which take u into restraint sphere with this interCA vector<float> tar(3,0), noi(3,0); while(1) { // add some noise to target, proportional to noise randomNormalVector(noi); linear_combination(tar, 1, target, targetTol*ran01(), noi); // for this interCA, find a,t that takes you closest to target r = calcDist(curCA, tar); a = calcAngle(curN, curCA, tar); t = calcDihed(curC, curN, curCA, tar); find4thPoint(tar, curC, curN, curCA, interCA, a, t); if( calcDist(target, tar) <= targetTol ) break; } // return a phi-psi value for this a,t if available if(fwd) { RATdata & ratdata = RATdata::fwdinstance(RATdataPath.c_str()); vector<PSO>::iterator bi, ei; ratdata.range(resn, interCA, a, t, bi, ei); int rs = ei - bi; assert(rs >= 0); if(rs == 0) { return; } int incr = (int) floor ( (0.+rs) * ran01() ); bi += incr; phi = bi->r; psi = bi->a; omega = bi->t; } else { RATdata & ratdata = RATdata::bwdinstance(RATdataPath.c_str()); vector<PSO>::iterator bi, ei; ratdata.range(resn, interCA, a, t, bi, ei); int rs = ei - bi; assert(rs >= 0); if(rs == 0) { return; } int incr = (int) floor ( (0.+rs) * ran01() ); bi += incr; psi = bi->r; phi = bi->a; omega = bi->t; } //cout << phi <<" "<< psi <<" "<< omega << " shd take u from ("<< curCA[0]<<" "<< curCA[1]<<" "<< curCA[2] <<" "; //vector<float> exppt(3,0); //find4thPoint(exppt, curC, curN, curCA, interCA, a, t); //cout << ") to ("<< exppt[0]<<" "<< exppt[1]<<" "<< exppt[2] <<") " << endl;; }
int FragSampler::sample(vector<vector<float> > & pts) { int myrand = (int) floor ( pts.size() * ran01() ); pts = samples[myrand]; }
double rndreal(double lo ,double hi) /* real random number between specified limits */ { return((ran01(&seed) * (hi - lo)) + lo); }