From d520cdf6b81d812c368f2ba22b693a15f8332875 Mon Sep 17 00:00:00 2001 From: nasos Date: Fri, 11 Apr 2014 18:32:13 -0400 Subject: [PATCH 1/2] Added fixed_vector class and relative reg. tests --- IDEs/qtcreator/test/test.pro | 3 +- include/boost/numeric/ublas/fwd.hpp | 8 + include/boost/numeric/ublas/vector.hpp | 757 ++++++++++++++++++++++++- test/Jamfile.v2 | 3 + test/common/testhelper.hpp | 5 +- test/test_assignment.cpp | 2 +- test/test_fixed_containers.cpp | 172 ++++++ 7 files changed, 942 insertions(+), 8 deletions(-) create mode 100644 test/test_fixed_containers.cpp diff --git a/IDEs/qtcreator/test/test.pro b/IDEs/qtcreator/test/test.pro index 7f58d359..eb8e5c01 100644 --- a/IDEs/qtcreator/test/test.pro +++ b/IDEs/qtcreator/test/test.pro @@ -59,4 +59,5 @@ SOURCES += \ ../../../test/num_columns.cpp \ ../../../test/concepts.cpp \ ../../../test/comp_mat_erase.cpp \ - ../../../test/begin_end.cpp + ../../../test/begin_end.cpp \ + ../../../test/test_fixed_containers.cpp diff --git a/include/boost/numeric/ublas/fwd.hpp b/include/boost/numeric/ublas/fwd.hpp index 88855ebc..7039bde6 100644 --- a/include/boost/numeric/ublas/fwd.hpp +++ b/include/boost/numeric/ublas/fwd.hpp @@ -17,6 +17,10 @@ #include +#ifdef BOOST_UBLAS_CPP11 +#include +#endif + namespace boost { namespace numeric { namespace ublas { // Storage types @@ -88,6 +92,10 @@ namespace boost { namespace numeric { namespace ublas { template > class vector; +#ifdef BOOST_UBLAS_CPP11 + template > + class fixed_vector; +#endif template class bounded_vector; diff --git a/include/boost/numeric/ublas/vector.hpp b/include/boost/numeric/ublas/vector.hpp index fb1cd090..b97e1fc8 100644 --- a/include/boost/numeric/ublas/vector.hpp +++ b/include/boost/numeric/ublas/vector.hpp @@ -22,6 +22,10 @@ #include #include +#ifdef BOOST_UBLAS_CPP11 +#include +#include +#endif // Iterators based on ideas of Jeremy Siek @@ -793,6 +797,748 @@ namespace boost { namespace numeric { namespace ublas { }; + + /** \brief A dense vector of values of type \c T. + * + * For a \f$n\f$-dimensional vector \f$v\f$ and \f$0\leq i < n\f$ every element \f$v_i\f$ is mapped + * to the \f$i\f$-th element of the container. A storage type \c A can be specified which defaults to \c unbounded_array. + * Elements are constructed by \c A, which need not initialise their value. + * + * \tparam T type of the objects stored in the vector (like int, double, complex,...) + * \tparam A The type of the storage array of the vector. Default is \c unbounded_array. \c and \c std::vector can also be used + */ + template + class fixed_vector: + public vector_container > { + + typedef fixed_vector self_type; + public: +#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS + using vector_container::operator (); +#endif + + typedef typename A::size_type size_type; + typedef typename A::difference_type difference_type; + typedef T value_type; + typedef typename type_traits::const_reference const_reference; + typedef T &reference; + typedef T *pointer; + typedef const T *const_pointer; + typedef A array_type; + typedef const vector_reference const_closure_type; + typedef vector_reference closure_type; + typedef self_type vector_temporary_type; + typedef dense_tag storage_category; + + // Construction and destruction + + /// \brief Constructor of a fixed_vector + BOOST_UBLAS_INLINE + fixed_vector (): + vector_container (), + data_ () {} + + /// \brief Constructor of a fixed_vector by copying from another container + /// This type has the generic name \c array_typ within the vector definition. + /// \param data container of type \c A + BOOST_UBLAS_INLINE + fixed_vector (const array_type &data): + vector_container (), + data_ (data) {} + + /// \brief Constructor of a fixed_vector with a predefined size and a unique initial value + /// \param init value to assign to each element of the vector + BOOST_UBLAS_INLINE + fixed_vector (const value_type &init): + vector_container (), + data_ ( init) {} + + /// \brief Copy-constructor of a fixed_vector + /// \param v is the fixed_vector to be duplicated + BOOST_UBLAS_INLINE + fixed_vector (const fixed_vector &v): + vector_container (), + data_ (v.data_) {} + + /// \brief Copy-constructor of a vector from a vector_expression + /// Depending on the vector_expression, this constructor can have the cost of the computations + /// of the expression (trivial to say it, but it is to take into account in your complexity calculations). + /// \param ae the vector_expression which values will be duplicated into the vector + template + BOOST_UBLAS_INLINE + fixed_vector (const vector_expression &ae): + vector_container (), + data_ ( ) { + vector_assign (*this, ae); + } + + /// \brief Construct a fixed_vector from a list of values + /// The list should be included in curly braces. Typical syntax is: + /// fixed_vector v = { 1, 2, 3 } or fixed_vector v( {1, 2, 3} ) or fixed_vector v( 1, 2, 3 ) + template + fixed_vector(U v0, Types... vrest) : data_{ { v0, vrest... } } {} + + // ----------------------- + // Random Access Container + // ----------------------- + + /// \brief Return the maximum size of the data container. + /// Return the upper bound (maximum size) on the data container. Depending on the container, it can be bigger than the current size of the vector. + BOOST_UBLAS_INLINE + size_type max_size () const { + return data_.max_size (); + } + + /// \brief Return true if the vector is empty (\c size==0) + /// \return \c true if empty, \c false otherwise + BOOST_UBLAS_INLINE + const bool &empty () const { + return data_.empty(); + } + + // --------- + // Accessors + // --------- + + /// \brief Return the size of the vector + BOOST_UBLAS_INLINE + constexpr size_type size () { // should have a const after C++14 + return data_.size (); + } + + // ----------------- + // Storage accessors + // ----------------- + + /// \brief Return a \c const reference to the container. Useful to access data directly for specific type of container. + BOOST_UBLAS_INLINE + const array_type &data () const { + return data_; + } + + /// \brief Return a reference to the container. Useful to speed-up write operations to the data in very specific case. + BOOST_UBLAS_INLINE + array_type &data () { + return data_; + } + + // --------------- + // Element support + // --------------- + + /// \brief Return a pointer to the element \f$i\f$ + /// \param i index of the element + // XXX this semantic is not the one expected by the name of this method + BOOST_UBLAS_INLINE + pointer find_element (size_type i) { + return const_cast (const_cast(*this).find_element (i)); + } + + /// \brief Return a const pointer to the element \f$i\f$ + /// \param i index of the element + // XXX this semantic is not the one expected by the name of this method + BOOST_UBLAS_INLINE + const_pointer find_element (size_type i) const { + BOOST_UBLAS_CHECK (i < data_.size(), bad_index() ); // Since std:array doesn't check for bounds + return & (data () [i]); + } + + // -------------- + // Element access + // -------------- + + /// \brief Return a const reference to the element \f$i\f$ + /// Return a const reference to the element \f$i\f$. With some compilers, this notation will be faster than \c[i] + /// \param i index of the element + BOOST_UBLAS_INLINE + const_reference operator () (size_type i) const { + BOOST_UBLAS_CHECK (i < data_.size(), bad_index() ); + return data () [i]; + } + + /// \brief Return a reference to the element \f$i\f$ + /// Return a reference to the element \f$i\f$. With some compilers, this notation will be faster than \c[i] + /// \param i index of the element + BOOST_UBLAS_INLINE + reference operator () (size_type i) { + BOOST_UBLAS_CHECK (i < data_.size(), bad_index() ); + return data () [i]; + } + + /// \brief Return a const reference to the element \f$i\f$ + /// \param i index of the element + BOOST_UBLAS_INLINE + const_reference operator [] (size_type i) const { + BOOST_UBLAS_CHECK (i < data_.size(), bad_index() ); + return (*this) (i); + } + + /// \brief Return a reference to the element \f$i\f$ + /// \param i index of the element + BOOST_UBLAS_INLINE + reference operator [] (size_type i) { + BOOST_UBLAS_CHECK (i < data_.size(), bad_index() ); + return (*this) (i); + } + + // ------------------ + // Element assignment + // ------------------ + + /// \brief Set element \f$i\f$ to the value \c t + /// \param i index of the element + /// \param t reference to the value to be set + // XXX semantic of this is to insert a new element and therefore size=size+1 ? + BOOST_UBLAS_INLINE + reference insert_element (size_type i, const_reference t) { + BOOST_UBLAS_CHECK (i < data_.size(), bad_index ()); + return (data () [i] = t); + } + + /// \brief Set element \f$i\f$ to the \e zero value + /// \param i index of the element + BOOST_UBLAS_INLINE + void erase_element (size_type i) { + BOOST_UBLAS_CHECK (i < data_.size(), bad_index ()); + data () [i] = value_type/*zero*/(); + } + + // ------- + // Zeroing + // ------- + + /// \brief Clear the vector, i.e. set all values to the \c zero value. + BOOST_UBLAS_INLINE + void clear () { + std::fill (data ().begin (), data ().end (), value_type/*zero*/()); + } + + // Assignment +#ifdef BOOST_UBLAS_MOVE_SEMANTICS + + /// \brief Assign a full fixed_vector (\e RHS-vector) to the current fixed_vector (\e LHS-vector) + /// \param v is the source vector + /// \return a reference to a fixed_vector (i.e. the destination vector) + /*! @note "pass by value" the key idea to enable move semantics */ + BOOST_UBLAS_INLINE + fixed_vector &operator = (fixed_vector v) { + assign_temporary(v); + return *this; + } +#else + /// \brief Assign a full vector (\e RHS-vector) to the current vector (\e LHS-vector) + /// \param v is the source vector + /// \return a reference to a vector (i.e. the destination vector) + BOOST_UBLAS_INLINE + fixed_vector &operator = (const fixed_vector &v) { + data () = v.data (); + return *this; + } +#endif + + /// \brief Assign a full fixed_vector (\e RHS-vector) to the current fixed_vector (\e LHS-vector) + /// Assign a full vector (\e RHS-vector) to the current fixed_vector (\e LHS-vector). This method does not create any temporary. + /// \param v is the source vector container + /// \return a reference to a vector (i.e. the destination vector) + template // Container assignment without temporary + BOOST_UBLAS_INLINE + fixed_vector &operator = (const vector_container &v) { + assign (v); + return *this; + } + + /// \brief Assign a full vector (\e RHS-vector) to the current vector (\e LHS-vector) + /// \param v is the source vector + /// \return a reference to a vector (i.e. the destination vector) + BOOST_UBLAS_INLINE + fixed_vector &assign_temporary (fixed_vector &v) { + swap ( v ); + return *this; + } + + /// \brief Assign the result of a vector_expression to the vector + /// Assign the result of a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + /// \return a reference to the resulting vector + template + BOOST_UBLAS_INLINE + fixed_vector &operator = (const vector_expression &ae) { + self_type temporary (ae); + return assign_temporary (temporary); + } + + /// \brief Assign the result of a vector_expression to the vector + /// Assign the result of a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + /// \return a reference to the resulting vector + template + BOOST_UBLAS_INLINE + fixed_vector &assign (const vector_expression &ae) { + vector_assign (*this, ae); + return *this; + } + + // ------------------- + // Computed assignment + // ------------------- + + /// \brief Assign the sum of the vector and a vector_expression to the vector + /// Assign the sum of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// A temporary is created for the computations. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + /// \return a reference to the resulting vector + template + BOOST_UBLAS_INLINE + fixed_vector &operator += (const vector_expression &ae) { + self_type temporary (*this + ae); + return assign_temporary (temporary); + } + + /// \brief Assign the sum of the vector and a vector_expression to the vector + /// Assign the sum of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// No temporary is created. Computations are done and stored directly into the resulting vector. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + /// \return a reference to the resulting vector + template // Container assignment without temporary + BOOST_UBLAS_INLINE + fixed_vector &operator += (const vector_container &v) { + plus_assign (v); + return *this; + } + + /// \brief Assign the sum of the vector and a vector_expression to the vector + /// Assign the sum of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// No temporary is created. Computations are done and stored directly into the resulting vector. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + /// \return a reference to the resulting vector + template + BOOST_UBLAS_INLINE + fixed_vector &plus_assign (const vector_expression &ae) { + vector_assign (*this, ae); + return *this; + } + + /// \brief Assign the difference of the vector and a vector_expression to the vector + /// Assign the difference of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// A temporary is created for the computations. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + template + BOOST_UBLAS_INLINE + fixed_vector &operator -= (const vector_expression &ae) { + self_type temporary (*this - ae); + return assign_temporary (temporary); + } + + /// \brief Assign the difference of the vector and a vector_expression to the vector + /// Assign the difference of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// No temporary is created. Computations are done and stored directly into the resulting vector. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + /// \return a reference to the resulting vector + template // Container assignment without temporary + BOOST_UBLAS_INLINE + fixed_vector &operator -= (const vector_container &v) { + minus_assign (v); + return *this; + } + + /// \brief Assign the difference of the vector and a vector_expression to the vector + /// Assign the difference of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// No temporary is created. Computations are done and stored directly into the resulting vector. + /// \tparam AE is the type of the vector_expression + /// \param ae is a const reference to the vector_expression + /// \return a reference to the resulting vector + template + BOOST_UBLAS_INLINE + fixed_vector &minus_assign (const vector_expression &ae) { + vector_assign (*this, ae); + return *this; + } + + /// \brief Assign the product of the vector and a scalar to the vector + /// Assign the product of the vector and a scalar to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// No temporary is created. Computations are done and stored directly into the resulting vector. + /// \tparam AE is the type of the vector_expression + /// \param at is a const reference to the scalar + /// \return a reference to the resulting vector + template + BOOST_UBLAS_INLINE + fixed_vector &operator *= (const AT &at) { + vector_assign_scalar (*this, at); + return *this; + } + + /// \brief Assign the division of the vector by a scalar to the vector + /// Assign the division of the vector by a scalar to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression. + /// No temporary is created. Computations are done and stored directly into the resulting vector. + /// \tparam AE is the type of the vector_expression + /// \param at is a const reference to the scalar + /// \return a reference to the resulting vector + template + BOOST_UBLAS_INLINE + fixed_vector &operator /= (const AT &at) { + vector_assign_scalar (*this, at); + return *this; + } + + // -------- + // Swapping + // -------- + + /// \brief Swap the content of the vector with another vector + /// \param v is the vector to be swapped with + BOOST_UBLAS_INLINE + void swap (fixed_vector &v) { + if (this != &v) { + data ().swap (v.data ()); + } + } + + /// \brief Swap the content of two vectors + /// \param v1 is the first vector. It takes values from v2 + /// \param v2 is the second vector It takes values from v1 + BOOST_UBLAS_INLINE + friend void swap (fixed_vector &v1, fixed_vector &v2) { + v1.swap (v2); + } + + // Iterator types + private: + // Use the storage array iterator + typedef typename A::const_iterator const_subiterator_type; + typedef typename A::iterator subiterator_type; + + public: +#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR + typedef indexed_iterator iterator; + typedef indexed_const_iterator const_iterator; +#else + class const_iterator; + class iterator; +#endif + + // -------------- + // Element lookup + // -------------- + + /// \brief Return a const iterator to the element \e i + /// \param i index of the element + BOOST_UBLAS_INLINE + const_iterator find (size_type i) const { +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + return const_iterator (*this, data ().begin () + i); +#else + return const_iterator (*this, i); +#endif + } + + /// \brief Return an iterator to the element \e i + /// \param i index of the element + BOOST_UBLAS_INLINE + iterator find (size_type i) { +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + return iterator (*this, data ().begin () + i); +#else + return iterator (*this, i); +#endif + } + +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + class const_iterator: + public container_const_reference, + public random_access_iterator_base { + public: + typedef typename fixed_vector::difference_type difference_type; + typedef typename fixed_vector::value_type value_type; + typedef typename fixed_vector::const_reference reference; + typedef const typename fixed_vector::pointer pointer; + + // ---------------------------- + // Construction and destruction + // ---------------------------- + + + BOOST_UBLAS_INLINE + const_iterator (): + container_const_reference (), it_ () {} + BOOST_UBLAS_INLINE + const_iterator (const self_type &v, const const_subiterator_type &it): + container_const_reference (v), it_ (it) {} + BOOST_UBLAS_INLINE + const_iterator (const typename self_type::iterator &it): // ISSUE vector:: stops VC8 using std::iterator here + container_const_reference (it ()), it_ (it.it_) {} + + // ---------- + // Arithmetic + // ---------- + + /// \brief Increment by 1 the position of the iterator + /// \return a reference to the const iterator + BOOST_UBLAS_INLINE + const_iterator &operator ++ () { + ++ it_; + return *this; + } + + /// \brief Decrement by 1 the position of the iterator + /// \return a reference to the const iterator + BOOST_UBLAS_INLINE + const_iterator &operator -- () { + -- it_; + return *this; + } + + /// \brief Increment by \e n the position of the iterator + /// \return a reference to the const iterator + BOOST_UBLAS_INLINE + const_iterator &operator += (difference_type n) { + it_ += n; + return *this; + } + + /// \brief Decrement by \e n the position of the iterator + /// \return a reference to the const iterator + BOOST_UBLAS_INLINE + const_iterator &operator -= (difference_type n) { + it_ -= n; + return *this; + } + + /// \brief Return the different in number of positions between 2 iterators + BOOST_UBLAS_INLINE + difference_type operator - (const const_iterator &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ - it.it_; + } + + /// \brief Dereference an iterator + /// Dereference an iterator: a bounds' check is done before returning the value. A bad_index() expection is returned if out of bounds. + /// \return a const reference to the value pointed by the iterator + BOOST_UBLAS_INLINE + const_reference operator * () const { + BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ()); + return *it_; + } + + /// \brief Dereference an iterator at the n-th forward value + /// Dereference an iterator at the n-th forward value, that is the value pointed by iterator+n. + /// A bounds' check is done before returning the value. A bad_index() expection is returned if out of bounds. + /// \return a const reference + BOOST_UBLAS_INLINE + const_reference operator [] (difference_type n) const { + return *(it_ + n); + } + + // Index + /// \brief return the index of the element referenced by the iterator + BOOST_UBLAS_INLINE + size_type index () const { + BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ()); + return it_ - (*this) ().begin ().it_; + } + + // Assignment + BOOST_UBLAS_INLINE + /// \brief assign the value of an iterator to the iterator + const_iterator &operator = (const const_iterator &it) { + container_const_reference::assign (&it ()); + it_ = it.it_; + return *this; + } + + // Comparison + /// \brief compare the value of two itetarors + /// \return true if they reference the same element + BOOST_UBLAS_INLINE + bool operator == (const const_iterator &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ == it.it_; + } + + + /// \brief compare the value of two iterators + /// \return return true if the left-hand-side iterator refers to a value placed before the right-hand-side iterator + BOOST_UBLAS_INLINE + bool operator < (const const_iterator &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ < it.it_; + } + + private: + const_subiterator_type it_; + + friend class iterator; + }; +#endif + + /// \brief return an iterator on the first element of the vector + BOOST_UBLAS_INLINE + const_iterator begin () const { + return find (0); + } + + /// \brief return an iterator after the last element of the vector + BOOST_UBLAS_INLINE + const_iterator end () const { + return find (data_.size ()); + } + +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + class iterator: + public container_reference, + public random_access_iterator_base { + public: + typedef typename fixed_vector::difference_type difference_type; + typedef typename fixed_vector::value_type value_type; + typedef typename fixed_vector::reference reference; + typedef typename fixed_vector::pointer pointer; + + + // Construction and destruction + BOOST_UBLAS_INLINE + iterator (): + container_reference (), it_ () {} + BOOST_UBLAS_INLINE + iterator (self_type &v, const subiterator_type &it): + container_reference (v), it_ (it) {} + + // Arithmetic + BOOST_UBLAS_INLINE + iterator &operator ++ () { + ++ it_; + return *this; + } + BOOST_UBLAS_INLINE + iterator &operator -- () { + -- it_; + return *this; + } + BOOST_UBLAS_INLINE + iterator &operator += (difference_type n) { + it_ += n; + return *this; + } + BOOST_UBLAS_INLINE + iterator &operator -= (difference_type n) { + it_ -= n; + return *this; + } + BOOST_UBLAS_INLINE + difference_type operator - (const iterator &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ - it.it_; + } + + // Dereference + BOOST_UBLAS_INLINE + reference operator * () const { + BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_ , bad_index ()); + return *it_; + } + BOOST_UBLAS_INLINE + reference operator [] (difference_type n) const { + return *(it_ + n); + } + + // Index + BOOST_UBLAS_INLINE + size_type index () const { + BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_ , bad_index ()); + return it_ - (*this) ().begin ().it_; + } + + // Assignment + BOOST_UBLAS_INLINE + iterator &operator = (const iterator &it) { + container_reference::assign (&it ()); + it_ = it.it_; + return *this; + } + + // Comparison + BOOST_UBLAS_INLINE + bool operator == (const iterator &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ == it.it_; + } + BOOST_UBLAS_INLINE + bool operator < (const iterator &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ < it.it_; + } + + private: + subiterator_type it_; + + friend class const_iterator; + }; +#endif + + /// \brief Return an iterator on the first element of the vector + BOOST_UBLAS_INLINE + iterator begin () { + return find (0); + } + + /// \brief Return an iterator at the end of the vector + BOOST_UBLAS_INLINE + iterator end () { + return find (data_.size ()); + } + + // Reverse iterator + typedef reverse_iterator_base const_reverse_iterator; + typedef reverse_iterator_base reverse_iterator; + + /// \brief Return a const reverse iterator before the first element of the reversed vector (i.e. end() of normal vector) + BOOST_UBLAS_INLINE + const_reverse_iterator rbegin () const { + return const_reverse_iterator (end ()); + } + + /// \brief Return a const reverse iterator on the end of the reverse vector (i.e. first element of the normal vector) + BOOST_UBLAS_INLINE + const_reverse_iterator rend () const { + return const_reverse_iterator (begin ()); + } + + /// \brief Return a const reverse iterator before the first element of the reversed vector (i.e. end() of normal vector) + BOOST_UBLAS_INLINE + reverse_iterator rbegin () { + return reverse_iterator (end ()); + } + + /// \brief Return a const reverse iterator on the end of the reverse vector (i.e. first element of the normal vector) + BOOST_UBLAS_INLINE + reverse_iterator rend () { + return reverse_iterator (begin ()); + } + + // ------------- + // Serialization + // ------------- + + /// Serialize a vector into and archive as defined in Boost + /// \param ar Archive object. Can be a flat file, an XML file or any other stream + /// \param file_version Optional file version (not yet used) + template + void serialize(Archive & ar, const unsigned int /* file_version */){ + ar & serialization::make_nvp("data",data_); + } + + private: + array_type data_; + }; + + // -------------------- // Bounded vector class // -------------------- @@ -867,6 +1613,7 @@ namespace boost { namespace numeric { namespace ublas { }; + // ----------------- // Zero vector class // ----------------- @@ -1597,13 +2344,13 @@ namespace boost { namespace numeric { namespace ublas { c_vector (size_type size): size_ (size) /* , data_ () */ { if (size_ > N) - bad_size ().raise (); + bad_size ().raise (); } BOOST_UBLAS_INLINE c_vector (const c_vector &v): size_ (v.size_) /* , data_ () */ { if (size_ > N) - bad_size ().raise (); + bad_size ().raise (); assign(v); } template @@ -1611,7 +2358,7 @@ namespace boost { namespace numeric { namespace ublas { c_vector (const vector_expression &ae): size_ (ae ().size ()) /* , data_ () */ { if (size_ > N) - bad_size ().raise (); + bad_size ().raise (); vector_assign (*this, ae); } @@ -1633,7 +2380,7 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE void resize (size_type size, bool preserve = true) { if (size > N) - bad_size ().raise (); + bad_size ().raise (); size_ = size; } @@ -1782,7 +2529,7 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE void swap (c_vector &v) { if (this != &v) { - BOOST_UBLAS_CHECK (size_ == v.size_, bad_size ()); + BOOST_UBLAS_CHECK (size_ == v.size_, bad_size ()); std::swap (size_, v.size_); std::swap_ranges (data_, data_ + size_, v.data_); } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index cc86d6ee..2e8ac451 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -212,4 +212,7 @@ test-suite numeric/uBLAS : : ] + [ run test_fixed_containers.cpp + : : : BOOST_UBLAS_CPP11 + ] ; diff --git a/test/common/testhelper.hpp b/test/common/testhelper.hpp index 4fbf9c4d..c4a2fb06 100644 --- a/test/common/testhelper.hpp +++ b/test/common/testhelper.hpp @@ -8,6 +8,9 @@ #define _HPP_TESTHELPER_ #include +#include +#include +#include static unsigned _success_counter = 0; static unsigned _fail_counter = 0; @@ -43,7 +46,7 @@ void assertEquals(const char* message, T expected, T actual) { } } -static +inline static std::pair getResults() { return std::make_pair(_success_counter, _fail_counter); } diff --git a/test/test_assignment.cpp b/test/test_assignment.cpp index 4a3897c7..64d8c857 100644 --- a/test/test_assignment.cpp +++ b/test/test_assignment.cpp @@ -807,5 +807,5 @@ int main () { BOOST_UBLAS_TEST_END(); - return EXIT_SUCCESS;; + return EXIT_SUCCESS; } diff --git a/test/test_fixed_containers.cpp b/test/test_fixed_containers.cpp new file mode 100644 index 00000000..2e4e524b --- /dev/null +++ b/test/test_fixed_containers.cpp @@ -0,0 +1,172 @@ +#undef BOOST_UBLAS_NO_EXCEPTIONS +#include "common/testhelper.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include "utils.hpp" + +using namespace boost::numeric::ublas; + +using std::cout; +using std::endl; + +template < class T > +bool test_vector( std::string type_name) +{ + std::stringstream stream; + stream << "Testing for: " << type_name; + BOOST_UBLAS_DEBUG_TRACE( stream.str() ); + + bool pass = true; + + { + typedef fixed_vector vec3; + + vec3 v1; + + pass &=(sizeof( vec3 ) == v1.size()*sizeof( T ) ) ; + + vector v( 3, 0 ) ; + + pass &= compare( v1, v ); + + v1 <<= 10.0, 10, 33; + v <<= 10.0, 10, 33; + + //cout << std::setprecision(20) << v1 << '\n' << v; + + pass &= compare( v1, v ); + + + vec3 v2; + + v2( 0 ) = 10.0; v2( 1 ) = 10; v2( 2 ) = 33; + pass &= compare( v, v2 ); + + + v2 += v; + + pass &= compare( v2, 2*v ); + + + v1 = 2*v1 + v - 6*v2; + pass &= compare( v1, (3-2*6)*v ); + + + vec3 v3{ (T)-90.0, (T)-90.0, (T)-297.0 }; + pass &= compare( v3, v1 ); + + vec3 v4 = { (T)-90.0, (T)-90.0, (T)-297.0 }; + pass &= compare( v4, v1 ); + + vec3 v5( (T)-90.0, (T)-90.0, (T)-297.0 ); + pass &= compare( v5, v1 ); + + vec3 v6((T) 5.0, (T)8.0, (T)9.0); + + matrix M = outer_prod( v6, v6), L( 3, 3); + + L <<= 25, 40, 45, 40, 64, 72, 45, 72, 81; + + pass &= compare( M, L ); + + L <<= 1, 2, 3, 4, 5, 6, 7, 8, 9; + v6 <<= 4, 5, 6; + vec3 v7 ( (T)32.0, (T)77.0, (T)122.0 ); + + pass &= compare( v7, prod(L, v6) ); + + vec3 v8; + noalias( v8 ) = prod(L, v6); + + pass &= compare( v7, v8 ); + + } + + + { + const std::size_t N = 33; + typedef fixed_vector vec33; + + vec33 v1; + vector v( N ); + + for ( std::size_t i = 0; i!= v1.size(); i++) + { + v1( i ) = 3.14159*i*i; + v ( i ) = 3.14159*i*i; + } + + pass &= compare( v1, v ); + + + auto ip = inner_prod( v, v); + auto ip1 = inner_prod( v1, v1); + + pass &= ( ip == ip1 ) ; + + T c = 0; + for (auto i = v1.begin(); i != v1.end(); i++) + { + *i = c; + c = c + 1; + } + + c = 0; + for (auto i = v.begin(); i != v.end(); i++) + { + *i = c; + c = c + 1; + } + + pass &= compare( v1, v ); + + try { + T a; + v1( 100 ); + a=v1( 100 ); + (void) a ; + + } catch ( bad_index &e) { + std::cout << " Caught: " << e.what() << endl; + pass &= true; + } + + + } + return pass; +} + +BOOST_UBLAS_TEST_DEF (test_vector) { + + BOOST_UBLAS_DEBUG_TRACE( "Starting fixed container tests" ); + + BOOST_UBLAS_TEST_CHECK( test_vector< double >( "double") ); + BOOST_UBLAS_TEST_CHECK( test_vector< float >( "float") ); + BOOST_UBLAS_TEST_CHECK( test_vector< int >( "int") ); + + BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); + BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); + BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); +} + +int main () { + +#ifdef BOOST_UBLAS_NO_EXCEPTIONS + std::cout << "DEFINED SDFSDF SDF SDF " << std::endl; +#endif + + BOOST_UBLAS_TEST_BEGIN(); + + BOOST_UBLAS_TEST_DO( test_vector ); + + BOOST_UBLAS_TEST_END(); + + return EXIT_SUCCESS; + +} From f5e491568042f22bf9455674e397480323893a70 Mon Sep 17 00:00:00 2001 From: nasos Date: Fri, 11 Apr 2014 23:28:21 -0400 Subject: [PATCH 2/2] Added fixed_matrix class and relative unit tests. Altered abunch of items to avoid warnings in clang. Moved the specialization of std::complex in deduction to avoid clang errors. Corrected a potential bug in assignment.hpp with respect to static relative moves. --- include/boost/numeric/ublas/assignment.hpp | 16 +- .../ublas/experimental/sparse_view.hpp | 10 +- include/boost/numeric/ublas/fwd.hpp | 4 + include/boost/numeric/ublas/matrix.hpp | 985 ++++++++++++++++++ include/boost/numeric/ublas/traits.hpp | 198 ++-- include/boost/numeric/ublas/vector.hpp | 26 +- test/Jamfile.v2 | 4 +- test/test_fixed_containers.cpp | 208 +++- 8 files changed, 1299 insertions(+), 152 deletions(-) diff --git a/include/boost/numeric/ublas/assignment.hpp b/include/boost/numeric/ublas/assignment.hpp index d505d0e5..d2079e13 100644 --- a/include/boost/numeric/ublas/assignment.hpp +++ b/include/boost/numeric/ublas/assignment.hpp @@ -170,7 +170,7 @@ BOOST_UBLAS_INLINE vector_move_manip move(T i) { * * \todo Doxygen has some problems with similar template functions. Correct that. */ -template +template class static_vector_move_manip: public index_manipulator > { public: template @@ -198,8 +198,8 @@ public: * * \todo Doxygen has some problems with similar template functions. Correct that. */ -template -BOOST_UBLAS_INLINE static_vector_move_manip move() { +template +static_vector_move_manip move() { return static_vector_move_manip(); } @@ -269,7 +269,7 @@ BOOST_UBLAS_INLINE matrix_move_to_manip move_to(T i, T j) { * * \todo Doxygen has some problems with similar template functions. Correct that. */ -template +template class static_matrix_move_to_manip: public index_manipulator > { public: template @@ -369,7 +369,7 @@ BOOST_UBLAS_INLINE matrix_move_manip move(T i, T j) { * * \todo Doxygen has some problems with similar template functions. Correct that. */ -template +template class static_matrix_move_manip: public index_manipulator > { public: template @@ -407,7 +407,7 @@ public: * * \todo Doxygen has some problems with similar template functions. Correct that. */ -template +template BOOST_UBLAS_INLINE static_matrix_move_manip move() { return static_matrix_move_manip(); } @@ -804,7 +804,7 @@ namespace traverse_policy { l++; j++; if (l>=e().size2()) { l=0; k++; j=j0; i++; - // It is assumed that the iteration starts from 0 and happens only using this function from within + // It is assumed that the iteration starts from 0 and progresses only using this function from within // an assigner object. // Otherwise (i.e. if it is called outside the assigner object) apply2 should have been // outside the if statement. @@ -850,7 +850,7 @@ namespace traverse_policy { k++; i++; if (k>=e().size1()) { k=0; l++; i=i0; j++; - // It is assumed that the iteration starts from 0 and happens only using this function from within + // It is assumed that the iteration starts from 0 and progresses only using this function from within // an assigner object. // Otherwise (i.e. if it is called outside the assigner object) apply2 should have been // outside the if statement. diff --git a/include/boost/numeric/ublas/experimental/sparse_view.hpp b/include/boost/numeric/ublas/experimental/sparse_view.hpp index cc6e28c9..52ad509e 100644 --- a/include/boost/numeric/ublas/experimental/sparse_view.hpp +++ b/include/boost/numeric/ublas/experimental/sparse_view.hpp @@ -190,11 +190,11 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE compressed_matrix_view(const compressed_matrix_view& o) : - size1_(size1_), size2_(size2_), - nnz_(nnz_), - index1_data_(index1_data_), - index2_data_(index2_data_), - value_data_(value_data_) + size1_(o.size1_), size2_(o.size2_), + nnz_(o.nnz_), + index1_data_(o.index1_data_), + index2_data_(o.index2_data_), + value_data_(o.value_data_) {} // diff --git a/include/boost/numeric/ublas/fwd.hpp b/include/boost/numeric/ublas/fwd.hpp index 7039bde6..858f43fa 100644 --- a/include/boost/numeric/ublas/fwd.hpp +++ b/include/boost/numeric/ublas/fwd.hpp @@ -133,6 +133,10 @@ namespace boost { namespace numeric { namespace ublas { template > class matrix; +#ifdef BOOST_UBLAS_CPP11 + template > + class fixed_matrix; +#endif template class bounded_matrix; diff --git a/include/boost/numeric/ublas/matrix.hpp b/include/boost/numeric/ublas/matrix.hpp index 052416d2..af52e454 100644 --- a/include/boost/numeric/ublas/matrix.hpp +++ b/include/boost/numeric/ublas/matrix.hpp @@ -1,6 +1,7 @@ // // Copyright (c) 2000-2010 // Joerg Walter, Mathias Koch, Gunter Winkler, David Bellot +// Copyright (c) 2014, Athanasios Iliopoulos // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at @@ -1093,6 +1094,989 @@ namespace boost { namespace numeric { array_type data_; }; + +#ifdef BOOST_UBLAS_CPP11 + /** \brief A fixed size dense matrix of values of type \c T. Equivalent to a c-style 2 dimensional array. + * + * For a \f$(m \times n)\f$-dimensional matrix and \f$ 0 \leq i < m, 0 \leq j < n\f$, every element \f$ m_{i,j} \f$ is mapped to + * the \f$(i.n + j)\f$-th element of the container for row major orientation or the \f$ (i + j.m) \f$-th element of + * the container for column major orientation. In a dense matrix all elements are represented in memory in a + * contiguous chunk of memory by definition. + * + * Orientation and storage can also be specified, otherwise a \c row_major and \c std::array are used. It is \b not + * required by the storage to initialize elements of the matrix. + * + * \tparam T the type of object stored in the matrix (like double, float, complex, etc...) + * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major + * \tparam A the type of Storage array. Default is \c std::array + */ + template + class fixed_matrix: + public matrix_container > { + + typedef T *pointer; + typedef L layout_type; + typedef fixed_matrix self_type; + public: +#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS + using matrix_container::operator (); +#endif + typedef typename A::size_type size_type; + typedef typename A::difference_type difference_type; + typedef T value_type; + typedef const T &const_reference; + typedef T &reference; + typedef A array_type; + typedef const matrix_reference const_closure_type; + typedef matrix_reference closure_type; + typedef vector vector_temporary_type; + typedef self_type matrix_temporary_type; + typedef dense_tag storage_category; + // This could be better for performance, + // typedef typename unknown_orientation_tag orientation_category; + // but others depend on the orientation information... + typedef typename L::orientation_category orientation_category; + + // Construction and destruction + + /// Default dense fixed_matrix constructor. Make a dense fixed_matrix of size (N,M) + BOOST_UBLAS_INLINE + fixed_matrix (): + matrix_container (), + data_ () {} + + /// \brief Construct a fixed_matrix from a list of values + /// The list should be included in curly braces. Typical syntax is: + /// fixed_matrix v = { 1, 2, 3, 4 } or fixed_matrix v( {1, 2, 3, 4} ) or fixed_matrix v( 1, 2, 3, 4 ) + template + fixed_matrix(value_type v0, Types... vrest) : + matrix_container (), + data_{ { v0, vrest... } } {} + + /** Dense matrix constructor with defined size a initial value for all the matrix elements + * \param size1 number of rows + * \param size2 number of columns + * \param init initial value assigned to all elements + */ + fixed_matrix (const value_type &init): + matrix_container (), + data_ ( ) { + data_.fill(init); + } + + /** Dense matrix constructor with defined size and an initial data array + * \param size1 number of rows + * \param size2 number of columns + * \param data array to copy into the matrix. Must have the same dimension as the matrix + */ + BOOST_UBLAS_INLINE + fixed_matrix (const array_type &data): + matrix_container (), + data_ (data) {} + + /** Copy-constructor of a dense fixed_matrix + * \param m is a dense fixed_matrix + */ + BOOST_UBLAS_INLINE + fixed_matrix (const fixed_matrix &m): + matrix_container (), + data_ (m.data_) {} + + /** Copy-constructor of a dense matrix from a matrix expression + * \param ae is a matrix expression + */ + template + BOOST_UBLAS_INLINE + fixed_matrix (const matrix_expression &ae): + matrix_container (), + data_ () { + matrix_assign (*this, ae); + } + + // Accessors + /** Return the number of rows of the fixed_matrix + * You can also use the free size<>() function in operation/size.hpp as size<1>(m) where m is a matrix + */ + BOOST_UBLAS_INLINE + constexpr size_type size1 () const { + return M; + } + + /** Return the number of colums of the fixed_matrix + * You can also use the free size<>() function in operation/size.hpp as size<2>(m) where m is a matrix + */ + BOOST_UBLAS_INLINE + constexpr size_type size2 () const { + return N; + } + + // Storage accessors + /** Return a constant reference to the internal storage of a dense matrix, i.e. the raw data + * It's type depends on the type used by the matrix to store its data + */ + BOOST_UBLAS_INLINE + const array_type &data () const { + return data_; + } + /** Return a reference to the internal storage of a dense matrix, i.e. the raw data + * It's type depends on the type used by the matrix to store its data + */ + BOOST_UBLAS_INLINE + array_type &data () { + return data_; + } + + + // Element access + + /** Access a fixed_matrix element. Here we return a const reference + * \param i the first coordinate of the element. By default it's the row + * \param j the second coordinate of the element. By default it's the column + * \return a const reference to the element + */ + BOOST_UBLAS_INLINE + const_reference operator () (size_type i, size_type j) const { + return data () [layout_type::element (i, M, j, N)]; + } + + /** Access a fixed_matrix element. Here we return a reference + * \param i the first coordinate of the element. By default it's the row + * \param j the second coordinate of the element. By default it's the column + * \return a reference to the element + */ + BOOST_UBLAS_INLINE + reference at_element (size_type i, size_type j) { + return data () [layout_type::element (i, M, j, N)]; + } + + /** Access a matrix element. Here we return a reference + * \param i the first coordinate of the element. By default it's the row + * \param j the second coordinate of the element. By default it's the column + * \return a reference to the element + */ + BOOST_UBLAS_INLINE + reference operator () (size_type i, size_type j) { + return at_element (i, j); + } + + // Element assignment + + /** Change the value of a matrix element. Return back a reference to it + * \param i the first coordinate of the element. By default it's the row + * \param j the second coordinate of the element. By default it's the column + * \param t the new value of the element + * \return a reference to the newly changed element + */ + BOOST_UBLAS_INLINE + reference insert_element (size_type i, size_type j, const_reference t) { + return (at_element (i, j) = t); + } + + /** Erase the element + * For most types (int, double, etc...) it means setting 0 (zero) the element at zero in fact. + * For user-defined types, it could be another value if you decided it. Your type in that case must + * contain a default null value. + * \param i the first coordinate of the element. By default it's the row + * \param j the second coordinate of the element. By default it's the column + */ + void erase_element (size_type i, size_type j) { + at_element (i, j) = value_type/*zero*/(); + } + + // Zeroing + /** Erase all elements in the matrix + * For most types (int, double, etc...) it means writing 0 (zero) everywhere. + * For user-defined types, it could be another value if you decided it. Your type in that case must + * contain a default null value. + */ + BOOST_UBLAS_INLINE + void clear () { + std::fill (data ().begin (), data ().end (), value_type/*zero*/()); + } + + // Assignment +#ifdef BOOST_UBLAS_MOVE_SEMANTICS + + /*! @note "pass by value" the key idea to enable move semantics */ + BOOST_UBLAS_INLINE + fixed_matrix &operator = (matrix m) { + assign_temporary(m); + return *this; + } +#else + BOOST_UBLAS_INLINE + fixed_matrix &operator = (const fixed_matrix &m) { + data () = m.data (); + return *this; + } +#endif + template // Container assignment without temporary + BOOST_UBLAS_INLINE + fixed_matrix &operator = (const matrix_container &m) { + resize (m ().size1 (), m ().size2 (), false); + assign (m); + return *this; + } + BOOST_UBLAS_INLINE + fixed_matrix &assign_temporary (fixed_matrix &m) { + swap (m); + return *this; + } + template + BOOST_UBLAS_INLINE + fixed_matrix &operator = (const matrix_expression &ae) { + self_type temporary (ae); + return assign_temporary (temporary); + } + template + BOOST_UBLAS_INLINE + fixed_matrix &assign (const matrix_expression &ae) { + matrix_assign (*this, ae); + return *this; + } + template + BOOST_UBLAS_INLINE + fixed_matrix& operator += (const matrix_expression &ae) { + self_type temporary (*this + ae); + return assign_temporary (temporary); + } + template // Container assignment without temporary + BOOST_UBLAS_INLINE + fixed_matrix &operator += (const matrix_container &m) { + plus_assign (m); + return *this; + } + template + BOOST_UBLAS_INLINE + fixed_matrix &plus_assign (const matrix_expression &ae) { + matrix_assign (*this, ae); + return *this; + } + template + BOOST_UBLAS_INLINE + fixed_matrix& operator -= (const matrix_expression &ae) { + self_type temporary (*this - ae); + return assign_temporary (temporary); + } + template // Container assignment without temporary + BOOST_UBLAS_INLINE + fixed_matrix &operator -= (const matrix_container &m) { + minus_assign (m); + return *this; + } + template + BOOST_UBLAS_INLINE + fixed_matrix &minus_assign (const matrix_expression &ae) { + matrix_assign (*this, ae); + return *this; + } + template + BOOST_UBLAS_INLINE + fixed_matrix& operator *= (const AT &at) { + matrix_assign_scalar (*this, at); + return *this; + } + template + BOOST_UBLAS_INLINE + fixed_matrix& operator /= (const AT &at) { + matrix_assign_scalar (*this, at); + return *this; + } + + // Swapping + BOOST_UBLAS_INLINE + void swap (fixed_matrix &m) { + if (this != &m) { + data ().swap (m.data ()); + } + } + BOOST_UBLAS_INLINE + friend void swap (fixed_matrix &m1, fixed_matrix &m2) { + m1.swap (m2); + } + + // Iterator types + private: + // Use the storage array iterator + typedef typename A::const_iterator const_subiterator_type; + typedef typename A::iterator subiterator_type; + + public: +#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR + typedef indexed_iterator1 iterator1; + typedef indexed_iterator2 iterator2; + typedef indexed_const_iterator1 const_iterator1; + typedef indexed_const_iterator2 const_iterator2; +#else + class const_iterator1; + class iterator1; + class const_iterator2; + class iterator2; +#endif + typedef reverse_iterator_base1 const_reverse_iterator1; + typedef reverse_iterator_base1 reverse_iterator1; + typedef reverse_iterator_base2 const_reverse_iterator2; + typedef reverse_iterator_base2 reverse_iterator2; + + // Element lookup + BOOST_UBLAS_INLINE + const_iterator1 find1 (int /* rank */, size_type i, size_type j) const { +#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR + return const_iterator1 (*this, i, j); +#else + return const_iterator1 (*this, data ().begin () + layout_type::address (i, M, j, N)); +#endif + } + BOOST_UBLAS_INLINE + iterator1 find1 (int /* rank */, size_type i, size_type j) { +#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR + return iterator1 (*this, i, j); +#else + return iterator1 (*this, data ().begin () + layout_type::address (i, M, j, N)); +#endif + } + BOOST_UBLAS_INLINE + const_iterator2 find2 (int /* rank */, size_type i, size_type j) const { +#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR + return const_iterator2 (*this, i, j); +#else + return const_iterator2 (*this, data ().begin () + layout_type::address (i, M, j, N)); +#endif + } + BOOST_UBLAS_INLINE + iterator2 find2 (int /* rank */, size_type i, size_type j) { +#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR + return iterator2 (*this, i, j); +#else + return iterator2 (*this, data ().begin () + layout_type::address (i, M, j, N)); +#endif + } + + +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + class const_iterator1: + public container_const_reference, + public random_access_iterator_base { + public: + typedef typename fixed_matrix::value_type value_type; + typedef typename fixed_matrix::difference_type difference_type; + typedef typename fixed_matrix::const_reference reference; + typedef const typename fixed_matrix::pointer pointer; + + typedef const_iterator2 dual_iterator_type; + typedef const_reverse_iterator2 dual_reverse_iterator_type; + + // Construction and destruction + BOOST_UBLAS_INLINE + const_iterator1 (): + container_const_reference (), it_ () {} + BOOST_UBLAS_INLINE + const_iterator1 (const self_type &m, const const_subiterator_type &it): + container_const_reference (m), it_ (it) {} + BOOST_UBLAS_INLINE + const_iterator1 (const iterator1 &it): + container_const_reference (it ()), it_ (it.it_) {} + + // Arithmetic + BOOST_UBLAS_INLINE + const_iterator1 &operator ++ () { + layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + const_iterator1 &operator -- () { + layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + const_iterator1 &operator += (difference_type n) { + layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + const_iterator1 &operator -= (difference_type n) { + layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + difference_type operator - (const const_iterator1 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ()); + } + + // Dereference + BOOST_UBLAS_INLINE + const_reference operator * () const { + BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ()); + BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ()); + return *it_; + } + BOOST_UBLAS_INLINE + const_reference operator [] (difference_type n) const { + return *(*this + n); + } + +#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_iterator2 begin () const { + const self_type &m = (*this) (); + return m.find2 (1, index1 (), 0); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_iterator2 end () const { + const self_type &m = (*this) (); + return m.find2 (1, index1 (), m.size2 ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_reverse_iterator2 rbegin () const { + return const_reverse_iterator2 (end ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_reverse_iterator2 rend () const { + return const_reverse_iterator2 (begin ()); + } +#endif + + // Indices + BOOST_UBLAS_INLINE + size_type index1 () const { + const self_type &m = (*this) (); + return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ()); + } + BOOST_UBLAS_INLINE + size_type index2 () const { + const self_type &m = (*this) (); + return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ()); + } + + // Assignment + BOOST_UBLAS_INLINE + const_iterator1 &operator = (const const_iterator1 &it) { + container_const_reference::assign (&it ()); + it_ = it.it_; + return *this; + } + + // Comparison + BOOST_UBLAS_INLINE + bool operator == (const const_iterator1 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ == it.it_; + } + BOOST_UBLAS_INLINE + bool operator < (const const_iterator1 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ < it.it_; + } + + private: + const_subiterator_type it_; + + friend class iterator1; + }; +#endif + + BOOST_UBLAS_INLINE + const_iterator1 begin1 () const { + return find1 (0, 0, 0); + } + BOOST_UBLAS_INLINE + const_iterator1 end1 () const { + return find1 (0, M, 0); + } + +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + class iterator1: + public container_reference, + public random_access_iterator_base { + public: + typedef typename fixed_matrix::value_type value_type; + typedef typename fixed_matrix::difference_type difference_type; + typedef typename fixed_matrix::reference reference; + typedef typename fixed_matrix::pointer pointer; + + typedef iterator2 dual_iterator_type; + typedef reverse_iterator2 dual_reverse_iterator_type; + + // Construction and destruction + BOOST_UBLAS_INLINE + iterator1 (): + container_reference (), it_ () {} + BOOST_UBLAS_INLINE + iterator1 (self_type &m, const subiterator_type &it): + container_reference (m), it_ (it) {} + + // Arithmetic + BOOST_UBLAS_INLINE + iterator1 &operator ++ () { + layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + iterator1 &operator -- () { + layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + iterator1 &operator += (difference_type n) { + layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + iterator1 &operator -= (difference_type n) { + layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + difference_type operator - (const iterator1 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ()); + } + + // Dereference + BOOST_UBLAS_INLINE + reference operator * () const { + BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ()); + BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ()); + return *it_; + } + BOOST_UBLAS_INLINE + reference operator [] (difference_type n) const { + return *(*this + n); + } + +#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + iterator2 begin () const { + self_type &m = (*this) (); + return m.find2 (1, index1 (), 0); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + iterator2 end () const { + self_type &m = (*this) (); + return m.find2 (1, index1 (), m.size2 ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + reverse_iterator2 rbegin () const { + return reverse_iterator2 (end ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + reverse_iterator2 rend () const { + return reverse_iterator2 (begin ()); + } +#endif + + // Indices + BOOST_UBLAS_INLINE + size_type index1 () const { + self_type &m = (*this) (); + return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ()); + } + BOOST_UBLAS_INLINE + size_type index2 () const { + self_type &m = (*this) (); + return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ()); + } + + // Assignment + BOOST_UBLAS_INLINE + iterator1 &operator = (const iterator1 &it) { + container_reference::assign (&it ()); + it_ = it.it_; + return *this; + } + + // Comparison + BOOST_UBLAS_INLINE + bool operator == (const iterator1 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ == it.it_; + } + BOOST_UBLAS_INLINE + bool operator < (const iterator1 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ < it.it_; + } + + private: + subiterator_type it_; + + friend class const_iterator1; + }; +#endif + + BOOST_UBLAS_INLINE + iterator1 begin1 () { + return find1 (0, 0, 0); + } + BOOST_UBLAS_INLINE + iterator1 end1 () { + return find1 (0, M, 0); + } + +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + class const_iterator2: + public container_const_reference, + public random_access_iterator_base { + public: + typedef typename fixed_matrix::value_type value_type; + typedef typename fixed_matrix::difference_type difference_type; + typedef typename fixed_matrix::const_reference reference; + typedef const typename fixed_matrix::pointer pointer; + + typedef const_iterator1 dual_iterator_type; + typedef const_reverse_iterator1 dual_reverse_iterator_type; + + // Construction and destruction + BOOST_UBLAS_INLINE + const_iterator2 (): + container_const_reference (), it_ () {} + BOOST_UBLAS_INLINE + const_iterator2 (const self_type &m, const const_subiterator_type &it): + container_const_reference (m), it_ (it) {} + BOOST_UBLAS_INLINE + const_iterator2 (const iterator2 &it): + container_const_reference (it ()), it_ (it.it_) {} + + // Arithmetic + BOOST_UBLAS_INLINE + const_iterator2 &operator ++ () { + layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + const_iterator2 &operator -- () { + layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + const_iterator2 &operator += (difference_type n) { + layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + const_iterator2 &operator -= (difference_type n) { + layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + difference_type operator - (const const_iterator2 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ()); + } + + // Dereference + BOOST_UBLAS_INLINE + const_reference operator * () const { + BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ()); + BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ()); + return *it_; + } + BOOST_UBLAS_INLINE + const_reference operator [] (difference_type n) const { + return *(*this + n); + } + +#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_iterator1 begin () const { + const self_type &m = (*this) (); + return m.find1 (1, 0, index2 ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_iterator1 end () const { + const self_type &m = (*this) (); + return m.find1 (1, m.size1 (), index2 ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_reverse_iterator1 rbegin () const { + return const_reverse_iterator1 (end ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + const_reverse_iterator1 rend () const { + return const_reverse_iterator1 (begin ()); + } +#endif + + // Indices + BOOST_UBLAS_INLINE + size_type index1 () const { + const self_type &m = (*this) (); + return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ()); + } + BOOST_UBLAS_INLINE + size_type index2 () const { + const self_type &m = (*this) (); + return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ()); + } + + // Assignment + BOOST_UBLAS_INLINE + const_iterator2 &operator = (const const_iterator2 &it) { + container_const_reference::assign (&it ()); + it_ = it.it_; + return *this; + } + + // Comparison + BOOST_UBLAS_INLINE + bool operator == (const const_iterator2 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ == it.it_; + } + BOOST_UBLAS_INLINE + bool operator < (const const_iterator2 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ < it.it_; + } + + private: + const_subiterator_type it_; + + friend class iterator2; + }; +#endif + + BOOST_UBLAS_INLINE + const_iterator2 begin2 () const { + return find2 (0, 0, 0); + } + BOOST_UBLAS_INLINE + const_iterator2 end2 () const { + return find2 (0, 0, N); + } + +#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR + class iterator2: + public container_reference, + public random_access_iterator_base { + public: + typedef typename fixed_matrix::value_type value_type; + typedef typename fixed_matrix::difference_type difference_type; + typedef typename fixed_matrix::reference reference; + typedef typename fixed_matrix::pointer pointer; + + typedef iterator1 dual_iterator_type; + typedef reverse_iterator1 dual_reverse_iterator_type; + + // Construction and destruction + BOOST_UBLAS_INLINE + iterator2 (): + container_reference (), it_ () {} + BOOST_UBLAS_INLINE + iterator2 (self_type &m, const subiterator_type &it): + container_reference (m), it_ (it) {} + + // Arithmetic + BOOST_UBLAS_INLINE + iterator2 &operator ++ () { + layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + iterator2 &operator -- () { + layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + iterator2 &operator += (difference_type n) { + layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + iterator2 &operator -= (difference_type n) { + layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ()); + return *this; + } + BOOST_UBLAS_INLINE + difference_type operator - (const iterator2 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ()); + } + + // Dereference + BOOST_UBLAS_INLINE + reference operator * () const { + BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ()); + BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ()); + return *it_; + } + BOOST_UBLAS_INLINE + reference operator [] (difference_type n) const { + return *(*this + n); + } + +#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + iterator1 begin () const { + self_type &m = (*this) (); + return m.find1 (1, 0, index2 ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + iterator1 end () const { + self_type &m = (*this) (); + return m.find1 (1, m.size1 (), index2 ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + reverse_iterator1 rbegin () const { + return reverse_iterator1 (end ()); + } + BOOST_UBLAS_INLINE +#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION + typename self_type:: +#endif + reverse_iterator1 rend () const { + return reverse_iterator1 (begin ()); + } +#endif + + // Indices + BOOST_UBLAS_INLINE + size_type index1 () const { + self_type &m = (*this) (); + return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ()); + } + BOOST_UBLAS_INLINE + size_type index2 () const { + self_type &m = (*this) (); + return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ()); + } + + // Assignment + BOOST_UBLAS_INLINE + iterator2 &operator = (const iterator2 &it) { + container_reference::assign (&it ()); + it_ = it.it_; + return *this; + } + + // Comparison + BOOST_UBLAS_INLINE + bool operator == (const iterator2 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ == it.it_; + } + BOOST_UBLAS_INLINE + bool operator < (const iterator2 &it) const { + BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); + return it_ < it.it_; + } + + private: + subiterator_type it_; + + friend class const_iterator2; + }; +#endif + + BOOST_UBLAS_INLINE + iterator2 begin2 () { + return find2 (0, 0, 0); + } + BOOST_UBLAS_INLINE + iterator2 end2 () { + return find2 (0, 0, N); + } + + // Reverse iterators + + BOOST_UBLAS_INLINE + const_reverse_iterator1 rbegin1 () const { + return const_reverse_iterator1 (end1 ()); + } + BOOST_UBLAS_INLINE + const_reverse_iterator1 rend1 () const { + return const_reverse_iterator1 (begin1 ()); + } + + BOOST_UBLAS_INLINE + reverse_iterator1 rbegin1 () { + return reverse_iterator1 (end1 ()); + } + BOOST_UBLAS_INLINE + reverse_iterator1 rend1 () { + return reverse_iterator1 (begin1 ()); + } + + BOOST_UBLAS_INLINE + const_reverse_iterator2 rbegin2 () const { + return const_reverse_iterator2 (end2 ()); + } + BOOST_UBLAS_INLINE + const_reverse_iterator2 rend2 () const { + return const_reverse_iterator2 (begin2 ()); + } + + BOOST_UBLAS_INLINE + reverse_iterator2 rbegin2 () { + return reverse_iterator2 (end2 ()); + } + BOOST_UBLAS_INLINE + reverse_iterator2 rend2 () { + return reverse_iterator2 (begin2 ()); + } + + // Serialization + template + void serialize(Archive & ar, const unsigned int /* file_version */){ + ar & serialization::make_nvp("data",data_); + } + + private: + array_type data_; + }; + +#endif // BOOST_UBLAS_CPP11 + /** \brief A dense matrix of values of type \c T with a variable size bounded to a maximum of \f$M\f$ by \f$N\f$. * * For a \f$(m \times n)\f$-dimensional matrix and \f$ 0 \leq i < m, 0 \leq j < n\f$, every element \f$m_{i,j}\f$ is mapped @@ -1176,6 +2160,7 @@ namespace boost { namespace numeric { } }; + /** \brief A dense matrix of values of type \c T stored as a vector of vectors. * * Rows or columns are not stored into contiguous chunks of memory but data inside rows (or columns) are. diff --git a/include/boost/numeric/ublas/traits.hpp b/include/boost/numeric/ublas/traits.hpp index ae020700..fae27f8a 100644 --- a/include/boost/numeric/ublas/traits.hpp +++ b/include/boost/numeric/ublas/traits.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #include // anonymous namespace to avoid ADL issues @@ -36,23 +37,120 @@ namespace { // we'll find either std::sqrt or else another version via ADL: return sqrt (t); } - template T boost_numeric_ublas_abs (const T& t) { - using namespace std; + // template T boost_numeric_ublas_abs (const T& t) { + // using namespace std; // we'll find either std::abs or else another version via ADL: - return abs (t); - } + // return abs (t); + // } + +template +typename boost::disable_if< + boost::is_unsigned, T >::type + inline boost_numeric_ublas_abs (const T &t ) { + using namespace std; + return abs( t ); + } + // unsigned types are always non-negative - template<> unsigned int boost_numeric_ublas_abs (const unsigned int& t) { - return t; - } + //template<> inline unsigned int boost_numeric_ublas_abs (const unsigned int& t) { + // return t; + //} // unsigned types are always non-negative - template<> unsigned long boost_numeric_ublas_abs (const unsigned long& t) { - return t; - } + //template<> inline unsigned long boost_numeric_ublas_abs (const unsigned long& t) { + // return t; + //} + +template +typename boost::enable_if< + boost::is_unsigned, T >::type + inline boost_numeric_ublas_abs (const T &t ) { + return t; + } } namespace boost { namespace numeric { namespace ublas { + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator+ (I in1, std::complex const& in2 ) { + return R (in1) + in2; + } + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator+ (std::complex const& in1, I in2) { + return in1 + R (in2); + } + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator- (I in1, std::complex const& in2) { + return R (in1) - in2; + } + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator- (std::complex const& in1, I in2) { + return in1 - R (in2); + } + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator* (I in1, std::complex const& in2) { + return R (in1) * in2; + } + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator* (std::complex const& in1, I in2) { + return in1 * R(in2); + } + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator/ (I in1, std::complex const& in2) { + return R(in1) / in2; + } + + template + typename boost::enable_if< + mpl::and_< + boost::is_float, + boost::is_integral + >, + std::complex >::type inline operator/ (std::complex const& in1, I in2) { + return in1 / R (in2); + } + // Use Joel de Guzman's return type deduction // uBLAS assumes a common return type for all binary arithmetic operators template @@ -73,86 +171,6 @@ namespace boost { namespace numeric { namespace ublas { typedef typename id::type promote_type; }; - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator+ (I in1, std::complex const& in2 ) { - return R (in1) + in2; - } - - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator+ (std::complex const& in1, I in2) { - return in1 + R (in2); - } - - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator- (I in1, std::complex const& in2) { - return R (in1) - in2; - } - - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator- (std::complex const& in1, I in2) { - return in1 - R (in2); - } - - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator* (I in1, std::complex const& in2) { - return R (in1) * in2; - } - - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator* (std::complex const& in1, I in2) { - return in1 * R(in2); - } - - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator/ (I in1, std::complex const& in2) { - return R(in1) / in2; - } - - template - typename boost::enable_if< - mpl::and_< - boost::is_float, - boost::is_integral - >, - std::complex >::type inline operator/ (std::complex const& in1, I in2) { - return in1 / R (in2); - } - // Type traits - generic numeric properties and functions diff --git a/include/boost/numeric/ublas/vector.hpp b/include/boost/numeric/ublas/vector.hpp index b97e1fc8..f6e0a1d1 100644 --- a/include/boost/numeric/ublas/vector.hpp +++ b/include/boost/numeric/ublas/vector.hpp @@ -1,6 +1,7 @@ // // Copyright (c) 2000-2010 // Joerg Walter, Mathias Koch, David Bellot +// Copyright (c) 2014, Athanasios Iliopoulos // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at @@ -797,15 +798,15 @@ namespace boost { namespace numeric { namespace ublas { }; - +#ifdef BOOST_UBLAS_CPP11 /** \brief A dense vector of values of type \c T. * * For a \f$n\f$-dimensional vector \f$v\f$ and \f$0\leq i < n\f$ every element \f$v_i\f$ is mapped - * to the \f$i\f$-th element of the container. A storage type \c A can be specified which defaults to \c unbounded_array. + * to the \f$i\f$-th element of the container. A storage type \c A can be specified which defaults to \c std::array. * Elements are constructed by \c A, which need not initialise their value. * * \tparam T type of the objects stored in the vector (like int, double, complex,...) - * \tparam A The type of the storage array of the vector. Default is \c unbounded_array. \c and \c std::vector can also be used + * \tparam A The type of the storage array of the vector. Default is \c std::array. */ template class fixed_vector: @@ -848,10 +849,12 @@ namespace boost { namespace numeric { namespace ublas { /// \brief Constructor of a fixed_vector with a predefined size and a unique initial value /// \param init value to assign to each element of the vector - BOOST_UBLAS_INLINE - fixed_vector (const value_type &init): - vector_container (), - data_ ( init) {} + BOOST_UBLAS_INLINE + fixed_vector (const value_type &init): + vector_container (), + data_ () { + data_.fill( init ); + } /// \brief Copy-constructor of a fixed_vector /// \param v is the fixed_vector to be duplicated @@ -875,8 +878,10 @@ namespace boost { namespace numeric { namespace ublas { /// \brief Construct a fixed_vector from a list of values /// The list should be included in curly braces. Typical syntax is: /// fixed_vector v = { 1, 2, 3 } or fixed_vector v( {1, 2, 3} ) or fixed_vector v( 1, 2, 3 ) - template - fixed_vector(U v0, Types... vrest) : data_{ { v0, vrest... } } {} + template + fixed_vector(value_type v0, Types... vrest) : + vector_container (), + data_{ { v0, vrest... } } {} // ----------------------- // Random Access Container @@ -902,7 +907,7 @@ namespace boost { namespace numeric { namespace ublas { /// \brief Return the size of the vector BOOST_UBLAS_INLINE - constexpr size_type size () { // should have a const after C++14 + constexpr size_type size () const{ // should have a const after C++14 return data_.size (); } @@ -1538,6 +1543,7 @@ namespace boost { namespace numeric { namespace ublas { array_type data_; }; +#endif // BOOST_UBLAS_CPP11 // -------------------- // Bounded vector class diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 2e8ac451..f238e3b7 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -213,6 +213,8 @@ test-suite numeric/uBLAS : ] [ run test_fixed_containers.cpp - : : : BOOST_UBLAS_CPP11 + : + : + : gcc:-std=c++11 clang:-std=c++11 intel:-std=c++11 BOOST_UBLAS_CPP11 ] ; diff --git a/test/test_fixed_containers.cpp b/test/test_fixed_containers.cpp index 2e4e524b..8a0a07d5 100644 --- a/test/test_fixed_containers.cpp +++ b/test/test_fixed_containers.cpp @@ -25,66 +25,74 @@ bool test_vector( std::string type_name) bool pass = true; { - typedef fixed_vector vec3; + typedef fixed_vector vec1; - vec3 v1; + vec1 v1( 122.0 ); - pass &=(sizeof( vec3 ) == v1.size()*sizeof( T ) ) ; + pass &= ( v1(0) == (T)122 ); - vector v( 3, 0 ) ; + } - pass &= compare( v1, v ); + { + typedef fixed_vector vec3; - v1 <<= 10.0, 10, 33; - v <<= 10.0, 10, 33; + vec3 v1((T)0.0, (T)0.0, (T)0.0); - //cout << std::setprecision(20) << v1 << '\n' << v; + pass &=(sizeof( vec3 ) == v1.size()*sizeof( T ) ) ; - pass &= compare( v1, v ); + vector v( 3, 0 ) ; + + pass &= compare( v1, v ); + + v1 <<= 10.0, 10, 33; + v <<= 10.0, 10, 33; + + //cout << std::setprecision(20) << v1 << '\n' << v; + + pass &= compare( v1, v ); - vec3 v2; + vec3 v2; - v2( 0 ) = 10.0; v2( 1 ) = 10; v2( 2 ) = 33; - pass &= compare( v, v2 ); + v2( 0 ) = 10.0; v2( 1 ) = 10; v2( 2 ) = 33; + pass &= compare( v, v2 ); + + v2 += v; + + pass &= compare( v2, 2*v ); - v2 += v; - - pass &= compare( v2, 2*v ); + v1 = 2*v1 + v - 6*v2; + pass &= compare( v1, (3-2*6)*v ); - v1 = 2*v1 + v - 6*v2; - pass &= compare( v1, (3-2*6)*v ); + vec3 v3{ (T)-90.0, (T)-90.0, (T)-297.0 }; + pass &= compare( v3, v1 ); + vec3 v4 = { (T)-90.0, (T)-90.0, (T)-297.0 }; + pass &= compare( v4, v1 ); - vec3 v3{ (T)-90.0, (T)-90.0, (T)-297.0 }; - pass &= compare( v3, v1 ); + vec3 v5( (T)-90.0, (T)-90.0, (T)-297.0 ); + pass &= compare( v5, v1 ); - vec3 v4 = { (T)-90.0, (T)-90.0, (T)-297.0 }; - pass &= compare( v4, v1 ); + vec3 v6((T) 5.0, (T)8.0, (T)9.0); - vec3 v5( (T)-90.0, (T)-90.0, (T)-297.0 ); - pass &= compare( v5, v1 ); + matrix M = outer_prod( v6, v6), L( 3, 3); - vec3 v6((T) 5.0, (T)8.0, (T)9.0); + L <<= 25, 40, 45, 40, 64, 72, 45, 72, 81; - matrix M = outer_prod( v6, v6), L( 3, 3); + pass &= compare( M, L ); - L <<= 25, 40, 45, 40, 64, 72, 45, 72, 81; + L <<= 1, 2, 3, 4, 5, 6, 7, 8, 9; + v6 <<= 4, 5, 6; + vec3 v7 ( (T)32.0, (T)77.0, (T)122.0 ); - pass &= compare( M, L ); + pass &= compare( v7, prod(L, v6) ); - L <<= 1, 2, 3, 4, 5, 6, 7, 8, 9; - v6 <<= 4, 5, 6; - vec3 v7 ( (T)32.0, (T)77.0, (T)122.0 ); + vec3 v8; + noalias( v8 ) = prod(L, v6); - pass &= compare( v7, prod(L, v6) ); - - vec3 v8; - noalias( v8 ) = prod(L, v6); - - pass &= compare( v7, v8 ); + pass &= compare( v7, v8 ); } @@ -126,9 +134,9 @@ bool test_vector( std::string type_name) pass &= compare( v1, v ); - try { + // Check if bad index indeed works + try { T a; - v1( 100 ); a=v1( 100 ); (void) a ; @@ -142,6 +150,121 @@ bool test_vector( std::string type_name) return pass; } +template < class T > +bool test_matrix( std::string type_name) +{ + std::stringstream stream; + stream << "Testing for: " << type_name; + BOOST_UBLAS_DEBUG_TRACE( stream.str() ); + + bool pass = true; + + typedef fixed_matrix mat34; + typedef fixed_matrix mat43; + typedef fixed_matrix mat33; + + + { + typedef fixed_matrix mat1; + + mat1 m1( 122.0 ); + + pass &= ( m1(0, 0) == (T)122 ); + } + + + { + mat34 m1( 3.0 ); + + pass &=(sizeof( mat34 ) == m1.size1()*m1.size2()*sizeof( T ) ) ; + + matrix m( 3.0, 4.0, 3.0 ) ; + + pass &= compare( m1, m ); + + cout << m1 << endl; + cout << m << endl; + + + m1 <<= 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; + m <<= 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; + + pass &= compare( m1, m ); + + cout << m1 << endl; + cout << m << endl; + + mat34 m2( 0.0 ); + + T count = 1 ; + for ( std::size_t i = 0; i != m2.size1(); i++) + { + for (std::size_t j = 0; j!= m2.size2(); j++) + { + m2( i, j ) = count; + count = count + 1; + } + + } + pass &= compare( m2, m ); + cout << m2 << endl; + + } + { + mat34 m1 = { (T)1, (T)2, (T)3, (T)3, (T)3, (T)2, (T)5, (T)4, (T)2, (T)6, (T)5, (T)2 }; + mat43 m2 = { (T)4, (T)5, (T)6, (T)3, (T)2, (T)2, (T)1, (T)4, (T)2, (T)6, (T)5, (T)2 }; + + mat33 m3 = prod(m1, m2); + + matrix m(3, 3); + m <<= 31,36,22,47,59,40,43,52,38; + + pass &= compare(m ,m3); + + mat33 m4; + m4 <<= (T)1, (T)2, (T)1, (T)2, (T)1, (T)3, (T)1, (T)2, (T) 5; + m3 = prod(m4, trans(m4)); + + m<<=6,7,10,7,14,19,10,19,30; + + cout << m3 << endl; + pass &= compare(m ,m3); + + m3 = 2 * m4 - 1 * m3; + + cout << m3; + + m <<= -4,-3,-8,-3,-12,-13,-8,-15,-20; + + pass &= compare(m, m3); + + m = m3; + + m3 = trans(m); + + pass &= compare(m3, trans(m)); + + // Check if bad index indeed works + try { + T a; + a=m1( 100, 100 ); + (void) a ; + + } catch ( bad_index &e) { + std::cout << " Caught: " << e.what() << endl; + pass &= true; + } + + } + + + + + + return pass; + +} + BOOST_UBLAS_TEST_DEF (test_vector) { BOOST_UBLAS_DEBUG_TRACE( "Starting fixed container tests" ); @@ -153,6 +276,14 @@ BOOST_UBLAS_TEST_DEF (test_vector) { BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); + + BOOST_UBLAS_TEST_CHECK( test_matrix< double >( "double") ); + BOOST_UBLAS_TEST_CHECK( test_vector< float >( "float") ); + BOOST_UBLAS_TEST_CHECK( test_vector< int >( "int") ); + + BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); + BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); + BOOST_UBLAS_TEST_CHECK( test_vector< std::complex >( "std::complex") ); } int main () { @@ -170,3 +301,4 @@ int main () { return EXIT_SUCCESS; } +