| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763 |
- //
- // Copyright (c) 2000-2002
- // Joerg Walter, Mathias Koch
- //
- // Distributed under the Boost Software License, Version 1.0. (See
- // accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- //
- // The authors gratefully acknowledge the support of
- // GeNeSys mbH & Co. KG in producing this work.
- //
- #ifndef _BOOST_UBLAS_VECTOR_EXPRESSION_
- #define _BOOST_UBLAS_VECTOR_EXPRESSION_
- #include <boost/numeric/ublas/expression_types.hpp>
- // Expression templates based on ideas of Todd Veldhuizen and Geoffrey Furnish
- // Iterators based on ideas of Jeremy Siek
- //
- // Classes that model the Vector Expression concept
- namespace boost { namespace numeric { namespace ublas {
- template<class E>
- class vector_reference:
- public vector_expression<vector_reference<E> > {
- typedef vector_reference<E> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_expression<vector_reference<E> >::operator ();
- #endif
- typedef typename E::size_type size_type;
- typedef typename E::difference_type difference_type;
- typedef typename E::value_type value_type;
- typedef typename E::const_reference const_reference;
- typedef typename boost::mpl::if_<boost::is_const<E>,
- typename E::const_reference,
- typename E::reference>::type reference;
- typedef E referred_type;
- typedef const self_type const_closure_type;
- typedef self_type closure_type;
- typedef typename E::storage_category storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- explicit vector_reference (referred_type &e):
- e_ (e) {}
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return expression ().size ();
- }
- public:
- // Expression accessors - const correct
- BOOST_UBLAS_INLINE
- const referred_type &expression () const {
- return e_;
- }
- BOOST_UBLAS_INLINE
- referred_type &expression () {
- return e_;
- }
- public:
- // Element access
- #ifndef BOOST_UBLAS_REFERENCE_CONST_MEMBER
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- return expression () (i);
- }
- BOOST_UBLAS_INLINE
- reference operator () (size_type i) {
- return expression () (i);
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return expression () [i];
- }
- BOOST_UBLAS_INLINE
- reference operator [] (size_type i) {
- return expression () [i];
- }
- #else
- BOOST_UBLAS_INLINE
- reference operator () (size_type i) const {
- return expression () (i);
- }
- BOOST_UBLAS_INLINE
- reference operator [] (size_type i) const {
- return expression () [i];
- }
- #endif
- // Assignment
- BOOST_UBLAS_INLINE
- vector_reference &operator = (const vector_reference &v) {
- expression ().operator = (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- vector_reference &operator = (const vector_expression<AE> &ae) {
- expression ().operator = (ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- vector_reference &assign (const vector_expression<AE> &ae) {
- expression ().assign (ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- vector_reference &operator += (const vector_expression<AE> &ae) {
- expression ().operator += (ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- vector_reference &plus_assign (const vector_expression<AE> &ae) {
- expression ().plus_assign (ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- vector_reference &operator -= (const vector_expression<AE> &ae) {
- expression ().operator -= (ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- vector_reference &minus_assign (const vector_expression<AE> &ae) {
- expression ().minus_assign (ae);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- vector_reference &operator *= (const AT &at) {
- expression ().operator *= (at);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- vector_reference &operator /= (const AT &at) {
- expression ().operator /= (at);
- return *this;
- }
- // Swapping
- BOOST_UBLAS_INLINE
- void swap (vector_reference &v) {
- expression ().swap (v.expression ());
- }
- // Closure comparison
- BOOST_UBLAS_INLINE
- bool same_closure (const vector_reference &vr) const {
- return &(*this).e_ == &vr.e_;
- }
- // Iterator types
- typedef typename E::const_iterator const_iterator;
- typedef typename boost::mpl::if_<boost::is_const<E>,
- typename E::const_iterator,
- typename E::iterator>::type iterator;
- // Element lookup
- BOOST_UBLAS_INLINE
- const_iterator find (size_type i) const {
- return expression ().find (i);
- }
- BOOST_UBLAS_INLINE
- iterator find (size_type i) {
- return expression ().find (i);
- }
- // Iterator is the iterator of the referenced expression.
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return expression ().begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return expression ().end ();
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end ();
- }
- BOOST_UBLAS_INLINE
- iterator begin () {
- return expression ().begin ();
- }
- BOOST_UBLAS_INLINE
- iterator end () {
- return expression ().end ();
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- typedef reverse_iterator_base<iterator> reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return rend ();
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rbegin () {
- return reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rend () {
- return reverse_iterator (begin ());
- }
- private:
- referred_type &e_;
- };
- template<class E, class F>
- class vector_unary:
- public vector_expression<vector_unary<E, F> > {
- typedef F functor_type;
- typedef typename boost::mpl::if_<boost::is_same<F, scalar_identity<typename E::value_type> >,
- E,
- const E>::type expression_type;
- typedef typename boost::mpl::if_<boost::is_const<expression_type>,
- typename E::const_closure_type,
- typename E::closure_type>::type expression_closure_type;
- typedef vector_unary<E, F> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_expression<vector_unary<E, F> >::operator ();
- #endif
- typedef typename E::size_type size_type;
- typedef typename E::difference_type difference_type;
- typedef typename F::result_type value_type;
- typedef value_type const_reference;
- typedef typename boost::mpl::if_<boost::is_same<F, scalar_identity<value_type> >,
- typename E::reference,
- value_type>::type reference;
- typedef const self_type const_closure_type;
- typedef self_type closure_type;
- typedef unknown_storage_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- // May be used as mutable expression.
- explicit vector_unary (expression_type &e):
- e_ (e) {}
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return e_.size ();
- }
- public:
- // Expression accessors
- BOOST_UBLAS_INLINE
- const expression_closure_type &expression () const {
- return e_;
- }
- public:
- // Element access
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- return functor_type::apply (e_ (i));
- }
- BOOST_UBLAS_INLINE
- reference operator () (size_type i) {
- BOOST_STATIC_ASSERT ((boost::is_same<functor_type, scalar_identity<value_type > >::value));
- return e_ (i);
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return functor_type::apply (e_ [i]);
- }
- BOOST_UBLAS_INLINE
- reference operator [] (size_type i) {
- BOOST_STATIC_ASSERT ((boost::is_same<functor_type, scalar_identity<value_type > >::value));
- return e_ [i];
- }
- // Closure comparison
- BOOST_UBLAS_INLINE
- bool same_closure (const vector_unary &vu) const {
- return (*this).expression ().same_closure (vu.expression ());
- }
- // Iterator types
- private:
- typedef typename E::const_iterator const_subiterator_type;
- typedef const value_type *const_pointer;
- public:
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- typedef indexed_const_iterator<const_closure_type, typename const_subiterator_type::iterator_category> const_iterator;
- typedef const_iterator iterator;
- #else
- class const_iterator;
- typedef const_iterator iterator;
- #endif
- // Element lookup
- BOOST_UBLAS_INLINE
- const_iterator find (size_type i) const {
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- const_subiterator_type it (e_.find (i));
- return const_iterator (*this, it.index ());
- #else
- return const_iterator (*this, e_.find (i));
- #endif
- }
- // Iterator enhances the iterator of the referenced expression
- // with the unary functor.
- #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
- class const_iterator:
- public container_const_reference<vector_unary>,
- public iterator_base_traits<typename E::const_iterator::iterator_category>::template
- iterator_base<const_iterator, value_type>::type {
- public:
- typedef typename E::const_iterator::iterator_category iterator_category;
- typedef typename vector_unary::difference_type difference_type;
- typedef typename vector_unary::value_type value_type;
- typedef typename vector_unary::const_reference reference;
- typedef typename vector_unary::const_pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- const_iterator ():
- container_const_reference<self_type> (), it_ () {}
- BOOST_UBLAS_INLINE
- const_iterator (const self_type &vu, const const_subiterator_type &it):
- container_const_reference<self_type> (vu), it_ (it) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- const_iterator &operator ++ () {
- ++ it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -- () {
- -- it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator += (difference_type n) {
- it_ += n;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -= (difference_type n) {
- it_ -= n;
- return *this;
- }
- BOOST_UBLAS_INLINE
- difference_type operator - (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- return it_ - it.it_;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- const_reference operator * () const {
- return functor_type::apply (*it_);
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (difference_type n) const {
- return *(*this + n);
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- return it_.index ();
- }
- // Assignment
- BOOST_UBLAS_INLINE
- const_iterator &operator = (const const_iterator &it) {
- container_const_reference<self_type>::assign (&it ());
- it_ = it.it_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- return it_ == it.it_;
- }
- BOOST_UBLAS_INLINE
- bool operator < (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- return it_ < it.it_;
- }
- private:
- const_subiterator_type it_;
- };
- #endif
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return find (size ());
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end ();
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return rend ();
- }
- private:
- expression_closure_type e_;
- };
- template<class E, class F>
- struct vector_unary_traits {
- typedef vector_unary<E, F> expression_type;
- //FIXME
- // #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
- typedef expression_type result_type;
- // #else
- // typedef typename E::vector_temporary_type result_type;
- // #endif
- };
- // (- v) [i] = - v [i]
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_unary_traits<E, scalar_negate<typename E::value_type> >::result_type
- operator - (const vector_expression<E> &e) {
- typedef typename vector_unary_traits<E, scalar_negate<typename E::value_type> >::expression_type expression_type;
- return expression_type (e ());
- }
- // (conj v) [i] = conj (v [i])
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::result_type
- conj (const vector_expression<E> &e) {
- typedef typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::expression_type expression_type;
- return expression_type (e ());
- }
- // (real v) [i] = real (v [i])
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_unary_traits<E, scalar_real<typename E::value_type> >::result_type
- real (const vector_expression<E> &e) {
- typedef typename vector_unary_traits<E, scalar_real<typename E::value_type> >::expression_type expression_type;
- return expression_type (e ());
- }
- // (imag v) [i] = imag (v [i])
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_unary_traits<E, scalar_imag<typename E::value_type> >::result_type
- imag (const vector_expression<E> &e) {
- typedef typename vector_unary_traits<E, scalar_imag<typename E::value_type> >::expression_type expression_type;
- return expression_type (e ());
- }
- // (trans v) [i] = v [i]
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_unary_traits<const E, scalar_identity<typename E::value_type> >::result_type
- trans (const vector_expression<E> &e) {
- typedef typename vector_unary_traits<const E, scalar_identity<typename E::value_type> >::expression_type expression_type;
- return expression_type (e ());
- }
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_unary_traits<E, scalar_identity<typename E::value_type> >::result_type
- trans (vector_expression<E> &e) {
- typedef typename vector_unary_traits<E, scalar_identity<typename E::value_type> >::expression_type expression_type;
- return expression_type (e ());
- }
- // (herm v) [i] = conj (v [i])
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::result_type
- herm (const vector_expression<E> &e) {
- typedef typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::expression_type expression_type;
- return expression_type (e ());
- }
- template<class E1, class E2, class F>
- class vector_binary:
- public vector_expression<vector_binary<E1, E2, F> > {
- typedef E1 expression1_type;
- typedef E2 expression2_type;
- typedef F functor_type;
- typedef typename E1::const_closure_type expression1_closure_type;
- typedef typename E2::const_closure_type expression2_closure_type;
- typedef vector_binary<E1, E2, F> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_expression<vector_binary<E1, E2, F> >::operator ();
- #endif
- typedef typename promote_traits<typename E1::size_type, typename E2::size_type>::promote_type size_type;
- typedef typename promote_traits<typename E1::difference_type, typename E2::difference_type>::promote_type difference_type;
- typedef typename F::result_type value_type;
- typedef value_type const_reference;
- typedef const_reference reference;
- typedef const self_type const_closure_type;
- typedef const_closure_type closure_type;
- typedef unknown_storage_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- vector_binary (const expression1_type &e1, const expression2_type &e2):
- e1_ (e1), e2_ (e2) {}
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return BOOST_UBLAS_SAME (e1_.size (), e2_.size ());
- }
- private:
- // Accessors
- BOOST_UBLAS_INLINE
- const expression1_closure_type &expression1 () const {
- return e1_;
- }
- BOOST_UBLAS_INLINE
- const expression2_closure_type &expression2 () const {
- return e2_;
- }
- public:
- // Element access
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- return functor_type::apply (e1_ (i), e2_ (i));
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return functor_type::apply (e1_ [i], e2_ [i]);
- }
- // Closure comparison
- BOOST_UBLAS_INLINE
- bool same_closure (const vector_binary &vb) const {
- return (*this).expression1 ().same_closure (vb.expression1 ()) &&
- (*this).expression2 ().same_closure (vb.expression2 ());
- }
- // Iterator types
- private:
- typedef typename E1::const_iterator const_subiterator1_type;
- typedef typename E2::const_iterator const_subiterator2_type;
- typedef const value_type *const_pointer;
- public:
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- typedef typename iterator_restrict_traits<typename const_subiterator1_type::iterator_category,
- typename const_subiterator2_type::iterator_category>::iterator_category iterator_category;
- typedef indexed_const_iterator<const_closure_type, iterator_category> const_iterator;
- typedef const_iterator iterator;
- #else
- class const_iterator;
- typedef const_iterator iterator;
- #endif
- // Element lookup
- BOOST_UBLAS_INLINE
- const_iterator find (size_type i) const {
- const_subiterator1_type it1 (e1_.find (i));
- const_subiterator1_type it1_end (e1_.find (size ()));
- const_subiterator2_type it2 (e2_.find (i));
- const_subiterator2_type it2_end (e2_.find (size ()));
- i = (std::min) (it1 != it1_end ? it1.index () : size (),
- it2 != it2_end ? it2.index () : size ());
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- return const_iterator (*this, i);
- #else
- return const_iterator (*this, i, it1, it1_end, it2, it2_end);
- #endif
- }
- // Iterator merges the iterators of the referenced expressions and
- // enhances them with the binary functor.
- #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
- class const_iterator:
- public container_const_reference<vector_binary>,
- public iterator_base_traits<typename iterator_restrict_traits<typename E1::const_iterator::iterator_category,
- typename E2::const_iterator::iterator_category>::iterator_category>::template
- iterator_base<const_iterator, value_type>::type {
- public:
- typedef typename iterator_restrict_traits<typename E1::const_iterator::iterator_category,
- typename E2::const_iterator::iterator_category>::iterator_category iterator_category;
- typedef typename vector_binary::difference_type difference_type;
- typedef typename vector_binary::value_type value_type;
- typedef typename vector_binary::const_reference reference;
- typedef typename vector_binary::const_pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- const_iterator ():
- container_const_reference<self_type> (), i_ (), it1_ (), it1_end_ (), it2_ (), it2_end_ () {}
- BOOST_UBLAS_INLINE
- const_iterator (const self_type &vb, size_type i,
- const const_subiterator1_type &it1, const const_subiterator1_type &it1_end,
- const const_subiterator2_type &it2, const const_subiterator2_type &it2_end):
- container_const_reference<self_type> (vb), i_ (i), it1_ (it1), it1_end_ (it1_end), it2_ (it2), it2_end_ (it2_end) {}
- private:
- // Dense specializations
- BOOST_UBLAS_INLINE
- void increment (dense_random_access_iterator_tag) {
- ++ i_; ++ it1_; ++ it2_;
- }
- BOOST_UBLAS_INLINE
- void decrement (dense_random_access_iterator_tag) {
- -- i_; -- it1_; -- it2_;
- }
- BOOST_UBLAS_INLINE
- void increment (dense_random_access_iterator_tag, difference_type n) {
- i_ += n; it1_ += n; it2_ += n;
- }
- BOOST_UBLAS_INLINE
- void decrement (dense_random_access_iterator_tag, difference_type n) {
- i_ -= n; it1_ -= n; it2_ -= n;
- }
- BOOST_UBLAS_INLINE
- value_type dereference (dense_random_access_iterator_tag) const {
- return functor_type::apply (*it1_, *it2_);
- }
- // Packed specializations
- BOOST_UBLAS_INLINE
- void increment (packed_random_access_iterator_tag) {
- if (it1_ != it1_end_)
- if (it1_.index () <= i_)
- ++ it1_;
- if (it2_ != it2_end_)
- if (it2_.index () <= i_)
- ++ it2_;
- ++ i_;
- }
- BOOST_UBLAS_INLINE
- void decrement (packed_random_access_iterator_tag) {
- if (it1_ != it1_end_)
- if (i_ <= it1_.index ())
- -- it1_;
- if (it2_ != it2_end_)
- if (i_ <= it2_.index ())
- -- it2_;
- -- i_;
- }
- BOOST_UBLAS_INLINE
- void increment (packed_random_access_iterator_tag, difference_type n) {
- while (n > 0) {
- increment (packed_random_access_iterator_tag ());
- --n;
- }
- while (n < 0) {
- decrement (packed_random_access_iterator_tag ());
- ++n;
- }
- }
- BOOST_UBLAS_INLINE
- void decrement (packed_random_access_iterator_tag, difference_type n) {
- while (n > 0) {
- decrement (packed_random_access_iterator_tag ());
- --n;
- }
- while (n < 0) {
- increment (packed_random_access_iterator_tag ());
- ++n;
- }
- }
- BOOST_UBLAS_INLINE
- value_type dereference (packed_random_access_iterator_tag) const {
- typename E1::value_type t1 = typename E1::value_type/*zero*/();
- if (it1_ != it1_end_)
- if (it1_.index () == i_)
- t1 = *it1_;
- typename E2::value_type t2 = typename E2::value_type/*zero*/();
- if (it2_ != it2_end_)
- if (it2_.index () == i_)
- t2 = *it2_;
- return functor_type::apply (t1, t2);
- }
- // Sparse specializations
- BOOST_UBLAS_INLINE
- void increment (sparse_bidirectional_iterator_tag) {
- size_type index1 = (*this) ().size ();
- if (it1_ != it1_end_) {
- if (it1_.index () <= i_)
- ++ it1_;
- if (it1_ != it1_end_)
- index1 = it1_.index ();
- }
- size_type index2 = (*this) ().size ();
- if (it2_ != it2_end_) {
- if (it2_.index () <= i_)
- ++ it2_;
- if (it2_ != it2_end_)
- index2 = it2_.index ();
- }
- i_ = (std::min) (index1, index2);
- }
- BOOST_UBLAS_INLINE
- void decrement (sparse_bidirectional_iterator_tag) {
- size_type index1 = (*this) ().size ();
- if (it1_ != it1_end_) {
- if (i_ <= it1_.index ())
- -- it1_;
- if (it1_ != it1_end_)
- index1 = it1_.index ();
- }
- size_type index2 = (*this) ().size ();
- if (it2_ != it2_end_) {
- if (i_ <= it2_.index ())
- -- it2_;
- if (it2_ != it2_end_)
- index2 = it2_.index ();
- }
- i_ = (std::max) (index1, index2);
- }
- BOOST_UBLAS_INLINE
- void increment (sparse_bidirectional_iterator_tag, difference_type n) {
- while (n > 0) {
- increment (sparse_bidirectional_iterator_tag ());
- --n;
- }
- while (n < 0) {
- decrement (sparse_bidirectional_iterator_tag ());
- ++n;
- }
- }
- BOOST_UBLAS_INLINE
- void decrement (sparse_bidirectional_iterator_tag, difference_type n) {
- while (n > 0) {
- decrement (sparse_bidirectional_iterator_tag ());
- --n;
- }
- while (n < 0) {
- increment (sparse_bidirectional_iterator_tag ());
- ++n;
- }
- }
- BOOST_UBLAS_INLINE
- value_type dereference (sparse_bidirectional_iterator_tag) const {
- typename E1::value_type t1 = typename E1::value_type/*zero*/();
- if (it1_ != it1_end_)
- if (it1_.index () == i_)
- t1 = *it1_;
- typename E2::value_type t2 = typename E2::value_type/*zero*/();
- if (it2_ != it2_end_)
- if (it2_.index () == i_)
- t2 = *it2_;
- return static_cast<value_type>(functor_type::apply (t1, t2));
- }
- public:
- // Arithmetic
- BOOST_UBLAS_INLINE
- const_iterator &operator ++ () {
- increment (iterator_category ());
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -- () {
- decrement (iterator_category ());
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator += (difference_type n) {
- increment (iterator_category (), n);
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -= (difference_type n) {
- decrement (iterator_category (), n);
- return *this;
- }
- BOOST_UBLAS_INLINE
- difference_type operator - (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- return index () - it.index ();
- }
- // Dereference
- BOOST_UBLAS_INLINE
- const_reference operator * () const {
- return dereference (iterator_category ());
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (difference_type n) const {
- return *(*this + n);
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- return i_;
- }
- // Assignment
- BOOST_UBLAS_INLINE
- const_iterator &operator = (const const_iterator &it) {
- container_const_reference<self_type>::assign (&it ());
- i_ = it.i_;
- it1_ = it.it1_;
- it1_end_ = it.it1_end_;
- it2_ = it.it2_;
- it2_end_ = it.it2_end_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- return index () == it.index ();
- }
- BOOST_UBLAS_INLINE
- bool operator < (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- return index () < it.index ();
- }
- private:
- size_type i_;
- const_subiterator1_type it1_;
- const_subiterator1_type it1_end_;
- const_subiterator2_type it2_;
- const_subiterator2_type it2_end_;
- };
- #endif
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return find (size ());
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end ();
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return rend ();
- }
- private:
- expression1_closure_type e1_;
- expression2_closure_type e2_;
- };
- template<class E1, class E2, class F>
- struct vector_binary_traits {
- typedef vector_binary<E1, E2, F> expression_type;
- #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
- typedef expression_type result_type;
- #else
- typedef typename E1::vector_temporary_type result_type;
- #endif
- };
- // (v1 + v2) [i] = v1 [i] + v2 [i]
- template<class E1, class E2>
- BOOST_UBLAS_INLINE
- typename vector_binary_traits<E1, E2, scalar_plus<typename E1::value_type,
- typename E2::value_type> >::result_type
- operator + (const vector_expression<E1> &e1,
- const vector_expression<E2> &e2) {
- typedef typename vector_binary_traits<E1, E2, scalar_plus<typename E1::value_type,
- typename E2::value_type> >::expression_type expression_type;
- return expression_type (e1 (), e2 ());
- }
- // (v1 - v2) [i] = v1 [i] - v2 [i]
- template<class E1, class E2>
- BOOST_UBLAS_INLINE
- typename vector_binary_traits<E1, E2, scalar_minus<typename E1::value_type,
- typename E2::value_type> >::result_type
- operator - (const vector_expression<E1> &e1,
- const vector_expression<E2> &e2) {
- typedef typename vector_binary_traits<E1, E2, scalar_minus<typename E1::value_type,
- typename E2::value_type> >::expression_type expression_type;
- return expression_type (e1 (), e2 ());
- }
- // (v1 * v2) [i] = v1 [i] * v2 [i]
- template<class E1, class E2>
- BOOST_UBLAS_INLINE
- typename vector_binary_traits<E1, E2, scalar_multiplies<typename E1::value_type,
- typename E2::value_type> >::result_type
- element_prod (const vector_expression<E1> &e1,
- const vector_expression<E2> &e2) {
- typedef typename vector_binary_traits<E1, E2, scalar_multiplies<typename E1::value_type,
- typename E2::value_type> >::expression_type expression_type;
- return expression_type (e1 (), e2 ());
- }
- // (v1 / v2) [i] = v1 [i] / v2 [i]
- template<class E1, class E2>
- BOOST_UBLAS_INLINE
- typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
- typename E2::value_type> >::result_type
- element_div (const vector_expression<E1> &e1,
- const vector_expression<E2> &e2) {
- typedef typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
- typename E2::value_type> >::expression_type expression_type;
- return expression_type (e1 (), e2 ());
- }
- template<class E1, class E2, class F>
- class vector_binary_scalar1:
- public vector_expression<vector_binary_scalar1<E1, E2, F> > {
- typedef F functor_type;
- typedef E1 expression1_type;
- typedef E2 expression2_type;
- public:
- typedef const E1& expression1_closure_type;
- typedef typename E2::const_closure_type expression2_closure_type;
- private:
- typedef vector_binary_scalar1<E1, E2, F> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_expression<vector_binary_scalar1<E1, E2, F> >::operator ();
- #endif
- typedef typename E2::size_type size_type;
- typedef typename E2::difference_type difference_type;
- typedef typename F::result_type value_type;
- typedef value_type const_reference;
- typedef const_reference reference;
- typedef const self_type const_closure_type;
- typedef const_closure_type closure_type;
- typedef unknown_storage_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- vector_binary_scalar1 (const expression1_type &e1, const expression2_type &e2):
- e1_ (e1), e2_ (e2) {}
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return e2_.size ();
- }
- public:
- // Element access
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- return functor_type::apply (e1_, e2_ (i));
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return functor_type::apply (e1_, e2_ [i]);
- }
- // Closure comparison
- BOOST_UBLAS_INLINE
- bool same_closure (const vector_binary_scalar1 &vbs1) const {
- return &e1_ == &(vbs1.e1_) &&
- (*this).e2_.same_closure (vbs1.e2_);
- }
- // Iterator types
- private:
- typedef expression1_type const_subiterator1_type;
- typedef typename expression2_type::const_iterator const_subiterator2_type;
- typedef const value_type *const_pointer;
- public:
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- typedef indexed_const_iterator<const_closure_type, typename const_subiterator2_type::iterator_category> const_iterator;
- typedef const_iterator iterator;
- #else
- class const_iterator;
- typedef const_iterator iterator;
- #endif
- // Element lookup
- BOOST_UBLAS_INLINE
- const_iterator find (size_type i) const {
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- const_subiterator2_type it (e2_.find (i));
- return const_iterator (*this, it.index ());
- #else
- return const_iterator (*this, const_subiterator1_type (e1_), e2_.find (i));
- #endif
- }
- // Iterator enhances the iterator of the referenced vector expression
- // with the binary functor.
- #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
- class const_iterator:
- public container_const_reference<vector_binary_scalar1>,
- public iterator_base_traits<typename E2::const_iterator::iterator_category>::template
- iterator_base<const_iterator, value_type>::type {
- public:
- typedef typename E2::const_iterator::iterator_category iterator_category;
- typedef typename vector_binary_scalar1::difference_type difference_type;
- typedef typename vector_binary_scalar1::value_type value_type;
- typedef typename vector_binary_scalar1::const_reference reference;
- typedef typename vector_binary_scalar1::const_pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- const_iterator ():
- container_const_reference<self_type> (), it1_ (), it2_ () {}
- BOOST_UBLAS_INLINE
- const_iterator (const self_type &vbs, const const_subiterator1_type &it1, const const_subiterator2_type &it2):
- container_const_reference<self_type> (vbs), it1_ (it1), it2_ (it2) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- const_iterator &operator ++ () {
- ++ it2_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -- () {
- -- it2_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator += (difference_type n) {
- it2_ += n;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -= (difference_type n) {
- it2_ -= n;
- return *this;
- }
- BOOST_UBLAS_INLINE
- difference_type operator - (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- // FIXME we shouldn't compare floats
- // BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
- return it2_ - it.it2_;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- const_reference operator * () const {
- return functor_type::apply (it1_, *it2_);
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (difference_type n) const {
- return *(*this + n);
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- return it2_.index ();
- }
- // Assignment
- BOOST_UBLAS_INLINE
- const_iterator &operator = (const const_iterator &it) {
- container_const_reference<self_type>::assign (&it ());
- it1_ = it.it1_;
- it2_ = it.it2_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- // FIXME we shouldn't compare floats
- // BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
- return it2_ == it.it2_;
- }
- BOOST_UBLAS_INLINE
- bool operator < (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- // FIXME we shouldn't compare floats
- // BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
- return it2_ < it.it2_;
- }
- private:
- const_subiterator1_type it1_;
- const_subiterator2_type it2_;
- };
- #endif
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return find (size ());
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end ();
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return end ();
- }
- private:
- expression1_closure_type e1_;
- expression2_closure_type e2_;
- };
- template<class E1, class E2, class F>
- struct vector_binary_scalar1_traits {
- typedef vector_binary_scalar1<E1, E2, F> expression_type; // allow E1 to be builtin type
- #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
- typedef expression_type result_type;
- #else
- typedef typename E2::vector_temporary_type result_type;
- #endif
- };
- // (t * v) [i] = t * v [i]
- template<class T1, class E2>
- BOOST_UBLAS_INLINE
- typename boost::enable_if< is_convertible<T1, typename E2::value_type >,
- typename vector_binary_scalar1_traits<const T1, E2, scalar_multiplies<T1, typename E2::value_type> >::result_type
- >::type
- operator * (const T1 &e1,
- const vector_expression<E2> &e2) {
- typedef typename vector_binary_scalar1_traits<const T1, E2, scalar_multiplies<T1, typename E2::value_type> >::expression_type expression_type;
- return expression_type (e1, e2 ());
- }
- template<class E1, class E2, class F>
- class vector_binary_scalar2:
- public vector_expression<vector_binary_scalar2<E1, E2, F> > {
- typedef F functor_type;
- typedef E1 expression1_type;
- typedef E2 expression2_type;
- typedef typename E1::const_closure_type expression1_closure_type;
- typedef const E2& expression2_closure_type;
- typedef vector_binary_scalar2<E1, E2, F> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_expression<vector_binary_scalar2<E1, E2, F> >::operator ();
- #endif
- typedef typename E1::size_type size_type;
- typedef typename E1::difference_type difference_type;
- typedef typename F::result_type value_type;
- typedef value_type const_reference;
- typedef const_reference reference;
- typedef const self_type const_closure_type;
- typedef const_closure_type closure_type;
- typedef unknown_storage_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- vector_binary_scalar2 (const expression1_type &e1, const expression2_type &e2):
- e1_ (e1), e2_ (e2) {}
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return e1_.size ();
- }
- public:
- // Element access
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- return functor_type::apply (e1_ (i), e2_);
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return functor_type::apply (e1_ [i], e2_);
- }
- // Closure comparison
- BOOST_UBLAS_INLINE
- bool same_closure (const vector_binary_scalar2 &vbs2) const {
- return (*this).e1_.same_closure (vbs2.e1_) &&
- &e2_ == &(vbs2.e2_);
- }
- // Iterator types
- private:
- typedef typename expression1_type::const_iterator const_subiterator1_type;
- typedef expression2_type const_subiterator2_type;
- typedef const value_type *const_pointer;
- public:
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- typedef indexed_const_iterator<const_closure_type, typename const_subiterator2_type::iterator_category> const_iterator;
- typedef const_iterator iterator;
- #else
- class const_iterator;
- typedef const_iterator iterator;
- #endif
- // Element lookup
- BOOST_UBLAS_INLINE
- const_iterator find (size_type i) const {
- #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
- const_subiterator1_type it (e1_.find (i));
- return const_iterator (*this, it.index ());
- #else
- return const_iterator (*this, e1_.find (i), const_subiterator2_type (e2_));
- #endif
- }
- // Iterator enhances the iterator of the referenced vector expression
- // with the binary functor.
- #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
- class const_iterator:
- public container_const_reference<vector_binary_scalar2>,
- public iterator_base_traits<typename E1::const_iterator::iterator_category>::template
- iterator_base<const_iterator, value_type>::type {
- public:
- typedef typename E1::const_iterator::iterator_category iterator_category;
- typedef typename vector_binary_scalar2::difference_type difference_type;
- typedef typename vector_binary_scalar2::value_type value_type;
- typedef typename vector_binary_scalar2::const_reference reference;
- typedef typename vector_binary_scalar2::const_pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- const_iterator ():
- container_const_reference<self_type> (), it1_ (), it2_ () {}
- BOOST_UBLAS_INLINE
- const_iterator (const self_type &vbs, const const_subiterator1_type &it1, const const_subiterator2_type &it2):
- container_const_reference<self_type> (vbs), it1_ (it1), it2_ (it2) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- const_iterator &operator ++ () {
- ++ it1_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -- () {
- -- it1_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator += (difference_type n) {
- it1_ += n;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -= (difference_type n) {
- it1_ -= n;
- return *this;
- }
- BOOST_UBLAS_INLINE
- difference_type operator - (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- // FIXME we shouldn't compare floats
- // BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
- return it1_ - it.it1_;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- const_reference operator * () const {
- return functor_type::apply (*it1_, it2_);
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (difference_type n) const {
- return *(*this + n);
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- return it1_.index ();
- }
- // Assignment
- BOOST_UBLAS_INLINE
- const_iterator &operator = (const const_iterator &it) {
- container_const_reference<self_type>::assign (&it ());
- it1_ = it.it1_;
- it2_ = it.it2_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- // FIXME we shouldn't compare floats
- // BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
- return it1_ == it.it1_;
- }
- BOOST_UBLAS_INLINE
- bool operator < (const const_iterator &it) const {
- BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
- // FIXME we shouldn't compare floats
- // BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
- return it1_ < it.it1_;
- }
- private:
- const_subiterator1_type it1_;
- const_subiterator2_type it2_;
- };
- #endif
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return find (size ());
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end ();
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return rend ();
- }
- private:
- expression1_closure_type e1_;
- expression2_closure_type e2_;
- };
- template<class E1, class E2, class F>
- struct vector_binary_scalar2_traits {
- typedef vector_binary_scalar2<E1, E2, F> expression_type; // allow E2 to be builtin type
- #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
- typedef expression_type result_type;
- #else
- typedef typename E1::vector_temporary_type result_type;
- #endif
- };
- // (v * t) [i] = v [i] * t
- template<class E1, class T2>
- BOOST_UBLAS_INLINE
- typename boost::enable_if< is_convertible<T2, typename E1::value_type >,
- typename vector_binary_scalar2_traits<E1, const T2, scalar_multiplies<typename E1::value_type, T2> >::result_type
- >::type
- operator * (const vector_expression<E1> &e1,
- const T2 &e2) {
- typedef typename vector_binary_scalar2_traits<E1, const T2, scalar_multiplies<typename E1::value_type, T2> >::expression_type expression_type;
- return expression_type (e1 (), e2);
- }
- // (v / t) [i] = v [i] / t
- template<class E1, class T2>
- BOOST_UBLAS_INLINE
- typename boost::enable_if< is_convertible<T2, typename E1::value_type >,
- typename vector_binary_scalar2_traits<E1, const T2, scalar_divides<typename E1::value_type, T2> >::result_type
- >::type
- operator / (const vector_expression<E1> &e1,
- const T2 &e2) {
- typedef typename vector_binary_scalar2_traits<E1, const T2, scalar_divides<typename E1::value_type, T2> >::expression_type expression_type;
- return expression_type (e1 (), e2);
- }
- template<class E, class F>
- class vector_scalar_unary:
- public scalar_expression<vector_scalar_unary<E, F> > {
- typedef E expression_type;
- typedef F functor_type;
- typedef typename E::const_closure_type expression_closure_type;
- typedef typename E::const_iterator::iterator_category iterator_category;
- typedef vector_scalar_unary<E, F> self_type;
- public:
- typedef typename F::result_type value_type;
- typedef typename E::difference_type difference_type;
- typedef const self_type const_closure_type;
- typedef const_closure_type closure_type;
- typedef unknown_storage_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- explicit vector_scalar_unary (const expression_type &e):
- e_ (e) {}
- private:
- // Expression accessors
- BOOST_UBLAS_INLINE
- const expression_closure_type &expression () const {
- return e_;
- }
- public:
- BOOST_UBLAS_INLINE
- operator value_type () const {
- return evaluate (iterator_category ());
- }
- private:
- // Dense random access specialization
- BOOST_UBLAS_INLINE
- value_type evaluate (dense_random_access_iterator_tag) const {
- #ifdef BOOST_UBLAS_USE_INDEXING
- return functor_type::apply (e_);
- #elif BOOST_UBLAS_USE_ITERATING
- difference_type size = e_.size ();
- return functor_type::apply (size, e_.begin ());
- #else
- difference_type size = e_.size ();
- if (size >= BOOST_UBLAS_ITERATOR_THRESHOLD)
- return functor_type::apply (size, e_.begin ());
- else
- return functor_type::apply (e_);
- #endif
- }
- // Packed bidirectional specialization
- BOOST_UBLAS_INLINE
- value_type evaluate (packed_random_access_iterator_tag) const {
- return functor_type::apply (e_.begin (), e_.end ());
- }
- // Sparse bidirectional specialization
- BOOST_UBLAS_INLINE
- value_type evaluate (sparse_bidirectional_iterator_tag) const {
- return functor_type::apply (e_.begin (), e_.end ());
- }
- private:
- expression_closure_type e_;
- };
- template<class E, class F>
- struct vector_scalar_unary_traits {
- typedef vector_scalar_unary<E, F> expression_type;
- #if !defined (BOOST_UBLAS_SIMPLE_ET_DEBUG) && defined (BOOST_UBLAS_USE_SCALAR_ET)
- // FIXME don't define USE_SCALAR_ET other then for testing
- // They do not work for complex types
- typedef expression_type result_type;
- #else
- typedef typename F::result_type result_type;
- #endif
- };
- // sum v = sum (v [i])
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_scalar_unary_traits<E, vector_sum<E> >::result_type
- sum (const vector_expression<E> &e) {
- typedef typename vector_scalar_unary_traits<E, vector_sum<E> >::expression_type expression_type;
- return expression_type (e ());
- }
- // real: norm_1 v = sum (abs (v [i]))
- // complex: norm_1 v = sum (abs (real (v [i])) + abs (imag (v [i])))
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_scalar_unary_traits<E, vector_norm_1<E> >::result_type
- norm_1 (const vector_expression<E> &e) {
- typedef typename vector_scalar_unary_traits<E, vector_norm_1<E> >::expression_type expression_type;
- return expression_type (e ());
- }
- // real: norm_2 v = sqrt (sum (v [i] * v [i]))
- // complex: norm_2 v = sqrt (sum (v [i] * conj (v [i])))
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_scalar_unary_traits<E, vector_norm_2<E> >::result_type
- norm_2 (const vector_expression<E> &e) {
- typedef typename vector_scalar_unary_traits<E, vector_norm_2<E> >::expression_type expression_type;
- return expression_type (e ());
- }
- // real: norm_2_square v = sum(v [i] * v [i])
- // complex: norm_2_square v = sum(v [i] * conj (v [i]))
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_scalar_unary_traits<E, vector_norm_2_square<E> >::result_type
- norm_2_square (const vector_expression<E> &e) {
- typedef typename vector_scalar_unary_traits<E, vector_norm_2_square<E> >::expression_type expression_type;
- return expression_type (e ());
- }
- // real: norm_inf v = maximum (abs (v [i]))
- // complex: norm_inf v = maximum (maximum (abs (real (v [i])), abs (imag (v [i]))))
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_scalar_unary_traits<E, vector_norm_inf<E> >::result_type
- norm_inf (const vector_expression<E> &e) {
- typedef typename vector_scalar_unary_traits<E, vector_norm_inf<E> >::expression_type expression_type;
- return expression_type (e ());
- }
- // real: index_norm_inf v = minimum (i: abs (v [i]) == maximum (abs (v [i])))
- template<class E>
- BOOST_UBLAS_INLINE
- typename vector_scalar_unary_traits<E, vector_index_norm_inf<E> >::result_type
- index_norm_inf (const vector_expression<E> &e) {
- typedef typename vector_scalar_unary_traits<E, vector_index_norm_inf<E> >::expression_type expression_type;
- return expression_type (e ());
- }
- template<class E1, class E2, class F>
- class vector_scalar_binary:
- public scalar_expression<vector_scalar_binary<E1, E2, F> > {
- typedef E1 expression1_type;
- typedef E2 expression2_type;
- typedef F functor_type;
- typedef typename E1::const_closure_type expression1_closure_type;
- typedef typename E2::const_closure_type expression2_closure_type;
- typedef typename iterator_restrict_traits<typename E1::const_iterator::iterator_category,
- typename E2::const_iterator::iterator_category>::iterator_category iterator_category;
- typedef vector_scalar_binary<E1, E2, F> self_type;
- public:
- static const unsigned complexity = 1;
- typedef typename F::result_type value_type;
- typedef typename E1::difference_type difference_type;
- typedef const self_type const_closure_type;
- typedef const_closure_type closure_type;
- typedef unknown_storage_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- vector_scalar_binary (const expression1_type &e1, const expression2_type &e2):
- e1_ (e1), e2_ (e2) {}
- private:
- // Accessors
- BOOST_UBLAS_INLINE
- const expression1_closure_type &expression1 () const {
- return e1_;
- }
- BOOST_UBLAS_INLINE
- const expression2_closure_type &expression2 () const {
- return e2_;
- }
- public:
- BOOST_UBLAS_INLINE
- operator value_type () const {
- return evaluate (iterator_category ());
- }
- private:
- // Dense random access specialization
- BOOST_UBLAS_INLINE
- value_type evaluate (dense_random_access_iterator_tag) const {
- BOOST_UBLAS_CHECK (e1_.size () == e2_.size (), external_logic());
- #ifdef BOOST_UBLAS_USE_INDEXING
- return functor_type::apply (e1_, e2_);
- #elif BOOST_UBLAS_USE_ITERATING
- difference_type size = BOOST_UBLAS_SAME (e1_.size (), e2_.size ());
- return functor_type::apply (size, e1_.begin (), e2_.begin ());
- #else
- difference_type size = BOOST_UBLAS_SAME (e1_.size (), e2_.size ());
- if (size >= BOOST_UBLAS_ITERATOR_THRESHOLD)
- return functor_type::apply (size, e1_.begin (), e2_.begin ());
- else
- return functor_type::apply (e1_, e2_);
- #endif
- }
- // Packed bidirectional specialization
- BOOST_UBLAS_INLINE
- value_type evaluate (packed_random_access_iterator_tag) const {
- BOOST_UBLAS_CHECK (e1_.size () == e2_.size (), external_logic());
- return functor_type::apply (e1_.begin (), e1_.end (), e2_.begin (), e2_.end ());
- }
- // Sparse bidirectional specialization
- BOOST_UBLAS_INLINE
- value_type evaluate (sparse_bidirectional_iterator_tag) const {
- BOOST_UBLAS_CHECK (e1_.size () == e2_.size (), external_logic());
- return functor_type::apply (e1_.begin (), e1_.end (), e2_.begin (), e2_.end (), sparse_bidirectional_iterator_tag ());
- }
- private:
- expression1_closure_type e1_;
- expression2_closure_type e2_;
- };
- template<class E1, class E2, class F>
- struct vector_scalar_binary_traits {
- typedef vector_scalar_binary<E1, E2, F> expression_type;
- #if !defined (BOOST_UBLAS_SIMPLE_ET_DEBUG) && defined (BOOST_UBLAS_USE_SCALAR_ET)
- // FIXME don't define USE_SCALAR_ET other then for testing
- // They do not work for complex types
- typedef expression_type result_type;
- #else
- typedef typename F::result_type result_type;
- #endif
- };
- // inner_prod (v1, v2) = sum (v1 [i] * v2 [i])
- template<class E1, class E2>
- BOOST_UBLAS_INLINE
- typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
- typename promote_traits<typename E1::value_type,
- typename E2::value_type>::promote_type> >::result_type
- inner_prod (const vector_expression<E1> &e1,
- const vector_expression<E2> &e2) {
- typedef typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
- typename promote_traits<typename E1::value_type,
- typename E2::value_type>::promote_type> >::expression_type expression_type;
- return expression_type (e1 (), e2 ());
- }
- template<class E1, class E2>
- BOOST_UBLAS_INLINE
- typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
- typename type_traits<typename promote_traits<typename E1::value_type,
- typename E2::value_type>::promote_type>::precision_type> >::result_type
- prec_inner_prod (const vector_expression<E1> &e1,
- const vector_expression<E2> &e2) {
- typedef typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
- typename type_traits<typename promote_traits<typename E1::value_type,
- typename E2::value_type>::promote_type>::precision_type> >::expression_type expression_type;
- return expression_type (e1 (), e2 ());
- }
- }}}
- #endif
|