• R/O
  • SSH
  • HTTPS

particle-filter: 提交


Commit MetaInfo

修订版9 (tree)
时间2010-04-19 20:50:30
作者(del#42041)

Log Message

introducing template

更改概述

差异

--- test/test_particle.cc (revision 8)
+++ test/test_particle.cc (revision 9)
@@ -5,6 +5,7 @@
55
66 namespace test_particle
77 {
8+
89 void cut_setup()
910 {
1011 }
@@ -15,7 +16,7 @@
1516 void test_construction(void)
1617 {
1718 // Constructor
18- Particle a(5);
19+ Particle<int> a(5);
1920 vector<int> p = a.get_particle();
2021 cppcut_assert_equal(5, (int)p.size());
2122
@@ -24,6 +25,10 @@
2425 for(int i=1;i<5;++i) b.at(i-1)=i;
2526 a = b;
2627 for(int i=1;i<5;++i) cut_assert_equal_int(i, a[i-1]);
28+ Particle<int> c(5);
29+ cut_assert_equal_int(5,c.size());
30+ cut_assert_equal_string("(0,0,0,0,0)",c.toString().c_str());
31+ for(int i=1;i<5;++i) cut_assert_equal_int(i, a[i-1]);
2732 }
2833 void test_MersennneTwister(void)
2934 {
@@ -32,7 +37,7 @@
3237 }
3338 void test_dumping(void)
3439 {
35- Particle a(5);
36- cout<<endl; cppcut_assert_equal(0,a.dump_particle());
40+ Particle<int> a(5);
41+ cut_assert_equal_string("(0,0,0,0,0)",a.toString().c_str());
3742 }
3843 }
--- test/test_filter.cc (revision 8)
+++ test/test_filter.cc (revision 9)
@@ -4,7 +4,7 @@
44 #include <filter.h>
55 using namespace std;
66
7-Particles func(Particles &p)
7+Particles<int> func(Particles<int> &p)
88 {
99 // Particles a(5,3);
1010 // Particles a;
@@ -30,7 +30,12 @@
3030 a.createInitialParticles();
3131 a.dump_predict_particles();
3232 a.set_state_func(&func);
33- Particles b(5,3);
33+ Particles<int> b(5,3);
3434 a.get_next_state(b);
3535 }
3636 }
37+int main()
38+{
39+ test_filter::test_construction();
40+ return(0);
41+}
\ No newline at end of file
--- test/test_particles.cc (revision 8)
+++ test/test_particles.cc (revision 9)
@@ -14,13 +14,38 @@
1414
1515 void test_construction(void)
1616 {
17- Particles a(3);
18- vector<Particle> p = a.get_particles();
17+ Particles<int> a(3);
18+ vector<Particle<int> > p = a.get_particles();
1919 cppcut_assert_equal(3, (int)p.size());
20+ cppcut_assert_equal(3, a.size());
21+ Particles<int> b(3,2);
22+ for(int i=0;i<3;++i)
23+ cut_assert_equal_string("(0,0)", b[i].toString().c_str());
2024 }
25+ void test_manipulate(void)
26+ {
27+ Particles<int> b(3,2);
28+ Particle<int> c(2);
29+ c[0]=1; c[1]=2;
30+ cut_assert_equal_string("(1,2)", c.toString().c_str());
31+ b[0] = c;
32+ cut_assert_equal_string("(1,2)", b[0].toString().c_str());
33+ int n=10;
34+ for(int i=0;i<3;++i)
35+ {
36+ for(int j=0;j<2;++j)
37+ {
38+ b[i][j] = n++;
39+ }
40+ }
41+ cut_assert_equal_string("(10,11)", b[0].toString().c_str());
42+ cut_assert_equal_string("(12,13)", b[1].toString().c_str());
43+ cut_assert_equal_string("(14,15)", b[2].toString().c_str());
44+// cppcut_assert_equal(0,b.dump_particles());
45+ }
2146 void test_dumping(void)
2247 {
23- Particles a(3,2);
48+ Particles<int> a(3,2);
2449 cppcut_assert_equal(0,a.dump_particles());
2550 }
2651
--- src/particle.cc (revision 8)
+++ src/particle.cc (revision 9)
@@ -1,48 +1,82 @@
11 #include <particle.h>
22
3-Particle::Particle()
3+
4+template<class C> Particle<C>::Particle()
45 {
6+ dimension = -1;
57 }
6-Particle::Particle(int dimension)
8+template<class C> Particle<C>::Particle(int dimension)
79 {
810 resize(dimension);
911 }
10-Particle::~Particle()
12+template<class C> Particle<C>::~Particle()
1113 {
1214 }
13-Particle& Particle::operator=(vector<int> &f)
15+template<class C> Particle<C>::Particle(const Particle &f)
1416 {
17+ p = f.p;
18+ dimension = f.dimension;
19+}
20+template<class C> Particle<C>& Particle<C>::operator=(const Particle &f)
21+{
22+ if(this != &f)
23+ {
24+ if(dimension<0)
25+ {
26+ dimension = f.dimension;
27+ resize(dimension);
28+ }
29+ p = f.p;
30+ }
31+ return(*this);
32+}
33+template<class C> Particle<C>& Particle<C>::operator=(vector<C> &f)
34+{
1535 p = f;
1636 return(*this);
1737 }
18-int Particle::operator[](unsigned int i)
38+template<class C> C &Particle<C>::operator[](unsigned int i)
1939 {
20- return(p.at(i));
40+ return(p[i]);
2141 }
22-int Particle::resize(int n)
42+template<class C> C Particle<C>::operator[](unsigned int i) const
2343 {
44+ return(p[i]);
45+}
46+template<class C> int Particle<C>::resize(int n)
47+{
2448 p.resize(n);
2549 this->dimension = n;
2650 return(0);
2751 }
28-vector<int> Particle::get_particle()
52+template<class C> int Particle<C>::size()
2953 {
54+ return(dimension);
55+}
56+template<class C> vector<C> Particle<C>::get_particle()
57+{
3058 return(p);
3159 }
32-int Particle::dump(vector<int> q)
60+template<class C> string Particle<C>::toString()
3361 {
34- cout<<"(";
35- for(vector<int>::iterator i=q.begin();
36- i!=q.end();++i)
62+ ostringstream s;
63+ s<<"(";
64+// for(vector<C>::iterator i=p.begin();
65+// i!=p.end();++i)
66+ for(int i=0;i<p.size();++i)
3767 {
38- if(i!=q.begin()) cout<<",";
39- cout<<*i;
68+// if(i!=p.begin()) s<<",";
69+ if(i!=0) s<<",";
70+// s<<dec<<*i;
71+ s<<dec<<p[i];
4072 }
41- cout<<")";
42- return(0);
73+ s<<")";
74+ return(s.str());
4375 }
44-int Particle::dump_particle()
76+template<class C> int Particle<C>::dump()
4577 {
46- dump(p);
78+ cout<<toString()<<endl;
4779 return(0);
4880 }
81+
82+template class Particle<int>;
--- src/filter.cc (revision 8)
+++ src/filter.cc (revision 9)
@@ -5,11 +5,14 @@
55 number = -1;
66 dimension = -1;
77 state_func = NULL;
8- x = NULL;
8+// x = NULL;
9+// v = NULL;
10+ mtrand.seed(time(NULL));
911 }
1012 Filter::~Filter()
1113 {
12- if(x) delete x;
14+// if(x) delete x;
15+// if(v) delete v;
1316 }
1417
1518 bool Filter::setNumber(int n)
@@ -25,20 +28,34 @@
2528 bool Filter::createInitialParticles()
2629 {
2730 if(number<0||dimension<0) return(false);
28- x = new Particles(number, dimension);
31+// x = new Particles(number, dimension);
32+// v = new Particles(number, dimension);
33+ x.resize(number, dimension);
34+ v.resize(number, dimension);
2935 return(true);
3036 }
37+bool Filter::createSystemNoise()
38+{
39+ for(int i=0;i<v.size();++i)
40+ {
41+ for(int j=0;j<v[i].size();++j)
42+ {
43+// v[i][j] =
44+ }
45+ }
46+
47+}
3148 int Filter::dump_predict_particles()
3249 {
33- x->dump_particles();
50+ x.dump_particles();
3451 return(0);
3552 }
36-bool Filter::set_state_func(Particles (*func)(Particles &p))
53+bool Filter::set_state_func(Particles<int> (*func)(Particles<int> &p))
3754 {
3855 state_func = func;
3956 return(true);
4057 }
41-Filter & Filter::get_next_state(Particles &p)
58+Filter & Filter::get_next_state(Particles<int> &p)
4259 {
4360 cout<<"go..."<<endl;
4461 (*state_func)(p);
--- src/particle.h (revision 8)
+++ src/particle.h (revision 9)
@@ -2,23 +2,29 @@
22 #define __PARTICLE
33 #include <iostream>
44 #include <vector>
5+#include <string>
6+#include <sstream>
57 #include <MersenneTwister.h>
68 using namespace std;
79
8-class Particle
10+template<class C> class Particle
911 {
10- vector<int> p;
12+ vector<C> p;
1113 int dimension;
1214 public:
1315 Particle();
1416 Particle(int dimension);
1517 ~Particle();
18+ Particle(const Particle &f);
19+ Particle& operator=(const Particle &f);
1620 int resize(int n);
17- vector<int> get_particle();
18- int dump(vector<int> q);
19- int dump_particle();
20- Particle& operator=(vector<int> &f);
21- int operator[](unsigned int i);
21+ int size();
22+ vector<C> get_particle();
23+ string toString();
24+ int dump();
25+ Particle& operator=(vector<C> &f);
26+ C &operator[](unsigned int i);
27+ C operator[](unsigned int i) const;
2228 };
2329
2430
--- src/particles.cc (revision 8)
+++ src/particles.cc (revision 9)
@@ -1,37 +1,58 @@
11 #include <particles.h>
22
3-Particles::Particles()
3+template<class C> Particles<C>::Particles()
44 {
55 }
6-Particles::Particles(int number)
6+template<class C> Particles<C>::Particles(int number)
77 {
88 p.resize(number);
99 this->number = number;
1010 }
11-Particles::Particles(int number, int dimension)
11+template<class C> Particles<C>::Particles(int number, int dimension)
1212 {
13+ resize(number, dimension);
14+}
15+template<class C> Particles<C>::~Particles()
16+{
17+}
18+template<class C> bool Particles<C>::resize(int number, int dimension)
19+{
1320 p.resize(number);
1421 this->number = number;
15- for(vector<Particle>::iterator i= p.begin();
16- i!=p.end();++i)
22+ typedef Particle<C> ParticleC;
23+// for(vector<ParticleC>::iterator i= p.begin();
24+// i!=p.end();++i)
25+ for(int i=0;i<p.size();++i)
1726 {
18- i->resize(dimension);
27+// i->resize(dimension);
28+ p[i].resize(dimension);
1929 }
30+ return(true);
2031 }
21-Particles::~Particles()
32+template<class C> int Particles<C>::size()
2233 {
34+ return(number);
2335 }
24-vector<Particle> Particles::get_particles()
36+template<class C> Particle<C> &Particles<C>::operator[](unsigned int i)
2537 {
38+ return(p.at(i));
39+}
40+template<class C> Particle<C> Particles<C>::operator[](unsigned int i) const
41+{
42+ return(p.at(i));
43+}
44+template<class C> vector<Particle<C> > Particles<C>::get_particles()
45+{
2646 return(p);
2747 }
28-int Particles::dump_particles()
48+template<class C> int Particles<C>::dump_particles()
2949 {
30- for(vector<Particle>::iterator i=p.begin();
31- i!=p.end();++i)
50+// for(vector<Particle<C> >::iterator i=p.begin();
51+// i!=p.end();++i)
52+ for(int i=0;i<p.size();++i)
3253 {
33- if(i!=p.begin()) cout<<endl;
34- i->dump_particle();
54+// i->dump();
55+ p[i].dump();
3556 }
3657 return(0);
3758 }
--- src/filter.h (revision 8)
+++ src/filter.h (revision 9)
@@ -4,14 +4,17 @@
44 #include <vector>
55 #include <particles.h>
66 #include <MersenneTwister.h>
7+#include <ctime>
78 using namespace std;
89
910 class Filter
1011 {
11- Particles *x;
12+ Particles<int> x, v;
13+ double w;
14+ MTRand mtrand;
1215 vector<int> y;
1316 int number, dimension;
14- Particles (*state_func)(Particles &p);
17+ Particles<int> (*state_func)(Particles<int> &p);
1518 public:
1619 Filter();
1720 ~Filter();
@@ -18,9 +21,10 @@
1821 bool setNumber(int n);
1922 bool setDimension(int d);
2023 bool createInitialParticles();
24+ bool createSystemNoise();
2125 int dump_predict_particles();
22- bool set_state_func(Particles (*func)(Particles &p));
23- Filter & get_next_state(Particles &p);
26+ bool set_state_func(Particles<int> (*func)(Particles<int> &p));
27+ Filter & get_next_state(Particles<int> &p);
2428 };
2529
2630 #endif
--- src/particles.h (revision 8)
+++ src/particles.h (revision 9)
@@ -5,9 +5,9 @@
55 #include <particle.h>
66 using namespace std;
77
8-class Particles
8+template<class C> class Particles
99 {
10- vector<Particle> p;
10+ vector<Particle<C> > p;
1111 int number;
1212 public:
1313 Particles();
@@ -14,7 +14,11 @@
1414 Particles(int number);
1515 Particles(int number, int dimension);
1616 ~Particles();
17- vector<Particle> get_particles();
17+ bool resize(int number, int dimension);
18+ int size();
19+ Particle<C> &operator[](unsigned int i);
20+ Particle<C> operator[](unsigned int i) const;
21+ vector<Particle<C> > get_particles();
1822 int dump_particles();
1923 };
2024
Show on old repository browser