functional.hpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  1. //
  2. // Copyright (c) 2000-2009
  3. // Joerg Walter, Mathias Koch, Gunter Winkler
  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_FUNCTIONAL_
  13. #define _BOOST_UBLAS_FUNCTIONAL_
  14. #include <functional>
  15. #include <boost/core/ignore_unused.hpp>
  16. #include <boost/numeric/ublas/traits.hpp>
  17. #ifdef BOOST_UBLAS_USE_DUFF_DEVICE
  18. #include <boost/numeric/ublas/detail/duff.hpp>
  19. #endif
  20. #ifdef BOOST_UBLAS_USE_SIMD
  21. #include <boost/numeric/ublas/detail/raw.hpp>
  22. #else
  23. namespace boost { namespace numeric { namespace ublas { namespace raw {
  24. }}}}
  25. #endif
  26. #ifdef BOOST_UBLAS_HAVE_BINDINGS
  27. #include <boost/numeric/bindings/traits/std_vector.hpp>
  28. #include <boost/numeric/bindings/traits/ublas_vector.hpp>
  29. #include <boost/numeric/bindings/traits/ublas_matrix.hpp>
  30. #include <boost/numeric/bindings/atlas/cblas.hpp>
  31. #endif
  32. #include <boost/numeric/ublas/detail/definitions.hpp>
  33. namespace boost { namespace numeric { namespace ublas {
  34. // Scalar functors
  35. // Unary
  36. template<class T>
  37. struct scalar_unary_functor {
  38. typedef T value_type;
  39. typedef typename type_traits<T>::const_reference argument_type;
  40. typedef typename type_traits<T>::value_type result_type;
  41. };
  42. template<class T>
  43. struct scalar_identity:
  44. public scalar_unary_functor<T> {
  45. typedef typename scalar_unary_functor<T>::argument_type argument_type;
  46. typedef typename scalar_unary_functor<T>::result_type result_type;
  47. static BOOST_UBLAS_INLINE
  48. result_type apply (argument_type t) {
  49. return t;
  50. }
  51. };
  52. template<class T>
  53. struct scalar_negate:
  54. public scalar_unary_functor<T> {
  55. typedef typename scalar_unary_functor<T>::argument_type argument_type;
  56. typedef typename scalar_unary_functor<T>::result_type result_type;
  57. static BOOST_UBLAS_INLINE
  58. result_type apply (argument_type t) {
  59. return - t;
  60. }
  61. };
  62. template<class T>
  63. struct scalar_conj:
  64. public scalar_unary_functor<T> {
  65. typedef typename scalar_unary_functor<T>::value_type value_type;
  66. typedef typename scalar_unary_functor<T>::argument_type argument_type;
  67. typedef typename scalar_unary_functor<T>::result_type result_type;
  68. static BOOST_UBLAS_INLINE
  69. result_type apply (argument_type t) {
  70. return type_traits<value_type>::conj (t);
  71. }
  72. };
  73. // Unary returning real
  74. template<class T>
  75. struct scalar_real_unary_functor {
  76. typedef T value_type;
  77. typedef typename type_traits<T>::const_reference argument_type;
  78. typedef typename type_traits<T>::real_type result_type;
  79. };
  80. template<class T>
  81. struct scalar_real:
  82. public scalar_real_unary_functor<T> {
  83. typedef typename scalar_real_unary_functor<T>::value_type value_type;
  84. typedef typename scalar_real_unary_functor<T>::argument_type argument_type;
  85. typedef typename scalar_real_unary_functor<T>::result_type result_type;
  86. static BOOST_UBLAS_INLINE
  87. result_type apply (argument_type t) {
  88. return type_traits<value_type>::real (t);
  89. }
  90. };
  91. template<class T>
  92. struct scalar_imag:
  93. public scalar_real_unary_functor<T> {
  94. typedef typename scalar_real_unary_functor<T>::value_type value_type;
  95. typedef typename scalar_real_unary_functor<T>::argument_type argument_type;
  96. typedef typename scalar_real_unary_functor<T>::result_type result_type;
  97. static BOOST_UBLAS_INLINE
  98. result_type apply (argument_type t) {
  99. return type_traits<value_type>::imag (t);
  100. }
  101. };
  102. // Binary
  103. template<class T1, class T2>
  104. struct scalar_binary_functor {
  105. typedef typename type_traits<T1>::const_reference argument1_type;
  106. typedef typename type_traits<T2>::const_reference argument2_type;
  107. typedef typename promote_traits<T1, T2>::promote_type result_type;
  108. };
  109. template<class T1, class T2>
  110. struct scalar_plus:
  111. public scalar_binary_functor<T1, T2> {
  112. typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
  113. typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
  114. typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
  115. static BOOST_UBLAS_INLINE
  116. result_type apply (argument1_type t1, argument2_type t2) {
  117. return t1 + t2;
  118. }
  119. };
  120. template<class T1, class T2>
  121. struct scalar_minus:
  122. public scalar_binary_functor<T1, T2> {
  123. typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
  124. typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
  125. typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
  126. static BOOST_UBLAS_INLINE
  127. result_type apply (argument1_type t1, argument2_type t2) {
  128. return t1 - t2;
  129. }
  130. };
  131. template<class T1, class T2>
  132. struct scalar_multiplies:
  133. public scalar_binary_functor<T1, T2> {
  134. typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
  135. typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
  136. typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
  137. static BOOST_UBLAS_INLINE
  138. result_type apply (argument1_type t1, argument2_type t2) {
  139. return t1 * t2;
  140. }
  141. };
  142. template<class T1, class T2>
  143. struct scalar_divides:
  144. public scalar_binary_functor<T1, T2> {
  145. typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
  146. typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
  147. typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
  148. static BOOST_UBLAS_INLINE
  149. result_type apply (argument1_type t1, argument2_type t2) {
  150. return t1 / t2;
  151. }
  152. };
  153. template<class T1, class T2>
  154. struct scalar_binary_assign_functor {
  155. // ISSUE Remove reference to avoid reference to reference problems
  156. typedef typename type_traits<typename boost::remove_reference<T1>::type>::reference argument1_type;
  157. typedef typename type_traits<T2>::const_reference argument2_type;
  158. };
  159. struct assign_tag {};
  160. struct computed_assign_tag {};
  161. template<class T1, class T2>
  162. struct scalar_assign:
  163. public scalar_binary_assign_functor<T1, T2> {
  164. typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
  165. typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
  166. #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
  167. static const bool computed ;
  168. #else
  169. static const bool computed = false ;
  170. #endif
  171. static BOOST_UBLAS_INLINE
  172. void apply (argument1_type t1, argument2_type t2) {
  173. t1 = t2;
  174. }
  175. template<class U1, class U2>
  176. struct rebind {
  177. typedef scalar_assign<U1, U2> other;
  178. };
  179. };
  180. #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
  181. template<class T1, class T2>
  182. const bool scalar_assign<T1,T2>::computed = false;
  183. #endif
  184. template<class T1, class T2>
  185. struct scalar_plus_assign:
  186. public scalar_binary_assign_functor<T1, T2> {
  187. typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
  188. typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
  189. #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
  190. static const bool computed ;
  191. #else
  192. static const bool computed = true ;
  193. #endif
  194. static BOOST_UBLAS_INLINE
  195. void apply (argument1_type t1, argument2_type t2) {
  196. t1 += t2;
  197. }
  198. template<class U1, class U2>
  199. struct rebind {
  200. typedef scalar_plus_assign<U1, U2> other;
  201. };
  202. };
  203. #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
  204. template<class T1, class T2>
  205. const bool scalar_plus_assign<T1,T2>::computed = true;
  206. #endif
  207. template<class T1, class T2>
  208. struct scalar_minus_assign:
  209. public scalar_binary_assign_functor<T1, T2> {
  210. typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
  211. typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
  212. #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
  213. static const bool computed ;
  214. #else
  215. static const bool computed = true ;
  216. #endif
  217. static BOOST_UBLAS_INLINE
  218. void apply (argument1_type t1, argument2_type t2) {
  219. t1 -= t2;
  220. }
  221. template<class U1, class U2>
  222. struct rebind {
  223. typedef scalar_minus_assign<U1, U2> other;
  224. };
  225. };
  226. #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
  227. template<class T1, class T2>
  228. const bool scalar_minus_assign<T1,T2>::computed = true;
  229. #endif
  230. template<class T1, class T2>
  231. struct scalar_multiplies_assign:
  232. public scalar_binary_assign_functor<T1, T2> {
  233. typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
  234. typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
  235. static const bool computed = true;
  236. static BOOST_UBLAS_INLINE
  237. void apply (argument1_type t1, argument2_type t2) {
  238. t1 *= t2;
  239. }
  240. template<class U1, class U2>
  241. struct rebind {
  242. typedef scalar_multiplies_assign<U1, U2> other;
  243. };
  244. };
  245. template<class T1, class T2>
  246. struct scalar_divides_assign:
  247. public scalar_binary_assign_functor<T1, T2> {
  248. typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
  249. typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
  250. static const bool computed ;
  251. static BOOST_UBLAS_INLINE
  252. void apply (argument1_type t1, argument2_type t2) {
  253. t1 /= t2;
  254. }
  255. template<class U1, class U2>
  256. struct rebind {
  257. typedef scalar_divides_assign<U1, U2> other;
  258. };
  259. };
  260. template<class T1, class T2>
  261. const bool scalar_divides_assign<T1,T2>::computed = true;
  262. template<class T1, class T2>
  263. struct scalar_binary_swap_functor {
  264. typedef typename type_traits<typename boost::remove_reference<T1>::type>::reference argument1_type;
  265. typedef typename type_traits<typename boost::remove_reference<T2>::type>::reference argument2_type;
  266. };
  267. template<class T1, class T2>
  268. struct scalar_swap:
  269. public scalar_binary_swap_functor<T1, T2> {
  270. typedef typename scalar_binary_swap_functor<T1, T2>::argument1_type argument1_type;
  271. typedef typename scalar_binary_swap_functor<T1, T2>::argument2_type argument2_type;
  272. static BOOST_UBLAS_INLINE
  273. void apply (argument1_type t1, argument2_type t2) {
  274. std::swap (t1, t2);
  275. }
  276. template<class U1, class U2>
  277. struct rebind {
  278. typedef scalar_swap<U1, U2> other;
  279. };
  280. };
  281. // Vector functors
  282. // Unary returning scalar
  283. template<class V>
  284. struct vector_scalar_unary_functor {
  285. typedef typename V::value_type value_type;
  286. typedef typename V::value_type result_type;
  287. };
  288. template<class V>
  289. struct vector_sum:
  290. public vector_scalar_unary_functor<V> {
  291. typedef typename vector_scalar_unary_functor<V>::value_type value_type;
  292. typedef typename vector_scalar_unary_functor<V>::result_type result_type;
  293. template<class E>
  294. static BOOST_UBLAS_INLINE
  295. result_type apply (const vector_expression<E> &e) {
  296. result_type t = result_type (0);
  297. typedef typename E::size_type vector_size_type;
  298. vector_size_type size (e ().size ());
  299. for (vector_size_type i = 0; i < size; ++ i)
  300. t += e () (i);
  301. return t;
  302. }
  303. // Dense case
  304. template<class D, class I>
  305. static BOOST_UBLAS_INLINE
  306. result_type apply (D size, I it) {
  307. result_type t = result_type (0);
  308. while (-- size >= 0)
  309. t += *it, ++ it;
  310. return t;
  311. }
  312. // Sparse case
  313. template<class I>
  314. static BOOST_UBLAS_INLINE
  315. result_type apply (I it, const I &it_end) {
  316. result_type t = result_type (0);
  317. while (it != it_end)
  318. t += *it, ++ it;
  319. return t;
  320. }
  321. };
  322. // Unary returning real scalar
  323. template<class V>
  324. struct vector_scalar_real_unary_functor {
  325. typedef typename V::value_type value_type;
  326. typedef typename type_traits<value_type>::real_type real_type;
  327. typedef real_type result_type;
  328. };
  329. template<class V>
  330. struct vector_norm_1:
  331. public vector_scalar_real_unary_functor<V> {
  332. typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
  333. typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
  334. typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
  335. template<class E>
  336. static BOOST_UBLAS_INLINE
  337. result_type apply (const vector_expression<E> &e) {
  338. real_type t = real_type ();
  339. typedef typename E::size_type vector_size_type;
  340. vector_size_type size (e ().size ());
  341. for (vector_size_type i = 0; i < size; ++ i) {
  342. real_type u (type_traits<value_type>::type_abs (e () (i)));
  343. t += u;
  344. }
  345. return t;
  346. }
  347. // Dense case
  348. template<class D, class I>
  349. static BOOST_UBLAS_INLINE
  350. result_type apply (D size, I it) {
  351. real_type t = real_type ();
  352. while (-- size >= 0) {
  353. real_type u (type_traits<value_type>::norm_1 (*it));
  354. t += u;
  355. ++ it;
  356. }
  357. return t;
  358. }
  359. // Sparse case
  360. template<class I>
  361. static BOOST_UBLAS_INLINE
  362. result_type apply (I it, const I &it_end) {
  363. real_type t = real_type ();
  364. while (it != it_end) {
  365. real_type u (type_traits<value_type>::norm_1 (*it));
  366. t += u;
  367. ++ it;
  368. }
  369. return t;
  370. }
  371. };
  372. template<class V>
  373. struct vector_norm_2:
  374. public vector_scalar_real_unary_functor<V> {
  375. typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
  376. typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
  377. typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
  378. template<class E>
  379. static BOOST_UBLAS_INLINE
  380. result_type apply (const vector_expression<E> &e) {
  381. typedef typename E::size_type vector_size_type;
  382. vector_size_type size (e ().size ());
  383. #ifndef BOOST_UBLAS_SCALED_NORM
  384. real_type t = real_type ();
  385. for (vector_size_type i = 0; i < size; ++ i) {
  386. real_type u (type_traits<value_type>::norm_2 (e () (i)));
  387. t += u * u;
  388. }
  389. return static_cast<result_type>(type_traits<real_type>::type_sqrt (t));
  390. #else
  391. real_type scale = real_type ();
  392. real_type sum_squares (1);
  393. for (vector_size_type i = 0; i < size; ++ i) {
  394. real_type u (type_traits<value_type>::norm_2 (e () (i)));
  395. if ( real_type () /* zero */ == u ) continue;
  396. if (scale < u) {
  397. real_type v (scale / u);
  398. sum_squares = sum_squares * v * v + real_type (1);
  399. scale = u;
  400. } else {
  401. real_type v (u / scale);
  402. sum_squares += v * v;
  403. }
  404. }
  405. return static_cast<result_type>(scale * type_traits<real_type>::type_sqrt (sum_squares));
  406. #endif
  407. }
  408. // Dense case
  409. template<class D, class I>
  410. static BOOST_UBLAS_INLINE
  411. result_type apply (D size, I it) {
  412. #ifndef BOOST_UBLAS_SCALED_NORM
  413. real_type t = real_type ();
  414. while (-- size >= 0) {
  415. real_type u (type_traits<value_type>::norm_2 (*it));
  416. t += u * u;
  417. ++ it;
  418. }
  419. return static_cast<result_type>(type_traits<real_type>::type_sqrt (t));
  420. #else
  421. real_type scale = real_type ();
  422. real_type sum_squares (1);
  423. while (-- size >= 0) {
  424. real_type u (type_traits<value_type>::norm_2 (*it));
  425. if (scale < u) {
  426. real_type v (scale / u);
  427. sum_squares = sum_squares * v * v + real_type (1);
  428. scale = u;
  429. } else {
  430. real_type v (u / scale);
  431. sum_squares += v * v;
  432. }
  433. ++ it;
  434. }
  435. return static_cast<result_type>(scale * type_traits<real_type>::type_sqrt (sum_squares));
  436. #endif
  437. }
  438. // Sparse case
  439. template<class I>
  440. static BOOST_UBLAS_INLINE
  441. result_type apply (I it, const I &it_end) {
  442. #ifndef BOOST_UBLAS_SCALED_NORM
  443. real_type t = real_type ();
  444. while (it != it_end) {
  445. real_type u (type_traits<value_type>::norm_2 (*it));
  446. t += u * u;
  447. ++ it;
  448. }
  449. return static_cast<result_type>(type_traits<real_type>::type_sqrt (t));
  450. #else
  451. real_type scale = real_type ();
  452. real_type sum_squares (1);
  453. while (it != it_end) {
  454. real_type u (type_traits<value_type>::norm_2 (*it));
  455. if (scale < u) {
  456. real_type v (scale / u);
  457. sum_squares = sum_squares * v * v + real_type (1);
  458. scale = u;
  459. } else {
  460. real_type v (u / scale);
  461. sum_squares += v * v;
  462. }
  463. ++ it;
  464. }
  465. return static_cast<result_type>(scale * type_traits<real_type>::type_sqrt (sum_squares));
  466. #endif
  467. }
  468. };
  469. template<class V>
  470. struct vector_norm_2_square :
  471. public vector_scalar_real_unary_functor<V> {
  472. typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
  473. typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
  474. typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
  475. template<class E>
  476. static BOOST_UBLAS_INLINE
  477. result_type apply (const vector_expression<E> &e) {
  478. real_type t = real_type ();
  479. typedef typename E::size_type vector_size_type;
  480. vector_size_type size (e ().size ());
  481. for (vector_size_type i = 0; i < size; ++ i) {
  482. real_type u (type_traits<value_type>::norm_2 (e () (i)));
  483. t += u * u;
  484. }
  485. return t;
  486. }
  487. // Dense case
  488. template<class D, class I>
  489. static BOOST_UBLAS_INLINE
  490. result_type apply (D size, I it) {
  491. real_type t = real_type ();
  492. while (-- size >= 0) {
  493. real_type u (type_traits<value_type>::norm_2 (*it));
  494. t += u * u;
  495. ++ it;
  496. }
  497. return t;
  498. }
  499. // Sparse case
  500. template<class I>
  501. static BOOST_UBLAS_INLINE
  502. result_type apply (I it, const I &it_end) {
  503. real_type t = real_type ();
  504. while (it != it_end) {
  505. real_type u (type_traits<value_type>::norm_2 (*it));
  506. t += u * u;
  507. ++ it;
  508. }
  509. return t;
  510. }
  511. };
  512. template<class V>
  513. struct vector_norm_inf:
  514. public vector_scalar_real_unary_functor<V> {
  515. typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
  516. typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
  517. typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
  518. template<class E>
  519. static BOOST_UBLAS_INLINE
  520. result_type apply (const vector_expression<E> &e) {
  521. real_type t = real_type ();
  522. typedef typename E::size_type vector_size_type;
  523. vector_size_type size (e ().size ());
  524. for (vector_size_type i = 0; i < size; ++ i) {
  525. real_type u (type_traits<value_type>::norm_inf (e () (i)));
  526. if (u > t)
  527. t = u;
  528. }
  529. return t;
  530. }
  531. // Dense case
  532. template<class D, class I>
  533. static BOOST_UBLAS_INLINE
  534. result_type apply (D size, I it) {
  535. real_type t = real_type ();
  536. while (-- size >= 0) {
  537. real_type u (type_traits<value_type>::norm_inf (*it));
  538. if (u > t)
  539. t = u;
  540. ++ it;
  541. }
  542. return t;
  543. }
  544. // Sparse case
  545. template<class I>
  546. static BOOST_UBLAS_INLINE
  547. result_type apply (I it, const I &it_end) {
  548. real_type t = real_type ();
  549. while (it != it_end) {
  550. real_type u (type_traits<value_type>::norm_inf (*it));
  551. if (u > t)
  552. t = u;
  553. ++ it;
  554. }
  555. return t;
  556. }
  557. };
  558. // Unary returning index
  559. template<class V>
  560. struct vector_scalar_index_unary_functor {
  561. typedef typename V::value_type value_type;
  562. typedef typename type_traits<value_type>::real_type real_type;
  563. typedef typename V::size_type result_type;
  564. };
  565. template<class V>
  566. struct vector_index_norm_inf:
  567. public vector_scalar_index_unary_functor<V> {
  568. typedef typename vector_scalar_index_unary_functor<V>::value_type value_type;
  569. typedef typename vector_scalar_index_unary_functor<V>::real_type real_type;
  570. typedef typename vector_scalar_index_unary_functor<V>::result_type result_type;
  571. template<class E>
  572. static BOOST_UBLAS_INLINE
  573. result_type apply (const vector_expression<E> &e) {
  574. // ISSUE For CBLAS compatibility return 0 index in empty case
  575. result_type i_norm_inf (0);
  576. real_type t = real_type ();
  577. typedef typename E::size_type vector_size_type;
  578. vector_size_type size (e ().size ());
  579. for (vector_size_type i = 0; i < size; ++ i) {
  580. real_type u (type_traits<value_type>::norm_inf (e () (i)));
  581. if (u > t) {
  582. i_norm_inf = i;
  583. t = u;
  584. }
  585. }
  586. return i_norm_inf;
  587. }
  588. // Dense case
  589. template<class D, class I>
  590. static BOOST_UBLAS_INLINE
  591. result_type apply (D size, I it) {
  592. // ISSUE For CBLAS compatibility return 0 index in empty case
  593. result_type i_norm_inf (0);
  594. real_type t = real_type ();
  595. while (-- size >= 0) {
  596. real_type u (type_traits<value_type>::norm_inf (*it));
  597. if (u > t) {
  598. i_norm_inf = it.index ();
  599. t = u;
  600. }
  601. ++ it;
  602. }
  603. return i_norm_inf;
  604. }
  605. // Sparse case
  606. template<class I>
  607. static BOOST_UBLAS_INLINE
  608. result_type apply (I it, const I &it_end) {
  609. // ISSUE For CBLAS compatibility return 0 index in empty case
  610. result_type i_norm_inf (0);
  611. real_type t = real_type ();
  612. while (it != it_end) {
  613. real_type u (type_traits<value_type>::norm_inf (*it));
  614. if (u > t) {
  615. i_norm_inf = it.index ();
  616. t = u;
  617. }
  618. ++ it;
  619. }
  620. return i_norm_inf;
  621. }
  622. };
  623. // Binary returning scalar
  624. template<class V1, class V2, class TV>
  625. struct vector_scalar_binary_functor {
  626. typedef TV value_type;
  627. typedef TV result_type;
  628. };
  629. template<class V1, class V2, class TV>
  630. struct vector_inner_prod:
  631. public vector_scalar_binary_functor<V1, V2, TV> {
  632. typedef typename vector_scalar_binary_functor<V1, V2, TV>::value_type value_type;
  633. typedef typename vector_scalar_binary_functor<V1, V2, TV>::result_type result_type;
  634. template<class C1, class C2>
  635. static BOOST_UBLAS_INLINE
  636. result_type apply (const vector_container<C1> &c1,
  637. const vector_container<C2> &c2) {
  638. #ifdef BOOST_UBLAS_USE_SIMD
  639. using namespace raw;
  640. typedef typename C1::size_type vector_size_type;
  641. vector_size_type size (BOOST_UBLAS_SAME (c1 ().size (), c2 ().size ()));
  642. const typename V1::value_type *data1 = data_const (c1 ());
  643. const typename V1::value_type *data2 = data_const (c2 ());
  644. vector_size_type s1 = stride (c1 ());
  645. vector_size_type s2 = stride (c2 ());
  646. result_type t = result_type (0);
  647. if (s1 == 1 && s2 == 1) {
  648. for (vector_size_type i = 0; i < size; ++ i)
  649. t += data1 [i] * data2 [i];
  650. } else if (s2 == 1) {
  651. for (vector_size_type i = 0, i1 = 0; i < size; ++ i, i1 += s1)
  652. t += data1 [i1] * data2 [i];
  653. } else if (s1 == 1) {
  654. for (vector_size_type i = 0, i2 = 0; i < size; ++ i, i2 += s2)
  655. t += data1 [i] * data2 [i2];
  656. } else {
  657. for (vector_size_type i = 0, i1 = 0, i2 = 0; i < size; ++ i, i1 += s1, i2 += s2)
  658. t += data1 [i1] * data2 [i2];
  659. }
  660. return t;
  661. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  662. return boost::numeric::bindings::atlas::dot (c1 (), c2 ());
  663. #else
  664. return apply (static_cast<const vector_expression<C1> > (c1), static_cast<const vector_expression<C2> > (c2));
  665. #endif
  666. }
  667. template<class E1, class E2>
  668. static BOOST_UBLAS_INLINE
  669. result_type apply (const vector_expression<E1> &e1,
  670. const vector_expression<E2> &e2) {
  671. typedef typename E1::size_type vector_size_type;
  672. vector_size_type size (BOOST_UBLAS_SAME (e1 ().size (), e2 ().size ()));
  673. result_type t = result_type (0);
  674. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  675. for (vector_size_type i = 0; i < size; ++ i)
  676. t += e1 () (i) * e2 () (i);
  677. #else
  678. vector_size_type i (0);
  679. DD (size, 4, r, (t += e1 () (i) * e2 () (i), ++ i));
  680. #endif
  681. return t;
  682. }
  683. // Dense case
  684. template<class D, class I1, class I2>
  685. static BOOST_UBLAS_INLINE
  686. result_type apply (D size, I1 it1, I2 it2) {
  687. result_type t = result_type (0);
  688. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  689. while (-- size >= 0)
  690. t += *it1 * *it2, ++ it1, ++ it2;
  691. #else
  692. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  693. #endif
  694. return t;
  695. }
  696. // Packed case
  697. template<class I1, class I2>
  698. static BOOST_UBLAS_INLINE
  699. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
  700. result_type t = result_type (0);
  701. typedef typename I1::difference_type vector_difference_type;
  702. vector_difference_type it1_size (it1_end - it1);
  703. vector_difference_type it2_size (it2_end - it2);
  704. vector_difference_type diff (0);
  705. if (it1_size > 0 && it2_size > 0)
  706. diff = it2.index () - it1.index ();
  707. if (diff != 0) {
  708. vector_difference_type size = (std::min) (diff, it1_size);
  709. if (size > 0) {
  710. it1 += size;
  711. it1_size -= size;
  712. diff -= size;
  713. }
  714. size = (std::min) (- diff, it2_size);
  715. if (size > 0) {
  716. it2 += size;
  717. it2_size -= size;
  718. diff += size;
  719. }
  720. }
  721. vector_difference_type size ((std::min) (it1_size, it2_size));
  722. while (-- size >= 0)
  723. t += *it1 * *it2, ++ it1, ++ it2;
  724. return t;
  725. }
  726. // Sparse case
  727. template<class I1, class I2>
  728. static BOOST_UBLAS_INLINE
  729. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, sparse_bidirectional_iterator_tag) {
  730. result_type t = result_type (0);
  731. if (it1 != it1_end && it2 != it2_end) {
  732. for (;;) {
  733. if (it1.index () == it2.index ()) {
  734. t += *it1 * *it2, ++ it1, ++ it2;
  735. if (it1 == it1_end || it2 == it2_end)
  736. break;
  737. } else if (it1.index () < it2.index ()) {
  738. increment (it1, it1_end, it2.index () - it1.index ());
  739. if (it1 == it1_end)
  740. break;
  741. } else if (it1.index () > it2.index ()) {
  742. increment (it2, it2_end, it1.index () - it2.index ());
  743. if (it2 == it2_end)
  744. break;
  745. }
  746. }
  747. }
  748. return t;
  749. }
  750. };
  751. // Matrix functors
  752. // Binary returning vector
  753. template<class M1, class M2, class TV>
  754. struct matrix_vector_binary_functor {
  755. typedef typename M1::size_type size_type;
  756. typedef typename M1::difference_type difference_type;
  757. typedef TV value_type;
  758. typedef TV result_type;
  759. };
  760. template<class M1, class M2, class TV>
  761. struct matrix_vector_prod1:
  762. public matrix_vector_binary_functor<M1, M2, TV> {
  763. typedef typename matrix_vector_binary_functor<M1, M2, TV>::size_type size_type;
  764. typedef typename matrix_vector_binary_functor<M1, M2, TV>::difference_type difference_type;
  765. typedef typename matrix_vector_binary_functor<M1, M2, TV>::value_type value_type;
  766. typedef typename matrix_vector_binary_functor<M1, M2, TV>::result_type result_type;
  767. template<class C1, class C2>
  768. static BOOST_UBLAS_INLINE
  769. result_type apply (const matrix_container<C1> &c1,
  770. const vector_container<C2> &c2,
  771. size_type i) {
  772. #ifdef BOOST_UBLAS_USE_SIMD
  773. using namespace raw;
  774. size_type size = BOOST_UBLAS_SAME (c1 ().size2 (), c2 ().size ());
  775. const typename M1::value_type *data1 = data_const (c1 ()) + i * stride1 (c1 ());
  776. const typename M2::value_type *data2 = data_const (c2 ());
  777. size_type s1 = stride2 (c1 ());
  778. size_type s2 = stride (c2 ());
  779. result_type t = result_type (0);
  780. if (s1 == 1 && s2 == 1) {
  781. for (size_type j = 0; j < size; ++ j)
  782. t += data1 [j] * data2 [j];
  783. } else if (s2 == 1) {
  784. for (size_type j = 0, j1 = 0; j < size; ++ j, j1 += s1)
  785. t += data1 [j1] * data2 [j];
  786. } else if (s1 == 1) {
  787. for (size_type j = 0, j2 = 0; j < size; ++ j, j2 += s2)
  788. t += data1 [j] * data2 [j2];
  789. } else {
  790. for (size_type j = 0, j1 = 0, j2 = 0; j < size; ++ j, j1 += s1, j2 += s2)
  791. t += data1 [j1] * data2 [j2];
  792. }
  793. return t;
  794. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  795. return boost::numeric::bindings::atlas::dot (c1 ().row (i), c2 ());
  796. #else
  797. return apply (static_cast<const matrix_expression<C1> > (c1), static_cast<const vector_expression<C2> > (c2, i));
  798. #endif
  799. }
  800. template<class E1, class E2>
  801. static BOOST_UBLAS_INLINE
  802. result_type apply (const matrix_expression<E1> &e1,
  803. const vector_expression<E2> &e2,
  804. size_type i) {
  805. size_type size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size ());
  806. result_type t = result_type (0);
  807. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  808. for (size_type j = 0; j < size; ++ j)
  809. t += e1 () (i, j) * e2 () (j);
  810. #else
  811. size_type j (0);
  812. DD (size, 4, r, (t += e1 () (i, j) * e2 () (j), ++ j));
  813. #endif
  814. return t;
  815. }
  816. // Dense case
  817. template<class I1, class I2>
  818. static BOOST_UBLAS_INLINE
  819. result_type apply (difference_type size, I1 it1, I2 it2) {
  820. result_type t = result_type (0);
  821. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  822. while (-- size >= 0)
  823. t += *it1 * *it2, ++ it1, ++ it2;
  824. #else
  825. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  826. #endif
  827. return t;
  828. }
  829. // Packed case
  830. template<class I1, class I2>
  831. static BOOST_UBLAS_INLINE
  832. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
  833. result_type t = result_type (0);
  834. difference_type it1_size (it1_end - it1);
  835. difference_type it2_size (it2_end - it2);
  836. difference_type diff (0);
  837. if (it1_size > 0 && it2_size > 0)
  838. diff = it2.index () - it1.index2 ();
  839. if (diff != 0) {
  840. difference_type size = (std::min) (diff, it1_size);
  841. if (size > 0) {
  842. it1 += size;
  843. it1_size -= size;
  844. diff -= size;
  845. }
  846. size = (std::min) (- diff, it2_size);
  847. if (size > 0) {
  848. it2 += size;
  849. it2_size -= size;
  850. diff += size;
  851. }
  852. }
  853. difference_type size ((std::min) (it1_size, it2_size));
  854. while (-- size >= 0)
  855. t += *it1 * *it2, ++ it1, ++ it2;
  856. return t;
  857. }
  858. // Sparse case
  859. template<class I1, class I2>
  860. static BOOST_UBLAS_INLINE
  861. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  862. sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag) {
  863. result_type t = result_type (0);
  864. if (it1 != it1_end && it2 != it2_end) {
  865. size_type it1_index = it1.index2 (), it2_index = it2.index ();
  866. for (;;) {
  867. difference_type compare = it1_index - it2_index;
  868. if (compare == 0) {
  869. t += *it1 * *it2, ++ it1, ++ it2;
  870. if (it1 != it1_end && it2 != it2_end) {
  871. it1_index = it1.index2 ();
  872. it2_index = it2.index ();
  873. } else
  874. break;
  875. } else if (compare < 0) {
  876. increment (it1, it1_end, - compare);
  877. if (it1 != it1_end)
  878. it1_index = it1.index2 ();
  879. else
  880. break;
  881. } else if (compare > 0) {
  882. increment (it2, it2_end, compare);
  883. if (it2 != it2_end)
  884. it2_index = it2.index ();
  885. else
  886. break;
  887. }
  888. }
  889. }
  890. return t;
  891. }
  892. // Sparse packed case
  893. template<class I1, class I2>
  894. static BOOST_UBLAS_INLINE
  895. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &/* it2_end */,
  896. sparse_bidirectional_iterator_tag, packed_random_access_iterator_tag) {
  897. result_type t = result_type (0);
  898. while (it1 != it1_end) {
  899. t += *it1 * it2 () (it1.index2 ());
  900. ++ it1;
  901. }
  902. return t;
  903. }
  904. // Packed sparse case
  905. template<class I1, class I2>
  906. static BOOST_UBLAS_INLINE
  907. result_type apply (I1 it1, const I1 &/* it1_end */, I2 it2, const I2 &it2_end,
  908. packed_random_access_iterator_tag, sparse_bidirectional_iterator_tag) {
  909. result_type t = result_type (0);
  910. while (it2 != it2_end) {
  911. t += it1 () (it1.index1 (), it2.index ()) * *it2;
  912. ++ it2;
  913. }
  914. return t;
  915. }
  916. // Another dispatcher
  917. template<class I1, class I2>
  918. static BOOST_UBLAS_INLINE
  919. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  920. sparse_bidirectional_iterator_tag) {
  921. typedef typename I1::iterator_category iterator1_category;
  922. typedef typename I2::iterator_category iterator2_category;
  923. return apply (it1, it1_end, it2, it2_end, iterator1_category (), iterator2_category ());
  924. }
  925. };
  926. template<class M1, class M2, class TV>
  927. struct matrix_vector_prod2:
  928. public matrix_vector_binary_functor<M1, M2, TV> {
  929. typedef typename matrix_vector_binary_functor<M1, M2, TV>::size_type size_type;
  930. typedef typename matrix_vector_binary_functor<M1, M2, TV>::difference_type difference_type;
  931. typedef typename matrix_vector_binary_functor<M1, M2, TV>::value_type value_type;
  932. typedef typename matrix_vector_binary_functor<M1, M2, TV>::result_type result_type;
  933. template<class C1, class C2>
  934. static BOOST_UBLAS_INLINE
  935. result_type apply (const vector_container<C1> &c1,
  936. const matrix_container<C2> &c2,
  937. size_type i) {
  938. #ifdef BOOST_UBLAS_USE_SIMD
  939. using namespace raw;
  940. size_type size = BOOST_UBLAS_SAME (c1 ().size (), c2 ().size1 ());
  941. const typename M1::value_type *data1 = data_const (c1 ());
  942. const typename M2::value_type *data2 = data_const (c2 ()) + i * stride2 (c2 ());
  943. size_type s1 = stride (c1 ());
  944. size_type s2 = stride1 (c2 ());
  945. result_type t = result_type (0);
  946. if (s1 == 1 && s2 == 1) {
  947. for (size_type j = 0; j < size; ++ j)
  948. t += data1 [j] * data2 [j];
  949. } else if (s2 == 1) {
  950. for (size_type j = 0, j1 = 0; j < size; ++ j, j1 += s1)
  951. t += data1 [j1] * data2 [j];
  952. } else if (s1 == 1) {
  953. for (size_type j = 0, j2 = 0; j < size; ++ j, j2 += s2)
  954. t += data1 [j] * data2 [j2];
  955. } else {
  956. for (size_type j = 0, j1 = 0, j2 = 0; j < size; ++ j, j1 += s1, j2 += s2)
  957. t += data1 [j1] * data2 [j2];
  958. }
  959. return t;
  960. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  961. return boost::numeric::bindings::atlas::dot (c1 (), c2 ().column (i));
  962. #else
  963. return apply (static_cast<const vector_expression<C1> > (c1), static_cast<const matrix_expression<C2> > (c2, i));
  964. #endif
  965. }
  966. template<class E1, class E2>
  967. static BOOST_UBLAS_INLINE
  968. result_type apply (const vector_expression<E1> &e1,
  969. const matrix_expression<E2> &e2,
  970. size_type i) {
  971. size_type size = BOOST_UBLAS_SAME (e1 ().size (), e2 ().size1 ());
  972. result_type t = result_type (0);
  973. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  974. for (size_type j = 0; j < size; ++ j)
  975. t += e1 () (j) * e2 () (j, i);
  976. #else
  977. size_type j (0);
  978. DD (size, 4, r, (t += e1 () (j) * e2 () (j, i), ++ j));
  979. #endif
  980. return t;
  981. }
  982. // Dense case
  983. template<class I1, class I2>
  984. static BOOST_UBLAS_INLINE
  985. result_type apply (difference_type size, I1 it1, I2 it2) {
  986. result_type t = result_type (0);
  987. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  988. while (-- size >= 0)
  989. t += *it1 * *it2, ++ it1, ++ it2;
  990. #else
  991. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  992. #endif
  993. return t;
  994. }
  995. // Packed case
  996. template<class I1, class I2>
  997. static BOOST_UBLAS_INLINE
  998. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
  999. result_type t = result_type (0);
  1000. difference_type it1_size (it1_end - it1);
  1001. difference_type it2_size (it2_end - it2);
  1002. difference_type diff (0);
  1003. if (it1_size > 0 && it2_size > 0)
  1004. diff = it2.index1 () - it1.index ();
  1005. if (diff != 0) {
  1006. difference_type size = (std::min) (diff, it1_size);
  1007. if (size > 0) {
  1008. it1 += size;
  1009. it1_size -= size;
  1010. diff -= size;
  1011. }
  1012. size = (std::min) (- diff, it2_size);
  1013. if (size > 0) {
  1014. it2 += size;
  1015. it2_size -= size;
  1016. diff += size;
  1017. }
  1018. }
  1019. difference_type size ((std::min) (it1_size, it2_size));
  1020. while (-- size >= 0)
  1021. t += *it1 * *it2, ++ it1, ++ it2;
  1022. return t;
  1023. }
  1024. // Sparse case
  1025. template<class I1, class I2>
  1026. static BOOST_UBLAS_INLINE
  1027. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  1028. sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag) {
  1029. result_type t = result_type (0);
  1030. if (it1 != it1_end && it2 != it2_end) {
  1031. size_type it1_index = it1.index (), it2_index = it2.index1 ();
  1032. for (;;) {
  1033. difference_type compare = it1_index - it2_index;
  1034. if (compare == 0) {
  1035. t += *it1 * *it2, ++ it1, ++ it2;
  1036. if (it1 != it1_end && it2 != it2_end) {
  1037. it1_index = it1.index ();
  1038. it2_index = it2.index1 ();
  1039. } else
  1040. break;
  1041. } else if (compare < 0) {
  1042. increment (it1, it1_end, - compare);
  1043. if (it1 != it1_end)
  1044. it1_index = it1.index ();
  1045. else
  1046. break;
  1047. } else if (compare > 0) {
  1048. increment (it2, it2_end, compare);
  1049. if (it2 != it2_end)
  1050. it2_index = it2.index1 ();
  1051. else
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. return t;
  1057. }
  1058. // Packed sparse case
  1059. template<class I1, class I2>
  1060. static BOOST_UBLAS_INLINE
  1061. result_type apply (I1 it1, const I1 &/* it1_end */, I2 it2, const I2 &it2_end,
  1062. packed_random_access_iterator_tag, sparse_bidirectional_iterator_tag) {
  1063. result_type t = result_type (0);
  1064. while (it2 != it2_end) {
  1065. t += it1 () (it2.index1 ()) * *it2;
  1066. ++ it2;
  1067. }
  1068. return t;
  1069. }
  1070. // Sparse packed case
  1071. template<class I1, class I2>
  1072. static BOOST_UBLAS_INLINE
  1073. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &/* it2_end */,
  1074. sparse_bidirectional_iterator_tag, packed_random_access_iterator_tag) {
  1075. result_type t = result_type (0);
  1076. while (it1 != it1_end) {
  1077. t += *it1 * it2 () (it1.index (), it2.index2 ());
  1078. ++ it1;
  1079. }
  1080. return t;
  1081. }
  1082. // Another dispatcher
  1083. template<class I1, class I2>
  1084. static BOOST_UBLAS_INLINE
  1085. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  1086. sparse_bidirectional_iterator_tag) {
  1087. typedef typename I1::iterator_category iterator1_category;
  1088. typedef typename I2::iterator_category iterator2_category;
  1089. return apply (it1, it1_end, it2, it2_end, iterator1_category (), iterator2_category ());
  1090. }
  1091. };
  1092. // Binary returning matrix
  1093. template<class M1, class M2, class TV>
  1094. struct matrix_matrix_binary_functor {
  1095. typedef typename M1::size_type size_type;
  1096. typedef typename M1::difference_type difference_type;
  1097. typedef TV value_type;
  1098. typedef TV result_type;
  1099. };
  1100. template<class M1, class M2, class TV>
  1101. struct matrix_matrix_prod:
  1102. public matrix_matrix_binary_functor<M1, M2, TV> {
  1103. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::size_type size_type;
  1104. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::difference_type difference_type;
  1105. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::value_type value_type;
  1106. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::result_type result_type;
  1107. template<class C1, class C2>
  1108. static BOOST_UBLAS_INLINE
  1109. result_type apply (const matrix_container<C1> &c1,
  1110. const matrix_container<C2> &c2,
  1111. size_type i, size_type j) {
  1112. #ifdef BOOST_UBLAS_USE_SIMD
  1113. using namespace raw;
  1114. size_type size = BOOST_UBLAS_SAME (c1 ().size2 (), c2 ().sizc1 ());
  1115. const typename M1::value_type *data1 = data_const (c1 ()) + i * stride1 (c1 ());
  1116. const typename M2::value_type *data2 = data_const (c2 ()) + j * stride2 (c2 ());
  1117. size_type s1 = stride2 (c1 ());
  1118. size_type s2 = stride1 (c2 ());
  1119. result_type t = result_type (0);
  1120. if (s1 == 1 && s2 == 1) {
  1121. for (size_type k = 0; k < size; ++ k)
  1122. t += data1 [k] * data2 [k];
  1123. } else if (s2 == 1) {
  1124. for (size_type k = 0, k1 = 0; k < size; ++ k, k1 += s1)
  1125. t += data1 [k1] * data2 [k];
  1126. } else if (s1 == 1) {
  1127. for (size_type k = 0, k2 = 0; k < size; ++ k, k2 += s2)
  1128. t += data1 [k] * data2 [k2];
  1129. } else {
  1130. for (size_type k = 0, k1 = 0, k2 = 0; k < size; ++ k, k1 += s1, k2 += s2)
  1131. t += data1 [k1] * data2 [k2];
  1132. }
  1133. return t;
  1134. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  1135. return boost::numeric::bindings::atlas::dot (c1 ().row (i), c2 ().column (j));
  1136. #else
  1137. boost::ignore_unused(j);
  1138. return apply (static_cast<const matrix_expression<C1> > (c1), static_cast<const matrix_expression<C2> > (c2, i));
  1139. #endif
  1140. }
  1141. template<class E1, class E2>
  1142. static BOOST_UBLAS_INLINE
  1143. result_type apply (const matrix_expression<E1> &e1,
  1144. const matrix_expression<E2> &e2,
  1145. size_type i, size_type j) {
  1146. size_type size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size1 ());
  1147. result_type t = result_type (0);
  1148. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  1149. for (size_type k = 0; k < size; ++ k)
  1150. t += e1 () (i, k) * e2 () (k, j);
  1151. #else
  1152. size_type k (0);
  1153. DD (size, 4, r, (t += e1 () (i, k) * e2 () (k, j), ++ k));
  1154. #endif
  1155. return t;
  1156. }
  1157. // Dense case
  1158. template<class I1, class I2>
  1159. static BOOST_UBLAS_INLINE
  1160. result_type apply (difference_type size, I1 it1, I2 it2) {
  1161. result_type t = result_type (0);
  1162. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  1163. while (-- size >= 0)
  1164. t += *it1 * *it2, ++ it1, ++ it2;
  1165. #else
  1166. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  1167. #endif
  1168. return t;
  1169. }
  1170. // Packed case
  1171. template<class I1, class I2>
  1172. static BOOST_UBLAS_INLINE
  1173. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, packed_random_access_iterator_tag) {
  1174. result_type t = result_type (0);
  1175. difference_type it1_size (it1_end - it1);
  1176. difference_type it2_size (it2_end - it2);
  1177. difference_type diff (0);
  1178. if (it1_size > 0 && it2_size > 0)
  1179. diff = it2.index1 () - it1.index2 ();
  1180. if (diff != 0) {
  1181. difference_type size = (std::min) (diff, it1_size);
  1182. if (size > 0) {
  1183. it1 += size;
  1184. it1_size -= size;
  1185. diff -= size;
  1186. }
  1187. size = (std::min) (- diff, it2_size);
  1188. if (size > 0) {
  1189. it2 += size;
  1190. it2_size -= size;
  1191. diff += size;
  1192. }
  1193. }
  1194. difference_type size ((std::min) (it1_size, it2_size));
  1195. while (-- size >= 0)
  1196. t += *it1 * *it2, ++ it1, ++ it2;
  1197. return t;
  1198. }
  1199. // Sparse case
  1200. template<class I1, class I2>
  1201. static BOOST_UBLAS_INLINE
  1202. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, sparse_bidirectional_iterator_tag) {
  1203. result_type t = result_type (0);
  1204. if (it1 != it1_end && it2 != it2_end) {
  1205. size_type it1_index = it1.index2 (), it2_index = it2.index1 ();
  1206. for (;;) {
  1207. difference_type compare = difference_type (it1_index - it2_index);
  1208. if (compare == 0) {
  1209. t += *it1 * *it2, ++ it1, ++ it2;
  1210. if (it1 != it1_end && it2 != it2_end) {
  1211. it1_index = it1.index2 ();
  1212. it2_index = it2.index1 ();
  1213. } else
  1214. break;
  1215. } else if (compare < 0) {
  1216. increment (it1, it1_end, - compare);
  1217. if (it1 != it1_end)
  1218. it1_index = it1.index2 ();
  1219. else
  1220. break;
  1221. } else if (compare > 0) {
  1222. increment (it2, it2_end, compare);
  1223. if (it2 != it2_end)
  1224. it2_index = it2.index1 ();
  1225. else
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. return t;
  1231. }
  1232. };
  1233. // Unary returning scalar norm
  1234. template<class M>
  1235. struct matrix_scalar_real_unary_functor {
  1236. typedef typename M::value_type value_type;
  1237. typedef typename type_traits<value_type>::real_type real_type;
  1238. typedef real_type result_type;
  1239. };
  1240. template<class M>
  1241. struct matrix_norm_1:
  1242. public matrix_scalar_real_unary_functor<M> {
  1243. typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
  1244. typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
  1245. typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
  1246. template<class E>
  1247. static BOOST_UBLAS_INLINE
  1248. result_type apply (const matrix_expression<E> &e) {
  1249. real_type t = real_type ();
  1250. typedef typename E::size_type matrix_size_type;
  1251. matrix_size_type size2 (e ().size2 ());
  1252. for (matrix_size_type j = 0; j < size2; ++ j) {
  1253. real_type u = real_type ();
  1254. matrix_size_type size1 (e ().size1 ());
  1255. for (matrix_size_type i = 0; i < size1; ++ i) {
  1256. real_type v (type_traits<value_type>::norm_1 (e () (i, j)));
  1257. u += v;
  1258. }
  1259. if (u > t)
  1260. t = u;
  1261. }
  1262. return t;
  1263. }
  1264. };
  1265. template<class M>
  1266. struct matrix_norm_frobenius:
  1267. public matrix_scalar_real_unary_functor<M> {
  1268. typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
  1269. typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
  1270. typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
  1271. template<class E>
  1272. static BOOST_UBLAS_INLINE
  1273. result_type apply (const matrix_expression<E> &e) {
  1274. real_type t = real_type ();
  1275. typedef typename E::size_type matrix_size_type;
  1276. matrix_size_type size1 (e ().size1 ());
  1277. for (matrix_size_type i = 0; i < size1; ++ i) {
  1278. matrix_size_type size2 (e ().size2 ());
  1279. for (matrix_size_type j = 0; j < size2; ++ j) {
  1280. real_type u (type_traits<value_type>::norm_2 (e () (i, j)));
  1281. t += u * u;
  1282. }
  1283. }
  1284. return type_traits<real_type>::type_sqrt (t);
  1285. }
  1286. };
  1287. template<class M>
  1288. struct matrix_norm_inf:
  1289. public matrix_scalar_real_unary_functor<M> {
  1290. typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
  1291. typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
  1292. typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
  1293. template<class E>
  1294. static BOOST_UBLAS_INLINE
  1295. result_type apply (const matrix_expression<E> &e) {
  1296. real_type t = real_type ();
  1297. typedef typename E::size_type matrix_size_type;
  1298. matrix_size_type size1 (e ().size1 ());
  1299. for (matrix_size_type i = 0; i < size1; ++ i) {
  1300. real_type u = real_type ();
  1301. matrix_size_type size2 (e ().size2 ());
  1302. for (matrix_size_type j = 0; j < size2; ++ j) {
  1303. real_type v (type_traits<value_type>::norm_inf (e () (i, j)));
  1304. u += v;
  1305. }
  1306. if (u > t)
  1307. t = u;
  1308. }
  1309. return t;
  1310. }
  1311. };
  1312. // forward declaration
  1313. template <class Z, class D> struct basic_column_major;
  1314. // This functor defines storage layout and it's properties
  1315. // matrix (i,j) -> storage [i * size_i + j]
  1316. template <class Z, class D>
  1317. struct basic_row_major {
  1318. typedef Z size_type;
  1319. typedef D difference_type;
  1320. typedef row_major_tag orientation_category;
  1321. typedef basic_column_major<Z,D> transposed_layout;
  1322. static
  1323. BOOST_UBLAS_INLINE
  1324. size_type storage_size (size_type size_i, size_type size_j) {
  1325. // Guard against size_type overflow
  1326. BOOST_UBLAS_CHECK (size_j == 0 || size_i <= (std::numeric_limits<size_type>::max) () / size_j, bad_size ());
  1327. return size_i * size_j;
  1328. }
  1329. // Indexing conversion to storage element
  1330. static
  1331. BOOST_UBLAS_INLINE
  1332. size_type element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1333. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1334. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1335. detail::ignore_unused_variable_warning(size_i);
  1336. // Guard against size_type overflow
  1337. BOOST_UBLAS_CHECK (i <= ((std::numeric_limits<size_type>::max) () - j) / size_j, bad_index ());
  1338. return i * size_j + j;
  1339. }
  1340. static
  1341. BOOST_UBLAS_INLINE
  1342. size_type address (size_type i, size_type size_i, size_type j, size_type size_j) {
  1343. BOOST_UBLAS_CHECK (i <= size_i, bad_index ());
  1344. BOOST_UBLAS_CHECK (j <= size_j, bad_index ());
  1345. // Guard against size_type overflow - address may be size_j past end of storage
  1346. BOOST_UBLAS_CHECK (size_j == 0 || i <= ((std::numeric_limits<size_type>::max) () - j) / size_j, bad_index ());
  1347. detail::ignore_unused_variable_warning(size_i);
  1348. return i * size_j + j;
  1349. }
  1350. // Storage element to index conversion
  1351. static
  1352. BOOST_UBLAS_INLINE
  1353. difference_type distance_i (difference_type k, size_type /* size_i */, size_type size_j) {
  1354. return size_j != 0 ? k / size_j : 0;
  1355. }
  1356. static
  1357. BOOST_UBLAS_INLINE
  1358. difference_type distance_j (difference_type k, size_type /* size_i */, size_type /* size_j */) {
  1359. return k;
  1360. }
  1361. static
  1362. BOOST_UBLAS_INLINE
  1363. size_type index_i (difference_type k, size_type /* size_i */, size_type size_j) {
  1364. return size_j != 0 ? k / size_j : 0;
  1365. }
  1366. static
  1367. BOOST_UBLAS_INLINE
  1368. size_type index_j (difference_type k, size_type /* size_i */, size_type size_j) {
  1369. return size_j != 0 ? k % size_j : 0;
  1370. }
  1371. static
  1372. BOOST_UBLAS_INLINE
  1373. bool fast_i () {
  1374. return false;
  1375. }
  1376. static
  1377. BOOST_UBLAS_INLINE
  1378. bool fast_j () {
  1379. return true;
  1380. }
  1381. // Iterating storage elements
  1382. template<class I>
  1383. static
  1384. BOOST_UBLAS_INLINE
  1385. void increment_i (I &it, size_type /* size_i */, size_type size_j) {
  1386. it += size_j;
  1387. }
  1388. template<class I>
  1389. static
  1390. BOOST_UBLAS_INLINE
  1391. void increment_i (I &it, difference_type n, size_type /* size_i */, size_type size_j) {
  1392. it += n * size_j;
  1393. }
  1394. template<class I>
  1395. static
  1396. BOOST_UBLAS_INLINE
  1397. void decrement_i (I &it, size_type /* size_i */, size_type size_j) {
  1398. it -= size_j;
  1399. }
  1400. template<class I>
  1401. static
  1402. BOOST_UBLAS_INLINE
  1403. void decrement_i (I &it, difference_type n, size_type /* size_i */, size_type size_j) {
  1404. it -= n * size_j;
  1405. }
  1406. template<class I>
  1407. static
  1408. BOOST_UBLAS_INLINE
  1409. void increment_j (I &it, size_type /* size_i */, size_type /* size_j */) {
  1410. ++ it;
  1411. }
  1412. template<class I>
  1413. static
  1414. BOOST_UBLAS_INLINE
  1415. void increment_j (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1416. it += n;
  1417. }
  1418. template<class I>
  1419. static
  1420. BOOST_UBLAS_INLINE
  1421. void decrement_j (I &it, size_type /* size_i */, size_type /* size_j */) {
  1422. -- it;
  1423. }
  1424. template<class I>
  1425. static
  1426. BOOST_UBLAS_INLINE
  1427. void decrement_j (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1428. it -= n;
  1429. }
  1430. // Triangular access
  1431. static
  1432. BOOST_UBLAS_INLINE
  1433. size_type triangular_size (size_type size_i, size_type size_j) {
  1434. size_type size = (std::max) (size_i, size_j);
  1435. // Guard against size_type overflow - simplified
  1436. BOOST_UBLAS_CHECK (size == 0 || size / 2 < (std::numeric_limits<size_type>::max) () / size /* +1/2 */, bad_size ());
  1437. return ((size + 1) * size) / 2;
  1438. }
  1439. static
  1440. BOOST_UBLAS_INLINE
  1441. size_type lower_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1442. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1443. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1444. BOOST_UBLAS_CHECK (i >= j, bad_index ());
  1445. detail::ignore_unused_variable_warning(size_i);
  1446. detail::ignore_unused_variable_warning(size_j);
  1447. // FIXME size_type overflow
  1448. // sigma_i (i + 1) = (i + 1) * i / 2
  1449. // i = 0 1 2 3, sigma = 0 1 3 6
  1450. return ((i + 1) * i) / 2 + j;
  1451. }
  1452. static
  1453. BOOST_UBLAS_INLINE
  1454. size_type upper_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1455. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1456. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1457. BOOST_UBLAS_CHECK (i <= j, bad_index ());
  1458. // FIXME size_type overflow
  1459. // sigma_i (size - i) = size * i - i * (i - 1) / 2
  1460. // i = 0 1 2 3, sigma = 0 4 7 9
  1461. return (i * (2 * (std::max) (size_i, size_j) - i + 1)) / 2 + j - i;
  1462. }
  1463. // Major and minor indices
  1464. static
  1465. BOOST_UBLAS_INLINE
  1466. size_type index_M (size_type index1, size_type /* index2 */) {
  1467. return index1;
  1468. }
  1469. static
  1470. BOOST_UBLAS_INLINE
  1471. size_type index_m (size_type /* index1 */, size_type index2) {
  1472. return index2;
  1473. }
  1474. static
  1475. BOOST_UBLAS_INLINE
  1476. size_type size_M (size_type size_i, size_type /* size_j */) {
  1477. return size_i;
  1478. }
  1479. static
  1480. BOOST_UBLAS_INLINE
  1481. size_type size_m (size_type /* size_i */, size_type size_j) {
  1482. return size_j;
  1483. }
  1484. };
  1485. // This functor defines storage layout and it's properties
  1486. // matrix (i,j) -> storage [i + j * size_i]
  1487. template <class Z, class D>
  1488. struct basic_column_major {
  1489. typedef Z size_type;
  1490. typedef D difference_type;
  1491. typedef column_major_tag orientation_category;
  1492. typedef basic_row_major<Z,D> transposed_layout;
  1493. static
  1494. BOOST_UBLAS_INLINE
  1495. size_type storage_size (size_type size_i, size_type size_j) {
  1496. // Guard against size_type overflow
  1497. BOOST_UBLAS_CHECK (size_i == 0 || size_j <= (std::numeric_limits<size_type>::max) () / size_i, bad_size ());
  1498. return size_i * size_j;
  1499. }
  1500. // Indexing conversion to storage element
  1501. static
  1502. BOOST_UBLAS_INLINE
  1503. size_type element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1504. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1505. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1506. detail::ignore_unused_variable_warning(size_j);
  1507. // Guard against size_type overflow
  1508. BOOST_UBLAS_CHECK (j <= ((std::numeric_limits<size_type>::max) () - i) / size_i, bad_index ());
  1509. return i + j * size_i;
  1510. }
  1511. static
  1512. BOOST_UBLAS_INLINE
  1513. size_type address (size_type i, size_type size_i, size_type j, size_type size_j) {
  1514. BOOST_UBLAS_CHECK (i <= size_i, bad_index ());
  1515. BOOST_UBLAS_CHECK (j <= size_j, bad_index ());
  1516. detail::ignore_unused_variable_warning(size_j);
  1517. // Guard against size_type overflow - address may be size_i past end of storage
  1518. BOOST_UBLAS_CHECK (size_i == 0 || j <= ((std::numeric_limits<size_type>::max) () - i) / size_i, bad_index ());
  1519. return i + j * size_i;
  1520. }
  1521. // Storage element to index conversion
  1522. static
  1523. BOOST_UBLAS_INLINE
  1524. difference_type distance_i (difference_type k, size_type /* size_i */, size_type /* size_j */) {
  1525. return k;
  1526. }
  1527. static
  1528. BOOST_UBLAS_INLINE
  1529. difference_type distance_j (difference_type k, size_type size_i, size_type /* size_j */) {
  1530. return size_i != 0 ? k / size_i : 0;
  1531. }
  1532. static
  1533. BOOST_UBLAS_INLINE
  1534. size_type index_i (difference_type k, size_type size_i, size_type /* size_j */) {
  1535. return size_i != 0 ? k % size_i : 0;
  1536. }
  1537. static
  1538. BOOST_UBLAS_INLINE
  1539. size_type index_j (difference_type k, size_type size_i, size_type /* size_j */) {
  1540. return size_i != 0 ? k / size_i : 0;
  1541. }
  1542. static
  1543. BOOST_UBLAS_INLINE
  1544. bool fast_i () {
  1545. return true;
  1546. }
  1547. static
  1548. BOOST_UBLAS_INLINE
  1549. bool fast_j () {
  1550. return false;
  1551. }
  1552. // Iterating
  1553. template<class I>
  1554. static
  1555. BOOST_UBLAS_INLINE
  1556. void increment_i (I &it, size_type /* size_i */, size_type /* size_j */) {
  1557. ++ it;
  1558. }
  1559. template<class I>
  1560. static
  1561. BOOST_UBLAS_INLINE
  1562. void increment_i (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1563. it += n;
  1564. }
  1565. template<class I>
  1566. static
  1567. BOOST_UBLAS_INLINE
  1568. void decrement_i (I &it, size_type /* size_i */, size_type /* size_j */) {
  1569. -- it;
  1570. }
  1571. template<class I>
  1572. static
  1573. BOOST_UBLAS_INLINE
  1574. void decrement_i (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1575. it -= n;
  1576. }
  1577. template<class I>
  1578. static
  1579. BOOST_UBLAS_INLINE
  1580. void increment_j (I &it, size_type size_i, size_type /* size_j */) {
  1581. it += size_i;
  1582. }
  1583. template<class I>
  1584. static
  1585. BOOST_UBLAS_INLINE
  1586. void increment_j (I &it, difference_type n, size_type size_i, size_type /* size_j */) {
  1587. it += n * size_i;
  1588. }
  1589. template<class I>
  1590. static
  1591. BOOST_UBLAS_INLINE
  1592. void decrement_j (I &it, size_type size_i, size_type /* size_j */) {
  1593. it -= size_i;
  1594. }
  1595. template<class I>
  1596. static
  1597. BOOST_UBLAS_INLINE
  1598. void decrement_j (I &it, difference_type n, size_type size_i, size_type /* size_j */) {
  1599. it -= n* size_i;
  1600. }
  1601. // Triangular access
  1602. static
  1603. BOOST_UBLAS_INLINE
  1604. size_type triangular_size (size_type size_i, size_type size_j) {
  1605. size_type size = (std::max) (size_i, size_j);
  1606. // Guard against size_type overflow - simplified
  1607. BOOST_UBLAS_CHECK (size == 0 || size / 2 < (std::numeric_limits<size_type>::max) () / size /* +1/2 */, bad_size ());
  1608. return ((size + 1) * size) / 2;
  1609. }
  1610. static
  1611. BOOST_UBLAS_INLINE
  1612. size_type lower_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1613. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1614. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1615. BOOST_UBLAS_CHECK (i >= j, bad_index ());
  1616. // FIXME size_type overflow
  1617. // sigma_j (size - j) = size * j - j * (j - 1) / 2
  1618. // j = 0 1 2 3, sigma = 0 4 7 9
  1619. return i - j + (j * (2 * (std::max) (size_i, size_j) - j + 1)) / 2;
  1620. }
  1621. static
  1622. BOOST_UBLAS_INLINE
  1623. size_type upper_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1624. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1625. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1626. BOOST_UBLAS_CHECK (i <= j, bad_index ());
  1627. boost::ignore_unused(size_i, size_j);
  1628. // FIXME size_type overflow
  1629. // sigma_j (j + 1) = (j + 1) * j / 2
  1630. // j = 0 1 2 3, sigma = 0 1 3 6
  1631. return i + ((j + 1) * j) / 2;
  1632. }
  1633. // Major and minor indices
  1634. static
  1635. BOOST_UBLAS_INLINE
  1636. size_type index_M (size_type /* index1 */, size_type index2) {
  1637. return index2;
  1638. }
  1639. static
  1640. BOOST_UBLAS_INLINE
  1641. size_type index_m (size_type index1, size_type /* index2 */) {
  1642. return index1;
  1643. }
  1644. static
  1645. BOOST_UBLAS_INLINE
  1646. size_type size_M (size_type /* size_i */, size_type size_j) {
  1647. return size_j;
  1648. }
  1649. static
  1650. BOOST_UBLAS_INLINE
  1651. size_type size_m (size_type size_i, size_type /* size_j */) {
  1652. return size_i;
  1653. }
  1654. };
  1655. template <class Z>
  1656. struct basic_full {
  1657. typedef Z size_type;
  1658. template<class L>
  1659. static
  1660. BOOST_UBLAS_INLINE
  1661. size_type packed_size (L, size_type size_i, size_type size_j) {
  1662. return L::storage_size (size_i, size_j);
  1663. }
  1664. static
  1665. BOOST_UBLAS_INLINE
  1666. bool zero (size_type /* i */, size_type /* j */) {
  1667. return false;
  1668. }
  1669. static
  1670. BOOST_UBLAS_INLINE
  1671. bool one (size_type /* i */, size_type /* j */) {
  1672. return false;
  1673. }
  1674. static
  1675. BOOST_UBLAS_INLINE
  1676. bool other (size_type /* i */, size_type /* j */) {
  1677. return true;
  1678. }
  1679. // FIXME: this should not be used at all
  1680. static
  1681. BOOST_UBLAS_INLINE
  1682. size_type restrict1 (size_type i, size_type /* j */) {
  1683. return i;
  1684. }
  1685. static
  1686. BOOST_UBLAS_INLINE
  1687. size_type restrict2 (size_type /* i */, size_type j) {
  1688. return j;
  1689. }
  1690. static
  1691. BOOST_UBLAS_INLINE
  1692. size_type mutable_restrict1 (size_type i, size_type /* j */) {
  1693. return i;
  1694. }
  1695. static
  1696. BOOST_UBLAS_INLINE
  1697. size_type mutable_restrict2 (size_type /* i */, size_type j) {
  1698. return j;
  1699. }
  1700. };
  1701. namespace detail {
  1702. template < class L >
  1703. struct transposed_structure {
  1704. typedef typename L::size_type size_type;
  1705. template<class LAYOUT>
  1706. static
  1707. BOOST_UBLAS_INLINE
  1708. size_type packed_size (LAYOUT l, size_type size_i, size_type size_j) {
  1709. return L::packed_size(l, size_j, size_i);
  1710. }
  1711. static
  1712. BOOST_UBLAS_INLINE
  1713. bool zero (size_type i, size_type j) {
  1714. return L::zero(j, i);
  1715. }
  1716. static
  1717. BOOST_UBLAS_INLINE
  1718. bool one (size_type i, size_type j) {
  1719. return L::one(j, i);
  1720. }
  1721. static
  1722. BOOST_UBLAS_INLINE
  1723. bool other (size_type i, size_type j) {
  1724. return L::other(j, i);
  1725. }
  1726. template<class LAYOUT>
  1727. static
  1728. BOOST_UBLAS_INLINE
  1729. size_type element (LAYOUT /* l */, size_type i, size_type size_i, size_type j, size_type size_j) {
  1730. return L::element(typename LAYOUT::transposed_layout(), j, size_j, i, size_i);
  1731. }
  1732. static
  1733. BOOST_UBLAS_INLINE
  1734. size_type restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
  1735. return L::restrict2(j, i, size2, size1);
  1736. }
  1737. static
  1738. BOOST_UBLAS_INLINE
  1739. size_type restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
  1740. return L::restrict1(j, i, size2, size1);
  1741. }
  1742. static
  1743. BOOST_UBLAS_INLINE
  1744. size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
  1745. return L::mutable_restrict2(j, i, size2, size1);
  1746. }
  1747. static
  1748. BOOST_UBLAS_INLINE
  1749. size_type mutable_restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
  1750. return L::mutable_restrict1(j, i, size2, size1);
  1751. }
  1752. static
  1753. BOOST_UBLAS_INLINE
  1754. size_type global_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1755. return L::global_restrict2(index2, size2, index1, size1);
  1756. }
  1757. static
  1758. BOOST_UBLAS_INLINE
  1759. size_type global_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1760. return L::global_restrict1(index2, size2, index1, size1);
  1761. }
  1762. static
  1763. BOOST_UBLAS_INLINE
  1764. size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1765. return L::global_mutable_restrict2(index2, size2, index1, size1);
  1766. }
  1767. static
  1768. BOOST_UBLAS_INLINE
  1769. size_type global_mutable_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1770. return L::global_mutable_restrict1(index2, size2, index1, size1);
  1771. }
  1772. };
  1773. }
  1774. template <class Z>
  1775. struct basic_lower {
  1776. typedef Z size_type;
  1777. typedef lower_tag triangular_type;
  1778. template<class L>
  1779. static
  1780. BOOST_UBLAS_INLINE
  1781. size_type packed_size (L, size_type size_i, size_type size_j) {
  1782. return L::triangular_size (size_i, size_j);
  1783. }
  1784. static
  1785. BOOST_UBLAS_INLINE
  1786. bool zero (size_type i, size_type j) {
  1787. return j > i;
  1788. }
  1789. static
  1790. BOOST_UBLAS_INLINE
  1791. bool one (size_type /* i */, size_type /* j */) {
  1792. return false;
  1793. }
  1794. static
  1795. BOOST_UBLAS_INLINE
  1796. bool other (size_type i, size_type j) {
  1797. return j <= i;
  1798. }
  1799. template<class L>
  1800. static
  1801. BOOST_UBLAS_INLINE
  1802. size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
  1803. return L::lower_element (i, size_i, j, size_j);
  1804. }
  1805. // return nearest valid index in column j
  1806. static
  1807. BOOST_UBLAS_INLINE
  1808. size_type restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
  1809. return (std::max)(j, (std::min) (size1, i));
  1810. }
  1811. // return nearest valid index in row i
  1812. static
  1813. BOOST_UBLAS_INLINE
  1814. size_type restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
  1815. return (std::max)(size_type(0), (std::min) (i+1, j));
  1816. }
  1817. // return nearest valid mutable index in column j
  1818. static
  1819. BOOST_UBLAS_INLINE
  1820. size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
  1821. return (std::max)(j, (std::min) (size1, i));
  1822. }
  1823. // return nearest valid mutable index in row i
  1824. static
  1825. BOOST_UBLAS_INLINE
  1826. size_type mutable_restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
  1827. return (std::max)(size_type(0), (std::min) (i+1, j));
  1828. }
  1829. // return an index between the first and (1+last) filled row
  1830. static
  1831. BOOST_UBLAS_INLINE
  1832. size_type global_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
  1833. return (std::max)(size_type(0), (std::min)(size1, index1) );
  1834. }
  1835. // return an index between the first and (1+last) filled column
  1836. static
  1837. BOOST_UBLAS_INLINE
  1838. size_type global_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
  1839. return (std::max)(size_type(0), (std::min)(size2, index2) );
  1840. }
  1841. // return an index between the first and (1+last) filled mutable row
  1842. static
  1843. BOOST_UBLAS_INLINE
  1844. size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
  1845. return (std::max)(size_type(0), (std::min)(size1, index1) );
  1846. }
  1847. // return an index between the first and (1+last) filled mutable column
  1848. static
  1849. BOOST_UBLAS_INLINE
  1850. size_type global_mutable_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
  1851. return (std::max)(size_type(0), (std::min)(size2, index2) );
  1852. }
  1853. };
  1854. // the first row only contains a single 1. Thus it is not stored.
  1855. template <class Z>
  1856. struct basic_unit_lower : public basic_lower<Z> {
  1857. typedef Z size_type;
  1858. typedef unit_lower_tag triangular_type;
  1859. template<class L>
  1860. static
  1861. BOOST_UBLAS_INLINE
  1862. size_type packed_size (L, size_type size_i, size_type size_j) {
  1863. // Zero size strict triangles are bad at this point
  1864. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0, bad_index ());
  1865. return L::triangular_size (size_i - 1, size_j - 1);
  1866. }
  1867. static
  1868. BOOST_UBLAS_INLINE
  1869. bool one (size_type i, size_type j) {
  1870. return j == i;
  1871. }
  1872. static
  1873. BOOST_UBLAS_INLINE
  1874. bool other (size_type i, size_type j) {
  1875. return j < i;
  1876. }
  1877. template<class L>
  1878. static
  1879. BOOST_UBLAS_INLINE
  1880. size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
  1881. // Zero size strict triangles are bad at this point
  1882. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0 && i != 0, bad_index ());
  1883. return L::lower_element (i-1, size_i - 1, j, size_j - 1);
  1884. }
  1885. static
  1886. BOOST_UBLAS_INLINE
  1887. size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
  1888. return (std::max)(j+1, (std::min) (size1, i));
  1889. }
  1890. static
  1891. BOOST_UBLAS_INLINE
  1892. size_type mutable_restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
  1893. return (std::max)(size_type(0), (std::min) (i, j));
  1894. }
  1895. // return an index between the first and (1+last) filled mutable row
  1896. static
  1897. BOOST_UBLAS_INLINE
  1898. size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
  1899. return (std::max)(size_type(1), (std::min)(size1, index1) );
  1900. }
  1901. // return an index between the first and (1+last) filled mutable column
  1902. static
  1903. BOOST_UBLAS_INLINE
  1904. size_type global_mutable_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
  1905. BOOST_UBLAS_CHECK( size2 >= 1 , external_logic() );
  1906. return (std::max)(size_type(0), (std::min)(size2-1, index2) );
  1907. }
  1908. };
  1909. // the first row only contains no element. Thus it is not stored.
  1910. template <class Z>
  1911. struct basic_strict_lower : public basic_unit_lower<Z> {
  1912. typedef Z size_type;
  1913. typedef strict_lower_tag triangular_type;
  1914. template<class L>
  1915. static
  1916. BOOST_UBLAS_INLINE
  1917. size_type packed_size (L, size_type size_i, size_type size_j) {
  1918. // Zero size strict triangles are bad at this point
  1919. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0, bad_index ());
  1920. return L::triangular_size (size_i - 1, size_j - 1);
  1921. }
  1922. static
  1923. BOOST_UBLAS_INLINE
  1924. bool zero (size_type i, size_type j) {
  1925. return j >= i;
  1926. }
  1927. static
  1928. BOOST_UBLAS_INLINE
  1929. bool one (size_type /*i*/, size_type /*j*/) {
  1930. return false;
  1931. }
  1932. static
  1933. BOOST_UBLAS_INLINE
  1934. bool other (size_type i, size_type j) {
  1935. return j < i;
  1936. }
  1937. template<class L>
  1938. static
  1939. BOOST_UBLAS_INLINE
  1940. size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
  1941. // Zero size strict triangles are bad at this point
  1942. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0 && i != 0, bad_index ());
  1943. return L::lower_element (i-1, size_i - 1, j, size_j - 1);
  1944. }
  1945. static
  1946. BOOST_UBLAS_INLINE
  1947. size_type restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
  1948. return basic_unit_lower<Z>::mutable_restrict1(i, j, size1, size2);
  1949. }
  1950. static
  1951. BOOST_UBLAS_INLINE
  1952. size_type restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
  1953. return basic_unit_lower<Z>::mutable_restrict2(i, j, size1, size2);
  1954. }
  1955. // return an index between the first and (1+last) filled row
  1956. static
  1957. BOOST_UBLAS_INLINE
  1958. size_type global_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1959. return basic_unit_lower<Z>::global_mutable_restrict1(index1, size1, index2, size2);
  1960. }
  1961. // return an index between the first and (1+last) filled column
  1962. static
  1963. BOOST_UBLAS_INLINE
  1964. size_type global_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1965. return basic_unit_lower<Z>::global_mutable_restrict2(index1, size1, index2, size2);
  1966. }
  1967. };
  1968. template <class Z>
  1969. struct basic_upper : public detail::transposed_structure<basic_lower<Z> >
  1970. {
  1971. typedef upper_tag triangular_type;
  1972. };
  1973. template <class Z>
  1974. struct basic_unit_upper : public detail::transposed_structure<basic_unit_lower<Z> >
  1975. {
  1976. typedef unit_upper_tag triangular_type;
  1977. };
  1978. template <class Z>
  1979. struct basic_strict_upper : public detail::transposed_structure<basic_strict_lower<Z> >
  1980. {
  1981. typedef strict_upper_tag triangular_type;
  1982. };
  1983. }}}
  1984. #endif