functional.hpp 77 KB

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