// [[Rcpp::export]] const bool test3(IntegerVector ipd, int ipd_) { //int max_ipd = max(ipd); if (std::find(ipd.begin(), ipd.end(), ipd_) != ipd.end()) { return true; } else { return false; } }
// [[Rcpp::export]] List integer_erase_range_2( IntegerVector x, IntegerVector y ){ IntegerVector::iterator it = x.begin()+1 ; while( it != x.end() ){ it = x.erase(it) ; } it = y.begin() + 1 ; while( it != y.end() ){ it = y.erase(it) ; } return List::create( x, y ) ; }
//' @title Mode //' @description //' \code{mode} returns the most frequent value of an integer vector //' //' @param x - An integer vector //' //' @examples //' mode(c(1,2,2)) //' //' @return Most frequent value of \code{x} //' //' @export // [[Rcpp::export]] int mode(IntegerVector x) { if(x.size()==0) return NA_INTEGER; IntegerVector y = clone(x); std::sort(y.begin(),y.end()); int maxCount=1, mode=y[0], count=1; for(int i=1;i<y.size();i++){ if(y[i]==y[i-1]) count++; else { if(count>maxCount) { maxCount=count; mode=y[i-1]; } count=1; } } if(count>maxCount) mode=y[y.size()-1]; return mode; }
void single_session(IntegerVector timestamps, IntegerVector& delta_output, int& ts_iter_count, int& threshold, std::deque <int>& hash_reps){ int delta_holding; int hash_rep_holding = 1; delta_output[ts_iter_count] = NA_INTEGER; if(timestamps.size() >= 2) { std::sort(timestamps.begin(), timestamps.end()); for(unsigned int i = 1; i < timestamps.size(); i++){ ts_iter_count++; delta_holding = (timestamps[i] - timestamps[i-1]); if(delta_holding >= threshold){ delta_output[ts_iter_count] = NA_INTEGER; hash_reps.push_back(hash_rep_holding); hash_rep_holding = 1; } else { if(timestamps[i] == NA_INTEGER || timestamps[i-1] == NA_INTEGER){ delta_output[ts_iter_count] = NA_INTEGER; } else { delta_output[ts_iter_count] = delta_holding; } hash_rep_holding++; } } } hash_reps.push_back(hash_rep_holding); ts_iter_count++; }
// **********************************************************// // Calculate xi over the entire corpus // // **********************************************************// // [[Rcpp::export]] List xi_all(NumericMatrix timemat, NumericMatrix eta1,NumericMatrix eta2, IntegerVector edgetrim) { List xi(timemat.nrow()); for (IntegerVector::iterator it = edgetrim.begin(); it != edgetrim.end(); ++it) { xi[*it-1] = ximat(timemat(*it-2, _), eta1, eta2); } return xi; }
RCPP_FUNCTION_2(List, lme4_PermChk, IntegerVector perm, IntegerVector x) { IntegerVector zerob = clone(perm); // modifiable copy int bb = *(std::min_element(zerob.begin(), zerob.end())); if (bb != 0) zerob = zerob - bb; MatrixNs::Permutation pp(zerob); return List::create(_["forw"] = pp.forward(x), _["inv"] = pp.inverse(x)); }
// [[Rcpp::export]] std::tr1::unordered_set<int> unique1(IntegerVector x) { std::tr1::unordered_set<int> seen; for(IntegerVector::iterator it = x.begin(); it != x.end(); ++it) { seen.insert(*it); } return seen; }
// **********************************************************// // Calculate mu matrix for entire document // // **********************************************************// // [[Rcpp::export]] NumericMatrix mu_mat(NumericMatrix p_d, List xi, IntegerVector edgetrim) { NumericMatrix sample = xi[max(edgetrim)-1]; NumericMatrix mumat(xi.size(), sample.nrow()); for (IntegerVector::iterator it = edgetrim.begin(); it != edgetrim.end(); ++it) { int it2 = *it-1; mumat(it2, _) = mu_vec(p_d(it2, _), xi[it2]); } return mumat; }
// [[Rcpp::export]] const bool test2(IntegerVector ipd, int ipd_) { //int max_ipd = max(ipd); std::set<int> ipd_set(ipd.begin(), ipd.end()); if (ipd_set.find(ipd_) != ipd_set.end()) { return true; } else { return false; } }
// **********************************************************// // Likelihood evaluation of Timepart // // **********************************************************// // [[Rcpp::export]] double Timepartsum(NumericMatrix mumat, double sigma_tau, IntegerVector senders, NumericVector timeinc, IntegerVector edgetrim){ double timesum = 0; for (IntegerVector::iterator it = edgetrim.begin(); it != edgetrim.end(); ++it) { int it2 = *it-1; double a_d = senders[it2]; timesum += Timepart(mumat(it2,_), sigma_tau, a_d, timeinc[it2]); } return timesum; }
int mode(IntegerVector x) { int n=unique(x).size(); NumericVector y(n); for (int i=0; i<n; ++i) y[i]=std::count(x.begin(),x.end(),unique(x)[i]); int m=max(y); int q=std::distance(y.begin(),std::find(y.begin(),y.end(),m)); return unique(x)[q]; }
// [[Rcpp::export]] List runit_lang_unarycall(IntegerVector x){ Language call( "seq", Named("from", 10 ), Named("to", 0 ) ) ; List output( x.size() ) ; std::transform( x.begin(), x.end(), output.begin(), unary_call<int>(call) ) ; return output ; }
// [[Rcpp::export]] List runit_lang_binarycall(IntegerVector x1, IntegerVector x2 ){ Language call( "seq", Named("from", 10 ), Named("to", 0 ) ) ; List output( x1.size() ) ; std::transform( x1.begin(), x1.end(), x2.begin(), output.begin(), binary_call<int,int>(call) ) ; return output ; }
// [[Rcpp::export]] List runit_lang_unarycallindex(IntegerVector x){ Language call( "seq", 10, 0 ) ; List output( x.size() ) ; std::transform( x.begin(), x.end(), output.begin(), unary_call<int>(call,2) ) ; return output ; }
// get a set of permutations of a vector, as columns of a matrix // [[Rcpp::export]] IntegerMatrix permute_ivector(const int n_perm, const IntegerVector x) { unsigned int length = x.size(); IntegerMatrix result(length,n_perm); for(unsigned int i=0; i<n_perm; i++) { IntegerVector permx = permute_ivector(x); std::copy(permx.begin(), permx.end(), result.begin()+i*length); } return result; }
inline STORAGE process_chunk(const SlicingIndex& indices) { int n = indices.size(); if (n == 0 || idx > n || idx < -n) return def; int i = idx > 0 ? (idx -1) : (n+idx); typedef VectorSliceVisitor<ORDER_RTYPE> Slice; typedef OrderVectorVisitorImpl<ORDER_RTYPE,true,Slice> Visitor; typedef Compare_Single_OrderVisitor<Visitor> Comparer; Comparer comparer(Visitor(Slice(order, indices))); IntegerVector sequence = seq(0,n-1); std::nth_element(sequence.begin(), sequence.begin() + i, sequence.end(), comparer); return data[ indices[ sequence[i] ] ]; }
//parses the GR object. void parseRegions(std::vector<GArray>& container, RObject& gr, samfile_t* in){ if (not gr.inherits("GRanges")) stop("must provide a GRanges object"); IntegerVector starts = as<IntegerVector>(as<RObject>(gr.slot("ranges")).slot("start")); IntegerVector lens = as<IntegerVector>(as<RObject>(gr.slot("ranges")).slot("width")); RObject chrsRle = as<RObject>(gr.slot("seqnames")); RObject strandsRle = as<RObject>(gr.slot("strand")); RleIter chrs(chrsRle); RleIter strands(strandsRle); container.reserve(container.size() + starts.length()); Iint e_starts = starts.end(); Iint i_starts = starts.begin(); Iint i_lens = lens.begin(); int lastStrandRun = -1; int strand = -1; int lastChrsRun = -1; int rid = -1; for (; i_starts < e_starts; ++i_starts, ++i_lens, chrs.next(), strands.next()){ //if new run, update chromosome if (lastChrsRun != chrs.run){ lastChrsRun = chrs.run; rid = getRefId(in, chrs.getValue()); if (rid == -1) stop("chromosome " + (std::string)chrs.getValue() + " not present in the bam file"); } //if new run, update strand if (lastStrandRun != strands.run){ lastStrandRun = strands.run; const std::string& s = strands.getValue(); if (s == "-"){ strand = -1; } else if (s == "+"){ strand = +1; } else { strand = 0; } } container.push_back(GArray(rid, *i_starts - 1, *i_lens, strand)); } }
// [[Rcpp::export]] List auxGAPbbDpMulthreadKPs(IntegerMatrix cost, NumericMatrix profitOrLoss, IntegerVector budget, int maxCore = 7, double tlimit = 60, bool greedyBranching = true, String optim = "max") { int Ntask = cost.ncol(), Nagent = cost.nrow(); vec<signed char> Bcontainer(cost.size() + Ntask, -1); vec<WV<double, int> > costprofitInfo(cost.size()); double C = -std::numeric_limits<double>::max(); if(optim == "max") { for(int i = 0, iend = cost.size(); i < iend; ++i) { costprofitInfo[i].weight = cost[i]; costprofitInfo[i].value = profitOrLoss[i]; } } else { C = *std::max_element(profitOrLoss.begin(), profitOrLoss.end()) + 1; for(int i = 0, iend = cost.size(); i < iend; ++i) { costprofitInfo[i].weight = cost[i]; costprofitInfo[i].value = C - profitOrLoss[i]; } } vec<WV<double, int>*> wvptr(Ntask); for(int j = 0; j < Ntask; ++j) { wvptr[j] = &costprofitInfo[0] + j * INT(Nagent); } WV<double, int> **info = &wvptr[0]; vec<int> residualBudget(budget.begin(), budget.end()); vec<signed char> solution; double solutionRevenue = 0; std::time_t timer; time(&timer); int Nnode = 0, Nkp = 0; if(greedyBranching) { solutionRevenue = gapBabDp<double, int, true> ( solution, Bcontainer, Nagent, Ntask, info, &residualBudget[0], maxCore, timer, tlimit, Nnode, Nkp); } else { solutionRevenue = gapBabDp<double, int, false> ( solution, Bcontainer, Nagent, Ntask, info, &residualBudget[0], maxCore, timer, tlimit, Nnode, Nkp); } if(solutionRevenue == -std::numeric_limits<double>::max()) return List::create(); if(C != -std::numeric_limits<double>::max()) { solutionRevenue = C * Ntask - solutionRevenue; } IntegerVector agentCost(Nagent); IntegerVector assignment(Ntask); for(int i = 0; i < Nagent; ++i) { agentCost[i] = 0; for(int j = 0; j < Ntask; ++j) { if(solution[j * (Nagent + 1) + i] <= 0) continue; agentCost[i] += cost[j * Nagent + i]; assignment[j] = i + 1; } } return List::create(Named("totalProfitOrLoss") = solutionRevenue, Named("agentCost") = agentCost, Named("assignment") = assignment, Named("nodes") = Nnode, Named("bkpSolved") = Nkp); }
// [[Rcpp::export]] IntegerVector test5(IntegerVector x) { IntegerVector y(x.begin(), x.end() - 1); return y; }
// [[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); }
void transformCppIndexes(IntegerVector& indexes) { if (!Rf_isNull(indexes) && indexes.size() > 0) { std::transform(indexes.begin(), indexes.end(), indexes.begin(), std::bind2nd(std::plus<int>(), 1)); } }
// [[Rcpp::export]] Rcpp::IntegerVector uniqueBatch(Rcpp::IntegerVector x) { IntegerVector tmp = unique(x) ; IntegerVector b = clone(tmp) ; std::sort(b.begin(), b.end()) ; return b ; }
// [[Rcpp::export]] List integer_erase_range( IntegerVector x, IntegerVector y ){ x.erase(x.begin()+5, x.end()-1 ); y.erase(y.begin()+5, y.end()-1 ); return List::create( x, y ) ; }
NumericalSample NumericalSample__getMarginal( NumericalSample* sample, IntegerVector indices ){ Indices ind( indices.begin(), indices.end() ) ; return sample->getMarginal( ind ); }
// [[Rcpp::export]] std::tr1::unordered_set<int> unique4(IntegerVector x) { return std::tr1::unordered_set<int>(x.begin(), x.end()); }
// [[Rcpp::export]] std::tr1::unordered_set<int> unique3(IntegerVector x) { std::tr1::unordered_set<int> seen(x.begin(), x.end()); return seen; }