bool calc_value( node_index_t node, const data_t & primary_data, cont_value_t secondary_value, mean_t primary_mean, mean_t secondary_mean, double secondary_variance, const primary_cov_model_t & primary_cov, const cross_cov_model_t & cross_cov, const n_lookup_t & n_lookup, cont_value_t & result) { typedef typename n_lookup_t::coord_t coord_t; std::vector<coord_t> coords; std::vector<node_index_t> indices; coord_t center; n_lookup.find(node, is_informed_predicate_t<data_t>(primary_data), center, indices, coords); if (indices.size() <= 0) return false; std::vector<kriging_weight_t> weights; if (!calc_weights(center, coords, primary_cov, cross_cov, secondary_variance, weights)) return false; std::vector<cont_value_t> values; select(primary_data, indices, values); if (!combine(values, weights, primary_mean, secondary_value, secondary_mean, result)) return false; return true; }
// Finds clusters for the given label's dists and puts the MCL mat in mclmat void cluster_dists(Mat &dists, Mat &mclmat, double r, const cluster_vars &vars) { //printf("\t\t\t - clustering dists...\n"); mclmat.create(dists.rows, dists.cols, dists.type()); // find weights calc_weights(dists, mclmat, vars.cluster_tierStep, vars.cluster_numTiers); // iterate mcl::cluster(mclmat, vars.mcl_iterations, vars.mcl_expansion_power, r, vars.mcl_prune_min); }
void nested_mpi::make_chain(){ //std::cout << "now we are making the chain....\n" << std::flush; int converged=0; do { choose_cpu_step(); take_step(); // If dump info, do it here and reset counters... if (dump_nb_mpi >= dump_lenght && myid == 0) { std::string name_here; calc_weights(); name_here=tag_name+"_pre_pre_chain"; dump_nb_mpi=0; //warning/N.B - these saves save from the start of the chain each time! save();save_pre(name_here); } if (conv_nb_mpi >= conv_lenght) { calc_weights(); conv_nb_mpi=0; converged=check_convergence(); if (myid == 0) if (verbose >= 1) std::cout<<"Convergence test:"<<converged<<"\n"<<std::flush; } } while ( steps_taken<=nb_steps && converged == 0 ); if (myid == 0) { std::cout<<"Finished:\n"<<std::flush; std::string name_here; // Now use calc_final_weights calc_final_weights(); name_here=tag_name+"_pre_pre_final_chain"; dump_nb_mpi=0; save();save_pre(name_here); if (verbose >= 1) std::cout<<"Saved\n"<<std::flush; } }
EMAlgorithm Bootstrap::run_em(const EMAlgorithm& em_start) { auto counts = mult_.sample(); auto weights = calc_weights(counts, ecmap_, eff_lens_); EMAlgorithm em(ecmap_, counts, target_names_, eff_lens_, weights); //em.set_start(em_start); em.run(10000, 20, false); /* em.compute_rho(); */ return em; }
static CvStatus icvFitLine3D( CvPoint3D32f * points, int count, int dist, float _param, float reps, float aeps, float *line ) { double EPS = count*FLT_EPSILON; void (*calc_weights) (float *, int, float *) = 0; void (*calc_weights_param) (float *, int, float *, float) = 0; float *w; /* weights */ float *r; /* square distances */ int i, j, k; float _line[6], _lineprev[6]; float rdelta = reps != 0 ? reps : 1.0f; float adelta = aeps != 0 ? aeps : 0.01f; double min_err = DBL_MAX, err = 0; CvRNG rng = cvRNG(-1); memset( line, 0, 6*sizeof(line[0]) ); switch (dist) { case CV_DIST_L2: return icvFitLine3D_wods( points, count, 0, line ); case CV_DIST_L1: calc_weights = icvWeightL1; break; case CV_DIST_L12: calc_weights = icvWeightL12; break; case CV_DIST_FAIR: calc_weights_param = icvWeightFair; break; case CV_DIST_WELSCH: calc_weights_param = icvWeightWelsch; break; case CV_DIST_HUBER: calc_weights_param = icvWeightHuber; break; /*case CV_DIST_USER: _PFP.p = param; calc_weights = (void ( * )(float *, int, float *)) _PFP.fp; break;*/ default: return CV_BADFACTOR_ERR; } w = (float *) cvAlloc( count * sizeof( float )); r = (float *) cvAlloc( count * sizeof( float )); for( k = 0; k < 20; k++ ) { int first = 1; for( i = 0; i < count; i++ ) w[i] = 0.f; for( i = 0; i < MIN(count,10); ) { j = cvRandInt(&rng) % count; if( w[j] < FLT_EPSILON ) { w[j] = 1.f; i++; } } icvFitLine3D_wods( points, count, w, _line ); for( i = 0; i < 30; i++ ) { double sum_w = 0; if( first ) { first = 0; } else { double t = _line[0] * _lineprev[0] + _line[1] * _lineprev[1] + _line[2] * _lineprev[2]; t = MAX(t,-1.); t = MIN(t,1.); if( fabs(acos(t)) < adelta ) { float x, y, z, ax, ay, az, dx, dy, dz, d; x = _line[3] - _lineprev[3]; y = _line[4] - _lineprev[4]; z = _line[5] - _lineprev[5]; ax = _line[0] - _lineprev[0]; ay = _line[1] - _lineprev[1]; az = _line[2] - _lineprev[2]; dx = (float) fabs( y * az - z * ay ); dy = (float) fabs( z * ax - x * az ); dz = (float) fabs( x * ay - y * ax ); d = dx > dy ? (dx > dz ? dx : dz) : (dy > dz ? dy : dz); if( d < rdelta ) break; } } /* calculate distances */ if( icvCalcDist3D( points, count, _line, r ) < FLT_EPSILON*count ) break; /* calculate weights */ if( calc_weights ) calc_weights( r, count, w ); else calc_weights_param( r, count, w, _param ); for( j = 0; j < count; j++ ) sum_w += w[j]; if( fabs(sum_w) > FLT_EPSILON ) { sum_w = 1./sum_w; for( j = 0; j < count; j++ ) w[j] = (float)(w[j]*sum_w); } else { for( j = 0; j < count; j++ ) w[j] = 1.f; } /* save the line parameters */ memcpy( _lineprev, _line, 6 * sizeof( float )); /* Run again... */ icvFitLine3D_wods( points, count, w, _line ); } if( err < min_err ) { min_err = err; memcpy( line, _line, 6 * sizeof(line[0])); if( err < EPS ) break; } } // Return... cvFree( &w ); cvFree( &r ); return CV_OK; }
static real gather_f(FILE *log,bool bVerbose, int start,int nr,rvec x[],rvec f[],real charge[],rvec box, real pot[],t_fftgrid *grid,rvec beta,t_nrnb *nrnb) { static bool bFirst=TRUE; static int *nnx,*nny,*nnz; static int JCXYZ[81]; int i,m; real energy; real qi,pi; ivec ixyz; rvec invh,c1,c2; real WXYZ[27]; real c1x,c1y,c1z,c2x,c2y,c2z; int ixw[7],iyw[7],izw[7]; int ll; int nx,ny,nz,la2,la12; t_fft_r *ptr; unpack_fftgrid(grid,&nx,&ny,&nz,&la2,&la12,TRUE,&ptr); calc_invh(box,nx,ny,nz,invh); for(m=0; (m<DIM); m++) { c1[m] = (beta[m]/2.0)*invh[m]; c2[m] = ((1.0-beta[m])/4.0)*invh[m]; } c1x = c1[XX]; c1y = c1[YY]; c1z = c1[ZZ]; c2x = c2[XX]; c2y = c2[YY]; c2z = c2[ZZ]; if (bFirst) { fprintf(log,"Gathering Forces using Triangle Shaped on %dx%dx%d grid\n", nx,ny,nz); fprintf(log,"beta = %10g,%10g,%10g\n",beta[XX],beta[YY],beta[ZZ]); fprintf(log,"c1 = %10g,%10g,%10g\n",c1[XX],c1[YY],c1[ZZ]); fprintf(log,"c2 = %10g,%10g,%10g\n",c2[XX],c2[YY],c2[ZZ]); fprintf(log,"invh = %10g,%10g,%10g\n",invh[XX],invh[YY],invh[ZZ]); calc_nxyz(nx,ny,nz,&nnx,&nny,&nnz); for(i=0; (i<27); i++) { JCXYZ[3*i] = 2 + (i/9); JCXYZ[3*i+1] = 2 + (i/3) % 3; JCXYZ[3*i+2] = 2 + (i % 3); } bFirst = FALSE; } energy=0.0; for(i=start; (i<start+nr); i++) { /* Each charge is spread over the nearest 27 grid cells, * thus we loop over -1..1 in X,Y and Z direction * We apply the TSC (triangle shaped charge) * see Luty et. al, JCP 103 (1995) 3014 */ calc_weights(i,nx,ny,nz,x[i],box,invh,ixyz,WXYZ); for(ll=llim2; (ll<=ulim2); ll++) { ixw[ll-llim2] = nnx[ixyz[XX]+ll+nx]; iyw[ll-llim2] = nny[ixyz[YY]+ll+ny]; izw[ll-llim2] = nnz[ixyz[ZZ]+ll+nz]; } qi = charge[i]; pi = gather_inner(JCXYZ,WXYZ,ixw,iyw,izw,la2,la12, c1x,c1y,c1z,c2x,c2y,c2z, qi,f[i],ptr); energy += pi*qi; pot[i] = pi; } inc_nrnb(nrnb,eNR_GATHERF,27*nr); inc_nrnb(nrnb,eNR_WEIGHTS,3*nr); return energy*0.5; }
void spread_q(FILE *log,bool bVerbose, int start,int nr, rvec x[],real charge[],rvec box, t_fftgrid *grid,t_nrnb *nrnb) { static bool bFirst = TRUE; static int *nnx,*nny,*nnz; rvec invh; real qi,qwt; #ifdef DEBUG real qt; #endif real WXYZ[27]; ivec ixyz; int i,iX,iY,iZ,index; int jx,jy,jz,jcx,jcy,jcz; int nxyz; int nx,ny,nz,la2,la12; t_fft_r *ptr; unpack_fftgrid(grid,&nx,&ny,&nz,&la2,&la12,TRUE,&ptr); calc_invh(box,nx,ny,nz,invh); if (bFirst) { fprintf(log,"Spreading Charges using Triangle Shaped on %dx%dx%d grid\n", nx,ny,nz); fprintf(log,"invh = %10g,%10g,%10g\n",invh[XX],invh[YY],invh[ZZ]); calc_nxyz(nx,ny,nz,&nnx,&nny,&nnz); bFirst = FALSE; } for(i=start; (i<start+nr); i++) { qi=charge[i]; /* Each charge is spread over the nearest 27 grid cells, * thus we loop over -1..1 in X,Y and Z direction * We apply the TSC (triangle shaped charge) * see Luty et. al, JCP 103 (1995) 3014 */ if (fabs(qi) > GMX_REAL_MIN) { calc_weights(i,nx,ny,nz,x[i],box,invh,ixyz,WXYZ); iX = ixyz[XX] + nx; iY = ixyz[YY] + ny; iZ = ixyz[ZZ] + nz; #ifdef DEBUG qt=0; #endif nxyz = 0; for(jx=-1; (jx<=1); jx++) { jcx = nnx[iX + jx]; for(jy=-1; (jy<=1); jy++) { jcy = nny[iY + jy]; for(jz=-1; (jz<=1); jz++,nxyz++) { jcz = nnz[iZ + jz]; index = INDEX(jcx,jcy,jcz); qwt = qi*WXYZ[nxyz]; grid->ptr[index]+=qwt; #ifdef DEBUG qt += qwt; if (bVerbose) fprintf(log,"spread %4d %2d %2d %2d %10.3e, weight=%10.3e\n", index,jcx,jcy,jcz,grid->ptr[index],WXYZ[nxyz]); #endif } } } #ifdef DEBUG fprintf(log,"q[%3d] = %6.3f, qwt = %6.3f\n",i,qi,qt); #endif } } inc_nrnb(nrnb,eNR_SPREADQ,9*nr); inc_nrnb(nrnb,eNR_WEIGHTS,3*nr); }
/* Takes an array of 3D points, type of distance (including user-defined distance specified by callbacks, fills the array of four floats with line parameters A, B, C, D, E, F, where (A, B, C) is the normalized direction vector, (D, E, F) is the point that belongs to the line. */ static void fitLine3D( Point3f * points, int count, int dist, float _param, float reps, float aeps, float *line ) { double EPS = count*FLT_EPSILON; void (*calc_weights) (float *, int, float *) = 0; void (*calc_weights_param) (float *, int, float *, float) = 0; int i, j, k; float _line[6]={0,0,0,0,0,0}, _lineprev[6]={0,0,0,0,0,0}; float rdelta = reps != 0 ? reps : 1.0f; float adelta = aeps != 0 ? aeps : 0.01f; double min_err = DBL_MAX, err = 0; RNG rng((uint64)-1); switch (dist) { case CV_DIST_L2: return fitLine3D_wods( points, count, 0, line ); case CV_DIST_L1: calc_weights = weightL1; break; case CV_DIST_L12: calc_weights = weightL12; break; case CV_DIST_FAIR: calc_weights_param = weightFair; break; case CV_DIST_WELSCH: calc_weights_param = weightWelsch; break; case CV_DIST_HUBER: calc_weights_param = weightHuber; break; default: CV_Error(CV_StsBadArg, "Unknown distance"); } AutoBuffer<float> buf(count*2); float *w = buf, *r = w + count; for( k = 0; k < 20; k++ ) { int first = 1; for( i = 0; i < count; i++ ) w[i] = 0.f; for( i = 0; i < MIN(count,10); ) { j = rng.uniform(0, count); if( w[j] < FLT_EPSILON ) { w[j] = 1.f; i++; } } fitLine3D_wods( points, count, w, _line ); for( i = 0; i < 30; i++ ) { double sum_w = 0; if( first ) { first = 0; } else { double t = _line[0] * _lineprev[0] + _line[1] * _lineprev[1] + _line[2] * _lineprev[2]; t = MAX(t,-1.); t = MIN(t,1.); if( fabs(acos(t)) < adelta ) { float x, y, z, ax, ay, az, dx, dy, dz, d; x = _line[3] - _lineprev[3]; y = _line[4] - _lineprev[4]; z = _line[5] - _lineprev[5]; ax = _line[0] - _lineprev[0]; ay = _line[1] - _lineprev[1]; az = _line[2] - _lineprev[2]; dx = (float) fabs( y * az - z * ay ); dy = (float) fabs( z * ax - x * az ); dz = (float) fabs( x * ay - y * ax ); d = dx > dy ? (dx > dz ? dx : dz) : (dy > dz ? dy : dz); if( d < rdelta ) break; } } /* calculate distances */ err = calcDist3D( points, count, _line, r ); //if( err < FLT_EPSILON*count ) // break; /* calculate weights */ if( calc_weights ) calc_weights( r, count, w ); else calc_weights_param( r, count, w, _param ); for( j = 0; j < count; j++ ) sum_w += w[j]; if( fabs(sum_w) > FLT_EPSILON ) { sum_w = 1./sum_w; for( j = 0; j < count; j++ ) w[j] = (float)(w[j]*sum_w); } else { for( j = 0; j < count; j++ ) w[j] = 1.f; } /* save the line parameters */ memcpy( _lineprev, _line, 6 * sizeof( float )); /* Run again... */ fitLine3D_wods( points, count, w, _line ); } if( err < min_err ) { min_err = err; memcpy( line, _line, 6 * sizeof(line[0])); if( err < EPS ) break; } } }
static void fitLine2D( const Point2f * points, int count, int dist, float _param, float reps, float aeps, float *line ) { double EPS = count*FLT_EPSILON; void (*calc_weights) (float *, int, float *) = 0; void (*calc_weights_param) (float *, int, float *, float) = 0; int i, j, k; float _line[6], _lineprev[6]; float rdelta = reps != 0 ? reps : 1.0f; float adelta = aeps != 0 ? aeps : 0.01f; double min_err = DBL_MAX, err = 0; RNG rng((uint64)-1); memset( line, 0, 4*sizeof(line[0]) ); switch (dist) { case CV_DIST_L2: return fitLine2D_wods( points, count, 0, line ); case CV_DIST_L1: calc_weights = weightL1; break; case CV_DIST_L12: calc_weights = weightL12; break; case CV_DIST_FAIR: calc_weights_param = weightFair; break; case CV_DIST_WELSCH: calc_weights_param = weightWelsch; break; case CV_DIST_HUBER: calc_weights_param = weightHuber; break; /*case DIST_USER: calc_weights = (void ( * )(float *, int, float *)) _PFP.fp; break;*/ default: CV_Error(CV_StsBadArg, "Unknown distance type"); } AutoBuffer<float> wr(count*2); float *w = wr, *r = w + count; for( k = 0; k < 20; k++ ) { int first = 1; for( i = 0; i < count; i++ ) w[i] = 0.f; for( i = 0; i < MIN(count,10); ) { j = rng.uniform(0, count); if( w[j] < FLT_EPSILON ) { w[j] = 1.f; i++; } } fitLine2D_wods( points, count, w, _line ); for( i = 0; i < 30; i++ ) { double sum_w = 0; if( first ) { first = 0; } else { double t = _line[0] * _lineprev[0] + _line[1] * _lineprev[1]; t = MAX(t,-1.); t = MIN(t,1.); if( fabs(acos(t)) < adelta ) { float x, y, d; x = (float) fabs( _line[2] - _lineprev[2] ); y = (float) fabs( _line[3] - _lineprev[3] ); d = x > y ? x : y; if( d < rdelta ) break; } } /* calculate distances */ err = calcDist2D( points, count, _line, r ); if( err < EPS ) break; /* calculate weights */ if( calc_weights ) calc_weights( r, count, w ); else calc_weights_param( r, count, w, _param ); for( j = 0; j < count; j++ ) sum_w += w[j]; if( fabs(sum_w) > FLT_EPSILON ) { sum_w = 1./sum_w; for( j = 0; j < count; j++ ) w[j] = (float)(w[j]*sum_w); } else { for( j = 0; j < count; j++ ) w[j] = 1.f; } /* save the line parameters */ memcpy( _lineprev, _line, 4 * sizeof( float )); /* Run again... */ fitLine2D_wods( points, count, w, _line ); } if( err < min_err ) { min_err = err; memcpy( line, _line, 4 * sizeof(line[0])); if( err < EPS ) break; } } }