当前位置: 首页>>代码示例>>C++>>正文


C++ NumericVector::length方法代码示例

本文整理汇总了C++中NumericVector::length方法的典型用法代码示例。如果您正苦于以下问题:C++ NumericVector::length方法的具体用法?C++ NumericVector::length怎么用?C++ NumericVector::length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NumericVector的用法示例。


在下文中一共展示了NumericVector::length方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: cpp_rbbinom

// [[Rcpp::export]]
NumericVector cpp_rbbinom(
    const int& n,
    const NumericVector& size,
    const NumericVector& alpha,
    const NumericVector& beta
  ) {
  
  if (std::min({size.length(), alpha.length(), beta.length()}) < 1) {
    Rcpp::warning("NAs produced");
    return NumericVector(n, NA_REAL);
  }

  NumericVector x(n);
  
  bool throw_warning = false;

  for (int i = 0; i < n; i++)
    x[i] = rng_bbinom(GETV(size, i), GETV(alpha, i), GETV(beta, i),
                      throw_warning);
  
  if (throw_warning)
    Rcpp::warning("NAs produced");

  return x;
}
开发者ID:cran,项目名称:extraDistr,代码行数:26,代码来源:beta-binomial-distribution.cpp

示例2: ptsThresh

// [[Rcpp::export]]
NumericVector ptsThresh(NumericVector values,NumericVector endDates,int window){
  int valuesLen = values.length();
  NumericVector out = clone(values);
  NumericVector startDates = endDates - window;

  for (int i = 0; i < valuesLen; ++i){
    LogicalVector idx = (endDates <= endDates[i]) & (endDates >= startDates[i]);
    NumericVector valuesWindow = values[idx];
    int lenCur = valuesWindow.length();

    if (lenCur == 1){
      out[i] = (1.4 / 1.3) * valuesWindow[i];
    }
    if (lenCur == 2){
      out[i] = (1.5 / 2.4) * sum(valuesWindow);
    }
    if (lenCur == 3){
      out[i] = (1.5 / 3.3) * sum(valuesWindow);
    }
    if (lenCur == 4){
      out[i] = (1.5 / 4.0) * sum(valuesWindow);
    }
    if (lenCur >= 5){
      std::nth_element(valuesWindow.begin(),valuesWindow.begin() + 5,valuesWindow.end());
      out[i] = valuesWindow[4];
    }
  }

  return out;
}
开发者ID:joranE,项目名称:statskier2,代码行数:31,代码来源:cummean_bestn.cpp

示例3: cpp_qhcauchy

// [[Rcpp::export]]
NumericVector cpp_qhcauchy(
    const NumericVector& p,
    const NumericVector& sigma,
    const bool& lower_tail = true,
    const bool& log_prob = false
  ) {
  
  if (std::min({p.length(), sigma.length()}) < 1) {
    return NumericVector(0);
  }
  
  int Nmax = std::max({
    p.length(),
    sigma.length()
  });
  NumericVector q(Nmax);
  NumericVector pp = Rcpp::clone(p);
  
  bool throw_warning = false;
  
  if (log_prob)
    pp = Rcpp::exp(pp);
  
  if (!lower_tail)
    pp = 1.0 - pp;
  
  for (int i = 0; i < Nmax; i++)
    q[i] = invcdf_hcauchy(GETV(pp, i), GETV(sigma, i),
                          throw_warning);
  
  if (throw_warning)
    Rcpp::warning("NaNs produced");
  
  return q;
}
开发者ID:cran,项目名称:extraDistr,代码行数:36,代码来源:half-cauchy-distribution.cpp

示例4: cpp_phcauchy

