matrix_assign.hpp 77 KB

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