Example #1
0
    // Specify the residual. This is where the ODE system and boundary
    // conditions are specified. The solver will attempt to find a solution
    // x so that this function returns 0 for all n and j.
    virtual doublereal residual(doublereal* x, size_t n, size_t j) {

        // if n = 0, return the residual for the first ODE
        if (n == 0) {
            if (isLeft(j)) { // here we specify zeta(0) = 0
                return zeta(x,j);
            } else
                // this implements d(zeta)/dz = u
            {
                return (zeta(x,j) - zeta(x,j-1))/(z(j)-z(j-1)) - u(x,j);
            }
        }
        // if n = 1, then return the residual for the second ODE
        else {
            if (isLeft(j)) { // here we specify u(0) = 0
                return u(x,j);
            } else if (isRight(j)) { // and here we specify u(L) = 1
                return u(x,j) - 1.0;
            } else
                // this implements the 2nd ODE
            {
                return cdif2(x,1,j) + 0.5*zeta(x,j)*centralFirstDeriv(x,1,j);
            }
        }
    }
Example #2
0
void YCSBQueryGenerator::init() {
	mrand = (myrand *) mem_allocator.alloc(sizeof(myrand));
	mrand->init(get_sys_clock());
  if (SKEW_METHOD == ZIPF) {
    zeta_2_theta = zeta(2, g_zipf_theta);
    uint64_t table_size = g_synth_table_size / g_part_cnt;
    the_n = table_size - 1;
    denom = zeta(the_n, g_zipf_theta);
  }
}
Example #3
0
static ex zeta1_evalf(const ex& x, PyObject* parent)
{
        /*
	if (is_exactly_a<lst>(x) && (x.nops()>1)) {

		// multiple zeta value
		const int count = x.nops();
		const lst& xlst = ex_to<lst>(x);
		std::vector<int> r(count);

		// check parameters and convert them
		auto it1 = xlst.begin();
		auto it2 = r.begin();
		do {
			if (!(*it1).info(info_flags::posint)) {
				return zeta(x).hold();
			}
			*it2 = ex_to<numeric>(*it1).to_int();
			it1++;
			it2++;
		} while (it2 != r.end());

		// check for divergence
		if (r[0] == 1) {
			return zeta(x).hold();
		}

		// decide on summation algorithm
		// this is still a bit clumsy
		int limit = 10;
		if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
			return numeric(zeta_do_sum_Crandall(r));
		} else {
			return numeric(zeta_do_sum_simple(r));
		}
	}*/

	// single zeta value
	if (x == 1) {
		return UnsignedInfinity;
	} else	if (is_exactly_a<numeric>(x)) {
		try {
			return zeta(ex_to<numeric>(x.evalf(0, parent)));
		} catch (const dunno &e) { }
	}

	return zeta(x).hold();
}
Example #4
0
void CurrentElem<real_num_mov>::PrintOrientation() const {
  
      const uint mesh_dim = _mesh.get_dim();
      const uint el_nnodes   = _el_conn.size();

       std::vector<double>   xi(mesh_dim,0.);
       std::vector<double>  eta(mesh_dim,0.);
       std::vector<double> zeta(mesh_dim,0.);  //TODO this should only be instantiated  in the 3D case, in any case we avoid USING IT 

      for (uint idim=0; idim< mesh_dim; idim++) {
                              xi[idim] = _xx_nds[1+idim*el_nnodes]-_xx_nds[0+idim*el_nnodes]; //1-0 xi axis
                             eta[idim] = _xx_nds[3+idim*el_nnodes]-_xx_nds[0+idim*el_nnodes]; //3-0 eta axis
        if ( mesh_dim == 3 )   zeta[idim] = _xx_nds[4+idim*el_nnodes]-_xx_nds[0+idim*el_nnodes]; //4-0 zeta axis

     }

     std::cout << "asse xi   ";
       for (uint idim=0; idim< mesh_dim; idim++)  std::cout << xi[idim] << " ";
      std::cout <<std::endl;
     std::cout << "asse eta  ";
       for (uint idim=0; idim< mesh_dim; idim++)  std::cout << eta[idim] << " ";
      std::cout <<std::endl;
      if ( mesh_dim == 3 )  {
     std::cout << "asse zeta ";
       for (uint idim=0; idim< mesh_dim; idim++)  std::cout << zeta[idim] << " ";
      std::cout <<std::endl;
      }

      
      
      return; 
    }
