示例#1
0
文件: pascal.cpp 项目: qbolec/c
long nk(long n,long k)
{
    if( k>n )return 0;
    if( n==0)return 1;
    if( k==0)return 1;
    return nk( n-1,k) + nk(n-1,k-1);
}
示例#2
0
文件: pascal.cpp 项目: qbolec/c
int main()
{
    long n;
    long k;
    while(1)
    {
        cin >> n >> k;
        cout << nk(n,k);
    }
    return 0;
}
示例#3
0
    T binomial_coefficient(T n, T k) {
        T nk(n-k);

        if (k <= nk){
            std::swap(k,nk);
        }

        auto numerator = factors_from_down_to(n,k+T(1));
        auto denominator = factorial(nk);

        return numerator / denominator;
    }
示例#4
0
std::string SwordFuncs::lookup(std::string ref) {
  std::ostringstream output;

  // Set up module specific variables
  sword::VerseKey vk;

  // Variables related to splitting up the reference for iteration
  sword::ListKey refRange = vk.parseVerseList(ref.c_str(), vk, true);
  refRange.setPersist(true);
  module->setKey(refRange);

  try {
    int i = 0;
    for ((*module) = sword::TOP; !module->popError(); (*module)++) {
      i++;
      sword::VerseKey nk(module->getKey());
      sword::SWBuf buffer = module->renderText();
      std::string text = buffer.c_str();
      if (versenum) {
        output << " " << nk.getVerse();
      }
      output << " " << trim(text);
    }
    if (i > 1)
      output << std::endl << module->getKey()->getRangeText();
    vkey = module->getKey();
  } catch (const std::runtime_error &re) {
    // speciffic handling for runtime_error
    std::cerr << "Runtime error: " << re.what() << std::endl;
  } catch (const std::exception &ex) {
    // speciffic handling for all exceptions extending std::exception, except
    // std::runtime_error which is handled explicitly
    std::cerr << "Error occurred: " << ex.what() << std::endl;
  } catch (...) {
    // catch any other errors (that we have no information about)
    std::cerr << "Unknown failure occured. Possible memory corruption"
              << std::endl;
  }
  return output.str();
}
示例#5
0
gaussian_model EMclustering::maximization(MatrixXf x, MatrixXf r)
{
	int d = x.rows();
	int n = x.cols();
	int k = r.cols();
	//cerr<<x<<endl;
	VectorXf nk(r.rows());
	nk = r.colwise().sum();
	VectorXf w(nk.size());
	w = nk/n;
	MatrixXf tmp1(x.rows(),r.cols());
	tmp1 = x * r;
	VectorXf tmp2(nk.size());
	tmp2 = nk.array().inverse();
	//cerr<<tmp2<<endl<<endl;
	MatrixXf mu(x.rows(),r.cols());
	mu = tmp1 * tmp2.asDiagonal() ;
	MatrixXf *sigma = new MatrixXf[k];
	for(int i=0;i<k;i++)
		sigma[i].setZero(d,d);
	MatrixXf sqrtr(r.rows(),r.cols());
	sqrtr = r.cwiseSqrt();
	MatrixXf xo(d,n);
	MatrixXf tmp3(d,d);
	tmp3.setIdentity(d,d);
	
	for(int i=0;i<k;i++)
	{
		xo = x.colwise() - mu.col(i);
		VectorXf tmp4(sqrtr.rows());
		tmp4 = sqrtr.col(i);
		tmp4 = tmp4.adjoint();
		xo = xo* tmp4.asDiagonal();
		sigma[i] = xo*xo.adjoint()/nk(i);
		sigma[i] = sigma[i] + tmp3*1e-6; 
		//cerr<<sigma[i]<<endl<<endl;
	}

	gaussian_model model;
	model.mu = mu;
	model.sigma = new MatrixXf[k];
	for(int i=0;i<k;i++)
		model.sigma[i] = sigma[i];
	
	model.weight = w;
	
	nk.resize(0);
	w.resize(0);
	tmp1.resize(0,0);
	tmp2.resize(0);
	tmp3.resize(0,0);
	mu.resize(0,0);
	for(int i=0;i<k;i++)
		sigma[i].resize(0,0);
	delete [] sigma;
	sqrtr.resize(0,0);
	xo.resize(0,0);
	tmp3.resize(0,0);

	//cerr<<"---"<<endl;
	model.weight = model.weight.adjoint();
	//cerr<<model.weight<<endl<<endl;
	//cerr<<model.mu<<endl<<endl;
	//for(int i=0;i<k;i++)
	//{
	//	cerr<<model.sigma[i]<<endl<<endl;
	//}
	return model;
}
示例#6
0
SEXP multitask(SEXP X0, SEXP y0, SEXP nk0, SEXP groups0, SEXP lambda0, SEXP corrfactor0, SEXP model0, SEXP conveps0, SEXP eps0, SEXP maxiter0,SEXP maxitersg0)
{
  Rcpp::NumericVector lasso_result;
  
  //convert parameters to Rcpp types
  Rcpp::NumericMatrix X(X0);
  Rcpp::NumericVector y(y0);
  Rcpp::IntegerVector nk(nk0);
  int K = nk.size();
  Rcpp::IntegerMatrix groups(groups0);
  Rcpp::NumericVector corrfactor(corrfactor0);
  double lambda = Rcpp::as<double>(lambda0);
  int model = Rcpp::as<int>(model0);
  double eps = Rcpp::as<double>(eps0);
  double conveps = Rcpp::as<double>(conveps0);
  int maxiter = Rcpp::as<int>(maxiter0);
  int maxitersg = Rcpp::as<int>(maxitersg0);
 
  assert(K > 0);
  assert(lambda >= 0.0);
  assert(model >= 0);
  assert(model < MULTITASK_MODEL_COUNT);
  assert(eps > 0.0);
  assert(maxiter > 0);
  assert(maxitersg > 0);
 
  int p = groups.nrow();
  int L = groups.ncol();
  
  //initialize start values  
  Rcpp::NumericMatrix alpha_cur(p, K);
  for (int i = 0; i < p * K; i++) {
    alpha_cur[i] = random(-0.1, 0.1);
  }
  
  Rcpp::NumericMatrix beta_cur = alpha_cur;
  
  Rcpp::NumericVector d_cur(L);
  std::fill(d_cur.begin(), d_cur.end(), 1.0);
  
  Rcpp::NumericMatrix eta_cur(L, K);
  std::fill(eta_cur.begin(), eta_cur.end(), 1.0);
	
  Rcpp::IntegerVector nz_cur(p*K + L*K + L);
  std::fill(nz_cur.begin(), nz_cur.end(), 1);

  Rcpp::NumericMatrix beta_new;
  
  bool converged = false;
  int iterations = 0;
    
  do {
    ++iterations;
    if (iterations >= maxiter && maxiter > 0) {
      break;
    }
    //update alpha
    Rcpp::NumericMatrix alpha_new(p, K);
    Rcpp::NumericMatrix Xtilde = x_tilde(X, nk, groups, d_cur, eta_cur);
    int idx = 0; int n; 
    for(int k=0; k < K; k++){
      n = nk[k];
      lasso_result = lasso(Xtilde(Rcpp::Range(idx,idx+n-1), Rcpp::Range(p*k, p*(k+1)-1)), y[Rcpp::Range(idx,idx+n-1)], lambda*corrfactor[k], model, false, eps, maxitersg);
      assert(lasso_result.size() == p);
      for (int j = 0; j < p; j++) {
	alpha_new(j,k) = lasso_result[j];
      }
      idx += n; 
    }
  			
    //update d
    Rcpp::NumericMatrix Xtilde2 = x_tilde_2(X, nk, groups, alpha_new, eta_cur);
    lasso_result = lasso(Xtilde2, y, 1.0, model, true, eps, maxitersg);
    assert(lasso_result.size() == L);
    Rcpp::NumericVector d_new(L);
    for (int i = 0; i < L; i++) {
      d_new[i] = lasso_result[i]/max(lasso_result);
      if(std::isnan(d_new[i])) d_new[i] = 0;
   }
 		
    //update eta
    Rcpp::NumericMatrix eta_new(L, K);
    Rcpp::NumericMatrix Xtilde3 = x_tilde_3(X, nk, groups, alpha_new, d_new);
    idx = 0;
    for(int k=0; k < K; k++){
      n = nk[k];
      lasso_result = lasso(Xtilde3(Rcpp::Range(idx,idx+n-1),Rcpp::Range(L*k, L*(k+1)-1)), y[Rcpp::Range(idx,idx+n-1)], 1.0, model, true, eps, maxitersg);
      assert(lasso_result.size() == L);
      for (int l = 0; l < L; l++) {
	eta_new(l,k) = lasso_result[l];
      }
      idx += n; 
    }
  		
    //update beta
    beta_new = next_beta(nk, groups, alpha_new, d_new, eta_new);
    assert(beta_new.nrow() == p);
    assert(beta_new.ncol() == K);
    
    //check structure convergence
    Rcpp::IntegerVector nz_new = nz_vec(alpha_new,eta_new,d_new,eps);
    
    int nz_diff = 0;
    for (int i = 0; i < nz_new.length(); i++) {
      nz_diff += nz_cur[i] - nz_new[i];
    }

    double max_diff = 0.0;
    for (int i = 0; i < p * K; i++) {
      double diff = fabs(beta_new[i] - beta_cur[i]);
      if (diff > max_diff) {
	max_diff = diff;
      }
    }
    if (max_diff < conveps && nz_diff==0) {
      converged = true;
    }
    
    alpha_cur = alpha_new;
    d_cur = d_new;
    eta_cur = eta_new;
    beta_cur = beta_new;
    nz_cur = nz_new;
  } while (converged != true);
  
  Rcpp::NumericMatrix beta_refit = refit_model(X, y,beta_new, nk, model, eps, maxitersg);

  Rcpp::List result;
  result["beta"] = beta_refit;
  result["d"] = nz(d_cur,eps);
  result["eta"] = nz(eta_cur,eps);
  switch (model) {
  case MULTITASK_LINEAR:
    result["bic"] = bic_linear(X, y, beta_refit, eps, nk);
    break;
  case MULTITASK_LOGISTIC:
    result["bic"] = bic_logistic(X, y, beta_refit, eps, nk);
    break;
  default:
    assert(0);
  }
  result["converged"] = converged;
  
  return result;
}