functional.hpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  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. #ifndef BOOST_UBLAS_SCALED_NORM
  382. real_type t = real_type ();
  383. typedef typename E::size_type vector_size_type;
  384. vector_size_type size (e ().size ());
  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 type_traits<real_type>::type_sqrt (t);
  390. #else
  391. real_type scale = real_type ();
  392. real_type sum_squares (1);
  393. size_type size (e ().size ());
  394. for (size_type i = 0; i < size; ++ i) {
  395. real_type u (type_traits<value_type>::norm_2 (e () (i)));
  396. if ( real_type () /* zero */ == u ) continue;
  397. if (scale < u) {
  398. real_type v (scale / u);
  399. sum_squares = sum_squares * v * v + real_type (1);
  400. scale = u;
  401. } else {
  402. real_type v (u / scale);
  403. sum_squares += v * v;
  404. }
  405. }
  406. return scale * type_traits<real_type>::type_sqrt (sum_squares);
  407. #endif
  408. }
  409. // Dense case
  410. template<class D, class I>
  411. static BOOST_UBLAS_INLINE
  412. result_type apply (D size, I it) {
  413. #ifndef BOOST_UBLAS_SCALED_NORM
  414. real_type t = real_type ();
  415. while (-- size >= 0) {
  416. real_type u (type_traits<value_type>::norm_2 (*it));
  417. t += u * u;
  418. ++ it;
  419. }
  420. return type_traits<real_type>::type_sqrt (t);
  421. #else
  422. real_type scale = real_type ();
  423. real_type sum_squares (1);
  424. while (-- size >= 0) {
  425. real_type u (type_traits<value_type>::norm_2 (*it));
  426. if (scale < u) {
  427. real_type v (scale / u);
  428. sum_squares = sum_squares * v * v + real_type (1);
  429. scale = u;
  430. } else {
  431. real_type v (u / scale);
  432. sum_squares += v * v;
  433. }
  434. ++ it;
  435. }
  436. return scale * type_traits<real_type>::type_sqrt (sum_squares);
  437. #endif
  438. }
  439. // Sparse case
  440. template<class I>
  441. static BOOST_UBLAS_INLINE
  442. result_type apply (I it, const I &it_end) {
  443. #ifndef BOOST_UBLAS_SCALED_NORM
  444. real_type t = real_type ();
  445. while (it != it_end) {
  446. real_type u (type_traits<value_type>::norm_2 (*it));
  447. t += u * u;
  448. ++ it;
  449. }
  450. return type_traits<real_type>::type_sqrt (t);
  451. #else
  452. real_type scale = real_type ();
  453. real_type sum_squares (1);
  454. while (it != it_end) {
  455. real_type u (type_traits<value_type>::norm_2 (*it));
  456. if (scale < u) {
  457. real_type v (scale / u);
  458. sum_squares = sum_squares * v * v + real_type (1);
  459. scale = u;
  460. } else {
  461. real_type v (u / scale);
  462. sum_squares += v * v;
  463. }
  464. ++ it;
  465. }
  466. return scale * type_traits<real_type>::type_sqrt (sum_squares);
  467. #endif
  468. }
  469. };
  470. template<class V>
  471. struct vector_norm_inf:
  472. public vector_scalar_real_unary_functor<V> {
  473. typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
  474. typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
  475. typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
  476. template<class E>
  477. static BOOST_UBLAS_INLINE
  478. result_type apply (const vector_expression<E> &e) {
  479. real_type t = real_type ();
  480. typedef typename E::size_type vector_size_type;
  481. vector_size_type size (e ().size ());
  482. for (vector_size_type i = 0; i < size; ++ i) {
  483. real_type u (type_traits<value_type>::norm_inf (e () (i)));
  484. if (u > t)
  485. t = u;
  486. }
  487. return t;
  488. }
  489. // Dense case
  490. template<class D, class I>
  491. static BOOST_UBLAS_INLINE
  492. result_type apply (D size, I it) {
  493. real_type t = real_type ();
  494. while (-- size >= 0) {
  495. real_type u (type_traits<value_type>::norm_inf (*it));
  496. if (u > t)
  497. t = u;
  498. ++ it;
  499. }
  500. return t;
  501. }
  502. // Sparse case
  503. template<class I>
  504. static BOOST_UBLAS_INLINE
  505. result_type apply (I it, const I &it_end) {
  506. real_type t = real_type ();
  507. while (it != it_end) {
  508. real_type u (type_traits<value_type>::norm_inf (*it));
  509. if (u > t)
  510. t = u;
  511. ++ it;
  512. }
  513. return t;
  514. }
  515. };
  516. // Unary returning index
  517. template<class V>
  518. struct vector_scalar_index_unary_functor {
  519. typedef typename V::value_type value_type;
  520. typedef typename type_traits<value_type>::real_type real_type;
  521. typedef typename V::size_type result_type;
  522. };
  523. template<class V>
  524. struct vector_index_norm_inf:
  525. public vector_scalar_index_unary_functor<V> {
  526. typedef typename vector_scalar_index_unary_functor<V>::value_type value_type;
  527. typedef typename vector_scalar_index_unary_functor<V>::real_type real_type;
  528. typedef typename vector_scalar_index_unary_functor<V>::result_type result_type;
  529. template<class E>
  530. static BOOST_UBLAS_INLINE
  531. result_type apply (const vector_expression<E> &e) {
  532. // ISSUE For CBLAS compatibility return 0 index in empty case
  533. result_type i_norm_inf (0);
  534. real_type t = real_type ();
  535. typedef typename E::size_type vector_size_type;
  536. vector_size_type size (e ().size ());
  537. for (vector_size_type i = 0; i < size; ++ i) {
  538. real_type u (type_traits<value_type>::norm_inf (e () (i)));
  539. if (u > t) {
  540. i_norm_inf = i;
  541. t = u;
  542. }
  543. }
  544. return i_norm_inf;
  545. }
  546. // Dense case
  547. template<class D, class I>
  548. static BOOST_UBLAS_INLINE
  549. result_type apply (D size, I it) {
  550. // ISSUE For CBLAS compatibility return 0 index in empty case
  551. result_type i_norm_inf (0);
  552. real_type t = real_type ();
  553. while (-- size >= 0) {
  554. real_type u (type_traits<value_type>::norm_inf (*it));
  555. if (u > t) {
  556. i_norm_inf = it.index ();
  557. t = u;
  558. }
  559. ++ it;
  560. }
  561. return i_norm_inf;
  562. }
  563. // Sparse case
  564. template<class I>
  565. static BOOST_UBLAS_INLINE
  566. result_type apply (I it, const I &it_end) {
  567. // ISSUE For CBLAS compatibility return 0 index in empty case
  568. result_type i_norm_inf (0);
  569. real_type t = real_type ();
  570. while (it != it_end) {
  571. real_type u (type_traits<value_type>::norm_inf (*it));
  572. if (u > t) {
  573. i_norm_inf = it.index ();
  574. t = u;
  575. }
  576. ++ it;
  577. }
  578. return i_norm_inf;
  579. }
  580. };
  581. // Binary returning scalar
  582. template<class V1, class V2, class TV>
  583. struct vector_scalar_binary_functor {
  584. typedef TV value_type;
  585. typedef TV result_type;
  586. };
  587. template<class V1, class V2, class TV>
  588. struct vector_inner_prod:
  589. public vector_scalar_binary_functor<V1, V2, TV> {
  590. typedef typename vector_scalar_binary_functor<V1, V2, TV>::value_type value_type;
  591. typedef typename vector_scalar_binary_functor<V1, V2, TV>::result_type result_type;
  592. template<class C1, class C2>
  593. static BOOST_UBLAS_INLINE
  594. result_type apply (const vector_container<C1> &c1,
  595. const vector_container<C2> &c2) {
  596. #ifdef BOOST_UBLAS_USE_SIMD
  597. using namespace raw;
  598. typedef typename C1::size_type vector_size_type;
  599. vector_size_type size (BOOST_UBLAS_SAME (c1 ().size (), c2 ().size ()));
  600. const typename V1::value_type *data1 = data_const (c1 ());
  601. const typename V1::value_type *data2 = data_const (c2 ());
  602. vector_size_type s1 = stride (c1 ());
  603. vector_size_type s2 = stride (c2 ());
  604. result_type t = result_type (0);
  605. if (s1 == 1 && s2 == 1) {
  606. for (vector_size_type i = 0; i < size; ++ i)
  607. t += data1 [i] * data2 [i];
  608. } else if (s2 == 1) {
  609. for (vector_size_type i = 0, i1 = 0; i < size; ++ i, i1 += s1)
  610. t += data1 [i1] * data2 [i];
  611. } else if (s1 == 1) {
  612. for (vector_size_type i = 0, i2 = 0; i < size; ++ i, i2 += s2)
  613. t += data1 [i] * data2 [i2];
  614. } else {
  615. for (vector_size_type i = 0, i1 = 0, i2 = 0; i < size; ++ i, i1 += s1, i2 += s2)
  616. t += data1 [i1] * data2 [i2];
  617. }
  618. return t;
  619. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  620. return boost::numeric::bindings::atlas::dot (c1 (), c2 ());
  621. #else
  622. return apply (static_cast<const vector_expression<C1> > (c1), static_cast<const vector_expression<C2> > (c2));
  623. #endif
  624. }
  625. template<class E1, class E2>
  626. static BOOST_UBLAS_INLINE
  627. result_type apply (const vector_expression<E1> &e1,
  628. const vector_expression<E2> &e2) {
  629. typedef typename E1::size_type vector_size_type;
  630. vector_size_type size (BOOST_UBLAS_SAME (e1 ().size (), e2 ().size ()));
  631. result_type t = result_type (0);
  632. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  633. for (vector_size_type i = 0; i < size; ++ i)
  634. t += e1 () (i) * e2 () (i);
  635. #else
  636. vector_size_type i (0);
  637. DD (size, 4, r, (t += e1 () (i) * e2 () (i), ++ i));
  638. #endif
  639. return t;
  640. }
  641. // Dense case
  642. template<class D, class I1, class I2>
  643. static BOOST_UBLAS_INLINE
  644. result_type apply (D size, I1 it1, I2 it2) {
  645. result_type t = result_type (0);
  646. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  647. while (-- size >= 0)
  648. t += *it1 * *it2, ++ it1, ++ it2;
  649. #else
  650. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  651. #endif
  652. return t;
  653. }
  654. // Packed case
  655. template<class I1, class I2>
  656. static BOOST_UBLAS_INLINE
  657. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
  658. result_type t = result_type (0);
  659. typedef typename I1::difference_type vector_difference_type;
  660. vector_difference_type it1_size (it1_end - it1);
  661. vector_difference_type it2_size (it2_end - it2);
  662. vector_difference_type diff (0);
  663. if (it1_size > 0 && it2_size > 0)
  664. diff = it2.index () - it1.index ();
  665. if (diff != 0) {
  666. vector_difference_type size = (std::min) (diff, it1_size);
  667. if (size > 0) {
  668. it1 += size;
  669. it1_size -= size;
  670. diff -= size;
  671. }
  672. size = (std::min) (- diff, it2_size);
  673. if (size > 0) {
  674. it2 += size;
  675. it2_size -= size;
  676. diff += size;
  677. }
  678. }
  679. vector_difference_type size ((std::min) (it1_size, it2_size));
  680. while (-- size >= 0)
  681. t += *it1 * *it2, ++ it1, ++ it2;
  682. return t;
  683. }
  684. // Sparse case
  685. template<class I1, class I2>
  686. static BOOST_UBLAS_INLINE
  687. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, sparse_bidirectional_iterator_tag) {
  688. result_type t = result_type (0);
  689. if (it1 != it1_end && it2 != it2_end) {
  690. while (true) {
  691. if (it1.index () == it2.index ()) {
  692. t += *it1 * *it2, ++ it1, ++ it2;
  693. if (it1 == it1_end || it2 == it2_end)
  694. break;
  695. } else if (it1.index () < it2.index ()) {
  696. increment (it1, it1_end, it2.index () - it1.index ());
  697. if (it1 == it1_end)
  698. break;
  699. } else if (it1.index () > it2.index ()) {
  700. increment (it2, it2_end, it1.index () - it2.index ());
  701. if (it2 == it2_end)
  702. break;
  703. }
  704. }
  705. }
  706. return t;
  707. }
  708. };
  709. // Matrix functors
  710. // Binary returning vector
  711. template<class M1, class M2, class TV>
  712. struct matrix_vector_binary_functor {
  713. typedef typename M1::size_type size_type;
  714. typedef typename M1::difference_type difference_type;
  715. typedef TV value_type;
  716. typedef TV result_type;
  717. };
  718. template<class M1, class M2, class TV>
  719. struct matrix_vector_prod1:
  720. public matrix_vector_binary_functor<M1, M2, TV> {
  721. typedef typename matrix_vector_binary_functor<M1, M2, TV>::size_type size_type;
  722. typedef typename matrix_vector_binary_functor<M1, M2, TV>::difference_type difference_type;
  723. typedef typename matrix_vector_binary_functor<M1, M2, TV>::value_type value_type;
  724. typedef typename matrix_vector_binary_functor<M1, M2, TV>::result_type result_type;
  725. template<class C1, class C2>
  726. static BOOST_UBLAS_INLINE
  727. result_type apply (const matrix_container<C1> &c1,
  728. const vector_container<C2> &c2,
  729. size_type i) {
  730. #ifdef BOOST_UBLAS_USE_SIMD
  731. using namespace raw;
  732. size_type size = BOOST_UBLAS_SAME (c1 ().size2 (), c2 ().size ());
  733. const typename M1::value_type *data1 = data_const (c1 ()) + i * stride1 (c1 ());
  734. const typename M2::value_type *data2 = data_const (c2 ());
  735. size_type s1 = stride2 (c1 ());
  736. size_type s2 = stride (c2 ());
  737. result_type t = result_type (0);
  738. if (s1 == 1 && s2 == 1) {
  739. for (size_type j = 0; j < size; ++ j)
  740. t += data1 [j] * data2 [j];
  741. } else if (s2 == 1) {
  742. for (size_type j = 0, j1 = 0; j < size; ++ j, j1 += s1)
  743. t += data1 [j1] * data2 [j];
  744. } else if (s1 == 1) {
  745. for (size_type j = 0, j2 = 0; j < size; ++ j, j2 += s2)
  746. t += data1 [j] * data2 [j2];
  747. } else {
  748. for (size_type j = 0, j1 = 0, j2 = 0; j < size; ++ j, j1 += s1, j2 += s2)
  749. t += data1 [j1] * data2 [j2];
  750. }
  751. return t;
  752. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  753. return boost::numeric::bindings::atlas::dot (c1 ().row (i), c2 ());
  754. #else
  755. return apply (static_cast<const matrix_expression<C1> > (c1), static_cast<const vector_expression<C2> > (c2, i));
  756. #endif
  757. }
  758. template<class E1, class E2>
  759. static BOOST_UBLAS_INLINE
  760. result_type apply (const matrix_expression<E1> &e1,
  761. const vector_expression<E2> &e2,
  762. size_type i) {
  763. size_type size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size ());
  764. result_type t = result_type (0);
  765. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  766. for (size_type j = 0; j < size; ++ j)
  767. t += e1 () (i, j) * e2 () (j);
  768. #else
  769. size_type j (0);
  770. DD (size, 4, r, (t += e1 () (i, j) * e2 () (j), ++ j));
  771. #endif
  772. return t;
  773. }
  774. // Dense case
  775. template<class I1, class I2>
  776. static BOOST_UBLAS_INLINE
  777. result_type apply (difference_type size, I1 it1, I2 it2) {
  778. result_type t = result_type (0);
  779. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  780. while (-- size >= 0)
  781. t += *it1 * *it2, ++ it1, ++ it2;
  782. #else
  783. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  784. #endif
  785. return t;
  786. }
  787. // Packed case
  788. template<class I1, class I2>
  789. static BOOST_UBLAS_INLINE
  790. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
  791. result_type t = result_type (0);
  792. difference_type it1_size (it1_end - it1);
  793. difference_type it2_size (it2_end - it2);
  794. difference_type diff (0);
  795. if (it1_size > 0 && it2_size > 0)
  796. diff = it2.index () - it1.index2 ();
  797. if (diff != 0) {
  798. difference_type size = (std::min) (diff, it1_size);
  799. if (size > 0) {
  800. it1 += size;
  801. it1_size -= size;
  802. diff -= size;
  803. }
  804. size = (std::min) (- diff, it2_size);
  805. if (size > 0) {
  806. it2 += size;
  807. it2_size -= size;
  808. diff += size;
  809. }
  810. }
  811. difference_type size ((std::min) (it1_size, it2_size));
  812. while (-- size >= 0)
  813. t += *it1 * *it2, ++ it1, ++ it2;
  814. return t;
  815. }
  816. // Sparse case
  817. template<class I1, class I2>
  818. static BOOST_UBLAS_INLINE
  819. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  820. sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag) {
  821. result_type t = result_type (0);
  822. if (it1 != it1_end && it2 != it2_end) {
  823. size_type it1_index = it1.index2 (), it2_index = it2.index ();
  824. while (true) {
  825. difference_type compare = it1_index - it2_index;
  826. if (compare == 0) {
  827. t += *it1 * *it2, ++ it1, ++ it2;
  828. if (it1 != it1_end && it2 != it2_end) {
  829. it1_index = it1.index2 ();
  830. it2_index = it2.index ();
  831. } else
  832. break;
  833. } else if (compare < 0) {
  834. increment (it1, it1_end, - compare);
  835. if (it1 != it1_end)
  836. it1_index = it1.index2 ();
  837. else
  838. break;
  839. } else if (compare > 0) {
  840. increment (it2, it2_end, compare);
  841. if (it2 != it2_end)
  842. it2_index = it2.index ();
  843. else
  844. break;
  845. }
  846. }
  847. }
  848. return t;
  849. }
  850. // Sparse packed case
  851. template<class I1, class I2>
  852. static BOOST_UBLAS_INLINE
  853. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &/* it2_end */,
  854. sparse_bidirectional_iterator_tag, packed_random_access_iterator_tag) {
  855. result_type t = result_type (0);
  856. while (it1 != it1_end) {
  857. t += *it1 * it2 () (it1.index2 ());
  858. ++ it1;
  859. }
  860. return t;
  861. }
  862. // Packed sparse case
  863. template<class I1, class I2>
  864. static BOOST_UBLAS_INLINE
  865. result_type apply (I1 it1, const I1 &/* it1_end */, I2 it2, const I2 &it2_end,
  866. packed_random_access_iterator_tag, sparse_bidirectional_iterator_tag) {
  867. result_type t = result_type (0);
  868. while (it2 != it2_end) {
  869. t += it1 () (it1.index1 (), it2.index ()) * *it2;
  870. ++ it2;
  871. }
  872. return t;
  873. }
  874. // Another dispatcher
  875. template<class I1, class I2>
  876. static BOOST_UBLAS_INLINE
  877. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  878. sparse_bidirectional_iterator_tag) {
  879. typedef typename I1::iterator_category iterator1_category;
  880. typedef typename I2::iterator_category iterator2_category;
  881. return apply (it1, it1_end, it2, it2_end, iterator1_category (), iterator2_category ());
  882. }
  883. };
  884. template<class M1, class M2, class TV>
  885. struct matrix_vector_prod2:
  886. public matrix_vector_binary_functor<M1, M2, TV> {
  887. typedef typename matrix_vector_binary_functor<M1, M2, TV>::size_type size_type;
  888. typedef typename matrix_vector_binary_functor<M1, M2, TV>::difference_type difference_type;
  889. typedef typename matrix_vector_binary_functor<M1, M2, TV>::value_type value_type;
  890. typedef typename matrix_vector_binary_functor<M1, M2, TV>::result_type result_type;
  891. template<class C1, class C2>
  892. static BOOST_UBLAS_INLINE
  893. result_type apply (const vector_container<C1> &c1,
  894. const matrix_container<C2> &c2,
  895. size_type i) {
  896. #ifdef BOOST_UBLAS_USE_SIMD
  897. using namespace raw;
  898. size_type size = BOOST_UBLAS_SAME (c1 ().size (), c2 ().size1 ());
  899. const typename M1::value_type *data1 = data_const (c1 ());
  900. const typename M2::value_type *data2 = data_const (c2 ()) + i * stride2 (c2 ());
  901. size_type s1 = stride (c1 ());
  902. size_type s2 = stride1 (c2 ());
  903. result_type t = result_type (0);
  904. if (s1 == 1 && s2 == 1) {
  905. for (size_type j = 0; j < size; ++ j)
  906. t += data1 [j] * data2 [j];
  907. } else if (s2 == 1) {
  908. for (size_type j = 0, j1 = 0; j < size; ++ j, j1 += s1)
  909. t += data1 [j1] * data2 [j];
  910. } else if (s1 == 1) {
  911. for (size_type j = 0, j2 = 0; j < size; ++ j, j2 += s2)
  912. t += data1 [j] * data2 [j2];
  913. } else {
  914. for (size_type j = 0, j1 = 0, j2 = 0; j < size; ++ j, j1 += s1, j2 += s2)
  915. t += data1 [j1] * data2 [j2];
  916. }
  917. return t;
  918. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  919. return boost::numeric::bindings::atlas::dot (c1 (), c2 ().column (i));
  920. #else
  921. return apply (static_cast<const vector_expression<C1> > (c1), static_cast<const matrix_expression<C2> > (c2, i));
  922. #endif
  923. }
  924. template<class E1, class E2>
  925. static BOOST_UBLAS_INLINE
  926. result_type apply (const vector_expression<E1> &e1,
  927. const matrix_expression<E2> &e2,
  928. size_type i) {
  929. size_type size = BOOST_UBLAS_SAME (e1 ().size (), e2 ().size1 ());
  930. result_type t = result_type (0);
  931. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  932. for (size_type j = 0; j < size; ++ j)
  933. t += e1 () (j) * e2 () (j, i);
  934. #else
  935. size_type j (0);
  936. DD (size, 4, r, (t += e1 () (j) * e2 () (j, i), ++ j));
  937. #endif
  938. return t;
  939. }
  940. // Dense case
  941. template<class I1, class I2>
  942. static BOOST_UBLAS_INLINE
  943. result_type apply (difference_type size, I1 it1, I2 it2) {
  944. result_type t = result_type (0);
  945. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  946. while (-- size >= 0)
  947. t += *it1 * *it2, ++ it1, ++ it2;
  948. #else
  949. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  950. #endif
  951. return t;
  952. }
  953. // Packed case
  954. template<class I1, class I2>
  955. static BOOST_UBLAS_INLINE
  956. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
  957. result_type t = result_type (0);
  958. difference_type it1_size (it1_end - it1);
  959. difference_type it2_size (it2_end - it2);
  960. difference_type diff (0);
  961. if (it1_size > 0 && it2_size > 0)
  962. diff = it2.index1 () - it1.index ();
  963. if (diff != 0) {
  964. difference_type size = (std::min) (diff, it1_size);
  965. if (size > 0) {
  966. it1 += size;
  967. it1_size -= size;
  968. diff -= size;
  969. }
  970. size = (std::min) (- diff, it2_size);
  971. if (size > 0) {
  972. it2 += size;
  973. it2_size -= size;
  974. diff += size;
  975. }
  976. }
  977. difference_type size ((std::min) (it1_size, it2_size));
  978. while (-- size >= 0)
  979. t += *it1 * *it2, ++ it1, ++ it2;
  980. return t;
  981. }
  982. // Sparse case
  983. template<class I1, class I2>
  984. static BOOST_UBLAS_INLINE
  985. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  986. sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag) {
  987. result_type t = result_type (0);
  988. if (it1 != it1_end && it2 != it2_end) {
  989. size_type it1_index = it1.index (), it2_index = it2.index1 ();
  990. while (true) {
  991. difference_type compare = it1_index - it2_index;
  992. if (compare == 0) {
  993. t += *it1 * *it2, ++ it1, ++ it2;
  994. if (it1 != it1_end && it2 != it2_end) {
  995. it1_index = it1.index ();
  996. it2_index = it2.index1 ();
  997. } else
  998. break;
  999. } else if (compare < 0) {
  1000. increment (it1, it1_end, - compare);
  1001. if (it1 != it1_end)
  1002. it1_index = it1.index ();
  1003. else
  1004. break;
  1005. } else if (compare > 0) {
  1006. increment (it2, it2_end, compare);
  1007. if (it2 != it2_end)
  1008. it2_index = it2.index1 ();
  1009. else
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. return t;
  1015. }
  1016. // Packed sparse case
  1017. template<class I1, class I2>
  1018. static BOOST_UBLAS_INLINE
  1019. result_type apply (I1 it1, const I1 &/* it1_end */, I2 it2, const I2 &it2_end,
  1020. packed_random_access_iterator_tag, sparse_bidirectional_iterator_tag) {
  1021. result_type t = result_type (0);
  1022. while (it2 != it2_end) {
  1023. t += it1 () (it2.index1 ()) * *it2;
  1024. ++ it2;
  1025. }
  1026. return t;
  1027. }
  1028. // Sparse packed case
  1029. template<class I1, class I2>
  1030. static BOOST_UBLAS_INLINE
  1031. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &/* it2_end */,
  1032. sparse_bidirectional_iterator_tag, packed_random_access_iterator_tag) {
  1033. result_type t = result_type (0);
  1034. while (it1 != it1_end) {
  1035. t += *it1 * it2 () (it1.index (), it2.index2 ());
  1036. ++ it1;
  1037. }
  1038. return t;
  1039. }
  1040. // Another dispatcher
  1041. template<class I1, class I2>
  1042. static BOOST_UBLAS_INLINE
  1043. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
  1044. sparse_bidirectional_iterator_tag) {
  1045. typedef typename I1::iterator_category iterator1_category;
  1046. typedef typename I2::iterator_category iterator2_category;
  1047. return apply (it1, it1_end, it2, it2_end, iterator1_category (), iterator2_category ());
  1048. }
  1049. };
  1050. // Binary returning matrix
  1051. template<class M1, class M2, class TV>
  1052. struct matrix_matrix_binary_functor {
  1053. typedef typename M1::size_type size_type;
  1054. typedef typename M1::difference_type difference_type;
  1055. typedef TV value_type;
  1056. typedef TV result_type;
  1057. };
  1058. template<class M1, class M2, class TV>
  1059. struct matrix_matrix_prod:
  1060. public matrix_matrix_binary_functor<M1, M2, TV> {
  1061. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::size_type size_type;
  1062. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::difference_type difference_type;
  1063. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::value_type value_type;
  1064. typedef typename matrix_matrix_binary_functor<M1, M2, TV>::result_type result_type;
  1065. template<class C1, class C2>
  1066. static BOOST_UBLAS_INLINE
  1067. result_type apply (const matrix_container<C1> &c1,
  1068. const matrix_container<C2> &c2,
  1069. size_type i, size_type j) {
  1070. #ifdef BOOST_UBLAS_USE_SIMD
  1071. using namespace raw;
  1072. size_type size = BOOST_UBLAS_SAME (c1 ().size2 (), c2 ().sizc1 ());
  1073. const typename M1::value_type *data1 = data_const (c1 ()) + i * stride1 (c1 ());
  1074. const typename M2::value_type *data2 = data_const (c2 ()) + j * stride2 (c2 ());
  1075. size_type s1 = stride2 (c1 ());
  1076. size_type s2 = stride1 (c2 ());
  1077. result_type t = result_type (0);
  1078. if (s1 == 1 && s2 == 1) {
  1079. for (size_type k = 0; k < size; ++ k)
  1080. t += data1 [k] * data2 [k];
  1081. } else if (s2 == 1) {
  1082. for (size_type k = 0, k1 = 0; k < size; ++ k, k1 += s1)
  1083. t += data1 [k1] * data2 [k];
  1084. } else if (s1 == 1) {
  1085. for (size_type k = 0, k2 = 0; k < size; ++ k, k2 += s2)
  1086. t += data1 [k] * data2 [k2];
  1087. } else {
  1088. for (size_type k = 0, k1 = 0, k2 = 0; k < size; ++ k, k1 += s1, k2 += s2)
  1089. t += data1 [k1] * data2 [k2];
  1090. }
  1091. return t;
  1092. #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
  1093. return boost::numeric::bindings::atlas::dot (c1 ().row (i), c2 ().column (j));
  1094. #else
  1095. boost::ignore_unused(j);
  1096. return apply (static_cast<const matrix_expression<C1> > (c1), static_cast<const matrix_expression<C2> > (c2, i));
  1097. #endif
  1098. }
  1099. template<class E1, class E2>
  1100. static BOOST_UBLAS_INLINE
  1101. result_type apply (const matrix_expression<E1> &e1,
  1102. const matrix_expression<E2> &e2,
  1103. size_type i, size_type j) {
  1104. size_type size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size1 ());
  1105. result_type t = result_type (0);
  1106. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  1107. for (size_type k = 0; k < size; ++ k)
  1108. t += e1 () (i, k) * e2 () (k, j);
  1109. #else
  1110. size_type k (0);
  1111. DD (size, 4, r, (t += e1 () (i, k) * e2 () (k, j), ++ k));
  1112. #endif
  1113. return t;
  1114. }
  1115. // Dense case
  1116. template<class I1, class I2>
  1117. static BOOST_UBLAS_INLINE
  1118. result_type apply (difference_type size, I1 it1, I2 it2) {
  1119. result_type t = result_type (0);
  1120. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  1121. while (-- size >= 0)
  1122. t += *it1 * *it2, ++ it1, ++ it2;
  1123. #else
  1124. DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
  1125. #endif
  1126. return t;
  1127. }
  1128. // Packed case
  1129. template<class I1, class I2>
  1130. static BOOST_UBLAS_INLINE
  1131. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, packed_random_access_iterator_tag) {
  1132. result_type t = result_type (0);
  1133. difference_type it1_size (it1_end - it1);
  1134. difference_type it2_size (it2_end - it2);
  1135. difference_type diff (0);
  1136. if (it1_size > 0 && it2_size > 0)
  1137. diff = it2.index1 () - it1.index2 ();
  1138. if (diff != 0) {
  1139. difference_type size = (std::min) (diff, it1_size);
  1140. if (size > 0) {
  1141. it1 += size;
  1142. it1_size -= size;
  1143. diff -= size;
  1144. }
  1145. size = (std::min) (- diff, it2_size);
  1146. if (size > 0) {
  1147. it2 += size;
  1148. it2_size -= size;
  1149. diff += size;
  1150. }
  1151. }
  1152. difference_type size ((std::min) (it1_size, it2_size));
  1153. while (-- size >= 0)
  1154. t += *it1 * *it2, ++ it1, ++ it2;
  1155. return t;
  1156. }
  1157. // Sparse case
  1158. template<class I1, class I2>
  1159. static BOOST_UBLAS_INLINE
  1160. result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, sparse_bidirectional_iterator_tag) {
  1161. result_type t = result_type (0);
  1162. if (it1 != it1_end && it2 != it2_end) {
  1163. size_type it1_index = it1.index2 (), it2_index = it2.index1 ();
  1164. while (true) {
  1165. difference_type compare = difference_type (it1_index - it2_index);
  1166. if (compare == 0) {
  1167. t += *it1 * *it2, ++ it1, ++ it2;
  1168. if (it1 != it1_end && it2 != it2_end) {
  1169. it1_index = it1.index2 ();
  1170. it2_index = it2.index1 ();
  1171. } else
  1172. break;
  1173. } else if (compare < 0) {
  1174. increment (it1, it1_end, - compare);
  1175. if (it1 != it1_end)
  1176. it1_index = it1.index2 ();
  1177. else
  1178. break;
  1179. } else if (compare > 0) {
  1180. increment (it2, it2_end, compare);
  1181. if (it2 != it2_end)
  1182. it2_index = it2.index1 ();
  1183. else
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. return t;
  1189. }
  1190. };
  1191. // Unary returning scalar norm
  1192. template<class M>
  1193. struct matrix_scalar_real_unary_functor {
  1194. typedef typename M::value_type value_type;
  1195. typedef typename type_traits<value_type>::real_type real_type;
  1196. typedef real_type result_type;
  1197. };
  1198. template<class M>
  1199. struct matrix_norm_1:
  1200. public matrix_scalar_real_unary_functor<M> {
  1201. typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
  1202. typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
  1203. typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
  1204. template<class E>
  1205. static BOOST_UBLAS_INLINE
  1206. result_type apply (const matrix_expression<E> &e) {
  1207. real_type t = real_type ();
  1208. typedef typename E::size_type matrix_size_type;
  1209. matrix_size_type size2 (e ().size2 ());
  1210. for (matrix_size_type j = 0; j < size2; ++ j) {
  1211. real_type u = real_type ();
  1212. matrix_size_type size1 (e ().size1 ());
  1213. for (matrix_size_type i = 0; i < size1; ++ i) {
  1214. real_type v (type_traits<value_type>::norm_1 (e () (i, j)));
  1215. u += v;
  1216. }
  1217. if (u > t)
  1218. t = u;
  1219. }
  1220. return t;
  1221. }
  1222. };
  1223. template<class M>
  1224. struct matrix_norm_frobenius:
  1225. public matrix_scalar_real_unary_functor<M> {
  1226. typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
  1227. typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
  1228. typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
  1229. template<class E>
  1230. static BOOST_UBLAS_INLINE
  1231. result_type apply (const matrix_expression<E> &e) {
  1232. real_type t = real_type ();
  1233. typedef typename E::size_type matrix_size_type;
  1234. matrix_size_type size1 (e ().size1 ());
  1235. for (matrix_size_type i = 0; i < size1; ++ i) {
  1236. matrix_size_type size2 (e ().size2 ());
  1237. for (matrix_size_type j = 0; j < size2; ++ j) {
  1238. real_type u (type_traits<value_type>::norm_2 (e () (i, j)));
  1239. t += u * u;
  1240. }
  1241. }
  1242. return type_traits<real_type>::type_sqrt (t);
  1243. }
  1244. };
  1245. template<class M>
  1246. struct matrix_norm_inf:
  1247. public matrix_scalar_real_unary_functor<M> {
  1248. typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
  1249. typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
  1250. typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
  1251. template<class E>
  1252. static BOOST_UBLAS_INLINE
  1253. result_type apply (const matrix_expression<E> &e) {
  1254. real_type t = real_type ();
  1255. typedef typename E::size_type matrix_size_type;
  1256. matrix_size_type size1 (e ().size1 ());
  1257. for (matrix_size_type i = 0; i < size1; ++ i) {
  1258. real_type u = real_type ();
  1259. matrix_size_type size2 (e ().size2 ());
  1260. for (matrix_size_type j = 0; j < size2; ++ j) {
  1261. real_type v (type_traits<value_type>::norm_inf (e () (i, j)));
  1262. u += v;
  1263. }
  1264. if (u > t)
  1265. t = u;
  1266. }
  1267. return t;
  1268. }
  1269. };
  1270. // forward declaration
  1271. template <class Z, class D> struct basic_column_major;
  1272. // This functor defines storage layout and it's properties
  1273. // matrix (i,j) -> storage [i * size_i + j]
  1274. template <class Z, class D>
  1275. struct basic_row_major {
  1276. typedef Z size_type;
  1277. typedef D difference_type;
  1278. typedef row_major_tag orientation_category;
  1279. typedef basic_column_major<Z,D> transposed_layout;
  1280. static
  1281. BOOST_UBLAS_INLINE
  1282. size_type storage_size (size_type size_i, size_type size_j) {
  1283. // Guard against size_type overflow
  1284. BOOST_UBLAS_CHECK (size_j == 0 || size_i <= (std::numeric_limits<size_type>::max) () / size_j, bad_size ());
  1285. return size_i * size_j;
  1286. }
  1287. // Indexing conversion to storage element
  1288. static
  1289. BOOST_UBLAS_INLINE
  1290. size_type element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1291. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1292. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1293. detail::ignore_unused_variable_warning(size_i);
  1294. // Guard against size_type overflow
  1295. BOOST_UBLAS_CHECK (i <= ((std::numeric_limits<size_type>::max) () - j) / size_j, bad_index ());
  1296. return i * size_j + j;
  1297. }
  1298. static
  1299. BOOST_UBLAS_INLINE
  1300. size_type address (size_type i, size_type size_i, size_type j, size_type size_j) {
  1301. BOOST_UBLAS_CHECK (i <= size_i, bad_index ());
  1302. BOOST_UBLAS_CHECK (j <= size_j, bad_index ());
  1303. // Guard against size_type overflow - address may be size_j past end of storage
  1304. BOOST_UBLAS_CHECK (size_j == 0 || i <= ((std::numeric_limits<size_type>::max) () - j) / size_j, bad_index ());
  1305. detail::ignore_unused_variable_warning(size_i);
  1306. return i * size_j + j;
  1307. }
  1308. // Storage element to index conversion
  1309. static
  1310. BOOST_UBLAS_INLINE
  1311. difference_type distance_i (difference_type k, size_type /* size_i */, size_type size_j) {
  1312. return size_j != 0 ? k / size_j : 0;
  1313. }
  1314. static
  1315. BOOST_UBLAS_INLINE
  1316. difference_type distance_j (difference_type k, size_type /* size_i */, size_type /* size_j */) {
  1317. return k;
  1318. }
  1319. static
  1320. BOOST_UBLAS_INLINE
  1321. size_type index_i (difference_type k, size_type /* size_i */, size_type size_j) {
  1322. return size_j != 0 ? k / size_j : 0;
  1323. }
  1324. static
  1325. BOOST_UBLAS_INLINE
  1326. size_type index_j (difference_type k, size_type /* size_i */, size_type size_j) {
  1327. return size_j != 0 ? k % size_j : 0;
  1328. }
  1329. static
  1330. BOOST_UBLAS_INLINE
  1331. bool fast_i () {
  1332. return false;
  1333. }
  1334. static
  1335. BOOST_UBLAS_INLINE
  1336. bool fast_j () {
  1337. return true;
  1338. }
  1339. // Iterating storage elements
  1340. template<class I>
  1341. static
  1342. BOOST_UBLAS_INLINE
  1343. void increment_i (I &it, size_type /* size_i */, size_type size_j) {
  1344. it += size_j;
  1345. }
  1346. template<class I>
  1347. static
  1348. BOOST_UBLAS_INLINE
  1349. void increment_i (I &it, difference_type n, size_type /* size_i */, size_type size_j) {
  1350. it += n * size_j;
  1351. }
  1352. template<class I>
  1353. static
  1354. BOOST_UBLAS_INLINE
  1355. void decrement_i (I &it, size_type /* size_i */, size_type size_j) {
  1356. it -= size_j;
  1357. }
  1358. template<class I>
  1359. static
  1360. BOOST_UBLAS_INLINE
  1361. void decrement_i (I &it, difference_type n, size_type /* size_i */, size_type size_j) {
  1362. it -= n * size_j;
  1363. }
  1364. template<class I>
  1365. static
  1366. BOOST_UBLAS_INLINE
  1367. void increment_j (I &it, size_type /* size_i */, size_type /* size_j */) {
  1368. ++ it;
  1369. }
  1370. template<class I>
  1371. static
  1372. BOOST_UBLAS_INLINE
  1373. void increment_j (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1374. it += n;
  1375. }
  1376. template<class I>
  1377. static
  1378. BOOST_UBLAS_INLINE
  1379. void decrement_j (I &it, size_type /* size_i */, size_type /* size_j */) {
  1380. -- it;
  1381. }
  1382. template<class I>
  1383. static
  1384. BOOST_UBLAS_INLINE
  1385. void decrement_j (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1386. it -= n;
  1387. }
  1388. // Triangular access
  1389. static
  1390. BOOST_UBLAS_INLINE
  1391. size_type triangular_size (size_type size_i, size_type size_j) {
  1392. size_type size = (std::max) (size_i, size_j);
  1393. // Guard against size_type overflow - simplified
  1394. BOOST_UBLAS_CHECK (size == 0 || size / 2 < (std::numeric_limits<size_type>::max) () / size /* +1/2 */, bad_size ());
  1395. return ((size + 1) * size) / 2;
  1396. }
  1397. static
  1398. BOOST_UBLAS_INLINE
  1399. size_type lower_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1400. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1401. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1402. BOOST_UBLAS_CHECK (i >= j, bad_index ());
  1403. detail::ignore_unused_variable_warning(size_i);
  1404. detail::ignore_unused_variable_warning(size_j);
  1405. // FIXME size_type overflow
  1406. // sigma_i (i + 1) = (i + 1) * i / 2
  1407. // i = 0 1 2 3, sigma = 0 1 3 6
  1408. return ((i + 1) * i) / 2 + j;
  1409. }
  1410. static
  1411. BOOST_UBLAS_INLINE
  1412. size_type upper_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1413. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1414. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1415. BOOST_UBLAS_CHECK (i <= j, bad_index ());
  1416. // FIXME size_type overflow
  1417. // sigma_i (size - i) = size * i - i * (i - 1) / 2
  1418. // i = 0 1 2 3, sigma = 0 4 7 9
  1419. return (i * (2 * (std::max) (size_i, size_j) - i + 1)) / 2 + j - i;
  1420. }
  1421. // Major and minor indices
  1422. static
  1423. BOOST_UBLAS_INLINE
  1424. size_type index_M (size_type index1, size_type /* index2 */) {
  1425. return index1;
  1426. }
  1427. static
  1428. BOOST_UBLAS_INLINE
  1429. size_type index_m (size_type /* index1 */, size_type index2) {
  1430. return index2;
  1431. }
  1432. static
  1433. BOOST_UBLAS_INLINE
  1434. size_type size_M (size_type size_i, size_type /* size_j */) {
  1435. return size_i;
  1436. }
  1437. static
  1438. BOOST_UBLAS_INLINE
  1439. size_type size_m (size_type /* size_i */, size_type size_j) {
  1440. return size_j;
  1441. }
  1442. };
  1443. // This functor defines storage layout and it's properties
  1444. // matrix (i,j) -> storage [i + j * size_i]
  1445. template <class Z, class D>
  1446. struct basic_column_major {
  1447. typedef Z size_type;
  1448. typedef D difference_type;
  1449. typedef column_major_tag orientation_category;
  1450. typedef basic_row_major<Z,D> transposed_layout;
  1451. static
  1452. BOOST_UBLAS_INLINE
  1453. size_type storage_size (size_type size_i, size_type size_j) {
  1454. // Guard against size_type overflow
  1455. BOOST_UBLAS_CHECK (size_i == 0 || size_j <= (std::numeric_limits<size_type>::max) () / size_i, bad_size ());
  1456. return size_i * size_j;
  1457. }
  1458. // Indexing conversion to storage element
  1459. static
  1460. BOOST_UBLAS_INLINE
  1461. size_type element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1462. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1463. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1464. detail::ignore_unused_variable_warning(size_j);
  1465. // Guard against size_type overflow
  1466. BOOST_UBLAS_CHECK (j <= ((std::numeric_limits<size_type>::max) () - i) / size_i, bad_index ());
  1467. return i + j * size_i;
  1468. }
  1469. static
  1470. BOOST_UBLAS_INLINE
  1471. size_type address (size_type i, size_type size_i, size_type j, size_type size_j) {
  1472. BOOST_UBLAS_CHECK (i <= size_i, bad_index ());
  1473. BOOST_UBLAS_CHECK (j <= size_j, bad_index ());
  1474. detail::ignore_unused_variable_warning(size_j);
  1475. // Guard against size_type overflow - address may be size_i past end of storage
  1476. BOOST_UBLAS_CHECK (size_i == 0 || j <= ((std::numeric_limits<size_type>::max) () - i) / size_i, bad_index ());
  1477. return i + j * size_i;
  1478. }
  1479. // Storage element to index conversion
  1480. static
  1481. BOOST_UBLAS_INLINE
  1482. difference_type distance_i (difference_type k, size_type /* size_i */, size_type /* size_j */) {
  1483. return k;
  1484. }
  1485. static
  1486. BOOST_UBLAS_INLINE
  1487. difference_type distance_j (difference_type k, size_type size_i, size_type /* size_j */) {
  1488. return size_i != 0 ? k / size_i : 0;
  1489. }
  1490. static
  1491. BOOST_UBLAS_INLINE
  1492. size_type index_i (difference_type k, size_type size_i, size_type /* size_j */) {
  1493. return size_i != 0 ? k % size_i : 0;
  1494. }
  1495. static
  1496. BOOST_UBLAS_INLINE
  1497. size_type index_j (difference_type k, size_type size_i, size_type /* size_j */) {
  1498. return size_i != 0 ? k / size_i : 0;
  1499. }
  1500. static
  1501. BOOST_UBLAS_INLINE
  1502. bool fast_i () {
  1503. return true;
  1504. }
  1505. static
  1506. BOOST_UBLAS_INLINE
  1507. bool fast_j () {
  1508. return false;
  1509. }
  1510. // Iterating
  1511. template<class I>
  1512. static
  1513. BOOST_UBLAS_INLINE
  1514. void increment_i (I &it, size_type /* size_i */, size_type /* size_j */) {
  1515. ++ it;
  1516. }
  1517. template<class I>
  1518. static
  1519. BOOST_UBLAS_INLINE
  1520. void increment_i (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1521. it += n;
  1522. }
  1523. template<class I>
  1524. static
  1525. BOOST_UBLAS_INLINE
  1526. void decrement_i (I &it, size_type /* size_i */, size_type /* size_j */) {
  1527. -- it;
  1528. }
  1529. template<class I>
  1530. static
  1531. BOOST_UBLAS_INLINE
  1532. void decrement_i (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
  1533. it -= n;
  1534. }
  1535. template<class I>
  1536. static
  1537. BOOST_UBLAS_INLINE
  1538. void increment_j (I &it, size_type size_i, size_type /* size_j */) {
  1539. it += size_i;
  1540. }
  1541. template<class I>
  1542. static
  1543. BOOST_UBLAS_INLINE
  1544. void increment_j (I &it, difference_type n, size_type size_i, size_type /* size_j */) {
  1545. it += n * size_i;
  1546. }
  1547. template<class I>
  1548. static
  1549. BOOST_UBLAS_INLINE
  1550. void decrement_j (I &it, size_type size_i, size_type /* size_j */) {
  1551. it -= size_i;
  1552. }
  1553. template<class I>
  1554. static
  1555. BOOST_UBLAS_INLINE
  1556. void decrement_j (I &it, difference_type n, size_type size_i, size_type /* size_j */) {
  1557. it -= n* size_i;
  1558. }
  1559. // Triangular access
  1560. static
  1561. BOOST_UBLAS_INLINE
  1562. size_type triangular_size (size_type size_i, size_type size_j) {
  1563. size_type size = (std::max) (size_i, size_j);
  1564. // Guard against size_type overflow - simplified
  1565. BOOST_UBLAS_CHECK (size == 0 || size / 2 < (std::numeric_limits<size_type>::max) () / size /* +1/2 */, bad_size ());
  1566. return ((size + 1) * size) / 2;
  1567. }
  1568. static
  1569. BOOST_UBLAS_INLINE
  1570. size_type lower_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1571. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1572. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1573. BOOST_UBLAS_CHECK (i >= j, bad_index ());
  1574. // FIXME size_type overflow
  1575. // sigma_j (size - j) = size * j - j * (j - 1) / 2
  1576. // j = 0 1 2 3, sigma = 0 4 7 9
  1577. return i - j + (j * (2 * (std::max) (size_i, size_j) - j + 1)) / 2;
  1578. }
  1579. static
  1580. BOOST_UBLAS_INLINE
  1581. size_type upper_element (size_type i, size_type size_i, size_type j, size_type size_j) {
  1582. BOOST_UBLAS_CHECK (i < size_i, bad_index ());
  1583. BOOST_UBLAS_CHECK (j < size_j, bad_index ());
  1584. BOOST_UBLAS_CHECK (i <= j, bad_index ());
  1585. // FIXME size_type overflow
  1586. // sigma_j (j + 1) = (j + 1) * j / 2
  1587. // j = 0 1 2 3, sigma = 0 1 3 6
  1588. return i + ((j + 1) * j) / 2;
  1589. }
  1590. // Major and minor indices
  1591. static
  1592. BOOST_UBLAS_INLINE
  1593. size_type index_M (size_type /* index1 */, size_type index2) {
  1594. return index2;
  1595. }
  1596. static
  1597. BOOST_UBLAS_INLINE
  1598. size_type index_m (size_type index1, size_type /* index2 */) {
  1599. return index1;
  1600. }
  1601. static
  1602. BOOST_UBLAS_INLINE
  1603. size_type size_M (size_type /* size_i */, size_type size_j) {
  1604. return size_j;
  1605. }
  1606. static
  1607. BOOST_UBLAS_INLINE
  1608. size_type size_m (size_type size_i, size_type /* size_j */) {
  1609. return size_i;
  1610. }
  1611. };
  1612. template <class Z>
  1613. struct basic_full {
  1614. typedef Z size_type;
  1615. template<class L>
  1616. static
  1617. BOOST_UBLAS_INLINE
  1618. size_type packed_size (L, size_type size_i, size_type size_j) {
  1619. return L::storage_size (size_i, size_j);
  1620. }
  1621. static
  1622. BOOST_UBLAS_INLINE
  1623. bool zero (size_type /* i */, size_type /* j */) {
  1624. return false;
  1625. }
  1626. static
  1627. BOOST_UBLAS_INLINE
  1628. bool one (size_type /* i */, size_type /* j */) {
  1629. return false;
  1630. }
  1631. static
  1632. BOOST_UBLAS_INLINE
  1633. bool other (size_type /* i */, size_type /* j */) {
  1634. return true;
  1635. }
  1636. // FIXME: this should not be used at all
  1637. static
  1638. BOOST_UBLAS_INLINE
  1639. size_type restrict1 (size_type i, size_type /* j */) {
  1640. return i;
  1641. }
  1642. static
  1643. BOOST_UBLAS_INLINE
  1644. size_type restrict2 (size_type /* i */, size_type j) {
  1645. return j;
  1646. }
  1647. static
  1648. BOOST_UBLAS_INLINE
  1649. size_type mutable_restrict1 (size_type i, size_type /* j */) {
  1650. return i;
  1651. }
  1652. static
  1653. BOOST_UBLAS_INLINE
  1654. size_type mutable_restrict2 (size_type /* i */, size_type j) {
  1655. return j;
  1656. }
  1657. };
  1658. namespace detail {
  1659. template < class L >
  1660. struct transposed_structure {
  1661. typedef typename L::size_type size_type;
  1662. template<class LAYOUT>
  1663. static
  1664. BOOST_UBLAS_INLINE
  1665. size_type packed_size (LAYOUT l, size_type size_i, size_type size_j) {
  1666. return L::packed_size(l, size_j, size_i);
  1667. }
  1668. static
  1669. BOOST_UBLAS_INLINE
  1670. bool zero (size_type i, size_type j) {
  1671. return L::zero(j, i);
  1672. }
  1673. static
  1674. BOOST_UBLAS_INLINE
  1675. bool one (size_type i, size_type j) {
  1676. return L::one(j, i);
  1677. }
  1678. static
  1679. BOOST_UBLAS_INLINE
  1680. bool other (size_type i, size_type j) {
  1681. return L::other(j, i);
  1682. }
  1683. template<class LAYOUT>
  1684. static
  1685. BOOST_UBLAS_INLINE
  1686. size_type element (LAYOUT /* l */, size_type i, size_type size_i, size_type j, size_type size_j) {
  1687. return L::element(typename LAYOUT::transposed_layout(), j, size_j, i, size_i);
  1688. }
  1689. static
  1690. BOOST_UBLAS_INLINE
  1691. size_type restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
  1692. return L::restrict2(j, i, size2, size1);
  1693. }
  1694. static
  1695. BOOST_UBLAS_INLINE
  1696. size_type restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
  1697. return L::restrict1(j, i, size2, size1);
  1698. }
  1699. static
  1700. BOOST_UBLAS_INLINE
  1701. size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
  1702. return L::mutable_restrict2(j, i, size2, size1);
  1703. }
  1704. static
  1705. BOOST_UBLAS_INLINE
  1706. size_type mutable_restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
  1707. return L::mutable_restrict1(j, i, size2, size1);
  1708. }
  1709. static
  1710. BOOST_UBLAS_INLINE
  1711. size_type global_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1712. return L::global_restrict2(index2, size2, index1, size1);
  1713. }
  1714. static
  1715. BOOST_UBLAS_INLINE
  1716. size_type global_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1717. return L::global_restrict1(index2, size2, index1, size1);
  1718. }
  1719. static
  1720. BOOST_UBLAS_INLINE
  1721. size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1722. return L::global_mutable_restrict2(index2, size2, index1, size1);
  1723. }
  1724. static
  1725. BOOST_UBLAS_INLINE
  1726. size_type global_mutable_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1727. return L::global_mutable_restrict1(index2, size2, index1, size1);
  1728. }
  1729. };
  1730. }
  1731. template <class Z>
  1732. struct basic_lower {
  1733. typedef Z size_type;
  1734. typedef lower_tag triangular_type;
  1735. template<class L>
  1736. static
  1737. BOOST_UBLAS_INLINE
  1738. size_type packed_size (L, size_type size_i, size_type size_j) {
  1739. return L::triangular_size (size_i, size_j);
  1740. }
  1741. static
  1742. BOOST_UBLAS_INLINE
  1743. bool zero (size_type i, size_type j) {
  1744. return j > i;
  1745. }
  1746. static
  1747. BOOST_UBLAS_INLINE
  1748. bool one (size_type /* i */, size_type /* j */) {
  1749. return false;
  1750. }
  1751. static
  1752. BOOST_UBLAS_INLINE
  1753. bool other (size_type i, size_type j) {
  1754. return j <= i;
  1755. }
  1756. template<class L>
  1757. static
  1758. BOOST_UBLAS_INLINE
  1759. size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
  1760. return L::lower_element (i, size_i, j, size_j);
  1761. }
  1762. // return nearest valid index in column j
  1763. static
  1764. BOOST_UBLAS_INLINE
  1765. size_type restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
  1766. return (std::max)(j, (std::min) (size1, i));
  1767. }
  1768. // return nearest valid index in row i
  1769. static
  1770. BOOST_UBLAS_INLINE
  1771. size_type restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
  1772. return (std::max)(size_type(0), (std::min) (i+1, j));
  1773. }
  1774. // return nearest valid mutable index in column j
  1775. static
  1776. BOOST_UBLAS_INLINE
  1777. size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
  1778. return (std::max)(j, (std::min) (size1, i));
  1779. }
  1780. // return nearest valid mutable index in row i
  1781. static
  1782. BOOST_UBLAS_INLINE
  1783. size_type mutable_restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
  1784. return (std::max)(size_type(0), (std::min) (i+1, j));
  1785. }
  1786. // return an index between the first and (1+last) filled row
  1787. static
  1788. BOOST_UBLAS_INLINE
  1789. size_type global_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
  1790. return (std::max)(size_type(0), (std::min)(size1, index1) );
  1791. }
  1792. // return an index between the first and (1+last) filled column
  1793. static
  1794. BOOST_UBLAS_INLINE
  1795. size_type global_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
  1796. return (std::max)(size_type(0), (std::min)(size2, index2) );
  1797. }
  1798. // return an index between the first and (1+last) filled mutable row
  1799. static
  1800. BOOST_UBLAS_INLINE
  1801. size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
  1802. return (std::max)(size_type(0), (std::min)(size1, index1) );
  1803. }
  1804. // return an index between the first and (1+last) filled mutable column
  1805. static
  1806. BOOST_UBLAS_INLINE
  1807. size_type global_mutable_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
  1808. return (std::max)(size_type(0), (std::min)(size2, index2) );
  1809. }
  1810. };
  1811. // the first row only contains a single 1. Thus it is not stored.
  1812. template <class Z>
  1813. struct basic_unit_lower : public basic_lower<Z> {
  1814. typedef Z size_type;
  1815. typedef unit_lower_tag triangular_type;
  1816. template<class L>
  1817. static
  1818. BOOST_UBLAS_INLINE
  1819. size_type packed_size (L, size_type size_i, size_type size_j) {
  1820. // Zero size strict triangles are bad at this point
  1821. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0, bad_index ());
  1822. return L::triangular_size (size_i - 1, size_j - 1);
  1823. }
  1824. static
  1825. BOOST_UBLAS_INLINE
  1826. bool one (size_type i, size_type j) {
  1827. return j == i;
  1828. }
  1829. static
  1830. BOOST_UBLAS_INLINE
  1831. bool other (size_type i, size_type j) {
  1832. return j < i;
  1833. }
  1834. template<class L>
  1835. static
  1836. BOOST_UBLAS_INLINE
  1837. size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
  1838. // Zero size strict triangles are bad at this point
  1839. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0 && i != 0, bad_index ());
  1840. return L::lower_element (i-1, size_i - 1, j, size_j - 1);
  1841. }
  1842. static
  1843. BOOST_UBLAS_INLINE
  1844. size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
  1845. return (std::max)(j+1, (std::min) (size1, i));
  1846. }
  1847. static
  1848. BOOST_UBLAS_INLINE
  1849. size_type mutable_restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
  1850. return (std::max)(size_type(0), (std::min) (i, j));
  1851. }
  1852. // return an index between the first and (1+last) filled mutable row
  1853. static
  1854. BOOST_UBLAS_INLINE
  1855. size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
  1856. return (std::max)(size_type(1), (std::min)(size1, index1) );
  1857. }
  1858. // return an index between the first and (1+last) filled mutable column
  1859. static
  1860. BOOST_UBLAS_INLINE
  1861. size_type global_mutable_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
  1862. BOOST_UBLAS_CHECK( size2 >= 1 , external_logic() );
  1863. return (std::max)(size_type(0), (std::min)(size2-1, index2) );
  1864. }
  1865. };
  1866. // the first row only contains no element. Thus it is not stored.
  1867. template <class Z>
  1868. struct basic_strict_lower : public basic_unit_lower<Z> {
  1869. typedef Z size_type;
  1870. typedef strict_lower_tag triangular_type;
  1871. template<class L>
  1872. static
  1873. BOOST_UBLAS_INLINE
  1874. size_type packed_size (L, size_type size_i, size_type size_j) {
  1875. // Zero size strict triangles are bad at this point
  1876. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0, bad_index ());
  1877. return L::triangular_size (size_i - 1, size_j - 1);
  1878. }
  1879. static
  1880. BOOST_UBLAS_INLINE
  1881. bool zero (size_type i, size_type j) {
  1882. return j >= i;
  1883. }
  1884. static
  1885. BOOST_UBLAS_INLINE
  1886. bool one (size_type /*i*/, size_type /*j*/) {
  1887. return false;
  1888. }
  1889. static
  1890. BOOST_UBLAS_INLINE
  1891. bool other (size_type i, size_type j) {
  1892. return j < i;
  1893. }
  1894. template<class L>
  1895. static
  1896. BOOST_UBLAS_INLINE
  1897. size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
  1898. // Zero size strict triangles are bad at this point
  1899. BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0 && i != 0, bad_index ());
  1900. return L::lower_element (i-1, size_i - 1, j, size_j - 1);
  1901. }
  1902. static
  1903. BOOST_UBLAS_INLINE
  1904. size_type restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
  1905. return basic_unit_lower<Z>::mutable_restrict1(i, j, size1, size2);
  1906. }
  1907. static
  1908. BOOST_UBLAS_INLINE
  1909. size_type restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
  1910. return basic_unit_lower<Z>::mutable_restrict2(i, j, size1, size2);
  1911. }
  1912. // return an index between the first and (1+last) filled row
  1913. static
  1914. BOOST_UBLAS_INLINE
  1915. size_type global_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1916. return basic_unit_lower<Z>::global_mutable_restrict1(index1, size1, index2, size2);
  1917. }
  1918. // return an index between the first and (1+last) filled column
  1919. static
  1920. BOOST_UBLAS_INLINE
  1921. size_type global_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
  1922. return basic_unit_lower<Z>::global_mutable_restrict2(index1, size1, index2, size2);
  1923. }
  1924. };
  1925. template <class Z>
  1926. struct basic_upper : public detail::transposed_structure<basic_lower<Z> >
  1927. {
  1928. typedef upper_tag triangular_type;
  1929. };
  1930. template <class Z>
  1931. struct basic_unit_upper : public detail::transposed_structure<basic_unit_lower<Z> >
  1932. {
  1933. typedef unit_upper_tag triangular_type;
  1934. };
  1935. template <class Z>
  1936. struct basic_strict_upper : public detail::transposed_structure<basic_strict_lower<Z> >
  1937. {
  1938. typedef strict_upper_tag triangular_type;
  1939. };
  1940. }}}
  1941. #endif