Partition ClusteringProjector::projectCoarseGraphToFinestClustering(const Graph& Gcoarse, const Graph& Gfinest, const std::vector<std::vector<node> >& maps) {

	Partition zeta(Gfinest.upperNodeIdBound());
	zeta.setUpperBound(Gcoarse.upperNodeIdBound());

	// store temporarily coarsest supernode here
	std::vector<node> super(Gfinest.upperNodeIdBound());
	// initialize to identity
	Gfinest.parallelForNodes([&](node v){
		super[v] = v;
	});


	// find coarsest supernode for each node
	for (auto iter = maps.begin(); iter != maps.end(); ++iter) {
		Gfinest.parallelForNodes([&](node v){
			super[v] = (* iter)[super[v]];
		});
	}

	// assign super node id as cluster id
	Gfinest.parallelForNodes([&](node v) {
		//zeta.addToSubset(super[v],v);
		zeta[v] = super[v];
	});

	DEBUG("number of clusters in projected clustering: " , zeta.numberOfSubsets());
	DEBUG("number of nodes in coarse graph: " , Gcoarse.numberOfNodes());
	assert (zeta.numberOfSubsets() == Gcoarse.numberOfNodes());

	return zeta;

}
TEST_F(OverlapGTest, testHashingOverlapperForCorrectness) {
	count n = 4;
	Graph G(n);

	Partition zeta(n);
	Partition eta(n);

	zeta.setUpperBound(2);
	zeta[0] = 0;
	zeta[1] = 0;
	zeta[2] = 1;
	zeta[3] = 1;

	eta.setUpperBound(2);
	eta[0] = 0;
	eta[1] = 1;
	eta[2] = 0;
	eta[3] = 1;

	std::vector<Partition> clusterings = {zeta, eta};
	HashingOverlapper overlapper;
	Partition overlap = overlapper.run(G, clusterings);

	INFO("overlap clustering number of clusters: ", overlap.numberOfSubsets());
	INFO("overlap clustering: ", overlap.getVector());
}
Example #7
0
void 
ycsb_query::calculateDenom()
{
	assert(the_n == 0);
	uint64_t table_size = g_synth_table_size / g_virtual_part_cnt;
	the_n = table_size - 1;
	denom = zeta(the_n, g_zipf_theta);
}
Example #8
0
void ycsb_query::init(uint64_t thd_id, workload * h_wl) {
//	mrand = (myrand *) mem_allocator.alloc(sizeof(myrand), thd_id);
//	mrand->init(thd_id);
//	cout << g_req_per_query << endl;
	requests = (ycsb_request *) 
		mem_allocator.alloc(sizeof(ycsb_request) * g_req_per_query, thd_id);
	part_to_access = (uint64_t *) 
		mem_allocator.alloc(sizeof(uint64_t) * g_part_per_txn, thd_id);
	zeta_2_theta = zeta(2, g_zipf_theta);
	if (the_n == 0) {
		//uint64_t table_size = g_synth_table_size / g_part_cnt;
		uint64_t table_size = g_synth_table_size / g_virtual_part_cnt;
		the_n = table_size - 1;
		denom = zeta(the_n, g_zipf_theta);
	}
	gen_requests(thd_id, h_wl);
}
Example #9
0
static ex zeta2_eval(const ex& m, const ex& s_)
{
	if (is_exactly_a<lst>(s_)) {
		const lst& s = ex_to<lst>(s_);
		for (const auto & elem : s) {
			if ((elem).info(info_flags::positive)) {
				continue;
			}
			return zeta(m, s_).hold();
		}
		return zeta(m);
	} else if (s_.info(info_flags::positive)) {
		return zeta(m);
	}

	return zeta(m, s_).hold();
}
Example #10
0
static ex zetaderiv_eval(const ex & n, const ex & x)
{
	if (n.info(info_flags::numeric)) {
		// zetaderiv(0,x) -> zeta(x)
		if (n.is_zero())
			return zeta(x).hold();
	}
	
	return zetaderiv(n, x).hold();
}
Example #11
0
void ycsb_query::init(uint64_t thd_id, workload * h_wl, Query_thd * query_thd) {
	_query_thd = query_thd;
	requests = (ycsb_request *) 
		mem_allocator.alloc(sizeof(ycsb_request) * g_req_per_query, thd_id);
	part_to_access = (uint64_t *) 
		mem_allocator.alloc(sizeof(uint64_t) * g_part_per_txn, thd_id);
	zeta_2_theta = zeta(2, g_zipf_theta);
	assert(the_n != 0);
	assert(denom != 0);
	gen_requests(thd_id, h_wl);
}
Example #12
0
static ex zeta1_eval(const ex& m)
{
	if (is_exactly_a<lst>(m)) {
		if (m.nops() == 1) {
			return zeta(m.op(0));
		}
		return zeta(m).hold();
	}

	if (m.info(info_flags::numeric)) {
		const numeric& y = ex_to<numeric>(m);
		// trap integer arguments:
		if (y.is_integer()) {
			if (y.is_zero()) {
				return _ex_1_2;
			}
			if (y.is_equal(*_num1_p)) {
				//return zeta(m).hold();
				return UnsignedInfinity;
			}
			if (y.info(info_flags::posint)) {
				if (y.info(info_flags::odd)) {
					return zeta(m).hold();
				} else {
					return abs(bernoulli(y)) * pow(Pi, y) * pow(*_num2_p, y-(*_num1_p)) / factorial(y);
				}
			} else {
				if (y.info(info_flags::odd)) {
					return -bernoulli((*_num1_p)-y) / ((*_num1_p)-y);
				} else {
					return _ex0;
				}
			}
		}
		// zeta(float)
		if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
			return zeta(y); // y is numeric
		}
	}
	return zeta(m).hold();
}
Example #13
0
  struct GenInfo *
