Beispiel #1
0
TEST(CollectionUtilsTest, vecShiftLeftByMoreThanSize) {
    typedef std::vector<size_t> Vec;
    Vec vec;
    
    vec.push_back('a');
    vec.push_back('b');
    vec.push_back('c');
    vec.push_back('d');
    vec.push_back('e');
    vec.push_back('f');
    vec.push_back('g');
    
    Vec leftBy10;
    leftBy10.push_back('d');
    leftBy10.push_back('e');
    leftBy10.push_back('f');
    leftBy10.push_back('g');
    leftBy10.push_back('a');
    leftBy10.push_back('b');
    leftBy10.push_back('c');

    Vec actual = vec;
    VectorUtils::shiftLeft(actual, 10);
    ASSERT_TRUE(std::equal(leftBy10.begin(), leftBy10.end(), actual.begin()));
}
Vec ASSR::initial_state_mean()const {
    Vec ans = StateSpaceModelBase::initial_state_mean();
    double y0 = StateSpaceModelBase::observation_matrix(0).dot(ans);
    ans.push_back(y0);
    ans.push_back(0.0);
    return ans;
}
Beispiel #3
0
TEST(CollectionUtilsTest, vecShiftRightBy2) {
    typedef std::vector<size_t> Vec;
    Vec vec;
    
    vec.push_back('a');
    vec.push_back('b');
    vec.push_back('c');
    vec.push_back('d');
    vec.push_back('e');
    vec.push_back('f');
    vec.push_back('g');
    
    Vec rightBy2;
    rightBy2.push_back('f');
    rightBy2.push_back('g');
    rightBy2.push_back('a');
    rightBy2.push_back('b');
    rightBy2.push_back('c');
    rightBy2.push_back('d');
    rightBy2.push_back('e');
    
    Vec actual = vec;
    VectorUtils::shiftRight(actual, 2);
    ASSERT_TRUE(std::equal(rightBy2.begin(), rightBy2.end(), actual.begin()));
}
TEST(VecOpr,throwtest){
	Vec A;
	Vec B;
	for(int i=0;i<10;i++){
		A.push_back(Rand(engine));
		EXPECT_THROW((A)+(B),Exception<Vec>);
		EXPECT_THROW((A)-(B),Exception<Vec>);
		EXPECT_THROW(SqDistance((A),(B)),Exception<Vec>);
		EXPECT_THROW(Distance((A),(B)),Exception<Vec>);
		B.push_back(Rand(engine));
		EXPECT_NO_THROW((A)+(B));
		EXPECT_NO_THROW((A)-(B));
		EXPECT_NO_THROW(SqDistance((A),(B)));
		EXPECT_NO_THROW(Distance((A),(B)));
	}
	A.clear();B.clear();
	for(int i=0;i<10;i++){
		B.push_back(Rand(engine));
		EXPECT_THROW((A)+(B),Exception<Vec>);
		EXPECT_THROW((A)-(B),Exception<Vec>);
		A.push_back(Rand(engine));
		EXPECT_NO_THROW((A)+(B));
		EXPECT_NO_THROW((A)-(B));
	}
}
TEST(VecOpr,Add_Sub){
	Vec A;
	Vec B;
	for(int i=0;i<10;i++){
		for(double c=-10;c<=10;c+=1){
			EXPECT_TRUE(((A)+(B)).size()==A.size());
			EXPECT_TRUE(((A)-(B)).size()==A.size());
		}
		A.push_back(Rand(engine));
		B.push_back(Rand(engine));
	}
	A.clear();
	B.clear();
	for(int i=0;i<10;i++){
		for(double c=-10;c<=10;c+=1){
			Vec C=(A)+(B);
			Vec D=(C)-(B);
			for(size_t i=0,n=A.size();i<n;i++){
				EXPECT_TRUE(D[i]==A[i]);
				EXPECT_TRUE(C[i]==(A[i]+B[i]));
			}
		}
		A.push_back(Rand(engine));
		B.push_back(Rand(engine));
	}
}
Beispiel #6
0
void Lib::sortedUnique(int n, double *x, Vec& uv)
{
   uv.clear();
   if(n==0) return;
   typedef std::vector<double>::size_type vec_sz;

   //copy x into vector xv
   Vec xv(n);
   vec_sz nv = (vec_sz)n;
   for(vec_sz i=0;i<nv;i++) xv[i] = *(x+i);

   //sort xv
   std::sort(xv.begin(),xv.end());

   //get unique values of xv into uv
   uv.push_back(xv[0]);
   vec_sz nu = 1;
   double ov = uv[0];
   for(vec_sz i=1;i<nv;i++) {
      if(xv[i] != ov) {
         ov = xv[i];
         uv.push_back(ov);
      }
   }

}
TEST(RectDimensions,WhereIntersects_throwing2){
	RectDimensions A;
	Vec O;Vec D;
	for(size_t i=1;i<10;i++){
		A<<make_pair(-10.0+i,double(i));
		EXPECT_THROW(A.WhereIntersects((O),(D)),Exception<RectDimensions>);
		D.push_back(0);
		EXPECT_THROW(A.WhereIntersects((O),(D)),Exception<RectDimensions>);
		O.push_back(0);
		EXPECT_TRUE(A.WhereIntersects((O),(D)).surface==RectDimensions::None);
	}
}
Beispiel #8
0
void Lib::acov(Vec& x,int nl, Vec& acov, bool cor)
{
        typedef Vec::size_type ST;
        double c;
        int n = (int)x.size();
        double m = mean(x);
        acov.clear();
        for(int i=0;i<=nl;i++)
        {
                c=0.0;
                for(int j=0;j<(n-i);j++) c += (x[j]-m)*(x[j+i]-m);
                acov.push_back(c);
        }
        if(cor)
        {
                double c0 = acov[0];
                for(int i=0;i<=nl;i++)
                {
                        acov[i] = acov[i]/c0;
                }
        }
        else
                for(int i=0;i<=nl;i++) acov[i] /= n;

}
Beispiel #9
0
int main(int argc, const char *argv[])
{
    Vec<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;

    while (record.read(cin)) {
        maxlen = max(maxlen, record.name().size());
        students.push_back(record);
    }

    //  
    sort(students.begin(), students.end(), compare);

    for (Vec<double>::size_type i = 0; i != students.size(); i++) {
        cout << students[i].name() << string(maxlen + 1 - students[i].name().size(), ' ');
        try {
            double final_grade = students[i].grade();
            streamsize prec = cout.precision();
            cout << setprecision(3) << final_grade << setprecision(prec) << endl;
        } catch (domain_error e) {
            cout << e.what() << endl;
        }
    }
    return 0;
}
Beispiel #10
0
int main(){
	int cases=0;
	while(1){
		int n,q;
		cin>>n>>q;
		if(n==0) break;
		++cases;
		cout<<"CASE# "<<cases<<":"<<endl;
		marble.clear();
		int k;
		for(int i=0;i<n;++i) {cin>>k;marble.push_back(k);}
		sort(marble.begin(),marble.end());
		for(int i=0;i<q;i++){
			cin>>k;
			Vec::iterator it=lower_bound(marble.begin(),marble.end(), k);
			if(it!=marble.end() && *it==k){
				cout<<k<<" found at "<<it-marble.begin()+1<<endl;
			}else{
				cout<<k<<" not found"<<endl;

			}
		}
	}
	return 0;	
}
Beispiel #11
0
 Vec ArModel::simulate(int n, const Vec &y0) const {
   if(y0.size() != number_of_lags()){
     ostringstream err;
     err << "Error in ArModel::simulate." << endl
         << "Initial state value y0 was size " << y0.size()
         << ", but the model has " << number_of_lags() << " lags."
         << endl;
     report_error(err.str());
   }
   const Vec &phi(this->phi());
   std::deque<double> lags(y0.rbegin(), y0.rend());
   Vec ans;
   ans.reserve(n);
   for(int i = 0; i < n; ++i) {
     double mu = 0;
     for(int lag = 0; lag < number_of_lags(); ++lag) {
       mu += phi[lag] * lags[lag];
     }
     double y = rnorm(mu, sigma());
     lags.push_front(y);
     lags.pop_back();
     ans.push_back(y);
   }
   return ans;
 }
