File indexing completed on 2025-01-18 09:43:13
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_UBLAS_MATRIX_VECTOR_HPP
0010 #define BOOST_UBLAS_MATRIX_VECTOR_HPP
0011
0012 #include <boost/numeric/ublas/matrix_proxy.hpp> //for matrix_row, matrix_column and matrix_expression
0013 #include <boost/numeric/ublas/vector.hpp>
0014 #include <boost/iterator/iterator_facade.hpp>
0015 #include <boost/range/iterator_range.hpp>
0016 #include <boost/type_traits/is_convertible.hpp>
0017 #include <boost/utility/enable_if.hpp>
0018
0019 namespace boost { namespace numeric { namespace ublas {
0020
0021 namespace detail{
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 template<class Matrix, class Reference>
0036 class matrix_vector_iterator: public boost::iterator_facade<
0037 matrix_vector_iterator<Matrix,Reference>,
0038 typename vector_temporary_traits<Reference>::type,
0039 boost::random_access_traversal_tag,
0040 Reference
0041 >{
0042 public:
0043 matrix_vector_iterator(){}
0044
0045
0046 BOOST_UBLAS_INLINE
0047 matrix_vector_iterator(Matrix& matrix, std::size_t position)
0048 : matrix_(&matrix),position_(position) {}
0049
0050 template<class M, class R>
0051 BOOST_UBLAS_INLINE
0052 matrix_vector_iterator(matrix_vector_iterator<M,R> const& other)
0053 : matrix_(other.matrix_),position_(other.position_) {}
0054
0055 private:
0056 friend class boost::iterator_core_access;
0057 template <class M,class R> friend class matrix_vector_iterator;
0058
0059 BOOST_UBLAS_INLINE
0060 void increment() {
0061 ++position_;
0062 }
0063
0064 BOOST_UBLAS_INLINE
0065 void decrement() {
0066 --position_;
0067 }
0068
0069 BOOST_UBLAS_INLINE
0070 void advance(std::ptrdiff_t n){
0071 position_ += n;
0072 }
0073
0074 template<class M,class R>
0075 BOOST_UBLAS_INLINE
0076 std::ptrdiff_t distance_to(matrix_vector_iterator<M,R> const& other) const{
0077 BOOST_UBLAS_CHECK (matrix_ == other.matrix_, external_logic ());
0078 return (std::ptrdiff_t)other.position_ - (std::ptrdiff_t)position_;
0079 }
0080
0081 template<class M,class R>
0082 BOOST_UBLAS_INLINE
0083 bool equal(matrix_vector_iterator<M,R> const& other) const{
0084 BOOST_UBLAS_CHECK (matrix_ == other.matrix_, external_logic ());
0085 return (position_ == other.position_);
0086 }
0087
0088 BOOST_UBLAS_INLINE
0089 Reference dereference() const {
0090 return Reference(*matrix_,position_);
0091 }
0092
0093 Matrix* matrix_;
0094 std::size_t position_;
0095 };
0096
0097 }
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 template<class Matrix>
0108 class matrix_row_vector {
0109 public:
0110 typedef ublas::matrix_row<Matrix> value_type;
0111 typedef ublas::matrix_row<Matrix> reference;
0112 typedef ublas::matrix_row<Matrix const> const_reference;
0113
0114 typedef ublas::detail::matrix_vector_iterator<Matrix, ublas::matrix_row<Matrix> > iterator;
0115 typedef ublas::detail::matrix_vector_iterator<Matrix const, ublas::matrix_row<Matrix const> const> const_iterator;
0116 typedef boost::reverse_iterator<iterator> reverse_iterator;
0117 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
0118
0119 typedef typename boost::iterator_difference<iterator>::type difference_type;
0120 typedef typename Matrix::size_type size_type;
0121
0122 BOOST_UBLAS_INLINE
0123 explicit matrix_row_vector(Matrix& matrix) :
0124 matrix_(&matrix) {
0125 }
0126
0127 BOOST_UBLAS_INLINE
0128 iterator begin(){
0129 return iterator(*matrix_, 0);
0130 }
0131
0132 BOOST_UBLAS_INLINE
0133 const_iterator begin() const {
0134 return const_iterator(*matrix_, 0);
0135 }
0136
0137 BOOST_UBLAS_INLINE
0138 const_iterator cbegin() const {
0139 return begin();
0140 }
0141
0142 BOOST_UBLAS_INLINE
0143 iterator end() {
0144 return iterator(*matrix_, matrix_->size1());
0145 }
0146
0147 BOOST_UBLAS_INLINE
0148 const_iterator end() const {
0149 return const_iterator(*matrix_, matrix_->size1());
0150 }
0151
0152 BOOST_UBLAS_INLINE
0153 const_iterator cend() const {
0154 return end();
0155 }
0156
0157 BOOST_UBLAS_INLINE
0158 reverse_iterator rbegin() {
0159 return reverse_iterator(end());
0160 }
0161
0162 BOOST_UBLAS_INLINE
0163 const_reverse_iterator rbegin() const {
0164 return const_reverse_iterator(end());
0165 }
0166
0167 BOOST_UBLAS_INLINE
0168 const_reverse_iterator crbegin() const {
0169 return rbegin();
0170 }
0171
0172 BOOST_UBLAS_INLINE
0173 reverse_iterator rend() {
0174 return reverse_iterator(begin());
0175 }
0176
0177 BOOST_UBLAS_INLINE
0178 const_reverse_iterator rend() const {
0179 return const_reverse_iterator(begin());
0180 }
0181
0182 BOOST_UBLAS_INLINE
0183 const_reverse_iterator crend() const {
0184 return end();
0185 }
0186
0187 BOOST_UBLAS_INLINE
0188 value_type operator()(size_type index) {
0189 return value_type(*matrix_, index);
0190 }
0191
0192 BOOST_UBLAS_INLINE
0193 value_type operator()(size_type index) const {
0194 return value_type(*matrix_, index);
0195 }
0196
0197 BOOST_UBLAS_INLINE
0198 reference operator[](size_type index){
0199 return (*this) (index);
0200 }
0201
0202 BOOST_UBLAS_INLINE
0203 const_reference operator[](size_type index) const {
0204 return (*this) (index);
0205 }
0206
0207 BOOST_UBLAS_INLINE
0208 size_type size() const {
0209 return matrix_->size1();
0210 }
0211
0212 BOOST_UBLAS_INLINE
0213 void resize(size_type size, bool preserve = true) {
0214 matrix_->resize(size, matrix_->size2(), preserve);
0215 }
0216
0217 private:
0218 Matrix* matrix_;
0219 };
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 template<class Matrix>
0231 BOOST_UBLAS_INLINE
0232 matrix_row_vector<Matrix> make_row_vector(matrix_expression<Matrix>& matrix){
0233 return matrix_row_vector<Matrix>(matrix());
0234 }
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245 template<class Matrix>
0246 BOOST_UBLAS_INLINE
0247 matrix_row_vector<Matrix const> make_row_vector(matrix_expression<Matrix> const& matrix){
0248 return matrix_row_vector<Matrix const>(matrix());
0249 }
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260 template<class Matrix>
0261 class matrix_column_vector {
0262 public:
0263 typedef ublas::matrix_column<Matrix> value_type;
0264 typedef ublas::matrix_column<Matrix> reference;
0265 typedef const ublas::matrix_column<Matrix const> const_reference;
0266
0267 typedef ublas::detail::matrix_vector_iterator<Matrix, ublas::matrix_column<Matrix> > iterator;
0268 typedef ublas::detail::matrix_vector_iterator<Matrix const, ublas::matrix_column<Matrix const> const > const_iterator;
0269 typedef boost::reverse_iterator<iterator> reverse_iterator;
0270 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
0271
0272 typedef typename boost::iterator_difference<iterator>::type difference_type;
0273 typedef typename Matrix::size_type size_type;
0274
0275 BOOST_UBLAS_INLINE
0276 explicit matrix_column_vector(Matrix& matrix) :
0277 matrix_(&matrix){
0278 }
0279
0280 BOOST_UBLAS_INLINE
0281 iterator begin() {
0282 return iterator(*matrix_, 0);
0283 }
0284
0285 BOOST_UBLAS_INLINE
0286 const_iterator begin() const {
0287 return const_iterator(*matrix_, 0);
0288 }
0289
0290 BOOST_UBLAS_INLINE
0291 const_iterator cbegin() const {
0292 return begin();
0293 }
0294
0295 BOOST_UBLAS_INLINE
0296 iterator end() {
0297 return iterator(*matrix_, matrix_->size2());
0298 }
0299
0300 BOOST_UBLAS_INLINE
0301 const_iterator end() const {
0302 return const_iterator(*matrix_, matrix_->size2());
0303 }
0304
0305 BOOST_UBLAS_INLINE
0306 const_iterator cend() const {
0307 return end();
0308 }
0309
0310 BOOST_UBLAS_INLINE
0311 reverse_iterator rbegin() {
0312 return reverse_iterator(end());
0313 }
0314
0315 BOOST_UBLAS_INLINE
0316 const_reverse_iterator rbegin() const {
0317 return const_reverse_iterator(end());
0318 }
0319
0320 BOOST_UBLAS_INLINE
0321 const_reverse_iterator crbegin() const {
0322 return rbegin();
0323 }
0324
0325 BOOST_UBLAS_INLINE
0326 reverse_iterator rend() {
0327 return reverse_iterator(begin());
0328 }
0329
0330 BOOST_UBLAS_INLINE
0331 const_reverse_iterator rend() const {
0332 return const_reverse_iterator(begin());
0333 }
0334
0335 BOOST_UBLAS_INLINE
0336 const_reverse_iterator crend() const {
0337 return rend();
0338 }
0339
0340 BOOST_UBLAS_INLINE
0341 value_type operator()(size_type index) {
0342 return value_type(*matrix_, index);
0343 }
0344
0345 BOOST_UBLAS_INLINE
0346 value_type operator()(size_type index) const {
0347 return value_type(*matrix_, index);
0348 }
0349
0350 BOOST_UBLAS_INLINE
0351 reference operator[](size_type index) {
0352 return (*this) (index);
0353 }
0354
0355 BOOST_UBLAS_INLINE
0356 const_reference operator[](size_type index) const {
0357 return (*this) (index);
0358 }
0359
0360 BOOST_UBLAS_INLINE
0361 size_type size() const {
0362 return matrix_->size2();
0363 }
0364
0365 BOOST_UBLAS_INLINE
0366 void resize(size_type size, bool preserve = true) {
0367 matrix_->resize(matrix_->size1(), size, preserve);
0368 }
0369
0370 private:
0371 Matrix* matrix_;
0372 };
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383 template<class Matrix>
0384 BOOST_UBLAS_INLINE
0385 matrix_column_vector<Matrix> make_column_vector(matrix_expression<Matrix>& matrix){
0386 return matrix_column_vector<Matrix>(matrix());
0387 }
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398 template<class Matrix>
0399 BOOST_UBLAS_INLINE
0400 matrix_column_vector<Matrix const> make_column_vector(matrix_expression<Matrix> const& matrix){
0401 return matrix_column_vector<Matrix const>(matrix());
0402 }
0403
0404 }}}
0405
0406 #endif