generator_new_zipfian(const uint64_t min, const uint64_t max)
{
  struct GenInfo * const gi = (typeof(gi))malloc(sizeof(*gi));
  struct GenInfo_Zipfian * const gz = &(gi->gen.zipfian);

  const uint64_t items = max - min + 1;
  gz->nr_items = items;
  gz->base = min;
  gz->zipfian_constant = ZIPFIAN_CONSTANT;
  gz->theta = ZIPFIAN_CONSTANT;
  gz->zeta2theta = zeta(2, ZIPFIAN_CONSTANT);
  gz->alpha = 1.0 / (1.0 - ZIPFIAN_CONSTANT);
  double zetan = zeta(items, ZIPFIAN_CONSTANT);
  gz->zetan = zetan;
  gz->eta = (1.0 - pow(2.0 / (double)items, 1.0 - ZIPFIAN_CONSTANT)) / (1.0 - (gz->zeta2theta / zetan));
  gz->countforzeta = items;
  gz->min = min;
  gz->max = max;

  gi->type = GEN_ZIPFIAN;
  gi->next = gen_zipfian;
  return gi;
}
Partition ClusteringGenerator::makeRandomClustering(Graph& G, count k) {
	count n = G.upperNodeIdBound();
	Partition zeta(n);

	zeta.setUpperBound(k);

	G.parallelForNodes([&](node v) {
		index c = Aux::Random::integer(k-1);
		zeta.addToSubset(c, v);
	});

	if (zeta.numberOfSubsets() != k) {
		WARN("random clustering does not contain k=",k," cluster: ",zeta.numberOfSubsets());
	}
	assert (GraphClusteringTools::isProperClustering(G, zeta));
	return zeta;
}
Example #15
0
static ex zeta2_evalf(const ex& x, const ex& s, PyObject* parent)
{
        /*
	if (is_exactly_a<lst>(x)) {

		// alternating Euler sum
		const int count = x.nops();
		const lst& xlst = ex_to<lst>(x);
		const lst& slst = ex_to<lst>(s);
		std::vector<int> xi(count);
		std::vector<int> si(count);

		// check parameters and convert them
		auto it_xread = xlst.begin();
		auto it_sread = slst.begin();
		auto it_xwrite = xi.begin();
		auto it_swrite = si.begin();
		do {
			if (!(*it_xread).info(info_flags::posint)) {
				return zeta(x, s).hold();
			}
			*it_xwrite = ex_to<numeric>(*it_xread).to_int();
			if (*it_sread > 0) {
				*it_swrite = 1;
			} else {
				*it_swrite = -1;
			}
			it_xread++;
			it_sread++;
			it_xwrite++;
			it_swrite++;
		} while (it_xwrite != xi.end());

		// check for divergence
		if ((xi[0] == 1) && (si[0] == 1)) {
			return zeta(x, s).hold();
		}

		// use Hoelder convolution
		return numeric(zeta_do_Hoelder_convolution(xi, si));
	}*/

	return zeta(x, s).hold();
}
Example #16
0
/* Compute lgam(x + 1) around x = 0 using its Taylor series. */
double lgam1p_taylor(double x)
{
    int n;
    double xfac, coeff, res;

    if (x == 0) {
        return 0;
    }
    res = -CEPHES_EULER * x;
    xfac = -x;
    for (n = 2; n < 42; n++) {
        xfac *= -x;
        coeff = zeta(n, 1) * xfac / n;
	res += coeff;
	if (fabs(coeff) < MACHEP * fabs(res)) {
            break;
	}
    }
    
    return res;
}
Example #17
0
int main()
{
  // alpha should propagate the error from epsilon
  xapi exit = zeta();
  assert_eq_exit(TEST_ERROR_ONE, exit);

  assert_eq_d(0, alpha_dead);
  assert_eq_d(1, beta_count);
  assert_eq_d(1, delta_count);
  assert_eq_d(1, epsilon_count);

  // lambda has a subsequence rooted at frame 0
  exit = lambda();
  assert_eq_exit(TEST_ERROR_ONE, exit);

  assert_eq_d(1, fi_live);
  assert_eq_d(0, fi_dead);
  assert_eq_d(1, lambda_live_one);
  assert_eq_d(0, lambda_dead_one);
  assert_eq_d(1, lambda_live_two);
  assert_eq_d(0, lambda_dead_two);

  succeed;
}
static void prix_en_0_ls(double *res_prix, PnlMat *asset, int M, int N, int N_trading, double spot, double T, param *P, PnlBasis *basis)
{
  PnlMat *V, *res_beta, *res_no_call;
  PnlMatInt *H, *mod_H;
  PnlVectInt *res_zeta, *res_tau,*res_theta;
  PnlVect *tmp_prix;
  int j, i, zeta_j, tau_j, theta_j;
  double sprix,s;
  double h=T/N;
  //initialisation 
  V=pnl_mat_new();
  res_no_call=pnl_mat_new();
  res_beta=pnl_mat_new();
  res_zeta=pnl_vect_int_new();
  res_theta=pnl_vect_int_new();
  res_tau=pnl_vect_int_new();
  tmp_prix=pnl_vect_create(M);
  H=pnl_mat_int_new();
  mod_H=pnl_mat_int_new();

  //calcul du vecteur H
  calcul_H(H,asset,M,N,N_trading,spot,P);
  calcul_mod_H(mod_H,H,M,N,N_trading,P);
  //calcul du vecteur theta
  theta(res_theta,mod_H,M,N,N_trading,P);

  //calcul du prix_no_call protection
  prix_no_call(res_no_call,M,N,asset,spot,T,P,basis);

  //calcul du prix standard protection
  prix(V,res_no_call,M,N,asset,res_theta,spot,T,P,basis);
  //calcul de tau, zeta et beta
  tau(res_tau,M,N,V,asset,res_theta,P);
  zeta(res_zeta,res_tau,res_theta);
  beta(res_beta,spot,asset,T,N,P);
  //calcul de la somme Monte Carlo
  for(j=0;j<M;j++)
    {
      s=0;
      tau_j=pnl_vect_int_get(res_tau,j);
      theta_j=pnl_vect_int_get(res_theta,j);
      zeta_j=pnl_vect_int_get(res_zeta,j);
      if(tau_j<theta_j)
        {
          pnl_vect_set(tmp_prix,j,pnl_mat_get(res_beta,zeta_j,j)*low(pnl_mat_get(asset,tau_j,j),P));
        }
      else
        {
          pnl_vect_set(tmp_prix,j,pnl_mat_get(res_beta,zeta_j,j)*pnl_mat_get(res_no_call,theta_j,j));
        }
      
      for(i=1;i<=zeta_j;i++) s=s+h*pnl_mat_get(res_beta,i,j)*c(pnl_mat_get(asset,i,j),spot,P);
      pnl_vect_set(tmp_prix,j,pnl_vect_get(tmp_prix,j)+s);    
    }
  sprix=pnl_vect_sum(tmp_prix);
  
  pnl_mat_free(&V);
  pnl_mat_free(&res_beta);
  pnl_mat_int_free(&H);
  pnl_mat_int_free(&mod_H);
  pnl_vect_int_free(&res_zeta);
  pnl_vect_int_free(&res_tau);
  pnl_vect_int_free(&res_theta);
  pnl_vect_free(&tmp_prix);
  *res_prix=sprix/M;
}
  void PseudoBlockStochasticCGIter<ScalarType,MV,OP>::iterate()
  {
    //
    // Allocate/initialize data structures
    //
    if (initialized_ == false) {
      initialize();
    }

    // Allocate memory for scalars.
    int i=0;
    std::vector<int> index(1);
    std::vector<ScalarType> rHz( numRHS_ ), rHz_old( numRHS_ ), pAp( numRHS_ );
    Teuchos::SerialDenseMatrix<int, ScalarType> alpha( numRHS_,numRHS_ ), beta( numRHS_,numRHS_ ), zeta(numRHS_,numRHS_);

    // Create convenience variables for zero and one.
    const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
    const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
    
    // Get the current solution std::vector.
    Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();

    // Compute first <r,z> a.k.a. rHz
    MVT::MvDot( *R_, *Z_, rHz );

    if ( assertPositiveDefiniteness_ )
        for (i=0; i<numRHS_; ++i)
            TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
                                CGIterateFailure,
                                "Belos::PseudoBlockStochasticCGIter::iterate(): negative value for r^H*M*r encountered!" );

    ////////////////////////////////////////////////////////////////
    // Iterate until the status test tells us to stop.
    //
    while (stest_->checkStatus(this) != Passed) {
      
      // Increment the iteration
      iter_++;

      // Multiply the current direction std::vector by A and store in AP_
      lp_->applyOp( *P_, *AP_ );
      
      // Compute alpha := <R_,Z_> / <P_,AP_>
      MVT::MvDot( *P_, *AP_, pAp );

      for (i=0; i<numRHS_; ++i) {
        if ( assertPositiveDefiniteness_ )
            // Check that pAp[i] is a positive number!
            TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(pAp[i]) <= zero,
                                CGIterateFailure,
                                "Belos::PseudoBlockStochasticCGIter::iterate(): non-positive value for p^H*A*p encountered!" );

        alpha(i,i) = rHz[i] / pAp[i];


	// Compute the scaling parameter for the stochastic vector
	ScalarType z = normal();
	zeta(i,i) = z / Teuchos::ScalarTraits<ScalarType>::squareroot(pAp[i]);
      }

      //
      // Update the solution std::vector x := x + alpha * P_
      //
      MVT::MvTimesMatAddMv( one, *P_, alpha, one, *cur_soln_vec );
      lp_->updateSolution();

      // Updates the stochastic vector y := y + zeta * P_
      MVT::MvTimesMatAddMv( one, *P_, zeta, one, *Y_);

      //
      // Save the denominator of beta before residual is updated [ old <R_, Z_> ]
      //
      for (i=0; i<numRHS_; ++i) {
        rHz_old[i] = rHz[i];
      }
      //
      // Compute the new residual R_ := R_ - alpha * AP_
      //
      MVT::MvTimesMatAddMv( -one, *AP_, alpha, one, *R_ );
      //
      // Compute beta := [ new <R_, Z_> ] / [ old <R_, Z_> ], 
      // and the new direction std::vector p.
      //
      if ( lp_->getLeftPrec() != Teuchos::null ) {
        lp_->applyLeftPrec( *R_, *Z_ );
        if ( lp_->getRightPrec() != Teuchos::null ) {
          Teuchos::RCP<MV> tmp = MVT::Clone( *Z_, numRHS_ );
          lp_->applyRightPrec( *Z_, *tmp );
          Z_ = tmp;
        }
      }
      else if ( lp_->getRightPrec() != Teuchos::null ) {
        lp_->applyRightPrec( *R_, *Z_ );
      } 
      else {
        Z_ = R_;
      }
      //
      MVT::MvDot( *R_, *Z_, rHz );
      if ( assertPositiveDefiniteness_ )
          for (i=0; i<numRHS_; ++i)
              TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
                                  CGIterateFailure,
                                  "Belos::PseudoBlockStochasticCGIter::iterate(): negative value for r^H*M*r encountered!" );
      //
      // Update the search directions.
      for (i=0; i<numRHS_; ++i) {
        beta(i,i) = rHz[i] / rHz_old[i];
	index[0] = i;
	Teuchos::RCP<const MV> Z_i = MVT::CloneView( *Z_, index );
	Teuchos::RCP<MV> P_i = MVT::CloneViewNonConst( *P_, index );
        MVT::MvAddMv( one, *Z_i, beta(i,i), *P_i, *P_i );
      }
      //      
    } // end while (sTest_->checkStatus(this) != Passed)
  }