// [[Rcpp::export]]
NumericVector cpp_phcauchy(
    const NumericVector& x,
    const NumericVector& sigma,
    bool lower_tail = true, bool log_prob = false
  ) {
  
  if (std::min({x.length(), sigma.length()}) < 1) {
    return NumericVector(0);
  }
  
  int Nmax = std::max({
    x.length(),
    sigma.length()
  });
  NumericVector p(Nmax);
  
  bool throw_warning = false;
  
  for (int i = 0; i < Nmax; i++)
    p[i] = cdf_hcauchy(GETV(x, i), GETV(sigma, i),
                       throw_warning);
  
  if (!lower_tail)
    p = 1.0 - p;
  
  if (log_prob)
    p = Rcpp::log(p);
  
  if (throw_warning)
    Rcpp::warning("NaNs produced");
  
  return p;
}
开发者ID:cran,项目名称:extraDistr,代码行数:34,代码来源:half-cauchy-distribution.cpp

示例5: cpp_rlst

// [[Rcpp::export]]
NumericVector cpp_rlst(
    const int& n,
    const NumericVector& nu,
    const NumericVector& mu,
    const NumericVector& sigma
  ) {
  
  if (std::min({nu.length(), mu.length(), sigma.length()}) < 1) {
    Rcpp::warning("NAs produced");
    return NumericVector(n, NA_REAL);
  }
  
  NumericVector x(n);
  
  bool throw_warning = false;
  
  for (int i = 0; i < n; i++)
    x[i] = rng_lst(GETV(nu, i), GETV(mu, i),
                   GETV(sigma, i), throw_warning);
  
  if (throw_warning)
    Rcpp::warning("NAs produced");
  
  return x;
}
开发者ID:cran,项目名称:extraDistr,代码行数:26,代码来源:location-scale-t-distribution.cpp

示例6: cpp_dbern

// [[Rcpp::export]]
NumericVector cpp_dbern(
    const NumericVector& x,
    const NumericVector& prob,
    const bool& log_prob = false
  ) {
  
  if (std::min({x.length(), prob.length()}) < 1) {
    return NumericVector(0);
  }
  
  int Nmax = std::max({
    x.length(),
    prob.length()
  });
  NumericVector p(Nmax);
  
  bool throw_warning = false;
  
  for (int i = 0; i < Nmax; i++)
    p[i] = pdf_bernoulli(GETV(x, i), GETV(prob, i),
                         throw_warning);
  
  if (log_prob)
    p = Rcpp::log(p);
  
  if (throw_warning)
    Rcpp::warning("NaNs produced");
  
  return p;
}
开发者ID:cran,项目名称:extraDistr,代码行数:31,代码来源:bernoulli-distribution.cpp

示例7: cpp_dhcauchy

// [[Rcpp::export]]
NumericVector cpp_dhcauchy(
    const NumericVector& x,
    const NumericVector& sigma,
    const bool& log_prob = false
  ) {
  
  if (std::min({x.length(), sigma.length()}) < 1) {
    return NumericVector(0);
  }
  
  int Nmax = std::max({
    x.length(),
    sigma.length()
  });
  NumericVector p(Nmax);
  
  bool throw_warning = false;
  
  for (int i = 0; i < Nmax; i++)
    p[i] = logpdf_hcauchy(GETV(x, i), GETV(sigma, i),
                          throw_warning);
  
  if (!log_prob)
    p = Rcpp::exp(p);
  
  if (throw_warning)
    Rcpp::warning("NaNs produced");
  
  return p;
}
开发者ID:cran,项目名称:extraDistr,代码行数:31,代码来源:half-cauchy-distribution.cpp

示例8: noSplitcv

// we process each dimension individually using this function
RcppExport SEXP noSplitcv(SEXP R_x,SEXP R_xv,SEXP R_ngroup, SEXP R_xtest,SEXP R_ngrouptest ,SEXP R_args){

	NumericVector x(R_x);
	NumericVector xv(R_xv);
	NumericVector xtest(R_xtest);
	NumericVector ngroup(R_ngroup);
	NumericVector ngrouptest(R_ngrouptest);
	List args(R_args);
  
	std::string weights = Rcpp::as<std::string>(args["weights"]); 
	double gamma = Rcpp::as<double>(args["gamma"]); 
	double epsilon = Rcpp::as<double>(args["epsilon"]);
	NumericMatrix W = args["W"];
	NumericVector lambdalist = args["lambdalist"];

	NumericVector error(lambdalist.length());

	vector<double> sl = calculateSlope(x,ngroup,xv,weights,gamma,W,x.length());
 
	Group *G = maketree(&x[0], x.length(), &sl[0],&ngroup[0],epsilon);

	error_cv(G,&lambdalist[0],lambdalist.length(),&xtest[0], &ngrouptest[0],&error[0]);

	delete_tree(G); 

	return(error);
}
开发者ID:rforge,项目名称:fusedanova,代码行数:28,代码来源:interfaceNS.cpp

