コード例 #1
0
ファイル: TEstimation.cpp プロジェクト: sakeel/ABCtoolbox
//---------------------------------------------------------------------------
double TEstimation::getArea(ColumnVector densities, ColumnVector theta){
   double area=0;
   for(int i=1; i<densities.size();++i){
	  area+=(theta(i+1)-theta(i))*(densities(i+1)+densities(i))/2;
   }
   return area;
}
コード例 #2
0
ファイル: dateDiff.cpp プロジェクト: filimonov/ClickHouse
 void constant_vector(
     T1 x, const ColumnVector<T2> & y,
     const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
     ColumnInt64::Container & result)
 {
     const auto & y_data = y.getData();
     for (size_t i = 0, size = y.size(); i < size; ++i)
         result[i] = calculate<Transform>(x, y_data[i], timezone_x, timezone_y);
 }
コード例 #3
0
ファイル: dateDiff.cpp プロジェクト: filimonov/ClickHouse
 void vector_constant(
     const ColumnVector<T1> & x, T2 y,
     const DateLUTImpl & timezone_x, const DateLUTImpl & timezone_y,
     ColumnInt64::Container & result)
 {
     const auto & x_data = x.getData();
     for (size_t i = 0, size = x.size(); i < size; ++i)
         result[i] = calculate<Transform>(x_data[i], y, timezone_x, timezone_y);
 }
コード例 #4
0
 void ExplicitSystemManager::save_column_vector(const ColumnVector &vec, std::string filename) {
     std::ofstream out_file(filename);
     if (out_file.is_open()) {
         for (int i = 0; i < vec.size(); ++i) {
             out_file << std::setprecision(15) << vec[i] << "\n";
         }
         out_file.close();
     }
     else {
         throw std::runtime_error((boost::format("Unable to open %s.") % filename).str());
     }
 }
コード例 #5
0
ファイル: LoadDNSSCD.cpp プロジェクト: stuartcampbell/mantid
void LoadDNSSCD::loadHuber(ITableWorkspace_sptr tws) {
  ColumnVector<double> huber = tws->getVector("Huber(degrees)");
  // set huber[0] for each run in m_data
  for (auto &ds : m_data) {
    ds.huber = huber[0];
  }
  // dublicate runs for each huber in the table
  std::vector<ExpData> old(m_data);
  for (size_t i = 1; i < huber.size(); ++i) {
    for (auto &ds : old) {
      ds.huber = huber[i];
      m_data.push_back(ds);
    }
  }
}
//constraint, gradient approximated with central difference
//opt++ has bug in approximating gradient with finite difference, so I compute it myself
void optppConstraint(int mode, int n, const ColumnVector &x, ColumnVector &c_x, Matrix &c_grad_x, int &result)
{
    //constraint: A^T*A=I,B^T*B=I ==> A^T*A-I=0,B^T*B-I=0
    //Note: A^T*A and B^T*B are both symmetric matrices, inactive constraints need to be removed
    //      otherwise it leads to singular jacobian of optimization system
    int col=CoupledQuasiHarmonics::static_col;
    int new_col=n/(2*col);
    if(CoupledQuasiHarmonics::static_objective_type==CoupledQuasiHarmonics::ONLY_A)
	new_col=n/col;
    //function evaluation
    if(mode&NLPFunction)
    {
	int idx_1d=1;
	for(int i=1;i<=new_col;++i)
	    for(int j=1;j<=i;++j)
	    {
		c_x(idx_1d)=0;
		for(int k=1;k<=col;++k)
		    c_x(idx_1d)+=x((i-1)*col+k)*x((j-1)*col+k);
		if(i==j)
		    c_x(idx_1d)-=1.0;
		++idx_1d;
	    }
	if(CoupledQuasiHarmonics::static_objective_type==CoupledQuasiHarmonics::A_AND_B)
	{
	    int bias=new_col*col;
	    for(int i=1;i<=new_col;++i)
		for(int j=1;j<=i;++j)
		{
		    c_x(idx_1d)=0;
		    for(int k=1;k<=col;++k)
			c_x(idx_1d)+=x((i-1)*col+k+bias)*x((j-1)*col+k+bias);
		    if(i==j)
			c_x(idx_1d)-=1.0;
		    ++idx_1d;
		}
	}
	result=NLPFunction;
    }
    //gradient evaluation
    if(mode&NLPGradient)
    {
	ColumnVector temp_x=x;
	ColumnVector fplus(c_x.size()),fminus(c_x.size());
	Matrix c_grad_x_t(c_x.size(),n);
	Matrix temp_grad(n,c_x.size());
	int temp_result;
	double perturb=1.0e-8;
	for(int i=1;i<=n;++i)
	{
	    temp_x(i)+=perturb;
	    optppConstraint(NLPFunction,n,temp_x,fplus,temp_grad,temp_result);
	    temp_x(i)-=2*perturb;
	    optppConstraint(NLPFunction,n,temp_x,fminus,temp_grad,temp_result);
	    c_grad_x_t.Column(i)=(fplus-fminus)/(2*perturb);
	    temp_x(i)=x(i);
	}
	c_grad_x=c_grad_x_t.t();
	result=NLPGradient;
    }
}