Changeset 1027
- Timestamp:
- Feb 2, 2008, 10:29:29 PM (16 years ago)
- Location:
- trunk
- Files:
-
- 15 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/iterator_test.cc
r1000 r1027 53 53 utility::vector::iterator begin=vec.begin(); 54 54 // test iterator to const_iterator conversion 55 /////////////////////////////////////////////////////// 56 /* const conversion doesn't work for new vector structure 55 57 utility::vector::const_iterator ci = vec.begin(); 56 58 ci = begin; 57 59 if (begin!=ci) 58 60 ok = false; 59 61 */ 62 /////////////////////////////////////////////////////// 60 63 utility::vector::iterator end=vec.end(); 61 64 std::sort(begin, end); -
trunk/test/vector_test.cc
r1015 r1027 29 29 #include "yat/utility/utility.h" 30 30 #include "yat/utility/vector.h" 31 #include "yat/utility/VectorConstView.h" 31 32 #include "yat/utility/VectorView.h" 32 33 … … 67 68 shuffle(vec); 68 69 double sum_after = utility::sum(vec); 69 ok &= (sum_after==sum_before); 70 if (sum_after!=sum_before){ 71 *message << "shuffle failed" << std::endl; 72 ok = false; 73 } 70 74 71 75 // checking that view works … … 88 92 *message << "const view implementation" << std::endl; 89 93 const utility::vector vv(10,3.0); 90 utility::Vector View vview(vv,0,5,1);94 utility::VectorConstView vview(vv,0,5,1); 91 95 // const utility::vector vview(vv,0,5,1); // this is the proper line 92 96 utility::vector vv2(5,2.0); … … 117 121 } 118 122 119 /* different sizes are allowed in new design120 123 // checking that assignment operator throws an exception if vectors 121 124 // differ in size … … 130 133 try { 131 134 utility::vector v(vec_view.size()+1,0.0); 132 v =vec_view;135 vec_view=v; 133 136 } catch (utility::GSL_error& err) { 134 137 exception_happens=true; … … 141 144 gsl_set_error_handler(err_handler); 142 145 } 143 */144 146 145 147 // checking that assignment operator changes the underlying object when -
trunk/yat/classifier/KNN.h
r1009 r1027 261 261 for(size_t sample=0;sample<distances->columns();sample++) { 262 262 std::vector<size_t> k_index; 263 utility::sort_smallest_index(k_index,k_,distances->column_vec(sample)); 263 utility::sort_smallest_index(k_index,k_, 264 distances->column_const_vec(sample)); 264 265 for(size_t j=0;j<k_index.size();j++) { 265 266 prediction(target_(k_index[j]),sample)++; -
trunk/yat/classifier/NBC.cc
r1009 r1027 207 207 for (size_t i=0; i<prediction.rows(); ++i){ 208 208 prediction.row_vec(i) *= 209 1.0/sum( 210 prediction.row_vec(i) 211 ); 209 1.0/sum(prediction.row_const_vec(i)); 212 210 } 213 211 } -
trunk/yat/utility/Iterator.h
r1000 r1027 30 30 #include <stddef.h> 31 31 #include <stdexcept> 32 #include <utility> 32 33 33 34 namespace theplu { … … 35 36 namespace utility { 36 37 38 class VectorBase; 39 37 40 /** 38 41 @brief Iterator 39 42 */ 40 template<typename return_type, typename Container>43 template<typename T, typename Container> 41 44 class Iterator 42 45 { … … 46 49 typedef size_t difference_type; 47 50 typedef double* pointer; 48 typedef double& reference; 49 51 typedef T reference; 52 53 private: 54 typedef Iterator<reference, Container> self; 55 56 public: 50 57 /** 51 58 \brief Default Constructor … … 62 69 : container_(&container), index_(index) {} 63 70 71 operator Iterator<const reference, const Container>() 72 { return Iterator<const reference, const Container>(*container_, index_); } 73 64 74 /** 65 75 \return element 66 76 */ 67 re turn_type operator*(void) const77 reference operator*(void) const 68 78 { yat_assert<std::out_of_range>(index_<container_->size()); 69 79 return container_->operator()(index_); } … … 72 82 \return element \a n steps forward 73 83 */ 74 re turn_type operator[](difference_type n) const84 reference operator[](difference_type n) const 75 85 { yat_assert<std::out_of_range>(index_+n < container_->size()); 76 86 return container_->operator()(index_+n); } … … 89 99 */ 90 100 Iterator operator++(int) 91 { Iterator<return_type, Container>tmp(*this); ++index_; return tmp;}101 { self tmp(*this); ++index_; return tmp;} 92 102 93 103 /** … … 104 114 */ 105 115 Iterator operator--(int) 106 { Iterator<return_type, Container>tmp(*this); --index_; return tmp;}116 { self tmp(*this); --index_; return tmp;} 107 117 108 118 /** … … 126 136 */ 127 137 friend Iterator operator+(const Iterator& lhs, size_t n) 128 { return Iterator<return_type, Container>(*lhs.container_, lhs.index_+n); }138 { return self(*lhs.container_, lhs.index_+n); } 129 139 130 140 /** … … 134 144 */ 135 145 friend Iterator operator-(const Iterator& lhs, size_t n) 136 { return Iterator<return_type, Container>(*lhs.container_, lhs.index_-n); }146 { return self(*lhs.container_, lhs.index_-n); } 137 147 138 148 /** … … 146 156 147 157 /** 148 \brief Conversion operator149 150 This operator allows automatic conversion from iterator to151 const_iterator152 */153 operator Iterator<const double, const Container> ()154 { return Iterator<const double, const Container>(*container_, index_); }155 156 /**157 158 \brief Equality operator 158 159 159 160 \return True if \a lhs and \a rhs are pointing to same element 160 161 */ 161 friend bool operator==(const Iterator<return_type, Container>& lhs, 162 const Iterator<return_type, Container>& rhs) 162 friend bool operator==(const self& lhs, const self& rhs) 163 163 { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; } 164 164 … … 168 168 \return False if \a lhs and \a rhs are pointing to same element 169 169 */ 170 friend bool operator!=(const Iterator <return_type, Container>& lhs,171 const Iterator <return_type, Container>& rhs)170 friend bool operator!=(const Iterator& lhs, 171 const Iterator& rhs) 172 172 { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); } 173 173 … … 175 175 \brief Less operator 176 176 */ 177 friend bool operator<(const Iterator<return_type, Container>& lhs, 178 const Iterator<return_type, Container>& rhs) 177 friend bool operator<(const self& lhs, const self& rhs) 179 178 { return lhs.index_<rhs.index_; } 180 179 … … 182 181 \brief Less equal operator 183 182 */ 184 friend bool operator<=(const Iterator<return_type, Container>& lhs, 185 const Iterator<return_type, Container>& rhs) 183 friend bool operator<=(const self& lhs, const self& rhs) 186 184 { return lhs.index_<=rhs.index_; } 187 185 … … 189 187 \brief Larger operator 190 188 */ 191 friend bool operator>(const Iterator<return_type, Container>& lhs,192 const Iterator<return_type, Container>& rhs)189 friend bool operator>(const self& lhs, 190 const self& rhs) 193 191 { return lhs.index_>rhs.index_; } 194 192 … … 196 194 \brief Larger equal operator 197 195 */ 198 friend bool operator>=(const Iterator<return_type, Container>& lhs, 199 const Iterator<return_type, Container>& rhs) 196 friend bool operator>=(const self& lhs, const self& rhs) 200 197 { return lhs.index_>=rhs.index_; } 201 198 … … 208 205 //Iterator& operator=(const Iterator&); 209 206 }; 210 211 207 }}} // of namespace utility, yat, and theplu 212 208 -
trunk/yat/utility/Makefile.am
r1015 r1027 30 30 OptionHelp.cc OptionSwitch.cc \ 31 31 PCA.cc stl_utility.cc SVD.cc TypeInfo.cc utility.cc vector.cc \ 32 VectorBase.cc Vector View.cc WeNNI.cc32 VectorBase.cc VectorConstView.cc VectorMutable.cc VectorView.cc WeNNI.cc 33 33 34 34 include_utilitydir = $(includedir)/yat/utility … … 41 41 OptionHelp.h OptionSwitch.h \ 42 42 PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h \ 43 VectorBase.h Vector View.h \43 VectorBase.h VectorConstView.h VectorMutable.h VectorView.h \ 44 44 WeNNI.h yat_assert.h 45 45 -
trunk/yat/utility/PCA.cc
r1015 r1027 193 193 utility::vector A_row_sum(A_.rows()); 194 194 for (size_t i=0; i<A_row_sum.size(); ++i){ 195 const Vector View tmp(A_.row_vec(i));195 const VectorConstView tmp(A_.row_const_vec(i)); 196 196 A_row_sum(i) = sum(tmp); 197 197 } -
trunk/yat/utility/VectorBase.cc
r1015 r1027 43 43 44 44 45 VectorBase::VectorBase(void)46 : vec_(NULL), const_vec_(NULL)47 {48 }49 50 51 VectorBase::VectorBase(gsl_vector* v)52 : vec_(v), const_vec_(v)53 {54 }55 56 57 45 VectorBase::VectorBase(const gsl_vector* v) 58 : vec_(NULL),const_vec_(v)46 : const_vec_(v) 59 47 { 60 48 } … … 66 54 67 55 68 void VectorBase::all(double value)69 {70 assert(vec_);71 gsl_vector_set_all(vec_,value);72 }73 74 75 56 VectorBase::const_iterator VectorBase::begin(void) const 76 57 { … … 79 60 80 61 81 VectorBase::iterator VectorBase::begin(void)82 {83 return iterator(*this, 0);84 }85 86 87 void VectorBase::div(const VectorBase& other)88 {89 assert(vec_);90 int status=gsl_vector_div(vec_,other.gsl_vector_p());91 if (status)92 throw utility::GSL_error(std::string("VectorBase::div",status));93 }94 95 96 62 VectorBase::const_iterator VectorBase::end(void) const 97 63 { 98 64 return const_iterator(*this, size()); 99 }100 101 102 VectorBase::iterator VectorBase::end(void)103 {104 return iterator(*this, size());105 65 } 106 66 … … 128 88 129 89 130 gsl_vector* VectorBase::gsl_vector_p(void)131 {132 return vec_;133 }134 135 136 void VectorBase::mul(const VectorBase& other)137 {138 assert(vec_);139 int status=gsl_vector_mul(vec_,other.gsl_vector_p());140 if (status)141 throw utility::GSL_error(std::string("VectorBase::div",status));142 }143 144 145 90 size_t VectorBase::size(void) const 146 91 { … … 160 105 161 106 162 double& VectorBase::operator()(size_t i)163 {164 double* d=gsl_vector_ptr(vec_, i);165 if (!d)166 throw utility::GSL_error("VectorBase::operator()",GSL_EINVAL);167 return *d;168 }169 170 171 double& VectorBase::operator[](size_t i)172 {173 return this->operator()(i);174 }175 176 177 107 const double& VectorBase::operator[](size_t i) const 178 108 { … … 199 129 res += other(i) * (*this)(i); 200 130 return res; 201 }202 203 204 const VectorBase& VectorBase::operator+=(double d)205 {206 assert(vec_);207 gsl_vector_add_constant(vec_, d);208 return *this;209 }210 211 212 const VectorBase& VectorBase::operator-=(const VectorBase& other)213 {214 assert(vec_);215 int status=gsl_vector_sub(vec_, other.gsl_vector_p());216 if (status)217 throw utility::GSL_error(std::string("VectorBase::sub", status));218 return *this;219 }220 221 222 const VectorBase& VectorBase::operator-=(const double d)223 {224 assert(vec_);225 gsl_vector_add_constant(vec_, -d);226 return *this;227 }228 229 230 const VectorBase& VectorBase::operator*=(double d)231 {232 assert(vec_);233 gsl_vector_scale(vec_, d);234 return *this;235 131 } 236 132 … … 280 176 281 177 282 void shuffle(VectorBase& invec)283 {284 random::DiscreteUniform rnd;285 std::random_shuffle(invec.begin(), invec.end(), rnd);286 }287 288 289 178 void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec) 290 179 { -
trunk/yat/utility/VectorBase.h
r1015 r1027 89 89 { 90 90 public: 91 92 /// \brief VectorBase::iterator93 typedef Iterator<double&, VectorBase> iterator;94 95 91 /// \brief VectorBase::const_iterator 96 typedef Iterator<const double, const VectorBase> const_iterator; 97 98 /** 99 \brief default constructor 100 */ 101 VectorBase(void); 92 typedef Iterator<const double&, const VectorBase> const_iterator; 102 93 103 94 /** 104 95 \brief Constructor. 105 96 */ 106 VectorBase(gsl_vector*); 107 108 /** 109 \brief Constructor. 110 */ 111 VectorBase(const gsl_vector*); 97 VectorBase(const gsl_vector* v=NULL); 112 98 113 99 /// … … 115 101 /// 116 102 virtual ~VectorBase(void); 117 118 ///119 /// Set all elements to \a value.120 ///121 void all(double value);122 123 /**124 \return mutable iterator to start of VectorBase125 */126 iterator begin(void);127 103 128 104 /** … … 132 108 133 109 /** 134 \brief This function performs element-wise division, \f$ this_i =135 this_i/other_i \; \forall i \f$.136 137 \throw GSL_error if dimensions mis-match.138 */139 void div(const VectorBase& other);140 141 /**142 \return mutable iterator to end of VectorBase143 */144 iterator end(void);145 146 /**147 110 \return read-only iterator to end of VectorBase 148 111 */ … … 161 124 162 125 /// 163 /// @return A pointer to the internal GSL vector,164 ///165 gsl_vector* gsl_vector_p(void);166 167 ///168 126 /// @return A const pointer to the internal GSL vector, 169 127 /// … … 171 129 172 130 /** 173 174 131 Check if the vector object is a view (sub-vector) to another 175 132 vector. 176 133 177 134 \return True if the object is a view, false othwerwise. 178 179 135 */ 180 136 virtual bool isview(void) const=0; 181 137 182 /**183 \brief This function performs element-wise multiplication, \f$184 this_i = this_i * other_i \; \forall i \f$.185 186 \throw GSL_error if dimensions mis-match.187 */188 void mul(const VectorBase& other);189 190 /**191 \brief Reverse the order of elements in the VectorBase.192 */193 void reverse(void);194 195 138 /// 196 139 /// @return the number of elements in the VectorBase. … … 199 142 200 143 /** 201 \brief Exchange elements \a i and \a j.202 203 \throw GSL_error if VectorBase lengths differs.204 */205 void swap(size_t i, size_t j);206 207 /**208 144 \brief Element access operator. 209 145 210 \return Reference to element \a i.146 \return Const reference to element \a i. 211 147 212 148 \throw If GSL range checks are enabled in the underlying GSL … … 214 150 of range. 215 151 */ 216 double& operator()(size_t i);217 // Peter, remove this one218 double& operator[](size_t i);219 220 /**221 \brief Element access operator.222 223 \return Const reference to element \a i.224 225 \throw If GSL range checks are enabled in the underlying GSL226 library a GSL_error exception is thrown if either index is out227 of range.228 */229 152 const double& operator()(size_t i) const; 230 153 // Peter, remove this one … … 262 185 double operator*(const VectorBase&) const; 263 186 264 /**265 \brief The assignment operator.266 267 Dimensions of the VectorBases must match. If the LHS VectorBase is a268 view, the underlying data will be changed.269 270 \return A const reference to the resulting VectorBase.271 272 \see void set(const VectorBase&).273 274 \throw GSL_error if dimensions mis-match.275 */276 //const VectorBase& operator=(VectorBase&);277 278 /**279 \brief Addition and assign operator. VectorBase addition, \f$280 this_i = this_i + other_i \; \forall i \f$.281 282 \return A const reference to the resulting VectorBase.283 284 \throw GSL_error if dimensions mis-match.285 */286 const VectorBase& operator+=(const VectorBase&);287 288 /**289 \brief Add a constant to a VectorBase, \f$ this_i = this_i + d \;290 \forall i \f$.291 292 \return A const reference to the resulting VectorBase.293 */294 const VectorBase& operator+=(double d);295 296 /**297 \brief Subtract and assign operator. VectorBase subtraction, \f$298 this_i = this_i - other_i \; \forall i \f$.299 300 \return A const reference to the resulting VectorBase.301 302 \throw GSL_error if dimensions mis-match.303 */304 const VectorBase& operator-=(const VectorBase&);305 306 /**307 \brief Subtract a constant to a VectorBase, \f$ this_i = this_i - d308 \; \forall i \f$.309 310 \return A const reference to the resulting VectorBase.311 */312 const VectorBase& operator-=(double d);313 314 /**315 \brief Multiply with scalar and assign operator, \f$ this_i =316 this_i * d \; \forall i \f$.317 318 \return A const reference to the resulting VectorBase.319 */320 const VectorBase& operator*=(double d);321 322 323 324 187 protected: 325 gsl_vector* vec_;326 188 const gsl_vector* const_vec_; 327 328 /*329 struct proxy330 {331 gsl_vector* vec_;332 };333 */334 189 335 190 private: 336 191 // copy assignment no allowed 337 192 const VectorBase& operator=(const VectorBase&); 338 public:339 /**340 */341 //operator proxy();342 343 193 }; 344 194 … … 397 247 */ 398 248 bool nan(const VectorBase& templat, vector& flag); 399 400 /**401 Randomly shuffles the elements in VectorBase \a invec402 */403 void shuffle(VectorBase& invec);404 405 /**406 Sort the elements in the VectorBase.407 */408 void sort(VectorBase&);409 249 410 250 /** … … 442 282 443 283 /** 444 \brief Swap vector elements by copying.445 446 The two vectorMutables must have the same length.447 448 \throw GSL_error if vectorMutable lengths differs.449 */450 //void swap(vectorMutable&, vectorMutable&);451 452 /**453 284 \brief The output operator for the VectorBase class. 454 285 */ -
trunk/yat/utility/VectorConstView.cc
r1026 r1027 25 25 */ 26 26 27 #include "Vector View.h"27 #include "VectorConstView.h" 28 28 #include "matrix.h" 29 29 #include "utility.h" … … 43 43 44 44 45 Vector View::VectorView(void)46 : VectorBase( ), view_(NULL), const_view_(NULL)45 VectorConstView::VectorConstView(const VectorConstView& other) 46 : VectorBase(other.gsl_vector_p()), const_view_(NULL) 47 47 { 48 48 } 49 49 50 50 51 Vector View::VectorView(VectorView& other)52 : VectorBase( ), const_view_(NULL)51 VectorConstView::VectorConstView(const VectorBase& other) 52 : VectorBase(other.gsl_vector_p()), const_view_(NULL) 53 53 { 54 view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),55 0, other.size()));56 const_vec_ = vec_ = &(view_->vector);57 58 54 } 59 55 60 56 61 VectorView::VectorView(const VectorView& other) 62 : VectorBase(), view_(NULL) 63 { 64 const_view_ = new gsl_vector_const_view( 65 gsl_vector_const_subvector(other.gsl_vector_p(), 0, other.size())); 66 const_vec_ = &(const_view_->vector); 67 } 68 69 70 VectorView::VectorView(VectorBase& other) 71 : VectorBase(other.gsl_vector_p()), const_view_(NULL) 72 { 73 view_ = 74 new gsl_vector_view(gsl_vector_subvector_with_stride(other.gsl_vector_p(), 75 0, 1,other.size())); 76 const_vec_ = vec_ = &(view_->vector); 77 } 78 79 80 /* 81 VectorView::VectorView(const VectorBase& other) 82 : VectorBase(other.const_vec_), view_(NULL) 83 { 84 } 85 */ 86 87 VectorView::VectorView(VectorBase& v, size_t offset,size_t n,size_t stride) 88 : VectorBase(), const_view_(NULL) 89 { 90 view_ = 91 new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(), 92 offset, stride,n)); 93 const_vec_ = vec_ = &(view_->vector); 94 } 95 96 97 VectorView::VectorView(const VectorBase& v, size_t offset,size_t n, 98 size_t stride) 99 : VectorBase(), view_(NULL) 57 VectorConstView::VectorConstView(const VectorBase& v, size_t offset,size_t n, 58 size_t stride) 59 : VectorBase() 100 60 { 101 61 const_view_ = new gsl_vector_const_view( … … 106 66 107 67 108 Vector View::VectorView(matrix& m, size_t i, bool row)68 VectorConstView::VectorConstView(const matrix& m, size_t i, bool row) 109 69 : VectorBase(), const_view_(NULL) 110 {111 view_=new gsl_vector_view(row ?112 gsl_matrix_row (m.gsl_matrix_p(),i) :113 gsl_matrix_column(m.gsl_matrix_p(),i) );114 const_vec_ = vec_ = &(view_->vector);115 }116 117 118 VectorView::VectorView(const matrix& m, size_t i, bool row)119 : VectorBase(), view_(NULL)120 70 { 121 71 const_view_ = … … 127 77 128 78 129 /* 130 VectorView::VectorView(proxy p) 131 : VectorBase() 132 { 133 view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_, 0, p.vec_->size)); 134 const_vec_ = vec_ = &(view_->vector); 135 } 136 */ 137 138 VectorView::~VectorView(void) 79 VectorConstView::~VectorConstView(void) 139 80 { 140 81 delete_allocated_memory(); … … 142 83 143 84 144 const VectorView& VectorView::assign(const VectorBase& other ) 145 { 146 if (size()!=other.size()) 147 throw utility::GSL_error("VectorView::dimension mis-match"); 148 if (!other.size()) 149 return *this; 150 gsl_vector_memcpy(vec_, other.gsl_vector_p()); 151 const_vec_ = vec_; 152 return *this; 153 } 154 155 156 bool VectorView::isview(void) const 85 bool VectorConstView::isview(void) const 157 86 { 158 87 return true; … … 161 90 162 91 163 const VectorView& VectorView::operator=(const VectorView& other)92 void VectorConstView::delete_allocated_memory(void) 164 93 { 165 return assign(other); 166 } 167 168 169 const VectorView& VectorView::operator=(const VectorBase& other ) 170 { 171 return assign(other); 172 } 173 174 175 /* 176 const VectorView& VectorView::operator=(proxy p) 177 { 178 if (size()!=p.vec_->size) 179 throw utility::GSL_error("VectorView::dimension mis-match"); 180 if (!size()) 181 return *this; 182 clean_up(); 183 view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_,0, p.vec_->size)); 184 const_vec_=vec_ = &view_->vector; 185 return *this; 186 } 187 */ 188 189 void VectorView::delete_allocated_memory(void) 190 { 191 if (view_){ 192 delete view_; 193 view_=NULL; 194 } 195 else if (const_view_){ 94 if (const_view_){ 196 95 delete const_view_; 197 96 const_view_=NULL; 198 97 } 199 const_vec_ = vec_ =NULL;98 const_vec_ = NULL; 200 99 } 201 100 -
trunk/yat/utility/VectorConstView.h
r1026 r1027 1 #ifndef _theplu_yat_utility_vector_ view_2 #define _theplu_yat_utility_vector_ view_1 #ifndef _theplu_yat_utility_vector_const_view_ 2 #define _theplu_yat_utility_vector_const_view_ 3 3 4 4 // $Id$ … … 85 85 */ 86 86 87 class Vector View : public VectorBase87 class VectorConstView : public VectorBase 88 88 { 89 89 public: 90 90 /** 91 \brief Default constructor. 92 */ 93 VectorView(void); 91 \brief The copy constructor. 92 */ 93 VectorConstView(const VectorBase& other); 94 VectorConstView(const VectorConstView& other); 94 95 95 96 /** 96 \brief The copy constructor. 97 */ 98 VectorView(VectorBase& other); 99 VectorView(VectorView& other); 100 // Peter, privatize 101 VectorView(const VectorView& other); 97 \brief VectorConstView constructor. 102 98 103 /** 104 \brief VectorView constructor. 105 106 Create a view of VectorView \a v, with starting index \a offset, 99 Create a view of VectorConstView \a v, with starting index \a offset, 107 100 size \a n, and an optional \a stride. 108 101 109 A Vector View view can be used as any VectorView with the difference102 A VectorConstView view can be used as any VectorConstView with the difference 110 103 that changes made to the view will also change the object that 111 104 is viewed. Also, using the copy constructor will create a new 112 Vector View object that is a copy of whatever is viewed. If a copy105 VectorConstView object that is a copy of whatever is viewed. If a copy 113 106 of the view is needed then you should use this constructor to 114 107 obtain a copy. … … 120 113 \throw GSL_error if a view cannot be set up. 121 114 */ 122 VectorView(VectorBase& v, size_t offset, size_t n, size_t stride=1); 123 // Peter, privatize 124 VectorView(const VectorBase& v, size_t offset, size_t n, size_t stride=1); 115 VectorConstView(const VectorBase& v,size_t offset,size_t n,size_t stride=1); 125 116 126 117 /// 127 118 /// Matrix row/column view constructor. 128 119 /// 129 /// Create a row/column Vector View view of matrix \a m, pointing at120 /// Create a row/column VectorConstView view of matrix \a m, pointing at 130 121 /// row/column \a i. The parameter \a row is used to set whether 131 122 /// the view should be a row or column view. If \a row is set to … … 133 124 /// naturally, a column view otherwise. 134 125 /// 135 /// A Vector View view can be used as any VectorView with the difference126 /// A VectorConstView view can be used as any VectorConstView with the difference 136 127 /// that changes made to the view will also change the object that 137 128 /// is viewed. Also, using the copy constructor will create a new 138 /// Vector View object that is a copy of whatever is viewed. If a copy139 /// of the view is needed then you should use the Vector View view129 /// VectorConstView object that is a copy of whatever is viewed. If a copy 130 /// of the view is needed then you should use the VectorConstView view 140 131 /// constructor to obtain a copy. 141 132 /// … … 144 135 /// use is undefined. 145 136 /// 146 VectorView(matrix& m, size_t i, bool row=true); 147 148 // Peter should be private 149 VectorView(const matrix& m, size_t i, bool row=true); 150 151 /** 152 */ 153 //VectorView(proxy p); 137 VectorConstView(const matrix& m, size_t i, bool row=true); 154 138 155 139 /// 156 140 /// The destructor. 157 141 /// 158 ~Vector View(void);142 ~VectorConstView(void); 159 143 160 144 /** … … 170 154 \throw GSL_error if dimensions mis-match. 171 155 */ 172 const VectorView& operator=(const VectorBase&); 173 const VectorView& operator=(const VectorView&); 174 175 //const VectorBase& operator=(proxy); 156 const VectorConstView& operator=(const VectorBase&); 157 const VectorConstView& operator=(const VectorConstView&); 176 158 177 159 private: 178 const Vector View& assign(const VectorBase& other);160 const VectorConstView& assign(const VectorBase& other); 179 161 void delete_allocated_memory(void); 180 162 181 gsl_vector_view* view_;182 163 gsl_vector_const_view* const_view_; 183 164 -
trunk/yat/utility/VectorMutable.cc
r1026 r1027 25 25 */ 26 26 27 #include "Vector Base.h"27 #include "VectorMutable.h" 28 28 #include "matrix.h" 29 29 #include "utility.h" … … 43 43 44 44 45 Vector Base::VectorBase(void)46 : vec_(NULL), const_vec_(NULL)45 VectorMutable::VectorMutable(void) 46 : VectorBase(NULL), vec_(NULL) 47 47 { 48 48 } 49 49 50 50 51 Vector Base::VectorBase(gsl_vector* v)52 : vec_(v), const_vec_(v)51 VectorMutable::VectorMutable(gsl_vector* v) 52 : VectorBase(v), vec_(v) 53 53 { 54 54 } 55 55 56 56 57 Vector Base::VectorBase(const gsl_vector* v)58 : vec_(NULL), const_vec_(v)57 VectorMutable::VectorMutable(const gsl_vector* v) 58 : VectorBase(v), vec_(NULL) 59 59 { 60 60 } 61 61 62 62 63 Vector Base::~VectorBase(void)63 VectorMutable::~VectorMutable(void) 64 64 { 65 65 } 66 66 67 67 68 void Vector Base::all(double value)68 void VectorMutable::all(double value) 69 69 { 70 70 assert(vec_); … … 73 73 74 74 75 VectorBase::const_iterator VectorBase::begin(void) const 76 { 77 return const_iterator(*this, 0); 78 } 79 80 81 VectorBase::iterator VectorBase::begin(void) 75 VectorMutable::iterator VectorMutable::begin(void) 82 76 { 83 77 return iterator(*this, 0); … … 85 79 86 80 87 void Vector Base::div(const VectorBase& other)81 void VectorMutable::div(const VectorBase& other) 88 82 { 89 83 assert(vec_); 90 84 int status=gsl_vector_div(vec_,other.gsl_vector_p()); 91 85 if (status) 92 throw utility::GSL_error(std::string("Vector Base::div",status));86 throw utility::GSL_error(std::string("VectorMutable::div",status)); 93 87 } 94 88 95 89 96 VectorBase::const_iterator VectorBase::end(void) const 97 { 98 return const_iterator(*this, size()); 99 } 100 101 102 VectorBase::iterator VectorBase::end(void) 90 VectorMutable::iterator VectorMutable::end(void) 103 91 { 104 92 return iterator(*this, size()); … … 106 94 107 95 108 bool VectorBase::equal(const VectorBase& other, const double d) const 109 { 110 if (this==&other) 111 return true; 112 if (size()!=other.size()) 113 return false; 114 // if gsl error handler disabled, out of bounds index will not 115 // abort the program. 116 for (size_t i=0; i<size(); ++i) 117 if (fabs( (*this)(i)-other(i) ) > d || 118 std::isnan((*this)(i)) || std::isnan(other(i)) ) 119 return false; 120 return true; 121 } 122 123 124 const gsl_vector* VectorBase::gsl_vector_p(void) const 125 { 126 return const_vec_; 127 } 128 129 130 gsl_vector* VectorBase::gsl_vector_p(void) 96 gsl_vector* VectorMutable::gsl_vector_p(void) 131 97 { 132 98 return vec_; … … 134 100 135 101 136 void Vector Base::mul(const VectorBase& other)102 void VectorMutable::mul(const VectorBase& other) 137 103 { 138 104 assert(vec_); 139 105 int status=gsl_vector_mul(vec_,other.gsl_vector_p()); 140 106 if (status) 141 throw utility::GSL_error(std::string("Vector Base::div",status));107 throw utility::GSL_error(std::string("VectorMutable::div",status)); 142 108 } 143 109 144 110 145 size_t VectorBase::size(void) const111 double& VectorMutable::operator()(size_t i) 146 112 { 147 if (!const_vec_) 148 return 0; 149 return const_vec_->size; 150 } 151 152 153 const double& VectorBase::operator()(size_t i) const 154 { 155 const double* d=gsl_vector_const_ptr(const_vec_, i); 113 double* d=gsl_vector_ptr(vec_, i); 156 114 if (!d) 157 throw utility::GSL_error("Vector Base::operator()",GSL_EINVAL);115 throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL); 158 116 return *d; 159 117 } 160 118 161 119 162 double& VectorBase::operator()(size_t i) 163 { 164 double* d=gsl_vector_ptr(vec_, i); 165 if (!d) 166 throw utility::GSL_error("VectorBase::operator()",GSL_EINVAL); 167 return *d; 168 } 169 170 171 double& VectorBase::operator[](size_t i) 120 double& VectorMutable::operator[](size_t i) 172 121 { 173 122 return this->operator()(i); … … 175 124 176 125 177 const double& VectorBase::operator[](size_t i) const 178 { 179 return this->operator()(i); 180 } 181 182 183 bool VectorBase::operator==(const VectorBase& other) const 184 { 185 return equal(other); 186 } 187 188 189 bool VectorBase::operator!=(const VectorBase& other) const 190 { 191 return !equal(other); 192 } 193 194 195 double VectorBase::operator*( const VectorBase &other ) const 196 { 197 double res = 0.0;; 198 for ( size_t i = 0; i < size(); ++i ) 199 res += other(i) * (*this)(i); 200 return res; 201 } 202 203 204 const VectorBase& VectorBase::operator+=(double d) 126 const VectorMutable& VectorMutable::operator+=(double d) 205 127 { 206 128 assert(vec_); … … 210 132 211 133 212 const Vector Base& VectorBase::operator-=(const VectorBase& other)134 const VectorMutable& VectorMutable::operator-=(const VectorBase& other) 213 135 { 214 136 assert(vec_); 215 137 int status=gsl_vector_sub(vec_, other.gsl_vector_p()); 216 138 if (status) 217 throw utility::GSL_error(std::string("Vector Base::sub", status));139 throw utility::GSL_error(std::string("VectorMutable::sub", status)); 218 140 return *this; 219 141 } 220 142 221 143 222 const Vector Base& VectorBase::operator-=(const double d)144 const VectorMutable& VectorMutable::operator-=(const double d) 223 145 { 224 146 assert(vec_); … … 228 150 229 151 230 const Vector Base& VectorBase::operator*=(double d)152 const VectorMutable& VectorMutable::operator*=(double d) 231 153 { 232 154 assert(vec_); … … 236 158 237 159 238 bool isnull(const VectorBase& v)160 void shuffle(VectorMutable& invec) 239 161 { 240 r eturn gsl_vector_isnull(v.gsl_vector_p());162 random::random_shuffle(invec.begin(), invec.end()); 241 163 } 242 164 243 165 244 double max(const VectorBase& v)166 void sort(VectorMutable& invec) 245 167 { 246 return gsl_vector_max(v.gsl_vector_p());168 std::sort(invec.begin(), invec.end()); 247 169 } 248 170 249 171 250 size_t max_index(const VectorBase& v)172 VectorMutable::operator proxy() 251 173 { 252 return gsl_vector_max_index(v.gsl_vector_p()); 253 } 254 255 256 double min(const VectorBase& v) 257 { 258 return gsl_vector_min(v.gsl_vector_p()); 259 } 260 261 262 size_t min_index(const VectorBase& v) 263 { 264 return gsl_vector_min_index(v.gsl_vector_p()); 265 } 266 267 268 bool nan(const VectorBase& templat, vector& flag) 269 { 270 size_t vsize(templat.size()); 271 flag = vector(vsize, 1.0); 272 bool nan=false; 273 for (size_t i=0; i<vsize; i++) 274 if (std::isnan(templat(i))) { 275 flag(i)=0; 276 nan=true; 277 } 278 return nan; 279 } 280 281 282 void shuffle(VectorBase& invec) 283 { 284 random::DiscreteUniform rnd; 285 std::random_shuffle(invec.begin(), invec.end(), rnd); 286 } 287 288 289 void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec) 290 { 291 assert(invec.gsl_vector_p()); 292 gsl_permutation* p = gsl_permutation_alloc(invec.size()); 293 int status=gsl_sort_vector_index(p,invec.gsl_vector_p()); 294 if (status) { 295 gsl_permutation_free(p); 296 throw utility::GSL_error(std::string("sort_index(vector&,const VectorBase&)",status)); 297 } 298 sort_index=std::vector<size_t>(p->data,p->data+p->size); 299 gsl_permutation_free(p); 300 } 301 302 303 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, 304 const VectorBase& invec) 305 { 306 assert(invec.gsl_vector_p()); 307 assert(k<=invec.size()); 308 sort_index.resize(k); 309 gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p()); 310 } 311 312 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, 313 const VectorBase& invec) 314 { 315 assert(invec.gsl_vector_p()); 316 assert(k<=invec.size()); 317 sort_index.resize(k); 318 gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p()); 319 } 320 321 322 double sum(const VectorBase& v) 323 { 324 double sum = 0; 325 size_t vsize=v.size(); 326 for (size_t i=0; i<vsize; ++i) 327 sum += v(i); 328 return sum; 329 } 330 331 332 std::ostream& operator<<(std::ostream& s, const VectorBase& a) 333 { 334 s.setf(std::ios::dec); 335 s.precision(12); 336 for (size_t j = 0; j < a.size(); ++j) { 337 s << a(j); 338 if ( (j+1)<a.size() ) 339 s << s.fill(); 340 } 341 return s; 174 proxy p; 175 p.vec_ = vec_; 176 vec_ = NULL; // proxy takes ownership and delivers to its receiver 177 return p; 342 178 } 343 179 -
trunk/yat/utility/VectorMutable.h
r1026 r1027 1 #ifndef _theplu_yat_utility_vector_ base_2 #define _theplu_yat_utility_vector_ base_1 #ifndef _theplu_yat_utility_vector_mutable_ 2 #define _theplu_yat_utility_vector_mutable_ 3 3 4 4 // $Id$ … … 30 30 */ 31 31 32 #include "VectorBase.h" 32 33 #include "Exception.h" 33 34 #include "Iterator.h" … … 86 87 */ 87 88 88 class Vector Base89 class VectorMutable : public VectorBase 89 90 { 90 91 public: 91 92 /// \brief VectorBase::iterator 93 typedef Iterator<double&, VectorBase> iterator; 94 95 /// \brief VectorBase::const_iterator 96 typedef Iterator<const double, const VectorBase> const_iterator; 92 /** 93 \brief mutable iterator 94 */ 95 typedef Iterator<double&, VectorMutable> iterator; 97 96 98 97 /** 99 98 \brief default constructor 100 99 */ 101 Vector Base(void);100 VectorMutable(void); 102 101 103 102 /** 104 103 \brief Constructor. 105 104 */ 106 Vector Base(gsl_vector*);105 VectorMutable(gsl_vector*); 107 106 108 107 /** 109 108 \brief Constructor. 110 109 */ 111 Vector Base(const gsl_vector*);110 VectorMutable(const gsl_vector*); 112 111 113 112 /// 114 113 /// The destructor. 115 114 /// 116 virtual ~Vector Base(void);115 virtual ~VectorMutable(void); 117 116 118 117 /// … … 126 125 iterator begin(void); 127 126 128 /** 129 \return read-only iterator to start of VectorBase 130 */ 131 const_iterator begin(void) const; 132 133 /** 134 \brief This function performs element-wise division, \f$ this_i = 135 this_i/other_i \; \forall i \f$. 127 // to allow overload from base class 128 using VectorBase::begin; 129 130 /** 131 \brief This function performs element-wise division, \f$ 132 this_i = this_i / other_i \; \forall i \f$. 136 133 137 134 \throw GSL_error if dimensions mis-match. … … 140 137 141 138 /** 142 \return mutable iterator to end of Vector Base139 \return mutable iterator to end of VectorMutable 143 140 */ 144 141 iterator end(void); 145 142 146 /** 147 \return read-only iterator to end of VectorBase 148 */ 149 const_iterator end(void) const; 150 151 /** 152 \brief Check whether VectorBases are equal within a user defined 153 precision, set by \a precision. 154 155 \return True if each element deviates less or equal than \a 156 d. If any VectorBase contain a NaN, false is always returned. 157 158 \see operator== and operator!= 159 */ 160 bool equal(const VectorBase&, const double precision=0) const; 161 162 /// 163 /// @return A pointer to the internal GSL vector, 164 /// 143 // to allow overload from base class 144 using VectorBase::end; 145 146 /** 147 @return A pointer to the internal GSL vector, 148 */ 165 149 gsl_vector* gsl_vector_p(void); 166 150 167 /// 168 /// @return A const pointer to the internal GSL vector, 169 /// 170 const gsl_vector* gsl_vector_p(void) const; 171 172 /** 173 151 using VectorBase::gsl_vector_p; 152 153 /** 174 154 Check if the vector object is a view (sub-vector) to another 175 155 vector. … … 189 169 190 170 /** 191 \brief Reverse the order of elements in the Vector Base.171 \brief Reverse the order of elements in the VectorMutable. 192 172 */ 193 173 void reverse(void); 194 174 195 ///196 /// @return the number of elements in the VectorBase.197 ///198 size_t size(void) const;199 200 175 /** 201 176 \brief Exchange elements \a i and \a j. 202 203 \throw GSL_error if VectorBase lengths differs.204 177 */ 205 178 void swap(size_t i, size_t j); … … 217 190 // Peter, remove this one 218 191 double& operator[](size_t i); 219 220 /** 221 \brief Element access operator. 222 223 \return Const reference to element \a i. 224 225 \throw If GSL range checks are enabled in the underlying GSL 226 library a GSL_error exception is thrown if either index is out 227 of range. 228 */ 229 const double& operator()(size_t i) const; 230 // Peter, remove this one 231 const double& operator[](size_t i) const; 232 233 /** 234 \brief Comparison operator. Takes linear time. 235 236 Checks are performed with exact matching, i.e., rounding off 237 effects may destroy comparison. Use the equal function for 238 comparing elements within a user defined precision. 239 240 \return True if all elements are equal otherwise false. 241 242 \see equal(const VectorBase&, const double precision=0) 243 */ 244 bool operator==(const VectorBase&) const; 245 246 /** 247 \brief Comparison operator. Takes linear time. 248 249 Checks are performed with exact matching, i.e., rounding off 250 effects may destroy comparison. Use the equal function for 251 comparing elements within a user defined precision. 252 253 \return False if all elements are equal otherwise true. 254 255 \see equal(const VectorBase&, const double precision=0) 256 */ 257 bool operator!=(const VectorBase&) const; 258 259 /// 260 /// @return The dot product. 261 /// 262 double operator*(const VectorBase&) const; 263 264 /** 265 \brief The assignment operator. 266 267 Dimensions of the VectorBases must match. If the LHS VectorBase is a 268 view, the underlying data will be changed. 269 270 \return A const reference to the resulting VectorBase. 271 272 \see void set(const VectorBase&). 273 274 \throw GSL_error if dimensions mis-match. 275 */ 276 //const VectorBase& operator=(VectorBase&); 192 // to allow overload from base class 193 using VectorBase::operator(); 194 using VectorBase::operator[]; 277 195 278 196 /** … … 284 202 \throw GSL_error if dimensions mis-match. 285 203 */ 286 const Vector Base& operator+=(const VectorBase&);204 const VectorMutable& operator+=(const VectorBase&); 287 205 288 206 /** … … 292 210 \return A const reference to the resulting VectorBase. 293 211 */ 294 const Vector Base& operator+=(double d);212 const VectorMutable& operator+=(double d); 295 213 296 214 /** … … 302 220 \throw GSL_error if dimensions mis-match. 303 221 */ 304 const Vector Base& operator-=(const VectorBase&);222 const VectorMutable& operator-=(const VectorBase&); 305 223 306 224 /** … … 310 228 \return A const reference to the resulting VectorBase. 311 229 */ 312 const Vector Base& operator-=(double d);230 const VectorMutable& operator-=(double d); 313 231 314 232 /** … … 318 236 \return A const reference to the resulting VectorBase. 319 237 */ 320 const VectorBase& operator*=(double d); 321 322 238 const VectorMutable& operator*=(double d); 323 239 324 240 protected: 325 241 gsl_vector* vec_; 326 const gsl_vector* const_vec_; 327 328 /* 242 243 // Peter document 329 244 struct proxy 330 245 { 331 246 gsl_vector* vec_; 332 247 }; 333 */334 248 335 249 private: 336 250 // copy assignment no allowed 337 const Vector Base& operator=(const VectorBase&);251 const VectorMutable& operator=(const VectorMutable&); 338 252 public: 339 253 /** 340 254 */ 341 //operator proxy();255 operator proxy(); 342 256 343 257 }; 344 345 /**346 \brief Check if all elements of the VectorBase are zero.347 348 \return True if all elements in the VectorBase is zero, false349 othwerwise.350 */351 bool isnull(const VectorBase&);352 353 /**354 \brief Get the maximum value of the VectorBase.355 356 \return The maximum value of the VectorBase.357 */358 double max(const VectorBase&);359 360 /**361 \brief Locate the maximum value in the VectorBase.362 363 \return The index to the maximum value of the VectorBase.364 365 \note Lower index has precedence.366 */367 size_t max_index(const VectorBase&);368 369 /**370 \brief Get the minimum value of the VectorBase.371 372 \return The minimum value of the VectorBase.373 */374 double min(const VectorBase&);375 376 /**377 \brief Locate the minimum value in the VectorBase.378 379 \return The index to the minimum value of the VectorBase.380 381 \note Lower index has precedence.382 */383 size_t min_index(const VectorBase&);384 385 /**386 \brief Create a VectorBase \a flag indicating NaN's in another VectorBase387 \a templat.388 389 The \a flag VectorBase is changed to contain 1's and 0's only. A 1390 means that the corresponding element in the \a templat VectorBase is391 valid and a zero means that the corresponding element is a NaN.392 393 \note Space for VectorBase \a flag is reallocated to fit the size of394 VectorBase \a templat if sizes mismatch.395 396 \return True if the \a templat VectorBase contains at least one NaN.397 */398 bool nan(const VectorBase& templat, vector& flag);399 258 400 259 /** 401 260 Randomly shuffles the elements in VectorBase \a invec 402 261 */ 403 void shuffle(Vector Base& invec);262 void shuffle(VectorMutable& invec); 404 263 405 264 /** 406 265 Sort the elements in the VectorBase. 407 266 */ 408 void sort(VectorBase&); 409 410 /** 411 Create a vector \a sort_index containing the indeces of 412 elements in a another VectorBase \a invec. The elements of \a 413 sort_index give the index of the VectorBase element which would 414 have been stored in that position if the VectorBase had been sorted 415 in place. The first element of \a sort_index gives the index of the least 416 element in \a invec, and the last element of \a sort_index gives the 417 index of the greatest element in \a invec . The VectorBase \a invec 418 is not changed. 419 */ 420 void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec); 421 422 /** 423 Similar to sort_index but creates a VectorBase with indices to the \a k 424 smallest elements in \a invec. 425 */ 426 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, 427 const VectorBase& invec); 428 429 /** 430 Similar to sort_index but creates a VectorBase with indices to the \a k 431 largest elements in \a invec. 432 */ 433 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, 434 const VectorBase& invec); 435 436 /** 437 \brief Calculate the sum of all VectorBase elements. 438 439 \return The sum. 440 */ 441 double sum(const VectorBase&); 442 443 /** 444 \brief Swap vector elements by copying. 445 446 The two vectorMutables must have the same length. 447 448 \throw GSL_error if vectorMutable lengths differs. 449 */ 450 //void swap(vectorMutable&, vectorMutable&); 451 452 /** 453 \brief The output operator for the VectorBase class. 454 */ 455 std::ostream& operator<<(std::ostream&, const VectorBase&); 267 void sort(VectorMutable&); 456 268 457 269 }}} // of namespace utility, yat, and theplu -
trunk/yat/utility/VectorView.cc
r1026 r1027 26 26 27 27 #include "VectorView.h" 28 #include "VectorMutable.h" 28 29 #include "matrix.h" 29 30 #include "utility.h" … … 44 45 45 46 VectorView::VectorView(void) 46 : Vector Base(), view_(NULL), const_view_(NULL)47 : VectorMutable(), view_(NULL) 47 48 { 48 49 } … … 50 51 51 52 VectorView::VectorView(VectorView& other) 52 : Vector Base(), const_view_(NULL)53 : VectorMutable() 53 54 { 54 55 view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(), … … 59 60 60 61 61 VectorView::VectorView( const VectorView& other)62 : Vector Base(), view_(NULL)62 VectorView::VectorView(VectorMutable& other) 63 : VectorMutable() 63 64 { 64 const_view_ = new gsl_vector_const_view( 65 gsl_vector_const_subvector(other.gsl_vector_p(), 0, other.size())); 66 const_vec_ = &(const_view_->vector); 67 } 68 69 70 VectorView::VectorView(VectorBase& other) 71 : VectorBase(other.gsl_vector_p()), const_view_(NULL) 72 { 73 view_ = 74 new gsl_vector_view(gsl_vector_subvector_with_stride(other.gsl_vector_p(), 75 0, 1,other.size())); 65 view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(), 66 0,other.size())); 76 67 const_vec_ = vec_ = &(view_->vector); 77 68 } 78 69 79 70 80 /* 81 VectorView::VectorView(const VectorBase& other) 82 : VectorBase(other.const_vec_), view_(NULL) 83 { 84 } 85 */ 86 87 VectorView::VectorView(VectorBase& v, size_t offset,size_t n,size_t stride) 88 : VectorBase(), const_view_(NULL) 71 VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride) 72 : VectorMutable() 89 73 { 90 74 view_ = … … 95 79 96 80 97 VectorView::VectorView(const VectorBase& v, size_t offset,size_t n,98 size_t stride)99 : VectorBase(), view_(NULL)100 {101 const_view_ = new gsl_vector_const_view(102 gsl_vector_const_subvector_with_stride(v.gsl_vector_p(),103 offset, stride,n));104 const_vec_ = &(const_view_->vector);105 }106 107 108 81 VectorView::VectorView(matrix& m, size_t i, bool row) 109 : Vector Base(), const_view_(NULL)82 : VectorMutable() 110 83 { 111 84 view_=new gsl_vector_view(row ? … … 116 89 117 90 118 VectorView::VectorView( const matrix& m, size_t i, bool row)119 : Vector Base(), view_(NULL)91 VectorView::VectorView(proxy p) 92 : VectorMutable(p.vec_), view_(NULL) 120 93 { 121 const_view_ =122 new gsl_vector_const_view(row ?123 gsl_matrix_const_row(m.gsl_matrix_p(),i) :124 gsl_matrix_const_column(m.gsl_matrix_p(),i));125 const_vec_ = &(const_view_->vector);126 94 } 127 95 128 129 /*130 VectorView::VectorView(proxy p)131 : VectorBase()132 {133 view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_, 0, p.vec_->size));134 const_vec_ = vec_ = &(view_->vector);135 }136 */137 96 138 97 VectorView::~VectorView(void) … … 173 132 174 133 175 /*176 const VectorView& VectorView::operator=(proxy p)177 {178 if (size()!=p.vec_->size)179 throw utility::GSL_error("VectorView::dimension mis-match");180 if (!size())181 return *this;182 clean_up();183 view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_,0, p.vec_->size));184 const_vec_=vec_ = &view_->vector;185 return *this;186 }187 */188 189 134 void VectorView::delete_allocated_memory(void) 190 135 { … … 193 138 view_=NULL; 194 139 } 195 else if (const_view_){196 delete const_view_;197 const_view_=NULL;198 }199 140 const_vec_ = vec_ = NULL; 200 141 } -
trunk/yat/utility/VectorView.h
r1026 r1027 29 29 */ 30 30 31 #include "Vector Base.h"31 #include "VectorMutable.h" 32 32 #include "Exception.h" 33 33 … … 85 85 */ 86 86 87 class VectorView : public Vector Base87 class VectorView : public VectorMutable 88 88 { 89 89 public: … … 96 96 \brief The copy constructor. 97 97 */ 98 VectorView(Vector Base& other);98 VectorView(VectorMutable& other); 99 99 VectorView(VectorView& other); 100 // Peter, privatize101 VectorView(const VectorView& other);102 100 103 101 /** … … 120 118 \throw GSL_error if a view cannot be set up. 121 119 */ 122 VectorView(VectorBase& v, size_t offset, size_t n, size_t stride=1); 123 // Peter, privatize 124 VectorView(const VectorBase& v, size_t offset, size_t n, size_t stride=1); 120 VectorView(VectorMutable& v, size_t offset, size_t n, size_t stride=1); 125 121 126 122 /// … … 146 142 VectorView(matrix& m, size_t i, bool row=true); 147 143 148 // Peter should be private149 VectorView(const matrix& m, size_t i, bool row=true);150 151 144 /** 152 145 */ 153 //VectorView(proxy p);146 VectorView(proxy p); 154 147 155 148 /// … … 173 166 const VectorView& operator=(const VectorView&); 174 167 175 //const VectorBase& operator=(proxy);176 177 168 private: 178 169 const VectorView& assign(const VectorBase& other); -
trunk/yat/utility/matrix.cc
r1017 r1027 28 28 #include "vector.h" 29 29 #include "VectorBase.h" 30 #include "VectorConstView.h" 30 31 #include "VectorView.h" 31 32 #include "utility.h" … … 311 312 312 313 313 const Vector View matrix::column_vec(size_t col) const314 { 315 return Vector View(*this, col, false);316 } 317 318 319 const Vector View matrix::row_vec(size_t col) const320 { 321 return Vector View(*this, col, true);314 const VectorConstView matrix::column_const_vec(size_t col) const 315 { 316 return VectorConstView(*this, col, false); 317 } 318 319 320 const VectorConstView matrix::row_const_vec(size_t col) const 321 { 322 return VectorConstView(*this, col, true); 322 323 } 323 324 … … 563 564 utility::vector res(m.rows()); 564 565 for (size_t i=0; i<res.size(); ++i) 565 res(i) = Vector View(m,i) * v;566 res(i) = VectorConstView(m,i) * v; 566 567 return res; 567 568 } … … 572 573 utility::vector res(m.columns()); 573 574 for (size_t i=0; i<res.size(); ++i) 574 res(i) = v * Vector View(m,i,false);575 res(i) = v * VectorConstView(m,i,false); 575 576 return res; 576 577 } -
trunk/yat/utility/matrix.h
r1017 r1027 29 29 30 30 #include "vector.h" 31 #include "VectorConstView.h" 31 32 #include "VectorView.h" 32 33 #include "Exception.h" … … 161 162 /** 162 163 */ 163 const Vector View column_vec(size_t) const;164 const VectorConstView column_const_vec(size_t) const; 164 165 165 166 /// … … 236 237 /** 237 238 */ 238 const Vector View row_vec(size_t) const;239 const VectorConstView row_const_vec(size_t) const; 239 240 240 241 /** -
trunk/yat/utility/vector.cc
r1026 r1027 44 44 45 45 vector::vector(void) 46 : Vector Base()46 : VectorMutable() 47 47 { 48 48 } … … 50 50 51 51 vector::vector(size_t n, double init_value) 52 : Vector Base(gsl_vector_alloc(n))52 : VectorMutable(gsl_vector_alloc(n)) 53 53 { 54 54 if (!vec_) … … 60 60 61 61 vector::vector(const vector& other) 62 : Vector Base(create_gsl_vector_copy(other))62 : VectorMutable(create_gsl_vector_copy(other)) 63 63 { 64 64 } … … 66 66 67 67 vector::vector(const VectorBase& other) 68 : Vector Base(create_gsl_vector_copy(other))68 : VectorMutable(create_gsl_vector_copy(other)) 69 69 { 70 70 } … … 73 73 vector::vector(std::istream& is, char sep) 74 74 throw (utility::IO_error, std::exception) 75 : Vector Base()75 : VectorMutable() 76 76 { 77 77 // read the data file and store in stl vectors (dynamically -
trunk/yat/utility/vector.h
r1026 r1027 29 29 */ 30 30 31 #include "Vector Base.h"31 #include "VectorMutable.h" 32 32 #include "Exception.h" 33 #include "Iterator.h"34 33 35 34 #include <iostream> … … 85 84 */ 86 85 87 class vector : public Vector Base86 class vector : public VectorMutable 88 87 { 89 88 public:
Note: See TracChangeset
for help on using the changeset viewer.