vector_expression.hpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. //
  2. // Copyright (c) 2000-2002
  3. // Joerg Walter, Mathias Koch
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // The authors gratefully acknowledge the support of
  10. // GeNeSys mbH & Co. KG in producing this work.
  11. //
  12. #ifndef _BOOST_UBLAS_VECTOR_EXPRESSION_
  13. #define _BOOST_UBLAS_VECTOR_EXPRESSION_
  14. #include <boost/numeric/ublas/expression_types.hpp>
  15. // Expression templates based on ideas of Todd Veldhuizen and Geoffrey Furnish
  16. // Iterators based on ideas of Jeremy Siek
  17. //
  18. // Classes that model the Vector Expression concept
  19. namespace boost { namespace numeric { namespace ublas {
  20. template<class E>
  21. class vector_reference:
  22. public vector_expression<vector_reference<E> > {
  23. typedef vector_reference<E> self_type;
  24. public:
  25. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  26. using vector_expression<vector_reference<E> >::operator ();
  27. #endif
  28. typedef typename E::size_type size_type;
  29. typedef typename E::difference_type difference_type;
  30. typedef typename E::value_type value_type;
  31. typedef typename E::const_reference const_reference;
  32. typedef typename boost::mpl::if_<boost::is_const<E>,
  33. typename E::const_reference,
  34. typename E::reference>::type reference;
  35. typedef E referred_type;
  36. typedef const self_type const_closure_type;
  37. typedef self_type closure_type;
  38. typedef typename E::storage_category storage_category;
  39. // Construction and destruction
  40. BOOST_UBLAS_INLINE
  41. explicit vector_reference (referred_type &e):
  42. e_ (e) {}
  43. // Accessors
  44. BOOST_UBLAS_INLINE
  45. size_type size () const {
  46. return expression ().size ();
  47. }
  48. public:
  49. // Expression accessors - const correct
  50. BOOST_UBLAS_INLINE
  51. const referred_type &expression () const {
  52. return e_;
  53. }
  54. BOOST_UBLAS_INLINE
  55. referred_type &expression () {
  56. return e_;
  57. }
  58. public:
  59. // Element access
  60. #ifndef BOOST_UBLAS_REFERENCE_CONST_MEMBER
  61. BOOST_UBLAS_INLINE
  62. const_reference operator () (size_type i) const {
  63. return expression () (i);
  64. }
  65. BOOST_UBLAS_INLINE
  66. reference operator () (size_type i) {
  67. return expression () (i);
  68. }
  69. BOOST_UBLAS_INLINE
  70. const_reference operator [] (size_type i) const {
  71. return expression () [i];
  72. }
  73. BOOST_UBLAS_INLINE
  74. reference operator [] (size_type i) {
  75. return expression () [i];
  76. }
  77. #else
  78. BOOST_UBLAS_INLINE
  79. reference operator () (size_type i) const {
  80. return expression () (i);
  81. }
  82. BOOST_UBLAS_INLINE
  83. reference operator [] (size_type i) const {
  84. return expression () [i];
  85. }
  86. #endif
  87. // Assignment
  88. BOOST_UBLAS_INLINE
  89. vector_reference &operator = (const vector_reference &v) {
  90. expression ().operator = (v);
  91. return *this;
  92. }
  93. template<class AE>
  94. BOOST_UBLAS_INLINE
  95. vector_reference &operator = (const vector_expression<AE> &ae) {
  96. expression ().operator = (ae);
  97. return *this;
  98. }
  99. template<class AE>
  100. BOOST_UBLAS_INLINE
  101. vector_reference &assign (const vector_expression<AE> &ae) {
  102. expression ().assign (ae);
  103. return *this;
  104. }
  105. template<class AE>
  106. BOOST_UBLAS_INLINE
  107. vector_reference &operator += (const vector_expression<AE> &ae) {
  108. expression ().operator += (ae);
  109. return *this;
  110. }
  111. template<class AE>
  112. BOOST_UBLAS_INLINE
  113. vector_reference &plus_assign (const vector_expression<AE> &ae) {
  114. expression ().plus_assign (ae);
  115. return *this;
  116. }
  117. template<class AE>
  118. BOOST_UBLAS_INLINE
  119. vector_reference &operator -= (const vector_expression<AE> &ae) {
  120. expression ().operator -= (ae);
  121. return *this;
  122. }
  123. template<class AE>
  124. BOOST_UBLAS_INLINE
  125. vector_reference &minus_assign (const vector_expression<AE> &ae) {
  126. expression ().minus_assign (ae);
  127. return *this;
  128. }
  129. template<class AT>
  130. BOOST_UBLAS_INLINE
  131. vector_reference &operator *= (const AT &at) {
  132. expression ().operator *= (at);
  133. return *this;
  134. }
  135. template<class AT>
  136. BOOST_UBLAS_INLINE
  137. vector_reference &operator /= (const AT &at) {
  138. expression ().operator /= (at);
  139. return *this;
  140. }
  141. // Swapping
  142. BOOST_UBLAS_INLINE
  143. void swap (vector_reference &v) {
  144. expression ().swap (v.expression ());
  145. }
  146. // Closure comparison
  147. BOOST_UBLAS_INLINE
  148. bool same_closure (const vector_reference &vr) const {
  149. return &(*this).e_ == &vr.e_;
  150. }
  151. // Iterator types
  152. typedef typename E::const_iterator const_iterator;
  153. typedef typename boost::mpl::if_<boost::is_const<E>,
  154. typename E::const_iterator,
  155. typename E::iterator>::type iterator;
  156. // Element lookup
  157. BOOST_UBLAS_INLINE
  158. const_iterator find (size_type i) const {
  159. return expression ().find (i);
  160. }
  161. BOOST_UBLAS_INLINE
  162. iterator find (size_type i) {
  163. return expression ().find (i);
  164. }
  165. // Iterator is the iterator of the referenced expression.
  166. BOOST_UBLAS_INLINE
  167. const_iterator begin () const {
  168. return expression ().begin ();
  169. }
  170. BOOST_UBLAS_INLINE
  171. const_iterator cbegin () const {
  172. return begin ();
  173. }
  174. BOOST_UBLAS_INLINE
  175. const_iterator end () const {
  176. return expression ().end ();
  177. }
  178. BOOST_UBLAS_INLINE
  179. const_iterator cend () const {
  180. return end ();
  181. }
  182. BOOST_UBLAS_INLINE
  183. iterator begin () {
  184. return expression ().begin ();
  185. }
  186. BOOST_UBLAS_INLINE
  187. iterator end () {
  188. return expression ().end ();
  189. }
  190. // Reverse iterator
  191. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  192. typedef reverse_iterator_base<iterator> reverse_iterator;
  193. BOOST_UBLAS_INLINE
  194. const_reverse_iterator rbegin () const {
  195. return const_reverse_iterator (end ());
  196. }
  197. BOOST_UBLAS_INLINE
  198. const_reverse_iterator crbegin () const {
  199. return rbegin ();
  200. }
  201. BOOST_UBLAS_INLINE
  202. const_reverse_iterator rend () const {
  203. return const_reverse_iterator (begin ());
  204. }
  205. BOOST_UBLAS_INLINE
  206. const_reverse_iterator crend () const {
  207. return rend ();
  208. }
  209. BOOST_UBLAS_INLINE
  210. reverse_iterator rbegin () {
  211. return reverse_iterator (end ());
  212. }
  213. BOOST_UBLAS_INLINE
  214. reverse_iterator rend () {
  215. return reverse_iterator (begin ());
  216. }
  217. private:
  218. referred_type &e_;
  219. };
  220. template<class E, class F>
  221. class vector_unary:
  222. public vector_expression<vector_unary<E, F> > {
  223. typedef F functor_type;
  224. typedef typename boost::mpl::if_<boost::is_same<F, scalar_identity<typename E::value_type> >,
  225. E,
  226. const E>::type expression_type;
  227. typedef typename boost::mpl::if_<boost::is_const<expression_type>,
  228. typename E::const_closure_type,
  229. typename E::closure_type>::type expression_closure_type;
  230. typedef vector_unary<E, F> self_type;
  231. public:
  232. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  233. using vector_expression<vector_unary<E, F> >::operator ();
  234. #endif
  235. typedef typename E::size_type size_type;
  236. typedef typename E::difference_type difference_type;
  237. typedef typename F::result_type value_type;
  238. typedef value_type const_reference;
  239. typedef typename boost::mpl::if_<boost::is_same<F, scalar_identity<value_type> >,
  240. typename E::reference,
  241. value_type>::type reference;
  242. typedef const self_type const_closure_type;
  243. typedef self_type closure_type;
  244. typedef unknown_storage_tag storage_category;
  245. // Construction and destruction
  246. BOOST_UBLAS_INLINE
  247. // May be used as mutable expression.
  248. explicit vector_unary (expression_type &e):
  249. e_ (e) {}
  250. // Accessors
  251. BOOST_UBLAS_INLINE
  252. size_type size () const {
  253. return e_.size ();
  254. }
  255. public:
  256. // Expression accessors
  257. BOOST_UBLAS_INLINE
  258. const expression_closure_type &expression () const {
  259. return e_;
  260. }
  261. public:
  262. // Element access
  263. BOOST_UBLAS_INLINE
  264. const_reference operator () (size_type i) const {
  265. return functor_type::apply (e_ (i));
  266. }
  267. BOOST_UBLAS_INLINE
  268. reference operator () (size_type i) {
  269. BOOST_STATIC_ASSERT ((boost::is_same<functor_type, scalar_identity<value_type > >::value));
  270. return e_ (i);
  271. }
  272. BOOST_UBLAS_INLINE
  273. const_reference operator [] (size_type i) const {
  274. return functor_type::apply (e_ [i]);
  275. }
  276. BOOST_UBLAS_INLINE
  277. reference operator [] (size_type i) {
  278. BOOST_STATIC_ASSERT ((boost::is_same<functor_type, scalar_identity<value_type > >::value));
  279. return e_ [i];
  280. }
  281. // Closure comparison
  282. BOOST_UBLAS_INLINE
  283. bool same_closure (const vector_unary &vu) const {
  284. return (*this).expression ().same_closure (vu.expression ());
  285. }
  286. // Iterator types
  287. private:
  288. typedef typename E::const_iterator const_subiterator_type;
  289. typedef const value_type *const_pointer;
  290. public:
  291. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  292. typedef indexed_const_iterator<const_closure_type, typename const_subiterator_type::iterator_category> const_iterator;
  293. typedef const_iterator iterator;
  294. #else
  295. class const_iterator;
  296. typedef const_iterator iterator;
  297. #endif
  298. // Element lookup
  299. BOOST_UBLAS_INLINE
  300. const_iterator find (size_type i) const {
  301. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  302. const_subiterator_type it (e_.find (i));
  303. return const_iterator (*this, it.index ());
  304. #else
  305. return const_iterator (*this, e_.find (i));
  306. #endif
  307. }
  308. // Iterator enhances the iterator of the referenced expression
  309. // with the unary functor.
  310. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  311. class const_iterator:
  312. public container_const_reference<vector_unary>,
  313. public iterator_base_traits<typename E::const_iterator::iterator_category>::template
  314. iterator_base<const_iterator, value_type>::type {
  315. public:
  316. typedef typename E::const_iterator::iterator_category iterator_category;
  317. typedef typename vector_unary::difference_type difference_type;
  318. typedef typename vector_unary::value_type value_type;
  319. typedef typename vector_unary::const_reference reference;
  320. typedef typename vector_unary::const_pointer pointer;
  321. // Construction and destruction
  322. BOOST_UBLAS_INLINE
  323. const_iterator ():
  324. container_const_reference<self_type> (), it_ () {}
  325. BOOST_UBLAS_INLINE
  326. const_iterator (const self_type &vu, const const_subiterator_type &it):
  327. container_const_reference<self_type> (vu), it_ (it) {}
  328. // Arithmetic
  329. BOOST_UBLAS_INLINE
  330. const_iterator &operator ++ () {
  331. ++ it_;
  332. return *this;
  333. }
  334. BOOST_UBLAS_INLINE
  335. const_iterator &operator -- () {
  336. -- it_;
  337. return *this;
  338. }
  339. BOOST_UBLAS_INLINE
  340. const_iterator &operator += (difference_type n) {
  341. it_ += n;
  342. return *this;
  343. }
  344. BOOST_UBLAS_INLINE
  345. const_iterator &operator -= (difference_type n) {
  346. it_ -= n;
  347. return *this;
  348. }
  349. BOOST_UBLAS_INLINE
  350. difference_type operator - (const const_iterator &it) const {
  351. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  352. return it_ - it.it_;
  353. }
  354. // Dereference
  355. BOOST_UBLAS_INLINE
  356. const_reference operator * () const {
  357. return functor_type::apply (*it_);
  358. }
  359. BOOST_UBLAS_INLINE
  360. const_reference operator [] (difference_type n) const {
  361. return *(*this + n);
  362. }
  363. // Index
  364. BOOST_UBLAS_INLINE
  365. size_type index () const {
  366. return it_.index ();
  367. }
  368. // Assignment
  369. BOOST_UBLAS_INLINE
  370. const_iterator &operator = (const const_iterator &it) {
  371. container_const_reference<self_type>::assign (&it ());
  372. it_ = it.it_;
  373. return *this;
  374. }
  375. // Comparison
  376. BOOST_UBLAS_INLINE
  377. bool operator == (const const_iterator &it) const {
  378. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  379. return it_ == it.it_;
  380. }
  381. BOOST_UBLAS_INLINE
  382. bool operator < (const const_iterator &it) const {
  383. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  384. return it_ < it.it_;
  385. }
  386. private:
  387. const_subiterator_type it_;
  388. };
  389. #endif
  390. BOOST_UBLAS_INLINE
  391. const_iterator begin () const {
  392. return find (0);
  393. }
  394. BOOST_UBLAS_INLINE
  395. const_iterator cbegin () const {
  396. return begin ();
  397. }
  398. BOOST_UBLAS_INLINE
  399. const_iterator end () const {
  400. return find (size ());
  401. }
  402. BOOST_UBLAS_INLINE
  403. const_iterator cend () const {
  404. return end ();
  405. }
  406. // Reverse iterator
  407. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  408. BOOST_UBLAS_INLINE
  409. const_reverse_iterator rbegin () const {
  410. return const_reverse_iterator (end ());
  411. }
  412. BOOST_UBLAS_INLINE
  413. const_reverse_iterator crbegin () const {
  414. return rbegin ();
  415. }
  416. BOOST_UBLAS_INLINE
  417. const_reverse_iterator rend () const {
  418. return const_reverse_iterator (begin ());
  419. }
  420. BOOST_UBLAS_INLINE
  421. const_reverse_iterator crend () const {
  422. return rend ();
  423. }
  424. private:
  425. expression_closure_type e_;
  426. };
  427. template<class E, class F>
  428. struct vector_unary_traits {
  429. typedef vector_unary<E, F> expression_type;
  430. //FIXME
  431. // #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
  432. typedef expression_type result_type;
  433. // #else
  434. // typedef typename E::vector_temporary_type result_type;
  435. // #endif
  436. };
  437. // (- v) [i] = - v [i]
  438. template<class E>
  439. BOOST_UBLAS_INLINE
  440. typename vector_unary_traits<E, scalar_negate<typename E::value_type> >::result_type
  441. operator - (const vector_expression<E> &e) {
  442. typedef typename vector_unary_traits<E, scalar_negate<typename E::value_type> >::expression_type expression_type;
  443. return expression_type (e ());
  444. }
  445. // (conj v) [i] = conj (v [i])
  446. template<class E>
  447. BOOST_UBLAS_INLINE
  448. typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::result_type
  449. conj (const vector_expression<E> &e) {
  450. typedef typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::expression_type expression_type;
  451. return expression_type (e ());
  452. }
  453. // (real v) [i] = real (v [i])
  454. template<class E>
  455. BOOST_UBLAS_INLINE
  456. typename vector_unary_traits<E, scalar_real<typename E::value_type> >::result_type
  457. real (const vector_expression<E> &e) {
  458. typedef typename vector_unary_traits<E, scalar_real<typename E::value_type> >::expression_type expression_type;
  459. return expression_type (e ());
  460. }
  461. // (imag v) [i] = imag (v [i])
  462. template<class E>
  463. BOOST_UBLAS_INLINE
  464. typename vector_unary_traits<E, scalar_imag<typename E::value_type> >::result_type
  465. imag (const vector_expression<E> &e) {
  466. typedef typename vector_unary_traits<E, scalar_imag<typename E::value_type> >::expression_type expression_type;
  467. return expression_type (e ());
  468. }
  469. // (trans v) [i] = v [i]
  470. template<class E>
  471. BOOST_UBLAS_INLINE
  472. typename vector_unary_traits<const E, scalar_identity<typename E::value_type> >::result_type
  473. trans (const vector_expression<E> &e) {
  474. typedef typename vector_unary_traits<const E, scalar_identity<typename E::value_type> >::expression_type expression_type;
  475. return expression_type (e ());
  476. }
  477. template<class E>
  478. BOOST_UBLAS_INLINE
  479. typename vector_unary_traits<E, scalar_identity<typename E::value_type> >::result_type
  480. trans (vector_expression<E> &e) {
  481. typedef typename vector_unary_traits<E, scalar_identity<typename E::value_type> >::expression_type expression_type;
  482. return expression_type (e ());
  483. }
  484. // (herm v) [i] = conj (v [i])
  485. template<class E>
  486. BOOST_UBLAS_INLINE
  487. typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::result_type
  488. herm (const vector_expression<E> &e) {
  489. typedef typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::expression_type expression_type;
  490. return expression_type (e ());
  491. }
  492. template<class E1, class E2, class F>
  493. class vector_binary:
  494. public vector_expression<vector_binary<E1, E2, F> > {
  495. typedef E1 expression1_type;
  496. typedef E2 expression2_type;
  497. typedef F functor_type;
  498. typedef typename E1::const_closure_type expression1_closure_type;
  499. typedef typename E2::const_closure_type expression2_closure_type;
  500. typedef vector_binary<E1, E2, F> self_type;
  501. public:
  502. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  503. using vector_expression<vector_binary<E1, E2, F> >::operator ();
  504. #endif
  505. typedef typename promote_traits<typename E1::size_type, typename E2::size_type>::promote_type size_type;
  506. typedef typename promote_traits<typename E1::difference_type, typename E2::difference_type>::promote_type difference_type;
  507. typedef typename F::result_type value_type;
  508. typedef value_type const_reference;
  509. typedef const_reference reference;
  510. typedef const self_type const_closure_type;
  511. typedef const_closure_type closure_type;
  512. typedef unknown_storage_tag storage_category;
  513. // Construction and destruction
  514. BOOST_UBLAS_INLINE
  515. vector_binary (const expression1_type &e1, const expression2_type &e2):
  516. e1_ (e1), e2_ (e2) {}
  517. // Accessors
  518. BOOST_UBLAS_INLINE
  519. size_type size () const {
  520. return BOOST_UBLAS_SAME (e1_.size (), e2_.size ());
  521. }
  522. private:
  523. // Accessors
  524. BOOST_UBLAS_INLINE
  525. const expression1_closure_type &expression1 () const {
  526. return e1_;
  527. }
  528. BOOST_UBLAS_INLINE
  529. const expression2_closure_type &expression2 () const {
  530. return e2_;
  531. }
  532. public:
  533. // Element access
  534. BOOST_UBLAS_INLINE
  535. const_reference operator () (size_type i) const {
  536. return functor_type::apply (e1_ (i), e2_ (i));
  537. }
  538. BOOST_UBLAS_INLINE
  539. const_reference operator [] (size_type i) const {
  540. return functor_type::apply (e1_ [i], e2_ [i]);
  541. }
  542. // Closure comparison
  543. BOOST_UBLAS_INLINE
  544. bool same_closure (const vector_binary &vb) const {
  545. return (*this).expression1 ().same_closure (vb.expression1 ()) &&
  546. (*this).expression2 ().same_closure (vb.expression2 ());
  547. }
  548. // Iterator types
  549. private:
  550. typedef typename E1::const_iterator const_subiterator1_type;
  551. typedef typename E2::const_iterator const_subiterator2_type;
  552. typedef const value_type *const_pointer;
  553. public:
  554. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  555. typedef typename iterator_restrict_traits<typename const_subiterator1_type::iterator_category,
  556. typename const_subiterator2_type::iterator_category>::iterator_category iterator_category;
  557. typedef indexed_const_iterator<const_closure_type, iterator_category> const_iterator;
  558. typedef const_iterator iterator;
  559. #else
  560. class const_iterator;
  561. typedef const_iterator iterator;
  562. #endif
  563. // Element lookup
  564. BOOST_UBLAS_INLINE
  565. const_iterator find (size_type i) const {
  566. const_subiterator1_type it1 (e1_.find (i));
  567. const_subiterator1_type it1_end (e1_.find (size ()));
  568. const_subiterator2_type it2 (e2_.find (i));
  569. const_subiterator2_type it2_end (e2_.find (size ()));
  570. i = (std::min) (it1 != it1_end ? it1.index () : size (),
  571. it2 != it2_end ? it2.index () : size ());
  572. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  573. return const_iterator (*this, i);
  574. #else
  575. return const_iterator (*this, i, it1, it1_end, it2, it2_end);
  576. #endif
  577. }
  578. // Iterator merges the iterators of the referenced expressions and
  579. // enhances them with the binary functor.
  580. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  581. class const_iterator:
  582. public container_const_reference<vector_binary>,
  583. public iterator_base_traits<typename iterator_restrict_traits<typename E1::const_iterator::iterator_category,
  584. typename E2::const_iterator::iterator_category>::iterator_category>::template
  585. iterator_base<const_iterator, value_type>::type {
  586. public:
  587. typedef typename iterator_restrict_traits<typename E1::const_iterator::iterator_category,
  588. typename E2::const_iterator::iterator_category>::iterator_category iterator_category;
  589. typedef typename vector_binary::difference_type difference_type;
  590. typedef typename vector_binary::value_type value_type;
  591. typedef typename vector_binary::const_reference reference;
  592. typedef typename vector_binary::const_pointer pointer;
  593. // Construction and destruction
  594. BOOST_UBLAS_INLINE
  595. const_iterator ():
  596. container_const_reference<self_type> (), i_ (), it1_ (), it1_end_ (), it2_ (), it2_end_ () {}
  597. BOOST_UBLAS_INLINE
  598. const_iterator (const self_type &vb, size_type i,
  599. const const_subiterator1_type &it1, const const_subiterator1_type &it1_end,
  600. const const_subiterator2_type &it2, const const_subiterator2_type &it2_end):
  601. container_const_reference<self_type> (vb), i_ (i), it1_ (it1), it1_end_ (it1_end), it2_ (it2), it2_end_ (it2_end) {}
  602. private:
  603. // Dense specializations
  604. BOOST_UBLAS_INLINE
  605. void increment (dense_random_access_iterator_tag) {
  606. ++ i_; ++ it1_; ++ it2_;
  607. }
  608. BOOST_UBLAS_INLINE
  609. void decrement (dense_random_access_iterator_tag) {
  610. -- i_; -- it1_; -- it2_;
  611. }
  612. BOOST_UBLAS_INLINE
  613. void increment (dense_random_access_iterator_tag, difference_type n) {
  614. i_ += n; it1_ += n; it2_ += n;
  615. }
  616. BOOST_UBLAS_INLINE
  617. void decrement (dense_random_access_iterator_tag, difference_type n) {
  618. i_ -= n; it1_ -= n; it2_ -= n;
  619. }
  620. BOOST_UBLAS_INLINE
  621. value_type dereference (dense_random_access_iterator_tag) const {
  622. return functor_type::apply (*it1_, *it2_);
  623. }
  624. // Packed specializations
  625. BOOST_UBLAS_INLINE
  626. void increment (packed_random_access_iterator_tag) {
  627. if (it1_ != it1_end_)
  628. if (it1_.index () <= i_)
  629. ++ it1_;
  630. if (it2_ != it2_end_)
  631. if (it2_.index () <= i_)
  632. ++ it2_;
  633. ++ i_;
  634. }
  635. BOOST_UBLAS_INLINE
  636. void decrement (packed_random_access_iterator_tag) {
  637. if (it1_ != it1_end_)
  638. if (i_ <= it1_.index ())
  639. -- it1_;
  640. if (it2_ != it2_end_)
  641. if (i_ <= it2_.index ())
  642. -- it2_;
  643. -- i_;
  644. }
  645. BOOST_UBLAS_INLINE
  646. void increment (packed_random_access_iterator_tag, difference_type n) {
  647. while (n > 0) {
  648. increment (packed_random_access_iterator_tag ());
  649. --n;
  650. }
  651. while (n < 0) {
  652. decrement (packed_random_access_iterator_tag ());
  653. ++n;
  654. }
  655. }
  656. BOOST_UBLAS_INLINE
  657. void decrement (packed_random_access_iterator_tag, difference_type n) {
  658. while (n > 0) {
  659. decrement (packed_random_access_iterator_tag ());
  660. --n;
  661. }
  662. while (n < 0) {
  663. increment (packed_random_access_iterator_tag ());
  664. ++n;
  665. }
  666. }
  667. BOOST_UBLAS_INLINE
  668. value_type dereference (packed_random_access_iterator_tag) const {
  669. typename E1::value_type t1 = typename E1::value_type/*zero*/();
  670. if (it1_ != it1_end_)
  671. if (it1_.index () == i_)
  672. t1 = *it1_;
  673. typename E2::value_type t2 = typename E2::value_type/*zero*/();
  674. if (it2_ != it2_end_)
  675. if (it2_.index () == i_)
  676. t2 = *it2_;
  677. return functor_type::apply (t1, t2);
  678. }
  679. // Sparse specializations
  680. BOOST_UBLAS_INLINE
  681. void increment (sparse_bidirectional_iterator_tag) {
  682. size_type index1 = (*this) ().size ();
  683. if (it1_ != it1_end_) {
  684. if (it1_.index () <= i_)
  685. ++ it1_;
  686. if (it1_ != it1_end_)
  687. index1 = it1_.index ();
  688. }
  689. size_type index2 = (*this) ().size ();
  690. if (it2_ != it2_end_) {
  691. if (it2_.index () <= i_)
  692. ++ it2_;
  693. if (it2_ != it2_end_)
  694. index2 = it2_.index ();
  695. }
  696. i_ = (std::min) (index1, index2);
  697. }
  698. BOOST_UBLAS_INLINE
  699. void decrement (sparse_bidirectional_iterator_tag) {
  700. size_type index1 = (*this) ().size ();
  701. if (it1_ != it1_end_) {
  702. if (i_ <= it1_.index ())
  703. -- it1_;
  704. if (it1_ != it1_end_)
  705. index1 = it1_.index ();
  706. }
  707. size_type index2 = (*this) ().size ();
  708. if (it2_ != it2_end_) {
  709. if (i_ <= it2_.index ())
  710. -- it2_;
  711. if (it2_ != it2_end_)
  712. index2 = it2_.index ();
  713. }
  714. i_ = (std::max) (index1, index2);
  715. }
  716. BOOST_UBLAS_INLINE
  717. void increment (sparse_bidirectional_iterator_tag, difference_type n) {
  718. while (n > 0) {
  719. increment (sparse_bidirectional_iterator_tag ());
  720. --n;
  721. }
  722. while (n < 0) {
  723. decrement (sparse_bidirectional_iterator_tag ());
  724. ++n;
  725. }
  726. }
  727. BOOST_UBLAS_INLINE
  728. void decrement (sparse_bidirectional_iterator_tag, difference_type n) {
  729. while (n > 0) {
  730. decrement (sparse_bidirectional_iterator_tag ());
  731. --n;
  732. }
  733. while (n < 0) {
  734. increment (sparse_bidirectional_iterator_tag ());
  735. ++n;
  736. }
  737. }
  738. BOOST_UBLAS_INLINE
  739. value_type dereference (sparse_bidirectional_iterator_tag) const {
  740. typename E1::value_type t1 = typename E1::value_type/*zero*/();
  741. if (it1_ != it1_end_)
  742. if (it1_.index () == i_)
  743. t1 = *it1_;
  744. typename E2::value_type t2 = typename E2::value_type/*zero*/();
  745. if (it2_ != it2_end_)
  746. if (it2_.index () == i_)
  747. t2 = *it2_;
  748. return static_cast<value_type>(functor_type::apply (t1, t2));
  749. }
  750. public:
  751. // Arithmetic
  752. BOOST_UBLAS_INLINE
  753. const_iterator &operator ++ () {
  754. increment (iterator_category ());
  755. return *this;
  756. }
  757. BOOST_UBLAS_INLINE
  758. const_iterator &operator -- () {
  759. decrement (iterator_category ());
  760. return *this;
  761. }
  762. BOOST_UBLAS_INLINE
  763. const_iterator &operator += (difference_type n) {
  764. increment (iterator_category (), n);
  765. return *this;
  766. }
  767. BOOST_UBLAS_INLINE
  768. const_iterator &operator -= (difference_type n) {
  769. decrement (iterator_category (), n);
  770. return *this;
  771. }
  772. BOOST_UBLAS_INLINE
  773. difference_type operator - (const const_iterator &it) const {
  774. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  775. return index () - it.index ();
  776. }
  777. // Dereference
  778. BOOST_UBLAS_INLINE
  779. const_reference operator * () const {
  780. return dereference (iterator_category ());
  781. }
  782. BOOST_UBLAS_INLINE
  783. const_reference operator [] (difference_type n) const {
  784. return *(*this + n);
  785. }
  786. // Index
  787. BOOST_UBLAS_INLINE
  788. size_type index () const {
  789. return i_;
  790. }
  791. // Assignment
  792. BOOST_UBLAS_INLINE
  793. const_iterator &operator = (const const_iterator &it) {
  794. container_const_reference<self_type>::assign (&it ());
  795. i_ = it.i_;
  796. it1_ = it.it1_;
  797. it1_end_ = it.it1_end_;
  798. it2_ = it.it2_;
  799. it2_end_ = it.it2_end_;
  800. return *this;
  801. }
  802. // Comparison
  803. BOOST_UBLAS_INLINE
  804. bool operator == (const const_iterator &it) const {
  805. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  806. return index () == it.index ();
  807. }
  808. BOOST_UBLAS_INLINE
  809. bool operator < (const const_iterator &it) const {
  810. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  811. return index () < it.index ();
  812. }
  813. private:
  814. size_type i_;
  815. const_subiterator1_type it1_;
  816. const_subiterator1_type it1_end_;
  817. const_subiterator2_type it2_;
  818. const_subiterator2_type it2_end_;
  819. };
  820. #endif
  821. BOOST_UBLAS_INLINE
  822. const_iterator begin () const {
  823. return find (0);
  824. }
  825. BOOST_UBLAS_INLINE
  826. const_iterator cbegin () const {
  827. return begin ();
  828. }
  829. BOOST_UBLAS_INLINE
  830. const_iterator end () const {
  831. return find (size ());
  832. }
  833. BOOST_UBLAS_INLINE
  834. const_iterator cend () const {
  835. return end ();
  836. }
  837. // Reverse iterator
  838. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  839. BOOST_UBLAS_INLINE
  840. const_reverse_iterator rbegin () const {
  841. return const_reverse_iterator (end ());
  842. }
  843. BOOST_UBLAS_INLINE
  844. const_reverse_iterator crbegin () const {
  845. return rbegin ();
  846. }
  847. BOOST_UBLAS_INLINE
  848. const_reverse_iterator rend () const {
  849. return const_reverse_iterator (begin ());
  850. }
  851. BOOST_UBLAS_INLINE
  852. const_reverse_iterator crend () const {
  853. return rend ();
  854. }
  855. private:
  856. expression1_closure_type e1_;
  857. expression2_closure_type e2_;
  858. };
  859. template<class E1, class E2, class F>
  860. struct vector_binary_traits {
  861. typedef vector_binary<E1, E2, F> expression_type;
  862. #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
  863. typedef expression_type result_type;
  864. #else
  865. typedef typename E1::vector_temporary_type result_type;
  866. #endif
  867. };
  868. // (v1 + v2) [i] = v1 [i] + v2 [i]
  869. template<class E1, class E2>
  870. BOOST_UBLAS_INLINE
  871. typename vector_binary_traits<E1, E2, scalar_plus<typename E1::value_type,
  872. typename E2::value_type> >::result_type
  873. operator + (const vector_expression<E1> &e1,
  874. const vector_expression<E2> &e2) {
  875. typedef typename vector_binary_traits<E1, E2, scalar_plus<typename E1::value_type,
  876. typename E2::value_type> >::expression_type expression_type;
  877. return expression_type (e1 (), e2 ());
  878. }
  879. // (v1 - v2) [i] = v1 [i] - v2 [i]
  880. template<class E1, class E2>
  881. BOOST_UBLAS_INLINE
  882. typename vector_binary_traits<E1, E2, scalar_minus<typename E1::value_type,
  883. typename E2::value_type> >::result_type
  884. operator - (const vector_expression<E1> &e1,
  885. const vector_expression<E2> &e2) {
  886. typedef typename vector_binary_traits<E1, E2, scalar_minus<typename E1::value_type,
  887. typename E2::value_type> >::expression_type expression_type;
  888. return expression_type (e1 (), e2 ());
  889. }
  890. // (v1 * v2) [i] = v1 [i] * v2 [i]
  891. template<class E1, class E2>
  892. BOOST_UBLAS_INLINE
  893. typename vector_binary_traits<E1, E2, scalar_multiplies<typename E1::value_type,
  894. typename E2::value_type> >::result_type
  895. element_prod (const vector_expression<E1> &e1,
  896. const vector_expression<E2> &e2) {
  897. typedef typename vector_binary_traits<E1, E2, scalar_multiplies<typename E1::value_type,
  898. typename E2::value_type> >::expression_type expression_type;
  899. return expression_type (e1 (), e2 ());
  900. }
  901. // (v1 / v2) [i] = v1 [i] / v2 [i]
  902. template<class E1, class E2>
  903. BOOST_UBLAS_INLINE
  904. typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
  905. typename E2::value_type> >::result_type
  906. element_div (const vector_expression<E1> &e1,
  907. const vector_expression<E2> &e2) {
  908. typedef typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
  909. typename E2::value_type> >::expression_type expression_type;
  910. return expression_type (e1 (), e2 ());
  911. }
  912. template<class E1, class E2, class F>
  913. class vector_binary_scalar1:
  914. public vector_expression<vector_binary_scalar1<E1, E2, F> > {
  915. typedef F functor_type;
  916. typedef E1 expression1_type;
  917. typedef E2 expression2_type;
  918. public:
  919. typedef const E1& expression1_closure_type;
  920. typedef typename E2::const_closure_type expression2_closure_type;
  921. private:
  922. typedef vector_binary_scalar1<E1, E2, F> self_type;
  923. public:
  924. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  925. using vector_expression<vector_binary_scalar1<E1, E2, F> >::operator ();
  926. #endif
  927. typedef typename E2::size_type size_type;
  928. typedef typename E2::difference_type difference_type;
  929. typedef typename F::result_type value_type;
  930. typedef value_type const_reference;
  931. typedef const_reference reference;
  932. typedef const self_type const_closure_type;
  933. typedef const_closure_type closure_type;
  934. typedef unknown_storage_tag storage_category;
  935. // Construction and destruction
  936. BOOST_UBLAS_INLINE
  937. vector_binary_scalar1 (const expression1_type &e1, const expression2_type &e2):
  938. e1_ (e1), e2_ (e2) {}
  939. // Accessors
  940. BOOST_UBLAS_INLINE
  941. size_type size () const {
  942. return e2_.size ();
  943. }
  944. public:
  945. // Element access
  946. BOOST_UBLAS_INLINE
  947. const_reference operator () (size_type i) const {
  948. return functor_type::apply (e1_, e2_ (i));
  949. }
  950. BOOST_UBLAS_INLINE
  951. const_reference operator [] (size_type i) const {
  952. return functor_type::apply (e1_, e2_ [i]);
  953. }
  954. // Closure comparison
  955. BOOST_UBLAS_INLINE
  956. bool same_closure (const vector_binary_scalar1 &vbs1) const {
  957. return &e1_ == &(vbs1.e1_) &&
  958. (*this).e2_.same_closure (vbs1.e2_);
  959. }
  960. // Iterator types
  961. private:
  962. typedef expression1_type const_subiterator1_type;
  963. typedef typename expression2_type::const_iterator const_subiterator2_type;
  964. typedef const value_type *const_pointer;
  965. public:
  966. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  967. typedef indexed_const_iterator<const_closure_type, typename const_subiterator2_type::iterator_category> const_iterator;
  968. typedef const_iterator iterator;
  969. #else
  970. class const_iterator;
  971. typedef const_iterator iterator;
  972. #endif
  973. // Element lookup
  974. BOOST_UBLAS_INLINE
  975. const_iterator find (size_type i) const {
  976. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  977. const_subiterator2_type it (e2_.find (i));
  978. return const_iterator (*this, it.index ());
  979. #else
  980. return const_iterator (*this, const_subiterator1_type (e1_), e2_.find (i));
  981. #endif
  982. }
  983. // Iterator enhances the iterator of the referenced vector expression
  984. // with the binary functor.
  985. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  986. class const_iterator:
  987. public container_const_reference<vector_binary_scalar1>,
  988. public iterator_base_traits<typename E2::const_iterator::iterator_category>::template
  989. iterator_base<const_iterator, value_type>::type {
  990. public:
  991. typedef typename E2::const_iterator::iterator_category iterator_category;
  992. typedef typename vector_binary_scalar1::difference_type difference_type;
  993. typedef typename vector_binary_scalar1::value_type value_type;
  994. typedef typename vector_binary_scalar1::const_reference reference;
  995. typedef typename vector_binary_scalar1::const_pointer pointer;
  996. // Construction and destruction
  997. BOOST_UBLAS_INLINE
  998. const_iterator ():
  999. container_const_reference<self_type> (), it1_ (), it2_ () {}
  1000. BOOST_UBLAS_INLINE
  1001. const_iterator (const self_type &vbs, const const_subiterator1_type &it1, const const_subiterator2_type &it2):
  1002. container_const_reference<self_type> (vbs), it1_ (it1), it2_ (it2) {}
  1003. // Arithmetic
  1004. BOOST_UBLAS_INLINE
  1005. const_iterator &operator ++ () {
  1006. ++ it2_;
  1007. return *this;
  1008. }
  1009. BOOST_UBLAS_INLINE
  1010. const_iterator &operator -- () {
  1011. -- it2_;
  1012. return *this;
  1013. }
  1014. BOOST_UBLAS_INLINE
  1015. const_iterator &operator += (difference_type n) {
  1016. it2_ += n;
  1017. return *this;
  1018. }
  1019. BOOST_UBLAS_INLINE
  1020. const_iterator &operator -= (difference_type n) {
  1021. it2_ -= n;
  1022. return *this;
  1023. }
  1024. BOOST_UBLAS_INLINE
  1025. difference_type operator - (const const_iterator &it) const {
  1026. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1027. // FIXME we shouldn't compare floats
  1028. // BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1029. return it2_ - it.it2_;
  1030. }
  1031. // Dereference
  1032. BOOST_UBLAS_INLINE
  1033. const_reference operator * () const {
  1034. return functor_type::apply (it1_, *it2_);
  1035. }
  1036. BOOST_UBLAS_INLINE
  1037. const_reference operator [] (difference_type n) const {
  1038. return *(*this + n);
  1039. }
  1040. // Index
  1041. BOOST_UBLAS_INLINE
  1042. size_type index () const {
  1043. return it2_.index ();
  1044. }
  1045. // Assignment
  1046. BOOST_UBLAS_INLINE
  1047. const_iterator &operator = (const const_iterator &it) {
  1048. container_const_reference<self_type>::assign (&it ());
  1049. it1_ = it.it1_;
  1050. it2_ = it.it2_;
  1051. return *this;
  1052. }
  1053. // Comparison
  1054. BOOST_UBLAS_INLINE
  1055. bool operator == (const const_iterator &it) const {
  1056. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1057. // FIXME we shouldn't compare floats
  1058. // BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1059. return it2_ == it.it2_;
  1060. }
  1061. BOOST_UBLAS_INLINE
  1062. bool operator < (const const_iterator &it) const {
  1063. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1064. // FIXME we shouldn't compare floats
  1065. // BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1066. return it2_ < it.it2_;
  1067. }
  1068. private:
  1069. const_subiterator1_type it1_;
  1070. const_subiterator2_type it2_;
  1071. };
  1072. #endif
  1073. BOOST_UBLAS_INLINE
  1074. const_iterator begin () const {
  1075. return find (0);
  1076. }
  1077. BOOST_UBLAS_INLINE
  1078. const_iterator cbegin () const {
  1079. return begin ();
  1080. }
  1081. BOOST_UBLAS_INLINE
  1082. const_iterator end () const {
  1083. return find (size ());
  1084. }
  1085. BOOST_UBLAS_INLINE
  1086. const_iterator cend () const {
  1087. return end ();
  1088. }
  1089. // Reverse iterator
  1090. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  1091. BOOST_UBLAS_INLINE
  1092. const_reverse_iterator rbegin () const {
  1093. return const_reverse_iterator (end ());
  1094. }
  1095. BOOST_UBLAS_INLINE
  1096. const_reverse_iterator crbegin () const {
  1097. return rbegin ();
  1098. }
  1099. BOOST_UBLAS_INLINE
  1100. const_reverse_iterator rend () const {
  1101. return const_reverse_iterator (begin ());
  1102. }
  1103. BOOST_UBLAS_INLINE
  1104. const_reverse_iterator crend () const {
  1105. return end ();
  1106. }
  1107. private:
  1108. expression1_closure_type e1_;
  1109. expression2_closure_type e2_;
  1110. };
  1111. template<class E1, class E2, class F>
  1112. struct vector_binary_scalar1_traits {
  1113. typedef vector_binary_scalar1<E1, E2, F> expression_type; // allow E1 to be builtin type
  1114. #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
  1115. typedef expression_type result_type;
  1116. #else
  1117. typedef typename E2::vector_temporary_type result_type;
  1118. #endif
  1119. };
  1120. // (t * v) [i] = t * v [i]
  1121. template<class T1, class E2>
  1122. BOOST_UBLAS_INLINE
  1123. typename boost::enable_if< is_convertible<T1, typename E2::value_type >,
  1124. typename vector_binary_scalar1_traits<const T1, E2, scalar_multiplies<T1, typename E2::value_type> >::result_type
  1125. >::type
  1126. operator * (const T1 &e1,
  1127. const vector_expression<E2> &e2) {
  1128. typedef typename vector_binary_scalar1_traits<const T1, E2, scalar_multiplies<T1, typename E2::value_type> >::expression_type expression_type;
  1129. return expression_type (e1, e2 ());
  1130. }
  1131. template<class E1, class E2, class F>
  1132. class vector_binary_scalar2:
  1133. public vector_expression<vector_binary_scalar2<E1, E2, F> > {
  1134. typedef F functor_type;
  1135. typedef E1 expression1_type;
  1136. typedef E2 expression2_type;
  1137. typedef typename E1::const_closure_type expression1_closure_type;
  1138. typedef const E2& expression2_closure_type;
  1139. typedef vector_binary_scalar2<E1, E2, F> self_type;
  1140. public:
  1141. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  1142. using vector_expression<vector_binary_scalar2<E1, E2, F> >::operator ();
  1143. #endif
  1144. typedef typename E1::size_type size_type;
  1145. typedef typename E1::difference_type difference_type;
  1146. typedef typename F::result_type value_type;
  1147. typedef value_type const_reference;
  1148. typedef const_reference reference;
  1149. typedef const self_type const_closure_type;
  1150. typedef const_closure_type closure_type;
  1151. typedef unknown_storage_tag storage_category;
  1152. // Construction and destruction
  1153. BOOST_UBLAS_INLINE
  1154. vector_binary_scalar2 (const expression1_type &e1, const expression2_type &e2):
  1155. e1_ (e1), e2_ (e2) {}
  1156. // Accessors
  1157. BOOST_UBLAS_INLINE
  1158. size_type size () const {
  1159. return e1_.size ();
  1160. }
  1161. public:
  1162. // Element access
  1163. BOOST_UBLAS_INLINE
  1164. const_reference operator () (size_type i) const {
  1165. return functor_type::apply (e1_ (i), e2_);
  1166. }
  1167. BOOST_UBLAS_INLINE
  1168. const_reference operator [] (size_type i) const {
  1169. return functor_type::apply (e1_ [i], e2_);
  1170. }
  1171. // Closure comparison
  1172. BOOST_UBLAS_INLINE
  1173. bool same_closure (const vector_binary_scalar2 &vbs2) const {
  1174. return (*this).e1_.same_closure (vbs2.e1_) &&
  1175. &e2_ == &(vbs2.e2_);
  1176. }
  1177. // Iterator types
  1178. private:
  1179. typedef typename expression1_type::const_iterator const_subiterator1_type;
  1180. typedef expression2_type const_subiterator2_type;
  1181. typedef const value_type *const_pointer;
  1182. public:
  1183. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1184. typedef indexed_const_iterator<const_closure_type, typename const_subiterator2_type::iterator_category> const_iterator;
  1185. typedef const_iterator iterator;
  1186. #else
  1187. class const_iterator;
  1188. typedef const_iterator iterator;
  1189. #endif
  1190. // Element lookup
  1191. BOOST_UBLAS_INLINE
  1192. const_iterator find (size_type i) const {
  1193. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1194. const_subiterator1_type it (e1_.find (i));
  1195. return const_iterator (*this, it.index ());
  1196. #else
  1197. return const_iterator (*this, e1_.find (i), const_subiterator2_type (e2_));
  1198. #endif
  1199. }
  1200. // Iterator enhances the iterator of the referenced vector expression
  1201. // with the binary functor.
  1202. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1203. class const_iterator:
  1204. public container_const_reference<vector_binary_scalar2>,
  1205. public iterator_base_traits<typename E1::const_iterator::iterator_category>::template
  1206. iterator_base<const_iterator, value_type>::type {
  1207. public:
  1208. typedef typename E1::const_iterator::iterator_category iterator_category;
  1209. typedef typename vector_binary_scalar2::difference_type difference_type;
  1210. typedef typename vector_binary_scalar2::value_type value_type;
  1211. typedef typename vector_binary_scalar2::const_reference reference;
  1212. typedef typename vector_binary_scalar2::const_pointer pointer;
  1213. // Construction and destruction
  1214. BOOST_UBLAS_INLINE
  1215. const_iterator ():
  1216. container_const_reference<self_type> (), it1_ (), it2_ () {}
  1217. BOOST_UBLAS_INLINE
  1218. const_iterator (const self_type &vbs, const const_subiterator1_type &it1, const const_subiterator2_type &it2):
  1219. container_const_reference<self_type> (vbs), it1_ (it1), it2_ (it2) {}
  1220. // Arithmetic
  1221. BOOST_UBLAS_INLINE
  1222. const_iterator &operator ++ () {
  1223. ++ it1_;
  1224. return *this;
  1225. }
  1226. BOOST_UBLAS_INLINE
  1227. const_iterator &operator -- () {
  1228. -- it1_;
  1229. return *this;
  1230. }
  1231. BOOST_UBLAS_INLINE
  1232. const_iterator &operator += (difference_type n) {
  1233. it1_ += n;
  1234. return *this;
  1235. }
  1236. BOOST_UBLAS_INLINE
  1237. const_iterator &operator -= (difference_type n) {
  1238. it1_ -= n;
  1239. return *this;
  1240. }
  1241. BOOST_UBLAS_INLINE
  1242. difference_type operator - (const const_iterator &it) const {
  1243. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1244. // FIXME we shouldn't compare floats
  1245. // BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  1246. return it1_ - it.it1_;
  1247. }
  1248. // Dereference
  1249. BOOST_UBLAS_INLINE
  1250. const_reference operator * () const {
  1251. return functor_type::apply (*it1_, it2_);
  1252. }
  1253. BOOST_UBLAS_INLINE
  1254. const_reference operator [] (difference_type n) const {
  1255. return *(*this + n);
  1256. }
  1257. // Index
  1258. BOOST_UBLAS_INLINE
  1259. size_type index () const {
  1260. return it1_.index ();
  1261. }
  1262. // Assignment
  1263. BOOST_UBLAS_INLINE
  1264. const_iterator &operator = (const const_iterator &it) {
  1265. container_const_reference<self_type>::assign (&it ());
  1266. it1_ = it.it1_;
  1267. it2_ = it.it2_;
  1268. return *this;
  1269. }
  1270. // Comparison
  1271. BOOST_UBLAS_INLINE
  1272. bool operator == (const const_iterator &it) const {
  1273. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1274. // FIXME we shouldn't compare floats
  1275. // BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  1276. return it1_ == it.it1_;
  1277. }
  1278. BOOST_UBLAS_INLINE
  1279. bool operator < (const const_iterator &it) const {
  1280. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1281. // FIXME we shouldn't compare floats
  1282. // BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  1283. return it1_ < it.it1_;
  1284. }
  1285. private:
  1286. const_subiterator1_type it1_;
  1287. const_subiterator2_type it2_;
  1288. };
  1289. #endif
  1290. BOOST_UBLAS_INLINE
  1291. const_iterator begin () const {
  1292. return find (0);
  1293. }
  1294. BOOST_UBLAS_INLINE
  1295. const_iterator cbegin () const {
  1296. return begin ();
  1297. }
  1298. BOOST_UBLAS_INLINE
  1299. const_iterator end () const {
  1300. return find (size ());
  1301. }
  1302. BOOST_UBLAS_INLINE
  1303. const_iterator cend () const {
  1304. return end ();
  1305. }
  1306. // Reverse iterator
  1307. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  1308. BOOST_UBLAS_INLINE
  1309. const_reverse_iterator rbegin () const {
  1310. return const_reverse_iterator (end ());
  1311. }
  1312. BOOST_UBLAS_INLINE
  1313. const_reverse_iterator crbegin () const {
  1314. return rbegin ();
  1315. }
  1316. BOOST_UBLAS_INLINE
  1317. const_reverse_iterator rend () const {
  1318. return const_reverse_iterator (begin ());
  1319. }
  1320. BOOST_UBLAS_INLINE
  1321. const_reverse_iterator crend () const {
  1322. return rend ();
  1323. }
  1324. private:
  1325. expression1_closure_type e1_;
  1326. expression2_closure_type e2_;
  1327. };
  1328. template<class E1, class E2, class F>
  1329. struct vector_binary_scalar2_traits {
  1330. typedef vector_binary_scalar2<E1, E2, F> expression_type; // allow E2 to be builtin type
  1331. #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
  1332. typedef expression_type result_type;
  1333. #else
  1334. typedef typename E1::vector_temporary_type result_type;
  1335. #endif
  1336. };
  1337. // (v * t) [i] = v [i] * t
  1338. template<class E1, class T2>
  1339. BOOST_UBLAS_INLINE
  1340. typename boost::enable_if< is_convertible<T2, typename E1::value_type >,
  1341. typename vector_binary_scalar2_traits<E1, const T2, scalar_multiplies<typename E1::value_type, T2> >::result_type
  1342. >::type
  1343. operator * (const vector_expression<E1> &e1,
  1344. const T2 &e2) {
  1345. typedef typename vector_binary_scalar2_traits<E1, const T2, scalar_multiplies<typename E1::value_type, T2> >::expression_type expression_type;
  1346. return expression_type (e1 (), e2);
  1347. }
  1348. // (v / t) [i] = v [i] / t
  1349. template<class E1, class T2>
  1350. BOOST_UBLAS_INLINE
  1351. typename boost::enable_if< is_convertible<T2, typename E1::value_type >,
  1352. typename vector_binary_scalar2_traits<E1, const T2, scalar_divides<typename E1::value_type, T2> >::result_type
  1353. >::type
  1354. operator / (const vector_expression<E1> &e1,
  1355. const T2 &e2) {
  1356. typedef typename vector_binary_scalar2_traits<E1, const T2, scalar_divides<typename E1::value_type, T2> >::expression_type expression_type;
  1357. return expression_type (e1 (), e2);
  1358. }
  1359. template<class E, class F>
  1360. class vector_scalar_unary:
  1361. public scalar_expression<vector_scalar_unary<E, F> > {
  1362. typedef E expression_type;
  1363. typedef F functor_type;
  1364. typedef typename E::const_closure_type expression_closure_type;
  1365. typedef typename E::const_iterator::iterator_category iterator_category;
  1366. typedef vector_scalar_unary<E, F> self_type;
  1367. public:
  1368. typedef typename F::result_type value_type;
  1369. typedef typename E::difference_type difference_type;
  1370. typedef const self_type const_closure_type;
  1371. typedef const_closure_type closure_type;
  1372. typedef unknown_storage_tag storage_category;
  1373. // Construction and destruction
  1374. BOOST_UBLAS_INLINE
  1375. explicit vector_scalar_unary (const expression_type &e):
  1376. e_ (e) {}
  1377. private:
  1378. // Expression accessors
  1379. BOOST_UBLAS_INLINE
  1380. const expression_closure_type &expression () const {
  1381. return e_;
  1382. }
  1383. public:
  1384. BOOST_UBLAS_INLINE
  1385. operator value_type () const {
  1386. return evaluate (iterator_category ());
  1387. }
  1388. private:
  1389. // Dense random access specialization
  1390. BOOST_UBLAS_INLINE
  1391. value_type evaluate (dense_random_access_iterator_tag) const {
  1392. #ifdef BOOST_UBLAS_USE_INDEXING
  1393. return functor_type::apply (e_);
  1394. #elif BOOST_UBLAS_USE_ITERATING
  1395. difference_type size = e_.size ();
  1396. return functor_type::apply (size, e_.begin ());
  1397. #else
  1398. difference_type size = e_.size ();
  1399. if (size >= BOOST_UBLAS_ITERATOR_THRESHOLD)
  1400. return functor_type::apply (size, e_.begin ());
  1401. else
  1402. return functor_type::apply (e_);
  1403. #endif
  1404. }
  1405. // Packed bidirectional specialization
  1406. BOOST_UBLAS_INLINE
  1407. value_type evaluate (packed_random_access_iterator_tag) const {
  1408. return functor_type::apply (e_.begin (), e_.end ());
  1409. }
  1410. // Sparse bidirectional specialization
  1411. BOOST_UBLAS_INLINE
  1412. value_type evaluate (sparse_bidirectional_iterator_tag) const {
  1413. return functor_type::apply (e_.begin (), e_.end ());
  1414. }
  1415. private:
  1416. expression_closure_type e_;
  1417. };
  1418. template<class E, class F>
  1419. struct vector_scalar_unary_traits {
  1420. typedef vector_scalar_unary<E, F> expression_type;
  1421. #if !defined (BOOST_UBLAS_SIMPLE_ET_DEBUG) && defined (BOOST_UBLAS_USE_SCALAR_ET)
  1422. // FIXME don't define USE_SCALAR_ET other then for testing
  1423. // They do not work for complex types
  1424. typedef expression_type result_type;
  1425. #else
  1426. typedef typename F::result_type result_type;
  1427. #endif
  1428. };
  1429. // sum v = sum (v [i])
  1430. template<class E>
  1431. BOOST_UBLAS_INLINE
  1432. typename vector_scalar_unary_traits<E, vector_sum<E> >::result_type
  1433. sum (const vector_expression<E> &e) {
  1434. typedef typename vector_scalar_unary_traits<E, vector_sum<E> >::expression_type expression_type;
  1435. return expression_type (e ());
  1436. }
  1437. // real: norm_1 v = sum (abs (v [i]))
  1438. // complex: norm_1 v = sum (abs (real (v [i])) + abs (imag (v [i])))
  1439. template<class E>
  1440. BOOST_UBLAS_INLINE
  1441. typename vector_scalar_unary_traits<E, vector_norm_1<E> >::result_type
  1442. norm_1 (const vector_expression<E> &e) {
  1443. typedef typename vector_scalar_unary_traits<E, vector_norm_1<E> >::expression_type expression_type;
  1444. return expression_type (e ());
  1445. }
  1446. // real: norm_2 v = sqrt (sum (v [i] * v [i]))
  1447. // complex: norm_2 v = sqrt (sum (v [i] * conj (v [i])))
  1448. template<class E>
  1449. BOOST_UBLAS_INLINE
  1450. typename vector_scalar_unary_traits<E, vector_norm_2<E> >::result_type
  1451. norm_2 (const vector_expression<E> &e) {
  1452. typedef typename vector_scalar_unary_traits<E, vector_norm_2<E> >::expression_type expression_type;
  1453. return expression_type (e ());
  1454. }
  1455. // real: norm_2_square v = sum(v [i] * v [i])
  1456. // complex: norm_2_square v = sum(v [i] * conj (v [i]))
  1457. template<class E>
  1458. BOOST_UBLAS_INLINE
  1459. typename vector_scalar_unary_traits<E, vector_norm_2_square<E> >::result_type
  1460. norm_2_square (const vector_expression<E> &e) {
  1461. typedef typename vector_scalar_unary_traits<E, vector_norm_2_square<E> >::expression_type expression_type;
  1462. return expression_type (e ());
  1463. }
  1464. // real: norm_inf v = maximum (abs (v [i]))
  1465. // complex: norm_inf v = maximum (maximum (abs (real (v [i])), abs (imag (v [i]))))
  1466. template<class E>
  1467. BOOST_UBLAS_INLINE
  1468. typename vector_scalar_unary_traits<E, vector_norm_inf<E> >::result_type
  1469. norm_inf (const vector_expression<E> &e) {
  1470. typedef typename vector_scalar_unary_traits<E, vector_norm_inf<E> >::expression_type expression_type;
  1471. return expression_type (e ());
  1472. }
  1473. // real: index_norm_inf v = minimum (i: abs (v [i]) == maximum (abs (v [i])))
  1474. template<class E>
  1475. BOOST_UBLAS_INLINE
  1476. typename vector_scalar_unary_traits<E, vector_index_norm_inf<E> >::result_type
  1477. index_norm_inf (const vector_expression<E> &e) {
  1478. typedef typename vector_scalar_unary_traits<E, vector_index_norm_inf<E> >::expression_type expression_type;
  1479. return expression_type (e ());
  1480. }
  1481. template<class E1, class E2, class F>
  1482. class vector_scalar_binary:
  1483. public scalar_expression<vector_scalar_binary<E1, E2, F> > {
  1484. typedef E1 expression1_type;
  1485. typedef E2 expression2_type;
  1486. typedef F functor_type;
  1487. typedef typename E1::const_closure_type expression1_closure_type;
  1488. typedef typename E2::const_closure_type expression2_closure_type;
  1489. typedef typename iterator_restrict_traits<typename E1::const_iterator::iterator_category,
  1490. typename E2::const_iterator::iterator_category>::iterator_category iterator_category;
  1491. typedef vector_scalar_binary<E1, E2, F> self_type;
  1492. public:
  1493. static const unsigned complexity = 1;
  1494. typedef typename F::result_type value_type;
  1495. typedef typename E1::difference_type difference_type;
  1496. typedef const self_type const_closure_type;
  1497. typedef const_closure_type closure_type;
  1498. typedef unknown_storage_tag storage_category;
  1499. // Construction and destruction
  1500. BOOST_UBLAS_INLINE
  1501. vector_scalar_binary (const expression1_type &e1, const expression2_type &e2):
  1502. e1_ (e1), e2_ (e2) {}
  1503. private:
  1504. // Accessors
  1505. BOOST_UBLAS_INLINE
  1506. const expression1_closure_type &expression1 () const {
  1507. return e1_;
  1508. }
  1509. BOOST_UBLAS_INLINE
  1510. const expression2_closure_type &expression2 () const {
  1511. return e2_;
  1512. }
  1513. public:
  1514. BOOST_UBLAS_INLINE
  1515. operator value_type () const {
  1516. return evaluate (iterator_category ());
  1517. }
  1518. private:
  1519. // Dense random access specialization
  1520. BOOST_UBLAS_INLINE
  1521. value_type evaluate (dense_random_access_iterator_tag) const {
  1522. BOOST_UBLAS_CHECK (e1_.size () == e2_.size (), external_logic());
  1523. #ifdef BOOST_UBLAS_USE_INDEXING
  1524. return functor_type::apply (e1_, e2_);
  1525. #elif BOOST_UBLAS_USE_ITERATING
  1526. difference_type size = BOOST_UBLAS_SAME (e1_.size (), e2_.size ());
  1527. return functor_type::apply (size, e1_.begin (), e2_.begin ());
  1528. #else
  1529. difference_type size = BOOST_UBLAS_SAME (e1_.size (), e2_.size ());
  1530. if (size >= BOOST_UBLAS_ITERATOR_THRESHOLD)
  1531. return functor_type::apply (size, e1_.begin (), e2_.begin ());
  1532. else
  1533. return functor_type::apply (e1_, e2_);
  1534. #endif
  1535. }
  1536. // Packed bidirectional specialization
  1537. BOOST_UBLAS_INLINE
  1538. value_type evaluate (packed_random_access_iterator_tag) const {
  1539. BOOST_UBLAS_CHECK (e1_.size () == e2_.size (), external_logic());
  1540. return functor_type::apply (e1_.begin (), e1_.end (), e2_.begin (), e2_.end ());
  1541. }
  1542. // Sparse bidirectional specialization
  1543. BOOST_UBLAS_INLINE
  1544. value_type evaluate (sparse_bidirectional_iterator_tag) const {
  1545. BOOST_UBLAS_CHECK (e1_.size () == e2_.size (), external_logic());
  1546. return functor_type::apply (e1_.begin (), e1_.end (), e2_.begin (), e2_.end (), sparse_bidirectional_iterator_tag ());
  1547. }
  1548. private:
  1549. expression1_closure_type e1_;
  1550. expression2_closure_type e2_;
  1551. };
  1552. template<class E1, class E2, class F>
  1553. struct vector_scalar_binary_traits {
  1554. typedef vector_scalar_binary<E1, E2, F> expression_type;
  1555. #if !defined (BOOST_UBLAS_SIMPLE_ET_DEBUG) && defined (BOOST_UBLAS_USE_SCALAR_ET)
  1556. // FIXME don't define USE_SCALAR_ET other then for testing
  1557. // They do not work for complex types
  1558. typedef expression_type result_type;
  1559. #else
  1560. typedef typename F::result_type result_type;
  1561. #endif
  1562. };
  1563. // inner_prod (v1, v2) = sum (v1 [i] * v2 [i])
  1564. template<class E1, class E2>
  1565. BOOST_UBLAS_INLINE
  1566. typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
  1567. typename promote_traits<typename E1::value_type,
  1568. typename E2::value_type>::promote_type> >::result_type
  1569. inner_prod (const vector_expression<E1> &e1,
  1570. const vector_expression<E2> &e2) {
  1571. typedef typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
  1572. typename promote_traits<typename E1::value_type,
  1573. typename E2::value_type>::promote_type> >::expression_type expression_type;
  1574. return expression_type (e1 (), e2 ());
  1575. }
  1576. template<class E1, class E2>
  1577. BOOST_UBLAS_INLINE
  1578. typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
  1579. typename type_traits<typename promote_traits<typename E1::value_type,
  1580. typename E2::value_type>::promote_type>::precision_type> >::result_type
  1581. prec_inner_prod (const vector_expression<E1> &e1,
  1582. const vector_expression<E2> &e2) {
  1583. typedef typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<E1, E2,
  1584. typename type_traits<typename promote_traits<typename E1::value_type,
  1585. typename E2::value_type>::promote_type>::precision_type> >::expression_type expression_type;
  1586. return expression_type (e1 (), e2 ());
  1587. }
  1588. }}}
  1589. #endif