Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:43:13

0001 //  Copyright (c) 2012 Oswin Krause
0002 //  Copyright (c) 2013 Joaquim Duran
0003 //
0004 //  Distributed under the Boost Software License, Version 1.0. (See
0005 //  accompanying file LICENSE_1_0.txt or copy at
0006 //  http://www.boost.org/LICENSE_1_0.txt)
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 /** \brief Iterator used in the represention of a matrix as a vector of rows or columns
0024  *
0025  * Iterator used in the represention of a matrix as a vector of rows/columns. It refers
0026  * to the i-th element of the matrix, a column or a row depending of Reference type.
0027  *
0028  * The type of Reference should provide a constructor Reference(matrix, i)
0029  *
0030  * This iterator is invalidated when the underlying matrix is resized.
0031  *
0032  * \tparameter Matrix type of matrix that is represented as a vector of row/column
0033  * \tparameter Reference Matrix row or matrix column type.
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     ///\brief constructs a matrix_vector_iterator as pointing to the i-th proxy
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_;//no matrix_closure here to ensure easy usage
0094     std::size_t position_;
0095 };
0096 
0097 }
0098 
0099 /** \brief Represents a \c Matrix as a vector of rows.
0100  *
0101  * Implements an interface to Matrix that the underlaying matrix is represented as a
0102  * vector of rows.
0103  *
0104  * The vector could be resized which causes the resize of the number of rows of
0105  * the underlaying matrix.
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 /** \brief Convenience function to create \c matrix_row_vector.
0223  *
0224  * Function to create \c matrix_row_vector objects.
0225  * \param matrix the \c matrix_expression that generates the matrix that \c matrix_row_vector is referring.
0226  * \return Created \c matrix_row_vector object.
0227  *
0228  * \tparam Matrix the type of matrix that \c matrix_row_vector is referring.
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 /** \brief Convenience function to create \c matrix_row_vector.
0238  *
0239  * Function to create \c matrix_row_vector objects.
0240  * \param matrix the \c matrix_expression that generates the matrix that \c matrix_row_vector is referring.
0241  * \return Created \c matrix_row_vector object.
0242  *
0243  * \tparam Matrix the type of matrix that \c matrix_row_vector is referring.
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 /** \brief Represents a \c Matrix as a vector of columns.
0253  *
0254  * Implements an interface to Matrix that the underlaying matrix is represented as a
0255  * vector of columns.
0256  *
0257  * The vector could be resized which causes the resize of the number of columns of
0258  * the underlaying matrix.
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 /** \brief Convenience function to create \c matrix_column_vector.
0376  *
0377  * Function to create \c matrix_column_vector objects.
0378  * \param matrix the \c matrix_expression that generates the matrix that \c matrix_column_vector is referring.
0379  * \return Created \c matrix_column_vector object.
0380  *
0381  * \tparam Matrix the type of matrix that \c matrix_column_vector is referring.
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 /** \brief Convenience function to create \c matrix_column_vector.
0391  *
0392  * Function to create \c matrix_column_vector objects.
0393  * \param matrix the \c matrix_expression that generates the matrix that \c matrix_column_vector is referring.
0394  * \return Created \c matrix_column_vector object.
0395  *
0396  * \tparam Matrix the type of matrix that \c matrix_column_vector is referring.
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