Changeset 422
- Timestamp:
- 07/20/09 12:41:12 (15 years ago)
- Location:
- library
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.cpp
r384 r422 55 55 } 56 56 57 void RV::init ( Array<std::string> in_names, ivec in_sizes,ivec in_times ) { 57 void RV::init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) 58 { 58 59 len = in_names.length(); 59 60 it_assert_debug ( in_names.length() ==in_times.length(), "check \"times\" " ); … … 106 107 ret.dsize=ret.countsize(); 107 108 return ret; 109 } 110 111 RV RV::operator()(int di1, int di2) const 112 { 113 ivec sz = cumsizes(); 114 int i1 = 0; 115 while (sz(i1) < di1) i1++; 116 int i2 = i1; 117 while (sz(i2) < di2) i2++; 118 return subselect(linspace(i1, i2)); 108 119 } 109 120 -
library/bdm/base/bdmbase.h
r412 r422 32 32 extern Array<string> RV_NAMES; 33 33 34 //! Structure of RV (used internally), i.e. expanded RVs - TODO tak proc je ve verejnem prostoru jmen? upravit34 //! Structure of RV, i.e. RVs expanded into a flat list of IDs, used for debugging. 35 35 class str 36 36 { … … 98 98 private: 99 99 //! auxiliary function used in constructor 100 void init( Array<std::string> in_names, ivec in_sizes, ivecin_times);101 int init(const 100 void init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times); 101 int init(const string &name, int size); 102 102 public: 103 103 //! \name Constructors … … 105 105 106 106 //! Full constructor 107 RV(Array<std::string> in_names, ivec in_sizes, ivec in_times) {init(in_names, in_sizes, in_times);}; 107 RV(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) { init(in_names, in_sizes, in_times); } 108 108 109 //! Constructor with times=0 109 RV(Array<std::string> in_names, ivec in_sizes) {init(in_names, in_sizes, zeros_i(in_names.length()));}; 110 RV(const Array<std::string> &in_names, const ivec &in_sizes) { init(in_names, in_sizes, zeros_i(in_names.length())); } 111 110 112 //! Constructor with sizes=1, times=0 111 RV(Array<std::string> in_names) {init(in_names, ones_i(in_names.length()), zeros_i(in_names.length()));} 113 RV(const Array<std::string> &in_names) { init(in_names, ones_i(in_names.length()), zeros_i(in_names.length())); } 114 112 115 //! Constructor of empty RV 113 RV() : dsize(0), len(0), ids(0), times(0) {} ;116 RV() : dsize(0), len(0), ids(0), times(0) {} 114 117 //! Constructor of a single RV with given id 115 118 RV(string name, int sz, int tm = 0); … … 119 122 //!@{ 120 123 121 //! Printing outpute.g. for debugging.124 //! State output, e.g. for debugging. 122 125 friend std::ostream &operator<< (std::ostream &os, const RV &rv); 123 int _dsize() const {return dsize;} ; 126 127 int _dsize() const { return dsize; } 128 124 129 //! Recount size of the corresponding data vector 125 130 int countsize() const; 126 131 ivec cumsizes() const; 127 int length() const {return len;} ;128 int id(int at) const {return ids(at);} ;129 int size(int at) const { return RV_SIZES(ids(at));};130 int time(int at) const { return times(at);};131 std::string name(int at) const { return RV_NAMES(ids(at));};132 void set_time(int at, int time0) { times(at) = time0;};132 int length() const {return len;} 133 int id(int at) const {return ids(at);} 134 int size(int at) const { return RV_SIZES(ids(at)); } 135 int time(int at) const { return times(at); } 136 std::string name(int at) const { return RV_NAMES(ids(at)); } 137 void set_time(int at, int time0) { times(at) = time0; } 133 138 //!@} 134 139 … … 146 151 //! Subtract another variable from the current one 147 152 RV subt(const RV &rv2) const; 148 //! Select only variables at ind eces ind153 //! Select only variables at indices ind 149 154 RV subselect(const ivec &ind) const; 150 //! Select only variables at indeces ind 151 RV operator()(const ivec &ind) const {return subselect(ind);}; 155 156 //! Select only variables at indices ind 157 RV operator()(const ivec &ind) const { return subselect(ind); } 158 152 159 //! Select from data vector starting at di1 to di2 153 RV operator()(int di1, int di2) const { 154 ivec sz = cumsizes(); 155 int i1 = 0; 156 while (sz(i1) < di1) i1++; 157 int i2 = i1; 158 while (sz(i2) < di2) i2++; 159 return subselect(linspace(i1, i2)); 160 }; 161 //! Shift \c time shifted by delta. 160 RV operator()(int di1, int di2) const; 161 162 //! Shift \c time by delta. 162 163 void t(int delta); 163 164 //!@} … … 168 169 //! generate \c str from rv, by expanding sizes TODO to_string.. 169 170 str tostr() const; 170 //! when this rv is a part of bigger rv, this function returns ind eces of self in the data vector of the bigger crv.171 //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. 171 172 //! Then, data can be copied via: data_of_this = cdata(ind); 172 173 ivec dataind(const RV &crv) const; 173 //! generate mutual ind eces when copying data betwenn self and crv.174 //! generate mutual indices when copying data between self and crv. 174 175 //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) 175 176 void dataind(const RV &rv2, ivec &selfi, ivec &rv2i) const; 176 177 //! Minimum time-offset 177 int mint() const {return min(times);} ;178 int mint() const {return min(times);} 178 179 //!@} 179 180 -
library/tests/CMakeLists.txt
r419 r422 11 11 12 12 # BASIC EXECS 13 EXEC(rv_test)14 13 EXEC(datalink_test) 15 14 EXEC(loggers_test) … … 42 41 43 42 # using UnitTest++ 44 add_executable(testsuite testsuite.cpp test_user_info.cpp test_shared_ptr.cpp)43 add_executable(testsuite rv_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp) 45 44 target_link_libraries(testsuite bdm itpp unittest) 46 45 -
library/tests/rv_test.cpp
r386 r422 1 1 #include <string> 2 #include <sstream> 2 3 #include "../bdm/base/bdmbase.h" 3 4 #include "../bdm/math/square_mat.h" 4 5 #include "../bdm/math/chmat.h" 6 #include "UnitTest++.h" 5 7 6 8 using namespace bdm; 7 9 8 //These lines are needed for use of cout and endl 9 using std::cout; 10 using std::endl; 10 TEST(test_rv) 11 { 12 RV a = RV("{a }", "3"); 13 CHECK_EQUAL(1, a.length()); 14 CHECK_EQUAL(3, a.size(0)); 15 CHECK_EQUAL(std::string("a"), a.name(0)); 11 16 12 int main() 13 { 14 RV a = RV ( "{a }","3"); 15 RV b = RV ( "{b }","2"); 16 RV c = RV ( "{c }"); 17 RV ef = RV ("{e f }"); 17 RV b = RV("{b }", "2"); 18 CHECK_EQUAL(0, b.mint()); 19 20 RV c = RV("{c }"); 21 CHECK_EQUAL(1, c.length()); 22 CHECK_EQUAL(1, c.size(0)); 23 24 RV trv = RV("{e f }", "1 2", "3 4"); 25 CHECK_EQUAL(2, trv.length()); 26 CHECK_EQUAL(3, trv.mint()); 27 28 // add a and b 29 RV ab = a; 30 bool added = ab.add(b); 31 CHECK(added); 32 CHECK_EQUAL(2, ab.length()); 33 CHECK_EQUAL(3, ab.size(0)); 34 CHECK_EQUAL(std::string("a"), ab.name(0)); 35 CHECK_EQUAL(2, ab.size(1)); 36 CHECK_EQUAL(std::string("b"), ab.name(1)); 37 38 std::stringstream abss; 39 abss << ab; 40 CHECK_EQUAL(std::string("1(3)=a_{0}; 2(2)=b_{0}; "), abss.str()); 41 42 // concat a, b and c 43 RV abc = concat(ab, c); 44 CHECK_EQUAL(3, abc.length()); 45 std::stringstream abcss; 46 abcss << abc; 47 CHECK_EQUAL(std::string("1(3)=a_{0}; 2(2)=b_{0}; 3(1)=c_{0}; "), abcss.str()); 48 49 // structure of a, b, c 50 str s = abc.tostr(); 51 int exp_ids[] = { 1, 1, 1, 2, 2, 3 }; 52 int exp_sz = sizeof(exp_ids) / sizeof(exp_ids[0]); 53 CHECK_EQUAL(exp_sz, s.ids.size()); 54 CHECK_EQUAL(exp_sz, s.times.size()); 55 for (int i = 0; i < exp_sz; ++i) { 56 CHECK_EQUAL(exp_ids[i], s.ids(i)); 57 CHECK_EQUAL(0, s.times(i)); 58 } 59 60 RV slice = abc(1, 2); 61 CHECK_EQUAL(1, slice.length()); 62 CHECK_EQUAL(3, slice.size(0)); 63 CHECK_EQUAL(std::string("a"), slice.name(0)); 64 65 // find a in abc 66 ivec f = a.findself(abc); 67 CHECK_EQUAL(1, f.length()); 68 CHECK_EQUAL(0, f(0)); 69 70 // find abc in a 71 f = abc.findself(a); 72 int exp_indices[] = { 0, -1, -1 }; 73 CHECK_EQUAL(3, f.length()); 74 for (unsigned i = 0; 75 i < sizeof(exp_indices) / sizeof(exp_indices[0]); 76 ++i) { 77 CHECK_EQUAL(exp_indices[i], f(i)); 78 } 79 80 // subtract b from abc 81 RV ac = abc.subt(b); 82 std::stringstream acss; 83 acss << ac; 84 CHECK_EQUAL(std::string("1(3)=a_{0}; 3(1)=c_{0}; "), acss.str()); 85 86 // data index of ac in abc 87 ivec di = ac.dataind(abc); 88 int exp_di[] = { 0, 1, 2, 5 }; 89 exp_sz = sizeof(exp_di) / sizeof(exp_di[0]); 90 CHECK_EQUAL(exp_sz, di.size()); 91 for (int i = 0; i < exp_sz; ++i) { 92 CHECK_EQUAL(exp_di[i], di(i)); 93 } 94 95 // Copy indices between ba and ab 96 RV ba = b; 97 ba.add(a); 18 98 19 cout << "Add a and b"<<endl; 20 RV ab = a; 21 ab.add(b); 22 cout << ab <<endl; 23 24 cout << "Concat a,b and c "<<endl; 25 RV abc = concat(ab,c); 26 cout << abc <<endl; 27 cout << "Structure of a,b,c "<<endl; 28 str s =abc.tostr(); 29 cout << s.ids <<endl; 30 cout << s.times <<endl; 31 32 cout << "Find a in abc "<<endl; 33 cout << a.findself(abc)<<endl; 34 cout << "Find abc in a "<<endl; 35 cout << abc.findself(a)<<endl; 36 37 cout << "Subtract b from abc"<<endl; 38 RV ac = abc.subt(b); 39 cout << ac <<endl; 40 41 cout << "Data index of ac in abc"<<endl; 42 cout << ac.dataind(abc) <<endl; 43 44 cout<< "copy indeces between ba and ab" << endl; 45 RV ba = b; 46 ba.add(a); 47 48 ivec ai; 49 ivec bi; 50 ba.dataind(ac,ai,bi); 51 cout << "ba(" << ai <<")=ac(" << bi <<")"<<endl; 52 53 //Exit program: 54 55 //getchar(); 56 return 0; 99 ivec ai; 100 ivec bi; 101 ba.dataind(ac, ai, bi); 57 102 103 int exp_ai[] = { 2, 3, 4 }; 104 exp_sz = sizeof(exp_ai) / sizeof(exp_ai[0]); 105 CHECK_EQUAL(exp_sz, ai.size()); 106 for (unsigned i = 0; 107 i < sizeof(exp_ai) / sizeof(exp_ai[0]); 108 ++i) { 109 CHECK_EQUAL(exp_ai[i], ai(i)); 110 } 111 112 int exp_bi[] = { 0, 1, 2 }; 113 exp_sz = sizeof(exp_bi) / sizeof(exp_bi[0]); 114 CHECK_EQUAL(exp_sz, bi.size()); 115 for (unsigned i = 0; 116 i < sizeof(exp_bi) / sizeof(exp_bi[0]); 117 ++i) { 118 CHECK_EQUAL(exp_bi[i], bi(i)); 119 } 58 120 }