示例9: cpp_rprop

// [[Rcpp::export]]
NumericVector cpp_rprop(
    const int& n,
    const NumericVector& size,
    const NumericVector& mean,
    const NumericVector& prior
  ) {
  
  if (std::min({size.length(), mean.length(), prior.length()}) < 1) {
    Rcpp::warning("NAs produced");
    return NumericVector(n, NA_REAL);
  }
  
  NumericVector x(n);
  
  bool throw_warning = false;
  
  for (int i = 0; i < n; i++)
    x[i] = rng_prop(GETV(size, i), GETV(mean, i),
                    GETV(prior, i), throw_warning);
  
  if (throw_warning)
    Rcpp::warning("NAs produced");
  
  return x;
}
开发者ID:cran,项目名称:extraDistr,代码行数:26,代码来源:proportion-distribution.cpp

示例10: rbindCpp

NumericMatrix rbindCpp(NumericVector a, NumericVector b){
  if( a.length() != b.length() )
    stop("rbind failed due to mismatch in length");

  NumericMatrix out(2, a.length());
  out(0,_) = a;
  out(1,_) = b;
  return(out);
}
开发者ID:aleksandar-spasojevic,项目名称:bootTimeInference,代码行数:9,代码来源:boot_time_inference.cpp

示例11: setSliceWidths

void samplingControl::setSliceWidths(NumericVector inWidths)
{
    if (inWidths.length() != 11)
    {
        ::Rf_error("Slice widths must have length 11.\n"); 
    }
    int i;
    for (i = 0; i < inWidths.length(); i++)
    {
        sliceWidths[i] = inWidths[i];
    }
}
开发者ID:grantbrown,项目名称:libspatialSEIR,代码行数:12,代码来源:samplingControl.cpp

示例12: setOffset

void exposureModel::setOffset(NumericVector offsets)
{
    if (offsets.length() != (nTpt))
    {
        Rcpp::Rcout << "Error: offsets must have length equal to the number of time points.\n";
        Rcpp::stop("Invalid offsets.");
    }
    int i;
    for (i = 0; i < offsets.length(); i++)
    {
        offset(i) = offsets(i);
    }
}
开发者ID:grantbrown,项目名称:ABSEIR,代码行数:13,代码来源:exposureModel.cpp

示例13: rmvnorm

  Rcpp::NumericVector rmvnorm(NumericVector mu, NumericMatrix eig_sigma) {
    NumericVector Z = no_init(mu.length());
    for (int i = 0; i < Z.length(); i++)
      Z[i] = R::norm_rand();

    NumericVector X = mu;
    for (int i = 0; i < X.length(); i++) {
      for (int j = 0; j < X.length(); j++) {
        X[i] += eig_sigma(i,j) * Z[j];
      }
    }

    return X;
  }
开发者ID:jmarshallnz,项目名称:epiclustR,代码行数:14,代码来源:rmvnorm.cpp

示例14: gv_writefits_img

