// [[Rcpp::export(name = ".contigCells")]] IntegerVector contigCells_cpp(int pt, int bgr, NumericMatrix mtx) { int rr; int cc; int dim1 = mtx.nrow(); int dim2 = mtx.ncol(); IntegerVector r(4); IntegerVector c(4); IntegerVector ad; int idx; if (pt % dim1 == 0) { rr = dim1; cc = pt / dim1; } else { cc = trunc(pt / dim1) + 1; rr = pt - (cc-1) * dim1; } r[0] = rr-1; r[1] = rr+1; r[2] = rr; r[3] = rr; c[0] = cc; c[1] = cc; c[2] = cc-1; c[3] = cc+1; for (int i = 0; i < 4; i++){ if(r[i] > 0 && r[i] <= dim1 && c[i] > 0 && c[i] <= dim2){ idx = r[i] + (c[i] - 1) * dim1; if(mtx[idx-1] == bgr){ ad.push_back(idx); } } } return(ad); }
//function to return indices of NA values // [[Rcpp::export]] IntegerVector whichNA(NumericVector input){ IntegerVector out; for(int i=0; i<input.size(); i++){ if(R_IsNA(input(i))){ out.push_back(i); } } return(out); }
int main() { Theron::Framework framework; Theron::ActorRef actor(framework.CreateActor<Catcher>()); // Create a message and fill it with some values. IntegerVector message; message.push_back(4); message.push_back(7); message.push_back(2); // Send the message to the catcher, passing the address of a local receiver // as the 'from' address. Note that the message is copied during message // passing, including all of its contents. See the EnvelopeMessages sample // for a workaround that avoids this overhead. Theron::Receiver receiver; framework.Send(message, receiver.GetAddress(), actor.GetAddress()); // Wait for confirmation that the message was received before terminating. receiver.Wait(); return 0; }
// [[Rcpp::export]] IntegerVector lower_bound__(const NumericVector xq, const NumericVector xvec) { IntegerVector idxvec; MyComparator comparator; try { for (int x:xq){ idxvec.push_back(std::distance(xvec.begin(), std::lower_bound(xvec.begin(), xvec.end(), x, comparator))); } return (idxvec); } catch( ...) { ::Rf_error("c++ exception (unknown reason)"); } // TODO(cp): handle possible exceptions }
IntegerVector score(IntegerVector& Profiles, int nContributors, int nLoci){ IntegerVector vResults; int nLoc, nContrib; for(nLoc = 0; nLoc < nLoci; nLoc++){ std::map<int, int> mapCounts; for(nContrib = 0; nContrib < nContributors; nContrib++){ int nOffset = 2 * nLoci * nContrib; int nA1 = Profiles[nOffset + 2 * nLoc]; int nA2 = Profiles[nOffset + 2 * nLoc + 1]; // doesn't matter what we count because it's presence we're interested in mapCounts[nA1] = 1; mapCounts[nA2] = 1; } vResults.push_back(mapCounts.size()); } return vResults; }
// [[Rcpp::export]] List starvingforager_eventNM( int L, //Lattice dim int t_term, //Terminal time double alpha, //Resource growth rate double K, //Resource carrying capacity double sigma, //Starvation rate double rho, //Recovery rate double lambda, //Growth rate double mu, //Mortality rate IntegerVector ind_vec, //Initial vector of states IntegerVector loc_vec //Initial vector of locations ) { //Dimension of the lattice int dim = 2; //Lattice size double size = pow(L-2,dim); //Initial time double t = 0; double max; double min; //Output Lists List ind_out(1); List loc_out(1); NumericVector t_out(1); //The initial state ind_out(0) = ind_vec; loc_out(0) = loc_vec; t_out(0) = 0; //ind_vec: the vector of individual states... 0 = resource, 1=starver, 2=full //pos_vec: the vector of individual locations //Initial count of how many resouces, starvers, and full in this timestep?? //Count the number of individual R + S + F int tot = ind_vec.size(); double R = 0.L; double S = 0.L; double F = 0.L; // double Rp; // double Sp; // double Fp; for (int i=0;i<tot;i++) { if (ind_vec(i) == 0) { R = R + 1.L; } if (ind_vec(i) == 1) { S = S + 1.L; } if (ind_vec(i) == 2) { F = F + 1.L; } } //R,S,F are thus densities over the landscape of size 'size' // R = R/size; // S = S/size; // F = F/size; double R_pr_line; double S_pr_line; double F_pr_line; //Iterate over time //The loop stops when t > t_term-1... and will record the last value int tic = 1; while (t < (t_term-1)) { //Construct probability lines, which are a function of R, S, F //Grow <-----> Consumed R_pr_line = (alpha*(K-R))/((alpha*(K-R)) + (F + S)); //R_pr_line(1) = R_pr_line(0) + ((F + S)/((alpha*(K-R)) + (F + S) + Dr)); //R_pr_line(2) = R_pr_line(1) + (Dr/((alpha*(K-R)) + (F + S) + Dr)); //Recover <-----> Mortality S_pr_line = (rho*R)/(rho*R + mu); //S_pr_line(1) = S_pr_line(0) + (mu/(rho*R + mu + Ds)); //S_pr_line(2) = S_pr_line(1) + (Ds/(rho*R + mu + Ds)); //Grow <-----> Starve F_pr_line = lambda/(lambda+sigma*(K-R)); //F_pr_line(1) = F_pr_line(0) + ((sigma*(K-R))/(lambda+sigma*(K-R)+Df)); //F_pr_line(2) = F_pr_line(1) + (Df/(lambda+sigma*(K-R)+Df)); //Initiate variables double dt; //Randomly select an individual (R,S,F) with probability 1/N //ind thus represents the POSITION of the individual //Update total number of individuals tot = ind_vec.size(); max = (double)(tot - 1); min = 0.L; int id = min + (rand() % (int)(max - min + 1)); int state; int location; double draw_event; //If ind is a resource... if (ind_vec(id) == 0) { state = 0; location = loc_vec(id); //Draw a random event //Grow, become consumed or move? draw_event = ((double) rand() / (RAND_MAX)); //Grow if (draw_event < R_pr_line) { //Append a new resource to the END of the vector ind_vec.push_back(state); //Append the resource's location to the END of the vector loc_vec.push_back(location); //Update Tally R = R + 1; //(1.L/size); } //Become consumed!!!! if ((draw_event >= R_pr_line) && (draw_event < 1.L)) { //Remove the consumed resource from the state vector ind_vec.erase(id); //Remove the consumed resource form the location vector loc_vec.erase(id); //Update Tally R = R - 1; //(1.L/size); } dt = 1.L/((alpha*(K-R)) + (F + S)); } //If ind is a starver... if (ind_vec(id) == 1) { state = 1; location = loc_vec(id); //Draw a random event //Recover, die, or move?? draw_event = ((double) rand() / (RAND_MAX)); //Recover if (draw_event < S_pr_line) { //Update the state from starver to full ind_vec(id) = 2; //Update Tally S = S - 1; //(1.L/size); F = F + 1; //(1.L/size); } //Die if ((draw_event >= S_pr_line) && (draw_event < 1.L)) { //Remove the consumed resource from the state vector ind_vec.erase(id); //Remove the consumed resource form the location vector loc_vec.erase(id); //Update Tally S = S - 1; //(1.L/size); } dt = 1.L/(rho*R + mu); } //If ind is Full... if (ind_vec(id) == 2) { state = 2; location = loc_vec(id); //Draw a random event //Grow, starve, or move? draw_event = ((double) rand() / (RAND_MAX)); //Grow if (draw_event < F_pr_line) { //Append a new resource to the END of the vector ind_vec.push_back(state); //Append the resource's location to the END of the vector loc_vec.push_back(location); F = F + 1; //(1.L/size); } //Starve if ((draw_event >= F_pr_line) && (draw_event < 1.L)) { //Update the state from full to starver ind_vec(id) = 1; //Update Tally F = F - 1; //(1.L/size); S = S + 1; //(1.L/size); } dt = 1.L/(lambda+sigma*(K-R)); } //Advance time t = t + dt; //Rcout << "t = " << dt << std::endl; //Update output ind_out.push_back(ind_vec); loc_out.push_back(loc_vec); t_out.push_back(t); tic = tic + 1; } //end while loop over t List cout(3); cout(0) = ind_out; cout(1) = loc_out; cout(2) = t_out; return(cout); }
// [[Rcpp::export]] List EDM_percent(const NumericVector& Z, int min_size=24, double percent=0, int degree=0){ //Z: time series //min_size: minimum segment size //beta: penalization term for the addition of a change point //identify which type of penalization to use double (*G)(double); switch(degree){ case 1: G=Linear; break; case 2: G=Quadratic; break; default: G=Const; break; } int n = Z.size(); vector<int> prev(n+1,0);//store optimal location of previous change point vector<int> number(n+1,0);//store the number of change points in optimal segmentation vector<double> F(n+1,0);//store optimal statistic value //F[s] is calculated using observations { Z[0], Z[1], ..., Z[s-1] } //trees used to store the "upper half" of the considered observations multiset<double> right_min, left_min; //trees used to store the "lower half" of the considered observations multiset<double, std::greater<double> > right_max, left_max; //Iterate over possible locations for the last change for(int s=2*min_size; s<n+1; ++s){ right_max.clear(); right_min.clear();//clear right trees left_max.clear(); left_min.clear();//clear left trees //initialize left and right trees to account for minimum segment size for(int i=prev[min_size-1]; i<min_size-1; ++i) insert_element(left_min, left_max, Z[i]); for(int i=min_size-1; i<s; ++i) insert_element(right_min, right_max, Z[i]); //Iterate over possible locations for the penultiamte chagne for(int t=min_size; t<s-min_size+1; ++t){//modify limits to deal with min_size insert_element(left_min, left_max, Z[t-1]);//insert element into left tree remove_element(right_min, right_max, Z[t-1]);//remove element from right tree //left tree now has { Z[prev[t-1]], ..., Z[t-1] } //right tree now has { Z[t], ..., Z[s-1] } //check to see if optimal position of previous change point has changed //if so update the left tree if(prev[t] > prev[t-1]){ for(int i=prev[t-1]; i<prev[t]; ++i) remove_element(left_min, left_max, Z[i]); } else if(prev[t] < prev[t-1]){ for(int i=prev[t]; i<prev[t-1]; ++i) insert_element(left_min, left_max, Z[i]); } //calculate statistic value double left_median = get_median(left_min,left_max), right_median = get_median(right_min,right_max); double normalize = ( (t-prev[t]) * (s-t) ) / ( std::pow(s-prev[t],2) ); double tmp = F[t] + normalize * std::pow(left_median - right_median,2); //Find best location for change point. check % condition later if(tmp > F[s]){ number[s] = number[t] + 1; F[s] = tmp; prev[s] = t; } } //check to make sure we meet the percent change requirement if( prev[s]){ if(F[s] - F[prev[s]] < percent*G(number[prev[s]])*F[prev[s]]){ number[s] = number[prev[s]]; F[s] = F[prev[s]]; prev[s] = prev[prev[s]]; } } } //obtain list of optimal change point estimates IntegerVector ret; int at = n; while(at){ if(prev[at])//don't insert 0 as a change point estimate ret.push_back(prev[at]); at = prev[at]; } sort(ret.begin(),ret.end()); //return statment for debugging //return List::create(_["loc"]=ret, _["F"]=F, _["number"]=number,_["prev"]=prev); return List::create(_["loc"]=ret); }
// [[Rcpp::export]] IntegerVector integer_push_back( IntegerVector y ){ y.push_back( 5 ) ; return y ; }
DataFrame span_matcher_worker(DataFrame df1, DataFrame df2) { // put info in neat litle vectors CharacterVector id_one_in = df1[0]; IntegerVector start_one_in = df1[1]; IntegerVector end_one_in = df1[2]; CharacterVector id_two_in = df2[0]; IntegerVector start_two_in = df2[1]; IntegerVector end_two_in = df2[2]; // determine span length int min_start_one = min(start_one_in); int min_start_two = min(start_two_in); int max_end_one = max(end_one_in); int max_end_two = max(end_two_in); int min_span = min(min_start_one, min_start_two) ; int max_span = max(max_end_one, max_end_two); // put days:ids into multimap multimap<int, string> days_id_one; multimap<int, string> days_id_two; for( int i = 0; i < id_one_in.size(); i++ ){ for (int day = start_one_in[i]; day <= end_one_in[i]; day++ ){ days_id_one.insert(pair<int, string>(day, as<string>(id_one_in[i]) )); } } for( int i = 0; i < id_two_in.size(); i++ ){ for (int day = start_two_in[i]; day <= end_two_in[i]; day++ ){ days_id_two.insert(pair<int, string>(day, as<string>(id_two_in[i]))); } } // access the columns IntegerVector days; CharacterVector id_one ; CharacterVector id_two ; for (int i = min_span; i <= max_span; i++) { // no matches if( days_id_one.count(i)==0 && days_id_two.count(i)==0 ){ days.push_back(i); id_one.push_back(NA_STRING); id_two.push_back(NA_STRING); continue; } // matches for one but not for two if( days_id_one.count(i)>0 && days_id_two.count(i)==0 ){ auto range = days_id_one.equal_range(i); for (auto iterate = range.first; iterate != range.second; ++iterate) { days.push_back(i); id_one.push_back(iterate->second); id_two.push_back(NA_STRING); } continue; } // matches for two but not for one if( days_id_one.count(i)==0 && days_id_two.count(i)>0 ){ auto range = days_id_two.equal_range(i); for (auto iterate = range.first; iterate != range.second; ++iterate) { days.push_back(i); id_one.push_back(NA_STRING); id_two.push_back(iterate->second); } continue; } // matches in both if( days_id_one.count(i)>0 && days_id_two.count(i)>0 ){ auto range = days_id_one.equal_range(i); for (auto it = range.first; it != range.second; it++) { auto range_two = days_id_two.equal_range(i); for (auto it_two = range_two.first; it_two != range_two.second; it_two++) { days.push_back(i); id_one.push_back(it->second); id_two.push_back(it_two->second); } } } }; // return a new data frame return DataFrame::create( _["days"]=days, _["id_one"]=id_one, _["id_two"]=id_two) ; }