int UCBKRandomized::getNextAction() { vector< double > cumsum( getArmNumber()+1 ); int i; cumsum[0] = 0.0; for( int i=0; i < getArmNumber(); i++ ) { cumsum[i+1] = cumsum[i] + _valueRecord[i]->first; } for( i=0; i < getArmNumber(); i++ ) { cumsum[i+1] /= cumsum[ getArmNumber() ]; } double r = rand() / (double) RAND_MAX; for( i=0; i < getArmNumber(); i++ ) { if ( (cumsum[i] <= r) && ( r<=cumsum[i+1] ) ) break; } return i; }
//X,P must be given at logscale //wrapper function for recursion methods: void prodrecfunction(double *pvalue, double *LRsuspect, double *LRlist, double *Plist, int *M, int *G,double *inflationfactor) { int *cG = malloc(sizeof(int)*((*M)+1)); cumsum(G, M, cG); #ifdef DEBUG printf("pval=%lf\n",*pvalue); printf("LRsuspect=%lf\n",*LRsuspect); printf("#markers=%d\n",*M); int i,j; for(i=0; i<*M; i++) { printf("nA[%i]=%d\n",i,G[i]); printf("CnA[%i]=%d\n",i,cG[i]); printf("Inflator[%i]=%lf\n",i,inflationfactor[i]); for(j=0; j<G[i]; j++) { printf("X[%d,%d]=%lf\n",i,j,LRlist[cG[i]+j]); } for(j=0; j<G[i]; j++) { printf("P[%d,%d]=%lf\n",i,j,Plist[cG[i]+j]); } printf("\n"); } #endif int marker = 0; double score = 1.0; double pval = 1; *pvalue = recfun(marker, score, pval, LRsuspect, LRlist, Plist, M, G, cG, inflationfactor); free(cG); }
//---------------------------------------------------------------- //---------------------------------------------------------------- int Exp3::getNextAction() { vector< AlphaReal > cumsum( getArmNumber()+1 ); int i; cumsum[0] = 0.0; for( int i=0; i < getArmNumber(); i++ ) { cumsum[i+1] = cumsum[i] + _pHat[i]; } for( i=0; i < getArmNumber(); i++ ) { cumsum[i+1] /= cumsum[ getArmNumber() ]; } AlphaReal r = rand() / (AlphaReal) RAND_MAX; for( i=0; i < getArmNumber(); i++ ) { if ( (cumsum[i] <= r) && ( r<=cumsum[i+1] ) ) break; } return i; }
Particles ResamplingMultinomialCUDA::resample(Particles* particles){ Particles resampledSet; resampledSet.samples = fmat(particles->samples.n_rows,particles->samples.n_cols); resampledSet.weights = frowvec(particles->weights.n_cols); fvec cumsum = zeros<fvec>(particles->weights.n_cols); fvec random = randu<fvec>(particles->weights.n_cols); for (unsigned int i=1; i < particles->weights.n_cols;++i){ cumsum(i) = cumsum(i-1) + particles->weights(i); } random=random * cumsum(cumsum.n_rows-1); // sort random random = sort(random); for (unsigned int j=0; j < random.n_rows; ++j){ for (unsigned int i=0 ; i < cumsum.n_rows; ++i){ if (random(j) <= cumsum(i)){ if(i > 0){ if(random(j) >= cumsum(i-1)) { for (unsigned int k=0;k<particles->samples.n_rows;++k){ resampledSet.samples(k,j) = particles->samples(k,i); } break; } } else { for (unsigned int k=0;k<particles->samples.n_rows;++k){ resampledSet.samples(k,j) = particles->samples(k,i); } break; } } // Normalize weights resampledSet.weights(j) = 1.0f/particles->weights.n_cols; } } return resampledSet; }
//' Generate a Random Walk without Drift //' //' Generates a random walk without drift. //' @param N An \code{integer} for signal length. //' @param sigma2 A \code{double} that contains process variance. //' @return grw A \code{vec} containing the random walk without drift. //' @backref src/gen_process.cpp //' @backref src/gen_process.h //' @keywords internal //' @examples //' gen_rw(10, 8.2) // [[Rcpp::export]] arma::vec gen_rw(const unsigned int N, const double sigma2 = 1) { arma::vec grw(N); double sigma = sqrt(sigma2); for(unsigned int i = 0; i < N; i++){ grw(i) = R::rnorm(0.0, sigma); } return cumsum(grw); }
SEXP do_cum(SEXP call, SEXP op, SEXP args, SEXP env) { SEXP s, t, ans; int i; checkArity(op, args); if (DispatchGroup("Math", call, op, args, env, &ans)) return ans; if (isComplex(CAR(args))) { t = CAR(args); s = allocVector(CPLXSXP, LENGTH(t)); setAttrib(s, R_NamesSymbol, getAttrib(t, R_NamesSymbol)); for (i = 0 ; i < length(t) ; i++) { COMPLEX(s)[i].r = NA_REAL; COMPLEX(s)[i].i = NA_REAL; } switch (PRIMVAL(op) ) { case 1: /* cumsum */ return ccumsum(t, s); break; case 2: /* cumprod */ return ccumprod(t, s); break; case 3: /* cummax */ case 4: /* cummin */ errorcall(call, _("min/max not defined for complex numbers")); break; default: errorcall(call, _("unknown cumxxx function")); } } else { /* Non-Complex: here, (sh|c)ould differentiate real / int */ PROTECT(t = coerceVector(CAR(args), REALSXP)); s = allocVector(REALSXP, LENGTH(t)); setAttrib(s, R_NamesSymbol, getAttrib(t, R_NamesSymbol)); for(i = 0 ; i < length(t) ; i++) REAL(s)[i] = NA_REAL; UNPROTECT(1); switch (PRIMVAL(op) ) { case 1: /* cumsum */ return cumsum(t,s); break; case 2: /* cumprod */ return cumprod(t,s); break; case 3: /* cummax */ return cummax(t,s); break; case 4: /* cummin */ return cummin(t,s); break; default: errorcall(call, _("unknown cumxxx function")); } } return R_NilValue; /* for -Wall */ }
/* "getBestStump()" function saves the best decision stump's threshold, weak hypothesis and its error; */ void DSC::getBestStump(Threshold &threshold, ivec &hypothesis, double &error) { /* Separating weights by classes: */ mat class1Weights = weights(find(classes == -1)); mat class2Weights = weights(find(classes == +1)); /* Accumulate weights for given thresholds ("number of thresholds" x "number of features"): */ mat thresholdWeights1 = accumarray(join_rows(class1Thresholds, featureIndexes1), repmat(class1Weights, features.n_cols, 1), SizeMat(N_THRESHOLDS, features.n_cols)); mat thresholdWeights2 = accumarray(join_rows(class2Thresholds, featureIndexes2), repmat(class2Weights, features.n_cols, 1), SizeMat(N_THRESHOLDS, features.n_cols)); /* Looking for threshold with minimum error; Here we construct cummulative sum of weights for seaprate classes, then we add them; In order to find the smallest error, we consider both directions of a threshold; */ mat thresholdErrors = flipud(cumsum(flipud(thresholdWeights1))) + cumsum(thresholdWeights2); thresholdErrors = join_cols(thresholdErrors, sum(weights) - thresholdErrors); uword index; uword featureType; error = thresholdErrors.min(index, featureType); threshold.featureType = featureType; if (index > N_THRESHOLDS - 1) { threshold.direction = -1; index -= N_THRESHOLDS; } else { threshold.direction = +1; } threshold.value = minFeatures(featureType) + (ranges(featureType) / N_THRESHOLDS) * index; /* Evaluating hypothesis for derived threshold: */ classify(threshold, features, hypothesis); return; }
void operate(array<T1, S1>& u, array <T2, S2>& p, array <T3, S3>& q, sep, const array<T4, S4>& a, const size_array& idx = size_array()) // idx only used internally! { if (a.empty()) { u.init(); p.init(); q.init(); return; } q = arr(true).cat(diff(a) != 0); if (FIRST) (_, p) = find++(q); else (_, p) = find++(cshift(q, -1)); u = a[p]; if (!idx.empty()) force(p) = idx[p]; q[0] = T3(); q = cumsum(q); }
void operate(array<T1, S1>& u, array <array <T2, S21>, S22>& p, array <T3, S3>& q, sep, const array<T4, S4>& a, const size_array& idx = size_array()) // idx only used internally! { if (a.empty()) { u.init(); p.init(); q.init(); return; } q = arr(true).cat(diff(a) != 0); size_array first = find(q); u = a[first]; if (idx.empty()) (_, p) = partition++(size_array((0, _, a.length() - 1)), first); // TODO: remove size_array copy else (_, p) = partition++(idx, first); q[0] = T3(); q = cumsum(q); }
void pm_ghosts_reduce(PMGhostData * pgd, int attributes) { PM * pm = pgd->pm; PMStore * p = pgd->p; size_t Nsend = cumsum(NULL, pgd->Nsend, pm->NTask); size_t Nrecv = cumsum(NULL, pgd->Nrecv, pm->NTask); ptrdiff_t i; pgd->elsize = p->pack(pgd->p, 0, NULL, attributes); pgd->recv_buffer = fastpm_memory_alloc(pm->mem, Nrecv * pgd->elsize, FASTPM_MEMORY_HEAP); pgd->send_buffer = fastpm_memory_alloc(pm->mem, Nsend * pgd->elsize, FASTPM_MEMORY_HEAP); pgd->ReductionAttributes = attributes; #pragma omp parallel for for(i = 0; i < pgd->nghosts; i ++) { p->pack(p, i + pgd->np, (char*) pgd->recv_buffer + i * pgd->elsize, pgd->ReductionAttributes); } MPI_Datatype GHOST_TYPE; MPI_Type_contiguous(pgd->elsize, MPI_BYTE, &GHOST_TYPE); MPI_Type_commit(&GHOST_TYPE); MPI_Alltoallv_sparse(pgd->recv_buffer, pgd->Nrecv, pgd->Orecv, GHOST_TYPE, pgd->send_buffer, pgd->Nsend, pgd->Osend, GHOST_TYPE, pm->Comm2D); MPI_Type_free(&GHOST_TYPE); /* now reduce the attributes. */ int ighost; #pragma omp parallel for for(ighost = 0; ighost < Nsend; ighost ++) { p->reduce(p, pgd->ighost_to_ipar[ighost], (char*) pgd->send_buffer + ighost * pgd->elsize, pgd->ReductionAttributes); } fastpm_memory_free(pm->mem, pgd->send_buffer); fastpm_memory_free(pm->mem, pgd->recv_buffer); }
/*! Inverse uniform cdf sampling to sample from discrete distribution * * * https://en.wikipedia.org/wiki/Inverse_transform_sampling * * @param densities Vector of discrete probabilities that sum to one (density) * * @retval Index of the randomly sampled vector item. */ size_t sample_discrete(const vec & densities){ size_t n = densities.size(); vec cum_prob = zeros(n+1); cum_prob(span(1, n))= cumsum(densities); double u = uniform(); for(auto i : range(n)){ if(u <= cum_prob(i+1)){ return i; } } // Shouldn't get to here throw invalid_argument("Function sample_discrete failed..."); }
Solution VDSpiral (SpiralParams& sp) { GradientParams gp; Matrix<double>& fov = sp.fov; Matrix<double>& rad = sp.rad; double k_max, fov_max, dr; Matrix<double> r, theta; long n = 0; assert (numel(rad) >= 2); assert (isvec(rad) == isvec(fov)); assert (numel(rad) == numel(fov)); k_max = 5.0 / sp.res; fov_max = m_max(fov); dr = sp.shots / (fov_max); n = size(fov,1)*100; r = linspace<double> (0.0, k_max, n); Matrix<double> x = k_max*rad; fov = interp1 (x, fov, r, INTERP::LINEAR); dr = sp.shots / (1500.0 * fov_max); n = ceil (k_max/dr); x = r; r = linspace<double> (0.0, k_max, n); fov = interp1 (x, fov, r, INTERP::AKIMA); theta = cumsum ((2 * PI * dr / sp.shots) * fov); gp.k = Matrix<double> (numel(r), 3); for (size_t i = 0; i < numel(r); i++) { gp.k(i,0) = r[i] * cos (theta[i]); gp.k(i,1) = r[i] * sin (theta[i]); } gp.mgr = sp.mgr; gp.msr = sp.msr; gp.dt = sp.dt; gp.gunits = sp.gunits; gp.lunits = sp.lunits; Solution s = ComputeGradient (gp); return s; }
double auc( vec preds, uvec labels ) { // one class auc, preds are for the positive class double auc; int n = preds.n_elem; int n_pos = sum( labels ); uvec order = sort_index( preds, "descend" ); vec preds_ord = preds( order ); uvec labels_ord = labels( order ); uvec above = cumsum( ones<uvec>( labels_ord.n_elem ) ) - cumsum( labels_ord ); auc = ( 1. - double(sum( above % labels_ord )) / ( double( n_pos ) * double( n - n_pos ) ) ); return auc; }
// Draw random variable from discrete distribution given by pp with nn entries. // !! Labels are 1:nn, NOT 0:NN-1 since we don't use them as indices !! // pp : array with probabilities // nn : number of possible outcomes // cump : array to store cumsum in (pre-allocated) // eps : machine epsilon inline unsigned int discrnd(const double *pp, unsigned int nn, double *cump, double eps) { double u; cumsum(cump,pp,nn); cump[nn-1] = 1+eps; u = rand() / (double)RAND_MAX; u *= 1-eps; for(int i = 0; i < nn; i++) { if(u < cump[i]) return (i+1); } mexErrMsgTxt("Did not sample discrte random variable!!\n"); return -1; }
int sample_from( float* p,int num) { float * a=cumsum(p,num); float u = ((float )rand()/( float )RAND_MAX); int i=num-2; for ( i=num-2; i>=0; i--) { if (a[i]<u) break; } delete [] a; return i+1; }
size_t multinomial(const std::vector<Double>& prb) { ASSERT_GT(prb.size(),0); if (prb.size() == 1) { return 0; } Double sum(0); for(size_t i = 0; i < prb.size(); ++i) { ASSERT_GE(prb[i], 0); // Each entry must be P[i] >= 0 sum += prb[i]; } ASSERT_GT(sum, 0); // Normalizer must be positive // actually draw the random number const Double rnd(uniform<Double>(0,1)); size_t ind = 0; for(Double cumsum(prb[ind]/sum); rnd > cumsum && (ind+1) < prb.size(); cumsum += (prb[++ind]/sum)); return ind; } // end of multinomial
// Draw multinomial variable with distribution pp with nn entries. // This is just a port of the meat of Matlab's mnrnd function. // out : array of uints to store result (pre-allocated). Since returned in // array results will need to be made 1-based. // N : number of trials // pp : array with probabilities // nn : number of possible outcomes // cump : array to store cumsum in (pre-allocated) // eps : machine epsilon inline void multirnd(double *out, unsigned int N, const double *pp, unsigned int nn, double *cump, double eps) { double u; memset(out,0,nn*sizeof(double)); cumsum(cump,pp,nn); cump[nn-1] = 1+eps; for(int n = 0; n < N; n++) { // Draw discrete r.v. according to pp and accumlate in entries of out u = rand() / (double)RAND_MAX; u *= 1-eps; for(int i = 0; i < nn; i++) { if(u < cump[i]) { out[i] += 1; break; } } } }
int Stump::train(const Mat& data, const Mat& labels, const Mat& weights) { CV_Assert(labels.rows == 1 && labels.cols == data.cols); CV_Assert(weights.rows == 1 && weights.cols == data.cols); /* Assert that data and labels have int type */ /* Assert that weights have float type */ /* Prepare labels for each feature rearranged according to sorted order */ Mat sorted_labels(data.rows, data.cols, labels.type()); Mat sorted_weights(data.rows, data.cols, weights.type()); Mat indices; sortIdx(data, indices, cv::SORT_EVERY_ROW | cv::SORT_ASCENDING); for( int row = 0; row < indices.rows; ++row ) { for( int col = 0; col < indices.cols; ++col ) { sorted_labels.at<int>(row, col) = labels.at<int>(0, indices.at<int>(row, col)); sorted_weights.at<float>(row, col) = weights.at<float>(0, indices.at<int>(row, col)); } } /* Sort feature values */ Mat sorted_data(data.rows, data.cols, data.type()); sort(data, sorted_data, cv::SORT_EVERY_ROW | cv::SORT_ASCENDING); /* Split positive and negative weights */ Mat pos_weights = Mat::zeros(sorted_weights.rows, sorted_weights.cols, sorted_weights.type()); Mat neg_weights = Mat::zeros(sorted_weights.rows, sorted_weights.cols, sorted_weights.type()); for( int row = 0; row < data.rows; ++row ) { for( int col = 0; col < data.cols; ++col ) { if( sorted_labels.at<int>(row, col) == +1 ) { pos_weights.at<float>(row, col) = sorted_weights.at<float>(row, col); } else { neg_weights.at<float>(row, col) = sorted_weights.at<float>(row, col); } } } /* Compute cumulative sums for fast stump error computation */ Mat pos_cum_weights = Mat::zeros(sorted_weights.rows, sorted_weights.cols, sorted_weights.type()); Mat neg_cum_weights = Mat::zeros(sorted_weights.rows, sorted_weights.cols, sorted_weights.type()); cumsum(pos_weights, pos_cum_weights); cumsum(neg_weights, neg_cum_weights); /* Compute total weights of positive and negative samples */ float pos_total_weight = pos_cum_weights.at<float>(0, weights.cols - 1); float neg_total_weight = neg_cum_weights.at<float>(0, weights.cols - 1); float eps = 1.0f / (4 * labels.cols); /* Compute minimal error */ float min_err = FLT_MAX; int min_row = -1; int min_col = -1; int min_polarity = 0; float min_pos_value = 1, min_neg_value = -1; for( int row = 0; row < sorted_weights.rows; ++row ) { for( int col = 0; col < sorted_weights.cols - 1; ++col ) { float err, h_pos, h_neg; // Direct polarity float pos_wrong = pos_cum_weights.at<float>(row, col); float pos_right = pos_total_weight - pos_wrong; float neg_right = neg_cum_weights.at<float>(row, col); float neg_wrong = neg_total_weight - neg_right; h_pos = (float)(.5 * log((pos_right + eps) / (pos_wrong + eps))); h_neg = (float)(.5 * log((neg_wrong + eps) / (neg_right + eps))); err = sqrt(pos_right * neg_wrong) + sqrt(pos_wrong * neg_right); if( err < min_err ) { min_err = err; min_row = row; min_col = col; min_polarity = +1; min_pos_value = h_pos; min_neg_value = h_neg; } // Opposite polarity swap(pos_right, pos_wrong); swap(neg_right, neg_wrong); h_pos = -h_pos; h_neg = -h_neg; err = sqrt(pos_right * neg_wrong) + sqrt(pos_wrong * neg_right); if( err < min_err ) { min_err = err; min_row = row; min_col = col; min_polarity = -1; min_pos_value = h_pos; min_neg_value = h_neg; } } } /* Compute threshold, store found values in fields */ threshold_ = ( sorted_data.at<int>(min_row, min_col) + sorted_data.at<int>(min_row, min_col + 1) ) / 2; polarity_ = min_polarity; pos_value_ = min_pos_value; neg_value_ = min_neg_value; return min_row; }
/* clear a; nb = 2500; a = rand(nb, nb) * 50; tic(); cumsum(a); toc clear a; nb = 2500; a = rand(nb, nb) * 50; a = a + a *%i; tic(); cumsum(a); toc */ types::Function::ReturnValue sci_cumsum(types::typed_list &in, int _iRetCount, types::typed_list &out) { types::Double* pDblIn = NULL; types::Double* pDblOut = NULL; types::Polynom* pPolyIn = NULL; types::Polynom* pPolyOut = NULL; int iOrientation = 0; int iOuttype = 1; // 1 = native | 2 = double (type of output value) if (in.size() < 1 || in.size() > 3) { Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "cumsum", 1, 3); return types::Function::Error; } if (_iRetCount > 1) { Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "cumsum", 1); return types::Function::Error; } bool isCloned = true; /***** get data *****/ switch (in[0]->getType()) { case types::InternalType::ScilabDouble: pDblIn = in[0]->getAs<types::Double>(); isCloned = false; break; case types::InternalType::ScilabBool: pDblIn = getAsDouble(in[0]->getAs<types::Bool>()); iOuttype = 2; break; case types::InternalType::ScilabPolynom: pPolyIn = in[0]->getAs<types::Polynom>(); isCloned = false; break; case types::InternalType::ScilabInt8: pDblIn = getAsDouble(in[0]->getAs<types::Int8>()); break; case types::InternalType::ScilabInt16: pDblIn = getAsDouble(in[0]->getAs<types::Int16>()); break; case types::InternalType::ScilabInt32: pDblIn = getAsDouble(in[0]->getAs<types::Int32>()); break; case types::InternalType::ScilabInt64: pDblIn = getAsDouble(in[0]->getAs<types::Int64>()); break; case types::InternalType::ScilabUInt8: pDblIn = getAsDouble(in[0]->getAs<types::UInt8>()); break; case types::InternalType::ScilabUInt16: pDblIn = getAsDouble(in[0]->getAs<types::UInt16>()); break; case types::InternalType::ScilabUInt32: pDblIn = getAsDouble(in[0]->getAs<types::UInt32>()); break; case types::InternalType::ScilabUInt64: pDblIn = getAsDouble(in[0]->getAs<types::UInt64>()); break; default: std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_cumsum"; return Overload::call(wstFuncName, in, _iRetCount, out); } if (in.size() >= 2) { if (in[1]->isDouble()) { types::Double* pDbl = in[1]->getAs<types::Double>(); if (pDbl->isScalar() == false) { if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong value for input argument #%d: A positive scalar expected.\n"), "cumsum", 2); return types::Function::Error; } iOrientation = static_cast<int>(pDbl->get(0)); if (iOrientation <= 0) { if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong value for input argument #%d: A positive scalar expected.\n"), "cumsum", 2); return types::Function::Error; } } else if (in[1]->isString()) { types::String* pStr = in[1]->getAs<types::String>(); if (pStr->isScalar() == false) { if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "cumsum", 2); return types::Function::Error; } wchar_t* wcsString = pStr->get(0); if (wcscmp(wcsString, L"*") == 0) { iOrientation = 0; } else if (wcscmp(wcsString, L"r") == 0) { iOrientation = 1; } else if (wcscmp(wcsString, L"c") == 0) { iOrientation = 2; } else if (wcscmp(wcsString, L"m") == 0) { int iDims = 0; int* piDimsArray = NULL; if (pDblIn) { iDims = pDblIn->getDims(); piDimsArray = pDblIn->getDimsArray(); } else { iDims = pPolyIn->getDims(); piDimsArray = pPolyIn->getDimsArray(); } // old function was "mtlsel" for (int i = 0; i < iDims; i++) { if (piDimsArray[i] > 1) { iOrientation = i + 1; break; } } } else if ((wcscmp(wcsString, L"native") == 0) && (in.size() == 2)) { iOuttype = 1; } else if ((wcscmp(wcsString, L"double") == 0) && (in.size() == 2)) { iOuttype = 2; } else { const char* pstrExpected = NULL; if (in.size() == 2) { pstrExpected = "\"*\",\"r\",\"c\",\"m\",\"native\",\"double\""; } else { pstrExpected = "\"*\",\"r\",\"c\",\"m\""; } if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "cumsum", 2, pstrExpected); return types::Function::Error; } } else { if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix or a string expected.\n"), "cumsum", 2); return types::Function::Error; } } if (in.size() == 3) { if (in[2]->isString() == false) { if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "cumsum", 3); return types::Function::Error; } types::String* pStr = in[2]->getAs<types::String>(); if (pStr->isScalar() == false) { if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "cumsum", 3); return types::Function::Error; } wchar_t* wcsString = pStr->get(0); if (wcscmp(wcsString, L"native") == 0) { iOuttype = 1; } else if (wcscmp(wcsString, L"double") == 0) { iOuttype = 2; } else { if (isCloned) { pDblIn->killMe(); } Scierror(999, _("%s: Wrong value for input argument #%d: %s or %s expected.\n"), "cumsum", 3, "\"native\"", "\"double\""); return types::Function::Error; } } /***** perform operation *****/ if (pDblIn) { if (iOrientation > pDblIn->getDims()) { if (in[0]->isDouble()) { pDblOut = pDblIn->clone()->getAs<types::Double>(); } else { pDblOut = pDblIn; } if (in[0]->isBool() == false) { iOuttype = 2; } } else { pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray(), pDblIn->isComplex()); cumsum(pDblIn, iOrientation, pDblOut); if (isCloned) { delete pDblIn; pDblIn = NULL; } } } else if (pPolyIn) { iOuttype = 1; if (iOrientation > pPolyIn->getDims()) { pPolyOut = pPolyIn->clone()->getAs<types::Polynom>(); } else { int* piRanks = new int[pPolyIn->getSize()]; pPolyIn->getRank(piRanks); pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray(), piRanks); pPolyOut->setComplex(pPolyIn->isComplex()); cumsum(pPolyIn, iOrientation, pPolyOut); delete[] piRanks; } } /***** set result *****/ if ((iOuttype == 1) && (in[0]->isDouble() == false)) { switch (in[0]->getType()) { case types::InternalType::ScilabBool: { types::Bool* pB = new types::Bool(pDblOut->getDims(), pDblOut->getDimsArray()); int* p = pB->get(); double* pd = pDblOut->get(); int size = pB->getSize(); for (int i = 0; i < size; ++i) { p[i] = pd[i] != 0 ? 1 : 0; } out.push_back(pB); break; } case types::InternalType::ScilabPolynom: { out.push_back(pPolyOut); break; } case types::InternalType::ScilabInt8: { out.push_back(toInt<types::Int8>(pDblOut)); break; } case types::InternalType::ScilabInt16: { out.push_back(toInt<types::Int16>(pDblOut)); break; } case types::InternalType::ScilabInt32: { out.push_back(toInt<types::Int32>(pDblOut)); break; } case types::InternalType::ScilabInt64: { out.push_back(toInt<types::Int64>(pDblOut)); break; } case types::InternalType::ScilabUInt8: { out.push_back(toInt<types::UInt8>(pDblOut)); break; } case types::InternalType::ScilabUInt16: { out.push_back(toInt<types::UInt16>(pDblOut)); break; } case types::InternalType::ScilabUInt32: { out.push_back(toInt<types::UInt32>(pDblOut)); break; } case types::InternalType::ScilabUInt64: { out.push_back(toInt<types::UInt64>(pDblOut)); break; } } if (pDblOut) { delete pDblOut; } } else { out.push_back(pDblOut); } return types::Function::OK; }
int asy2_higherterms(double a, double b, double* tB1, double* A2) { double A = (.25-a*a), B = (.25-b*b); double t[NN], v[NN], g[NN], gp[NN], gpp[NN], f[NN]; double B0[NN], B0p[NN], B1[NN], A1[NN], A1p[NN], A1p_t[NN]; double I[NN], J[NN], K[NN], K2[NN], tmpvec[NN]; double sgn, tmp, tk, sin5t, cos5t, tan5t, sin5t2, sin5t4, sint; int j, k; // Chebyshev points and barycentric weights sgn = 1.; for ( k=0; k<NN; k++ ) { t[NN-k-1] = .5*CC*(1+cos(PI*k/(NN-1.))); v[NN-k-1] = sgn; sgn = -sgn; } v[NN] = .5*v[NN]; v[0] = .5*v[0]; // Basic vectors g[0] = 0.; gp[0] = -A/6.-.5*B; gpp[0] = 0.; f[0] = -A/12.-.25*B; for ( k=1; k<NN; k++ ) { tk = t[k]; cos5t = cos(.5*tk); sin5t = sin(.5*tk); tan5t = tan(.5*tk); sin5t2 = sin5t*sin5t; sin5t4 = sin5t2*sin5t2; g[k] = A*(1./tan5t-2./tk)-B*tan5t; gp[k] = A*(2./(tk*tk)-.5/(sin5t*sin5t))-.5*B/(cos5t*cos5t); sint = sin(tk); gpp[k] = A*(-4./(tk*tk*tk)+.25*sint/sin5t4) - 4.*B*sin5t4/(sint*sint*sint); f[k] = A*(1./(tk*tk) - .25/sin5t2) - .25*B/(cos5t*cos5t); } // B0 and A1 tmp = a*(A+3.*B)/24.; B0[0] = .25*(-A/6.-.5*B); B0p[0] = 0.; A1[0] = 0.; A1p[0] = 0.; A1p_t[0] = -A/720.-A*A/576.-A*B/96.-B*B/64.-B/48.+a*(A/720.+B/48.); for ( k=1; k<NN; k++ ) { B0[k] = .25*g[k]/t[k]; B0p[k] = .25*(gp[k]-g[k]/t[k])/t[k]; A1[k] = .125*gp[k] - .5*(1.+2.*a)*B0[k] - g[k]*g[k]/32. - tmp; A1p[k] = .125*gpp[k] - .5*(1.+2.*a)*B0p[k] - gp[k]*g[k]/16.; A1p_t[k] = A1p[k]/t[k]; } // Integrals cumsum(A1p_t, I); for ( k=0; k<NN; k++ ) tmpvec[k] = f[k]*A1[k]; cumsum(tmpvec, J); // B1 tB1[0] = 0.; B1[0] = A/720.+A*A/576.+A*B/96.+B*B/64.+B/48.+a*(A*A/576.+B*B/64.+A*B/96.)-a*a*(A/720.+B/48.); for ( k=1; k<NN; k++ ) { tB1[k] = -.5*A1p[k] - (.5+a)*I[k] + .5*J[k]; B1[k] = tB1[k]/t[k]; } // Integrals for ( k=0; k<NN; k++ ) tmpvec[k] = f[k]*tB1[k]; cumsum( tmpvec, K ); diff( tB1, K2 ); // A2 A2[0] = 0.; tmp = .5*K2[0] - (.5+a)*B1[0] - .5*K[0]; for ( k=1; k<NN; k++ ) A2[k] = .5*K2[k] - (.5+a)*B1[k] - .5*K[k] - tmp; for ( k=0; k<NN; k++ ) { tB1[k] = tB1[k]; A2[k] = A2[k]; } return(0); }
int otsu(double* data_out, double** thr, double* sep, double* data, int xsize, int ysize, int N) { int _verbose=1; int LEVELS = 256; //double data_out[xsize * ysize]; // Unique int unI_size; double* unI; unique(xsize*ysize, data, &unI_size, &unI); if(_verbose>=1) { std::cout << std::endl << "unI_c = "; double_vector_print(unI_size, unI); } // Histogram int* counts; double* bins; int nbins; if (unI_size < LEVELS) nbins = unI_size; else nbins = LEVELS; if (nbins < LEVELS) { counts = new int[unI_size]; for (int i = 0; i < unI_size; i++) counts[i] = 0; bins = new double[unI_size]; for (int i = 0; i < unI_size; i++) bins[i] = unI[i]; } else if (nbins == LEVELS) { counts = new int[LEVELS]; for (int i = 0; i < LEVELS; i++) counts[i] = 0; bins = new double[LEVELS]; double step = (LEVELS - 1) / LEVELS / 2; bins[0] = step; for (int i = 1; i < LEVELS; i++) bins[i] = bins[i - 1] + step; } free(unI); histogram(counts, xsize*ysize, nbins, data, bins); // Display vector for matlab comparison if(_verbose) { std::cout << std::endl << "cBins = "; double_vector_print(nbins, bins); double_vector_save_to_file("cBins.txt", nbins, bins); std::cout << std::endl << "cHist = "; int_vector_print(nbins, counts); int_vector_save_to_file("cHist.txt", nbins, counts); } double* P=(double*)malloc(nbins*sizeof(*P)); for (int i = 0; i < nbins; i++) P[i] = (double) counts[i] / (xsize * ysize); // cumsum double* w=(double*)malloc(nbins*sizeof(*P)); double* mu=(double*)malloc(nbins*sizeof(*P)); double* Pi=(double*)malloc(nbins*sizeof(*P)); Pi[0] = P[0]; for (int i = 1; i < nbins; i++) Pi[i] = (i + 1) * P[i]; cumsum(w, P, nbins); cumsum(mu, Pi, nbins); if(_verbose) { double_vector_save_to_file("P.txt", nbins, P); double_vector_save_to_file("w.txt", nbins, w); double_vector_save_to_file("mu.txt", nbins, mu); } double* w_aux = NULL; double* mu_aux = NULL; double* aux1; double* aux2; double* sigma2B; double sigma2Bmax; int ind_sigma2Bmax; if (N == 2) { w_aux = new double[nbins - 2]; mu_aux = new double[nbins - 2]; aux1 = new double[nbins - 2]; aux2 = new double[nbins - 2]; sigma2B = new double[nbins - 2]; for (int i = 0; i < nbins - 2; i++) { w_aux[i] = w[i + 1]; mu_aux[i] = mu[i + 1]; aux1[i] = mu[nbins - 1] * w_aux[i] - mu_aux[i]; } if(_verbose) { std::cout << std::endl << "saving aux"<<std::endl; double_vector_save_to_file("aux1.txt", nbins - 2, aux1); //double_vector_save_to_file("aux2.txt", nbins - 2, aux2); } vector_pow(aux2, aux1, 2, nbins - 2); divide_vectors(aux1, aux2, w_aux, nbins - 2); for (int i = 0; i < nbins - 2; i++) aux2[i] = 1 - w_aux[i]; divide_vectors(sigma2B, aux1, aux2, nbins - 2); vector_max(&sigma2Bmax, &ind_sigma2Bmax, sigma2B, nbins - 2); if(_verbose) { double_vector_save_to_file("aux1b.txt", nbins - 2, aux1); double_vector_save_to_file("aux2b.txt", nbins - 2, aux2); double_vector_save_to_file("sigma2B.txt", nbins - 2, sigma2B); std::cout << "Maximo: " << sigma2Bmax << std::endl << "Posicion del maximo: " << ind_sigma2Bmax << std::endl; std::cout << "pixval: " << bins[ind_sigma2Bmax + 1] << std::endl; } for (int i = 0; i < xsize * ysize; i++) if (data[i] <= bins[ind_sigma2Bmax + 1]) data_out[i] = 0.0; else data_out[i] = 1.0; double* th_aux; th_aux = new double[1]; th_aux[0] = bins[ind_sigma2Bmax + 1]; *thr = th_aux; } else if (N == 3) { // w_aux = new double[nbins ]; // mu_aux = new double[nbins ]; aux1 = new double[nbins]; aux2 = new double[nbins]; sigma2B = new double[nbins * nbins]; double* w0= new double[nbins*nbins]; double* w1= new double[nbins*nbins]; double* w2= new double[nbins*nbins]; double* mu0_aux= new double[nbins]; double* mu2_aux= new double[nbins]; double* mu0= new double[nbins*nbins]; double* mu2= new double[nbins*nbins]; vector_flip(aux1, P, nbins); cumsum(aux2, aux1, nbins); vector_flip(aux1, aux2, nbins); for (int i = 0; i < nbins; i++) for (int j = 0; j < nbins; j++) { w0[i * nbins + j] = w[i]; w2[i * nbins + j] = aux1[j]; } double_vector_save_to_file("w0.txt", nbins*nbins, w0); double_vector_save_to_file("w2.txt", nbins*nbins, w2); divide_vectors(mu0_aux, mu, w, nbins); vector_flip(aux1, Pi, nbins); cumsum(aux2, aux1, nbins); double* aux3=new double[nbins]; vector_flip(aux3, P, nbins); cumsum(aux1, aux3, nbins); divide_vectors(aux3, aux2, aux1, nbins); vector_flip(mu2_aux, aux3, nbins); for (int i = 0; i < nbins; i++) for (int j = 0; j < nbins; j++) { mu0[i * nbins + j] = mu0_aux[i]; mu2[i * nbins + j] = mu2_aux[j]; } double_vector_save_to_file("mu0.txt", nbins*nbins, mu0); double_vector_save_to_file("mu2.txt", nbins*nbins, mu2); for (int i = 0; i < nbins * nbins; i++) { w1[i] = 1 - w0[i] - w2[i]; } double* aux4=new double[nbins * nbins]; double* aux5=new double[nbins * nbins]; double* t1=new double[nbins * nbins]; double* t2=new double[nbins * nbins]; double* t3=new double[nbins * nbins]; double* t4=new double[nbins * nbins]; double* t34=new double[nbins * nbins]; double* t34_aux=new double[nbins * nbins]; for (int i = 0; i < nbins * nbins; i++) aux4[i] = mu0[i] - mu[nbins - 1]; vector_pow(aux5, aux4, 2, nbins * nbins); multiplicate_vectors(t1, w0, aux5, nbins * nbins); multiplicate_vectors(t3, w0, aux4, nbins * nbins); for (int i = 0; i < nbins * nbins; i++) aux4[i] = mu2[i] - mu[nbins - 1]; vector_pow(aux5, aux4, 2, nbins * nbins); multiplicate_vectors(t2, w2, aux5, nbins * nbins); multiplicate_vectors(t4, w2, aux4, nbins * nbins); for (int i = 0; i < nbins * nbins; i++) t34[i] = t3[i] + t4[i]; vector_pow(t34_aux, t34, 2, nbins * nbins); divide_vectors(t34, t34_aux, w1, nbins * nbins); for (int i = 0; i < nbins * nbins; i++) { sigma2B[i] = t1[i] + t2[i] + t34[i]; if (w1[i] <= 0) sigma2B[i] = 0; } if(_verbose) { double_vector_save_to_file("sigma2B.txt", nbins*nbins, sigma2B); double_vector_save_to_file("sigma2B.txt", nbins*nbins, sigma2B); } vector_max(&sigma2Bmax, &ind_sigma2Bmax, sigma2B, nbins * nbins); std::cout << "Maximo: " << sigma2Bmax << std::endl << "Posicion del maximo: " << ind_sigma2Bmax << std::endl; int k1, k2; k1 = (int) ind_sigma2Bmax / nbins; k2 = ind_sigma2Bmax % nbins; std::cout << "k1 = " << k1 << ", k2 = " << k2 << std::endl; for (int i = 0; i < xsize * ysize; i++) if (data[i] <= bins[k1]) data_out[i] = 0.0; else if ((data[i] > bins[k1]) && (data[i] <= bins[k2])) data_out[i] = 0.5; else data_out[i] = 1.0; double* thr_aux; thr_aux = new double[2]; thr_aux[0] = bins[k1]; thr_aux[1] = bins[k2]; *thr = thr_aux; delete[] w0; delete[] w1; delete[] w2; delete[] mu0_aux; delete[] mu2_aux; delete[] mu0; delete[] mu2; delete[] aux3; delete[] aux4; delete[] aux5; delete[] t1; delete[] t2; delete[] t3; delete[] t4; delete[] t34; delete[] t34_aux; } double* sep_aux=new double[nbins]; double* sep_aux_2=new double[nbins]; for(int i=0;i<nbins;i++) sep_aux[i]=i+1-mu[nbins-1]; vector_pow(sep_aux_2, sep_aux, 2, nbins); multiplicate_vectors(sep_aux, sep_aux_2, P, nbins); double cumu=0; for (int i=0;i<nbins;i++) cumu+=sep_aux[i]; *sep=sigma2Bmax/cumu; //*Iseg = data_out; delete[] counts; delete[] aux1; delete[] aux2; delete[] bins; if (w_aux) delete[] w_aux; if (mu_aux) delete[] mu_aux; delete[] sigma2B; delete[] sep_aux; delete[] sep_aux_2; free(w); free(mu); free(Pi); return 0; }
//' Generate a Drift Process //' //' Generates a Drift Process with a given slope, \eqn{\omega}. //' @param N An \code{integer} for signal length. //' @param slope A \code{double} that contains drift slope //' @return A \code{vec} containing the drift. //' @backref src/gen_process.cpp //' @backref src/gen_process.h //' @keywords internal //' @examples //' gen_dr(10, 8.2) // [[Rcpp::export]] arma::vec gen_dr(const unsigned int N, const double slope = 5) { arma::vec gd(N); gd.fill(slope); return cumsum(gd); }
sample_search_generator(const array <T, S>& distr) : D(distr.length()), cum(cumsum(distr)), total(cum[D - 1]) { }
void bee_colony::evolve(population &pop) const { // Let's store some useful variables. const problem::base &prob = pop.problem(); const problem::base::size_type prob_i_dimension = prob.get_i_dimension(), D = prob.get_dimension(), Dc = D - prob_i_dimension, prob_c_dimension = prob.get_c_dimension(); const decision_vector &lb = prob.get_lb(), &ub = prob.get_ub(); const population::size_type NP = (int) pop.size(); //We perform some checks to determine wether the problem/population are suitable for ABC if ( Dc == 0 ) { pagmo_throw(value_error,"There is no continuous part in the problem decision vector for ABC to optimise"); } if ( prob.get_f_dimension() != 1 ) { pagmo_throw(value_error,"The problem is not single objective and ABC is not suitable to solve it"); } if ( prob_c_dimension != 0 ) { pagmo_throw(value_error,"The problem is not box constrained and ABC is not suitable to solve it"); } if (NP < 2) { pagmo_throw(value_error,"for ABC at least 2 individuals in the population are needed"); } // Get out if there is nothing to do. if (m_iter == 0) { return; } // Some vectors used during evolution are allocated here. fitness_vector fnew(prob.get_f_dimension()); decision_vector dummy(D,0); //used for initialisation purposes std::vector<decision_vector > X(NP,dummy); //set of food sources std::vector<fitness_vector> fit(NP); //food sources fitness decision_vector temp_solution(D,0); std::vector<int> trial(NP,0); std::vector<double> probability(NP); population::size_type neighbour = 0; decision_vector::size_type param2change = 0; std::vector<double> selectionfitness(NP), cumsum(NP), cumsumTemp(NP); std::vector <population::size_type> selection(NP); double r = 0; // Copy the food sources position and their fitness for ( population::size_type i = 0; i<NP; i++ ) { X[i] = pop.get_individual(i).cur_x; fit[i] = pop.get_individual(i).cur_f; } // Main ABC loop for (int j = 0; j < m_iter; ++j) { //1- Send employed bees for (population::size_type ii = 0; ii< NP; ++ii) { //selects a random component (only of the continuous part) of the decision vector param2change = boost::uniform_int<decision_vector::size_type>(0,Dc-1)(m_urng); //randomly chose a solution to be used to produce a mutant solution of solution ii //randomly selected solution must be different from ii do{ neighbour = boost::uniform_int<population::size_type>(0,NP-1)(m_urng); } while(neighbour == ii); //copy local solution into temp_solution (the whole decision_vector, also the integer part) for(population::size_type i=0; i<D; ++i) { temp_solution[i] = X[ii][i]; } //mutate temp_solution temp_solution[param2change] = X[ii][param2change] + boost::uniform_real<double>(-1,1)(m_drng) * (X[ii][param2change] - X[neighbour][param2change]); //if generated parameter value is out of boundaries, it is shifted onto the boundaries*/ if (temp_solution[param2change]<lb[param2change]) { temp_solution[param2change] = lb[param2change]; } if (temp_solution[param2change]>ub[param2change]) { temp_solution[param2change] = ub[param2change]; } //Calling void prob.objfun(fitness_vector,decision_vector) is more efficient as no memory allocation occur //A call to fitness_vector prob.objfun(decision_vector) allocates memory for the return value. prob.objfun(fnew,temp_solution); //If the new solution is better than the old one replace it with the mutant one and reset its trial counter if(prob.compare_fitness(fnew, fit[ii])) { X[ii][param2change] = temp_solution[param2change]; pop.set_x(ii,X[ii]); prob.objfun(fit[ii], X[ii]); //update the fitness vector trial[ii] = 0; } else { trial[ii]++; //if the solution can't be improved incrase its trial counter } } //End of loop on the population members //2 - Send onlooker bees //We scale all fitness values from 0 (worst) to absolute value of the best fitness fitness_vector worstfit=fit[0]; for (pagmo::population::size_type i = 1; i < NP;i++) { if (prob.compare_fitness(worstfit,fit[i])) worstfit=fit[i]; } for (pagmo::population::size_type i = 0; i < NP; i++) { selectionfitness[i] = fabs(worstfit[0] - fit[i][0]) + 1.; } // We build and normalise the cumulative sum cumsumTemp[0] = selectionfitness[0]; for (pagmo::population::size_type i = 1; i< NP; i++) { cumsumTemp[i] = cumsumTemp[i - 1] + selectionfitness[i]; } for (pagmo::population::size_type i = 0; i < NP; i++) { cumsum[i] = cumsumTemp[i]/cumsumTemp[NP-1]; } for (pagmo::population::size_type i = 0; i < NP; i++) { r = m_drng(); for (pagmo::population::size_type j = 0; j < NP; j++) { if (cumsum[j] > r) { selection[i]=j; break; } } } for(pagmo::population::size_type t = 0; t < NP; ++t) { r = m_drng(); pagmo::population::size_type ii = selection[t]; //selects a random component (only of the continuous part) of the decision vector param2change = boost::uniform_int<decision_vector::size_type>(0,Dc-1)(m_urng); //randomly chose a solution to be used to produce a mutant solution of solution ii //randomly selected solution must be different from ii do{ neighbour = boost::uniform_int<population::size_type>(0,NP-1)(m_urng); } while(neighbour == ii); //copy local solution into temp_solution (also integer part) for(population::size_type i=0; i<D; ++i) { temp_solution[i] = X[ii][i]; } //mutate temp_solution temp_solution[param2change] = X[ii][param2change] + boost::uniform_real<double>(-1,1)(m_drng) * (X[ii][param2change] - X[neighbour][param2change]); /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/ if (temp_solution[param2change]<lb[param2change]) { temp_solution[param2change] = lb[param2change]; } if (temp_solution[param2change]>ub[param2change]) { temp_solution[param2change] = ub[param2change]; } //Calling void prob.objfun(fitness_vector,decision_vector) is more efficient as no memory allocation occur //A call to fitness_vector prob.objfun(decision_vector) allocates memory for the return value. prob.objfun(fnew,temp_solution); //If the new solution is better than the old one replace it with the mutant one and reset its trial counter if(prob.compare_fitness(fnew, fit[ii])) { X[ii][param2change] = temp_solution[param2change]; pop.set_x(ii,X[ii]); prob.objfun(fit[ii], X[ii]); //update the fitness vector trial[ii] = 0; } else { trial[ii]++; //if the solution can't be improved incrase its trial counter } } //3 - Send scout bees int maxtrialindex = 0; for (population::size_type ii=1; ii<NP; ++ii) { if (trial[ii] > trial[maxtrialindex]) { maxtrialindex = ii; } } if(trial[maxtrialindex] >= m_limit) { //select a new random solution for(problem::base::size_type jj = 0; jj < Dc; ++jj) { X[maxtrialindex][jj] = boost::uniform_real<double>(lb[jj],ub[jj])(m_drng); } trial[maxtrialindex] = 0; pop.set_x(maxtrialindex,X[maxtrialindex]); } } // end of main ABC loop }
List log_EMx(const arma::mat& transition_, const arma::cube& emission_, const arma::vec& init_, const arma::ucube& obs, const arma::uvec& nSymbols, const arma::mat& coef_, const arma::mat& X, const arma::uvec& numberOfStates, int itermax, double tol, int trace, unsigned int threads) { // Make sure we don't alter the original vec/mat/cube // needed for cube, in future maybe in other cases as well arma::cube emission = log(emission_); arma::mat transition = log(transition_); arma::vec init = log(init_); arma::mat coef(coef_); coef.col(0).zeros(); arma::mat weights = exp(X * coef).t(); if (!weights.is_finite()) { return List::create(Named("error") = 3); } weights.each_row() /= sum(weights, 0); weights = log(weights); arma::mat initk(emission.n_rows, obs.n_slices); for (unsigned int k = 0; k < obs.n_slices; k++) { initk.col(k) = init + reparma(weights.col(k), numberOfStates); } arma::cube alpha(emission.n_rows, obs.n_cols, obs.n_slices); //m,n,k arma::cube beta(emission.n_rows, obs.n_cols, obs.n_slices); //m,n,k log_internalForwardx(transition, emission, initk, obs, alpha, threads); log_internalBackward(transition, emission, obs, beta, threads); arma::vec ll(obs.n_slices); #pragma omp parallel for if(obs.n_slices >= threads) schedule(static) num_threads(threads) \ default(none) shared(obs, alpha, ll) for (unsigned int k = 0; k < obs.n_slices; k++) { ll(k) = logSumExp(alpha.slice(k).col(obs.n_cols - 1)); } double sumlogLik = sum(ll); if (trace > 0) { Rcout << "Log-likelihood of initial model: " << sumlogLik << std::endl; } // // //EM-algorithm begins // double change = tol + 1.0; int iter = 0; arma::uvec cumsumstate = cumsum(numberOfStates); while ((change > tol) & (iter < itermax)) { iter++; arma::mat ksii(emission.n_rows, emission.n_rows, arma::fill::zeros); arma::cube gamma(emission.n_rows, emission.n_cols, emission.n_slices, arma::fill::zeros); arma::vec delta(emission.n_rows, arma::fill::zeros); for (unsigned int k = 0; k < obs.n_slices; k++) { delta += exp(alpha.slice(k).col(0) + beta.slice(k).col(0) - ll(k)); } #pragma omp parallel for if(obs.n_slices>=threads) schedule(static) num_threads(threads) \ default(none) shared(transition, obs, ll, alpha, beta, emission, ksii, gamma, nSymbols) for (unsigned int k = 0; k < obs.n_slices; k++) { if (obs.n_cols > 1) { for (unsigned int j = 0; j < emission.n_rows; j++) { for (unsigned int i = 0; i < emission.n_rows; i++) { if (transition(i, j) > -arma::datum::inf) { arma::vec tmpnm1(obs.n_cols - 1); for (unsigned int t = 0; t < (obs.n_cols - 1); t++) { tmpnm1(t) = alpha(i, t, k) + transition(i, j) + beta(j, t + 1, k); for (unsigned int r = 0; r < obs.n_rows; r++) { tmpnm1(t) += emission(j, obs(r, t + 1, k), r); } } #pragma omp atomic ksii(i, j) += exp(logSumExp(tmpnm1) - ll(k)); } } } } for (unsigned int r = 0; r < emission.n_slices; r++) { for (unsigned int l = 0; l < nSymbols[r]; l++) { for (unsigned int i = 0; i < emission.n_rows; i++) { if (emission(i, l, r) > -arma::datum::inf) { arma::vec tmpn(obs.n_cols); for (unsigned int t = 0; t < obs.n_cols; t++) { if (l == (obs(r, t, k))) { tmpn(t) = alpha(i, t, k) + beta(i, t, k); } else tmpn(t) = -arma::datum::inf; } #pragma omp atomic gamma(i, l, r) += exp(logSumExp(tmpn) - ll(k)); } } } } } unsigned int error = log_optCoef(weights, obs, emission, initk, beta, ll, coef, X, cumsumstate, numberOfStates, trace); if (error != 0) { return List::create(Named("error") = error); } if (obs.n_cols > 1) { ksii.each_col() /= sum(ksii, 1); transition = log(ksii); } for (unsigned int r = 0; r < emission.n_slices; r++) { gamma.slice(r).cols(0, nSymbols(r) - 1).each_col() /= sum( gamma.slice(r).cols(0, nSymbols(r) - 1), 1); emission.slice(r).cols(0, nSymbols(r) - 1) = log(gamma.slice(r).cols(0, nSymbols(r) - 1)); } for (unsigned int i = 0; i < numberOfStates.n_elem; i++) { delta.subvec(cumsumstate(i) - numberOfStates(i), cumsumstate(i) - 1) /= arma::as_scalar( arma::accu(delta.subvec(cumsumstate(i) - numberOfStates(i), cumsumstate(i) - 1))); } init = log(delta); for (unsigned int k = 0; k < obs.n_slices; k++) { initk.col(k) = init + reparma(weights.col(k), numberOfStates); } log_internalForwardx(transition, emission, initk, obs, alpha, threads); log_internalBackward(transition, emission, obs, beta, threads); for (unsigned int k = 0; k < obs.n_slices; k++) { ll(k) = logSumExp(alpha.slice(k).col(obs.n_cols - 1)); } double tmp = sum(ll); change = (tmp - sumlogLik) / (std::abs(sumlogLik) + 0.1); sumlogLik = tmp; if (!arma::is_finite(sumlogLik)) { return List::create(Named("error") = 6); } if (trace > 1) { Rcout << "iter: " << iter; Rcout << " logLik: " << sumlogLik; Rcout << " relative change: " << change << std::endl; } } if (trace > 0) { if (iter == itermax) { Rcpp::Rcout << "EM algorithm stopped after reaching the maximum number of " << iter << " iterations." << std::endl; } else { Rcpp::Rcout << "EM algorithm stopped after reaching the relative change of " << change; Rcpp::Rcout << " after " << iter << " iterations." << std::endl; } Rcpp::Rcout << "Final log-likelihood: " << sumlogLik << std::endl; } return List::create(Named("coefficients") = wrap(coef), Named("initialProbs") = wrap(exp(init)), Named("transitionMatrix") = wrap(exp(transition)), Named("emissionArray") = wrap(exp(emission)), Named("logLik") = sumlogLik, Named("iterations") = iter, Named("change") = change, Named("error") = 0); }
SEXP attribute_hidden do_cum(SEXP call, SEXP op, SEXP args, SEXP env) { SEXP s, t, ans; R_xlen_t i, n; checkArity(op, args); if (DispatchGroup("Math", call, op, args, env, &ans)) return ans; if (isComplex(CAR(args))) { t = CAR(args); n = XLENGTH(t); PROTECT(s = allocVector(CPLXSXP, n)); setAttrib(s, R_NamesSymbol, getAttrib(t, R_NamesSymbol)); UNPROTECT(1); if(n == 0) return s; for (i = 0 ; i < n ; i++) { COMPLEX(s)[i].r = NA_REAL; COMPLEX(s)[i].i = NA_REAL; } switch (PRIMVAL(op) ) { case 1: /* cumsum */ return ccumsum(t, s); break; case 2: /* cumprod */ return ccumprod(t, s); break; case 3: /* cummax */ errorcall(call, _("'cummax' not defined for complex numbers")); break; case 4: /* cummin */ errorcall(call, _("'cummin' not defined for complex numbers")); break; default: errorcall(call, "unknown cumxxx function"); } } else if( ( isInteger(CAR(args)) || isLogical(CAR(args)) ) && PRIMVAL(op) != 2) { PROTECT(t = coerceVector(CAR(args), INTSXP)); n = XLENGTH(t); PROTECT(s = allocVector(INTSXP, n)); setAttrib(s, R_NamesSymbol, getAttrib(t, R_NamesSymbol)); if(n == 0) { UNPROTECT(2); /* t, s */ return s; } for(i = 0 ; i < n ; i++) INTEGER(s)[i] = NA_INTEGER; switch (PRIMVAL(op) ) { case 1: /* cumsum */ ans = icumsum(t,s); break; case 3: /* cummax */ ans = icummax(t,s); break; case 4: /* cummin */ ans = icummin(t,s); break; default: errorcall(call, _("unknown cumxxx function")); ans = R_NilValue; } UNPROTECT(2); /* t, s */ return ans; } else { PROTECT(t = coerceVector(CAR(args), REALSXP)); n = XLENGTH(t); PROTECT(s = allocVector(REALSXP, n)); setAttrib(s, R_NamesSymbol, getAttrib(t, R_NamesSymbol)); UNPROTECT(2); if(n == 0) return s; for(i = 0 ; i < n ; i++) REAL(s)[i] = NA_REAL; switch (PRIMVAL(op) ) { case 1: /* cumsum */ return cumsum(t,s); break; case 2: /* cumprod */ return cumprod(t,s); break; case 3: /* cummax */ return cummax(t,s); break; case 4: /* cummin */ return cummin(t,s); break; default: errorcall(call, _("unknown cumxxx function")); } } return R_NilValue; /* for -Wall */ }
int main(int ac, char** av) { if(ac != 2) { std::cout << "usage: " << av[0] << " <run number>" << std::endl; return 1; } // thomas's data size_t run_index = atoi(av[1]); real T_in_pipe[] = {152.9, 203.6, 415.1}; real T_in_head[] = {203.5, 291.8, 487.8}; real T_in_flux[] = {227.7, 315.7, 504.7}; real T_out_flux[] = {244.6, 346.9, 522.5}; real T_out_head[] = {250.8, 355.7, 538.0}; real T_out_pipe[] = {229.3, 332.6, 512.8}; real* T_ym = new real[3]; const char* prob_names[] = {"opt2_run1","opt2_run2","opt2_run3"}; // convert to kelvin for(size_t i = 0; i < 3; ++i) { T_in_pipe[i] += 273.15; T_in_head[i] += 273.15; T_in_flux[i] += 273.15; T_out_flux[i] += 273.15; T_out_head[i] += 273.15; T_out_pipe[i] += 273.15; T_ym[i] = (T_in_flux[i] + T_out_flux[i]) * 0.5; } // solve // dimensions real w_total = 6.00e-2; real w_irrad = 2.00e-2; real pipe = 6.35e-3; real l_total = 4.00e-2; real l_irrad = 2.00e-2; // calculations real l_1 = (l_total - l_irrad) / 2.0; real l_3 = l_irrad - pipe * 2.0; real w_ends = (w_total - w_irrad) / 2.0; real w_1 = (w_ends - pipe) / 2.0; // lists auto xd = math::make_array_1<real,1>({0, w_1, pipe, w_1, w_irrad, w_1, pipe, w_1}); auto yd = math::make_array_1<real,1>({0, 1e-2, 5e-2}); auto zd = math::make_array_1<real,1>({0, l_1, pipe, l_3*0.5, l_3*0.5, pipe, l_1}); real nom_size = 5e-4; auto nx = xd->sub({1},{-1})->divide(nom_size)->ceil<size_t>(); auto ny = yd->sub({1},{-1})->divide(nom_size)->ceil<size_t>(); auto nz = zd->sub({1},{-1})->divide(nom_size)->ceil<size_t>(); auto x = xd->cumsum(); auto y = yd->cumsum(); auto z = zd->cumsum(); coor_type X({x,y,z}); cell_count_type n({nx,ny,nz}); //================================================================= real s = 1.0e10; //nx = [10, 10, 10, 50, 10, 10, 10]; //ny = [50, 50]; //nz = [10, 10, 30, 10, 10]; // the default boundary for solve source is const = 1.0 auto const_bou(std::make_shared<boundary_single>(1.0)); auto T_bou_i(std::make_shared<boundary_single>(T_in_pipe[run_index])); auto T_bou_o(std::make_shared<boundary_single>(T_out_pipe[run_index])); patch_v_bou_vec_type v_bou_s_def({ {{const_bou},{const_bou}}, {{const_bou},{const_bou}} }); patch_v_bou_vec_type v_bou_T_i({ {{T_bou_i},{T_bou_i}}, {{T_bou_i},{T_bou_i}} }); patch_v_bou_vec_type v_bou_T_o({ {{T_bou_o},{T_bou_o}}, {{T_bou_o},{T_bou_o}} }); patch_v_bou_type v_bou_def({{"s",v_bou_s_def}}); patch_v_bou_type v_bou_i({{"s",v_bou_s_def},{"T",v_bou_T_i}}); patch_v_bou_type v_bou_o({{"s",v_bou_s_def},{"T",v_bou_T_o}}); // create patch groups; point pt_xz( x->get(0), (y->get(0) + y->get(1)) / 2.0, (z->get(2) + z->get(3)) / 2.0); point pt_ym( (x->get(2) + x->get(5)) / 2.0, 0.0, z->get(3)); point pt_h_in( (x->get(3) + x->get(4)) / 2.0, y->get(1), (z->get(1) + z->get(2)) / 2.0); point pt_h_out( (x->get(3) + x->get(4)) / 2.0, y->get(1), (z->get(4) + z->get(5)) / 2.0); point pt_in( x->get(2), (y->get(1) + y->get(2)) / 2.0, (z->get(1) + z->get(2)) / 2.0); point pt_out( x->get(5), (y->get(1) + y->get(2)) / 2.0, (z->get(4) + z->get(5)) / 2.0); //============================================================== auto prob = std::make_shared<Prob>(prob_names[run_index], X, n, 2E4, 2E4); prob->create_equation("T", 10.0, 1.5, 1.5); prob->create_equation("s", 10.0, 1.5, 1.5); auto g_xyz = prob->create_patch_group("xyz", {{"T",0.0}, {"s",2.0}}, {{"T",0.0},{"s",s}}, pt_xz); auto g_h_in = prob->create_patch_group("h_in", {{"T",0.0}, {"s",2.0}}, {{"T",0.0},{"s",s}}, pt_h_in); auto g_h_out = prob->create_patch_group("h_out", {{"T",0.0}, {"s",2.0}}, {{"T",0.0},{"s",s}}, pt_h_out); auto g_in = prob->create_patch_group("in", {{"T",0.0}, {"s",2.0}}, {{"T",0.0},{"s",s}}, pt_in); auto g_out = prob->create_patch_group("out", {{"T",0.0}, {"s",2.0}}, {{"T",0.0},{"s",s}}, pt_out); auto g_ym = prob->create_patch_group("out", {{"T",T_ym[run_index]}, {"s",2.0}}, {{"T",0.0},{"s",s}}, pt_ym); // create patches auto p_in_xm = g_in->create_patch("p_in_xm", -1, {1}, {1,2}, {1,2}, v_bou_i); auto p_in_xp = g_in->create_patch("p_in_xp", 1, {2}, {1,2}, {1,2}, v_bou_i); auto p_in_zm = g_in->create_patch("p_in_zm", -3, {1,2}, {1,2}, {1}, v_bou_i); auto p_in_zp = g_in->create_patch("p_in_zp", 3, {1,2}, {1,2}, {2}, v_bou_i); auto p_out_xm = g_out->create_patch("p_out_xm", -1, {5}, {1,2}, {4,5}, v_bou_o); auto p_out_xp = g_out->create_patch("p_out_xp", 1, {6}, {1,2}, {4,5}, v_bou_o); auto p_out_zm = g_out->create_patch("p_out_zm", -3, {5,6}, {1,2}, {4}, v_bou_o); auto p_out_zp = g_out->create_patch("p_out_zp", 3, {5,6}, {1,2}, {5}, v_bou_o); // ym auto p_ym_0_0 = g_ym->create_patch("p_ym_0_0", -2, {0,1,2,3}, {0}, {0,1}, v_bou_def); auto p_ym_0_1 = g_ym->create_patch("p_ym_0_1", -2, {0,1,2,3}, {0}, {1,2,3,4,5}, v_bou_def); auto p_ym_0_2 = g_ym->create_patch("p_ym_0_2", -2, {0,1,2,3}, {0}, {5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}}, "s":{{1.0,1.0},{1.0,1.0}}}); auto p_ym_1_0 = g_ym->create_patch("p_ym_1_0", -2, {3,4}, {0}, {0,1}, v_bou_def); auto p_ym_1_1 = g_ym->create_patch("p_ym_1_1", -2, {3,4}, {0}, {1,2,3,4,5}, v_bou_def); auto p_ym_1_2 = g_ym->create_patch("p_ym_1_2", -2, {3,4}, {0}, {5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,T_irr_zp}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_ym_2_0 = g_ym->create_patch("p_ym_2_0", -2, {4,5,6,7}, {0}, {0,1}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}}, "s":{{1.0,1.0},{1.0,1.0}}}); auto p_ym_2_1 = g_ym->create_patch("p_ym_2_1", -2, {4,5,6,7}, {0}, {1,2,3,4,5}, v_bou_def);// = {"T":{{0.0,T_irr_xp},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_ym_2_2 = g_ym->create_patch("p_ym_2_2", -2, {4,5,6,7}, {0}, {5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}}, "s":{{1.0,1.0},{1.0,1.0}}}); // yp auto p_yp_0_0 = g_in->create_patch("p_yp_0_0", 2, {0,1,2,3}, {1}, {0,1}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_0_1_0 = g_in->create_patch("p_yp_0_1_0", 2, {0,1}, {1}, {1,2,3}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_0_1_1 = g_in->create_patch("p_yp_0_1_1", 2, {1,2}, {1}, {2,3}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_0_1_2 = g_in->create_patch("p_yp_0_1_2", 2, {2,3}, {1}, {1,2,3}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_0_2 = g_h_out->create_patch("p_yp_0_2", 2, {0,1,2,3}, {1}, {3,4,5}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_0_3 = g_h_out->create_patch("p_yp_0_3", 2, {0,1,2,3}, {1}, {5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_1_0 = g_h_in->create_patch("p_yp_1_0", 2, {3,4}, {1}, {0,1}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_1_1 = g_h_in->create_patch("p_yp_1_1", 2, {3,4}, {1}, {1,2,3}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_1_2 = g_h_out->create_patch("p_yp_1_2", 2, {3,4}, {1}, {3,4,5}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_1_3 = g_h_out->create_patch("p_yp_1_3", 2, {3,4}, {1}, {5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_2_0 = g_h_in->create_patch("p_yp_2_0", 2, {4,5,6,7}, {1}, {0,1}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_2_1 = g_h_in->create_patch("p_yp_2_1", 2, {4,5,6,7}, {1}, {1,2,3}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_2_2_0 = g_out->create_patch("p_yp_2_2_0", 2, {4,5}, {1}, {3,4,5}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_2_2_1 = g_out->create_patch("p_yp_2_2_1", 2, {5,6}, {1}, {3,4}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_2_2_2 = g_out->create_patch("p_yp_2_2_2", 2, {6,7}, {1}, {3,4,5}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_yp_2_3 = g_out->create_patch("p_yp_2_3", 2, {4,5,6,7}, {1}, {5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); // xp auto p_xm = g_xyz->create_patch("p_xm", -1, {0}, {0,1}, {0,1,2,3,4,5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_xp = g_xyz->create_patch("p_xp", 1, {7}, {0,1}, {0,1,2,3,4,5,6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_zm = g_xyz->create_patch("p_zm", -3, {0,1,2,3,4,5,6,7}, {0,1}, {0}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); auto p_zp = g_xyz->create_patch("p_zp", 3, {0,1,2,3,4,5,6,7}, {0,1}, {6}, v_bou_def);// = {"T":{{0.0,0.0},{0.0,0.0}},"s":{{1.0,1.0},{1.0,1.0}}}); // stitching // in xm stitch(p_in_xm, p_in_zm); stitch(p_in_xm, p_in_zp); stitch(p_in_xm, p_yp_0_1_0); // in xp stitch(p_in_xp, p_in_zm); stitch(p_in_xp, p_in_zp); stitch(p_in_xp, p_yp_0_1_2); // in z stitch(p_in_zm, p_yp_0_0); stitch(p_in_zp, p_yp_0_1_1); // out xm stitch(p_out_xm, p_out_zm); stitch(p_out_xm, p_out_zp); stitch(p_out_xm, p_yp_2_2_0); // out xp stitch(p_out_xp, p_out_zm); stitch(p_out_xp, p_out_zp); stitch(p_out_xp, p_yp_2_2_2); // out z stitch(p_out_zp, p_yp_2_3); stitch(p_out_zm, p_yp_2_2_1); // xm stitch(p_xm,p_ym_0_0); stitch(p_xm,p_ym_0_1); stitch(p_xm,p_ym_0_2); stitch(p_xm,p_yp_0_0); stitch(p_xm,p_yp_0_1_0); stitch(p_xm,p_yp_0_2); stitch(p_xm,p_yp_0_3); stitch(p_xm,p_zm); stitch(p_xm,p_zp); // xp stitch(p_xp,p_ym_2_0); stitch(p_xp,p_ym_2_1);; stitch(p_xp,p_ym_2_2); stitch(p_xp,p_yp_2_0); stitch(p_xp,p_yp_2_1); stitch(p_xp,p_yp_2_2_2); stitch(p_xp,p_yp_2_3); stitch(p_xp,p_zm); stitch(p_xp,p_zp); //zm; stitch(p_zm,p_ym_0_0); stitch(p_zm,p_ym_1_0); stitch(p_zm,p_ym_2_0); stitch(p_zm,p_yp_0_0); stitch(p_zm,p_yp_1_0); stitch(p_zm,p_yp_2_0); // zp; stitch(p_zp,p_ym_0_2); stitch(p_zp,p_ym_1_2); stitch(p_zp,p_ym_2_2); stitch(p_zp,p_yp_0_3); stitch(p_zp,p_yp_1_3); stitch(p_zp,p_yp_2_3); // yp // row 0 stitch(p_yp_0_0, p_yp_1_0); stitch(p_yp_0_0, p_yp_0_1_0); stitch(p_yp_0_0, p_yp_0_1_2); stitch(p_yp_1_0, p_yp_1_1); stitch(p_yp_1_0, p_yp_2_0); stitch(p_yp_2_0, p_yp_2_1); // row 1 stitch(p_yp_0_1_0, p_yp_0_1_1); stitch(p_yp_0_1_0, p_yp_0_2); stitch(p_yp_0_1_1, p_yp_0_1_2); stitch(p_yp_0_1_1, p_yp_0_2); stitch(p_yp_0_1_2, p_yp_1_1); stitch(p_yp_0_1_2, p_yp_0_2); stitch(p_yp_1_1, p_yp_2_1); stitch(p_yp_1_1, p_yp_1_2); stitch(p_yp_2_1, p_yp_2_2_0); stitch(p_yp_2_1, p_yp_2_2_1); stitch(p_yp_2_1, p_yp_2_2_2); // row 2 stitch(p_yp_0_2, p_yp_1_2); stitch(p_yp_0_2, p_yp_0_3); stitch(p_yp_1_2, p_yp_2_2_0); stitch(p_yp_1_2, p_yp_1_3); stitch(p_yp_2_2_0, p_yp_2_2_1); stitch(p_yp_2_2_0, p_yp_2_3); stitch(p_yp_2_2_1, p_yp_2_2_2); stitch(p_yp_2_2_2, p_yp_2_3); // row 3 stitch(p_yp_0_3, p_yp_1_3); stitch(p_yp_1_3, p_yp_2_3); // ym; stitch(p_ym_0_0, p_ym_0_1); stitch(p_ym_0_0, p_ym_1_0); stitch(p_ym_1_0, p_ym_1_1); stitch(p_ym_1_0, p_ym_2_0); stitch(p_ym_2_0, p_ym_2_1); stitch(p_ym_0_1, p_ym_1_1); stitch(p_ym_0_1, p_ym_0_2); stitch(p_ym_1_1, p_ym_2_1); stitch(p_ym_1_1, p_ym_1_2); stitch(p_ym_2_1, p_ym_2_2); stitch(p_ym_0_2, p_ym_1_2); stitch(p_ym_1_2, p_ym_2_2); for(auto g : prob->patch_groups_) { g->get_value_of_interest_residual("T"); } // solve; prob->connection_info(); solve_with_source(prob); //solve_source(prob); //solve_temp(prob); prob->value_stats("T"); };
void ICM_iteration(int N, int n, int K, int nlast, int first[], int m[], int **freq, int **ind, double **y, double **F, double **F_new, double **cumw, double **cs, double **v, double **w, double **nabla, double *lambda1, double *alpha1) { int i,j,k; double a,lambda,alpha; lambda=*lambda1; for (k=1;k<=K;k++) { for (i=1;i<=m[k];i++) v[k][i]=w[k][i]=0; } for (k=1;k<=K;k++) { j=0; for (i=first[k];i<=n;i++) { if (freq[k][i]>0) { j++; v[k][j] = freq[k][i]/(N*F[k][i]); w[k][j] = freq[k][i]/(N*SQR(F[k][i])); } if (freq[0][i]>0) { v[k][j] -= freq[0][i]/(N*(1-F[K+1][i])); w[k][j] += freq[0][i]/(N*SQR(1-F[K+1][i])); } } if (nlast<n) v[k][m[k]] -=lambda; for (i=1;i<=m[k];i++) v[k][i] += w[k][i]*F[k][ind[k][i]]; } cumsum(K,m,v,cs); convexmin(K,m,cumw,cs,w,y); Compute_F(K,n,freq,first,y,F_new); if (F_new[K+1][nlast]>=1) { a=(1-F[K+1][nlast])/(F_new[K+1][nlast]-F[K+1][nlast]); for (k=1;k<=K;k++) { for (i=first[k];i<=n;i++) F_new[k][i]=F[k][i]+a*(F_new[k][i]-F[k][i]); } compute_Fplus(n,K,F_new); for (k=1;k<=K;k++) { j=0; for (i=first[k];i<=n;i++) { if (freq[k][i]>0) { j++; y[k][j]=F_new[k][i]; } } } } if (f_alpha_prime(N,n,K,freq,1.0,F,F_new,lambda)<=0) alpha=1; else alpha=golden(N,n,K,freq,F,F_new,0.1,1,f_alpha,lambda); for (k=1;k<=K;k++) { for (i=first[k];i<=n;i++) F[k][i] += alpha*(F_new[k][i]-F[k][i]); } compute_Fplus(n,K,F); if (nlast<n) lambda=compute_lambda(N,n,K,freq,F); else lambda=0; compute_nabla(N,n,K,first,m,freq,F,lambda,nabla); *lambda1=lambda; *alpha1=alpha; }
PMGhostData * pm_ghosts_create(PM * pm, PMStore *p, int attributes, fastpm_posfunc get_position) { PMGhostData * pgd = malloc(sizeof(pgd[0])); pgd->pm = pm; pgd->p = p; pgd->np = p->np; pgd->np_upper = p->np_upper; pgd->attributes = attributes; if(get_position == NULL) pgd->get_position = p->get_position; else pgd->get_position = get_position; pgd->nghosts = 0; ptrdiff_t i; size_t Nsend; size_t Nrecv; size_t elsize = p->pack(pgd->p, 0, NULL, pgd->attributes); pgd->Nsend = calloc(pm->NTask, sizeof(int)); pgd->Osend = calloc(pm->NTask, sizeof(int)); pgd->Nrecv = calloc(pm->NTask, sizeof(int)); pgd->Orecv = calloc(pm->NTask, sizeof(int)); pgd->elsize = elsize; memset(pgd->Nsend, 0, sizeof(pgd->Nsend[0]) * pm->NTask); pm_iter_ghosts(pm, pgd, count_ghosts); Nsend = cumsum(pgd->Osend, pgd->Nsend, pm->NTask); MPI_Alltoall(pgd->Nsend, 1, MPI_INT, pgd->Nrecv, 1, MPI_INT, pm->Comm2D); Nrecv = cumsum(pgd->Orecv, pgd->Nrecv, pm->NTask); pgd->ighost_to_ipar = fastpm_memory_alloc(pm->mem, Nsend * sizeof(int), FASTPM_MEMORY_HEAP); pgd->send_buffer = fastpm_memory_alloc(pm->mem, Nsend * pgd->elsize, FASTPM_MEMORY_HEAP); pgd->recv_buffer = fastpm_memory_alloc(pm->mem, Nrecv * pgd->elsize, FASTPM_MEMORY_HEAP); memset(pgd->Nsend, 0, sizeof(pgd->Nsend[0]) * pm->NTask); pm_iter_ghosts(pm, pgd, build_ghost_buffer); /* exchange */ pgd->nghosts = Nrecv; if(Nrecv + pgd->np > pgd->np_upper) { fastpm_raise(-1, "Too many ghosts; asking for %td, space for %td\n", Nrecv, pgd->np_upper - pgd->np); } MPI_Datatype GHOST_TYPE; MPI_Type_contiguous(pgd->elsize, MPI_BYTE, &GHOST_TYPE); MPI_Type_commit(&GHOST_TYPE); MPI_Alltoallv_sparse(pgd->send_buffer, pgd->Nsend, pgd->Osend, GHOST_TYPE, pgd->recv_buffer, pgd->Nrecv, pgd->Orecv, GHOST_TYPE, pm->Comm2D); MPI_Type_free(&GHOST_TYPE); #pragma omp parallel for for(i = 0; i < Nrecv; i ++) { p->unpack(pgd->p, pgd->np + i, (char*) pgd->recv_buffer + i * pgd->elsize, pgd->attributes); } fastpm_memory_free(pm->mem, pgd->recv_buffer); fastpm_memory_free(pm->mem, pgd->send_buffer); return pgd; }
Particles ResamplingResidual::resample(Particles* particles){ Particles resampledSet; Particles stage1; int count = 0; resampledSet.samples = fmat(particles->samples.n_rows,particles->samples.n_cols); resampledSet.weights = frowvec(particles->weights.n_cols); fvec cumsum; fvec random; unsigned int number = particles->samples.n_cols; unsigned int numberOfStage1 = 0; // Generating copie information of every particle ivec copies = zeros<ivec>(particles->samples.n_cols); for (unsigned int i=0;i<particles->samples.n_cols;++i){ copies = (int) floor(number*particles->weights(i)); } numberOfStage1 = sum(copies); stage1.samples = fmat(particles->samples.n_rows,numberOfStage1); stage1.weights = frowvec(numberOfStage1); //Picking N_i = N*w_i copies of i-th particle for (unsigned int i=1; i < copies.n_rows;++i){ for (int j=0;j<copies(i);++j){ for (unsigned int k=0;k<particles->samples.n_rows;++k){ stage1.samples(k,count) = particles->samples(k,i); } stage1.weights(count) = particles->weights(i) - (float)copies(i)/number; count++; } } // multinomial resampling with residuum weights w_i = w_i - N_i/N cumsum = zeros<fvec>(numberOfStage1); for (unsigned int i=1; i < stage1.weights.n_cols;++i){ cumsum(i) = cumsum(i-1) + stage1.weights(i); } // generate sorted random set random = randu<fvec>(numberOfStage1); random=random*cumsum(cumsum.n_rows-1); random = sort(random); for (unsigned int j=0; j < random.n_rows; ++j){ for (unsigned int i=0 ; i < cumsum.n_rows; ++i){ if (random(j) <= cumsum(i)){ if(i > 0){ if(random(j) >= cumsum(i-1)) { for (unsigned int k=0;k<stage1.samples.n_rows;++k){ resampledSet.samples(k,j) = stage1.samples(k,i); assignmentVec.push_back(i); } break; } } else { for (unsigned int k=0; k<stage1.samples.n_rows; ++k){ resampledSet.samples(k,j) = stage1.samples(k,i); assignmentVec.push_back(i); } break; } } // Normalize weights resampledSet.weights(j) = 1.0f/particles->weights.n_cols; } } return resampledSet; }