int main()
{
    int H, W, x;
    while (cin >> H >> W, H) {
        Vec subT;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> x;  
                if (x == 1) {
                    subT.push_back(i * W + j);
                }
            }
        }

        Graph G(H * W, Vec(H * W));
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                for (int k = 0; k < H; k++) {
                    for (int l = 0; l < W; l++) {
                        G[i * W + j][k * W + l] = get_dist(i, j, k, l);
                    }
                }
            }
        }
        cout << H * W - minimum_steiner_tree(G, subT) << endl;
    }
    return 0;
}
Beispiel #13
0
TEST(CollectionUtilsTest, vecShiftLeftBySize) {
    typedef std::vector<size_t> Vec;
    Vec vec;
    
    vec.push_back('a');
    vec.push_back('b');
    vec.push_back('c');
    vec.push_back('d');
    vec.push_back('e');
    vec.push_back('f');
    vec.push_back('g');
    
    Vec actual = vec;
    VectorUtils::shiftLeft(actual, vec.size());
    ASSERT_TRUE(std::equal(vec.begin(), vec.end(), actual.begin()));
}
Beispiel #14
0
void TestTransform()
{
  typedef vector<int> Vec;
  Vec test1;
  Vec checked1;
  Vec checked;
  for(int i = 1; i < 5; i++) 
  {
	test1.push_back (i * i);                    // test1: 1 4 9  16 
	checked1.push_back (i * i);
  }

  //vector<int> test2(test1.size());
  //transform(test1.begin(), test1.end(), test2.begin(), [](int i){return ++i;});       // test2: 2 5 10 17

  Vec test2p;
  transform(test1.begin(), test1.end(), back_inserter(test2p), [](int i){return ++i;});       // test2: 2 5 10 17


  cout << "Unary operation, the first is the same: ";
  checked.push_back(2);
  checked.push_back(5);
  checked.push_back(10);
  checked.push_back(17);
  CHECK_RESULTS(IsEqual(test1, checked1));
  cout << "Unary operation, the second has changed: ";
  CHECK_RESULTS(IsEqual(test2p, checked));

  cout << "Binary operation, the first is the same: ";
  checked.clear();
  checked.push_back(3);
  checked.push_back(9);
  checked.push_back(19);
  checked.push_back(33);
  transform(test1.begin(), test1.end(), test2p.begin(), test2p.begin(), [](int i, int j){return i + j;}); // test1: 3 9 19 33 
  CHECK_RESULTS(IsEqual(test1, checked1));   
  cout << "Binary operation, the second has changed: ";
  CHECK_RESULTS(IsEqual(test2p, checked));

  cout << "No elements: ";
  test1.erase(test1.begin(), test1.end());
  checked.erase(checked.begin(), checked.end());
  test2p.erase(test2p.begin(), test2p.end());
  transform(test1.begin(), test1.end(), back_inserter(test2p),[](int i){return ++i;});
  CHECK_RESULTS(IsEqual(test2p, checked));
 }