Example #20
0
void mlalign_TKF91(mlalign_Nucleotide *as, size_t na, mlalign_Nucleotide *bs,
                   size_t nb, double lambda, double mu, double tau, double pi[mlalign_NUMBER_OF_BASES],
                   mlalign_Site *alignment, size_t *n, double *score) {

  /***********************/
  /* initialize matrices */
  /***********************/

  size_t rows = na + 1;
  size_t cols = nb + 1;
  auto idxM = diagonalizedIndexer(rows, cols, VECSIZE);
  auto idxA = identityIndexer(na);
  auto idxB = identityIndexer(nb);
  auto idxP1 = identityIndexer(mlalign_NUMBER_OF_BASES);
  auto idxP2 = rowMajorIndexer(mlalign_NUMBER_OF_BASES, mlalign_NUMBER_OF_BASES);
  auto idx2 = identityIndexer(2);


  // Allocate enough bytes for the matrices and properly align that.
  // We won't use vector because
  //  1) it initializes every element to 0
  //  2) has some extra payload we won't need (e.g. capacity and size for bounds checks)
  //  3) we don't resize
  const size_t doubles_per_matrix = to_next_multiple(idxM(rows - 1, cols - 1) + 1, VECSIZE);
  std::unique_ptr<double[]> buffer(new double[3 * doubles_per_matrix + VECSIZE]);
  auto M0 = reinterpret_cast<double*>(
          to_next_multiple(
                  reinterpret_cast<size_t>(buffer.get()),
                  VECSIZE * sizeof(double)));
  auto M1 = M0 + doubles_per_matrix;
  auto M2 = M1 + doubles_per_matrix;
  assert(M0 - buffer.get() < static_cast<std::ptrdiff_t>(VECSIZE));

  //
  // Memoization
  //
  const double beta = (1 - std::exp((lambda - mu) * tau)) / (mu - lambda * std::exp((lambda - mu) * tau));
  const double delta = 1 / (1 - pi[0] * pi[0] - pi[1] * pi[1] - pi[2] * pi[2] - pi[3] * pi[3]);
  const double lambda_div_mu = lambda / mu;
  const double lambda_mul_beta = lambda * beta;
  double p_desc_1 = p_desc(1, beta, lambda, mu, tau);
  double M0_factor[mlalign_NUMBER_OF_BASES];
  double M1_factor[mlalign_NUMBER_OF_BASES * mlalign_NUMBER_OF_BASES];
  double M2_factor[mlalign_NUMBER_OF_BASES];
  double p_desc_inv_n[2];

  for (size_t i = 0; i < 2; ++i) {
    p_desc_inv_n[idx2(i)] = p_desc_inv(i, beta, lambda, mu, tau);
  }

  for (size_t i = 0; i < mlalign_NUMBER_OF_BASES; ++i) {
    const auto a = static_cast<mlalign_Nucleotide>(i);
    M0_factor[idxP1(a)] = std::log(lambda_div_mu * pi[idxP1(a)] * p_desc_inv_n[idx2(0)]);
  }

  for (size_t i = 0; i < mlalign_NUMBER_OF_BASES; ++i) {
    for (size_t j = 0; j < mlalign_NUMBER_OF_BASES; ++j) {
      const auto a = static_cast<mlalign_Nucleotide>(i);
      const auto b = static_cast<mlalign_Nucleotide>(j);
      M1_factor[idxP2(a, b)] = std::log(lambda_div_mu * pi[idxP1(a)]
                                 * std::max(p_trans(a, b, delta, pi, tau) * p_desc_1, pi[idxP1(b)] * p_desc_inv_n[idx2(1)]));
    }
  }

  for (size_t i = 0; i < mlalign_NUMBER_OF_BASES; ++i) {
    const auto b = static_cast<mlalign_Nucleotide>(i);
    M2_factor[idxP1(b)] = std::log(pi[idxP1(b)] * lambda_mul_beta);
  }

  size_t ix = idxM(0, 0); // We'll reuse ix throughout this function for caching idx calculations
  M0[ix] = -std::numeric_limits<double>::max();
  M1[ix] = std::log(gamma2(0, lambda, mu) * zeta(1, beta, lambda));
  M2[ix] = -std::numeric_limits<double>::max();

  double accum = 0.0;
  for (size_t i = 1; i < rows; i++) {
    ix = idxM(i, 0);
    accum += std::log(pi[idxP1(as[idxA(i-1)])] * p_desc_inv_n[idx2(0)]);

    M0[ix] = std::log(gamma2(i, lambda, mu) * zeta(i, beta, lambda)) + accum;
    M1[ix] = -std::numeric_limits<double>::max();
    M2[ix] = -std::numeric_limits<double>::max();
  }

  accum = 0.0;
  for (size_t j = 1; j < cols; j++) {
    ix = idxM(0, j);
    accum += std::log(pi[idxP1(bs[idxB(j-1)])]);

    M0[ix] = -std::numeric_limits<double>::max();
    M1[ix] = -std::numeric_limits<double>::max();
    M2[ix] = std::log(gamma2(0, lambda, mu) * zeta(j + 1, beta, lambda)) + accum;
  }

  /****************/
  /* DP algorithm */
  /****************/

  // TODO: Implement these in circular queues.
  size_t top[3] = { 0, 0, SIZE_MAX };
  size_t bottom[3] { 1, 0, SIZE_MAX };
  // This is tricky. We store the corrected offsets for diagonals offset -1 and -2 (7, 3 resp.) and the
  // uncorrected (e.g. not aligned) offset for the current diagonal (which is 7 + 2 = 9).
  // We inititialize with the values for diagonal 2, 1 and 0, we start at diagonal = 2 anyway.
  size_t diagonal_offsets[3] = { 2*VECSIZE + 1, 2*VECSIZE - 1, VECSIZE - 1 };
  const size_t number_of_diagonals = rows + cols - 1;
  for (size_t diagonal = 2; diagonal < number_of_diagonals; ++diagonal) {

    // Topmost and bottommost row index of the diagonal,
    // skipping the already initialized first row and column (diagonal - 1 for that reason).
    // It's helpful for intuition to visualize this (rows=7, cols=6):
    //
    //   3  7 11 15 19
    //   8 12 16 20 24
    //   5 17 21 25 32
    //  18 22 26 33 40
    //  23 27 34 41 44
    //  28 35 42 45 48
    //  36 43 46 49 52
    //
    // diagonal             | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
    // bottom_without_first | 1 | 2 | 3 | 4 | 5 | 6 | 6 | 6 |
    // top_without_first    | 1 | 1 | 1 | 1 | 2 | 3 | 4 | 5 |
    //
    // We will then iterate over the diagonal through the indices
    // [top_without_first, bottom_without_first] inclusive. Column
    // indices are easily computed from the diagonal and row index.
    // We start with diagonal = 2, e.g. the third diagonal, because
    // it's the first containing uninitialized values.
    //
    const size_t bottom_without_first = std::min(diagonal - 1, rows - 1);
    const size_t top_without_first = diagonal - std::min(diagonal - 1, cols - 1);

    std::rotate(top, top + 2, top + 3); // This pushes all elements on further back
    std::rotate(bottom, bottom + 2, bottom + 3);
    bottom[0] = std::min(diagonal, rows - 1);
    top[0] = diagonal - std::min(diagonal, cols - 1);
    const size_t length_of_diagonal = bottom[0] + 1 - top[0];

    // We have to correct the diagonal offset for the alignment rules layed out in diagonalizedIndexer.
    //    1. If the diagonal starts in the first row (top[0]=0), then the offset must have alignment -1.
    //    2. If the diagonal doesn't start in the first row, then the offset must have alignment 0.
    diagonal_offsets[0] += top[0] == 0
                  ? VECSIZE - 1 - diagonal_offsets[0] % VECSIZE
                  : (VECSIZE - diagonal_offsets[0] % VECSIZE) % VECSIZE;

    assert((top[0] == 0 && diagonal_offsets[0] % VECSIZE == VECSIZE - 1)
           || (top[0] != 0 && diagonal_offsets[0] % VECSIZE == 0));

    // INDEX EXPLANATIONS
    // row_offset   offset of the row where the current SIMD vector begins
    // i            index into the SIMD vector
    // col          index of the column where the currently processed vector element is located in the DP matrix
    // row          index of the row where the currently processed vector element is located in the DP matrix
    
    // Step in vector size over diagonal entries
    for (size_t row_offset = top_without_first; row_offset <= bottom_without_first; row_offset += VECSIZE) {


      // Copy the factors values from the lookuptable into the vector
      // Apparently it is not better to copy into temp memory and the use loaddvec, but it is faster to just use the [] operator.
      dvec factors[3];
      for (size_t i = 0; i < VECSIZE; ++i) {
        const size_t row = row_offset + i;
        const size_t col = diagonal - row;

        if (row > bottom_without_first) {
          // We would load from somewhere out of the matrix, so we just leave the
          // other vector elements untouched
          break;
        }

        const mlalign_Nucleotide a = as[idxA(row - 1)];
        const mlalign_Nucleotide b = bs[idxB(col - 1)];

        factors[0][i] = M0_factor[idxP1(a)];
        factors[1][i] = M1_factor[idxP2(a, b)];
        factors[2][i] = M2_factor[idxP1(b)];
      }

      // Compute the indexes of the top, left and top left cells and load them.
      const size_t ix_top = diagonal_offsets[1] + row_offset - 1 - top[1];
      assert(ix_top == idxM(row_offset - 1, diagonal - row_offset));
      const dvec max_top[3] = {
              loaddvec(M0 + ix_top),
              loaddvec(M1 + ix_top),
              loaddvec(M2 + ix_top),
      };

      const size_t ix_topleft = diagonal_offsets[2] + row_offset - 1 - top[2];
      assert(ix_topleft == idxM(row_offset - 1, diagonal - row_offset - 1));
      const dvec max_topleft[3] = {
              loaddvec(M0 + ix_topleft),
              loaddvec(M1 + ix_topleft),
              loaddvec(M2 + ix_topleft),
      };

      // Against all symmetry M0 isn't part of the formula.
      const size_t ix_left = ix_top + 1;
      assert(ix_left == idxM(row_offset, diagonal - row_offset - 1));
      const dvec max_left[2] = {
              loaddvec(M1 + ix_left),
              loaddvec(M2 + ix_left),
      };

      // Maxima for each cells on top/topleft/left of the calculated cell
      const dvec mt = maxdvec(max_top[0], maxdvec(max_top[1], max_top[2]));
      const dvec mtl = maxdvec(max_topleft[0], maxdvec(max_topleft[1], max_topleft[2]));
      const dvec ml = maxdvec(max_left[0], max_left[1]);

      // Vectorised add
      const dvec m0v = factors[0] + mt;
      const dvec m1v = factors[1] + mtl;
      const dvec m2v = factors[2] + ml;

      // Write back to matrix
      const size_t occupied_vector_elements = std::min(bottom_without_first + 1 - row_offset, VECSIZE);
      ix = diagonal_offsets[0] + row_offset - top[0];
      assert(ix % VECSIZE == 0);
      assert(ix == idxM(row_offset, diagonal - row_offset));
      storedvec(M0 + ix, m0v, occupied_vector_elements);
      storedvec(M1 + ix, m1v, occupied_vector_elements);
      storedvec(M2 + ix, m2v, occupied_vector_elements);
    }

    std::rotate(diagonal_offsets, diagonal_offsets + 2, diagonal_offsets + 3);
    diagonal_offsets[0] = diagonal_offsets[1] + length_of_diagonal;
  }

  /****************/
  /* Backtracking */
  /****************/

  ix = idxM(na, nb);
  *n = 0;
  *score = std::exp(std::max({M0[ix], M1[ix], M2[ix]}));
  size_t cur = max_idx({M0[ix], M1[ix], M2[ix]});

  for (size_t i = na, j = nb; i > 0 || j > 0;) {
    mlalign_Site site = {mlalign_Gap, mlalign_Gap};
    switch (cur) {
      case 0: // insert
        assert(i > 0);
        site.a = as[--i];
        site.b = mlalign_Gap;

        ix = idxM(i, j);
        cur = max_idx({M0[ix], M1[ix], M2[ix]});
        break;
      case 1: // substitution
        assert(i > 0);
        assert(j > 0);
        site.a = as[--i];
        site.b = bs[--j];

        ix = idxM(i, j);
        cur = max_idx({M0[ix], M1[ix], M2[ix]});
        break;
      case 2: // deletion
        assert(j > 0);
        site.a = mlalign_Gap;
        site.b = bs[--j];

        ix = idxM(i, j);
        cur = max_idx({M1[ix], M2[ix]}) + 1;
        break;
      default:
        assert(0);
    }

    alignment[(*n)++] = site; // Doesn't get any scarier
  }

  // The alignment is reversed, so we have to undo that.
  for (size_t i = 0; i < (*n) / 2; ++i) {
    std::swap(alignment[i], alignment[*n - i - 1]);
  }
}
Example #21
0
  	void FULLCOND_rj_mix::update(void)
    {
		  step_aborted = true;


		/*********	in principle like FULLCOND_rj::rj_step, but now 
					depending on the var_type functions of 
					FULLCOND_rj or FULLCOND_rj_int are used **********************/
		
		  while (step_aborted==true)
		  {
				// two variables are randomly chosen
				unsigned int i,j;

				i= rand() % nvar;
				j=i;

				while(i==j)
				j= rand() % nvar;


				// decide which kind of step (birth, death, switch)
				if (zeta(i,j) == 1)
				{
					 if(preg_mods[j]->tell_var_type()=='d')
					 {
						 if(conditions == false)
							FULLCOND_rj_int::death_step(i,j);
						 else
							if(conditions_okay_d(i,j) ==true)
								FULLCOND_rj_int::death_step(i,j);
							

					 }
					 else
					 {
						 if(conditions == false)
							FULLCOND_rj::death_step(i,j);
						 else
							if(conditions_okay_d(i,j) ==true)
							
								FULLCOND_rj::death_step(i,j);

					 }
				}
				else if (zeta(j,i)==1)	
				{
					if(preg_mods[j]->tell_var_type()=='d' ||
						preg_mods[i]->tell_var_type()=='d')
					{
						if(conditions == false)
							FULLCOND_rj_int::switch_step(i,j);
						
						 else
							if(conditions_okay_s(i,j) ==true)
							{
								FULLCOND_rj_int::switch_step(i,j);
								//cout<<"s_int: "<<i<<" "<<j<<endl;
							}
						 
					}
					else  if(preg_mods[j]->tell_var_type()=='c' && 
								preg_mods[i]->tell_var_type()=='c')
					{
                          if(conditions == false)
							FULLCOND_rj::switch_step(i,j);
						  else
							if(conditions_okay_s(i,j) ==true)
								FULLCOND_rj::switch_step(i,j);
					}
				}
				else
				{
					if(preg_mods[j]->tell_var_type()=='d')
					{
						 if(conditions == false)
							FULLCOND_rj_int::birth_step(i,j);
						  else
							if(conditions_okay_b(i,j) ==true)
								FULLCOND_rj_int::birth_step(i,j);
					}
					else
					{
						if(conditions == false)
							FULLCOND_rj::birth_step(i,j);
						  else
							if(conditions_okay_b(i,j) ==true)
								FULLCOND_rj::birth_step(i,j);
					}
				}
		}



		  /************* from this point on like in Fullcond_rj::update ***************/
		  nrtrials++;
		  update_zeta();


		  
		  //std::set < ST::string, unsigned int> freq;

		  if((optionsp->get_nriter() > optionsp->get_burnin()) &&
			  (optionsp->get_nriter() % (optionsp->get_step()) == 0))
		  {
			  store_model();
		  }

	}