//' FITS image writer
//' 
//' Writes a vector, matrix or 3D array to a FITS file as an image.
//' The data is written to the primary HDU.
//' 
// [[Rcpp::export]]
int gv_writefits_img(NumericVector img, CharacterVector fits_name, CharacterVector hdu_name = "")
{
  IntegerVector dim;
  if (!img.hasAttribute("dim"))
  {
    REprintf("ERROR: image has not been dimensioned.\n");
    return 1;
  }
  dim = img.attr("dim");
  if (dim.length() > 3)
	{
		REprintf("ERROR: dimension of more than 3 unsupported.\n");
		return 1;
	}

  fitsfile *pfits=NULL;
	int err=0;

	std::string fname = as<std::string>(fits_name[0]);
	fits_create_file(&pfits, (char *) fname.c_str(), &err);
	if (err)
	{
		gv_print_fits_err(err);
		return err;
	}

#ifdef GV_DEBUG
  Rcout << "Number of dim: " << dim.length() << std::endl;
	for (int i=0; i<dim.length(); i++)
	{
		Rcout << "Dim[" << i << "]: " << dim[i] << std::endl;
	}
	Rcout << "Number of elements: " << img.length() << std::endl;
  double *p = &(*img.begin());
  for (int i=0; i<img.length(); i++)
  {
    Rcout << "*(p+" << i << ") = " << *(p+i) << std::endl;
  }
#endif
  
  long longdim[3], startpix[3] = {1,1,1}; // default start
  for (int i=0; i<dim.length(); i++) longdim[i] = (long) dim[i];
  
	// start writing to file
	fits_create_img(pfits, DOUBLE_IMG, dim.length(), longdim, &err);
	fits_write_pix(pfits, TDOUBLE, startpix, img.length(), &(*img.begin()), &err);

	fits_close_file(pfits, &err);
  return err;
}
开发者ID:yitping,项目名称:gravityr,代码行数:56,代码来源:gv_writefits_img.cpp

示例15: find_discords_brute_force

//' Finds a discord using brute force algorithm.
//'
//' @param ts the input timeseries.
//' @param w_size the sliding window size.
//' @param discords_num the number of discords to report.
//' @useDynLib jmotif
//' @export
//' @references Keogh, E., Lin, J., Fu, A.,
//' HOT SAX: Efficiently finding the most unusual time series subsequence.
//' Proceeding ICDM '05 Proceedings of the Fifth IEEE International Conference on Data Mining
//' @examples
//' discords = find_discords_brute_force(ecg0606[1:600], 100, 1)
//' plot(ecg0606[1:600], type = "l", col = "cornflowerblue", main = "ECG 0606")
//' lines(x=c(discords[1,2]:(discords[1,2]+100)),
//'    y=ecg0606[discords[1,2]:(discords[1,2]+100)], col="red")
// [[Rcpp::export]]
Rcpp::DataFrame find_discords_brute_force(
    NumericVector ts, int w_size, int discords_num) {

  std::map<int, double> res;

  VisitRegistry registry(ts.length());
  registry.markVisited(ts.length() - w_size, ts.length());

  // Rcout << "starting search of " << discords_num << " discords..." << "\n";

  int discord_counter = 0;
  while(discord_counter < discords_num){

    discord_record rec = find_best_discord_brute_force(ts, w_size, &registry);

    //     Rcout << "found a discord " << discord_counter << " at " << rec.index;
    //     Rcout << ", NN distance: " << rec.nn_distance << "\n";

    if(rec.nn_distance == 0 || rec.index == -1){ break; }

    res.insert(std::make_pair(rec.index, rec.nn_distance));

    int start = rec.index - w_size;
    if(start<0){
      start = 0;
    }
    int end = rec.index + w_size;
    if(end>=ts.length()){
      end = ts.length();
    }

    // Rcout << "marking as visited from " << start << " to " << end << "\n";
    registry.markVisited(start, end);
    discord_counter = discord_counter + 1;
  }

  std::vector<int> positions;
  std::vector<double > distances;

  for(std::map<int, double>::iterator it = res.begin(); it != res.end(); it++) {
    positions.push_back(it->first);
    distances.push_back(it->second);
  }
  // make results
  return Rcpp::DataFrame::create(
    Named("nn_distance") = distances,
    Named("position") = positions
  );
}
开发者ID:Maarten1975,项目名称:jmotif-R,代码行数:65,代码来源:discord.cpp


注:本文中的NumericVector::length方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。