Beispiel #15
0
Vec<string> getVec(istream &is)
{
	Vec<string> svec;
	string s;
	while (is >> s)
		svec.push_back(s);
	return svec;
}
Beispiel #16
0
void nextFr(int* recPar, int* recNode,int recS ,int offset, int* parents, Vec &Fr )
{
    for (int i = 0; i < recS; i++)
        if (parents[recNode[i]-offset] == -1){
            Fr.push_back(recNode[i]);
            parents[recNode[i]-offset] = recPar[i];
        }
}
Beispiel #17
0
int main(int argc, char* argv[])
{
    int rank,size,offset;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
  
    ostringstream convert;
    convert << rank+1;
    string file = "data"+ convert.str();
    int numLocal, totalNode;
    Graph g(rank,file);

    Matpair buf(4);
    Vec F;
    numLocal = g.localNode();
    offset = rank*numLocal;
    int root = 512;
    int finish,localfinish;
    int parents[numLocal];
    for (int i=0;i<numLocal;i++){
        parents[i] = -1;
    }
    if (rank == root/numLocal){
        F.push_back(root);
        parents[root-offset] = -2;}
    localfinish = !F.empty(); 

    Vec nxFr;
   
    int dep=0;
    int depth[numLocal];
    for (int i = 0; i < numLocal; i++){
	    depth[i]=0;
    }

    while(1){
    	localfinish=!F.empty();
	MPI_Barrier(MPI_COMM_WORLD);
	MPI_Allreduce(&localfinish,&finish,1,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
    	if (finish==0)
		break;
        if (!F.empty()){
	   for (Vec::iterator i = F.begin();i != F.end(); i++){
		depth[*i-offset] = dep+1;
		cout << "node "<< *i <<" depth: "<<dep+1<<endl;
	   }
	}

        nextFrBuf(g,F,rank,size,buf,offset);
        alltoallPersonalized(buf,parents,nxFr,size,offset);
	F=nxFr;
	dep++;
    } 

    MPI_Finalize();
    return 0;
}
Beispiel #18
0
Vec  NetworkLayer:: getOutput(){
    Vec output; 
    
    for(int i=0;i<Neurons.size();i++){
        output.push_back(Neurons[i].getOutput());       
    }

    return output;
}
TEST(VecOpr,Distances){
	Vec A;
	Vec B;
	for(int i=0;i<10;i++){
		for(double c=-10;c<=10;c+=1){
			double d=Distance((A),(B));
			EXPECT_TRUE(d>=0);
			EXPECT_TRUE(sqrt(SqDistance((A),(B)))==d);
		}
		A.push_back(Rand(engine));
		B.push_back(Rand(engine));
	}
	A.clear();
	B.clear();
	A.push_back(Rand(engine));
	B.push_back(Rand(engine));
	EXPECT_TRUE(SqDistance((A),(B))==pow(A[0]-B[0],2));
}
Beispiel #20
0
    //template definition
    void calculatePseudo(typename Kernel::Vector& point, Vec& v1, typename Kernel::Vector& segment, Vec& v2) {
        Vector3 dir = (segment.template head<3>() - segment.template tail<3>()).normalized();
        Vector3 pp = segment.head(3) + (segment.head(3)-point.head(3)).norm()*dir;
#ifdef DCM_USE_LOGGING
        if(!boost::math::isnormal(pp.norm()))
            BOOST_LOG_SEV(log, error) << "Unnormal pseudopoint detected";
#endif
        v2.push_back(pp);
    };
int main()
{
    Vec<int> ages;
    ages.push_back(31);
    ages.push_back(60);
    ages.push_back(0);
    ages.push_back(36);
    ages.push_back(14);
    ages.push_back(0);
    Vec<int>::size_type n = ages.size();
    ages[n-1] = 7;

    ages.erase(2);
    n = ages.size();

    for (Vec<int>::size_type i = 0; i != n; i++)
        cout << ages[i] << endl;

    return 0;
}
TEST(VecOpr,Mul){
	Vec A;
	for(int i=0;i<10;i++){
		for(double c=-10;c<=10;c+=1)
			EXPECT_TRUE(((A)*c).size()==A.size());
		A.push_back(Rand(engine));
	}
	A.clear();
	for(int i=0;i<10;i++){
		for(double c=-9.5;c<10;c+=1){//escape zero
			Vec B=(A)*c;
			Vec C=(B)*(1.0/c);
			for(size_t i=0,n=A.size();i<n;i++){
				EXPECT_TRUE(C[i]==A[i]);
				EXPECT_TRUE(B[i]==(A[i]*c));
			}
		}
		A.push_back(iRand(engine));
	}
}
Beispiel #23
0
int PosCluster::removeOverlaps (int positionDistance)
{
   debug_mustbe (positionDistance > 0);
   if (size () <= 1)
      return 0;

   int lastStartPosition = INT_MAX;

   //
   // greedy algorithm - 
   // (1) the positions are ordered by starting positions
   // (2) go from the end of the list to the beginning,
   //       adding a position iff it does not overlap
   //
   // same as the known activity-selection problem.
   // this algorithm yields an optimal solution
   // 
   Vec <const SeqPosition*> positions;
   PositionSet::reverse_iterator revIt = _set.rbegin();
   for (; revIt != _set.rend () ; ++revIt) {
      //
      //
      const int currentPosition = 
         PositionComparator::strandPos (*revIt);
      const int currentFinishPosition = 
         PositionComparator::strandPos (*revIt, positionDistance);
      //
      //
      if (currentFinishPosition > lastStartPosition) {
         //
         // this is an overlap, we should remove this position
         positions.push_back ((*revIt));
      }
      else {
         //
         // we are keeping this position, we are not keeping any other positions
         // that start before the feature starting here ends
         lastStartPosition = currentPosition;
      }
   }

   //
   // now after we have decided which positions to remove, we actually
   // do the removing
   int size = positions.size ();
   for (int i=0 ; i<size ; i++) {
      removePosition (positions [i]);
   }

   //
   // that's it, we have removed all overlaps!!!
   debug_mustbe (this->size () >= 1);
   return size;
}
	void AbstractMultiInput::OneChannelEnd(){
		m_state--;
		if(m_state==0){
			Vec values;
			for(auto slot:m_input_slots)
				values.push_back(slot->Value());
			Process(values);
			Finish();
		}
		if(m_state<0)
			throw Exception<AbstractMultiInput>("MultiInput error: state changed to an invalid value.");
	}
Beispiel #25
0
void IterTest::iter3()
{
  typedef vector<const char*> Vec;
  Vec v; // Vector of character strings.
  v.push_back("zippy"); // First element.
  v.push_back("motorboy"); // Second element.
  Vec::reverse_iterator it;
  unsigned counter = 0;
  for (it = v.rbegin(); it != v.rend(); ++it, ++counter) {
    switch (counter) {
      case 1:
        CPPUNIT_ASSERT(!strcmp(*it, "zippy"));
        break;
      case 0:
        CPPUNIT_ASSERT(!strcmp(*it, "motorboy"));
        break;
      default:
        CPPUNIT_FAIL;
    }
  }
}
Beispiel #26
0
int main(){
	int kase =0;
	while(true){
		int c,s,q;
		cin>>c>>s>>q;
		if (c==0) break;
		++kase;
		edges.clear();
		for(int i=0;i<s;++i){
			Edge e;
			cin>>e.from>>e.to>>e.weight;
			edges.push_back(e);
		}
		sort(edges.begin(), edges.end());
		for(int i=1;i<=c;++i){
			nodes[i].parent = nodes+i;
			nodes[i].count = 0;
			nodes[i].edges.clear();
		}
		int count =0;
		for(Vec::iterator it = edges.begin(); count< c-1 && it!= edges.end(); ++it){
			Node *rs = getRoot(it->from), *rt = getRoot(it->to);
			if(rs == rt) continue;
			if (rs->count > rt->count){
				rt->parent = rs;
				rs->count+=rt->count;
			} else {
				rs->parent = rt;
				rt->count += rs->count;
			}
			++count;

			nodes[it->from].edges.push_back(*it);
			int tmp = it->to;
			it->to = it->from; it->from =tmp;
			nodes[it->from].edges.push_back(*it);
		}
		if (kase >1) cout<<endl;
		cout<<"Case #"<<kase<<endl;
		for(int i=0;i<q; ++i){
			int s,t;
			cin>>s>>t;
			CalcResult result = calc(s,t,0);
			if (result.reached) {
				cout<<result.value<<endl;
			} else {
				cout<<"no path"<<endl;
			}
		}
	}
	return 0;
}
Beispiel #27
0
int main()
{
    Vec<char> v = {'H', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd', '!'};
    for(const auto &c : v)
        cout << c;
    cout << endl;
    v.push_back(':');
    v.push_back(')');
    cout << v.size() << endl;
    cout << v.capacity() << endl;
    for(auto iter = v.begin(); iter != v.end(); ++iter)
        cout << *iter;
    cout << endl;
    return 0;
}
Beispiel #28
0
void Lib::batchMeans(Vec& x, int bsize, Vec& means)
{
        means.clear();
        int n = (int)x.size();
        bool someleft = (n>=bsize);
        int nbatch=0;
        while(someleft)
        {
                double sum = 0.0;
                int off = nbatch*bsize;
                for(int i=0;i<bsize;i++) sum += x[off+i];
                means.push_back(sum/bsize);
                nbatch += 1;
                someleft = (n>=(bsize)*(nbatch+1));
        }
}
TEST(RectDimensions,IsInside){
	RectDimensions A;
	for(size_t i=1;i<10;i++){
		A<<make_pair(-10.0+i,double(i));
		for(size_t cnt=0;cnt<(500*i);cnt++){
			bool test=true;
			Vec v;
			for(size_t j=0,n=A.NumberOfDimensions();j<n;j++){
				double x=Rand(engine);
				test&=(x>=A.Dimension(j).first)&&(x<=A.Dimension(j).second);
				EXPECT_THROW(A.IsInside((v)),Exception<RectDimensions>);
				v.push_back(x);
			}
			EXPECT_TRUE(A.IsInside((v))==test);
		}
	}
}
Beispiel #30
0
int main()
{
	//test example
	Vec<int> data;
	for (int i = 0; i < 30000; i++)
		data.push_back(i);
	
	std::cout << data[29999] << std::endl;

//	for (int i = 0; i < 10; i++)
//		std::cout << data[i] << std::endl;
//
//	data.erase(data.begin(), data.begin() + 1);
//	for (int i = 0; i < 9; i++)
//		std::cout << data[i] << std::endl;
//	data.clear();

	return 0;
}