Partition ClusteringGenerator::makeOneClustering(Graph& G) {
	count n = G.upperNodeIdBound();
	Partition zeta(n);
	zeta.allToOnePartition();
	return zeta;
}
Example #23
0
static ex Li2_series(const ex &x, const relational &rel, int order, unsigned options)
{
	const ex x_pt = x.subs(rel, subs_options::no_pattern);
	if (x_pt.info(info_flags::numeric)) {
		// First special case: x==0 (derivatives have poles)
		if (x_pt.is_zero()) {
			// method:
			// The problem is that in d/dx Li2(x==0) == -log(1-x)/x we cannot 
			// simply substitute x==0.  The limit, however, exists: it is 1.
			// We also know all higher derivatives' limits:
			// (d/dx)^n Li2(x) == n!/n^2.
			// So the primitive series expansion is
			// Li2(x==0) == x + x^2/4 + x^3/9 + ...
			// and so on.
			// We first construct such a primitive series expansion manually in
			// a dummy symbol s and then insert the argument's series expansion
			// for s.  Reexpanding the resulting series returns the desired
			// result.
			const symbol s;
			ex ser;
			// manually construct the primitive expansion
			for (int i=1; i<order; ++i)
				ser += pow(s,i) / pow(numeric(i), *_num2_p);
			// substitute the argument's series expansion
			ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
			// maybe that was terminating, so add a proper order term
			epvector nseq { expair(Order(_ex1), order) };
			ser += pseries(rel, std::move(nseq));
			// reexpanding it will collapse the series again
			return ser.series(rel, order);
			// NB: Of course, this still does not allow us to compute anything
			// like sin(Li2(x)).series(x==0,2), since then this code here is
			// not reached and the derivative of sin(Li2(x)) doesn't allow the
			// substitution x==0.  Probably limits *are* needed for the general
			// cases.  In case L'Hospital's rule is implemented for limits and
			// basic::series() takes care of this, this whole block is probably
			// obsolete!
		}
		// second special case: x==1 (branch point)
		if (x_pt.is_equal(_ex1)) {
			// method:
			// construct series manually in a dummy symbol s
			const symbol s;
			ex ser = zeta(_ex2);
			// manually construct the primitive expansion
			for (int i=1; i<order; ++i)
				ser += pow(1-s,i) * (numeric(1,i)*(I*Pi+log(s-1)) - numeric(1,i*i));
			// substitute the argument's series expansion
			ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
			// maybe that was terminating, so add a proper order term
			epvector nseq { expair(Order(_ex1), order) };
			ser += pseries(rel, std::move(nseq));
			// reexpanding it will collapse the series again
			return ser.series(rel, order);
		}
		// third special case: x real, >=1 (branch cut)
		if (!(options & series_options::suppress_branchcut) &&
			ex_to<numeric>(x_pt).is_real() && ex_to<numeric>(x_pt)>1) {
			// method:
			// This is the branch cut: assemble the primitive series manually
			// and then add the corresponding complex step function.
			const symbol &s = ex_to<symbol>(rel.lhs());
			const ex point = rel.rhs();
			const symbol foo;
			epvector seq;
			// zeroth order term:
			seq.push_back(expair(Li2(x_pt), _ex0));
			// compute the intermediate terms:
			ex replarg = series(Li2(x), s==foo, order);
			for (size_t i=1; i<replarg.nops()-1; ++i)
				seq.push_back(expair((replarg.op(i)/power(s-foo,i)).series(foo==point,1,options).op(0).subs(foo==s, subs_options::no_pattern),i));
			// append an order term:
			seq.push_back(expair(Order(_ex1), replarg.nops()-1));
			return pseries(rel, std::move(seq));
		}
	}
	// all other cases should be safe, by now:
	throw do_taylor();  // caught by function::series()
}
Example #24
0
int main(int argc, char* argv[])
{

	int program = 0;

	// Get input file from first command line argument
	if(argc < 3){ // No input file given
		std::cerr << "Usage: ./drudeh [input_file] [basis_file]\n";
		program = -1;
	} else {
		std::string ifname = argv[1]; // Input filename
		std::string bfname = argv[2]; // Basis filename
		std::string ofname = ifname; // Output file prefix
		std::size_t pos = ofname.find('.');
		if (pos != std::string::npos) { // Cut off extension
			ofname.erase(pos, ofname.length());
		}
		ofname += ".output";

		// Open the input file
		std::ifstream input(ifname);
		// Check it opened successfully
		if (!input.is_open()){
			std::cerr << "Failed to open input file.\n";
			program = -1;
		} else {

			// Declare and read parameters
			int N;
			std::vector<double> mu, omega, q;
			N = readParams(input, mu, omega, q);

			// Make the zeta vector
			std::vector<double> zeta(N);
			for (int i = 0; i < N; i++) zeta[i] = 0.5*mu[i]*omega[i];

			// Geometry
			Eigen::MatrixXd R(N-1, 3);
			// Rewind input file
			input.clear();
			input.seekg(0, std::ios::beg);
			// Read in geometry
			readGeom(input, R, N);
						
			// Open basis file
			std::ifstream basis(bfname);
			// Check if opened successfully
			if (!basis.is_open()){
				std::cerr << "Failed to open basis file.\n";
				program = -1;
			} else {
				
				// Initialise array of basis functions
				std::vector<BasisFunction> bfs;
								
				// Read in the basis functions
				int nbfs = readBasis(basis, bfs, N, zeta, R);

				// Form and diagonalise hamiltonian matrix
				Eigen::MatrixXd D = hamiltonian(N, nbfs, bfs, R, mu, omega, q);

				// Find lowest non-zero eigenvalue
				//int i = 0;
	     			double lowest_eig = D(0);
				// while ( D(i) < 0.1 ) i++;
				// if ( i < nbfs ) 
				//lowest_eig = D(i);  
								
				// Open output file
				std::ofstream output(ofname);
				if (!output.is_open()){
					std::cout << "Couldn't open output file.\n";
					std::cout << "Total number of basis functions = " << nbfs << "\n";
					std::cout << "Lowest eigenvalue = " << std::setprecision(15) << lowest_eig << "\n";
				} else {
					output << "Total number of basis functions = " << nbfs << "\n";
					output << "Lowest eigenvalue = "<< std::setprecision(15) << lowest_eig << "\n";
 				}
			}
		}
	}
	return program;
}
Example #25
0
void Gauss_siedel(double *phi, double *mu, double *V) {
   long i, j, index, index_right, index_front, index_left, index_back;
   double V_old,A[MESHX*MESHY],B[MESHX*MESHY],C[MESHX*MESHY],E[MESHX*MESHY],F[MESHX*MESHY];
   double error;
   double tol=1.0e-6;

   for(i=1; i < MESHX-1; i++) {
      for(j=1; j< MESHY-1; j++) {
	indx            = i*MESHY    + j;
	indx_rght       = indx      + 1;
	indx_frnt       = indx      + MESHY;
	indx_lft        = indx      - 1;
	indx_bck        = indx      - MESHY;

	A[index]        = (zeta(phi[index_left])  + zeta(phi[index]));  //left
	B[index]        = (zeta(phi[index_right]) + zeta(phi[index]));  //right
	C[index]             = (zeta(phi[index_back])  + zeta(phi[index]));  //back
        E[index]             = (zeta(phi[index_front]) + zeta(phi[index])); //front
        F[index]             = -(A[index] + B[index] + C[index] + E[index]);
      }
   }
   for(;;) {
    for(i=1; i < MESHX-1; i++) {
      for(j=1;j < MESHY-1;j++) {
	index               = i*MESHY    + j;
	index_right         = index      + 1;
	index_front         = index      + MESHY;
	index_left          = index      - 1;
	index_back          = index      - MESHY;

	if (((i+j)%2) == 0) {
	  V_old     = V[index];

	  V[index]  = A[index]*V[index_left] + B[index]*V[index_right]
		    + C[index]*V[index_back] + E[index]*V[index_front];
	  V[index] /= (-F[index]);
	}
      }
    }
    for(i=1; i < MESHX-1; i++) {
      for(j=1;j < MESHY-1; j++) {
	index               = i*MESHY    + j;
	index_right         = index      + 1;
	index_front         = index      + MESHY;
	index_left          = index      - 1;
	index_back          = index      - MESHY;

	if (((i+j)%2) != 0) {
	  V_old     = V[index];

	  V[index]  = A[index]*V[index_left] + B[index]*V[index_right]
		    + C[index]*V[index_back] + E[index]*V[index_front];
	  V[index] /= (-F[index]);
	}
      }
    }
    error = compute_error(A, B, C, E, F, V);
    if (fabs(error) < tol) {
      break;
    }
  }
}